我想要实现的是将部分结果广播到其他线程,并在不同的代码行接收其他线程的结果,它可以表示为以下伪代码:
if have any incoming message:
read the message and compare it with the local optimal
if is optimal:
update the local optimal
calculate local result
if local result is better than local optimal:
update local optimal
send the local optimal to others问题是,MPI_Bcast/MPI_Ibcast在同一个位置进行发送和接收,我想要的是单独发送和接收。我想知道MPI是否为我的目的提供了内置支持,还是只能通过在for循环中调用MPI_Send/MPI_Isend来实现?
发布于 2021-04-08 09:05:20
我想要实现的是将部分结果广播到其他线程,并在不同的代码行接收其他线程的结果,它可以表示为以下伪代码:
通常,在MPI和这种上下文中,人们倾向于使用术语process而不是线程。
问题是,MPI_Bcast/MPI_Ibcast在同一个位置进行发送和接收,我想要的是单独发送和接收。
这是全减的典型用例
组合来自所有进程的值,并将结果分发回所有进程。
因此,一个示例说明了您的伪代码:
#include <stdio.h>
#include <mpi.h>
int main(int argc,char *argv[]){
MPI_Init(NULL,NULL); // Initialize the MPI environment
int world_rank;
int world_size;
MPI_Comm_rank(MPI_COMM_WORLD,&world_rank);
MPI_Comm_size(MPI_COMM_WORLD,&world_size);
int my_local_optimal = world_rank;
MPI_Allreduce(&my_local_optimal, &my_local_optimal, 1, MPI_INT, MPI_MAX, MPI_COMM_WORLD);
printf("Step 1 : Process %d -> max local %d \n", world_rank, my_local_optimal);
my_local_optimal += world_rank * world_size;
MPI_Allreduce(&my_local_optimal, &my_local_optimal, 1, MPI_INT, MPI_MAX, MPI_COMM_WORLD);
printf("Step 2 : Process %d -> max local %d \n", world_rank, my_local_optimal);
MPI_Finalize();
return 0;
}因此,所有流程都从一个局部最优开始:
int my_local_optimal = world_rank;然后他们执行一个MPI_Allreduce
MPI_Allreduce(&my_local_optimal, &my_local_optimal, 1, MPI_INT, MPI_MAX, MPI_COMM_WORLD);它将基本上得到所有进程的变量MPI_MAX的最大值(即my_local_optimal ),并将该值存储到my_local_optimal中。
从概念上讲,上述方法与以下方法之间的区别:
if have any incoming message:
read the message and compare it with the local optimal
if is optimal:
update the local optimal您既没有显式地检查"if have any incoming message:",也没有显式地检查"if is optimal":,您只需计算进程间的最大值,并相应地更新本地最优值。这使得这种方法更容易处理。
在我的示例中,我使用了MPI_MAX,但是,您需要使用定义什么是最优的操作(在代码中)。
https://stackoverflow.com/questions/66916100
复制相似问题