From 76676d8c080642f3dad33c65ad86d07841161b1a Mon Sep 17 00:00:00 2001
From: Matthieu Schaller <matthieu.schaller@durham.ac.uk>
Date: Tue, 21 Nov 2017 16:16:22 +0000
Subject: [PATCH] Less verbose i/o over MPI. Larger buffer alignment for the
 temporary cache.

---
 src/common_io.c   | 24 ++++++++++++++----------
 src/common_io.h   |  3 ++-
 src/parallel_io.c |  8 +++++---
 src/serial_io.c   |  9 +++++----
 src/single_io.c   |  4 ++--
 5 files changed, 28 insertions(+), 20 deletions(-)

diff --git a/src/common_io.c b/src/common_io.c
index cf1239b1a2..d071e7703c 100644
--- a/src/common_io.c
+++ b/src/common_io.c
@@ -296,16 +296,19 @@ void io_write_attribute_s(hid_t grp, const char* name, const char* str) {
  * @brief Reads the Unit System from an IC file.
  * @param h_file The (opened) HDF5 file from which to read.
  * @param us The unit_system to fill.
+ * @param mpi_rank The MPI rank we are on.
  *
  * If the 'Units' group does not exist in the ICs, cgs units will be assumed
  */
-void io_read_unit_system(hid_t h_file, struct unit_system* us) {
+void io_read_unit_system(hid_t h_file, struct unit_system* us, int mpi_rank) {
 
   /* First check if it exists as this is *not* required. */
   const htri_t exists = H5Lexists(h_file, "/Units", H5P_DEFAULT);
 
   if (exists == 0) {
-    message("'Units' group not found in ICs. Assuming CGS unit system.");
+
+    if (mpi_rank == 0)
+      message("'Units' group not found in ICs. Assuming CGS unit system.");
 
     /* Default to CGS */
     us->UnitMass_in_cgs = 1.;
@@ -320,7 +323,7 @@ void io_read_unit_system(hid_t h_file, struct unit_system* us) {
           exists);
   }
 
-  message("Reading IC units from ICs.");
+  if (mpi_rank == 0) message("Reading IC units from ICs.");
   hid_t h_grp = H5Gopen(h_file, "/Units", H5P_DEFAULT);
 
   /* Ok, Read the damn thing */
@@ -407,7 +410,8 @@ void io_write_code_description(hid_t h_file) {
  *
  * @param temp The buffer to be filled. Must be allocated and aligned properly.
  * @param e The #engine.
- * @param props The #io_props corresponding to the particle field we are copying.
+ * @param props The #io_props corresponding to the particle field we are
+ * copying.
  * @param N The number of particles to copy
  * @param internal_units The system of units used internally.
  * @param snapshot_units The system of units used for the snapshots.
@@ -433,7 +437,7 @@ void io_copy_temp_buffer(void* temp, const struct engine* e,
 
     if (props.convert_part_f != NULL) {
 
-      swift_declare_aligned_ptr(float, temp_f, temp, SWIFT_CACHE_ALIGNMENT);
+      swift_declare_aligned_ptr(float, temp_f, temp, IO_BUFFER_ALIGNMENT);
       swift_declare_aligned_ptr(const struct part, parts, props.parts,
                                 SWIFT_STRUCT_ALIGNMENT);
 
@@ -443,7 +447,7 @@ void io_copy_temp_buffer(void* temp, const struct engine* e,
 
     } else if (props.convert_part_d != NULL) {
 
-      swift_declare_aligned_ptr(double, temp_d, temp, SWIFT_CACHE_ALIGNMENT);
+      swift_declare_aligned_ptr(double, temp_d, temp, IO_BUFFER_ALIGNMENT);
       swift_declare_aligned_ptr(const struct part, parts, props.parts,
                                 SWIFT_STRUCT_ALIGNMENT);
 
@@ -453,7 +457,7 @@ void io_copy_temp_buffer(void* temp, const struct engine* e,
 
     } else if (props.convert_gpart_f != NULL) {
 
-      swift_declare_aligned_ptr(float, temp_f, temp, SWIFT_CACHE_ALIGNMENT);
+      swift_declare_aligned_ptr(float, temp_f, temp, IO_BUFFER_ALIGNMENT);
       swift_declare_aligned_ptr(const struct gpart, gparts, props.gparts,
                                 SWIFT_STRUCT_ALIGNMENT);
 
@@ -463,7 +467,7 @@ void io_copy_temp_buffer(void* temp, const struct engine* e,
 
     } else if (props.convert_gpart_d != NULL) {
 
-      swift_declare_aligned_ptr(double, temp_d, temp, SWIFT_CACHE_ALIGNMENT);
+      swift_declare_aligned_ptr(double, temp_d, temp, IO_BUFFER_ALIGNMENT);
       swift_declare_aligned_ptr(const struct gpart, gparts, props.gparts,
                                 SWIFT_STRUCT_ALIGNMENT);
 
@@ -484,10 +488,10 @@ void io_copy_temp_buffer(void* temp, const struct engine* e,
     /* message("Converting ! factor=%e", factor); */
 
     if (io_is_double_precision(props.type)) {
-      swift_declare_aligned_ptr(double, temp_d, temp, SWIFT_CACHE_ALIGNMENT);
+      swift_declare_aligned_ptr(double, temp_d, temp, IO_BUFFER_ALIGNMENT);
       for (size_t i = 0; i < num_elements; ++i) temp_d[i] *= factor;
     } else {
-      swift_declare_aligned_ptr(float, temp_f, temp, SWIFT_CACHE_ALIGNMENT);
+      swift_declare_aligned_ptr(float, temp_f, temp, IO_BUFFER_ALIGNMENT);
       for (size_t i = 0; i < num_elements; ++i) temp_f[i] *= factor;
     }
   }
diff --git a/src/common_io.h b/src/common_io.h
index 08de4a492e..ee886b3263 100644
--- a/src/common_io.h
+++ b/src/common_io.h
@@ -30,6 +30,7 @@
 #define FIELD_BUFFER_SIZE 200
 #define PARTICLE_GROUP_BUFFER_SIZE 50
 #define FILENAME_BUFFER_SIZE 150
+#define IO_BUFFER_ALIGNMENT 512
 
 /* Avoid cyclic inclusion problems */
 struct io_props;
@@ -72,7 +73,7 @@ void io_write_attribute_s(hid_t grp, const char* name, const char* str);
 
 void io_write_code_description(hid_t h_file);
 
-void io_read_unit_system(hid_t h_file, struct unit_system* us);
+void io_read_unit_system(hid_t h_file, struct unit_system* us, int mpi_rank);
 void io_write_unit_system(hid_t h_grp, const struct unit_system* us,
                           const char* groupName);
 
diff --git a/src/parallel_io.c b/src/parallel_io.c
index e82650fa30..3cd26e6147 100644
--- a/src/parallel_io.c
+++ b/src/parallel_io.c
@@ -262,7 +262,7 @@ void writeArray_chunk(struct engine* e, hid_t h_data, hid_t h_plist_id,
 
   /* Allocate temporary buffer */
   void* temp = malloc(num_elements * typeSize);
-  if (posix_memalign((void**)&temp, SWIFT_CACHE_ALIGNMENT,
+  if (posix_memalign((void**)&temp, IO_BUFFER_ALIGNMENT,
                      num_elements * typeSize) != 0)
     error("Unable to allocate temporary i/o buffer");
 
@@ -584,7 +584,7 @@ void read_ic_parallel(char* fileName, const struct unit_system* internal_units,
   /* Read the unit system used in the ICs */
   struct unit_system* ic_units = malloc(sizeof(struct unit_system));
   if (ic_units == NULL) error("Unable to allocate memory for IC unit system");
-  io_read_unit_system(h_file, ic_units);
+  io_read_unit_system(h_file, ic_units, mpi_rank);
 
   /* Tell the user if a conversion will be needed */
   if (mpi_rank == 0) {
@@ -702,7 +702,9 @@ void read_ic_parallel(char* fileName, const struct unit_system* internal_units,
         break;
 
       default:
-        message("Particle Type %d not yet supported. Particles ignored", ptype);
+        if (mpi_rank == 0)
+          message("Particle Type %d not yet supported. Particles ignored",
+                  ptype);
     }
 
     /* Read everything */
diff --git a/src/serial_io.c b/src/serial_io.c
index c9ac16f131..f8e3927c4a 100644
--- a/src/serial_io.c
+++ b/src/serial_io.c
@@ -301,7 +301,7 @@ void writeArray(const struct engine* e, hid_t grp, char* fileName,
 
   /* Allocate temporary buffer */
   void* temp = NULL;
-  if (posix_memalign((void**)&temp, SWIFT_CACHE_ALIGNMENT,
+  if (posix_memalign((void**)&temp, IO_BUFFER_ALIGNMENT,
                      num_elements * typeSize) != 0)
     error("Unable to allocate temporary i/o buffer");
 
@@ -480,7 +480,7 @@ void read_ic_serial(char* fileName, const struct unit_system* internal_units,
 
     /* Read the unit system used in the ICs */
     if (ic_units == NULL) error("Unable to allocate memory for IC unit system");
-    io_read_unit_system(h_file, ic_units);
+    io_read_unit_system(h_file, ic_units, mpi_rank);
 
     if (units_are_equal(ic_units, internal_units)) {
 
@@ -624,8 +624,9 @@ void read_ic_serial(char* fileName, const struct unit_system* internal_units,
             break;
 
           default:
-            message("Particle Type %d not yet supported. Particles ignored",
-                    ptype);
+            if (mpi_rank == 0)
+              message("Particle Type %d not yet supported. Particles ignored",
+                      ptype);
         }
 
         /* Read everything */
diff --git a/src/single_io.c b/src/single_io.c
index ca09232a49..a8199af6a8 100644
--- a/src/single_io.c
+++ b/src/single_io.c
@@ -182,7 +182,7 @@ void writeArray(const struct engine* e, hid_t grp, char* fileName,
 
   /* Allocate temporary buffer */
   void* temp = NULL;
-  if (posix_memalign((void**)&temp, SWIFT_CACHE_ALIGNMENT,
+  if (posix_memalign((void**)&temp, IO_BUFFER_ALIGNMENT,
                      num_elements * typeSize) != 0)
     error("Unable to allocate temporary i/o buffer");
 
@@ -387,7 +387,7 @@ void read_ic_single(char* fileName, const struct unit_system* internal_units,
   /* Read the unit system used in the ICs */
   struct unit_system* ic_units = malloc(sizeof(struct unit_system));
   if (ic_units == NULL) error("Unable to allocate memory for IC unit system");
-  io_read_unit_system(h_file, ic_units);
+  io_read_unit_system(h_file, ic_units, 0);
 
   /* Tell the user if a conversion will be needed */
   if (units_are_equal(ic_units, internal_units)) {
-- 
GitLab