2
votes

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 expect 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 
1

1 Answers

3
votes

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.