【问题标题】:Does MPI support only broadcasting?MPI 是否只支持广播?
【发布时间】:2021-06-29 03:15:18
【问题描述】:

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

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

【问题讨论】:

    标签: c performance parallel-processing mpi hpc


    【解决方案1】:

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

    通常,在 MPI 和此上下文中,人们倾向于使用术语 process 而不是 thread

    问题是,MPI_Bcast/MPI_Ibcast 在 同一个地方,我想要的是分开发送和接收。

    这是MPI_Allreduce典型用例:

    组合来自所有进程的值并将结果分发回 所有进程

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

    #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,但是,您需要使用(在您的代码中)定义什么是最佳的操作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-04
      • 1970-01-01
      • 2012-11-13
      • 2014-05-07
      • 1970-01-01
      • 2012-02-26
      相关资源
      最近更新 更多