gpt4 book ai didi

c++ - 在 MPI 中逐元素求和和收集数组元素

转载 作者:行者123 更新时间:2023-11-28 05:12:56 26 4
gpt4 key购买 nike

在使用笛卡尔拓扑计算矩阵与 vector 相乘之后。我用它们的等级和 vector 得到了以下过程。

P0 (process with rank = 0) =[2 , 9].
P1 (process with rank = 1) =[2 , 3]
P2 (process with rank = 2) =[1 , 9]
P3 (process with rank = 3) =[4 , 6].

现在。我需要分别对偶数秩过程和奇数过程的元素求和,如下所示:

温度 1 = [3 , 18]
temp2 = [6 , 9]

然后,将结果收集到不同的 vector 中,如下所示:

result = [3 , 18 , 6 , 9]

我的尝试是使用 MPI_Reduce,然后像这样使用 MPI_Gather :

// Previous code 
double* temp1 , *temp2;
if(myrank %2 == 0){
BOOLEAN flag = Allocate_vector(&temp1 ,local_m); // function to allocate space for vectors
MPI_Reduce(local_y, temp1, local_n, MPI_DOUBLE, MPI_SUM, 0 , comm);
MPI_Gather(temp1, local_n, MPI_DOUBLE, gResult, local_n, MPI_DOUBLE,0, comm);
free(temp1);
}
else{
Allocate_vector(&temp2 ,local_m);
MPI_Reduce(local_y, temp2, local_n , MPI_DOUBLE, MPI_SUM, 0 , comm);
MPI_Gather(temp2, local_n, MPI_DOUBLE, gResult, local_n, MPI_DOUBLE, 0,comm);
free(temp2);
}

但答案不正确。代码似乎将偶数和奇数过程的所有元素相加在一起然后给出了段错误:错误结果 = [21 15 0 0] 还有这个错误

** Error in ./test': double free or corruption (fasttop): 0x00000000013c7510 ***
*** Error in
./test': double free or corruption (fasttop): 0x0000000001605b60 ***

最佳答案

它不会像您尝试的那样工作。要对流程子集的元素执行缩减,您必须为它们创建一个子通信器。在您的情况下,奇数和偶数进程共享相同的 comm,因此操作不是针对两个独立的进程组,而是针对合并的组。

您应该使用 MPI_Comm_split 执行拆分,使用两个新的子通信器执行缩减,最后在每个子通信器中排名 0(我们称这些领导者)参与在另一个仅包含这两个子通信器的集合中:

// Make sure rank is set accordingly

MPI_Comm_rank(comm, &rank);

// Split even and odd ranks in separate subcommunicators

MPI_Comm subcomm;
MPI_Comm_split(comm, rank % 2, 0, &subcomm);

// Perform the reduction in each separate group

double *temp;
Allocate_vector(&temp, local_n);
MPI_Reduce(local_y, temp, local_n , MPI_DOUBLE, MPI_SUM, 0, subcomm);

// Find out our rank in subcomm

int subrank;
MPI_Comm_rank(subcomm, &subrank);

// At this point, we no longer need subcomm. Free it and reuse the variable.

MPI_Comm_free(&subcomm);

// Separate both group leaders (rank 0) into their own subcommunicator

MPI_Comm_split(comm, subrank == 0 ? 0 : MPI_UNDEFINED, 0, &subcomm);
if (subcomm != MPI_COMM_NULL) {
MPI_Gather(temp, local_n, MPI_DOUBLE, gResult, local_n, MPI_DOUBLE, 0, subcomm);
MPI_Comm_free(&subcomm);
}

// Free resources

free(temp);

结果将在后面的subcomm中排名0的gResult中,由于方式恰好在comm中排名0执行拆分。

我想并没有预期的那么简单,但这是在 MPI 中进行方便的集体操作的代价。


在侧节点上,在显示的代码中,您正在分配 temp1temp2 的长度为 local_m,而在所有集体调用中长度指定为 local_n。如果碰巧 local_n > local_m,则会发生堆损坏。

关于c++ - 在 MPI 中逐元素求和和收集数组元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43175423/

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