2013-04-28 57 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