Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.

Channels ▼

James Reinders

Dr. Dobb's Bloggers

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/task_scheduler_init.h"
#include "tbb/blocked_range.h"

using namespace std;
using namespace tbb;

#define Nthreads 2

#define GrainSize      10000

long long num_steps =  1000000000;

class MyPi {
                double *const my_step;
                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);
                task_scheduler_init init(task_scheduler_init::deferred);

                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;


Related Reading

More Insights

Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

Disqus Tips 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.