From cb7cc535ca02ce0457dabdbc5a40f527c98b0985 Mon Sep 17 00:00:00 2001
From: Michael Edwards <m.k.edwards@gmail.com>
Date: Tue, 14 Nov 2017 16:42:11 -0500
Subject: [PATCH] Proof of concept (no control path) demonstration of
 collective filtered (compressed) HDF5 writes

---
 makefile                              | 14 +++++++-------
 src/dm/impls/da/gr2.c                 | 11 +++++++++++
 src/vec/is/is/impls/general/general.c | 25 ++++++++++++++++---------
 src/vec/vec/impls/mpi/pdvec.c         | 11 +++++++++++
 4 files changed, 45 insertions(+), 16 deletions(-)

diff --git a/makefile b/makefile
index 965b052..c2f7c89 100644
--- a/makefile
+++ b/makefile
@@ -281,13 +281,13 @@ install:
 	${OMAKE} PETSC_ARCH=${PETSC_ARCH} PETSC_DIR=${PETSC_DIR} mpi4py-install petsc4py-install
 
 newall:
-	-@cd src/sys;  @${PYTHON} ${PETSC_DIR}/config/builder.py
-	-@cd src/vec;  @${PYTHON} ${PETSC_DIR}/config/builder.py
-	-@cd src/mat;  @${PYTHON} ${PETSC_DIR}/config/builder.py
-	-@cd src/dm;   @${PYTHON} ${PETSC_DIR}/config/builder.py
-	-@cd src/ksp;  @${PYTHON} ${PETSC_DIR}/config/builder.py
-	-@cd src/snes; @${PYTHON} ${PETSC_DIR}/config/builder.py
-	-@cd src/ts;   @${PYTHON} ${PETSC_DIR}/config/builder.py
+	-@cd src/sys;  ${PYTHON} ${PETSC_DIR}/config/builder.py
+	-@cd src/vec;  ${PYTHON} ${PETSC_DIR}/config/builder.py
+	-@cd src/mat;  ${PYTHON} ${PETSC_DIR}/config/builder.py
+	-@cd src/dm;   ${PYTHON} ${PETSC_DIR}/config/builder.py
+	-@cd src/ksp;  ${PYTHON} ${PETSC_DIR}/config/builder.py
+	-@cd src/snes; ${PYTHON} ${PETSC_DIR}/config/builder.py
+	-@cd src/ts;   ${PYTHON} ${PETSC_DIR}/config/builder.py
 
 streams:
 	cd src/benchmarks/streams; ${OMAKE} PETSC_DIR=${PETSC_DIR} PETSC_ARCH=${PETSC_ARCH} streams
diff --git a/src/dm/impls/da/gr2.c b/src/dm/impls/da/gr2.c
index beea5a5..4100755 100644
--- a/src/dm/impls/da/gr2.c
+++ b/src/dm/impls/da/gr2.c
@@ -7,6 +7,7 @@
 #include <petsc/private/glvisvecimpl.h>
 #include <petscdraw.h>
 #include <petscviewerhdf5.h>
+#include "H5Zzfp_plugin.h"
 
 /*
         The data that is passed into the graphics callback
@@ -513,6 +514,16 @@ PetscErrorCode VecView_MPI_HDF5_DA(Vec xin,PetscViewer viewer)
     /* Create chunk */
     PetscStackCallHDF5Return(chunkspace,H5Pcreate,(H5P_DATASET_CREATE));
     PetscStackCallHDF5(H5Pset_chunk,(chunkspace, dim, chunkDims));
+    {
+      unsigned int cdata[H5Z_ZFP_CD_NELMTS_MEM];
+      size_t ndata = H5Z_ZFP_CD_NELMTS_MEM;
+      size_t i;
+      for (i = 0; i < ndata; ++i) {
+        cdata[i] = 0;
+      }
+      H5Pset_zfp_accuracy_cdata(1.0e-11, ndata, cdata);
+      PetscStackCallHDF5(H5Pset_filter,(chunkspace, H5Z_FILTER_ZFP, H5Z_FLAG_MANDATORY, ndata, cdata));
+    }
 
 #if (H5_VERS_MAJOR * 10000 + H5_VERS_MINOR * 100 + H5_VERS_RELEASE >= 10800)
     PetscStackCallHDF5Return(dset_id,H5Dcreate2,(group, vecname, filescalartype, filespace, H5P_DEFAULT, chunkspace, H5P_DEFAULT));
diff --git a/src/vec/is/is/impls/general/general.c b/src/vec/is/is/impls/general/general.c
index 067abcb..c1ba1ff 100644
--- a/src/vec/is/is/impls/general/general.c
+++ b/src/vec/is/is/impls/general/general.c
@@ -6,6 +6,7 @@
 #include <petscvec.h>
 #include <petscviewer.h>
 #include <petscviewerhdf5.h>
