gpt4 book ai didi

c - 在MPI中,如何编写下面的程序 等待所有计算完成

转载 作者:太空狗 更新时间:2023-10-29 17:22:39 26 4
gpt4 key购买 nike

我是 MPI 的新手,这个程序是用 C 语言编写的。在下面的程序中,我要求其他处理器打印消息。但是我想在所有这些其他进程完成后在进程/级别 0 打印“结束”消息。为了运行这个程序,我使用了 4 个处理器并遵循命令 mpicc file.c -o objfilempirun -np 4 objfile
如果可能,请给我举个例子。

#include <mpi.h>
#include <stdio.h>
#include <unistd.h>

int main(int argc, char** argv)
{
MPI_Init(&argc, &argv);
int world_rank;
MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);
int world_size;
MPI_Comm_size(MPI_COMM_WORLD, &world_size);

int i;
double centroid[3];/*ignore this array*/

if (world_rank == 0)
{
int destination;
for (i=0; i<3; i++)
{
/*Ignore centroid buffer been sent for now*/

destination = i+1;/*destination rank or process*/
MPI_Send(&centroid, 3, MPI_DOUBLE, destination, 0, MPI_COMM_WORLD);
}

printf("\nEND: This need to print after all MPI_Send/MPI_Recv has been completed\n\n");
}
else
{
MPI_Recv(&centroid, 3, MPI_DOUBLE, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
sleep(1); /*This represent many calculations that will happen here later, instead of sleep*/
printf("Printing at Rank/Process number: %d\n", world_rank);
}


MPI_Finalize();
return 0;
}

结果:

END:这需要在所有 MPI_Send/MPI_Recv 完成后打印
列印/工序数:2
列印/工序数:3
打印在Rank/Process number: 1

请修改此代码或向我展示如何等待所有这些其他处理器完成的示例

最佳答案

好吧,你快到了。所缺少的只是为所有进程调用 MPI_Barrier()。这可以通过以下方式完成:

#include <mpi.h>
#include <stdio.h>
#include <unistd.h>

int main(int argc, char** argv)
{
MPI_Init(&argc, &argv);
int world_rank;
MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);
int world_size;
MPI_Comm_size(MPI_COMM_WORLD, &world_size);

int i;
double centroid[3];/*ignore this array*/

if (world_rank == 0)
{
int destination;
for (i=0; i<3; i++)
{
/*Ignore centroid buffer been sent for now*/

destination = i+1;/*destination rank or process*/
MPI_Send(&centroid, 3, MPI_DOUBLE, destination, 0, MPI_COMM_WORLD);
}
MPI_Barrier(MPI_COMM_WORLD);
printf("\nEND: This need to print after all MPI_Send/MPI_Recv has been completed\n\n");
}
else
{
MPI_Recv(&centroid, 3, MPI_DOUBLE, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
sleep(1); /*This represent many calculations that will happen here later, instead of sleep*/
printf("Printing at Rank/Process number: %d\n", world_rank);
MPI_Barrier(MPI_COMM_WORLD);
}


MPI_Finalize();
return 0;
}

添加此屏障后,代码在我的笔记本电脑上显示:

~/tmp$ mpirun -n 4 ./a.out 
Printing at Rank/Process number: 1
Printing at Rank/Process number: 2
Printing at Rank/Process number: 3

END: This need to print after all MPI_Send/MPI_Recv has been completed

注意:在这种情况下,排名 1 到 3 的打印是有序的,但这只是偶然,因为这可能以任何顺序发生。实际上,上面的代码将保证在进程 #0 和其他进程之间调用 printf() 的顺序,但它不能保证打印到屏幕的顺序,因为缓冲这样的事情可能会发生并把事情搞砸。
公平地说,它应该适用于大多数环境(大部分时间)。但严格来说,这并不能保证。

关于c - 在MPI中,如何编写下面的程序 等待所有计算完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39605351/

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