2
對MPI_Barrier的調用是否會影響MPI進程中的每個線程,或者隻影響調用該線程的線程 ? 有關您的信息,我的MPI應用程序將使用MPI_THREAD_MULTIPLE運行。對MPI_Barrier的調用是否會影響MPI進程中的每個線程?
謝謝。
對MPI_Barrier的調用是否會影響MPI進程中的每個線程,或者隻影響調用該線程的線程 ? 有關您的信息,我的MPI應用程序將使用MPI_THREAD_MULTIPLE運行。對MPI_Barrier的調用是否會影響MPI進程中的每個線程?
謝謝。
想到這一點的方法是MPI_Barrier(和其他集體)阻止函數調用,阻止函數調用,直到通信器中的所有進程都完成該函數爲止。我認爲,這使得弄清楚會發生什麼變得容易一些;功能塊,但其他線程不受阻礙地繼續前進。
因此考慮下面的代碼塊(共享「完成」標誌被刷新線程間的通信是不是你應該做的線程的通信,所以請不要以此作爲任何一個模板):
#include <mpi.h>
#include <omp.h>
#include <stdio.h>
#include <unistd.h>
int main(int argc, char**argv) {
int ierr, size, rank;
int provided;
volatile int done=0;
MPI_Comm comm;
ierr = MPI_Init_thread(&argc, &argv, MPI_THREAD_MULTIPLE, &provided);
if (provided == MPI_THREAD_SINGLE) {
fprintf(stderr,"Could not initialize with thread support\n");
MPI_Abort(MPI_COMM_WORLD,1);
}
comm = MPI_COMM_WORLD;
ierr = MPI_Comm_size(comm, &size);
ierr = MPI_Comm_rank(comm, &rank);
if (rank == 1) sleep(10);
#pragma omp parallel num_threads(2) default(none) shared(rank,comm,done)
{
#pragma omp single
{
/* spawn off one thread to do the barrier,... */
#pragma omp task
{
MPI_Barrier(comm);
printf("%d -- thread done Barrier\n", rank);
done = 1;
#pragma omp flush
}
/* and another to do some printing while we're waiting */
#pragma omp task
{
while(!done) {
printf("%d -- thread waiting\n", rank);
sleep(1);
}
}
}
}
MPI_Finalize();
return 0;
}
等級1睡10分鐘,所有等級在一個線程中開始屏障。如果你運行這個與mpirun -np 2
,你所期望的第一等級0線程擊中屏障,而其他各地印刷及等待週期 - 果然,那會發生什麼:
$ mpirun -np 2 ./threadbarrier
0 -- thread waiting
0 -- thread waiting
0 -- thread waiting
0 -- thread waiting
0 -- thread waiting
0 -- thread waiting
0 -- thread waiting
0 -- thread waiting
0 -- thread waiting
0 -- thread waiting
1 -- thread waiting
0 -- thread done Barrier
1 -- thread done Barrier
+1一個很好的例子。對Barrier的調用只會阻塞調用它的線程,並不是所有的線程都需要調用Barrier來讓進程完成調用。 – suszterpatt 2011-03-29 17:00:53
@喬納森,謝謝你的回答! – 2011-03-29 22:30:55
任何想法,爲什麼在我的情況下,這個程序永遠不會停止? ('done'變量在''while(!done)'部分永遠不會被讀爲1,我已經在那裏添加了'#pragma omp flush(done)',但它並沒有幫助我的編譯器gcc 6.3.0/Mac OS。 – Krzysztof 2017-02-08 09:22:54