Created
March 3, 2020 05:19
-
-
Save kaityo256/8397eb94c57e55e10651e96e927501a1 to your computer and use it in GitHub Desktop.
MPI_Probe sample
This file contains hidden or 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 <cstdio> | |
| #include <mpi.h> | |
| #include <string> | |
| void send() { | |
| std::string str = "Abracadabra"; | |
| MPI_Send(str.data(), str.size(), MPI_CHAR, 1, 0, MPI_COMM_WORLD); | |
| printf("Sent: %s\n", str.c_str()); | |
| } | |
| void receive() { | |
| MPI_Status status; | |
| MPI_Probe(0, 0, MPI_COMM_WORLD, &status); | |
| int length = 0; | |
| MPI_Get_count(&status, MPI_CHAR, &length); | |
| std::unique_ptr<char[]> buf(new char[length]); | |
| MPI_Recv(buf.get(), length, MPI_CHAR, 0, 0, MPI_COMM_WORLD, &status); | |
| printf("Recieved: %s\n", buf.get()); | |
| } | |
| int main(int argc, char **argv) { | |
| MPI_Init(&argc, &argv); | |
| int rank = 0; | |
| MPI_Comm_rank(MPI_COMM_WORLD, &rank); | |
| if (rank == 0) { | |
| send(); | |
| } else { | |
| receive(); | |
| } | |
| MPI_Finalize(); | |
| } |
Author
kaityo256
commented
Mar 3, 2020
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment