#include #include int main (int argc, char **argv) { MPI_Comm myComm; /* intrakomunikator w grupie lokalnej */ MPI_Comm myFirstComm; /* interkomunikator */ MPI_Comm mySecondComm; /* drugi interkomunikator */ int membershipKey, rank, newrank, mtype, data; MPI_Status recv_status; MPI_Request request; MPI_Init(&argc, &argv); MPI_Comm_rank(MPI_COMM_WORLD, &rank); /* Generujemy klucz przynaleznosci membershipKey; nalezy on do zbioru [0, 1, 2] */ membershipKey = rank % 3; printf("Procesor %2d membershipKey %d\n",rank,membershipKey); /* Budujemy intrakomunikator dla grupy lokalnej */ MPI_Comm_split(MPI_COMM_WORLD, membershipKey, rank, &myComm); MPI_Comm_rank( myComm, &newrank ); /* Budujemy interkomunikatory. Znaczniki sa zakodowane explicite */ if (membershipKey == 0) { /* Grupa 0 komunikuje sie z grupa 1 */ MPI_Intercomm_create(myComm, 0, MPI_COMM_WORLD, 1, 01, &myFirstComm); printf("Procesor %2d (%2d) membershipKey %d zakonczyl MPI_Intercomm_create\n", newrank,rank,membershipKey); } else if (membershipKey == 1) { /* Grupa 1 komunikuje sie z grupami 0 i 2 */ MPI_Intercomm_create(myComm, 0, MPI_COMM_WORLD, 0, 01, &myFirstComm); MPI_Intercomm_create(myComm, 0, MPI_COMM_WORLD, 2, 12, &mySecondComm); printf("Procesor %2d (%2d) membershipKey %d zakonczyl MPI_Intercomm_create\n", newrank,rank,membershipKey); } else if (membershipKey == 2) { /* Grupa 2 komunikuje sie z grupa 1 */ printf("Procesor %2d (%2d) membershipKey %d\n",newrank,rank,membershipKey); MPI_Intercomm_create(myComm, 0, MPI_COMM_WORLD, 1, 12, &myFirstComm); printf("Procesor %2d (%2d) membershipKey %d zakonczyl MPI_Intercomm_create\n", newrank,rank,membershipKey); } /* Wlasciwa robota; tutaj oczywiscie tylko maly przyklad */ /* Dane beda "plynac" czterema kanalami od grupy 0 do grupy 2 */ mtype = 1001; if (membershipKey == 0) { data = rank*rank; MPI_Send( &data, 1, MPI_INT, newrank, mtype, myFirstComm ); printf ("Poczatek rury: procesor %2d (%1d) wyslal %4d\n", newrank, rank, data ); } else if (membershipKey == 1) { MPI_Recv( &data, 1, MPI_INT, newrank, mtype, myFirstComm, &recv_status); printf ("Srodek rury: procesor %2d (%1d) odebral %4d\n", newrank, rank, data ); MPI_Send( &data, 1, MPI_INT, newrank, mtype, mySecondComm ); printf ("Srodek rury: procesor %2d (%1d) wyslal %4d\n", newrank, rank, data ); } else if (membershipKey == 2) { MPI_Recv( &data, 1, MPI_INT, newrank, mtype, myFirstComm, &recv_status); printf ("Koniec rury: procesor %2d (%1d) odebral %4d\n", newrank, rank, data ); } /* zwalniamy komunikatory */ MPI_Comm_free( &myComm ); MPI_Comm_free( &myFirstComm ); if (membershipKey == 1) MPI_Comm_free(&mySecondComm); MPI_Finalize(); }