/* mpiCC distribute.c -o distribute mpirun -np 4 distribute */ #include #include #include "mpi.h" int fill_data(float *x); int share_data( int i, float *x, float *xlocal); int print_new(float *x); int change_on_slaves(float *x); #define maxn 7 FILE *fp=stdout; int main( int argc, char ** argv ) { int rank, size; int i,j,n,err,jobid; int maxn2=maxn*maxn; float *x = (float *) malloc(sizeof(float)*maxn2); //float x[maxn]; float *xlocal = (float *) malloc(sizeof(float)*maxn2); //float xlocal[maxn]; char buf[256]; int slave; MPI_Status statusS1,statusS2; MPI_Comm master_comm, slave_comm; MPI_Init( &argc, &argv ); MPI_Comm_rank( MPI_COMM_WORLD, &rank ); MPI_Comm_size( MPI_COMM_WORLD, &size ); MPI_Comm_split( MPI_COMM_WORLD, rank == 0, 0, &slave_comm ); //123->012 //0->0 // if (size != 4) MPI_Abort( MPI_COMM_WORLD, 1 ); if (rank == 0) { fill_data( x ); // print_init_data( x ); } i=0; n=0; slave=0; jobid=0; while (n < maxn) { err=MPI_Bcast(&n, 1, MPI_INT, 0, MPI_COMM_WORLD); fprintf(fp,"{%d}## Bcast: n=%d slave=%d procesor=%d\n",err,n,slave,rank); if (rank == 0) { slave=1; while ( ( slave < size ) && ( n < maxn) ) { fprintf(fp,"[%d]#####Gospod: n=%d slave=%d \n",i,n,slave); share_data( n, x, xlocal ); /* data od 0 do maxn, proc od*/ err=MPI_Send( xlocal, maxn, MPI_FLOAT, slave, slave, MPI_COMM_WORLD ); fprintf(fp,"err=%d sporocilo poslano na S%d in \n", err,slave); slave++; n++; i++; } fprintf(fp,"nov set #####Gospod: n=%d slave=%d \n",n,slave); } else { if ( n >= maxn) break; jobid=i*(size-1)+ (rank-1) ; if (jobid < maxn) { fprintf(fp,"[%d] Suzenj: n=%d slave=%d procesor=%d jobid%d\n",i,n,slave,rank,jobid); err=MPI_Recv( xlocal, maxn, MPI_FLOAT, 0, rank, MPI_COMM_WORLD, &statusS1 ); fprintf(fp,"in na S%d sprejeto err=%d\n",rank,err); print_new(xlocal); change_on_slaves(xlocal); print_new(xlocal); } else { fprintf(fp,"ERROR:[%d] Suzenj: n=%d slave=%d procesor=%d jobid%d\n",i,n,slav\ e,rank,jobid); } i++; } } if (rank == 0) { err=MPI_Bcast(&n, 1, MPI_INT, 0, MPI_COMM_WORLD); fprintf(fp,"{%d}## Konec Bcast: n=%d slave=%d procesor=%d\n",err,n,slave,rank); } fprintf(fp,"Finalize: n=%d slave=%d procesor=%d\n",n,slave,rank); MPI_Finalize( ); return 0; } //======================= int fill_data(float *x){ int i,j, indx; /* Fill the data as specified */ for (i=0; i