Loading [MathJax]/jax/output/CommonHTML/jax.js

# Introduction to MPI

Aveuh
169.4K views

## Reductions - exercise 2

In the last exercise we have been computing a reduction on one process. In this one, we will use the MPI_Allreduce operator to compute a reduction on all processes and use the given result.

Consider the following problem. We have a list of points in three dimensions (so with three coordinates). We want to compute the distance of each point to the barycentre of the set. For this, we will use processes in parallel having points each. The algorithm will have to proceed in four steps :

• Each process will compute the sum of all of its own points (sum avery coordinate
• The program will then call the reduction to get the sum of all the points on all processes.
• Then, the barycentre position is given by dividing this sum by the number of points
• Finally, every process will compute the distance of each point to the barycentre, and print the result on stdout.

### MPI_Allreduce

As stated in the lesson, MPI_Allreduce computes a reduction just like MPI_Reduce but instead of storing the result on only one process, the result will be sent back to every process. The prototype is the following :

int MPI_Allreduce(void *sendbuf, void *recvbuf, int count, MPI_Datatype datatype, MPI_Op op, MPI_Comm comm);


As you can see, the prototype is the same as for MPI_Reduce except we don't need to specify a root on which the result will be stored.

### Euclidian distance

The distance that you are asked to compute is the Euclidian distance. For those of you who don't remember this distance, here is the formula :

Where are the coordinates of the current point and are the coordinates of the barycentre.

You have all you need to do the exercise. Good luck.

Barycentre
Create your playground on Tech.io
This playground was created on Tech.io, our hands-on, knowledge-sharing platform for developers.   Join the CodinGame community on Discord to chat about puzzle contributions, challenges, streams, blog articles - all that good stuff!
Online Participants