From dd4c9aa470fcbbf6170b38352e2fd1f01cd228c9 Mon Sep 17 00:00:00 2001
From: "raphael.bach" <raphael.bach@etu.hesge.ch>
Date: Tue, 14 Jun 2022 22:17:21 +0200
Subject: [PATCH] Add `fmpi_partition_block_1d()` in `fmpi_domain` module

---
 src/fmpi_domain.c | 55 +++++++++++++++++++++++++++++++++++++----------
 src/fmpi_task.c   |  5 +++++
 2 files changed, 49 insertions(+), 11 deletions(-)

diff --git a/src/fmpi_domain.c b/src/fmpi_domain.c
index a3b415f..79f359c 100644
--- a/src/fmpi_domain.c
+++ b/src/fmpi_domain.c
@@ -29,9 +29,22 @@
 // Internal
 #include "fmpi_data.h"
 #include "internal/fmpi_ctx.h"
+#include "internal/fmpi_error.h"
 #include "internal/fmpi_mpi.h"
 /*==============================================================================
-    PUBLIC FUNCTION
+    PRIVATE FUNCTION DECLARATION
+==============================================================================*/
+/*------------------------------------------------------------------------------
+    fmpi_partition_block_1d()
+------------------------------------------------------------------------------*/
+/**
+ * TODO
+ */
+static struct fmpi_data * fmpi_partition_block_1d(
+    const struct fmpi_ctx * ctx, const struct fmpi_data * data
+);
+/*==============================================================================
+    PUBLIC FUNCTION DEFINITION
 ==============================================================================*/
 //! @todo Handle creation of domain with data in 2D and 3D
 /*------------------------------------------------------------------------------
@@ -44,29 +57,49 @@ struct fmpi_domain fmpi_new_domain(
     const size_t proc_cnt = (size_t)ctx->mpi->size;
     struct fmpi_domain domain = {
         .data = data,
+        .parts = NULL,
         .part_cnt = proc_cnt
     };
-    domain.parts = malloc(proc_cnt * sizeof(*domain.parts));
+    domain.parts = fmpi_partition_block_1d(ctx, &data);
     if(domain.parts == NULL) {
-        FMPI_RAISE_ERROR(ctx->err_handler, "FMPI", "malloc(domain.parts) failed!");
+        FMPI_RAISE_ERROR(ctx->err_handler, "FMPI", "fmpi_partition_block_1d() failed!");
     }
-    const size_t cnt_per_proc = data.cnt/proc_cnt;
-    size_t rem = data.cnt % proc_cnt;
+    return domain;
+}
+/*==============================================================================
+    PRIVATE FUNCTION DEFINITION
+==============================================================================*/
+/*------------------------------------------------------------------------------
+    fmpi_partition_block_1d()
+------------------------------------------------------------------------------*/
+static struct fmpi_data * fmpi_partition_block_1d(
+    const struct fmpi_ctx * const ctx, const struct fmpi_data * const data
+) {
+    assert(ctx != NULL);
+    assert(data != NULL);
+    const size_t proc_cnt = (size_t)ctx->mpi->size;
+    struct fmpi_data * parts = malloc(proc_cnt * sizeof(*parts));
+    if(parts == NULL) {
+        FMPI_RAISE_ERROR(ctx->err_handler, "FMPI", "malloc(parts) failed!");
+        return NULL;
+    }
+    const size_t cnt_per_proc = data->cnt/proc_cnt;
+    size_t rem = data->cnt % proc_cnt;
     size_t offset = 0;
     for(size_t i = 0; i < proc_cnt; i++) {
         const size_t cnt = rem != 0 ? cnt_per_proc + 1 : cnt_per_proc;
         rem = rem != 0 ? rem - 1 : rem;
-        const size_t size = cnt * data.type_size;
-        domain.parts[i] = (struct fmpi_data){ \
-            .type = data.type,
-            .type_size = data.type_size,
+        const size_t size = cnt * data->type_size;
+        parts[i] = (struct fmpi_data){ \
+            .type = data->type,
+            .type_size = data->type_size,
             .cnt = cnt,
             .size = size,
             .dim_len = {cnt, 1, 1},
             .dim_cnt = 1,
-            .start = (char *)data.start + offset
+            .start = (char *)data->start + offset
         };
         offset += size;
     }
-    return domain;
+    return parts;
 }
diff --git a/src/fmpi_task.c b/src/fmpi_task.c
index 2995731..e65be4d 100644
--- a/src/fmpi_task.c
+++ b/src/fmpi_task.c
@@ -30,6 +30,7 @@
 #include "fmpi_domain.h"
 #include "fmpi_stencil.h"
 #include "internal/fmpi_ctx.h"
+#include "internal/fmpi_error.h"
 #include "internal/fmpi_futhark.h"
 #include "internal/fmpi_futhark_entry.h"
 #include "internal/fmpi_mpi.h"
@@ -55,6 +56,10 @@ struct fmpi_task fmpi_task_register(
     const size_t rank = (size_t)ctx->mpi->rank;
     for(size_t i = 0; i < task.args.cnt; i++) {
         task.domains[i] = fmpi_new_domain(ctx, args->in[i]);
+        if(task.domains[i].parts == NULL) {
+            FMPI_RAISE_ERROR(ctx->err_handler, "FMPI", "fmpi_new_domain() failed!");
+            continue;
+        }
         void * start = fmpi_futhark_new_data(
             ctx->fut, task.domains[i].parts[rank].start, task.domains[i].data.type,
             task.domains[i].data.dim_cnt,
-- 
GitLab