From f633bb0afbb8382707630b066d16e43a61c61163 Mon Sep 17 00:00:00 2001
From: "raphael.bach" <raphael.bach@etu.hesge.ch>
Date: Sun, 26 Jun 2022 22:18:52 +0200
Subject: [PATCH] Add `fmpi_mpi_reduce_in_place()`

---
 include/internal/fmpi_mpi.h |  7 +++++++
 src/fmpi_mpi.c              | 19 +++++++++++++++++++
 2 files changed, 26 insertions(+)

diff --git a/include/internal/fmpi_mpi.h b/include/internal/fmpi_mpi.h
index 91fbf91..cfeadf1 100644
--- a/include/internal/fmpi_mpi.h
+++ b/include/internal/fmpi_mpi.h
@@ -223,6 +223,13 @@ int fmpi_mpi_world_gather_in_place(
     const struct fmpi_mpi_ctx * ctx, void * buf, MPI_Datatype type,
     size_t send_cnt, size_t recv_cnt
 );
+/*------------------------------------------------------------------------------
+    fmpi_mpi_reduce_in_place()
+------------------------------------------------------------------------------*/
+int fmpi_mpi_reduce_in_place(
+    const struct fmpi_mpi_ctx * ctx, void * buf, size_t cnt, MPI_Datatype type,
+    MPI_Op op, int root, MPI_Comm comm
+);
 /*==============================================================================
     GUARD
 ==============================================================================*/
diff --git a/src/fmpi_mpi.c b/src/fmpi_mpi.c
index 959b054..83dacda 100644
--- a/src/fmpi_mpi.c
+++ b/src/fmpi_mpi.c
@@ -274,3 +274,22 @@ int fmpi_mpi_world_gather_in_place(
     }
     return err;
 }
+/*------------------------------------------------------------------------------
+    fmpi_mpi_reduce_in_place()
+------------------------------------------------------------------------------*/
+int fmpi_mpi_reduce_in_place(
+    const struct fmpi_mpi_ctx * const ctx, void * const buf, const size_t cnt,
+    MPI_Datatype type, MPI_Op op, const int root, MPI_Comm comm
+){
+    assert(ctx != NULL);
+    assert(buf != NULL);
+    assert(cnt <= INT_MAX);
+
+    int err = MPI_SUCCESS;
+    if(ctx->rank == root) {
+        err = MPI_Reduce(MPI_IN_PLACE, buf, (int)cnt, type, op, root, comm);
+    } else {
+        err = MPI_Reduce(buf, buf, (int)cnt, type, op, root, comm);
+    }
+    return fmpi_mpi_check_error(ctx, err, "MPI_Reduce");
+}
-- 
GitLab