/************************************************* * this is a mpi demo program for course 4f03 * what it does is to add 1000 random numbers * in parallel on several machines and return * the results to the root process * * how to compile: * mpicc mpidemo.c -o mpidemo * * how to run: *mpirun -np 4 -machinefile machines mpidemo * *4 number of processes(machines) *machines is a text file for machine name list * on which to run the program, looks like: * wolf35 * wolf36 * wolf37 * wolf38 * * root process gather all results and and add them up * printf total sum * when slaves start, they send a message to master * master displays their name * after calculation finishes, master sends a message to * slaves to exit */ #include "mpi.h" #include #include #include #define MAXSIZE 1000 #define FILE_NAME "rand_data.txt" int main(int argc, char *argv[]) { int myid, numprocs; int data[MAXSIZE], i, x, low, high, myresult=0, result=0; FILE *fp; struct utsname my_name; char name[SYS_NMLN]; MPI_Status status; MPI_Init(&argc,&argv); MPI_Comm_size(MPI_COMM_WORLD,&numprocs); MPI_Comm_rank(MPI_COMM_WORLD,&myid); /*get my name*/ uname(&my_name); if(myid == 0) { printf("\n\nMaster (rank %d): %s started\n\n\n", myid, my_name.nodename); for(i=1; i