/* "q1_slave.txt" * Question #1, Master/Slave to add up N numbers (integers only) * * NOTE: the first slave (me=0) has a value of 1 * the Nth slave (me=n-1) has a value of n-1 * so the value added to sum is always * me+1. This isn't very clear here but running behind on time. * * tjn 3:54 PM 3/2/00 * * SLAVE */ #include #include #define MAX 100 int main() { int i,me,n, tids[MAX],mytid,sum; sum = 0; mytid = pvm_mytid(); pvm_recv(pvm_parent(), -1); pvm_upkint(&n, 1, 1); pvm_upkint(tids, n, 1); /* determine who were are */ for(i=0; i < n; i++) if(mytid == tids[i]) { me = i; break; } /* if I'm the first slave */ if(me == 0) { /* start sending, get ball rolling */ sum = 1; pvm_initsend(PvmDataDefault); pvm_pkint(&sum, 1, 1); pvm_send(tids[me+1], 1); } else { /* everyone else */ pvm_recv(tids[me-1], 1); pvm_upkint(&sum, 1, 1); sum += (me+1); pvm_initsend(PvmDataDefault); pvm_pkint(&sum, 1, 1); /* Checkto see if I'm the Nth (n-1) */ if(me == n-1) pvm_send(pvm_parent(), 1); /* Nth sends to parent */ else pvm_send(tids[me+1], 1); /* Others send to next */ } pvm_exit(); return 0; }