[Commits] [svn:einsteintoolkit] incoming/PITTNullCode/SphericalHarmonicReconGen/src/ (Rev. 105)

rhaas at tapir.caltech.edu rhaas at tapir.caltech.edu
Fri Dec 27 16:13:17 CST 2013


User: rhaas
Date: 2013/12/27 04:13 PM

Modified:
 /PITTNullCode/SphericalHarmonicReconGen/src/
  sph_database.cc

Log:
 add error checking around HDF5 calls
 
 in particular abort when HDF5 file cannot be opened

File Changes:

Directory: /PITTNullCode/SphericalHarmonicReconGen/src/
=======================================================

File [modified]: sph_database.cc
Delta lines: +69 -37
===================================================================
--- PITTNullCode/SphericalHarmonicReconGen/src/sph_database.cc	2013-12-27 22:09:03 UTC (rev 104)
+++ PITTNullCode/SphericalHarmonicReconGen/src/sph_database.cc	2013-12-27 22:13:16 UTC (rev 105)
@@ -1,11 +1,26 @@
 #include "sph_database.hh"
+#include <cassert>
 
 namespace SHR {
 
 using namespace std;
 
+// check return code of HDF5 call and print a warning in case of an error
+#define HDF5_ERROR(fn_call) HDF5_CALL(__LINE__, __FILE__, CCTK_THORNSTRING,   \
+                                      fn_call, #fn_call)
+static int HDF5_CALL(const int line, const char* file,
+                     const char* thornstring, int error_code,
+                     const char* fn_call)
+{
+  if (error_code < 0)
+  {
+    CCTK_VError (line, file, thornstring,
+                "HDF5 call '%s' returned error code %d",
+                fn_call, error_code);
+  }
+  return error_code;
+}
 
-
 SPH_db_ASCII::SPH_db_ASCII(const string& fname_,
                          const bool verbose_,
                          const int cached_timesteps,
@@ -899,6 +914,10 @@
 { 
    // open file
    file = H5Fopen(_fname.c_str(), H5F_ACC_RDONLY, H5P_DEFAULT);
+   if (file < 0) {
+     CCTK_VError(__LINE__, __FILE__, CCTK_THORNSTRING, 
+                 "Could not open file \"%s\".", _fname.c_str());
+   }
    
    scan(); 
 }
@@ -907,7 +926,7 @@
 SPH_db_SpEC_H5::~SPH_db_SpEC_H5() 
 { 
    // close file
-   H5Fclose(file);
+   HDF5_ERROR(H5Fclose(file));
 }
             
 
@@ -977,10 +996,12 @@
       CCTK_WARN(0, "Requested timestep not in worldtube data file. Stopping!");
    }
 
-   hid_t dataset = H5Dopen(file, (varname_table[varno][0]+"/"+steps[timestep].name+"/"+varname_table[varno][1]).c_str());//, H5P_DEFAULT);
+   hid_t dataset = HDF5_ERROR(H5Dopen(file, (varname_table[varno][0]+"/"+
+                                             steps[timestep].name+"/"+
+                                             varname_table[varno][1]).c_str()));
    vector<double> buffer((_lmax+1)*(_lmax+1)*2);
-   H5Dread(dataset, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL, H5P_DEFAULT, &buffer.front());
-   H5Dclose(dataset);
+   HDF5_ERROR(H5Dread(dataset, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL, H5P_DEFAULT, &buffer.front()));
+   HDF5_ERROR(H5Dclose(dataset));
    
    int c = 0;
    for (int l=0; l <= _lmax; ++l) {
@@ -1005,11 +1026,11 @@
          if (gname.find(step_basename) != string::npos) {
             // get time attribute
             double time;
-            hid_t group = H5Gopen(loc_id, name);
-            hid_t attribute = H5Aopen(group, time_attrib_name.c_str(), H5P_DEFAULT);
-            H5Aread(attribute, H5T_NATIVE_DOUBLE, &time);
-            H5Aclose(attribute);
-            H5Gclose(group);
+            hid_t group = HDF5_ERROR(H5Gopen(loc_id, name));
+            hid_t attribute = HDF5_ERROR(H5Aopen(group, time_attrib_name.c_str(), H5P_DEFAULT));
+            HDF5_ERROR(H5Aread(attribute, H5T_NATIVE_DOUBLE, &time));
+            HDF5_ERROR(H5Aclose(attribute));
+            HDF5_ERROR(H5Gclose(group));
             
             SpEC_H5->steps.push_back(timestep_t(name, time));
          }
@@ -1033,7 +1054,9 @@
    // pick one variable and browse through entire file
    // to find out how many (and what) cycles and times there are
    // (assuming that all variables are present at the same timesteps)
-   H5Literate_by_name (file, varname_table[0][0].c_str(), H5_INDEX_NAME, H5_ITER_NATIVE, NULL, H5iter, this, H5P_DEFAULT);
+   HDF5_ERROR(H5Literate_by_name (file, varname_table[0][0].c_str(),
+                                  H5_INDEX_NAME, H5_ITER_NATIVE, NULL,
+                                  H5iter, this, H5P_DEFAULT));
 
    // sort 'steps' vector
    //sort(steps.begin(), steps.end());
@@ -1046,13 +1069,17 @@
    }
 
    // get lmax from first variable and timestep
-   hid_t dataset = H5Dopen(file, (varname_table[0][0]+"/"+steps[0].name+"/"+varname_table[0][1]).c_str()); //, H5P_DEFAULT);
+   hid_t dataset = HDF5_ERROR(H5Dopen(file, (varname_table[0][0]+"/"+
+                                             steps[0].name+"/"+
+                                             varname_table[0][1]).c_str()));
    //cout << (varname_table[0][0]+"/"+steps[0].name+"/"+varname_table[0][1]) << endl;
-   hid_t dataspace = H5Dget_space(dataset);
-   hsize_t size = H5Sget_simple_extent_npoints(dataspace);
-   H5Sclose(dataspace);
-   H5Dclose(dataset);
+   hid_t dataspace = HDF5_ERROR(H5Dget_space(dataset));
+   hsize_t size = HDF5_ERROR(H5Sget_simple_extent_npoints(dataspace));
+   HDF5_ERROR(H5Sclose(dataspace));
+   HDF5_ERROR(H5Dclose(dataset));
 
+   assert (size % 2 == 0);
+
    _lmax = sqrt(size/2)-1;
    _n_variables = 30;
    _n_spheres = 1;
@@ -1110,6 +1137,10 @@
 { 
    // open file
    file = H5Fopen(_fname.c_str(), H5F_ACC_RDONLY, H5P_DEFAULT);
+   if (file < 0) {
+     CCTK_VError(__LINE__, __FILE__, CCTK_THORNSTRING, 
+                 "Could not open file \"%s\".", _fname.c_str());
+   }
    
    scan(); 
 }
@@ -1118,7 +1149,7 @@
 SPH_db_SpEC_H5_v2::~SPH_db_SpEC_H5_v2() 
 { 
    // close file
-   H5Fclose(file);
+   HDF5_ERROR(H5Fclose(file));
 }
 
 
@@ -1216,27 +1247,28 @@
 
    for (int v=0; v < _n_variables; ++v) {
 
-      hid_t dataset = H5Dopen(file, varname_table[v].c_str());
+      hid_t dataset = HDF5_ERROR(H5Dopen(file, varname_table[v].c_str()));
       const int nmodes = (_lmax+1)*(_lmax+1)*2;
       vector<double> buffer(nmodes*nsteps);
    
       hsize_t dimsm[1] = { buffer.size() };
-      hid_t memspace = H5Screate_simple (1, dimsm, NULL);
+      hid_t memspace = HDF5_ERROR(H5Screate_simple (1, dimsm, NULL));
    
-      hid_t dataspace = H5Dget_space(dataset);
+      hid_t dataspace = HDF5_ERROR(H5Dget_space(dataset));
       hsize_t dims[2];
-      H5Sget_simple_extent_dims (dataspace, dims, NULL);
+      HDF5_ERROR(H5Sget_simple_extent_dims (dataspace, dims, NULL));
    
       hsize_t offset[2] = { timestep, 1 };
       hsize_t count[2]  = { nsteps, nmodes };
-      H5Sselect_hyperslab (dataspace, H5S_SELECT_SET, offset, NULL, 
-                                      count, NULL);
+      HDF5_ERROR(H5Sselect_hyperslab (dataspace, H5S_SELECT_SET, offset, NULL, 
+                                      count, NULL));
    
-      H5Dread(dataset, H5T_NATIVE_DOUBLE, memspace, dataspace, H5P_DEFAULT, &buffer.front());
+      HDF5_ERROR(H5Dread(dataset, H5T_NATIVE_DOUBLE, memspace, dataspace, H5P_DEFAULT,
+                 &buffer.front()));
    
-      H5Sclose(memspace);
-      H5Sclose(dataspace);
-      H5Dclose(dataset);
+      HDF5_ERROR(H5Sclose(memspace));
+      HDF5_ERROR(H5Sclose(dataspace));
+      HDF5_ERROR(H5Dclose(dataset));
    
       for (int t=0; t < nsteps; ++t) {
    
@@ -1279,11 +1311,11 @@
 */
    
    // get lmax from first variable and timestep
-   hid_t dataset = H5Dopen(file, "gxx.dat"); //, H5P_DEFAULT);
+   hid_t dataset = HDF5_ERROR(H5Dopen(file, "gxx.dat"));
    //cout << (varname_table[0][0]+"/"+steps[0].name+"/"+varname_table[0][1]) << endl;
-   hid_t dataspace = H5Dget_space(dataset);
+   hid_t dataspace = HDF5_ERROR(H5Dget_space(dataset));
    hsize_t dims[2];
-   H5Sget_simple_extent_dims (dataspace, dims, NULL);
+   HDF5_ERROR(H5Sget_simple_extent_dims (dataspace, dims, NULL));
    
    // first dim is number of timesteps
    steps.resize(dims[0]);
@@ -1293,20 +1325,20 @@
    // select hyperslab in dataset
    hsize_t offset[2] = { 0, 0 };
    hsize_t count[2]  = { dims[0], 1 };
-   H5Sselect_hyperslab (dataspace, H5S_SELECT_SET, offset, NULL, 
-                                      count, NULL);
+   HDF5_ERROR(H5Sselect_hyperslab (dataspace, H5S_SELECT_SET, offset, NULL, 
+                                   count, NULL));
    
    // define dataspace of memory
    hsize_t dimsm[1] = { steps.size() };
    hid_t memspace = H5Screate_simple (1, dimsm, NULL);
    
    // read "times"
-   H5Dread (dataset, H5T_NATIVE_DOUBLE, memspace, dataspace,
-                      H5P_DEFAULT, &steps.front());
+   HDF5_ERROR(H5Dread (dataset, H5T_NATIVE_DOUBLE, memspace, dataspace,
+                       H5P_DEFAULT, &steps.front()));
    
-   H5Sclose(memspace);
-   H5Sclose(dataspace);
-   H5Dclose(dataset);
+   HDF5_ERROR(H5Sclose(memspace));
+   HDF5_ERROR(H5Sclose(dataspace));
+   HDF5_ERROR(H5Dclose(dataset));
 
    _n_variables = 30;
    _n_spheres = 1;



More information about the Commits mailing list