Skip to content

Commit

Permalink
Add demo for reductions
Browse files Browse the repository at this point in the history
  • Loading branch information
trossi committed Jun 26, 2024
1 parent 525563e commit e9f667c
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
25 changes: 25 additions & 0 deletions mpi/demos/reduce.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include <stdio.h>
#include <mpi.h>

int main(int argc, char *argv[])
{
int size, rank;
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &size);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);

printf("Hello from rank %d of %d\n", rank, size);

double data[2] = {1. * rank, -1. * rank};
printf("Rank %d data is (%.1f, %.1f)\n", rank, data[0], data[1]);

double sum_of_data[2];
MPI_Reduce(data, sum_of_data, 2, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD);
// MPI_Allreduce(data, sum_of_data, 2, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
// MPI_Allreduce(MPI_IN_PLACE, data, 2, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);

printf("Rank %d data after reduce is (%.1f, %.1f)\n", rank, data[0], data[1]);
printf("Rank %d sum_of_data after reduce is (%.1f, %.1f)\n", rank, sum_of_data[0], sum_of_data[1]);

MPI_Finalize();
}
10 changes: 10 additions & 0 deletions mpi/docs/05-collective-reductions.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ lang: en
MPI_Reduce(`sendbuf`{.input}, `recvbuf`{.output}, `count`{.input}, `datatype`{.input}, `op`{.input}, `root`{.input}, `comm`{.input})
: Combines values to the root process from all processes of the group

<p>
- Demo: `reduce.c`


# Global reduction

MPI_Allreduce(`sendbuf`{.input}, `recvbuf`{.output}, `count`{.input}, `datatype`{.input}, `op`{.input}, `comm`{.input})
Expand All @@ -53,6 +57,9 @@ MPI_Allreduce(`sendbuf`{.input}, `recvbuf`{.output}, `count`{.input}, `datatype`
<p>
- Similar to `MPI_Reduce` + `MPI_Bcast` but more efficient

<p>
- Demo: `reduce.c`


# Allreduce example: parallel dot product

Expand Down Expand Up @@ -93,6 +100,9 @@ call mpi_allreduce(rloc, r, 1, MPI_REAL, &
`call mpi_allreduce(a, a, n, mpi_real,...`
- One should employ `MPI_IN_PLACE` for this purpose
- Replacing the send buffer with the `MPI_IN_PLACE` clause informs MPI that the buffers are the same
<p>
- Demo: `reduce.c`


# Summary

Expand Down

0 comments on commit e9f667c

Please sign in to comment.