MC logo

Parallel Integration Slave

  Some PVM Examples

parallel_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);


        /* Find our portion. */
        double startx, endx, tol;
        int rn = pvm_recv(ptid, 2);
        pvm_unpackf("%lf %lf %lf ", &startx, &endx, &tol);

        /* Compute it and send it. */
        double res = find(startx, f(startx), endx, f(endx), tol);

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

        pvm_exit();
        exit(0);
}