/* *************** Ring Sum of Ranks, Sample for MPI calls */ #include #include struct { int myrank; double dmyrank; } rankpak,rankpak1; #define RESULT_PACKET_NBLOCKS 2 MPI_Datatype array_of_types[RESULT_PACKET_NBLOCKS]= {MPI_INT,MPI_DOUBLE}; int array_of_blocklenghts[RESULT_PACKET_NBLOCKS] = {1,1}; MPI_Aint array_of_displacements[RESULT_PACKET_NBLOCKS]; main(argc,argv) int argc; char **argv; { int myrankd; int size; int i,extent; int message,message1; MPI_Status status,status1; MPI_Request request; double start,end; int neighbor; int lsum=0; double dlsum=0; /* new datatype */ MPI_Datatype newtype; /* init MPI */ MPI_Init(&argc,&argv); MPI_Comm_rank(MPI_COMM_WORLD,&myrankd); MPI_Comm_size(MPI_COMM_WORLD,&size); MPI_Aint array_of_displacements[0]=0; MPI_Type_extent (MPI_INT, &extent); MPI_Aint array_of_displacements[1]=extent; MPI_Type_struct (2, array_of_blocklengths, array_of_displacements, array_of_types, &newtype); MPI_Type_commit (&newtype); /* message=myrank; */ rankpak.myrank=myrankd; rankpak.dmyrank=myrankd; neighbor=(myrankd+1)%size; for(;;) { /* this call can be used to measure time */ start=MPI_Wtime(); /* non-blocking send */ MPI_Isend(&rankpak,1,newtype,neighbor ,5,MPI_COMM_WORLD,&request); end=MPI_Wtime(); printf ("\n\rMessage send time:%f",end-start); MPI_Recv(&rankpak1,1,newtype, MPI_ANY_SOURCE, 5,MPI_COMM_WORLD,&status); /* wait for the message */ MPI_Wait(&request, &status1); rankpak.myrank=rankpak1.myrank; rankpak.dmyrank=rankpak1.dmyrank; lsum+=rankpak.myrank; dlsum+=rankpak.dmyrank; /* break when the original message has come back */ printf("\n\rLOCAL SUM:%d, %f",lsum,dlsum); if (rankpak1.myrank==myrankd) { printf("\n\rSUM:%d,%f",lsum,dlsum); break; } } printf("\n\r"); MPI_Finalize(); }