2013-04-28 59 views
0

當談到MPI時,我是一個noob,但這沒有任何意義。所以我在這裏有一段代碼,它使用MPI_Recv和MPI_Send,但是在告訴我我的網格大小之後,在第一個「我在這裏創建它」之前,這個東西被凍結了。被髮送到屏幕。爲什麼我的C++ MPI代碼凍結在我身上?

我不明白爲什麼。第一個「我在這裏製作」和最後一個輸出到屏幕上的東西之間幾乎沒有任何關係。

這裏的代碼剪斷

void initMesh(double* &phi, double &h, double &riptime, 
    double &deltat, int &x, int &y, int &xlength, int &ylength, int &tlength, int &ttasks, int &jtasks, int &itasks, int &tstart, int &jstart, int &istart, int &myrank, int &cores) { 
    int tasksize, remains, tremains; 
    MPI_Comm_rank(MPI_COMM_WORLD, &myrank); 
    MPI_Comm_size(MPI_COMM_WORLD, &cores); 
     if (myrank == 0) { 
     cout << "How large would you like the mesh" 
     <<" to be in the x-direction?" << endl; 
     cin >> x; 
     cout << "How large would you like the mesh" 
     << " to be in the y-direction?\n"; 
     cin >> y; 
     cout << "What is the distance between each x/y spot h (equal distance)?\n"; 
     cin >> h; 
     cout << "How much time would you like the program to run for?\n"; 
     cin >> riptime; 
     cout << "What would you like the time-step for the analysis to be?\n"; 
     cin >> deltat; 
     xlength = (int) (x/h); 
     ylength = (int) (y/h); 
     tlength = (int) (riptime/deltat); 
     cout << "Mesh x-points = " << xlength << endl; 
     cout << "Mesh y-points = " << ylength << endl; 
     cout << "Mesh time points = " << tlength << endl; 
     cout << "I made it here!"; 
     } 

//GOOD UP TO HERE!!! Then it freezes when I run the thing with 3 or more processors 


     for (int i=1; i < cores; i++) { 
      if (myrank==0) { 
      cout << "I made it here!"; 
      MPI_Send(&xlength, 1, MPI_INT, i, 0, MPI_COMM_WORLD); 
      } 
      else { 
      MPI_Recv(&xlength, 1, MPI_INT, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE); 
      cout << "I made it here!"; 
      } 
     } 
     for (int i=1; i < cores; i++) { 
      if (myrank==0) { 
      MPI_Send(&ylength, 1, MPI_INT, i, 1, MPI_COMM_WORLD); 
      } 
      else { 
      MPI_Recv(&ylength, 1, MPI_INT, 0, 1, MPI_COMM_WORLD, MPI_STATUS_IGNORE); 
      } 
     } 
     for (int i=1; i < cores; i++) { 
      if (myrank==0) { 
      MPI_Send(&tlength, 1, MPI_INT, i, 2, MPI_COMM_WORLD); 
      } 
      else { 
      MPI_Recv(&tlength, 1, MPI_INT, 0, 2, MPI_COMM_WORLD, MPI_STATUS_IGNORE); 
      } 
     } 
    cout << "I made it here!"; 

代碼的上述部分就是麻煩的是現在。

+0

第一個''我把它放在這裏!'''被緩衝(沒有'std :: endl'來刷新)。如果你解決了這個問題會怎樣? – chrisaycock 2013-04-28 22:10:23

+0

對不起,我是這個東西的新手。你是什​​麼意思由std :: endl刷新?我注意到,當我用(int)部分的時候,我說隱藏了其餘的代碼後,它被零除。 – Mechy 2013-04-28 22:11:23

+0

嘗試使用'cerr'而不是所有'cout',並在行尾加上'<< std :: endl;'。這將確保您的輸出不被緩衝,而是直接寫入屏幕。 @chrisaycock懷疑,最後一行沒有打印的事實只是緩衝問題,並不是因爲程序在產生輸出的行之前失敗。 – 2013-04-28 22:14:43

回答

2

正如評論中所述,您看不到"I made it here!",因爲您缺少<< endl

至於MPI:在每個for循環中,0級似乎發送一些東西到其他每個級別。然而,其他等級期望爲循環的每個迭代接收一條消息。你想要的是每個等級每個循環只能接收一個等級。

實際上,因爲你發送相同的信息給每個等級有兩個甚至更好的選擇:

  • 假設每個等級具有相同的程序輸入,計算x - ,y - 和tlength冗餘上每個進程(比傳遞它的方式更快)。
  • 如果這不是一個選項,請使用MPI_Broadcast與0級作爲來源。
+0

這有助於很多謝謝你。我會試試看看會發生什麼。 – Mechy 2013-04-28 22:39:36

+0

修復了這個問題,謝謝! – Mechy 2013-04-28 23:02:47