Mpi的2D Radom Walk

时间:2018-07-28 18:51:35

标签: mpi deadlock random-walk

我正在尝试扩展here中针对随机行走问题的代码,以处理在4D方向上以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 
       -----

但是,这似乎不起作用,这是我的发送/接收代码

   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;

}

0 个答案:

没有答案