2011-07-27 33 views
2

我已經運行的MPI函數的一些測試,以瞭解它是如何工作的,並得到了與MPI_Barrier怪異的結果裏面工作:它如果我使用它的代碼就像MPI_Barrier不是循環

int main(int argc, char *argv[]) 
{ 
    <some code> 
    MPI_Barrier(MPI_COMM_WORLD); 
    <more code> 
    MPI_Barrier(MPI_COMM_WORLD); 
    <...> 
} 
大家所期待的

但是當我從循環內部調用它時,我得到了隨機結果。具體而言,我有以下的測試代碼:

#include "mpi.h" 
#include <stdio.h> 

int main(int argc, char *argv[]) 
{ 
    int i, rb, rank, nprocs; 

    MPI_Init(&argc,&argv); 
    MPI_Comm_size(MPI_COMM_WORLD,&nprocs); 
    MPI_Comm_rank(MPI_COMM_WORLD,&rank); 

    i=0; 
    while(i<5) 
    { 
    rb=MPI_Barrier(MPI_COMM_WORLD); 
    printf("Itartion %d. I am %d of %d. MPIBarrierRes: %d\n", i, rank, nprocs, rb); 
    i++; 
    } 
    MPI_Finalize(); 
    return 0; 
} 

當我與3個任務運行它,我隨機得到:

Itartion 0. I am 0 of 3. MPIBarrierRes: 0 
Itartion 0. I am 2 of 3. MPIBarrierRes: 0 
Itartion 0. I am 1 of 3. MPIBarrierRes: 0 
Itartion 1. I am 0 of 3. MPIBarrierRes: 0 
Itartion 1. I am 1 of 3. MPIBarrierRes: 0 
Itartion 1. I am 2 of 3. MPIBarrierRes: 0 
Itartion 2. I am 0 of 3. MPIBarrierRes: 0 
Itartion 2. I am 1 of 3. MPIBarrierRes: 0 
Itartion 2. I am 2 of 3. MPIBarrierRes: 0 
Itartion 3. I am 0 of 3. MPIBarrierRes: 0 
Itartion 3. I am 1 of 3. MPIBarrierRes: 0 
Itartion 3. I am 2 of 3. MPIBarrierRes: 0 
Itartion 4. I am 0 of 3. MPIBarrierRes: 0 
Itartion 4. I am 1 of 3. MPIBarrierRes: 0 
Itartion 4. I am 2 of 3. MPIBarrierRes: 0 

這是啥子我應該想到,或者只是oposite:

Itartion 0. I am 2 of 3. MPIBarrierRes: 0 
Itartion 1. I am 2 of 3. MPIBarrierRes: 0 
Itartion 2. I am 2 of 3. MPIBarrierRes: 0 
Itartion 3. I am 2 of 3. MPIBarrierRes: 0 
Itartion 4. I am 2 of 3. MPIBarrierRes: 0 
Itartion 0. I am 0 of 3. MPIBarrierRes: 0 
Itartion 1. I am 0 of 3. MPIBarrierRes: 0 
Itartion 2. I am 0 of 3. MPIBarrierRes: 0 
Itartion 3. I am 0 of 3. MPIBarrierRes: 0 
Itartion 4. I am 0 of 3. MPIBarrierRes: 0 
Itartion 0. I am 1 of 3. MPIBarrierRes: 0 
Itartion 1. I am 1 of 3. MPIBarrierRes: 0 
Itartion 2. I am 1 of 3. MPIBarrierRes: 0 
Itartion 3. I am 1 of 3. MPIBarrierRes: 0 
Itartion 4. I am 1 of 3. MPIBarrierRes: 0 

或介於兩者之間,像

Itartion 0. I am 1 of 3. MPIBarrierRes: 0 
Itartion 0. I am 0 of 3. MPIBarrierRes: 0 
Itartion 1. I am 0 of 3. MPIBarrierRes: 0 
Itartion 0. I am 2 of 3. MPIBarrierRes: 0 
Itartion 1. I am 1 of 3. MPIBarrierRes: 0 
Itartion 2. I am 0 of 3. MPIBarrierRes: 0 
Itartion 1. I am 2 of 3. MPIBarrierRes: 0 
Itartion 2. I am 1 of 3. MPIBarrierRes: 0 
Itartion 3. I am 0 of 3. MPIBarrierRes: 0 
Itartion 2. I am 2 of 3. MPIBarrierRes: 0 
Itartion 3. I am 1 of 3. MPIBarrierRes: 0 
Itartion 4. I am 0 of 3. MPIBarrierRes: 0 
Itartion 3. I am 2 of 3. MPIBarrierRes: 0 
Itartion 4. I am 1 of 3. MPIBarrierRes: 0 
Itartion 4. I am 2 of 3. MPIBarrierRes: 0 

任何人都可以告訴我,如果MPI_Barrier和循環之間有一些衝突嗎? (我只發現了警告,以避免在不同任務中使用不同大小的循環造成死鎖。) 如果有一個,我可以如何強制任務在開始循環的新迭代之前等待對方? 如果沒有,這個代碼有什麼問題?

謝謝!

+0

您可以在printf語句下放置另一個MPI_Brier,並檢查問題是否存在? – FFox

回答

3

它與你的循環無關。 MPI沒有暗示IO的序列化。如果您需要打印出來,您必須明確地將它們發送到一個等級,並在那裏打印出來。

+0

謝謝!我添加了一些關於何時發送消息的信息,現在我可以看到它是如何工作的,儘管順序仍然是隨機的。我會嘗試你爲下一次測試建議的。 – josh