# Happy Pi (Approximation) Day

July 21, 2009

"Happy Pi Day," a friend told me today. Pi Day is March 14th (3.14) to many, but since 22/7 is pi for most of the world he is celebrating again. Additionally, most of the world writes DAY/MONTH hence 22/7. For much of the world, March 14 is 14/3 which is more than 48% too high for pi. Alas, since my friend feels he should celebrate pi day with pizza - his motives for an additional pi day may not be entirely unselfish.
To celebrate - here is a sample of how TBB  can be used to compute an approximation to pi using a parallel reduction. The code is portable virtually everywhere (TBB open source project has been widely ported). I'll have to post a version using the draft standard "lambda" feature and the soon to be released TBB 2.2 - I should have that before the next pi day.  Question is: do I need to wait for 3/14 or is there another approximation I could use.
Happy Pi Day - here is the TBB code:

#include <stdio.h>
#include <time.h>
#include <cassert>
#include <cstdio>
#include <cstring>
#include <math.h>
#include <cstdlib>
#include <cctype>
#include "tbb/parallel_reduce.h"
#include "tbb/blocked_range.h"

using namespace std;
using namespace tbb;

#define GrainSize      10000

long long num_steps =  1000000000;

class MyPi {
double *const my_step;
public:
double sum;
void operator()( const blocked_range<size_t>& r ) {
double step = *my_step;
double x;
for (size_t i=r.begin(); i!=r.end(); ++i)
{
x = (i + .5)*step;
sum = sum + 4.0/(1.+ x*x);
}
}

MyPi( MyPi& x, split ) : my_step(x.my_step), sum(0) {}
void join( const MyPi& y ) {sum += y.sum;}
MyPi(double *const step) : my_step(step), sum(0) {}
};

int main(int argc, char* argv[])
{
clock_t start, stop;
double pi;
double width = 1./(double)num_steps;
printf("Approximating pi with %ld rectangles\n\n", num_steps);

MyPi step((double *const)&width);

start = clock();
parallel_reduce(blocked_range<size_t>(0,num_steps,GrainSize), step);
pi = step.sum*width;
stop = clock();

printf("The value of PI is %15.12f\n",pi);

printf("The time to calculate PI was %f seconds\n",((double)(stop - start)/1000.0));

return 0;

}

### More Insights

 To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.

# First C Compiler Now on Github

The earliest known C compiler by the legendary Dennis Ritchie has been published on the repository.

# HTML5 Mobile Development: Seven Good Ideas (and Three Bad Ones)

HTML5 Mobile Development: Seven Good Ideas (and Three Bad Ones)

# Building Bare Metal ARM Systems with GNU

All you need to know to get up and running... and programming on ARM

# Amazon's Vogels Challenges IT: Rethink App Dev

Amazon Web Services CTO says promised land of cloud computing requires a new generation of applications that follow different principles.

# How to Select a PaaS Partner

Eventually, the vast majority of Web applications will run on a platform-as-a-service, or PaaS, vendor's infrastructure. To help sort out the options, we sent out a matrix with more than 70 decision points to a variety of PaaS providers.

More "Best of the Web" >>