带Mpi的二维天线罩漫游

带Mpi的二维天线罩漫游,mpi,deadlock,random-walk,Mpi,Deadlock,Random Walk,我试图扩展为随机行走问题找到的代码,以处理在4个方向的2D网格中行走的步行者。为了稍微简化问题,我的代码假设子域大小始终为4,进程数(世界大小)始终是用于计算网格的完美正方形。我的第一步是计算每个进程的4个邻居。然后我去散步,一切看起来都很好。我的问题是程序似乎总是陷入僵局,我无法找到有效的方法来解决它。我的想法是计算网格中每个进程的行索引,偶数行中的进程先发送,即 ----- Row 0 |0|1| <--- Processes in the even grid

我试图扩展为随机行走问题找到的代码,以处理在4个方向的2D网格中行走的步行者。为了稍微简化问题,我的代码假设子域大小始终为4,进程数(世界大小)始终是用于计算网格的完美正方形。我的第一步是计算每个进程的4个邻居。然后我去散步,一切看起来都很好。我的问题是程序似乎总是陷入僵局,我无法找到有效的方法来解决它。我的想法是计算网格中每个进程的行索引,偶数行中的进程先发送,即

       -----
Row 0  |0|1|     <--- Processes in the even grid row send first
Row 1  |2|3|     <--- Processes in the odd grid row receive first 
       -----
-----

第0排| 0 | 1 |欢迎来到Stackoverflow!逻辑的第一部分是有意义的:偶数行发送其传出的\u up \u walker,而奇数行接收其传入的\u up \u walker。因此,阻塞同步发送将与相应的接收相匹配。但这一逻辑必须适应左右行走的人。实际上,按原样,第0行的所有进程都将发送传出的\u left\u walker。如果使用阻塞同步发送,所有这些进程都将停留在那里,因为相应的接收永远不会被发布。相反,第1行上的所有进程将等待它们的walker。。。好久不见了…谢谢,非常感谢你的帮助。你是完全正确的,我编辑了我的代码,现在可以工作了。
   if ( process_row % 2 == 0)  //Processes on the even grid index will send first, that should make the program deadlock free
{
     cout << "Process " << world_rank << " sending " << outgoing_up_walkers.size()
         << " outgoing up walkers to process " <<neigbours[0]
         << endl;
  // Send all outgoing walkers to the next process.
  send_outgoing_walkers(&outgoing_up_walkers, world_rank,
      world_size, neigbours[0]);

  cout << "Process " << world_rank << " waiting to recieve incoming up walkers" << endl;
  receive_incoming_walkers(&incoming_up_walkers, world_rank,
                           world_size, neigbours[0]);

  cout << "Process " << world_rank << " sending " << outgoing_down_walkers.size()
         << " outgoing down walkers to process " <<neigbours[1]
         << endl;
  send_outgoing_walkers(&outgoing_down_walkers, world_rank,
      world_size, neigbours[1]);

        cout << "Process " << world_rank << " waiting to recieve incoming down walkers" << endl;
   receive_incoming_walkers(&incoming_down_walkers, world_rank,
                           world_size, neigbours[1]);

   cout << "Process " << world_rank << " sending " << outgoing_left_walkers.size()
         << " outgoing left walkers to process " << neigbours[2]
         << endl;
  send_outgoing_walkers(&outgoing_left_walkers, world_rank,
      world_size, neigbours[2]);

        cout << "Process " << world_rank << " waiting to recieve incoming left walkers" << endl;
  receive_incoming_walkers(&incoming_left_walkers, world_rank,
                           world_size, neigbours[2]);


  cout << "Process " << world_rank << " sending " << outgoing_right_walkers.size()
         << " outgoing right walkers to process " << neigbours[3]
         << endl;
  send_outgoing_walkers(&outgoing_right_walkers, world_rank,
      world_size, neigbours[3]);

    cout << "Process " << world_rank << " waiting to recieve incoming right walkers" << endl;
    receive_incoming_walkers(&incoming_right_walkers, world_rank,
                           world_size, neigbours[3]);

} else {
     cout << "Process " << world_rank << " waiting to recieve incoming up walkers" << endl;
  receive_incoming_walkers(&incoming_up_walkers, world_rank,
                           world_size,neigbours[0]);
    cout << "Process " << world_rank << " sending " << outgoing_up_walkers.size()
    << " outgoing up walkers to process " <<neigbours[0]
     << endl;
 send_outgoing_walkers(&outgoing_up_walkers, world_rank,
      world_size, neigbours[0]);

  cout << "Process " << world_rank << " waiting to recieve incoming down walkers" << endl;
  receive_incoming_walkers(&incoming_down_walkers, world_rank,
                           world_size, neigbours[1]);
  cout << "Process " << world_rank << " sending " << outgoing_down_walkers.size()
     << " outgoing down walkers to process " <<neigbours[1]
     << endl;
   send_outgoing_walkers(&outgoing_down_walkers, world_rank,
      world_size, neigbours[1]);

 cout << "Process " << world_rank << " waiting to recieve incoming left walkers" << endl;
  receive_incoming_walkers(&incoming_left_walkers, world_rank,
                           world_size, neigbours[2]);
     cout << "Process " << world_rank << " sending " << outgoing_left_walkers.size()
     << " outgoing left walkers to process " << neigbours[2]
     << endl;
   send_outgoing_walkers(&outgoing_left_walkers, world_rank,
      world_size, neigbours[2]);

    cout << "Process " << world_rank << " waiting to recieve incoming right walkers" << endl;
  receive_incoming_walkers(&incoming_right_walkers, world_rank,
                           world_size, neigbours[3]);

  cout << "Process " << world_rank << " sending " << outgoing_right_walkers.size()
     << " outgoing right walkers to process " << neigbours[3]
     << endl;
  send_outgoing_walkers(&outgoing_right_walkers, world_rank,
      world_size, neigbours[3]);
}
cout << "Process " << world_rank << " received " << incoming_up_walkers.size()
     << " incoming up walkers" << endl;
cout << "Process " << world_rank << " received " << incoming_down_walkers.size()
     << " incoming down walkers" << endl;
cout << "Process " << world_rank << " received " << incoming_left_walkers.size()
     << " incoming left walkers" << endl;
cout << "Process " << world_rank << " received " << incoming_right_walkers.size()
     << " incoming right walkers" << endl;