MPI_Scatterv
就是針對這種情況。您既指定了sendcounts矢量,又指定了偏移矢量。這可能有點棘手,所以有一個例子:
int remainder = rows % comm_size;
int local_rows = (rows/comm_size)
if (comm_rank < remainder) {
local_rows++;
}
int* sendcounts = NULL;
int* displacements = NULL;
double* data = NULL;
if (comm_rank = root) {
data = ...;
sendcounts = malloc(sizeof(int) * comm_size);
displacements = malloc(sizeof(int) * comm_size);
int sum = 0;
for (int i = 0; i < comm_size; i++) {
sendcounts[i] = (rows/comm_size) * columns;
if (remainder > 0) {
sendcounts[i] += columns;
remainder--;
}
displacements[i] = sum;
sum += sendcounts[i];
}
}
double* local_data = malloc(sizeof(*local_data) * local_rows * columns);
MPI_Scatterv(data, sendcounts, displacements, MPI_DOUBLE,
local_data, local_rows * columns, MPI_DOUBLE, root, MPI_COMM_WORLD);
謝謝。正是我在找什麼! – PPGoodMan