2011-06-30 7 views
2

MPI_Barrier(OpenMPI)を使用して、すべてのプロセスを再帰呼び出しと同じ深度にするようにしています。MPI_Barrierとrecursion

これは私が(mpirunの-np 2関門で実行)され得るものコード

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

ある

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

関連する問題