Does a call to MPI_Barrier affect every thread in an MPI process?

谁说我不能喝 提交于 2019-11-30 19:34:25

问题


Does a call to MPI_Barrier affect every thread in an MPI process or only the thread that makes the call? For your information , my MPI application will run with MPI_THREAD_MULTIPLE.

Thanks.


回答1:


The way to think of this is that MPI_Barrier (and other collectives) are blocking function calls, which block until all processes in the communicator have completed the function. That, I think, makes it a little easier to figure out what should happen; the function blocks, but other threads continue on their way unimpeded.

So consider the following chunk of code (The shared "done" flag being flushed to communicate between threads is not how you should be doing thread communication, so please don't use this as a template for anything):

#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;
}

Rank 1 sleeps for 10 minutes, and all the ranks start a barrier in one thread. If you run this with mpirun -np 2, you'd expect the first of rank 0s threads to hit the barrier, and the other to cycle around printing and waiting -- and sure enough, that's what happens:

$ 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


来源:https://stackoverflow.com/questions/5473482/does-a-call-to-mpi-barrier-affect-every-thread-in-an-mpi-process

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!