Skip to content

Instantly share code, notes, and snippets.

@kaityo256
Created March 3, 2020 05:19
Show Gist options
  • Save kaityo256/8397eb94c57e55e10651e96e927501a1 to your computer and use it in GitHub Desktop.
Save kaityo256/8397eb94c57e55e10651e96e927501a1 to your computer and use it in GitHub Desktop.
MPI_Probe sample
#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();
}
@kaityo256
Copy link
Author

$ mpic++ test.cpp
$ mpirun -np 2 ./a.out
Sent: Abracadabra
Recieved: Abracadabra

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment