MPI_Allgather中奇怪的死锁

Bizarre deadlock in MPI_Allgather

本文关键字:死锁 Allgather MPI      更新时间:2023-10-16

在谷歌搜索了很多之后,我不知道是什么导致了这个问题。

我有一个简单的调用MPI_Allgather在我的代码,我有双重,三重和四重检查是正确的(发送/接收缓冲区大小适当;调用中的发送/接收大小是正确的),但对于"大"数量的进程,我得到死锁或MPI_ERR_TRUNCATE。用于Allgather的通信器使用MPI_Comm_split从MPI_COMM_WORLD中分离出来。在我当前的测试中,第0位分配给一个通信器,其余的分配给第二个通信器。对于总共6个或更少的等级,Allgather工作得很好。如果我使用7个秩,我得到一个MPI_ERR_TRUNCATE。8级,僵局。我已经验证了通信器被正确分割(MPI_Comm_rank和MPI_Comm_size在两个通信的所有级别上都是正确的)。

我已经手动验证了每个发送和接收缓冲区的大小,以及接收的最大数量。我的第一个解决方法是将MPI_Allgather交换为每个进程的MPI_Gather的for循环。这适用于一种情况,但是改变给我的代码的网格(使用METIS对CFD网格进行分区)将问题带回来。现在我的解决方案,我还没有能够打破(尚未),是用Allgatherv替换Allgather,我认为这是更有效的,因为我有不同数量的数据从每个进程发送。

这是(我希望)在上下文中相关的违规代码;如果我错过了什么,那么有问题的Allgather就在这个文件的第599行。

  // Get the number of mpiFaces on each processor (for later communication)
  // 'nProgGrid' is the size of the communicator 'gridComm'
  vector<int> nMpiFaces_proc(nProcGrid);
  // This MPI_Allgather works just fine, every time      
  // int nMpiFaces is assigned on preceding lines
  MPI_Allgather(&nMpiFaces,1,MPI_INT,nMpiFaces_proc.data(),1,MPI_INT,gridComm);
  int maxNodesPerFace = (nDims==2) ? 2 : 4;
  int maxNMpiFaces = getMax(nMpiFaces_proc);
  // The matrix class is just a fancy wrapper around std::vector that 
  // allows for (i,j) indexing.  The getSize() and getData() methods just
  // call the size() and data() methods, respectively, of the underlying
  // vector<int> object.
  matrix<int> mpiFaceNodes_proc(nProcGrid,maxNMpiFaces*maxNodesPerFace);
  // This is the MPI_Allgather which (sometimes) doesn't work.
  // vector<int> mpiFaceNodes is assigned in preceding lines
  MPI_Allgather(mpiFaceNodes.data(),mpiFaceNodes.size(),MPI_INT,
                mpiFaceNodes_proc.getData(),maxNMpiFaces*maxNodesPerFace,
                MPI_INT,gridComm);

我目前使用OpenMPI 1.6.4, g++ 4.9.2,和AMD FX-8350 8核处理器,16GB内存,运行最新的Elementary OS Freya 0.3(基本上是Ubuntu 14.04)。但是,我在另一台使用CentOS,英特尔硬件和MPICH2的机器上也遇到了这个问题。

任何想法?我听说可以改变MPI的内部缓冲区大小来解决类似的问题,但是快速尝试这样做(如http://www.caps.ou.edu/pipermail/arpssupport/2002-May/000361.html所示)没有效果。

作为参考,这个问题与这里显示的问题非常相似:https://software.intel.com/en-us/forums/topic/285074,除了在我的情况下,我只有一个8核的处理器,在一台台式计算机上。

我已经设法把这个失败的最简单的例子放在一起:

#include <iostream>
#include <vector>
#include <stdlib.h>
#include <time.h>
#include "mpi.h"
using namespace std;
int main(int argc, char* argv[])
{
  MPI_Init(&argc,&argv);
  int rank, nproc, newID, newRank, newSize;
  MPI_Comm newComm;
  MPI_Comm_rank(MPI_COMM_WORLD,&rank);
  MPI_Comm_size(MPI_COMM_WORLD,&nproc);
  newID = rank%2;
  MPI_Comm_split(MPI_COMM_WORLD,newID,rank,&newComm);
  MPI_Comm_rank(newComm,&newRank);
  MPI_Comm_size(newComm,&newSize);
  srand(time(NULL));
  // Get a different 'random' number for each rank on newComm
  //int nSend = rand()%10000;
  //for (int i=0; i<newRank; i++) nSend = rand()%10000;
  /*! -- Found a set of #'s which fail for nproc=8: -- */
  int badSizes[4] = {2695,7045,4256,8745};
  int nSend = badSizes[newRank];
  cout << "Comm " << newID << ", rank " << newRank << ": nSend = " << nSend << endl;
  vector<int> send(nSend);
  for (int i=0; i<nSend; i++) 
    send[i] = rand();
  vector<int> nRecv(newSize);
  MPI_Allgather(&nSend,1,MPI_INT,nRecv.data(),1,MPI_INT,newComm);
  int maxNRecv = 0;
  for (int i=0; i<newSize; i++)
    maxNRecv = max(maxNRecv,nRecv[i]);
  vector<int> recv(newSize*maxNRecv);
  MPI_Barrier(MPI_COMM_WORLD);
  cout << "rank " << rank << ": Allgather-ing data for communicator " << newID << endl;
  MPI_Allgather(send.data(),nSend,MPI_INT,recv.data(),maxNRecv,MPI_INT,newComm);
  cout << "rank " << rank << ": Done Allgathering-data for communicator " << newID << endl;
  MPI_Finalize();
  return 0;
}

以上代码被编译并运行为:

mpicxx -std=c++11 mpiTest.cpp -o mpitest
mpirun -np 8 ./mpitest

在我的16核CentOS和8核Ubuntu机器上都有以下输出:

Comm 0, rank 0: nSend = 2695
Comm 1, rank 0: nSend = 2695
Comm 0, rank 1: nSend = 7045
Comm 1, rank 1: nSend = 7045
Comm 0, rank 2: nSend = 4256
Comm 1, rank 2: nSend = 4256
Comm 0, rank 3: nSend = 8745
Comm 1, rank 3: nSend = 8745
rank 5: Allgather-ing data for communicator 1
rank 6: Allgather-ing data for communicator 0
rank 7: Allgather-ing data for communicator 1
rank 0: Allgather-ing data for communicator 0
rank 1: Allgather-ing data for communicator 1
rank 2: Allgather-ing data for communicator 0
rank 3: Allgather-ing data for communicator 1
rank 4: Allgather-ing data for communicator 0
rank 5: Done Allgathering-data for communicator 1
rank 3: Done Allgathering-data for communicator 1
rank 4: Done Allgathering-data for communicator 0
rank 2: Done Allgathering-data for communicator 0

注意,每个通信器中只有2个秩退出Allgather;这不是在我的实际代码中发生的事情(没有"破碎"通信器退出Allgather的排名),但最终的结果是相同的-代码挂起,直到我杀死它。

我猜这与每个进程上的不同发送数量有关,但据我所知,从MPI文档和我所看到的教程中可以看出,这应该是允许的,对吗?当然,MPI_Allgatherv更适用一些,但为了简单起见,我一直使用Allgather。

如果所有进程的输入计数不相同,则必须使用MPI_Allgatherv

确切地说,必须匹配的是类型签名count,type,因为从技术上讲,您可以使用不同的数据类型获得相同的基本表示(例如N个元素vs 1个元素,这是N个元素的连续类型),但是如果您在所有地方使用相同的参数,这是MPI集合的常见用法,那么您的计数必须匹配所有地方。

最新MPI标准(3.1)的相关部分在第165页:

在进程中与sendcount, sendtype相关联的类型签名必须等于与recvcount关联的类型签名,