From ccaad32b716919837ba8d048b879487be420385f Mon Sep 17 00:00:00 2001
From: "raphael.bach" <raphael.bach@etu.hesge.ch>
Date: Thu, 23 Jun 2022 18:20:15 +0200
Subject: [PATCH] Add `enum fmpi_task_type`

---
 examples/array_sum/main.c                    |  2 +-
 include/fmpi_task.h                          | 15 ++++++++++++---
 include/internal/generic/fmpi_task_generic.h | 12 ++++++------
 src/fmpi_task.c                              |  9 +++++++--
 4 files changed, 26 insertions(+), 12 deletions(-)

diff --git a/examples/array_sum/main.c b/examples/array_sum/main.c
index 6e0ee99..e804de8 100644
--- a/examples/array_sum/main.c
+++ b/examples/array_sum/main.c
@@ -32,7 +32,7 @@ int main(int argc, char * argv[])
     const size_t in_size = sizeof(in)/sizeof(T);
     T out = -1;
     struct fmpi_task array_sum_task = FMPI_TASK_REGISTER(
-        ctx, array_sum, fmpi_no_stencil(),
+        ctx, array_sum, FMPI_TASK_TYPE_SYNC, fmpi_no_stencil(),
         fmpi_data_out(ctx, &out),
         fmpi_data_1d_in(ctx, in, in_size)
     );
