Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
MPI Basic Usage
#include <iostream>
#include <array>
#include <mpi.h>
using message_t = std::array<int, 3>;
void printMessage(const int rank, const message_t& message)
{
std::cout << "Process " << rank << ": ";
for (auto i : message) {
std::cout << i << " ";
}
std::cout << std::endl;
}
int main(int argc, char** argv)
{
// Initialize MPI; should come as early in the program as possible
MPI_Init(&argc, &argv);
// Get the number of processes in the global process group (communicator)
int numProc;
MPI_Comm_size(MPI_COMM_WORLD, &numProc);
// Get the unique identifier of the current process inside the process group
int myRank;
MPI_Comm_rank(MPI_COMM_WORLD, &myRank);
std::cout << "Hello! I am process " << myRank + 1
<< " of " << numProc
<< std::endl;
MPI_Barrier(MPI_COMM_WORLD);
// Blocking send/receive between processes 0 and 1
using message_t = std::array<int, 3>;
if (myRank == 0) {
const int messageTag = 111;
const int destinationRank = 1;
const auto message = message_t{{1, 2, 3}};
MPI_Send(message.data(), message.size(), MPI_INT, destinationRank,
messageTag, MPI_COMM_WORLD);
std::cout << "Process " << myRank << " sent message " << messageTag << std::endl;
} else if (myRank == 1) {
const int messageTag = 111;
const int sourceRank = 0;
message_t message;
MPI_Status status;
MPI_Recv(message.data(), message.size(), MPI_INT, sourceRank,
messageTag, MPI_COMM_WORLD, &status);
std::cout << "Process " << myRank << " received message " << messageTag << std::endl;
printMessage(myRank, message);
}
// Non-blocking send/receive between processes 0 and 1
if (myRank == 0) {
const int messageTag = 112;
const int destinationRank = 1;
const auto message = message_t{{4, 5, 6}};
MPI_Request request;
// Begin send
MPI_Isend(message.data(), message.size(), MPI_INT, destinationRank,
messageTag, MPI_COMM_WORLD, &request);
MPI_Status status;
MPI_Wait(&request, &status); // This call ensures that sending the message has completed
std::cout << "Process " << myRank << " sent message " << messageTag << std::endl;
} else if (myRank == 1) {
const int messageTag = 112;
const int sourceRank = 0;
message_t message;
MPI_Request request;
// Begin receive
MPI_Irecv(message.data(), message.size(), MPI_INT, sourceRank,
messageTag, MPI_COMM_WORLD, &request);
MPI_Status status;
MPI_Wait(&request, &status); // This call ensures that sending the message has completed
std::cout << "Process " << myRank << " received message " << messageTag << std::endl;
printMessage(myRank, message);
}
// Reduction on process 0: sum the rank (unique id) of all processes and print on process 0
int result;
MPI_Reduce(&myRank, &result, 1, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD);
if (myRank == 0)
{
// Result should be (numProc - 1) * numProc / 2
std::cout << "Process " << myRank << " obtained result: " << result << std::endl;
}
// Shutdown MPI; ideally, it's the last thing before exiting
MPI_Finalize();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.