#include #include "mpi.h" #define N 100 #define Tol 0.00001 float **matrix(int m, int n) { int i; float **ptr; ptr = (float **)calloc(m,sizeof(float *)); for(i=0; i < m; i++) ptr[i] = (float *) calloc(n, sizeof(float)); return (ptr); } float iteration( float **old, float **new, int start, int finish) { float diff, maxerr =0; int i,j; for (i= start; i< finish; i++) for (j=1; j Tol){ tmp = new; new = old; old = tmp; /* Example for four processors 0 <- 1 2 <- 3 */ if( nid%2 == 1) { if(nid > 0) MPI_Send(&old[1][1],N-1,MPI_FLOAT, nid-1,10,MPI_COMM_WORLD);} else { if(nid < nprocs-1)MPI_Recv(&old[size][1],N-1,MPI_FLOAT, nid+1,10,MPI_COMM_WORLD,&status); } /* Example for four processors 0 -> 1 2 -> 3 */ if( nid%2 == 1){ if(nid > 0)MPI_Recv(&old[0][1], N-1, MPI_FLOAT, nid-1, 20,MPI_COMM_WORLD,&status);} else { if(nid < nprocs-1)MPI_Send(&old[size-1][1], N-1,MPI_FLOAT, nid+1,20, MPI_COMM_WORLD);} /* Example for four processors 0 1 <- 2 3 */ if( nid%2 == 1){ if(nid < nprocs-1)MPI_Send(&old[size-1][1], N-1,MPI_FLOAT, nid+1,30, MPI_COMM_WORLD);} else {if(nid > 0) MPI_Recv(&old[0][1], N-1, MPI_FLOAT, nid-1, 30,MPI_COMM_WORLD,&status);} /* Example for four processors 0 1 -> 2 3 */ if( nid%2 == 1){ if(nid < nprocs-1)MPI_Recv(&old[size][1], N-1,MPI_FLOAT, nid+1,40, MPI_COMM_WORLD,&status);} else {if(nid > 0) MPI_Send(&old[1][1], N-1, MPI_FLOAT, nid-1, 40,MPI_COMM_WORLD);} maxerr = iteration (old, new, 2, size-1); err = iteration (old,new,size-1,size); if(err > maxerr) maxerr = err; err = iteration (old,new,1,2); if(err > maxerr) maxerr = err; MPI_Allreduce(&maxerr,&maxerrG, 1,MPI_FLOAT,MPI_MAX, MPI_COMM_WORLD); } sprintf (str, "Solution%d.Txt",nid); fp = fopen(str,"wt"); if(nid ==0) for(j=0; j