From e29c1a6083600d4ee6bcef4fcbb4263aa7ea3bb6 Mon Sep 17 00:00:00 2001
From: Matthieu Schaller <schaller@strw.leidenuniv.nl>
Date: Fri, 27 Dec 2019 14:10:29 +0100
Subject: [PATCH] Do not compress the datasets if there are 0 elements.

---
 src/distributed_io.c | 47 ++++++++++++++++++++++++++------------------
 1 file changed, 28 insertions(+), 19 deletions(-)

diff --git a/src/distributed_io.c b/src/distributed_io.c
index 158fc3c2a3..1dd9a5689d 100644
--- a/src/distributed_io.c
+++ b/src/distributed_io.c
@@ -99,7 +99,12 @@ void write_distributed_array(const struct engine* e, hid_t grp,
   io_copy_temp_buffer(temp, e, props, N, internal_units, snapshot_units);
 
   /* Create data space */
-  const hid_t h_space = H5Screate(H5S_SIMPLE);
+  hid_t h_space;
+  if (N > 0)
+    h_space = H5Screate(H5S_SIMPLE);
+  else
+    h_space = H5Screate(H5S_NULL);
+
   if (h_space < 0)
     error("Error while creating data space for field '%s'.", props.name);
 
@@ -132,28 +137,32 @@ void write_distributed_array(const struct engine* e, hid_t grp,
   /* Dataset properties */
   const hid_t h_prop = H5Pcreate(H5P_DATASET_CREATE);
 
-  /* Set chunk size */
-  h_err = H5Pset_chunk(h_prop, rank, chunk_shape);
-  if (h_err < 0)
-    error("Error while setting chunk size (%llu, %llu) for field '%s'.",
-          chunk_shape[0], chunk_shape[1], props.name);
-
-  /* Impose check-sum to verify data corruption */
-  h_err = H5Pset_fletcher32(h_prop);
-  if (h_err < 0)
-    error("Error while setting checksum options for field '%s'.", props.name);
+  /* Create filters and set compression level if we have something to write */
+  if (N > 0) {
 
-  /* Impose data compression */
-  if (e->snapshot_compression > 0) {
-    h_err = H5Pset_shuffle(h_prop);
+    /* Set chunk size */
+    h_err = H5Pset_chunk(h_prop, rank, chunk_shape);
     if (h_err < 0)
-      error("Error while setting shuffling options for field '%s'.",
-            props.name);
+      error("Error while setting chunk size (%llu, %llu) for field '%s'.",
+            chunk_shape[0], chunk_shape[1], props.name);
 
-    h_err = H5Pset_deflate(h_prop, e->snapshot_compression);
+    /* Impose check-sum to verify data corruption */
+    h_err = H5Pset_fletcher32(h_prop);
     if (h_err < 0)
-      error("Error while setting compression options for field '%s'.",
-            props.name);
+      error("Error while setting checksum options for field '%s'.", props.name);
+
+    /* Impose data compression */
+    if (e->snapshot_compression > 0) {
+      h_err = H5Pset_shuffle(h_prop);
+      if (h_err < 0)
+        error("Error while setting shuffling options for field '%s'.",
+              props.name);
+
+      h_err = H5Pset_deflate(h_prop, e->snapshot_compression);
+      if (h_err < 0)
+        error("Error while setting compression options for field '%s'.",
+              props.name);
+    }
   }
 
   /* Create dataset */
-- 
GitLab