本文介绍了调用MPI_Barrier是否会影响MPI进程中的每个线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对MPI_Barrier的调用会影响MPI进程中的每个线程还是仅影响该线程那打了电话? 供您参考,我的MPI应用程序将与MPI_THREAD_MULTIPLE一起运行.

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

谢谢.

推荐答案

想到这一点的方法是MPI_Barrier(和其他集合体)正在阻塞函数调用,直到通信器中的所有进程都完成了该函数为止.我认为,这使确定应该发生的事情变得容易一些.功能块,但其他线程继续畅行无阻.

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.

因此,请考虑以下代码块(被刷新以在线程之间进行通信的共享"done"标志不是您应该如何进行线程通信,因此,请勿将其用作任何模板):

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

等级1睡眠10分钟,所有等级都在一个线程中启动屏障.如果使用mpirun -np 2运行此程序,则期望等级0s中的第一个线程遇到障碍,而另一个将围绕打印和等待周期循环-当然,这就是发生的情况:

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

这篇关于调用MPI_Barrier是否会影响MPI进程中的每个线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 02:18