//#include "mpi.h" #include #include #include #include #define MAXSIZE 1000 int main(int argc, char **argv) { int myid, numprocs; int data[MAXSIZE], i, x, low, high, myresult, result; char filename[255]; FILE *fileptr; int eofmarker; // MPI_Init(&argc, &argv); // MPI_Comm_size(MPI_COMM_WORLD, &numprocs); // MPI_Comm_rank(MPI_COMM_WORLD, &myid); // if(myid == 0 ) { // strcpy(filename, getenv("HOME")); // strcat(filename, "/SuperComp2006/nums.txt"); strcpy(filename, "nums.txt"); if ((fileptr = fopen(filename, "r")) == NULL) { printf("Can't open the input file: %s\n\n", filename); exit(1); } i = 0; // see http://www.sci.brooklyn.cuny.edu/~jones/CIS1.5/fromfile.html eofmarker = fscanf(fileptr, "%d", &data[i]); while (eofmarker != EOF && i < MAXSIZE) { i++; eofmarker = fscanf(fileptr, "%d", &data[i]); } printf("%d\n", i); // } // Broadcast data // Add this processor's section of the array result = 0; numprocs = 4; x = MAXSIZE/numprocs; myid = 0; low = myid*x; high = low + x; myresult = 0; for(i = low; i < high; i++) myresult += data[i]; printf("Process %d, Sum = %d\n", myid, myresult); result += myresult; myid = 1; low = myid*x; high = low + x; myresult = 0; for(i = low; i < high; i++) myresult += data[i]; printf("Process %d, Sum = %d\n", myid, myresult); result += myresult; myid = 2; low = myid*x; high = low + x; myresult = 0; for(i = low; i < high; i++) myresult += data[i]; printf("Process %d, Sum = %d\n", myid, myresult); result += myresult; myid = 3; low = myid*x; high = low + x; myresult = 0; for(i = low; i < high; i++) myresult += data[i]; printf("Process %d, Sum = %d\n", myid, myresult); result += myresult; // Compute global sum printf("Process 0, total sum = %d\n", result); // MPI_Finalize(); return 0; } /* mpirun -np 4 a.out 1000 Process 0, Sum = 127769 Process 1, Sum = 123735 Process 2, Sum = 131857 Process 0, total sum = 510653 Process 3, Sum = 127292 */