# Introduction to MPI

Aveuh
69.7K views

### Statement

In this exercise, we are going to setup a simple program with 3 processes. Each process will have a different role. Process #0 will read data (a buffer of doubles) on stdin, and send this data to the other processes. This will be done in two steps : first the process will send the number of elements it read from stdin, then the actual buffer. We do this, so processes #1 and #2 are able to create the reception buffers with the right size. Then every process will sum some elements and print the resulting value on stdout :

• Process 0 will sum and display all the elements of the buffer
• Process 1 will sum and display the positive elements
• Process 2 will sum and display the negative elements

The basic broadcasting function in MPI is MPI_Bcast, which is defined as follows :

int MPI_Bcast(void* buffer, int count, MPI_Datatype datatype, int root, MPI_Comm comm);


There is also a non-blocking version that works as the non-blocking p2p communications (and that should be followed by MPI_Test and MPI_Wait)

int MPI_Ibcast(void *buffer, int count, MPI_Datatype datatype, int root, MPI_Comm comm, MPI_Request *request)


All parameters to these functions should be pretty obvious now. root is the id of the process sending the data, all other processes will be receivers and, as such, the values in buffer will be overwritten.

This playground was created on Tech.io, our hands-on, knowledge-sharing platform for developers.
#include <iostream>
#include <iomanip>
#include <mpi.h>
int main(int argc, char **argv) {
MPI_Init(&argc, &argv);
int rank, size;
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
// Input reading for process 0
int n_elements;
double *buffer;
if (rank == 0) {
std::cin >> n_elements;
buffer = new double[n_elements];
for (int i=0; i < n_elements; ++i)
std::cin >> buffer[i];
}
// 1- Broadcast the value n_elements to all processes
// [...]
// Here we create the buffers for the non-root processes now that
// we have n_elements
if (rank != 0)
buffer = new double[n_elements];
// 2- Broadcast the buffer to every process
// NOTE : The type here should be MPI_DOUBLE not MPI_FLOAT !
// [...]
// 3- Compute the sum depending on the rank
double sum = 0.0;
// [...]
// Printing the result and terminating the program
// Precision is set high for the validation process, please do not modify this.
std::cout << std::setprecision(16) << sum << std::endl;
MPI_Finalize();
delete [] buffer;
return 0;
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Join the CodinGame community on Discord to chat about puzzle contributions, challenges, streams, blog articles - all that good stuff!
Online Participants