首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MPI_Barrier和递归

MPI_Barrier和递归
EN

Stack Overflow用户
提问于 2011-07-01 00:48:06
回答 1查看 988关注 0票数 2

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

这是代码

代码语言:javascript
复制
#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屏障)

代码语言:javascript
复制
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  

而我希望是这样的东西

代码语言:javascript
复制
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 
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-07-01 01:07:45

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

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

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6538234

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档