2011-06-30 70 views
2

我想使用MPI_Barrier(OpenMPI)來強制所有進程處於遞歸調用的相同深度。MPI_Barrier和遞歸

這是代碼

#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 

回答

3

有不能保證MPI進程之間的stdout以任何方式排序。

也就是說,您需要讓進程進行通信以證明它們都處於相同的遞歸深度。例如。在屏障之後,每個進程!= 0發送一條消息給等級爲0的打印內容。

+1

+1,但也許值得一提的是代碼看起來是正確的,因爲沒有進程可以下降到下一級遞歸,直到所有進程完成當前深度。只是stdout的無序性會擾亂屏幕上的結果。 – suszterpatt

+0

謝謝你的幫助。問題只是標準輸出。代碼實際上是正確的。 – mambro