2013-01-07 92 views
1

我在MPI上編寫了一個程序,它將圍繞每個處理器以一種環形方式x次數(例如,如果我希望它在四個處理器的「環」中繞兩圈,它會去0,1,2,3,0,1 .... 3)。非常奇怪的MPI行爲

一切都很好編譯,但是當我在Ubuntu VM上運行程序時,它永遠不會輸出任何東西。它甚至不會運行第一個輸出。任何人都可以解釋發生了什麼?

這是我的代碼:

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

int main(int argc, char **argv){ 
    int rank, size, tag, next, from, num; 
    tag = 201; 
    MPI_Status status; 
    MPI_Init(&argc, &argv); 
    MPI_Comm_rank(MPI_COMM_WORLD, &rank); 
    MPI_Comm_size(MPI_COMM_WORLD, &size); 
    next = (rank + 1)/ size; 
    from = (rank - 1)/size; 
    if (rank == 0){ 
      printf("How many times around the ring? :: "); 
     scanf ("%d", &num); 
     MPI_Send(&num, 1, MPI_INT, 1, tag, MPI_COMM_WORLD); 
    } 
    do{ 
     MPI_Recv(&num, 1, MPI_INT, from, tag, MPI_COMM_WORLD, &status); 
     printf("Process %d received %d from process %d\n", rank, num, status.MPI_SOURCE); 
     if (rank == 0){ 
      num--; 
      printf("Process 0 has decremented the number\n"); 
     } 
     printf("Process %d sending %d to process %d\n", rank, num ,next); 
     MPI_Send(&num, 1, MPI_INT, next, tag, MPI_COMM_WORLD); 
    }while (num > 0); 
    printf("Process %d has exited", rank); 
    if (rank == 0){ 
     MPI_Recv(&num, 1, MPI_INT, size - 1, tag, MPI_COMM_WORLD, &status); 
     printf("Process 0 has received the last round, exiting"); 
    } 
    MPI_Finalize(); 
    return 0; 
} 

回答

3

有一個與你的鄰居分配的問題。如果我們插入next/from計算

printf("Rank %d: from = %d, next = %d\n", rank, from, next); 

後,下面的線,我們得到:

$ mpirun -np 4 ./ring 
Rank 0: from = 0, next = 0 
Rank 1: from = 0, next = 0 
Rank 2: from = 0, next = 0 
Rank 3: from = 0, next = 1 

你想要的東西更像

next = (rank + 1) % size; 
from = (rank - 1 + size) % size; 

這給

$ mpirun -np 4 ./ring 
Rank 0: from = 3, next = 1 
Rank 1: from = 0, next = 2 
Rank 2: from = 1, next = 3 
Rank 3: from = 2, next = 0 

之後,你的代碼似乎工作。

+0

我在發佈後對代碼進行了更改。主要的問題是代碼不會輸出任何內容,甚至不會輸出第一個printf語句,不管它是什麼(對吧?)。 –

+0

爲了澄清我的系統的一些問題,我在Oracle虛擬機上使用了Ubuntu 12.10虛擬機。在虛擬機上,我已經爲虛擬機分配了2個內存,並允許它在我的英特爾酷睿i7上使用全部8個CPU。在我的虛擬機上運行MPI程序時,我沒有別的東西在運行。當我在一個單獨的工作區上運行'top'命令時,程序的任何給定實例的CPU消耗在100%左右振盪。 –

0

無論你的代碼是否好,你的第一個printf應該輸出。

如果您根本沒有打印任何消息,甚至是「if(rank ==)」塊中的printf,那麼它可能是您的VM的問題。你確定你有任何網絡接口在該虛擬機上激活?

如果答案是肯定的,它可能是有用的檢查the OpenMPI FAQ over tcp questions.第7,檢查與MPI兼容性(我如何告訴開放MPI要使用的TCP網絡?)和13(不開放MPI支持虛擬IP接口?)對於在虛擬機中運行MPI的任何可能的問題似乎都很有趣。