I'm trying to do MPI_Gatherv for array of int without knowing the size of each array in each processes. 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. 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
.
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.
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. If there are several of the latter with fixed sizes on the different ranks, then the Gather
would be called in a preprocessing step.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.