+#include "blosc_filter.h"
 
 static PetscErrorCode ISDuplicate_General(IS is,IS *newIS)
 {
@@ -301,6 +302,18 @@ static PetscErrorCode ISView_General_HDF5(IS is, PetscViewer viewer)
     /* Create chunk */
     PetscStackCallHDF5Return(chunkspace,H5Pcreate,(H5P_DATASET_CREATE));
     PetscStackCallHDF5(H5Pset_chunk,(chunkspace, dim, chunkDims));
+    {
+      unsigned int cdata[7];
+      size_t ndata = 7;
+      size_t i;
+      for (i = 0; i < ndata; ++i) {
+        cdata[i] = 0;
+      }
+      cdata[4] = 5;
+      cdata[5] = BLOSC_SHUFFLE;
+      cdata[6] = BLOSC_BLOSCLZ;
+      PetscStackCallHDF5(H5Pset_filter,(chunkspace, FILTER_BLOSC, H5Z_FLAG_MANDATORY, ndata, cdata));
+    }
 
 #if (H5_VERS_MAJOR * 10000 + H5_VERS_MINOR * 100 + H5_VERS_RELEASE >= 10800)
     PetscStackCallHDF5Return(dset_id,H5Dcreate2,(group, isname, inttype, filespace, H5P_DEFAULT, chunkspace, H5P_DEFAULT));
@@ -326,12 +339,7 @@ static PetscErrorCode ISView_General_HDF5(IS is, PetscViewer viewer)
     count[dim] = bs;
     ++dim;
   }
-  if (n > 0) {
-    PetscStackCallHDF5Return(memspace,H5Screate_simple,(dim, count, NULL));
-  } else {
-    /* Can't create dataspace with zero for any dimension, so create null dataspace. */
-    PetscStackCallHDF5Return(memspace,H5Screate,(H5S_NULL));
-  }
+  PetscStackCallHDF5Return(memspace,H5Screate_simple,(dim, count, NULL));
 
   /* Select hyperslab in the file */
   ierr = PetscLayoutGetRange(is->map, &low, NULL);CHKERRQ(ierr);
@@ -346,12 +354,11 @@ static PetscErrorCode ISView_General_HDF5(IS is, PetscViewer viewer)
     offset[dim] = 0;
     ++dim;
   }
+  PetscStackCallHDF5Return(filespace,H5Dget_space,(dset_id));
   if (n > 0) {
-    PetscStackCallHDF5Return(filespace,H5Dget_space,(dset_id));
     PetscStackCallHDF5(H5Sselect_hyperslab,(filespace, H5S_SELECT_SET, offset, NULL, count, NULL));
   } else {
-    /* Create null filespace to match null memspace. */
-    PetscStackCallHDF5Return(filespace,H5Screate,(H5S_NULL));
+    PetscStackCallHDF5(H5Sselect_none,(filespace));
   }
 
   /* Create property list for collective dataset write */
diff --git a/src/vec/vec/impls/mpi/pdvec.c b/src/vec/vec/impls/mpi/pdvec.c
index 454ba74..d8b9524 100644
--- a/src/vec/vec/impls/mpi/pdvec.c
+++ b/src/vec/vec/impls/mpi/pdvec.c
@@ -6,6 +6,7 @@
 #include <petsc/private/glvisviewerimpl.h>
 #include <petsc/private/glvisvecimpl.h>
 #include <petscviewerhdf5.h>
+#include "H5Zzfp_plugin.h"
 
 PetscErrorCode VecDestroy_MPI(Vec v)
 {
@@ -737,6 +738,16 @@ PetscErrorCode VecView_MPI_HDF5(Vec xin, PetscViewer viewer)
     /* Create chunk */
     PetscStackCallHDF5Return(chunkspace,H5Pcreate,(H5P_DATASET_CREATE));
     PetscStackCallHDF5(H5Pset_chunk,(chunkspace, dim, chunkDims));
+    {
+      unsigned int cdata[H5Z_ZFP_CD_NELMTS_MEM];
+      size_t ndata = H5Z_ZFP_CD_NELMTS_MEM;
+      size_t i;
+      for (i = 0; i < ndata; ++i) {
+        cdata[i] = 0;
+      }
+      H5Pset_zfp_accuracy_cdata(1.0e-11, ndata, cdata);
+      PetscStackCallHDF5(H5Pset_filter,(chunkspace, H5Z_FILTER_ZFP, H5Z_FLAG_MANDATORY, ndata, cdata));
+    }
 
 #if (H5_VERS_MAJOR * 10000 + H5_VERS_MINOR * 100 + H5_VERS_RELEASE >= 10800)
     PetscStackCallHDF5Return(dset_id,H5Dcreate2,(group, vecname, filescalartype, filespace, H5P_DEFAULT, chunkspace, H5P_DEFAULT));
-- 
1.8.3.1

