MPI4PY大阵列散射产生死锁

我尝试使用np.float64在3个进程之间散布大小为(3,512,48,2)的数组,数据类型为双精度Scatter()

# mpirun -np 3 python3 prog.py
import numpy as np
from mpi4py import MPI

if __name__ == "__main__":
 comm = MPI.COMM_WORLD
 nproc = comm.Get_size()
 rank = comm.Get_rank()  
 a = None

 a_split = np.empty([512,2],dtype = np.float64)


 if rank==0:

     a = np.zeros([3,dtype = np.float64)

     print(a.shape)

 comm.Barrier()

 print('Scattering')


 comm.Scatter([a,MPI.DOUBLE],a_split,root = 0)

但是,程序陷入僵局。从我在这里找到的

mpi4py scatter and gather with large numpy arrays

在这里

Along what axis does mpi4py Scatterv function split a numpy array?

对于大数组,我必须使用Scatterv()函数。因此,这是使用此功能的另一个代码:

# mpirun -np 3 python3 prog.py
import numpy as np
from mpi4py import MPI

if __name__ == "__main__":
    comm = MPI.COMM_WORLD
    nproc = comm.Get_size()
    rank = comm.Get_rank()  
    a = None

    a_split = np.empty([512,dtype = np.float64)

    size = 512*512*48*2 

    if rank==0:

        a = np.zeros([3,dtype = np.float64)

        print(a.shape)

    comm.Barrier()

    print('Scattering')

    comm.Scatterv([a,(size,size,size),(0,2*size),root =0)

但是,这也导致了僵局。我还尝试过通过与Send()Recv()进行点对点通信来发送数组,但这无济于事。死锁似乎仅取决于数组的大小,例如,如果我将数组的大小从[512,2]更改为[512,10,2],则代码有效。

有人可以建议我在这种情况下可以做什么吗?

ferfrevervr 回答:MPI4PY大阵列散射产生死锁

一个问题是混合使用np.floatMPI.DOUBLE。 一个有效的脚本可能是:

# mpirun -np 3 python3 prog.py
import numpy as np
from mpi4py import MPI
comm = MPI.COMM_WORLD
nproc = comm.Get_size()
rank = comm.Get_rank()  
a = None

a_split = np.empty([512,512,48,2],dtype = np.float)
a_split[:,:,:] = -666

if rank==0:
    a = np.zeros([3,dtype = np.float)
    print(a.shape)

print('Scattering')
comm.Scatter(a,a_split,root = 0)

print(a_split[1,1,1],a_split[-1,-1,-1])

我添加了最后一个打印行,以表明-np 4可以工作,但不能完全填充a_split-np 2失败,并出现截断错误。我的猜测是-np 3是故意的。

如果您故意使用np.float和MPI.DOUBLE ,请在问题中提及它,并添加用于启动程序的-np

>

[编辑]这也是脚本的C ++版本,因此您可以查看它是否也陷入僵局:

// mpic++ scat.cxx && mpirun -np <asmuchasyouwant> ./a.out

#include <iostream>
#include <vector>
#include <mpi.h>

int main(int argc,char** argv)
{
  MPI_Init(&argc,&argv);

  unsigned sz = 1*512*512*48*2;
  int rank,nbproc;
  std::vector<double> a;
  std::vector<double> a_split(sz);

  MPI_Comm_rank(MPI_COMM_WORLD,&rank);
  MPI_Comm_size(MPI_COMM_WORLD,&nbproc);

  if (rank == 0) {
    a.resize(nbproc * sz);
    std::fill(a.begin(),a.end(),2.71);
  }
  else {
    std::fill(a_split.begin(),a_split.end(),-666.666);
  }  

  MPI_Scatter(a.data(),sz,MPI_DOUBLE,a_split.data(),MPI_COMM_WORLD
              );


  std::cout << rank << " done " << a_split[sz-1] << std::endl;

  MPI_Finalize();
}
,

因此,最后,解决方案非常简单-我通常不关闭我的电脑,这似乎就是为什么它在经过大量计算后会产生死锁的原因。简单重启即可解决问题。

本文链接:https://www.f2er.com/3093698.html

大家都在问