C语言gather结束循环,c - 如何在循环中重用MPI_Scatter和MPI_Gather - 堆栈内存溢出

本文档展示了如何在MPI环境中使用MPI_Scatter和MPI_Gather进行多轮计算。代码示例演示了如何在进程0上初始化一个数组,然后通过Scatter和Gather将其分散和聚合到各个进程中进行运算。在每轮计算后,更新的数组会返回给进程0,以便在下一轮迭代中重复此过程。最终,所有计算完成后,将在所有进程中打印出结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我正在尝试学习如何多次使用MPI_Scatter和MPI_Gather ,并在等待这两个MPI函数完成后打印出结果。 在程序顶部的进程0,我想使用一个调用Scatter and Gather的while循环。 完成所有计算后,我想将此数组发送回这些函数以进行更多计算。 我已经在下面的代码中解释了我要做什么。 /*.....*/中的注释是我要完成的任务。

以下代码使用4个处理器运行。

:$ mpicc test.c -o test

:$ mpirun -np 4 test

#include

#include

#include

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

int size, rank;

MPI_Init(&argc, &argv);

MPI_Comm_size(MPI_COMM_WORLD, &size);

MPI_Comm_rank(MPI_COMM_WORLD, &rank);

int globaldata[8];

int localdata[2];

int counter, i;

if (rank == 0)

{

for (i=0; i

globaldata[i] = 0;

/*counter=0;

do

{

counter++; */

printf("Sending at Processor %d has data: ", rank);

for (i=0; i

printf("%d ", globaldata[i]);

printf("\n");

/*After MPI_Gather is done, I want to get the newly assined array here.

Now the globaldata array should hold values: 0 0 1 1 2 2 3 3

Therefore, in the next iteration of this while loop, these array values need

to be send for a new calculation with Scatter & Gather

}while(counter<2);*/

//Following need to be executed after all the scatter and gather has completed

printf("Finally at Processor %d has data: ", rank);

for (i=0; i

printf("%d ", globaldata[i]);

printf("\n");

}

MPI_Scatter(globaldata, 2, MPI_INT, &localdata, 2, MPI_INT, 0, MPI_COMM_WORLD);

localdata[0]= localdata[0]+rank;

localdata[1]= localdata[1]+rank;

MPI_Gather(&localdata, 2, MPI_INT, globaldata, 2, MPI_INT, 0, MPI_COMM_WORLD);

if (rank == 0) {//Currently I can only see the newly assinged array values if I print out the result at the bottom

printf("At the bottom, Processor %d has data: ", rank);

for (i=0; i

printf("%d ", globaldata[i]);

printf("\n");

}

MPI_Finalize();

return 0;

}

以上是我要尝试做的更多解释:我想将我的globaldata数组发送到所有处理器。 然后获取更新的globaldata数组。 获得更新后的数组后,我想将该数组重新发送回所有其他进程以进行更多计算。 我已经编写了以下代码,使用MPI_Send和MPI_Recv做类似的工作。 在这里,我使用MPI_Send将阵列发送到所有处理器。 然后,此数组将对其进行更改,并将其发送回根目录/进程0。一旦获得修改后的数组, do while循环将再次运行并执行更多计算。 我想要做的是:以类似的方式使用MPI_Scatter和MPI_Gather。 在哪里获取更新的globaldata数组并将其发送回MPI_Scatter和MPI_Gather以再次更改该数组

#include

#include

#include

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

int size, rank;

MPI_Init(&argc, &argv);

MPI_Comm_size(MPI_COMM_WORLD, &size);

MPI_Comm_rank(MPI_COMM_WORLD, &rank);

int globaldata[8];

int counter, i;

if (rank == 0)

{

for (i=0; i

globaldata[i] = 0;

counter=0;

do

{ /*becase of this do while loop "globaldata" array will always be updated and resend for more caculations*/

counter++;

printf("Sending at Processor %d has data: ", rank);

for (i=0; i

printf("%d ", globaldata[i]);

printf("\n");

for(i = 0; i < 4; i++)

{

MPI_Send(&globaldata, 8, MPI_INT, i, 0, MPI_COMM_WORLD);

}

for(i = 1; i < 4; i++)

{

MPI_Recv(&globaldata, 8, MPI_INT, i, 99, MPI_COMM_WORLD, MPI_STATUS_IGNORE);

}

}while(counter<2);

/*Following executes after all the above calculations has completed*/

printf("Finally at Processor %d has data: ", rank);

for (i=0; i

printf("%d ", globaldata[i]);

printf("\n");

}

counter=0;

do

{

counter++;

MPI_Recv(&globaldata, 8, MPI_INT, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);

globaldata[rank]=globaldata[rank]+rank;

globaldata[rank+1]=globaldata[rank+1]+rank;

MPI_Send(&globaldata, 8, MPI_INT, 0, 99, MPI_COMM_WORLD);

}while(counter<2);

MPI_Finalize();

return 0;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值