MC logo

Parallel Integration Slave 2

  Some PVM Examples

parallel2_slave.c
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <pvm3.h>
#include "func.h"

/* 
 * Find the area to the tolerance.
 */
static double find(double x1, double y1, double x2, double y2, double tol)
{
        /* Compute the midpoint from the funcion. */
        double midx = (x1 + x2) / 2;
        double midy = f(midx);

        /* Estimate the midpoint from the y value. */
        double midest = (y1 + y2) / 2;

        /* See if we're getting close. */
        if(fabs(midy - midest) <= tol)
                /* Will do.  Compute the area using the midy, since we
                   found it.  Two trapazoids algebraically simplified. */
                return 0.25*(x2-x1)*(y1 + y2 + 2*midy);
        else 
                /* Subdivide and try again. */
                return find(x1, y1, midx, midy, tol) +
                        find(midx, midy, x2, y2, tol);
}

#define INITMAX 20

main()
{
        int ptid;
        char buf[100];
        int argc;
        char *argv[INITMAX];

        ptid = pvm_parent();

        pvm_recv(ptid, 1);
        pvm_unpackf(" %d ", &argc);
        int n;
        for(n = 0; n < argc; n++) {
                int len;
                pvm_upkint(&len, 1, 1);
                argv[n] = malloc(len + 1);
                pvm_upkstr(argv[n]);
        }
        finit(argc, argv);

        /* Execute regions until all have been taken care of. */
        double tot = 0.0;
        while(1) {
                int bid = pvm_recvinfo
                        ("work", 0, PvmMboxFirstAvail|PvmMboxReadAndDelete);
                if(bid < 0) break;
                double startx, endx, tol;
                pvm_unpackf("%lf %lf %lf ", &startx, &endx, &tol);
                tot += find(startx, f(startx), endx, f(endx), tol);
        }

        // Return the result.
        pvm_initsend( PvmDataDefault );
        pvm_pkdouble(&tot, 1, 1);
        pvm_send(ptid, 3);

        pvm_exit();
        exit(0);
}