kspaceFirstOrder3D-OMP  1.2
The C++ implementation of the k-wave toolbox for the time-domain simulation of acoustic wave fields in 3D
Hdf5FileHeader.cpp
Go to the documentation of this file.
1 /**
2  * @file Hdf5FileHeader.cpp
3  *
4  * @author Jiri Jaros \n
5  * Faculty of Information Technology \n
6  * Brno University of Technology \n
7  * jarosjir@fit.vutbr.cz
8  *
9  * @brief The implementation of the class responsible for working with file headers.
10  *
11  * @version kspaceFirstOrder3D 2.16
12  *
13  * @date 24 August 2017, 09:51 (created) \n
14  * 04 September 2017, 15:03 (revised)
15  *
16  * @copyright Copyright (C) 2017 Jiri Jaros and Bradley Treeby.
17  *
18  * This file is part of the C++ extension of the [k-Wave Toolbox](http://www.k-wave.org).
19  *
20  * This file is part of the k-Wave. k-Wave is free software: you can redistribute it and/or modify it under the terms
21  * of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the
22  * License, or (at your option) any later version.
23  *
24  * k-Wave is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
25  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
26  * more details.
27  *
28  * You should have received a copy of the GNU Lesser General Public License along with k-Wave.
29  * If not, see [http://www.gnu.org/licenses/](http://www.gnu.org/licenses/).
30  */
31 
32 #include <iostream>
33 #include <stdexcept>
34 #include <ctime>
35 
36 // Linux build
37 #ifdef __linux__
38  #include <unistd.h>
39 #endif
40 
41 //Windows 64 build
42 #ifdef _WIN64
43  #include<Winsock2.h>
44  #pragma comment(lib, "Ws2_32.lib")
45 #endif
46 
47 #include <Hdf5/Hdf5FileHeader.h>
48 
49 #include <Parameters/Parameters.h>
50 #include <Logger/Logger.h>
51 
52 using std::string;
53 using std::ios;
54 
55 //--------------------------------------------------------------------------------------------------------------------//
56 //---------------------------------------------------- Constants -----------------------------------------------------//
57 //--------------------------------------------------------------------------------------------------------------------//
58 
59 const string Hdf5FileHeader::kFileTypesNames[] = {"input", "output", "checkpoint", "unknown"};
60 
61 const string Hdf5FileHeader::kMajorFileVersionsNames[] = {"1"};
62 const string Hdf5FileHeader::kMinorFileVersionsNames[] = {"0","1"};
63 
64 
65 /**
66  * Initialisation of static map with header attribute names.
67  */
68 std::map<Hdf5FileHeader::FileHeaderItems, std::string> Hdf5FileHeader::sHeaderNames
69 {
76 
78  {Hdf5FileHeader::FileHeaderItems::kNumberOfCores , "number_of_cpu_cores"},
80  {Hdf5FileHeader::FileHeaderItems::kPeakMemoryConsumption , "peak_core_memory_in_use"},
81 
83  {Hdf5FileHeader::FileHeaderItems::kDataLoadTime , "data_loading_phase_execution_time"},
84  {Hdf5FileHeader::FileHeaderItems::kPreProcessingTime , "pre-processing_phase_execution_time"},
85  {Hdf5FileHeader::FileHeaderItems::kSimulationTime , "simulation_phase_execution_time"},
86  {Hdf5FileHeader::FileHeaderItems::kPostProcessingTime , "post-processing_phase_execution_time"}
87 };// mHeaderNames
88 //----------------------------------------------------------------------------------------------------------------------
89 
90 //--------------------------------------------------------------------------------------------------------------------//
91 //------------------------------------------------- Public methods ---------------------------------------------------//
92 //--------------------------------------------------------------------------------------------------------------------//
93 
94 /**
95  * Constructor.
96  */
98  : mHeaderValues()
99 {
100 
101 }// end of constructor
102 //----------------------------------------------------------------------------------------------------------------------
103 
104 /**
105  * Copy constructor.
106  */
109 {
110 
111 }// end of copy constructor
112 //----------------------------------------------------------------------------------------------------------------------
113 
114 /**
115  * Destructor.
116  */
118 {
119  mHeaderValues.clear();
120 }// end of destructor
121 //----------------------------------------------------------------------------------------------------------------------
122 
123 /**
124  * Read header from the input file.
125  */
127 {
128  // Get file root handle
129  hid_t rootGroup = inputFile.getRootGroup();
130  // shortcut
131  using FHI = FileHeaderItems;
132 
133  // read file type
134  mHeaderValues[FHI::kFileType] = inputFile.readStringAttribute(rootGroup,"/", sHeaderNames[FHI::kFileType]);
135 
136  if (getFileType() == FileType::kInput)
137  {
138  mHeaderValues[FHI::kCreatedBy]
139  = inputFile.readStringAttribute(rootGroup, "/", sHeaderNames[FHI::kCreatedBy]);
140  mHeaderValues[FHI::kCreationDate]
141  = inputFile.readStringAttribute(rootGroup, "/", sHeaderNames[FHI::kCreationDate]);
142  mHeaderValues[FHI::kFileDescription]
143  = inputFile.readStringAttribute(rootGroup, "/", sHeaderNames[FHI::kFileDescription]);
144  mHeaderValues[FHI::kMajorVersion]
145  = inputFile.readStringAttribute(rootGroup, "/", sHeaderNames[FHI::kMajorVersion]);
146  mHeaderValues[FHI::kMinorVersion]
147  = inputFile.readStringAttribute(rootGroup, "/",sHeaderNames[FHI::kMinorVersion]);
148  }
149  else
150  {
151  throw ios::failure(kErrFmtBadInputFileType);
152  }
153 }// end of readHeaderFromInputFile
154 //----------------------------------------------------------------------------------------------------------------------
155 
156 
157 /**
158  * Read header from output file (necessary for checkpoint-restart).
159  */
161 {
162  // Get file root handle
163  hid_t rootGroup = outputFile.getRootGroup();
164  // shortcut
165  using FHI = FileHeaderItems;
166 
167  mHeaderValues[FHI::kFileType] = outputFile.readStringAttribute(rootGroup, "/", sHeaderNames[FHI::kFileType]);
168 
170  {
171  mHeaderValues[FHI::kTotalExecutionTime]
172  = outputFile.readStringAttribute(rootGroup, "/", sHeaderNames[FHI::kTotalExecutionTime]);
173  mHeaderValues[FHI::kDataLoadTime]
174  = outputFile.readStringAttribute(rootGroup, "/", sHeaderNames[FHI::kDataLoadTime]);
175  mHeaderValues[FHI::kPreProcessingTime]
176  = outputFile.readStringAttribute(rootGroup, "/", sHeaderNames[FHI::kPreProcessingTime]);
177  mHeaderValues[FHI::kSimulationTime]
178  = outputFile.readStringAttribute(rootGroup, "/", sHeaderNames[FHI::kSimulationTime]);
179  mHeaderValues[FHI::kPostProcessingTime]
180  = outputFile.readStringAttribute(rootGroup, "/", sHeaderNames[FHI::kPostProcessingTime]);
181  }
182  else
183  {
184  throw ios::failure(kErrFmtBadOutputFileType);
185  }
186 }// end of readHeaderFromOutputFile
187 //----------------------------------------------------------------------------------------------------------------------
188 
189 /**
190  * Read the file header form the checkpoint file.
191  */
193 {
194  // Get file root handle
195  hid_t rootGroup = checkpointFile.getRootGroup();
196  // shortcut
197  using FHI = FileHeaderItems;
198  // read file type
199  mHeaderValues[FHI::kFileType] =
200  checkpointFile.readStringAttribute(rootGroup,"/", sHeaderNames[FHI::kFileType]);
201 
203  {
204  mHeaderValues[FHI::kCreatedBy]
205  = checkpointFile.readStringAttribute(rootGroup, "/", sHeaderNames[FHI::kCreatedBy]);
206  mHeaderValues[FHI::kCreationDate]
207  = checkpointFile.readStringAttribute(rootGroup, "/", sHeaderNames[FHI::kCreationDate]);
208  mHeaderValues[FHI::kFileDescription]
209  = checkpointFile.readStringAttribute(rootGroup, "/", sHeaderNames[FHI::kFileDescription]);
210  mHeaderValues[FHI::kMajorVersion]
211  = checkpointFile.readStringAttribute(rootGroup, "/", sHeaderNames[FHI::kMajorVersion]);
212  mHeaderValues[FHI::kMinorVersion]
213  = checkpointFile.readStringAttribute(rootGroup, "/", sHeaderNames[FHI::kMinorVersion]);
214  }
215  else
216  {
217  throw ios::failure(kErrFmtBadCheckpointFileType);
218  }
219 }// end of readHeaderFromCheckpointFile
220 //----------------------------------------------------------------------------------------------------------------------
221 
222 /**
223  * Write header into the output file.
224  */
226 {
227  // Get file root handle
228  hid_t rootGroup = outputFile.getRootGroup();
229 
230  for (const auto& it : sHeaderNames)
231  {
232  outputFile.writeStringAttribute(rootGroup, "/", it.second, mHeaderValues[it.first]);
233  }
234 }// end of writeHeaderToOutputFile
235 //----------------------------------------------------------------------------------------------------------------------
236 
237 
238 /**
239  * Write header to the output file (only a subset of all possible fields are written).
240  */
242 {
243  // Get file root handle
244  hid_t rootGroup = checkpointFile.getRootGroup();
245  // shortcut
246  using FHI = FileHeaderItems;
247 
248  // Write header
249  checkpointFile.writeStringAttribute(rootGroup,
250  "/",
251  sHeaderNames [FHI::kFileType],
252  mHeaderValues[FHI::kFileType]);
253 
254  checkpointFile.writeStringAttribute(rootGroup,
255  "/",
256  sHeaderNames [FHI::kCreatedBy],
257  mHeaderValues[FHI::kCreatedBy]);
258 
259  checkpointFile.writeStringAttribute(rootGroup,
260  "/",
261  sHeaderNames [FHI::kCreationDate],
262  mHeaderValues[FHI::kCreationDate]);
263 
264  checkpointFile.writeStringAttribute(rootGroup,
265  "/",
266  sHeaderNames [FHI::kFileDescription],
267  mHeaderValues[FHI::kFileDescription]);
268 
269  checkpointFile.writeStringAttribute(rootGroup,
270  "/",
271  sHeaderNames [FHI::kMajorVersion],
272  mHeaderValues[FHI::kMajorVersion]);
273 
274  checkpointFile.writeStringAttribute(rootGroup,
275  "/",
276  sHeaderNames [FHI::kMinorVersion],
277  mHeaderValues[FHI::kMinorVersion]);
278 }// end of writeHeaderToCheckpointFile
279 //----------------------------------------------------------------------------------------------------------------------
280 
281 /**
282  * Set actual date and time.
283  */
285 {
286  struct tm* current;
287  time_t now;
288  time(&now);
289  current = localtime(&now);
290 
291  mHeaderValues[FileHeaderItems::kCreationDate] = Logger::formatMessage("%02i/%02i/%02i, %02i:%02i:%02i",
292  current->tm_mday, current->tm_mon + 1, current->tm_year - 100,
293  current->tm_hour, current->tm_min, current->tm_sec);
294 }// end of setActualCreationTime
295 //----------------------------------------------------------------------------------------------------------------------
296 
297 /**
298  * Get file version as an enum.
299  */
301 {
304  {
306  }
307 
310  {
312  }
313 
315 }// end of getFileVersion
316 //----------------------------------------------------------------------------------------------------------------------
317 
318 
319 /**
320  * Get File type.
321  */
323 {
324  for (int i = static_cast<int>(FileType::kInput); i < static_cast<int>(FileType::kUnknown); i++)
325  {
327  {
328  return static_cast<FileType >(i);
329  }
330  }
331 
333 }// end of getFileType
334 //----------------------------------------------------------------------------------------------------------------------
335 
336 
337 /**
338  * Set File type.
339  */
341 {
342  mHeaderValues[FileHeaderItems::kFileType] = kFileTypesNames[static_cast<int>(fileType)];
343 }// end of setFileType
344 //----------------------------------------------------------------------------------------------------------------------
345 
346 /**
347  * Set Host name.
348  */
350 {
351  char hostName[256];
352 
353  //Linux build
354  #ifdef __linux__
355  gethostname(hostName, 256);
356  #endif
357 
358  //Windows build
359  #ifdef _WIN64
360  WSADATA wsaData;
361  WSAStartup(MAKEWORD(2, 2), &wsaData);
362  gethostname(hostName, 256);
363 
364  WSACleanup();
365  #endif
366 
368 }// end of setHostName
369 //----------------------------------------------------------------------------------------------------------------------
370 
371 
372 /**
373  * Set memory consumption.
374  */
375 void Hdf5FileHeader::setMemoryConsumption(const size_t totalMemory)
376 {
378 
380  = Logger::formatMessage("%ld MB", totalMemory / Parameters::getInstance().getNumberOfThreads());
381 
382 }// end of setMemoryConsumption
383 //----------------------------------------------------------------------------------------------------------------------
384 
385 
386 /**
387  * Set execution times in file header.
388  */
389 void Hdf5FileHeader::setExecutionTimes(const double totalTime,
390  const double loadTime,
391  const double preProcessingTime,
392  const double simulationTime,
393  const double postprocessingTime)
394 {
400 }// end of setExecutionTimes
401 //----------------------------------------------------------------------------------------------------------------------
402 
403 /**
404  * Get execution times stored in the output file header.
405  */
406 void Hdf5FileHeader::getExecutionTimes(double& totalTime,
407  double& loadTime,
408  double& preProcessingTime,
409  double& simulationTime,
410  double& postprocessingTime)
411 {
412  totalTime = std::stof(mHeaderValues[FileHeaderItems::kTotalExecutionTime]);
413  loadTime = std::stof(mHeaderValues[FileHeaderItems::kDataLoadTime]);
414  preProcessingTime = std::stof(mHeaderValues[FileHeaderItems::kPreProcessingTime]);
415  simulationTime = std::stof(mHeaderValues[FileHeaderItems::kSimulationTime]);
416  postprocessingTime = std::stof(mHeaderValues[FileHeaderItems::kPostProcessingTime]);
417 }// end of getExecutionTimes
418 //----------------------------------------------------------------------------------------------------------------------
419 
420 
421 /**
422  * Set Number of cores.
423  */
425 {
427  = Logger::formatMessage("%ld", Parameters::getInstance().getNumberOfThreads());
428 }// end of setNumberOfCores
429 //----------------------------------------------------------------------------------------------------------------------
430 
431 
432 //--------------------------------------------------------------------------------------------------------------------//
433 //------------------------------------------------- Protected methods ------------------------------------------------//
434 //--------------------------------------------------------------------------------------------------------------------//
435 
436 
437 //--------------------------------------------------------------------------------------------------------------------//
438 //------------------------------------------------- Private methods --------------------------------------------------//
439 //--------------------------------------------------------------------------------------------------------------------//
void writeStringAttribute(const hid_t parentGroup, MatrixName &datasetName, MatrixName &attributeName, const std::string &value)
Write string attribute into the dataset under the root group.
Definition: Hdf5File.cpp:876
Description of the file (e.g. simulation).
void setMemoryConsumption(const size_t totalMemory)
Set memory consumption.
hid_t getRootGroup() const
Get handle to the root group of the file.
Definition: Hdf5File.h:608
Total amount of memory consumed by the code.
void setHostName()
Set host name.
static const std::string kFileTypesNames[]
String representation of different file types.
FileType
HDF5 file type.
void writeHeaderToOutputFile(Hdf5File &outputFile)
Write header into the output file.
~Hdf5FileHeader()
Destructor.
Hdf5FileHeader()
Constructor.
The header file containing the parameters of the simulation.
void getExecutionTimes(double &totalTime, double &loadTime, double &preProcessingTime, double &simulationTime, double &postprocessingTime)
Get execution times stored in the output file header.
Machines the code were executed on.
std::map< FileHeaderItems, std::string > mHeaderValues
map for the header values.
The header file containing the class processing file headers. Detail about the file header are descri...
void readHeaderFromCheckpointFile(Hdf5File &checkpointFile)
Read the file header form the checkpoint file.
FileVersion getFileVersion()
Get file version as an enum.
FileVersion
HDF5 file version.
Peak memory consumption (by process).
static std::map< FileHeaderItems, std::string > sHeaderNames
map for the header names.
static const std::string kMajorFileVersionsNames[]
String representations of Major file versions.
std::string readStringAttribute(const hid_t parentGroup, MatrixName &datasetName, MatrixName &attributeName)
Read string attribute from the dataset under the root group.
Definition: Hdf5File.cpp:892
The header file containing a class responsible for printing out info and error messages (stdout...
Class wrapping the HDF5 routines.
Definition: Hdf5File.h:490
ErrorMessage kErrFmtBadCheckpointFileType
HDF5 error message.
void setActualCreationTime()
Set creation time.
static std::string formatMessage(const std::string &format, Args ... args)
C++-11 replacement for sprintf that works with std::string instead of char*.
Definition: Logger.h:157
void readHeaderFromInputFile(Hdf5File &inputFile)
Read header from the input file.
Class for HDF5 file header.
void setExecutionTimes(const double totalTime, const double loadTime, const double preProcessingTime, const double simulationTime, const double postprocessingTime)
Set execution times in file header.
void readHeaderFromOutputFile(Hdf5File &outputFile)
Read header from output file (necessary for checkpoint-restart).
void writeHeaderToCheckpointFile(Hdf5File &checkpointFile)
Write header to the output file (only a subset of all possible fields are written).
Hdf5FileHeader::FileType getFileType()
Get File type.
ErrorMessage kErrFmtBadInputFileType
HDF5 error message.
ErrorMessage kErrFmtBadOutputFileType
HDF5 error message.
static const std::string kMinorFileVersionsNames[]
String representations of Major file versions.
static Parameters & getInstance()
Get instance of the singleton class.
Definition: Parameters.cpp:84
void setFileType(const Hdf5FileHeader::FileType fileType)
Set File type.
Number of cores the simulation was executed.
FileHeaderItems
List of all header items.
void setNumberOfCores()
Set number of cores.