From a1f2133c5cbe5ed3ae4b9f74e93bec7479569f15 Mon Sep 17 00:00:00 2001
From: Matthieu Schaller <matthieu.schaller@durham.ac.uk>
Date: Sat, 25 Feb 2017 23:01:22 +0000
Subject: [PATCH] Added multipole communication tasks.

---
 src/part.c      | 7 +++++++
 src/part.h      | 1 +
 src/runner.c    | 2 ++
 src/scheduler.c | 6 ++++++
 src/task.c      | 5 +++--
 src/task.h      | 1 +
 6 files changed, 20 insertions(+), 2 deletions(-)

diff --git a/src/part.c b/src/part.c
index ecc5ca977a..98033097d7 100644
--- a/src/part.c
+++ b/src/part.c
@@ -27,6 +27,7 @@
 
 /* This object's header. */
 #include "error.h"
+#include "multipole.h"
 #include "part.h"
 
 /**
@@ -233,6 +234,7 @@ MPI_Datatype part_mpi_type;
 MPI_Datatype xpart_mpi_type;
 MPI_Datatype gpart_mpi_type;
 MPI_Datatype spart_mpi_type;
+MPI_Datatype multipole_mpi_type;
 
 /**
  * @brief Registers MPI particle types.
@@ -265,5 +267,10 @@ void part_create_mpi_types() {
       MPI_Type_commit(&spart_mpi_type) != MPI_SUCCESS) {
     error("Failed to create MPI type for sparts.");
   }
+  if (MPI_Type_contiguous(sizeof(struct multipole) / sizeof(unsigned char),
+                          MPI_BYTE, &multipole_mpi_type) != MPI_SUCCESS ||
+      MPI_Type_commit(&multipole_mpi_type) != MPI_SUCCESS) {
+    error("Failed to create MPI type for multipole.");
+  }
 }
 #endif
diff --git a/src/part.h b/src/part.h
index 4ed4b49096..e9a151f5b6 100644
--- a/src/part.h
+++ b/src/part.h
@@ -86,6 +86,7 @@ extern MPI_Datatype part_mpi_type;
 extern MPI_Datatype xpart_mpi_type;
 extern MPI_Datatype gpart_mpi_type;
 extern MPI_Datatype spart_mpi_type;
+extern MPI_Datatype multipole_mpi_type;
 
 void part_create_mpi_types();
 #endif
diff --git a/src/runner.c b/src/runner.c
index dec6c4e87a..e8fe6723bf 100644
--- a/src/runner.c
+++ b/src/runner.c
@@ -1786,6 +1786,8 @@ void *runner_main(void *data) {
             runner_do_recv_gpart(r, ci, 1);
           } else if (t->subtype == task_subtype_spart) {
             runner_do_recv_spart(r, ci, 1);
+          } else if (t->subtype == task_subtype_multipole) {
+            ci->ti_old_multipole = e->ti_current;
           } else {
             error("Unknown/invalid task subtype (%d).", t->subtype);
           }
diff --git a/src/scheduler.c b/src/scheduler.c
index e5e53096d8..9f0f0fd944 100644
--- a/src/scheduler.c
+++ b/src/scheduler.c
@@ -1220,6 +1220,9 @@ void scheduler_enqueue(struct scheduler *s, struct task *t) {
         } else if (t->subtype == task_subtype_spart) {
           err = MPI_Irecv(t->ci->sparts, t->ci->scount, spart_mpi_type,
                           t->ci->nodeID, t->flags, MPI_COMM_WORLD, &t->req);
+        } else if (t->subtype == task_subtype_multipole) {
+          err = MPI_Irecv(t->ci->multipole, 1, multipole_mpi_type,
+                          t->ci->nodeID, t->flags, MPI_COMM_WORLD, &t->req);
         } else {
           error("Unknown communication sub-type");
         }
@@ -1257,6 +1260,9 @@ void scheduler_enqueue(struct scheduler *s, struct task *t) {
         } else if (t->subtype == task_subtype_spart) {
           err = MPI_Isend(t->ci->sparts, t->ci->scount, spart_mpi_type,
                           t->cj->nodeID, t->flags, MPI_COMM_WORLD, &t->req);
+        } else if (t->subtype == task_subtype_multipole) {
+          err = MPI_Isend(t->ci->multipole, 1, multipole_mpi_type,
+                          t->cj->nodeID, t->flags, MPI_COMM_WORLD, &t->req);
         } else {
           error("Unknown communication sub-type");
         }
diff --git a/src/task.c b/src/task.c
index 21a3db9e8d..ff6bb9bee2 100644
--- a/src/task.c
+++ b/src/task.c
@@ -69,9 +69,10 @@ const char *taskID_names[task_type_count] = {"none",
                                              "cooling",
                                              "sourceterms"};
 
+/* Sub-task type names. */
 const char *subtaskID_names[task_subtype_count] = {
-    "none", "density", "gradient", "force", "grav", "external_grav",
-    "tend", "xv",      "rho",      "gpart", "spart"};
+    "none", "density", "gradient", "force", "grav",      "external_grav",
+    "tend", "xv",      "rho",      "gpart", "multipole", "spart"};
 
 /**
  * @brief Computes the overlap between the parts array of two given cells.
diff --git a/src/task.h b/src/task.h
index 39f67fef84..5aa1f75e62 100644
--- a/src/task.h
+++ b/src/task.h
@@ -74,6 +74,7 @@ enum task_subtypes {
   task_subtype_xv,
   task_subtype_rho,
   task_subtype_gpart,
+  task_subtype_multipole,
   task_subtype_spart,
   task_subtype_count
 } __attribute__((packed));
-- 
GitLab