#include #include #include "pvm3.h" #include "constants.h" int main(void){ int mytid, tids[MAX_CHILD], n, nproc, numt, i, who; float data[DATA_SIZE], result[MAX_CHILD]; /* enroll in pvm */ mytid = pvm_mytid(); /* Set number of slaves to start */ do{ printf("How many slave programs (1-%d)?", MAX_CHILD); scanf("%d", &nproc); }while((nproc > MAX_CHILD) || (nproc <= 0)); /* start up slave tasks */ numt = pvm_spawn(SLAVENAME, NULL, 0, "", nproc, tids); if( numt < nproc ){ printf("Trouble spawning slaves. Aborting. Error codes are:\n"); for( i = numt ; i < nproc ; i++ ) { printf("TID %d %d\n",i,tids[i]); } for( i = 0 ; i < numt ; i++ ){ pvm_kill( tids[i] ); } pvm_exit(); exit(EXIT_FAILURE); } /* Begin User Program */ n = DATA_SIZE; /* initialize_data( data, n ); */ for(i = 0; i < n; i++){ data[i] = 1; } /* Broadcast initial data to slave tasks */ pvm_initsend(PvmDataDefault); pvm_pkint(&nproc, 1, 1); pvm_pkint(tids, nproc, 1); pvm_pkint(&n, 1, 1); pvm_pkfloat(data, n, 1); pvm_mcast(tids, nproc, MASTER_SLAVE); /* Wait for results from slaves */ for(i = 0; i < nproc; i++){ pvm_recv(-1, SLAVE_MASTER); pvm_upkint(&who, 1, 1 ); pvm_upkfloat(&result[who], 1, 1 ); printf("I got %f from %d\n", result[who], who); } /* Program Finished exit PVM before stopping */ pvm_exit(); exit(EXIT_SUCCESS); }