简体   繁体   English

有没有办法在不知道每个进程中每个数组的大小的情况下执行 MPI_Gatherv ?

[英]Is there a way to do MPI_Gatherv without knowing the size of each array in each process?

I'm trying to do MPI_Gatherv for array of int without knowing the size of each array in each processes.我正在尝试为 int 数组执行 MPI_Gatherv 而不知道每个进程中每个数组的大小。 Is there a way to do it?有没有办法做到这一点?

Here is snippet of my code.这是我的代码片段。

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

using namespace std;

int main(int argc, char *argv[]) {
    MPI_Init(&argc, &argv);
    int rank;
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);

    int *local_arr;
    int *arr;
    int local_n;
    int n;

    if (rank == 0) {

        // do gatherv without knowing the element sizes and displacements

    } else {
        // fill array with random size
        srand(rank);
        local_n = (rand()%10)+1;
        local_arr = new int[local_n];

        for (int i = 0; i < local_n; i++) {
            local_arr[i] = i;
        }

        // do gatherv without knowing the element sizes and displacements
    }
}

You cannot really do that.你真的不能那样做。 The presence of the displacements and element_sizes arguments suggest that. displacementselement_sizes参数的存在表明了这一点。 If there really is no way of knowing (no way of calculating at root) the counts that root will receive, you will need to do a MPI_Gather of a single int from each node and use that as displacements in MPI_Gatherv .如果确实无法知道(无法在根处计算)根将接收到的计数,您将需要从每个节点执行单个intMPI_Gather并将其用作MPI_Gatherv displacements

When you have a call to Gatherv , you have to know the sizes and displacements beforehand, because you simply need to allocate sufficient space on the target rank.当您调用Gatherv ,您必须事先知道大小和位移,因为您只需要在目标等级上分配足够的空间。

That's why you will often see a Gather call before to get each rank's size, then a reduction to get the displacements and then the Gatherv call.这就是为什么你会经常看到一个Gather调用来获得每个等级的大小,然后是一个减少以获得位移,然后是Gatherv调用。 If there are several of the latter with fixed sizes on the different ranks, then the Gather would be called in a preprocessing step.如果在不同等级上有几个固定大小的后者,则将在预处理步骤中调用Gather

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM