Happy Pi Day!
In 2017, we created a Pi API that served 750 billion digits. To celebrate 2018 Pi Day, we calculate 1 trillion digits of Pi and made it available via the Pi API!
We used the Pi API to build some fun demos, including listening and visualizing Pi digits.
This is just for fun. There is NO SLA. It might be turned off at any time!
Visualize
This demo visualizes digit transitions in Pi using D3.js. Each transition draws a line from a digit in the circle to another digit. Once you have a large amount of digits you can see which digits transition more often and which digits are more common.
Listen
This demo plays midi piano notes for each digit of Pi. Each digit plays a different note so you can get to know Pi via music!
Google Home
We also created a demo Google Home assistant! If you have a Google Home, you can:
- Say: “Ok Google, talk to Pi Digit agent.”
- Ask: “What is the 10th digit of Pi?”
Calculating Pi
In 2017, we used y-cruncher to calculate 750 billion digits of Pi on Google Compute Engine. We used a 64-core instance, with 416GB of RAM, and tons of Local SSD + Persistent SSD.
Sometimes, you just need a big honkin' VM @googlecloud pic.twitter.com/WUtLIhW4bs
— Greg Wilson (@gregsramblings) March 5, 2017
At its peak CPU utilization, it consumed all 64 cores, and 5+TB of SSD storage.
Consumed all 64 cores on a @googlecloud vm, of 8x375GB Local SSD & using 382GB of 416GB RAM for some serious computation pic.twitter.com/Fzptt1nDCw
— Ray Tsang (@saturnism) March 6, 2017
In 2018, we were able to recalculate 750 billion digits in just 10 hours!
Happy #PiDay! We just calculated 750-billion digits of Pi calculation in 10 hours (compared to 2-days it took in 2017) using something we'll announce soon on @GCPcloud. Stayed tuned! #PiDay2018 pic.twitter.com/lFfSv63EzL
— Ray Tsang (@saturnism) March 14, 2018
Finally, we also calculated 1 trillion digits of Pi using a 96-core 14TB n1-megamem-96
machine.
Near full utilization of a 96-cores & 1.4TB of RAM n1-megamem-96 instance on @GCPcloud pic.twitter.com/C6JxMHrrzj
— Ray Tsang (@saturnism) March 13, 2018
Serving the API
We developed the core backend using gRPC and Go. The gRPC service has two methods - one is a unary request/response, and another that takes advantage of server side streaming.
We then use grpc-gateway to expose the unary request/response as RESTful service.
The service definition looks like this:
syntax = "proto3";
package piaas;
import "google/api/annotations.proto";
message Range {
int64 start = 1;
int64 numberOfDigits = 2;
int64 chunkSize = 3;
}
message ByteArray {
string content = 1;
}
service PiaaS {
rpc Get(Range) returns (ByteArray) {
option (google.api.http) = {
get: "/v1/pi"
};
}
rpc Stream(Range) returns (stream ByteArray);
}
The RESTful API is also registered and managed by Google Cloud Endpoint, so that we can easily monitor and manage service deployments.
love the stats that @googlecloud endpoints provides me. used the esp proxy to front my restful service. pic.twitter.com/5Gk2yxaUQx
— Ray Tsang (@saturnism) March 13, 2017
.@grpcio + @googlecloud endpoints and #GKE makes for a *very* scalable experience 🚀 pic.twitter.com/FRDVjjXiJl
— Francesc (@francesc) March 14, 2017
The services are deployed into a Google Container Engine (managed Kubernetes) cluster. The API endpoint, api.pi.delivery
is exposed via a Kubernetes Ingress that’ll automatically create a Global HTTP(s) Load Balancer.
We used kube-logo to automatically provision a SSL certificate from Let’s Encrypt for HTTPS.
Writing the Assistant
We used Dialogflow and Google Cloud Functions to develop the demo Google Home assistant. Learn more technical details on Google Home helps you learn the digits of Pi.
Serving This Site
Finally, this site is hosted on Firebase Hosting, which not only automatically serves the static content via a CDN, it also automatically provisions a SSL certificate for custom domain!
Who are We?
We are part of the Google Cloud Platform Developer Advocacy team.
This demo was created by Francesc Campoy, Guillaume Laforge, Ian Lewis, Ray Tsang, Sandeep Dinesh
In the past, different members of the team worked on different Pi Day demos!
In 2016, we calculated 500 billion digits of Pi and made it searchable. The indexing peaked at 2-million writes per second. You can learn more how we did that in the blog Calculating and searching 500 billion digits of Pi, by Francesc Campoy, Jen Tong, Ray Tsang, Sara Robinson
In 2015, we calculated 250 billion digits of Pi, by Greg Wilson, Ray Tsang
/v1/pi
Get Pi Digits
Returns digits of Pi based on the start
digit position and numberOfDigits
.
We may require an API key in the future & or turn off the service. If you really want to use it, let us know!
$.get("https://api.pi.delivery/v1/pi", {
start: 0,
numberOfDigits: 100
}, function(data) {
console.log(data);
});
<script src="https://api.pi.delivery/js/pi.js"></script>
<script>
var pi = new Pi();
pi.get(0, 100, function(digits) {
alert(digits);
});
</script>
<script src="https://api.pi.delivery/js/pi.js"></script>
<script>
var stream = new PiStream();
stream.listen(function(e) {
console.log(e.detail.position);
console.log(e.detail.digit);
});
stream.start();
</script>
{
"start": 0,
"numberOfDigits": 100
}
{
"content": "314159...",
}
Fetching Digits
This demo uses the REST API to fetch 100 digits of Pi from the specified starting point.
Press 'Fetch'
var pi = new Pi();
pi.get(0, 100, function(digits) {
...
});
{
"start": 0,
"numberOfDigits": 100
}
{
"content": 314159....
}
Streaming Digit
This demo uses the streaming API to stream digits into the page.
var streamer = new PiStream();
streamer.listen(function(e) {
// e.detail.position
// e.detail.digit
});
// optional (delay in milliseconds):
// streamer.delay(314);
streamer.start();