From 451b72521d28255fa0f56f508bce16d0e41b0ccf Mon Sep 17 00:00:00 2001 From: Erik Schnetter Date: Tue, 28 Aug 2012 20:18:45 -0400 Subject: CarpetIOHDF5: Use string instead of char[] in HDF5 for large attributes --- Carpet/CarpetIOHDF5/src/CarpetIOHDF5.cc | 19 ++++++------ Carpet/CarpetIOHDF5/src/Input.cc | 51 ++++++++++++++++++++++++++------- 2 files changed, 52 insertions(+), 18 deletions(-) diff --git a/Carpet/CarpetIOHDF5/src/CarpetIOHDF5.cc b/Carpet/CarpetIOHDF5/src/CarpetIOHDF5.cc index 9d3403d91..0ce40eee4 100644 --- a/Carpet/CarpetIOHDF5/src/CarpetIOHDF5.cc +++ b/Carpet/CarpetIOHDF5/src/CarpetIOHDF5.cc @@ -1007,10 +1007,10 @@ static void Checkpoint (const cGH* const cctkGH, int called_from) int const len = Util_TableGetString (gdata.tagstable, 0, NULL, "checkpoint"); if (len > 0) { - char* value = new char[len + 1]; + vector value_buf(len+1); + char* value = &value_buf[0]; Util_TableGetString (gdata.tagstable, len + 1, value, "checkpoint"); if (len == sizeof ("no") - 1 and CCTK_Equals (value, "no")) { - delete[] value; continue; } else if (not CCTK_Equals (value, "yes")) { char* groupname = CCTK_GroupName (group); @@ -1019,7 +1019,6 @@ static void Checkpoint (const cGH* const cctkGH, int called_from) value, groupname); free (groupname); } - delete[] value; } /* get the number of active timelevels */ @@ -1512,18 +1511,22 @@ static int WriteLargeAttribute (hid_t const group, char const * const name, char const * const svalue) { - hid_t dataspace, dataset; + hid_t dataspace, dataset, datatype; int error_count = 0; // Create a dataset, since the data may not fit into an attribute hsize_t const size = strlen (svalue) + 1; - HDF5_ERROR (dataspace = H5Screate_simple (1, & size, NULL)); - HDF5_ERROR (dataset = H5Dcreate (group, name, H5T_NATIVE_CHAR, - dataspace, H5P_DEFAULT)); - HDF5_ERROR (H5Dwrite (dataset, H5T_NATIVE_CHAR, H5S_ALL, H5S_ALL, + HDF5_ERROR (datatype = H5Tcopy (H5T_C_S1)); + HDF5_ERROR (H5Tset_size (datatype, size)); + + HDF5_ERROR (dataspace = H5Screate (H5S_SCALAR)); + HDF5_ERROR (dataset = H5Dcreate (group, name, datatype, + dataspace, H5P_DEFAULT)); + HDF5_ERROR (H5Dwrite (dataset, datatype, H5S_ALL, H5S_ALL, H5P_DEFAULT, svalue)); HDF5_ERROR (H5Dclose (dataset)); HDF5_ERROR (H5Sclose (dataspace)); + HDF5_ERROR (H5Tclose (datatype)); return error_count; } diff --git a/Carpet/CarpetIOHDF5/src/Input.cc b/Carpet/CarpetIOHDF5/src/Input.cc index e50acf1c9..efd5f3199 100644 --- a/Carpet/CarpetIOHDF5/src/Input.cc +++ b/Carpet/CarpetIOHDF5/src/Input.cc @@ -470,12 +470,12 @@ int Recover (cGH* cctkGH, const char *basefilename, int called_from) int tagstable = CCTK_GroupTagsTableI (gindex); int const len = Util_TableGetString (tagstable, 0, NULL, "checkpoint"); if (len > 0) { - char* value = new char[len + 1]; + vector value_buf(len+1); + char* value = &value_buf[0]; Util_TableGetString (tagstable, len + 1, value, "checkpoint"); if (len == sizeof ("no") - 1 and CCTK_Equals (value, "no")) { not_checkpointed[vindex] = true; } - delete[] value; } } } @@ -972,15 +972,29 @@ static list::iterator OpenFileSet (const cGH* const cctkGH, CCTK_VInfo (CCTK_THORNSTRING, "Recovering parameters from checkpoint " "file '%s'", file.filename); } - hid_t dataset; + hid_t dataset, datatype; + size_t len; + htri_t old_data; + HDF5_ERROR (dataset = H5Dopen (file.file, METADATA_GROUP "/" ALL_PARAMETERS)); - char *parameters = new char[H5Dget_storage_size (dataset) + 1]; - HDF5_ERROR (H5Dread (dataset, H5T_NATIVE_CHAR, H5S_ALL, H5S_ALL, + HDF5_ERROR (datatype = H5Dget_type(dataset)); + HDF5_ERROR (old_data = H5Tequal(datatype, H5T_NATIVE_CHAR)); + if (old_data) { + /* old data that stored this as an array of chars */ + CCTK_WARN (CCTK_WARN_ALERT, "Old-style checkpoint data found."); + HDF5_ERROR (len = H5Dget_storage_size(dataset) + 1); + } else { + HDF5_ERROR (len = H5Tget_size(datatype)); + } + vector parameter_buf(len); + char* parameters = ¶meter_buf[0]; + + HDF5_ERROR (H5Dread (dataset, datatype, H5S_ALL, H5S_ALL, H5P_DEFAULT, parameters)); HDF5_ERROR (H5Dclose (dataset)); + HDF5_ERROR (H5Tclose (datatype)); IOUtil_SetAllParameters (parameters); - delete[] parameters; num_reflevels = fileset.num_reflevels; } @@ -1062,11 +1076,28 @@ static void ReadMetadata (fileset_t& fileset, hid_t file) dataset = H5Dopen (metadata, GRID_STRUCTURE); } H5E_END_TRY; if (dataset >= 0) { - vector gs_cstr (H5Dget_storage_size (dataset) + 1); - HDF5_ERROR (H5Dread (dataset, H5T_NATIVE_CHAR, H5S_ALL, H5S_ALL, - H5P_DEFAULT, &gs_cstr.front())); + hid_t datatype; + htri_t old_data; + size_t len; + + HDF5_ERROR (datatype = H5Dget_type(dataset)); + HDF5_ERROR (datatype = H5Dget_type(dataset)); + HDF5_ERROR (old_data = H5Tequal(datatype, H5T_NATIVE_CHAR)); + if (old_data) { + /* old data that stored this as an array of chars */ + CCTK_WARN (CCTK_WARN_ALERT, "Old-style checkpoint data found."); + HDF5_ERROR (len = H5Dget_storage_size(dataset) + 1); + } else { + HDF5_ERROR (len = H5Tget_size(datatype)); + } + vector gs_str_buf(len); + char* gs_str = &gs_str_buf[0]; + + HDF5_ERROR (H5Dread (dataset, datatype, H5S_ALL, H5S_ALL, + H5P_DEFAULT, gs_str)); HDF5_ERROR (H5Dclose (dataset)); - istringstream gs_buf (&gs_cstr.front()); + HDF5_ERROR (H5Tclose (datatype)); + istringstream gs_buf (gs_str); skipws (gs_buf); consume (gs_buf, "grid_superstructure:"); -- cgit v1.2.3