zoukankan      html  css  js  c++  java
  • MPI消息传递MPI_Sendrecv的用法

    利用mpi求解微分方程时,经常会遇到不同进程的通讯,特别是如下形式的通讯:

        进程0->进程1->进程2->进程3...->进程n->进程0

    这时,若单纯的利用MPI_Send, MPI_Recv函数进行通讯的话,容易造成死锁,下面介绍MPI_Sendrecv的来解决这个问题。顾名思义,MPI_Sendrecv表示的作用是将本进程的信息发送出去,并接收其他进程的信息,其调用方式如下:

    MPI_Sendrecv( void *sendbuf //initial address of send buffer 
                  int sendcount //number of entries to send 
                  MPI_Datatype sendtype //type of entries in send buffer 
                  int dest //rank of destination 
                  int sendtag //send tag 
                  void *recvbuf //initial address of receive buffer
                  int recvcount //max number of entries to receive 
                  MPI_Datatype recvtype //type of entries in receive buffer  (这里数目是按实数的数目,若数据类型为MPI_COMPLEX时,传递的数目要乘以2) 
             int source //rank of source
             int recvtag //receive tag
             MPI_Comm comm //group communicator
             MPI_Status status //return status;

     下面给出一个实例:

    #include<stdio.h>
    #include "mpi.h"
    #include <math.h>
    #define n 4
    int 
    main(int argc, char* argv[]){
        int nProcs, Rank, i;
        double A0[n],A1[n];
        MPI_Status status;
    
        MPI_Init(&argc, &argv);
        MPI_Comm_size(MPI_COMM_WORLD, &nProcs);
        MPI_Comm_rank(MPI_COMM_WORLD, &Rank);
    
        for(int i=0; i<n; i++){
          A0[i] = Rank;
          A1[i] = Rank;
        }
    
        printf("
    Before exchange A0 A1:
    ");
        for(i=0;i<n;i++){
            printf("rank:%d	%f	%f
    ",Rank, A0[i], A1[i]);
        }    
    
        int rightrank = (Rank + 1) % nProcs;
        int leftrank = (Rank + nProcs-1)%nProcs;
    
        MPI_Barrier(MPI_COMM_WORLD);
        MPI_Sendrecv(A0, n, MPI_DOUBLE, rightrank,990,
                 A1, n, MPI_DOUBLE, leftrank,990,  MPI_COMM_WORLD,&status);
    
        MPI_Finalize();
    
        printf("After exchange A0 A1
    ");
        for(i=0;i<n;i++){
            printf("rank:%d %f	%f
    ",Rank,  A0[i], A1[i]);
        }
    }
    

     下面这条语句表示:将进程为Rank的A0发送到rightrank进程,并接收来自leftrank的A1。

      MPI_Sendrecv(A0, n, MPI_DOUBLE, rightrank,990,
                 A1, n, MPI_DOUBLE, leftrank,990,  MPI_COMM_WORLD,&status);
    

     得到的数值结果如下:

    Before exchange A0 A1
    rank:0	0.000000	0.000000
    rank:0	0.000000	0.000000
    rank:0	0.000000	0.000000
    rank:0	0.000000	0.000000
    
    Before exchange A0 A1
    rank:1	1.000000	1.000000
    rank:1	1.000000	1.000000
    rank:1	1.000000	1.000000
    rank:1	1.000000	1.000000
    
    Before exchange A0 A1
    rank:2	2.000000	2.000000
    rank:2	2.000000	2.000000
    rank:2	2.000000	2.000000
    rank:2	2.000000	2.000000
    
    Before exchange A0 A1
    rank:3	3.000000	3.000000
    rank:3	3.000000	3.000000
    rank:3	3.000000	3.000000
    rank:3	3.000000	3.000000
    
    After exchange A0 A1 
    rank:1 1.000000	0.000000
    rank:1 1.000000	0.000000
    rank:1 1.000000	0.000000
    rank:1 1.000000	0.000000
    After exchange A0 A1
    rank:2 2.000000	1.000000
    rank:2 2.000000	1.000000
    rank:2 2.000000	1.000000
    rank:2 2.000000	1.000000
    After exchange A0 A1
    rank:3 3.000000	2.000000
    rank:3 3.000000	2.000000
    rank:3 3.000000	2.000000
    rank:3 3.000000	2.000000
    After exchange A0 A1
    rank:0 0.000000	3.000000
    rank:0 0.000000	3.000000
    rank:0 0.000000	3.000000
    rank:0 0.000000	3.000000
    

      

  • 相关阅读:
    模仿商品分类点击效果
    Element MenuNav刷新后点击菜单保留选中状态
    element后端管理布局
    Element NavMenu动态生成导航菜单
    谁的速度快!谁背锅(技术解析)
    “非科班自学”复盘两个月时间在年底成功拿下了字节、阿里offer,入职了字节!
    用了这么久,你真的明白 HttpClient的实现原理了吗?
    求你了,不要再在对外接口中使用枚举类型了
    Docker 实战总结(非常全面),收藏了!
    Service层和Dao层真的有必要每个类都加上接口吗?
  • 原文地址:https://www.cnblogs.com/kuangsyx/p/7903774.html
Copyright © 2011-2022 走看看