From 84cbefbfe829bb0b01664905de223035ac27758f Mon Sep 17 00:00:00 2001
From: "raphael.bach" <raphael.bach@etu.hesge.ch>
Date: Sun, 26 Jun 2022 22:27:58 +0200
Subject: [PATCH] Add `fmpi_mpi_world_reduce_in_place()`

---
 include/internal/fmpi_mpi.h |  7 +++++++
 src/fmpi_mpi.c              | 21 +++++++++++++++++++++
 src/fmpi_task.c             | 23 ++++++++++-------------
 3 files changed, 38 insertions(+), 13 deletions(-)

diff --git a/include/internal/fmpi_mpi.h b/include/internal/fmpi_mpi.h
index cfeadf1..5202625 100644
--- a/include/internal/fmpi_mpi.h
+++ b/include/internal/fmpi_mpi.h
@@ -230,6 +230,13 @@ 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
 );
+/*------------------------------------------------------------------------------
+    fmpi_mpi_world_reduce_in_place()
+------------------------------------------------------------------------------*/
+int fmpi_mpi_world_reduce_in_place(
+    const struct fmpi_mpi_ctx * ctx, void * buf, size_t cnt, MPI_Datatype type,
+    MPI_Op op
+);
 /*==============================================================================
     GUARD
 ==============================================================================*/
diff --git a/src/fmpi_mpi.c b/src/fmpi_mpi.c
index 83dacda..3eb7f10 100644
--- a/src/fmpi_mpi.c
+++ b/src/fmpi_mpi.c
@@ -293,3 +293,24 @@ int fmpi_mpi_reduce_in_place(
     }
     return fmpi_mpi_check_error(ctx, err, "MPI_Reduce");
 }
+/*------------------------------------------------------------------------------
+    fmpi_mpi_world_reduce_in_place()
+------------------------------------------------------------------------------*/
+int fmpi_mpi_world_reduce_in_place(
+    const struct fmpi_mpi_ctx * const ctx, void * const buf, const size_t cnt,
+    MPI_Datatype type, MPI_Op op
+){
+    assert(ctx != NULL);
+    assert(buf != NULL);
+    assert(cnt <= INT_MAX);
+
+    const int err = fmpi_mpi_reduce_in_place(
+        ctx, buf, cnt, type, op, ctx->root, ctx->world
+    );
+    if(err != FMPI_SUCCESS) {
+        FMPI_RAISE_ERROR(ctx->err_handler, "FMPI",
+            "fmpi_mpi_reduce_in_place() failed!"
+        );
+    }
+    return err;
+}
diff --git a/src/fmpi_task.c b/src/fmpi_task.c
index cfc1ef6..66c0178 100644
--- a/src/fmpi_task.c
+++ b/src/fmpi_task.c
@@ -180,22 +180,19 @@ int fmpi_task_finalize(
     assert(ctx != NULL);
     assert(task != NULL);
     if(op == FMPI_TASK_OP_NONE) {
-        return 0;
+        return FMPI_SUCCESS;
     }
     if(op == FMPI_TASK_OP_SUM) {
-        if(fmpi_mpi_is_root(ctx->mpi)) {
-            MPI_Reduce(
-                MPI_IN_PLACE, task->args.out.raw, (int)task->args.out.cnt,
-                fmpi_mpi_type(task->args.out.type.base), MPI_SUM,
-                ctx->mpi->root, ctx->mpi->world
-            );
-        } else {
-            MPI_Reduce(
-                task->args.out.raw, task->args.out.raw, (int)task->args.out.cnt,
-                fmpi_mpi_type(task->args.out.type.base), MPI_SUM,
-                ctx->mpi->root, ctx->mpi->world
+        const int err = fmpi_mpi_world_reduce_in_place(
+            ctx->mpi, task->args.out.raw, task->args.out.cnt,
+            fmpi_mpi_type(task->args.out.type.base), MPI_SUM
+        );
+        if(err != FMPI_SUCCESS) {
+            FMPI_RAISE_ERROR(ctx->err_handler, "FMPI",
+                "fmpi_mpi_world_reduce_in_place() failed!"
             );
         }
+        return err;
     }
-    return 0;
+    return FMPI_SUCCESS;
 }
-- 
GitLab