From 6367ea49c192a898de4429647786c29c90cf2f92 Mon Sep 17 00:00:00 2001
From: "raphael.bach" <raphael.bach@etu.hesge.ch>
Date: Mon, 18 Jul 2022 22:08:01 +0200
Subject: [PATCH] Add `fmpi_partition_block_2d()`

---
 src/fmpi_domain.c | 40 ++++++++++++++++++++++++++++++++++++++--
 1 file changed, 38 insertions(+), 2 deletions(-)

diff --git a/src/fmpi_domain.c b/src/fmpi_domain.c
index 62d1ea8..0761367 100644
--- a/src/fmpi_domain.c
+++ b/src/fmpi_domain.c
@@ -27,7 +27,6 @@
 #include <assert.h>
 #include <stdlib.h> // size_t, NULL, malloc(), free()
 #include <string.h> // memcpy()
-#include <stdio.h>
 // Internal
 #include "fmpi_data.h"
 #include "fmpi_stencil.h"
@@ -46,6 +45,12 @@
 static struct fmpi_data fmpi_partition_block_1d(
     const struct fmpi_ctx * ctx, const struct fmpi_data * data
 );
+/*------------------------------------------------------------------------------
+    fmpi_partition_block_2d()
+------------------------------------------------------------------------------*/
+static struct fmpi_data fmpi_partition_block_2d(
+    const struct fmpi_ctx * ctx, const struct fmpi_data * data
+);
 /*------------------------------------------------------------------------------
     fmpi_halo_1d()
 ------------------------------------------------------------------------------*/
@@ -152,7 +157,38 @@ static struct fmpi_data fmpi_partition_block_1d(
     };
 }
 /*------------------------------------------------------------------------------
-    fmpi_create_halo()
+    fmpi_partition_block_2d()
+------------------------------------------------------------------------------*/
+static struct fmpi_data fmpi_partition_block_2d(
+    const struct fmpi_ctx * const ctx, const struct fmpi_data * const data
+) {
+    assert(ctx != NULL);
+    assert(data != NULL);
+
+    size_t dim_len[1] = {0};
+    if(fmpi_mpi_dims_create(ctx->mpi, dim_len, 1) != FMPI_SUCCESS) {
+        FMPI_RAISE_ERROR(ctx->err_handler, "FMPI",
+            "fmpi_mpi_dims_create() failed!"
+        );
+    }
+    const size_t rank = (size_t)ctx->mpi->rank;
+    const size_t cnt_per_proc = data->cnt/dim_len[0];
+    const size_t rem = data->cnt % dim_len[0];
+    const size_t cnt = (rank < rem) ? (cnt_per_proc + 1) : cnt_per_proc;
+    const size_t size = cnt * data->type.size;
+    const size_t offset = (rank * size) + (rank < rem ? 0 : (rem * data->type.size));
+    return (struct fmpi_data){
+        .type = data->type,
+        .cnt = cnt,
+        .size = size,
+        .dim_len = {data->dim_len[0], data->dim_len[1]/dim_len[0], 1},
+        .dim_cnt = 2,
+        .raw = (char *)data->raw + offset,
+        .gpu = NULL
+    };
+}
+/*------------------------------------------------------------------------------
+    fmpi_halo_1d()
 ------------------------------------------------------------------------------*/
 static struct fmpi_data fmpi_halo_1d(
     const struct fmpi_ctx * const ctx, const struct fmpi_data data,
-- 
GitLab