/* sr_slave.c: worker for Simpson's 1/3 Rule. receives the interval
   index from the sr_master, calcualtes the interval's area and sends
   this information back to the sr_master */

#include "mypvm.h"

main()
{

int ptid, precv, count, nworkers, i;
double h, back, x, xf, start, end;

/*get parent id, so we know where to receive from */
ptid = pvm_parent();


/* define parameters for integration */
h = (b - a) / (double) STEPS;


/* start receiving from administrator */
for (;;) {

  /* receive count=current iteration number */
  precv = pvm_recv(ptid, -1);
  pvm_upkint(&count, 1, 1);
  pvm_upkint(&nworkers, 1, 1);
  
  start = a + ((double) count * (b-a) / (double) nworkers);

  if (count == nworkers - 1)
    end = a + ((double) (count + 1) * (b-a) / (double) nworkers) - h;
  else
    end = a + ((double) (count + 1) * (b-a) / (double) nworkers);

  back = 0.0;

  for (i = 1; i <= ((STEPS-2)/(2*nworkers)); i++) {
    x = start + h * (double) (2*i-1);
    xf = start + h * (double) (2*i);
    back += 4.0*(cos(50.0*x)*exp(-3.0*x)+1.0)+2.0*(cos(50.0*xf)*exp(-3.0*xf)+1.0);
  }

  /* send back count=current step #, step contribution to total area */
  pvm_initsend(PvmDataDefault);
  pvm_pkdouble(&back, 1, 1);
  pvm_send(ptid, count);

} /* end infinite for */

} /* end sr_slave */






