gpt4 book ai didi

c++ - 为什么我的 C++ 并行程序在 MPI_Gather 中出现 MPI fatal error ?

转载 作者:行者123 更新时间:2023-12-04 08:05:16 25 4
gpt4 key购买 nike

我的排序程序在数组中偶数个元素的情况下工作正常,但出现错误

" Fatal error in MPI_Gather: Message truncated, error stack:MPI_Gather(sbuf=0x00A2A700, scount=4, MPI_INT, rbuf=0x00A302C8,rcount=4, MPI_INT, root=0, MPI_COMM_WORLD) failed Message from rank 1and tag -1342177184 truncated; 28 bytes received but buffer size is16 "


对于数组中的奇数个元素。
问题始于代码 if ((world_rank == 1) && (n % world_size != 0)) .我尝试了一切,但没有奏效。我怎样才能解决这个问题?提前致谢!
void merge(int*, int*, int, int, int);
void mergeSort(int*, int*, int, int);

int main(int argc, char** argv) {




int world_rank;
int world_size;

MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);
MPI_Comm_size(MPI_COMM_WORLD, &world_size);

int n = atoi(argv[1]);
int* original_array{ new int[n] {} };
//int original_array[]=new int[n];

int c;
srand(time(NULL));

if (world_rank == 0) {
printf("This is the unsorted array: ");
for (c = 0; c < n; c++) {

original_array[c] = rand() % n;
printf("%d ", original_array[c]);

}
printf("\n");
printf("\n");
}


int size = n / world_size;
int* sub_array=NULL;
int* tmp_array = NULL;
int* sorted = NULL;

if (world_rank == 0) {

sorted = { new int[n] {} };

}


if ((world_rank == 1) && (n % world_size != 0)) {
int r = n % world_size;
int size2 = size + r;
sub_array = { new int[size2] {} };
MPI_Scatter(original_array, size2, MPI_INT, sub_array, size2, MPI_INT, 0, MPI_COMM_WORLD);
tmp_array = { new int[size2] {} };
mergeSort(sub_array, tmp_array, 0, (size2 - 1));
MPI_Gather(sub_array, size2, MPI_INT, sorted, size2, MPI_INT, 0, MPI_COMM_WORLD);
}
else {
sub_array = { new int[size] {} };
MPI_Scatter(original_array, size, MPI_INT, sub_array, size, MPI_INT, 0, MPI_COMM_WORLD);
tmp_array = { new int[size] {} };
mergeSort(sub_array, tmp_array, 0, (size - 1));
MPI_Gather(sub_array, size, MPI_INT, sorted, size, MPI_INT, 0, MPI_COMM_WORLD);
}






if (world_rank == 0) {

printf("Array state before final mergeSort call: ");
for (c = 0; c < n; c++) {

printf("%d ", sorted[c]);

}

printf("\n");

int* other_array{ new int[n] {} };
mergeSort(sorted, other_array, 0, (n - 1));

printf("This is the sorted array: ");
for (c = 0; c < n; c++) {

printf("%d ", sorted[c]);

}

printf("\n");
printf("\n");

delete[] sorted;
delete[] other_array;

}

delete[] original_array;
delete[] sub_array;
delete[] tmp_array;

/********** Finalize MPI **********/
MPI_Finalize();

}

最佳答案

TL;博士:对于偶数个元素,进程调用 MPI_ScatterMPI_Gathercount ,对于奇数,他们没有。

My sorting program works fine with even number of elements in array


数组大小是偶数 所有进程都执行 else部分:
   if ((world_rank == 1) && (n % world_size != 0)) {
int r = n % world_size;
int size2 = size + r;
sub_array = { new int[size2] {} };
MPI_Scatter(original_array, size2, MPI_INT, sub_array, size2, MPI_INT, 0, MPI_COMM_WORLD);
tmp_array = { new int[size2] {} };
mergeSort(sub_array, tmp_array, 0, (size2 - 1));
MPI_Gather(sub_array, size2, MPI_INT, sorted, size2, MPI_INT, 0, MPI_COMM_WORLD);
}
else {
sub_array = { new int[size] {} };
MPI_Scatter(original_array, size, MPI_INT, sub_array, size, MPI_INT, 0, MPI_COMM_WORLD);
tmp_array = { new int[size] {} };
mergeSort(sub_array, tmp_array, 0, (size - 1));
MPI_Gather(sub_array, size, MPI_INT, sorted, size, MPI_INT, 0, MPI_COMM_WORLD);
}

but gives error " Fatal error in MPI_Gather: Message truncated, errorstack: MPI_Gather(sbuf=0x00A2A700, scount=4, MPI_INT, rbuf=0x00A302C8,rcount=4, MPI_INT, root=0, MPI_COMM_WORLD) failed Message from rank 1and tag -1342177184 truncated; 28 bytes received but buffer size is 16" for odd number of elements in array.


然而,当 数组的大小为奇数 进程 1 执行 if前述 if and else的一部分,而其他进程执行了 else部分。因此一些进程会调用例程 MPI_GatherMPI_Scatter与不同的 count .这些例程应该被所有进程以相同的方式调用。
要修复您的代码,您可以更改它,以便所有进程调用相同的 count MPI_ScatterMPI_Gather例程。
通常,当输入的大小没有被进程数平均划分时,您将面临与您相同的问题。为了解决这个问题,可以将虚拟值添加到数组中,以便大小除以进程数。或者可以使用 MPI_Gatherv :

Gathers into specified locations from all processes in a group


MPI_Scatterv

Scatters a buffer in parts to all processes in a communicator


来自 source可以阅读:

MPI_Gatherv and MPI_Scatterv are the variable-message-size versions ofMPI_Gather and MPI_Scatter. MPI_Gatherv extends the functionality ofMPI_Gather to permit a varying count of data from each process, and toallow some flexibility in where the gathered data is placed on theroot process. It does this by changing the count argument from asingle integer to an integer array and providing a new argument displs(an array) . MPI_Scatterv extends MPI_Scatter in a similar manner.More information on the use of these routines will be presented in anApplication Example later in this module.

关于c++ - 为什么我的 C++ 并行程序在 MPI_Gather 中出现 MPI fatal error ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66246536/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com