Velocity Reviews > Parallel program to calculate PI

# Parallel program to calculate PI

user923005
Guest
Posts: n/a

 05-13-2008
On May 13, 10:06*am, Richard Heathfield <(E-Mail Removed)> wrote:
[snip]
> I once saw a wonderful pi calculation method on the Web (to which I can't
> find a URL right now) - it was a Monte Carlo method, which used millions
> of digits of pi as a PRNG, and concluded... that pi is about 3. Fabulous!

You can also calculate pi by dropping needles on a grid and seeing if
they touch the lines.
http://www.angelfire.com/wa/hurben/buff.html

I have calculated pi to several thousand digits by numerical
integration of arctangent identities.

However, I used C++ so it is not even tangentially topical.

So let's revive the Dik Winter pi programs:

The minimalistic C source code to calculate pi to 32372 digits reads:

/* Calculation of pi to 32372 decimal digits */
/* Size of program: 152 characters */
/* After Dik T. Winter, CWI Amsterdam */
unsigned a=1e4,b,c=113316,d,e,f[113316],g,h,i;
main(){for(;b=c,c-=14;i=printf("%04d",e+d/a),e=d%a)
while(g=--b*2)d=h*b+a*(i?f[b]:a/5),h=d/--g,f[b]=d-g*h;}

An even shorter version creates the first 16276 digits of pi:

/* Calculation of pi to 16276 decimal digits */
/* Size of program: 143 characters */
/* After Dik T. Winter, CWI Amsterdam */
int a=1e4,b,c=56980,d,e,f[56980],g,h,i;
main(){for(;b=c,c-=14;i=printf("%04d",e+d/a),e=d%a)
while(g=--b*2)d=h*b+a*(i?f[b]:a/5),h=d/--g,f[b]=d%g;}

Prime Mover
Guest
Posts: n/a

 05-13-2008
Very interesting discussions!

Have anyone used header files such as omp.h or mpi.h that seem
to call functions for parallel computing in C?

I promise this would be the last question about parallel programming
here

Prime Mover
Guest
Posts: n/a

 05-13-2008
On 13 maio, 16:30, Prime Mover <(E-Mail Removed)> wrote:
> Very interesting discussions!
>
> Have anyone used header files such as omp.h or mpi.h that seem
> to call functions for parallel computing in C?
>
> I promise this would be the last question about parallel programming
> here

I forgot to ask if anyone could share these files with me, because I

Thank you again.

Keith Thompson
Guest
Posts: n/a

 05-13-2008
Prime Mover <(E-Mail Removed)> writes:
> Very interesting discussions!
>
> Have anyone used header files such as omp.h or mpi.h that seem
> to call functions for parallel computing in C?
>
> I promise this would be the last question about parallel programming
> here

Assuming that omp.h is for OpenMP, see <http://openmp.org/>.

For MPI, see comp.parallel.mpi.

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

user923005
Guest
Posts: n/a

 05-14-2008
On May 13, 2:57*pm, Richard Heathfield <(E-Mail Removed)> wrote:
> user923005 said:
>
> > On May 13, 10:06 am, Richard Heathfield <(E-Mail Removed)> wrote:
> > [snip]
> >> I once saw a wonderful pi calculation method on the Web (to which I
> >> can't find a URL right now) - it was a Monte Carlo method, which used
> >> millions of digits of pi as a PRNG, and concluded... that pi is about 3..
> >> Fabulous!

>
> > You can also calculate pi by dropping needles on a grid and seeing if
> > they touch the lines.

>
> Yes, I know. I once demonstrated this to my kids, using the tiles on our
> kitchen floor as the grid. I just asked one of them what value we came up
> with, and he'd completely forgotten not only the value but the entire
> demonstration. So it's time to do it all over again!
>
> <snip>
>
> > So let's revive the Dik Winter pi programs:

>
> Please don't, or I'll turn green.

Here is a version with less UB:

#include <stdio.h>
static unsigned a = 10000,
b,
c = 113316,
d,
e,
f[113316],
g,
h,
i;
int main(void)
{
for (; b = c, c -= 14; i = (unsigned) printf("%04u", e + d / a), e
= d % a)
while ((g = --b * 2))
d = h * b + a * (i ? f[b] : a / 5), h = d / --g, f[b] = d
- g * h;
return 0;
}

dSpam@arcor.de
Guest
Posts: n/a

 05-15-2008
On 13 Mai, 16:28, Prime Mover <(E-Mail Removed)> wrote:
> My (understading) problem starts in the line 5 of the pseudo-code:
> 5. find out if I am MASTER or WORKER

Read the description of fork() (the absence of which in the C standard
documents lets you conclude that it's not part of the C standard