Last active
October 18, 2022 03:35
-
-
Save kaityo256/ac8b29168200b97a3e48b1b1afe0a26e to your computer and use it in GitHub Desktop.
MPI_Probe sample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <mpi.h> | |
#include <cstdio> | |
#include <vector> | |
int main(int argv, char **argc){ | |
MPI_Init(&argv, &argc); | |
int rank; | |
MPI_Comm_rank(MPI_COMM_WORLD, &rank); | |
int send_buf[] = {3,5,3,1,6,7,8,8}; | |
int count = sizeof(send_buf)/sizeof(int); | |
if (rank==0){ | |
printf("I will send %d data\n",count); | |
printf("Here are the data\n"); | |
for(int i=0;i<count;i++){ | |
printf("%d:%d\n",i, send_buf[i]); | |
} | |
MPI_Send(send_buf, count, MPI_INT, 1, 0, MPI_COMM_WORLD); | |
} | |
MPI_Barrier(MPI_COMM_WORLD); | |
if (rank==1){ | |
MPI_Status st; | |
MPI_Probe(0, 0, MPI_COMM_WORLD, &st); | |
int size = st._ucount/sizeof(int); | |
printf("I will receive %d data from rank %d\n",size, st.MPI_SOURCE); | |
std::vector<int> recv_buf(size); | |
MPI_Recv(recv_buf.data(), size, MPI_INT, 0, 0, MPI_COMM_WORLD, &st); | |
printf("Here are the data.\n"); | |
for(int i=0;i<size;i++){ | |
printf("%d:%d\n",i, recv_buf[i]); | |
} | |
} | |
MPI_Finalize(); | |
} |
Author
kaityo256
commented
Oct 18, 2022
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment