簡體   English   中英

有沒有辦法在不知道每個進程中每個數組的大小的情況下執行 MPI_Gatherv ?

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

我正在嘗試為 int 數組執行 MPI_Gatherv 而不知道每個進程中每個數組的大小。 有沒有辦法做到這一點?

這是我的代碼片段。

#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
    }
}

你真的不能那樣做。 displacementselement_sizes參數的存在表明了這一點。 如果確實無法知道(無法在根處計算)根將接收到的計數,您將需要從每個節點執行單個intMPI_Gather並將其用作MPI_Gatherv displacements

當您調用Gatherv ,您必須事先知道大小和位移,因為您只需要在目標等級上分配足夠的空間。

這就是為什么你會經常看到一個Gather調用來獲得每個等級的大小,然后是一個減少以獲得位移,然后是Gatherv調用。 如果在不同等級上有幾個固定大小的后者,則將在預處理步驟中調用Gather

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM