MPI_Allgather 中的奇怪死锁

Bizarre deadlock in MPI_Allgather

经过大量谷歌搜索,我不知道是什么导致了这个问题。在这里:

我在我的代码中对 MPI_Allgather 进行了简单的调用,我对其进行了双重、三次和四次检查以确保正确(send/receive 缓冲区大小合适;send/receive调用中的大小是正确的),但是对于 'large' 个进程,我遇到了死锁或 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 循环。这适用于那个案例,但更改给我的代码的网格(使用 METIS 划分的 CFD 网格)使问题又回来了。现在我的解决方案是用 Allgatherv 替换 Allgather,我想这是反正效率更高,因为我从每个进程发送了不同数量的数据。

这是(我希望)上下文相关的违规代码;如果我错过了什么,有问题的 Allgather 位于 this file.

的第 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芙蕾雅 0.3(基本上 Ubuntu 14.04)。但是,我在另一台使用 CentOS、Intel 硬件和 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;这不是我实际代码中发生的情况('broken' 通信器上没有等级退出 Allgather),但最终结果是相同的 - 代码挂起,直到我杀死它。

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

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

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

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

The type signature associated with sendcount, sendtype, at a process must be equal to the type signature associated with recvcount, recvtype at any other process.