libSplash
SDCHelper.cpp
1 
23 #include "splash/core/SDCHelper.hpp"
24 
25 #include "splash/version.hpp"
26 #include "splash/basetypes/basetypes.hpp"
27 #include "splash/core/DCAttribute.hpp"
28 #include "splash/core/logging.hpp"
29 
30 #include <sstream>
31 
32 namespace splash
33 {
34 
35  std::string SDCHelper::getExceptionString(std::string msg)
36  {
37  return (std::string("Exception for [SDCHelper] ") + msg);
38  }
39 
40  void SDCHelper::getReferenceData(const char* filename, int32_t* maxID, Dimensions *mpiSize)
41  throw (DCException)
42  {
43  log_msg(1, "loading reference data from %s", filename);
44 
45  // open the file to get reference data from
46  hid_t reference_file = H5Fopen(filename, H5F_ACC_RDONLY, H5P_FILE_ACCESS_DEFAULT);
47  if (reference_file < 0)
48  throw DCException(getExceptionString(std::string("Failed to open reference file ") +
49  std::string(filename)));
50 
51  // reference data is located in the header only
52  hid_t group_header = H5Gopen(reference_file, SDC_GROUP_HEADER, H5P_DEFAULT);
53  if (group_header < 0) {
54  H5Fclose(reference_file);
55  throw DCException(getExceptionString(
56  std::string("Failed to open header group in reference file ") +
57  std::string(filename)));
58  }
59 
60  try {
61  if (maxID != NULL) {
62  DCAttribute::readAttribute(SDC_ATTR_MAX_ID,
63  group_header, maxID);
64  }
65 
66  if (mpiSize != NULL) {
67  ColTypeDim dim_t;
68  DCAttribute::readAttribute(SDC_ATTR_MPI_SIZE,
69  group_header, mpiSize->getPointer());
70  }
71 
72  } catch (const DCException&) {
73  H5Fclose(reference_file);
74  throw DCException(getExceptionString(
75  std::string("Failed to read attributes from reference file ") +
76  std::string(filename)));
77  }
78 
79  // cleanup
80  H5Gclose(group_header);
81  H5Fclose(reference_file);
82  }
83 
84  void SDCHelper::writeHeader(hid_t file, Dimensions mpiPosition,
85  int32_t *maxID, bool *enableCompression, Dimensions *mpiSize,
86  bool master)
87  throw (DCException)
88  {
89  // create group for header information (position, grid size, ...)
90  hid_t group_header = H5Gcreate(file, SDC_GROUP_HEADER, H5P_LINK_CREATE_DEFAULT,
91  H5P_GROUP_CREATE_DEFAULT, H5P_GROUP_ACCESS_DEFAULT);
92  if (group_header < 0)
93  throw DCException(getExceptionString("Failed to create header group in reference file"));
94 
95  try {
96  std::stringstream splashVersion;
97  splashVersion << SPLASH_VERSION_MAJOR << "."
98  << SPLASH_VERSION_MINOR << "."
99  << SPLASH_VERSION_PATCH;
100  std::stringstream splashFormat;
101  splashFormat << SPLASH_FILE_FORMAT_MAJOR << "."
102  << SPLASH_FILE_FORMAT_MINOR;
103 
104  ColTypeInt32 ctInt32;
105  ColTypeBool ctBool;
106  ColTypeDim dim_t;
107  ColTypeString ctStringVersion(splashVersion.str().length());
108  ColTypeString ctStringFormat(splashFormat.str().length());
109 
110  // create master specific header attributes
111  if (master) {
112  DCAttribute::writeAttribute(SDC_ATTR_MAX_ID, ctInt32.getDataType(),
113  group_header, maxID);
114  } else {
115  DCAttribute::writeAttribute(SDC_ATTR_MPI_POSITION, dim_t.getDataType(),
116  group_header, mpiPosition.getPointer());
117  }
118 
119  DCAttribute::writeAttribute(SDC_ATTR_COMPRESSION, ctBool.getDataType(),
120  group_header, enableCompression);
121 
122  DCAttribute::writeAttribute(SDC_ATTR_MPI_SIZE, dim_t.getDataType(),
123  group_header, mpiSize->getPointer());
124 
125  DCAttribute::writeAttribute(SDC_ATTR_VERSION, ctStringVersion.getDataType(),
126  group_header, splashVersion.str().c_str());
127 
128  DCAttribute::writeAttribute(SDC_ATTR_FORMAT, ctStringFormat.getDataType(),
129  group_header, splashFormat.str().c_str());
130 
131  } catch (const DCException& attr_error) {
132  throw DCException(getExceptionString(
133  std::string("Failed to write header attribute in reference file. Error was: ") +
134  std::string(attr_error.what())));
135  }
136 
137  H5Gclose(group_header);
138  }
139 
140 } /* namespace splash */
EXTERN void log_msg(int level, const char *fmt,...)
Definition: logging.cpp:56