Задача: есть n процессов. от нулевого к первому передаётся сообщение. Затем от первого ко второму и т.д. Когда сообщение приходит к n-1 процессу, то он посылает сообщение нулевому. Всё это повторяется m раз.
Код:
#include <mpi.h>
#include <stdio.h>
void ring( int m, int argc, char** argv );
int main ( int argc, char** argv ) {
ring( 3, argc, argv );
return 0;
}
void ring( int m, int argc, char **argv ) {
int i = 0, ProcNum, ProcRank, done = 0, buf, process;
MPI_Status status;
MPI_Init( &argc, &argv );
//for ( i = 0; i < m; i++ ) {
while( i != m ) {
MPI_Comm_rank( MPI_COMM_WORLD, &ProcRank );
MPI_Comm_size( MPI_COMM_WORLD, &ProcNum );
if ( ProcRank == 0 ) {
MPI_Send( &ProcRank, 1, MPI_INT, 1, 0, MPI_COMM_WORLD );
}
else {
MPI_Recv( &buf, 1, MPI_INT, ProcRank - 1, 0, MPI_COMM_WORLD, &status );
printf( "bom-bom from %d to %d on itteration: %d\n", buf, ProcRank, i );
process = ProcRank + 1;
if ( process >= ProcNum )
process = 0;
MPI_Send( &ProcRank, 1, MPI_INT, process, 0, MPI_COMM_WORLD );
}
MPI_Barrier( MPI_COMM_WORLD );
if ( ProcRank == 0 ) {
MPI_Recv( &buf, 1, MPI_INT, ProcNum - 1, 0, MPI_COMM_WORLD, &status );
printf( "bom-bom from %d to %d on itteration: %d\n", buf, ProcRank, i );
printf( "DONE\n\n" );
}
i += 1;
MPI_Barrier( MPI_COMM_WORLD );
}
MPI_Finalize();
}
$ mpirun -np 4 ./prog
bom-bom from 0 to 1 on itteration: 0
bom-bom from 1 to 2 on itteration: 0
bom-bom from 2 to 3 on itteration: 0
bom-bom from 2 to 3 on itteration: 1
bom-bom from 3 to 0 on itteration: 0
DONE
bom-bom from 3 to 0 on itteration: 1
DONE
bom-bom from 0 to 1 on itteration: 1
bom-bom from 0 to 1 on itteration: 2
bom-bom from 1 to 2 on itteration: 1
bom-bom from 1 to 2 on itteration: 2
bom-bom from 2 to 3 on itteration: 2
bom-bom from 3 to 0 on itteration: 2
DONE
$ mpirun -np 4 ./prog
bom-bom from 0 to 1 on itteration: 0
bom-bom from 1 to 2 on itteration: 0
bom-bom from 2 to 3 on itteration: 0
bom-bom from 3 to 0 on itteration: 0
DONE
bom-bom from 0 to 1 on itteration: 1
bom-bom from 1 to 2 on itteration: 1
bom-bom from 2 to 3 on itteration: 1
bom-bom from 3 to 0 on itteration: 1
DONE
bom-bom from 1 to 2 on itteration: 2
bom-bom from 2 to 3 on itteration: 2
bom-bom from 0 to 1 on itteration: 2
bom-bom from 3 to 0 on itteration: 2
DONE
Как видно, в первом случае процессы ведут себя неправильно. Второй запуск: всё нормально.