diff --git a/include/fmpi_task.h b/include/fmpi_task.h
index 01da477..c40dac7 100644
--- a/include/fmpi_task.h
+++ b/include/fmpi_task.h
@@ -83,6 +83,13 @@ typedef struct fmpi_task_args {
 typedef int (* fmpi_task_func)(
     const struct fmpi_ctx * ctx, const struct fmpi_task_args * args
 );
+/*------------------------------------------------------------------------------
+    fmpi_task_type
+------------------------------------------------------------------------------*/
+typedef enum fmpi_task_type {
+    FMPI_TASK_TYPE_SYNC,
+    FMPI_TASK_TYPE_ASYNC
+} fmpi_task_type;
 /*------------------------------------------------------------------------------
     fmpi_task
 ------------------------------------------------------------------------------*/
@@ -92,6 +99,7 @@ typedef int (* fmpi_task_func)(
 typedef struct fmpi_task {
     fmpi_task_func func;        //!< TODO
     const char * name; //!< Name of the function.
+    enum fmpi_task_type type; //!< Sync/async
     struct fmpi_task_args args; //!< TODO
     struct fmpi_domain domains[FMPI_TASK_ARGS_MAX]; //!< TODO
     struct fmpi_stencil stencil; //!< TODO
@@ -122,7 +130,8 @@ typedef struct fmpi_task {
  */
 struct fmpi_task fmpi_task_register(
     struct fmpi_ctx * ctx, fmpi_task_func func, const char * name,
-    struct fmpi_stencil stencil, const struct fmpi_task_args * args
+    enum fmpi_task_type type, struct fmpi_stencil stencil,
+    const struct fmpi_task_args * args
 );
 /*------------------------------------------------------------------------------
     fmpi_task_run_sync()
@@ -174,8 +183,8 @@ int fmpi_task_run_async(const struct fmpi_ctx * ctx, const struct fmpi_task * ta
 /*------------------------------------------------------------------------------
     FMPI_TASK_REGISTER()
 ------------------------------------------------------------------------------*/
-#define FMPI_TASK_REGISTER(ctx, func, stencil, ...) \
-    FMPI_TASK_REGISTER_IMPL(ctx, func, stencil, __VA_ARGS__)
+#define FMPI_TASK_REGISTER(ctx, func, type, stencil, ...) \
+    FMPI_TASK_REGISTER_IMPL(ctx, func, type, stencil, __VA_ARGS__)
 /*------------------------------------------------------------------------------
     FMPI_TASK_FUTHARK()
 ------------------------------------------------------------------------------*/
diff --git a/include/internal/generic/fmpi_task_generic.h b/include/internal/generic/fmpi_task_generic.h
index 29ac808..9b5bbcc 100644
--- a/include/internal/generic/fmpi_task_generic.h
+++ b/include/internal/generic/fmpi_task_generic.h
@@ -73,18 +73,18 @@ _Pragma("GCC diagnostic warning \"-Wincompatible-pointer-types\"")\
         return futhark_entry_##FUNC(ctx, out, FMPI_PRIV_TASK_ARGS_##N(args_in));  \
     }
 
-#define FMPI_TASK_REGISTER_IMPL(ctx, FUNC, stencil, ...) \
+#define FMPI_TASK_REGISTER_IMPL(ctx, FUNC, type, stencil, ...) \
     FMPI_PRIV_TASK_CONCAT(FMPI_PRIV_TASK_REGISTER_, CPL_ARG_COUNT(__VA_ARGS__)) \
-    (FUNC, ctx, stencil ,__VA_ARGS__)
+    (FUNC, ctx, type, stencil ,__VA_ARGS__)
 
-#define FMPI_PRIV_TASK_REGISTER_1(FUNC, ctx, stencil, arg_out) \
-    fmpi_task_register((ctx), FUNC##_0, #FUNC, (stencil), &(struct fmpi_task_args){ \
+#define FMPI_PRIV_TASK_REGISTER_1(FUNC, ctx, type, stencil, arg_out) \
+    fmpi_task_register((ctx), FUNC##_0, #FUNC, (type), (stencil), &(struct fmpi_task_args){ \
         .out = (arg_out), \
         .cnt = 0 \
     })
 
-#define FMPI_PRIV_TASK_REGISTER_N(N, FUNC, ctx, stencil, arg_out, ...) \
-    fmpi_task_register((ctx), FUNC##_##N, #FUNC, (stencil), &(struct fmpi_task_args){ \
+#define FMPI_PRIV_TASK_REGISTER_N(N, FUNC, ctx, type, stencil, arg_out, ...) \
+    fmpi_task_register((ctx), FUNC##_##N, #FUNC, (type), (stencil), &(struct fmpi_task_args){ \
         .in = {__VA_ARGS__}, \
         .out = (arg_out), \
         .cnt = N \
diff --git a/src/fmpi_task.c b/src/fmpi_task.c
index 4c37be6..a2babfb 100644
--- a/src/fmpi_task.c
+++ b/src/fmpi_task.c
@@ -27,6 +27,7 @@
 #include <assert.h>
 #include <stdbool.h>
 #include <stddef.h> // NULL, size_t
+#include <stdio.h>
 // Internal
 #include "fmpi_domain.h"
 #include "fmpi_stencil.h"
@@ -41,7 +42,8 @@
     fmpi_task_register()
 ------------------------------------------------------------------------------*/
 struct fmpi_task fmpi_task_register(
-    struct fmpi_ctx * const ctx, const fmpi_task_func func, const char * const name,
+    struct fmpi_ctx * const ctx, const fmpi_task_func func,
+    const char * const name, const enum fmpi_task_type type,
     const struct fmpi_stencil stencil, const struct fmpi_task_args * const args
 ){
     assert(ctx != NULL);
@@ -51,6 +53,7 @@ struct fmpi_task fmpi_task_register(
     struct fmpi_task task = {
         .func = func,
         .name = name,
+        .type = type,
         .args = *args,
         .stencil = stencil
     };
@@ -71,7 +74,9 @@ struct fmpi_task fmpi_task_register(
         task.args.in[i].start = start;
     }
     ctx->tasks[ctx->task_cnt++] = task;
-    fmpi_futhark_sync(ctx->fut);
+    if(task.type == FMPI_TASK_TYPE_SYNC) {
+        fmpi_futhark_sync(ctx->fut);
+    }
     return task;
 }
 /*------------------------------------------------------------------------------
-- 
GitLab