/* psimeq1.c using broadcast and gather, unoptimized */ #include "mpi.h" #include #include #undef abs #define abs(x) ((x)<0.0?(-(x)):(x)) static int n=1024; /* number of equations */ int main(int argc, char *argv[]) { int proc, myid, numprocs; int count = 1; int master = 0; double esec = 0.0; double * gath; /* only master receives here from Gather */ double * Y; /* only master has solution */ int * ROW; /* only master keeps row interchange */ int i, j, jj; int knout[2]; /* column being reduced, number of equations */ int ipivrow; /* pivot row */ int * upivrow; /* local used pivot rows */ double *rbuf; /* gather buffer row, ipiv, abspiv */ double *B; /* local equations nn by n+1 */ int nn; /* nn=(n+numprocs-1)/numprocs local equations */ int ii; /* ii=nn*myid index of first local equation */ int k; /* pivot column global */ int ipivot; /* pivot row of max pivot */ double abspivot; /* absolute value of largest pivot */ MPI_Init(&argc, &argv); MPI_Comm_size(MPI_COMM_WORLD, &numprocs); MPI_Comm_rank(MPI_COMM_WORLD, &myid); knout[0] = 0; /* initialize for Bcast */ knout[1] = n; ipivrow = -1; if(myid == master) { printf("psimeq1.c numprocs=%d, n=%d \n", numprocs, n); printf("Bcast is blocking and must be executed by all.\n"); printf("Gather automatically indexes through gath.\n"); printf("only master receives from Gather.\n"); printf("no order guaranteed for printf output.\n"); } /* end master */ /* all */ count = 2; MPI_Bcast(knout, count, MPI_INT, master, MPI_COMM_WORLD); k = knout[0]; n = knout[1]; nn=(n+numprocs-1)/numprocs; /* number of local equations */ ii=nn*myid; /* index of first local equation */ B = (double *)malloc((n+1)*nn*sizeof(double)); rbuf = (double *)malloc((n+3)*sizeof(double)); upivrow = (int *)malloc(nn*sizeof(int)); for(i=0; ij) B[i*(n+1)+j] = (double)(ii+i+1); /* test data */ else B[i*(n+1)+j] = (double)(j+1); } B[i*(n+1)+n] = 0.0; for(j=0; jabspivot) { abspivot = abs(B[i*(n+1)+k]); ipivot = i; } } if(ipivot>=0) { for(j=k+1; j<=n; j++) rbuf[j] = B[ipivot*(n+1)+j]/B[ipivot*(n+1)+k]; } else { for(j=k+1; j<=n; j++) rbuf[j] = 0.0; } rbuf[n+1] = (double)(ipivot); /* local row */ rbuf[n+2] = abspivot; /* unused locations */ for(j=0; j=0 && gath[i*(n+3)+(n+2)]>abspivot) { abspivot = gath[i*(n+3)+(n+2)]; ipivrow = (int)gath[i*(n+3)+(n+1)]; jj = i; } } if(jj<0) { printf("Big trouble, non independent equations! \n"); for(j=0; j=0) { upivrow[ipivrow] = 1; } /* reduce */ for(i=0; i