mpi.hでは、MPI_Barrierを使用しようとしていますが、うまく動作しません。 ここに例があります:
int main(int argc, char **argv)
{
MPI_Init(&argc,&argv);
int i,j,rank,np;
MPI_Comm_size(MPI_COMM_WORLD,&np);
MPI_Comm_rank(MPI_COMM_WORLD,&rank);
for(i=0;i
出力は次のとおりです。
(0)
0
(1)
0
(2)
0
(3)
0
1 2 3 4 1 2 3 4 1 2 3 4
1 2 3 4
(4つのプロセッサで実行) したがって、障壁があっても、すべてのプロセッサが同時に値を印刷し始めます。 どうして?
printf
to stdout
is buffered, and by default flushes the buffer at each \n
. So each processor in turn prints its rank and flushes the buffer with \n
, then prints 0 and flushes the buffer. Each processor then uses printf
to load "1 2 3 4" into the output buffer, but doesn't flush. The flush happens the the end of the program, so all the final lines appear at once.
IOを順番に確認するには、障壁の前に fflush(stdout)
を追加してください。 I/O順序付けを完全に制御するには、MPIがI/O順序保証を提供しないため、1つのプロセッサーをI/O実行専用にします。