Go Back
// This slave process will take the first and last values in a domain and
// calculate a chunk of Gregory's Formula. If the numbers are a and b, it
// calculates +/- [1/a - 1/(a+2) + 1/(a+4) - ...] until 1/b is reached.
// It then sends the result back to the master (aka 'da masta').
#include <stdio.h>
#include "/usr/local/pvm3/include/pvm3.h"
int main()
{
int mytid, nproc, who;
int master, msgtype;
double tempsum;
long i, sign;
unsigned long first, last;
// Enroll in PVM
mytid = pvm_mytid();
// Receive data from master
msgtype = 314;
pvm_recv(-1, msgtype);
// Get task ID number and first and last value in range
pvm_upkint(&who, 1, 1);
pvm_upklong(&first, 1, 1);
pvm_upklong(&last, 1, 1);
// Start with odd numbers, then find the sign of the first term.
if (first % 2 == 0) first++;
if (sign % 4 == 1) sign = -4;
else sign = 4;
// Initialize partial sum to 0, then go ahead with the formula.
tempsum = 0;
for (i = first; i < last; i += 2)
{
tempsum += (double)sign / (double)i;
sign *= -1;
}
// Send data back to master
pvm_initsend(PvmDataDefault);
pvm_pkint(&who, 1, 1);
pvm_pkdouble(&tempsum, 1, 1);
msgtype = 2718;
master = pvm_parent();
pvm_send(master, msgtype);
// Exit PVM, then exit the program.
pvm_exit();
return 0;
}