开发者

MPI_Barrier and recursion

开发者 https://www.devze.com 2023-03-16 07:18 出处:网络
I\'m trying to use MPI_Barrier (OpenMPI) to force all the process to be in the same depth of a recursive call.

I'm trying to use MPI_Barrier (OpenMPI) to force all the process to be in the same depth of a recursive call.

This is the code

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

What I get is (running with mpirun -np 2 barrier)

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  

While I would expec开发者_如何学Pythont something like

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 


There is no guarantee that stdout among the MPI processes is ordered in any way.

That is, you need to have the processes communicate to prove that they are all at the same recursion depth. E.g. after the barrier each process != 0 sends a message to rank 0 which prints something.

0

精彩评论

暂无评论...
验证码 换一张
取 消