MPI-send and recv

Send and Recv 通讯是一种piont to piont 点对点通讯
而Reduce 是集合通讯

#include <stdio.h>
#include <mpi.h>
#include <string.h>
int main(int argc, char **argv)
{
    int myid, numprocs, source;
    MPI_Status status;
    char message[100];
    char a[100];
    MPI_Init(&argc, &argv);
    MPI_Comm_rank(MPI_COMM_WORLD, &myid);
    MPI_Comm_size(MPI_COMM_WORLD, &numprocs);

    if (myid != 0)
    {
        strcpy(message, "hello world!");

        // your code here
        MPI_Send(message, 100, MPI_CHAR, 0, 99, MPI_COMM_WORLD);
        // end of your code
    }
    else
    { // myid == 0
        for (source = 1; source < numprocs; source++)
        {
            // your code here
            MPI_Recv(a, 100, MPI_CHAR, source, 99, MPI_COMM_WORLD, &status);
            // end of your code

            printf("%s\n", a);// int a 多开辟了空间出来,但是可见recv()的第一个参数只是一个容器用来装send发来的数据
        }
    }

    MPI_Finalize();
    return 0;
}

MPI_Send可以以两种不同的方式实现:

将消息复制到MPI设置的缓冲区并返回;
直到对应的MPI_Recv出现前都阻塞

1.MPI_Send调用返回时表明数据已被发出或被MPI系统复制,随后对发送缓冲区的修改不会改变所发送的数据;

2.MPI_Recv返回时表明数据接收已经完成;

posted @ 2023-01-29 16:49  MITE's_BKY  阅读(113)  评论(0)    收藏  举报