MPI 只支持广播吗?

Does MPI support only broadcasting?

我想实现的是将部分结果广播给其他线程,并在不同的代码行接收其他线程的结果,可以表示为如下伪代码:

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 来实现?

What I want to achieve is to broadcast partial result to other threads and receive other threads' result at a different line of code, it can be expressed as the following pseudo code:

通常,在 MPI 和这种情况下,人们倾向于使用术语 process 而不是 thread:

The question is, MPI_Bcast/MPI_Ibcast do the send and receive in the same place, what I want is separate send and receive.

这是典型用例 MPI_Allreduce:

Combines values from all processes and distributes the result back to all processes

所以一个说明你的伪代码的例子:

#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);

这将基本上获得所有进程的变量my_local_optimal的最大值(MPI_MAX)并将该值存储到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,但是,您需要使用定义最佳或非最佳的操作(在您的代码中)。