【问题标题】:MPI Receive/Gather Dynamic Vector LengthMPI 接收/收集动态向量长度
【发布时间】:2012-08-22 20:25:04
【问题描述】:

我有一个存储结构向量的应用程序。这些结构保存有关系统上每个 GPU 的信息,例如内存和 giga-flop/s。每个系统上有不同数量的 GPU。

我有一个同时在多台机器上运行的程序,我需要收集这些数据。我对 MPI 很陌生,但大多数情况下可以使用 MPI_Gather(),但是我想知道如何收集/接收这些动态大小的向量。

class MachineData
{
    unsigned long hostMemory;
    long cpuCores;
    int cudaDevices;
    public:
    std::vector<NviInfo> nviVec; 
    std::vector<AmdInfo> amdVec;
    ...
};

struct AmdInfo
{
    int platformID;
    int deviceID;
    cl_device_id device;
    long gpuMem;
    float sgflops;
    double dgflops;
};

集群中的每台机器都会填充其MachineData 的实例。我想收集这些实例中的每一个,但我不确定如何收集 nviVecamdVec,因为它们的长度因每台机器而异。

【问题讨论】:

  • 请张贴代码。另请查看MPI_GATHERV()
  • 正如我所说,您可以尝试使用 GATHERV。有了这个,每台机器都可以发送一个自己长度的向量。使用recvcounts 来完成此操作。
  • 感谢 MPI_Gatherv() 运行良好!

标签: mpi gpgpu multi-gpu


【解决方案1】:

您可以将MPI_GATHERVMPI_GATHER 结合使用来完成此操作。 MPI_GATHERVMPI_GATHER 的可变版本,它允许根等级从每个发送过程中收集不同数量的元素。但是为了让根等级指定这些数字,它必须知道每个等级包含多少个元素。这可以在此之前使用简单的单个元素 MPI_GATHER 来实现。像这样的:

// To keep things simple: root is fixed to be rank 0 and MPI_COMM_WORLD is used

// Number of MPI processes and current rank
int size, rank;
MPI_Comm_size(MPI_COMM_WORLD, &size);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);

int *counts = new int[size];
int nelements = (int)vector.size();
// Each process tells the root how many elements it holds
MPI_Gather(&nelements, 1, MPI_INT, counts, 1, MPI_INT, 0, MPI_COMM_WORLD);

// Displacements in the receive buffer for MPI_GATHERV
int *disps = new int[size];
// Displacement for the first chunk of data - 0
for (int i = 0; i < size; i++)
   disps[i] = (i > 0) ? (disps[i-1] + counts[i-1]) : 0;

// Place to hold the gathered data
// Allocate at root only
type *alldata = NULL;
if (rank == 0)
  // disps[size-1]+counts[size-1] == total number of elements
  alldata = new int[disps[size-1]+counts[size-1]];
// Collect everything into the root
MPI_Gatherv(vectordata, nelements, datatype,
            alldata, counts, disps, datatype, 0, MPI_COMM_WORLD);

您还应该为结构注册 MPI 派生数据类型(上面代码中的datatype)(二进制发送可以工作,但不可移植,并且不能在异构设置中工作)。

【讨论】:

    猜你喜欢
    • 2013-02-16
    • 2018-07-21
    • 1970-01-01
    • 2011-12-13
    • 2013-08-07
    • 2014-09-17
    • 2015-03-08
    • 2018-03-12
    • 1970-01-01
    相关资源
    最近更新 更多