OpenFPM_pdata  1.1.0
Project that contain the implementation of distributed structures
 All Data Structures Namespaces Functions Variables Typedefs Enumerations Friends Pages
HDF5_XdmfWriter_util.hpp
1 /*
2  * H5PartWriteData_meta.hpp
3  *
4  * Created on: Feb 22, 2016
5  * Author: i-bird
6  */
7 
8 #ifndef OPENFPM_IO_SRC_HDF5_XDMFWRITER_HDF5_XDMFWRITER_UTIL_HPP_
9 #define OPENFPM_IO_SRC_HDF5_XDMFWRITER_HDF5_XDMFWRITER_UTIL_HPP_
10 
11 #include "hdf5.h"
12 #include "Vector/map_vector.hpp"
13 
27 template<typename type> bool HDF5CreateDataSet(hid_t file_id, const std::string & str ,void * ptr, size_t sz)
28 {
29  hid_t plist_id = H5Pcreate(H5P_DATASET_XFER);
30  if (plist_id < 0)
31  return false;
32 
33  /* Create the dataspace for the position dataset. */
34  hsize_t dimsf[1] = {sz};
35  hid_t filespace = H5Screate_simple(1, dimsf, NULL);
36  if (filespace < 0)
37  return false;
38 
39  if (std::is_same<type,char>::value == true)
40  {
41  hid_t dset_id = H5Dcreate(file_id, str.c_str(), H5T_NATIVE_CHAR, filespace, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
42  if (dset_id < 0)
43  return false;
44 
45  herr_t status = H5Dwrite(dset_id, H5T_NATIVE_CHAR, H5S_ALL, H5S_ALL, plist_id, ptr);
46  if (status < 0)
47  return false;
48 
49  H5Dclose(dset_id);
50  H5Dclose(filespace);
51  return true;
52  }
53 /* else if (std::is_same<type,signed char>::value == true)
54  {
55  dset_id = H5Dcreate(file_id, std.c_str(), H5T_NATIVE_SCHAR, filespace, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
56  H5Dclose(dset_id);
57  return status;
58  }
59  else if (std::is_same<type,unsigned char>::value == true)
60  {
61  dset_id = H5Dcreate(file_id, std.c_str(), H5T_NATIVE_UCHAR, filespace, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
62  }
63  else if (std::is_same<type,short>::value == true)
64  {
65  dset_id = H5Dcreate(file_id, std.c_str(), H5T_NATIVE_SHORT, filespace, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
66  }
67  else if (std::is_same<type,unsigned short>::value == true)
68  {
69  dset_id = H5Dcreate(file_id, std.c_str(), H5T_NATIVE_USHORT, filespace, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
70  }
71  else if (std::is_same<type,int>::value == true)
72  {
73  dset_id = H5Dcreate(file_id, std.c_str(), H5T_NATIVE_INT, filespace, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
74  }
75  else if (std::is_same<type,unsigned int>::value == true)
76  {
77  dset_id = H5Dcreate(file_id, std.c_str(), H5T_NATIVE_UINT, filespace, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
78  }
79  else if (std::is_same<type,long>::value == true)
80  {
81  dset_id = H5Dcreate(file_id, std.c_str(), H5T_NATIVE_LONG, filespace, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
82  }
83  else if (std::is_same<type,unsigned long>::value == true)
84  {
85  dset_id = H5Dcreate(file_id, std.c_str(), H5T_NATIVE_ULONG, filespace, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
86  }
87  else if (std::is_same<type,long long>::value == true)
88  {
89  dset_id = H5Dcreate(file_id, std.c_str(), H5T_NATIVE_LLONG, filespace, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
90  }
91  else if (std::is_same<type,unsigned long long>::value == true)
92  {
93  dset_id = H5Dcreate(file_id, std.c_str(), H5T_NATIVE_ULLONG, filespace, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
94  }
95  else if (std::is_same<type,float>::value == true)
96  {
97  dset_id = H5Dcreate(file_id, std.c_str(), H5T_NATIVE_FLOAT, filespace, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
98  }*/
99  else if (std::is_same<type,double>::value == true)
100  {
101  hid_t dset_id = H5Dcreate(file_id, str.c_str(), H5T_NATIVE_DOUBLE, filespace, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
102  if (dset_id < 0)
103  return false;
104 
105  herr_t status = H5Dwrite(dset_id, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL, plist_id, ptr);
106  if (status < 0)
107  return false;
108 
109  H5Dclose(dset_id);
110  H5Dclose(filespace);
111  return true;
112  }
113  /*else if (std::is_same<type,long double>::value == true)
114  {
115  dset_id = H5Dcreate(file_id, std.c_str(), H5T_NATIVE_LDOUBLE, filespace, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
116  }*/
117 
118  return true;
119 }
120 
121 
129 template<typename T,size_t pid, typename V>
130 struct H5_write
131 {
140  static inline void write(hid_t file_id, const std::string & str, V & v, size_t stop)
141  {
142  // Create the buffer
143  openfpm::vector<T> buffer;
144  buffer.resize(stop);
145 
146  for (size_t j = 0 ; j < stop ; j++)
147  buffer.get(j) = v.template get<pid>(j);
148 
149  HDF5CreateDataSet<T>(file_id,str.c_str(),buffer.getPointer(),stop*sizeof(T));
150  }
151 };
152 
154 template<typename T,size_t pid, typename V,size_t N1>
155 struct H5_write<T[N1],pid,V>
156 {
157 
166  static inline void write(hid_t file_id, const std::string & str, V & v, size_t stop)
167  {
168  for (size_t i1 = 0 ; i1 < N1 ; i1++)
169  {
170  // Create the buffer
171  openfpm::vector<T> buffer;
172  buffer.resize(stop);
173 
174  for (size_t j = 0 ; j < stop ; j++)
175  buffer.get(j) = v.template get<pid>(j)[i1];
176 
177  std::stringstream sstr;
178  sstr << "_" << i1;
179 
180  HDF5CreateDataSet<T>(file_id,std::string(str) + sstr.str(),v.getPointer(),stop*sizeof(T));
181  }
182  }
183 };
184 
186 template<typename T, size_t pid, typename V,size_t N1,size_t N2>
187 struct H5_write<T[N1][N2],pid,V>
188 {
189 
198  static inline void write(hid_t file_id, const std::string & str, V & v, size_t stop)
199  {
200  for (size_t i1 = 0 ; i1 < N1 ; i1++)
201  {
202  for (size_t i2 = 0 ; i2 < N2 ; i2++)
203  {
204  // Create the buffer
205  openfpm::vector<T> buffer;
206  buffer.resize(stop);
207 
208  for (size_t j = 0 ; j < stop ; j++)
209  buffer.get(j) = v.template get<pid>(j)[i1][i2];
210 
211  std::stringstream sstr;
212  sstr << "_" << i1 << "_" << i2;
213 
214  HDF5CreateDataSet<T>(file_id,std::string(str) + sstr.str(),v.getPointer(),stop*sizeof(T));
215  }
216  }
217  }
218 };
219 
221 template<typename T, size_t pid, typename V,size_t N1,size_t N2,size_t N3>
222 struct H5_write<T[N1][N2][N3],pid,V>
223 {
224 
233  static inline void write(hid_t file_id, const std::string & str, V & v, size_t stop)
234  {
235  for (size_t i1 = 0 ; i1 < N1 ; i1++)
236  {
237  for (size_t i2 = 0 ; i2 < N2 ; i2++)
238  {
239  for (size_t i3 = 0 ; i3 < N3 ; i3++)
240  {
241  // Create the buffer
242  openfpm::vector<T> buffer;
243  buffer.resize(stop);
244 
245  for (size_t j = 0 ; j < stop ; j++)
246  buffer.get(j) = v.template get<pid>(j)[i1][i2][i3];
247 
248  std::stringstream sstr;
249  sstr << "_" << i1 << "_" << i2 << "_" << i3;
250 
251  HDF5CreateDataSet<T>(file_id,std::string(str) + sstr.str(),v.getPointer(),stop*sizeof(T));
252  }
253  }
254  }
255  }
256 };
257 
259 template<typename T, size_t pid, typename V ,size_t N1,size_t N2,size_t N3,size_t N4>
260 struct H5_write<T[N1][N2][N3][N4],pid,V>
261 {
262 
271  static inline void write(hid_t file_id, const std::string & str, V & v, size_t stop)
272  {
273  for (size_t i1 = 0 ; i1 < N1 ; i1++)
274  {
275  for (size_t i2 = 0 ; i2 < N2 ; i2++)
276  {
277  for (size_t i3 = 0 ; i3 < N3 ; i3++)
278  {
279  for (size_t i4 = 0 ; i4 < N4 ; i4++)
280  {
281  // Create the buffer
282  openfpm::vector<T> buffer;
283  buffer.resize(stop);
284 
285  for (size_t j = 0 ; j < stop ; j++)
286  buffer.get(j) = v.template get<pid>(j)[i1][i2][i3][i4];
287 
288 
289  std::stringstream sstr;
290  sstr << "_" << i1 << "_" << i2 << "_" << i3 << "_" << i4;
291 
292  HDF5CreateDataSet<T>(file_id,std::string(str) + sstr.str(),v.getPointer(),stop*sizeof(T));
293  }
294  }
295  }
296  }
297  }
298 };
299 
300 #endif /* OPENFPM_IO_SRC_HDF5_XDMFWRITER_HDF5_XDMFWRITER_UTIL_HPP_ */
static void write(hid_t file_id, const std::string &str, V &v, size_t stop)
write
static void write(hid_t file_id, const std::string &str, V &v, size_t stop)
write
Write an HDF5 dataset in case of scalars and vectors.
static void write(hid_t file_id, const std::string &str, V &v, size_t stop)
write
static void write(hid_t file_id, const std::string &str, V &v, size_t stop)
write
Implementation of 1-D std::vector like structure.
Definition: map_vector.hpp:61
static void write(hid_t file_id, const std::string &str, V &v, size_t stop)
write