Arcane  4.1.12.0
User documentation
Loading...
Searching...
No Matches
Hdf5ReaderWriter.h
1// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
2//-----------------------------------------------------------------------------
3// Copyright 2000-2026 CEA (www.cea.fr) IFPEN (www.ifpenergiesnouvelles.com)
4// See the top-level COPYRIGHT file for details.
5// SPDX-License-Identifier: Apache-2.0
6//-----------------------------------------------------------------------------
7/*---------------------------------------------------------------------------*/
8/* Hdf5ReaderWriter.h (C) 2000-2023 */
9/* */
10/* Tools for reading/writing in an HDF5 file. */
11/*---------------------------------------------------------------------------*/
12#ifndef ARCANE_HDF5_HDF5READERWRITER_H
13#define ARCANE_HDF5_HDF5READERWRITER_H
14/*---------------------------------------------------------------------------*/
15/*---------------------------------------------------------------------------*/
16
17#include "arcane/core/IDataReader.h"
18#include "arcane/core/IDataWriter.h"
19
20#include "arcane/hdf5/Hdf5Utils.h"
21#include "arcane/core/VariableTypes.h"
22
23/*---------------------------------------------------------------------------*/
24/*---------------------------------------------------------------------------*/
25
26namespace Arcane
27{
28
29/*---------------------------------------------------------------------------*/
30/*---------------------------------------------------------------------------*/
31
32/*!
33 \brief Reading/Writing in HDF5 format.
34
35 The HDF5 version used is at least version 1.4.3.
36
37 Regarding real numbers, only double precision is supported. They are
38 therefore stored in 8 bytes as well.
39
40 For #Real2, #Real2x2, #Real3, and #Real3x3, a composite type is used.
41
42 The structure of the saved information is as follows:
43 <ul>
44 <li> * all variables are saved in a group called "Variables". * .</li>
45 <li> * for each variable, a subgroup named after the variable is created. This
46 subgroup contains the following attributes and datasets:
47 <ul>
48 <li> * An attribute named "Dims" which is an array of 1 or 2 #Integer
49 elements containing information about the sizes and dimensions of the
50 variable. This attribute is always present and is used, among other
51 things, to determine if the other two datasets are present. The first value
52 (index 0) is always the number of elements in the array. If the variable is a
53 one-dimensional array, there are no other values. If the array is
54 two-dimensional, the second value is equal to the size of the first dimension
55 of the array, while the sizes of the second dimensions are given by the
56 "Dim2" attribute.</li>
57 <li> * A dataset named "Dim2". This dataset is only present if the variable
58 is a two-dimensional array, when the first dimension is not zero and the
59 number of elements is not zero. In this case, this dataset is an array of
60 #Integer type whose size is equal to the size of the first dimension of the
61 variable, and thus each value is equal to the size of the second dimension.
62 </li>
63 <li> * A dataset named "Values" containing the values of the variable. This
64 dataset is not present in the case of an array variable whose number of
65 elements is zero or when the variable is temporary (IVariable::PNoDump
66 property). * </li>
67 </ul>
68 </li>
69 </ul>
70
71 \todo save/read the list of mesh entity groups.
72
73 \warning * The handling of reading/writing in this format is currently at
74 the experimental stage and cannot be used to ensure long-term data persistence.
75 */
76class Hdf5ReaderWriter
77: public TraceAccessor
78, public IDataReader
79, public IDataWriter
80{
81
82 public:
83
84 enum eOpenMode
85 {
86 OpenModeRead,
87 OpenModeTruncate,
88 OpenModeAppend
89 };
90
91 public:
92
93 Hdf5ReaderWriter(ISubDomain* sd, const String& filename, const String& m_sub_group_name,
94 Integer fileset_size,
95 Integer write_index, Integer index_modulo,
96 eOpenMode om, bool do_verif = false);
97 ~Hdf5ReaderWriter();
98
99 public:
100
101 virtual void initialize();
102
103 virtual void beginWrite(const VariableCollection& vars)
104 {
105 ARCANE_UNUSED(vars);
106 }
107 virtual void endWrite();
108 virtual void beginRead(const VariableCollection& vars)
109 {
110 ARCANE_UNUSED(vars);
111 }
112 virtual void endRead() {}
113
114 virtual void setMetaData(const String& meta_data);
115 virtual String metaData();
116
117 virtual void write(IVariable* v, IData* data);
118 virtual void read(IVariable* v, IData* data);
119
120 public:
121
122 herr_t iterateMe(hid_t group_id, const char* member_name);
123
124 private:
125
126 IParallelMng* m_parallel_mng; //!< Parallelism manager;
127 eOpenMode m_open_mode; //!< Open mode
128 String m_filename; //!< Filename.
129 String m_sub_group_name; //!< Subgroup name.
130 bool m_is_initialized; //!< True if already initialized
131
133
134 Hdf5Utils::HFile m_file_id; //!< HDF file identifier
135 Hdf5Utils::HGroup m_sub_group_id; //!< HDF group identifier containing the protection
136 Hdf5Utils::HGroup m_variable_group_id; //!< HDF group identifier containing the variables
137
138 StringList m_variables_name; //!< List of names of saved variables.
139 Timer m_io_timer;
140
141 private:
142
143 //! Active parallel mode: WARNING: for testing only
144 bool m_is_parallel;
145 Int32 m_my_rank;
146 Int32 m_send_rank;
147 Int32 m_last_recv_rank;
148
149 Integer m_fileset_size;
150 Integer m_index_write;
151 Integer m_index_modulo;
152
153 private:
154
155 void _writeVal(const String& var_group_name,
156 const String& sub_group_name,
157 const ISerializedData* sdata,
158 const Int32 from_rank = 0);
159 void _writeValParallel(IVariable* v, const ISerializedData* sdata);
160 void _readVal(IVariable* var, IData* data);
161
162 Ref<ISerializedData> _readDim2(IVariable* v);
163
164 void _directReadVal(IVariable* v, IData* data);
165 void _directWriteVal(IVariable* v, IData* data);
166 void _checkValid();
167 String _variableGroupName(IVariable* var);
168
169 void _receiveRemoteVariables();
170 void _writeRemoteVariable(ISerializer* sb);
171 void _setMetaData(const String& meta_data, const String& sub_group_name);
172};
173
174/*---------------------------------------------------------------------------*/
175/*---------------------------------------------------------------------------*/
176
177} // End namespace Arcane
178
179/*---------------------------------------------------------------------------*/
180/*---------------------------------------------------------------------------*/
181
182#endif
virtual void read(IVariable *v, IData *data)
Reads the data data of the variable var.
virtual String metaData()
Metadata.
virtual void write(IVariable *v, IData *data)
Writes the data data of the variable var.
virtual void setMetaData(const String &meta_data)
Sets the metadata information.
Encapsulates a hid_t for a file.
Definition Hdf5Utils.h:275
Encapsulates a hid_t for a group.
Definition Hdf5Utils.h:351
Definition of standard Arcane types for hdf5.
Definition Hdf5Utils.h:593
Interface for reading variable data.
Definition IDataReader.h:35
Interface for writing variable data.
Definition IDataWriter.h:45
Interface of a data item.
Definition IData.h:34
Interface of the parallelism manager for a subdomain.
Interface of the subdomain manager.
Definition ISubDomain.h:75
Interface of a variable.
Definition IVariable.h:40
Reference to an instance.
Management of a timer.
Definition Timer.h:63
TraceAccessor(ITraceMng *m)
Constructs an accessor via the trace manager m.
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --
Int32 Integer
Type representing an integer.
List< String > StringList
Unicode string list.
Definition UtilsTypes.h:509
std::int32_t Int32
Signed integer type of 32 bits.