【问题标题】:MPI_Barrier and recursionMPI_Barrier 和递归
【发布时间】:2011-07-01 01:07:45
【问题描述】:

我正在尝试使用 MPI_Barrier (OpenMPI) 来强制所有进程处于递归调用的相同深度。

这是代码

#include <iostream>
#include <fstream>
#include <stdio.h>
#include <mpi.h>

using namespace std;

void recursive_function(int,int,int);

int main(int argc, char *argv[]) {
    int rank, size;

    MPI_Init(&argc, &argv);
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);
    MPI_Comm_size(MPI_COMM_WORLD, &size);

    recursive_function(0,3,rank);

    MPI_Finalize();
}

void recursive_function(int depth, int limit, int rank) {
    printf("depth: %d, processor %d\n", depth, rank);

    MPI_Barrier(MPI_COMM_WORLD);
    if(depth == limit) return;
    else recursive_function(depth+1,limit,rank);
}

我得到的是(使用 mpirun -np 2 屏障运行)

depth: 0, processor 0
depth: 1, processor 0
depth: 2, processor 0
depth: 3, processor 0
depth: 0, processor 1
depth: 1, processor 1
depth: 2, processor 1
depth: 3, processor 1  

虽然我希望像

depth: 0, processor 0
depth: 0, processor 1
depth: 1, processor 0
depth: 1, processor 1
depth: 2, processor 1
depth: 2, processor 0
depth: 3, processor 1
depth: 3, processor 0 

【问题讨论】:

    标签: c++ recursion mpi


    【解决方案1】:

    不保证 MPI 进程中的 stdout 以任何方式排序。

    也就是说,您需要让进程进行通信以证明它们都处于相同的递归深度。例如。在屏障之后,每个进程 != 0 都会向排名 0 发送一条消息,该消息会打印一些内容。

    【讨论】:

    • +1,但也许值得强调的是代码看起来是正确的,因为在所有进程完成当前深度之前,没有进程可以下降到下一个递归级别。只是stdout的无序弄乱了屏幕上的结果。
    • 感谢您的帮助。问题只是标准输出。代码实际上是正确的。
    猜你喜欢
    • 1970-01-01
    • 2011-11-14
    • 2011-06-21
    • 2011-01-04
    • 2014-01-17
    • 2011-07-08
    • 2013-07-08
    • 2014-10-27
    • 1970-01-01
    相关资源
    最近更新 更多