Slave Source Code For Monte Carlo Example - Quadrant 1
/* parallel Monte Carlo area calculation */
/* slave program for first quadrant */
#include <stdio.h>
#include <math.h>
#include <pvm3.h>
#define steps 50000
#define xmin 0.0
#define xmax 0.5
#define ymin 0.0
#define ymax 0.5
double f(double x) /* the function for the */
{ /* border in this quadrant */
return(-sqrt(-x*x+x)+0.5);
}
main()
{
int i, ptid, bufid, dum, type, send[1];
double x, y;
srand48(pvm_mytid()); /* seed the number generator */
/* not a good way of doing it */
ptid = pvm_parent();
do
{
send[0]=0;
for (i=1; i <= steps; i++)
{
x= drand48()*(xmax+xmin); /* random points in the */
y= drand48()*(ymax+ymin); /* quadrant */
if (f(x) <= y) send[0]++; /* point is inside the figure */
}
pvm_initsend(PvmDataDefault); /* send result back to master */
pvm_pkint(send, 1, 1);
pvm_send(ptid, 1);
bufid=pvm_recv(ptid, -1); /* any message from master */
pvm_bufinfo(bufid, &dum, &type, &dum); /* more work ? */
}while(type);
pvm_exit; /* type=0 means we are done */
}
Download: A html-free code you can save
Back: Slaves for Monte Carlo example