MPI:非阻塞實現節點間的迴圈訊息傳遞
非阻塞實現節點間的迴圈訊息傳遞
一、題目要求
MPI非阻塞方式實現節點間的迴圈訊息傳遞
二、思路
使用MPI_Isend、MPI_Irecv和MPI_Wait等函式;
三、我的程式碼
#include <stdio.h>
#include <string.h>
#include "mpi.h"
int main(int argc, char *argv[])
{
char sendmsg[100] = "asdfasdf";
char recvmsg[100] = "";
int myid, numprocs;
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &myid);
MPI_Comm_size(MPI_COMM_WORLD, &numprocs);
int dest = myid+1;
int source = myid-1;
if(myid == 0)
source = numprocs-1;
if(myid == numprocs-1)
dest = 0;
//傳送
MPI_Request request1;
MPI_Status status1;
MPI_Isend(sendmsg, strlen(sendmsg), MPI_CHAR, dest, 99, MPI_COMM_WORLD, &request1);
//接收
MPI_Request request2;
MPI_Status status2;
MPI_Irecv(recvmsg, 100 , MPI_CHAR, source, 99, MPI_COMM_WORLD, &request2);
MPI_Wait(&request2, &status2);
//列印
// printf("has send from %d to %d\n", myid, dest);
// printf("has recv from %d to %d\n", source, myid);
// printf("%s", recvmsg);
return 0;
}