Skip to content

Commit

Permalink
Add demo for nonblocking collectives
Browse files Browse the repository at this point in the history
  • Loading branch information
trossi committed Jun 24, 2024
1 parent 4ea4665 commit 33b9599
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
47 changes: 47 additions & 0 deletions mpi/demos/nonblocking_collectives.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#include <stdio.h>
#include <unistd.h> // for sleep()
#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);
fflush(stdout);

double t0 = MPI_Wtime();

// Computing 1
usleep(1000 * rank);
double data1 = 1.0 * rank;

// Allreduce
double reduced;
#ifdef NONBLOCKING
MPI_Request request;
MPI_Iallreduce(&data1, &reduced, 1, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD, &request);
#else
MPI_Allreduce(&data1, &reduced, 1, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
#endif

// Computing 2 (independent of 1)
usleep(1000 * (size - rank));
double data2 = 2.0 * rank;

#ifdef NONBLOCKING
MPI_Wait(&request, MPI_STATUS_IGNORE);
#endif

// Computing 3 (requiring 1 and 2)
usleep(1000);
double data3 = reduced + data2;

double t1 = MPI_Wtime();

printf("Rank %d computing took %.2f ms, data: %6.1f, %6.1f, %6.1f\n", rank, (t1-t0) * 1e3, data1, data2, data3);

MPI_Finalize();
}
4 changes: 4 additions & 0 deletions mpi/docs/06-non-blocking-communication.md
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,10 @@ MPI_Ibcast(`buf`{.input}`fer`{.output}, `count`{.input}, `datatype`{.input}, `ro
![](img/green_arrow.png){width=1%} (Computation) work 2, not
involving data in the `MPI_Allreduce` operation

# Non-blocking collectives

- Demo: `nonblocking_collectives.c`


# Summary

Expand Down

0 comments on commit 33b9599

Please sign in to comment.