Arcane  4.1.12.0
User documentation
Loading...
Searching...
No Matches
Hdf5Utils.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/* Hdf5Utils.h (C) 2000-2026 */
9/* */
10/* Utility functions for hdf5. */
11/*---------------------------------------------------------------------------*/
12#ifndef ARCANE_HDF5_HDF5UTILS_H
13#define ARCANE_HDF5_HDF5UTILS_H
14/*---------------------------------------------------------------------------*/
15/*---------------------------------------------------------------------------*/
16
18#include "arcane/utils/String.h"
19#include "arcane/utils/Array.h"
20#include "arcane/utils/NumericTypes.h"
21
22#include "arcane/datatype/DataTypes.h"
23
24#include "arcane/hdf5/ArcaneHdf5Global.h"
25
26// This macro for MSVC with DLLs, to avoid undefined external symbols
27// undefined with H5T_NATIVE*
28#define _HDF5USEDLL_
29#include <hdf5.h>
30
31#include <mutex>
32
33/*---------------------------------------------------------------------------*/
34/*---------------------------------------------------------------------------*/
35
36// At least hdf5 1.8 is required
37#if (H5_VERS_MAJOR < 2) && (H5_VERS_MAJOR == 1 && H5_VERS_MINOR < 10)
38#error "This version of HDF5 is too old. Version 1.10+ is required"
39#endif
40
41// Keep these macros for compatibility but they will need to be removed.
42#define ARCANE_HDF5_1_6_AND_AFTER
43#define ARCANE_HDF5_1_8_AND_AFTER
44
45/*---------------------------------------------------------------------------*/
46/*---------------------------------------------------------------------------*/
47
48namespace Arcane
49{
50class IParallelMng;
51}
52
53/*---------------------------------------------------------------------------*/
54/*---------------------------------------------------------------------------*/
55
56/*!
57 * \brief Utility functions for Hdf5.
58 */
59namespace Arcane::Hdf5Utils
60{
61extern "C" {
62ARCANE_HDF5_EXPORT herr_t _ArcaneHdf5UtilsGroupIterateMe(hid_t, const char*, void*);
63}
64
65/*---------------------------------------------------------------------------*/
66/*---------------------------------------------------------------------------*/
67
68class ARCANE_HDF5_EXPORT Hdf5Mutex
69{
70
71 public:
72
73 Hdf5Mutex(std::mutex& mutex, bool& is_active)
74 : m_mutex(mutex)
75 , m_is_active(is_active)
76 {}
77
78 public:
79
80 void lock() const
81 {
82 if (m_is_active)
83 m_mutex.lock();
84 }
85 void unlock() const
86 {
87 if (m_is_active)
88 m_mutex.unlock();
89 }
90
91 private:
92
93 std::mutex& m_mutex;
94 bool& m_is_active;
95};
96
97/*---------------------------------------------------------------------------*/
98/*---------------------------------------------------------------------------*/
99
100extern "C++" ARCANE_HDF5_EXPORT Hdf5Mutex&
101_ArcaneHdf5UtilsMutex();
102
103/*---------------------------------------------------------------------------*/
104/*---------------------------------------------------------------------------*/
105/*!
106 * \brief Class serving as an initializer for HDF.
107 *
108 * This object allows safe initialization of HDF5 in multi-thread mode.
109 */
110class ARCANE_HDF5_EXPORT HInit
111{
112 public:
113
114 HInit();
115
116 public:
117
118 //! True HDF5 is compiled with MPI support
119 static constexpr bool hasParallelHdf5()
120 {
121#ifdef H5_HAVE_PARALLEL
122 return true;
123#else
124 return false;
125#endif
126 }
127
128 /*!
129 * \brief Function allowing activation or deactivation of locks
130 * on each HDF5 call.
131 * \warning The environment variable ARCANE_HDF5_DISABLE_MUTEX is
132 * prioritized over the parameter of this function.
133 * \warning In hydride, if a hybrid parallelMng is used in parallel
134 * and a full MPI parallelMng is used, and useMutex() is changed regularly,
135 * be careful not to mix HDF5 calls with the
136 * two parallelMngs.
137 *
138 * \param is_active true if mutexes are activated.
139 */
140 static void useMutex(bool is_active, IParallelMng* pm);
141};
142
143/*---------------------------------------------------------------------------*/
144/*---------------------------------------------------------------------------*/
145/*!
146 * \brief Encapsulates a hid_t.
147 *
148 * This class is not copyable.
149 */
150class ARCANE_HDF5_EXPORT Hid
151{
152 public:
153
154 Hid() = default;
155 Hid(hid_t id)
156 : m_id(id)
157 {}
158 virtual ~Hid() {}
159
160 protected:
161
162 // This copy constructor will eventually need to be forbidden
163 Hid(const Hid& hid)
164 : m_id(hid.id())
165 {}
166 void _setId(hid_t id) { m_id = id; }
167 void _setNullId() { m_id = -1; }
168
169 private:
170
171 Hid& operator=(const Hid& hid) = delete;
172
173 public:
174
175 hid_t id() const { return m_id; }
176 bool isBad() const { return m_id < 0; }
177
178 private:
179
180 hid_t m_id = -1;
181};
182
183/*---------------------------------------------------------------------------*/
184/*---------------------------------------------------------------------------*/
185/*!
186 * \brief Encapsulates a hid_t for a property (H5P*).
187 */
188class ARCANE_HDF5_EXPORT HProperty
189: public Hid
190{
191 public:
192
193 HProperty() { _setId(H5P_DEFAULT); }
194 ~HProperty()
195 {
196 close();
197 }
198 HProperty(HProperty&& rhs)
199 : Hid(rhs.id())
200 {
201 rhs._setNullId();
202 }
203 HProperty& operator=(HProperty&& rhs)
204 {
205 _setId(rhs.id());
206 rhs._setNullId();
207 return (*this);
208 }
209
210 public:
211
212 HProperty(const HProperty& v) = delete;
213 HProperty& operator=(const HProperty& hid) = delete;
214
215 public:
216
217 void close();
218
219 void create(hid_t cls_id);
220 void setId(hid_t new_id)
221 {
222 _setId(new_id);
223 }
224
225 /*!
226 * \brief Creates a file property for MPIIO.
227 *
228 * Only works if HDF5 is compiled with MPI. Otherwise, it throws
229 * an exception. If \a mpi_comm is the MPI communicator associated
230 * with \a pm, calling this method creates a property as follows:
231 *
232 * \code
233 * create(H5P_FILE_ACCESS);
234 * H5Pset_fapl_mpio(id(), mpi_comm, MPI_INFO_NULL);
235 * \endcode
236 */
238
239 /*!
240 * \brief Creates a dataset property for MPIIO.
241 *
242 * Only works if HDF5 is compiled with MPI. Otherwise, it throws
243 * an exception. Calling this method creates a property as follows:
244 *
245 * \code
246 * create(H5P_DATASET_XFER);
247 * H5Pset_dxpl_mpio(id(), H5FD_MPIO_COLLECTIVE);
248 * H5Pset_selection_io(id(), H5D_SELECTION_IO_MODE_OFF);
249 * \endcode
250 */
252
253 /*!
254 * \brief Creates a dataset property for MPIIO.
255 *
256 * Only works if HDF5 is compiled with MPI. Otherwise, it throws
257 * an exception. Calling this method creates a property as follows:
258 *
259 * \code
260 * create(H5P_DATASET_XFER);
261 * H5Pset_dxpl_mpio(id(), H5FD_MPIO_INDEPENDENT);
262 * H5Pset_selection_io(id(), H5D_SELECTION_IO_MODE_OFF);
263 * \endcode
264 */
266};
267
268/*---------------------------------------------------------------------------*/
269/*---------------------------------------------------------------------------*/
270/*!
271 * \brief Encapsulates a hid_t for a file.
272 */
273class ARCANE_HDF5_EXPORT HFile
274: public Hid
275{
276 public:
277
278 HFile() = default;
279 ~HFile() { _close(); }
280 HFile(HFile&& rhs)
281 : Hid(rhs.id())
282 {
283 rhs._setNullId();
284 }
285 HFile& operator=(HFile&& rhs)
286 {
287 _setId(rhs.id());
288 rhs._setNullId();
289 return (*this);
290 }
291 HFile& operator=(const HFile& hid) = delete;
292
293 public:
294
295 ARCANE_DEPRECATED_REASON("Y2023: Copy constructor is deprecated. This class has unique ownership")
296 HFile(const HFile& rhs)
297 : Hid(rhs)
298 {}
299
300 public:
301
302 void openTruncate(const String& var);
303 void openAppend(const String& var);
304 void openRead(const String& var);
305 void openTruncate(const String& var, hid_t plist_id);
306 void openAppend(const String& var, hid_t plist_id);
307 void openRead(const String& var, hid_t plist_id);
308 void close();
309
310 private:
311
312 herr_t _close();
313};
314
315/*---------------------------------------------------------------------------*/
316/*---------------------------------------------------------------------------*/
317/*!
318 * \brief Helper class for searching a group.
319 */
320class ARCANE_HDF5_EXPORT HGroupSearch
321{
322 public:
323
324 HGroupSearch(const String& group_name)
325 : m_group_name(group_name)
326 {
327 }
328
329 public:
330
331 herr_t iterateMe(const char* member_name)
332 {
333 //cerr << "** ITERATE <" << member_name << ">\n";
334 if (m_group_name == member_name)
335 return 1;
336 return 0;
337 }
338
339 private:
340
341 String m_group_name;
342};
343
344/*---------------------------------------------------------------------------*/
345/*---------------------------------------------------------------------------*/
346/*!
347 * \brief Encapsulates a hid_t for a group.
348 */
349class ARCANE_HDF5_EXPORT HGroup
350: public Hid
351{
352 public:
353
354 HGroup() {}
355 ~HGroup() { close(); }
356 HGroup(HGroup&& rhs)
357 : Hid(rhs.id())
358 {
359 rhs._setNullId();
360 }
361 HGroup& operator=(HGroup&& rhs)
362 {
363 _setId(rhs.id());
364 rhs._setNullId();
365 return (*this);
366 }
367 HGroup& operator=(const HGroup& hid) = delete;
368
369 public:
370
371 ARCANE_DEPRECATED_REASON("Y2023: Copy constructor is deprecated. This class has unique ownership")
372 HGroup(const HGroup& rhs)
373 : Hid(rhs)
374 {}
375
376 public:
377
378 void create(const Hid& loc_id, const String& group_name);
379 void openOrCreate(const Hid& loc_id, const String& group_name);
380 void recursiveCreate(const Hid& loc_id, const String& var);
381 void recursiveCreate(const Hid& loc_id, const Array<String>& paths);
382 void checkDelete(const Hid& loc_id, const String& var);
383 void recursiveOpen(const Hid& loc_id, const String& var);
384 void open(const Hid& loc_id, const String& var);
385 void openIfExists(const Hid& loc_id, const Array<String>& var);
386 bool hasChildren(const String& var);
387 void close();
388 static bool hasChildren(hid_t loc_id, const String& var);
389
390 private:
391
392 hid_t _checkOrCreate(hid_t loc_id, const String& group_name);
393 hid_t _checkExist(hid_t loc_id, const String& group_name);
394
395 public:
396};
397
398/*---------------------------------------------------------------------------*/
399/*---------------------------------------------------------------------------*/
400/*!
401 * \brief Encapsulates a hid_t for a dataspace.
402 */
403class ARCANE_HDF5_EXPORT HSpace
404: public Hid
405{
406 public:
407
408 HSpace() {}
409 explicit HSpace(hid_t id)
410 : Hid(id)
411 {}
412 HSpace(HSpace&& rhs)
413 : Hid(rhs.id())
414 {
415 rhs._setNullId();
416 }
417 ~HSpace();
418 HSpace& operator=(HSpace&& rhs)
419 {
420 _setId(rhs.id());
421 rhs._setNullId();
422 return (*this);
423 }
424 HSpace& operator=(const HSpace& hid) = delete;
425
426 public:
427
428 ARCANE_DEPRECATED_REASON("Y2023: Copy constructor is deprecated. This class has unique ownership")
429 HSpace(const HSpace& v)
430 : Hid(v)
431 {}
432
433 public:
434
435 void createSimple(int nb, hsize_t dims[]);
436 void createSimple(int nb, hsize_t dims[], hsize_t max_dims[]);
437 int nbDimension();
438 herr_t getDimensions(hsize_t dims[], hsize_t max_dims[]);
439};
440
441/*---------------------------------------------------------------------------*/
442/*---------------------------------------------------------------------------*/
443/*!
444 * \brief Encapsulates a hid_t for a dataset.
445 */
446class ARCANE_HDF5_EXPORT HDataset
447: public Hid
448{
449 public:
450
451 HDataset() {}
452 ~HDataset() { close(); }
453 HDataset(HDataset&& rhs)
454 : Hid(rhs.id())
455 {
456 rhs._setNullId();
457 }
458 HDataset& operator=(HDataset&& rhs)
459 {
460 _setId(rhs.id());
461 rhs._setNullId();
462 return (*this);
463 }
464 HDataset& operator=(const HDataset& hid) = delete;
465
466 public:
467
468 ARCANE_DEPRECATED_REASON("Y2023: Copy constructor is deprecated. This class has unique ownership")
469 HDataset(const HDataset& v)
470 : Hid(v)
471 {}
472
473 public:
474
475 void close();
476 void create(const Hid& loc_id, const String& var, hid_t save_type, const HSpace& space_id, hid_t plist);
477 void create(const Hid& loc_id, const String& var, hid_t save_type,
478 const HSpace& space_id, const HProperty& link_plist,
479 const HProperty& creation_plist, const HProperty& access_plist);
480 void recursiveCreate(const Hid& loc_id, const String& var, hid_t save_type, const HSpace& space_id, hid_t plist);
481 void open(const Hid& loc_id, const String& var);
482 void openIfExists(const Hid& loc_id, const String& var);
483 herr_t write(hid_t native_type, const void* array);
484 herr_t write(hid_t native_type, const void* array, const HSpace& memspace_id,
485 const HSpace& filespace_id, hid_t plist);
486 herr_t write(hid_t native_type, const void* array, const HSpace& memspace_id,
487 const HSpace& filespace_id, const HProperty& plist);
488 herr_t read(hid_t native_type, void* array);
489 void readWithException(hid_t native_type, void* array);
490 HSpace getSpace();
491 herr_t setExtent(const hsize_t new_dims[]);
492
493 private:
494
495 void _remove(hid_t hid, const String& var);
496};
497
498/*---------------------------------------------------------------------------*/
499/*---------------------------------------------------------------------------*/
500/*!
501 * \brief Encapsulates a hid_t for an attribute.
502 */
503class ARCANE_HDF5_EXPORT HAttribute
504: public Hid
505{
506 public:
507
508 HAttribute() {}
509 ~HAttribute();
510 HAttribute(HAttribute&& rhs)
511 : Hid(rhs.id())
512 {
513 rhs._setNullId();
514 }
515 HAttribute& operator=(HAttribute&& rhs)
516 {
517 _setId(rhs.id());
518 rhs._setNullId();
519 return (*this);
520 }
521 HAttribute& operator=(const HAttribute& hid) = delete;
522
523 public:
524
525 ARCANE_DEPRECATED_REASON("Y2023: Copy constructor is deprecated. This class has unique ownership")
526 HAttribute(const HAttribute& v)
527 : Hid(v)
528 {}
529
530 public:
531
532 void remove(const Hid& loc_id, const String& var);
533 void create(const Hid& loc_id, const String& var, hid_t save_type, const HSpace& space_id);
534 void open(const Hid& loc_id, const String& var);
535 herr_t write(hid_t native_type, void* array);
536 herr_t read(hid_t native_type, void* array);
537 HSpace getSpace();
538};
539
540/*---------------------------------------------------------------------------*/
541/*---------------------------------------------------------------------------*/
542/*!
543 * \brief Encapsulates a hid_t for a type.
544 */
545class ARCANE_HDF5_EXPORT HType
546: public Hid
547{
548 public:
549
550 HType() {}
551 ~HType();
552 HType(HType&& rhs)
553 : Hid(rhs.id())
554 {
555 rhs._setNullId();
556 }
557 HType& operator=(HType&& rhs)
558 {
559 _setId(rhs.id());
560 rhs._setNullId();
561 return (*this);
562 }
563 HType& operator=(const HType& hid) = delete;
564
565 public:
566
567 ARCANE_DEPRECATED_REASON("Y2023: Copy constructor is deprecated. This class has unique ownership")
568 HType(const HType& v)
569 : Hid(v)
570 {}
571
572 public:
573
574 void setId(hid_t new_id)
575 {
576 _setId(new_id);
577 }
578};
579
580/*---------------------------------------------------------------------------*/
581/*---------------------------------------------------------------------------*/
582/*!
583 * \brief Definition of standard Arcane types for hdf5.
584 *
585 * An instance of this class constructs HDF5 types to perform the
586 * conversion between HDF5 types and Arcane types.
587 *
588 * The default constructor using HDF5 calls is not thread-safe.
589 * If running in a multi-threaded context, it is preferable to use
590 * StandardTypes(false) and call init() to initialize the types.
591 */
592class ARCANE_HDF5_EXPORT StandardTypes
593{
594 public:
595
596 /*!
597 * \brief Creates an instance by initializing the types.
598 *
599 * \warning not thread-safe.
600 */
602
603 //! Creates an instance without initializing the types, i.e., do_init is false.
604 explicit StandardTypes(bool do_init);
605
606 ARCANE_DEPRECATED_REASON("Y2023: Copy constructor is deprecated. This class has unique ownership")
607 StandardTypes(const StandardTypes& rhs) = default;
608
610
611 StandardTypes& operator=(const StandardTypes& rhs) = delete;
612
613 public:
614
615 //! Initializes the types.
616 void initialize();
617
618 public:
619
620 hid_t nativeType(float) const { return H5T_NATIVE_FLOAT; }
621 hid_t nativeType(double) const { return H5T_NATIVE_DOUBLE; }
622 hid_t nativeType(Real2) const { return m_real2_id.id(); }
623 hid_t nativeType(Real3) const { return m_real3_id.id(); }
624 hid_t nativeType(Real2x2) const { return m_real2x2_id.id(); }
625 hid_t nativeType(Real3x3) const { return m_real3x3_id.id(); }
626 hid_t nativeType(long double) const { return H5T_NATIVE_LDOUBLE; }
627 hid_t nativeType(unsigned int) const { return H5T_NATIVE_UINT; }
628 hid_t nativeType(unsigned long) const { return H5T_NATIVE_ULONG; }
629 hid_t nativeType(unsigned long long) const { return H5T_NATIVE_ULLONG; }
630 hid_t nativeType(int) const { return H5T_NATIVE_INT; }
631 hid_t nativeType(long long) const { return H5T_NATIVE_LLONG; }
632 hid_t nativeType(long) const { return H5T_NATIVE_LONG; }
633 hid_t nativeType(char) const { return H5T_NATIVE_CHAR; }
634 hid_t nativeType(unsigned char) const { return H5T_NATIVE_UCHAR; }
635 hid_t nativeType(signed char) const { return H5T_NATIVE_SCHAR; }
636 hid_t nativeType(unsigned short) const { return H5T_NATIVE_USHORT; }
637 hid_t nativeType(short) const { return H5T_NATIVE_SHORT; }
638#ifdef ARCANE_REAL_NOT_BUILTIN
639 hid_t nativeType(Real) const;
640#endif
641 hid_t nativeType(eDataType sd) const;
642 hid_t nativeType(BFloat16) const { return m_bfloat16_id.id(); }
643 hid_t nativeType(Float16) const { return m_float16_id.id(); }
644
645 public:
646
647 hid_t saveType(float) const
648 {
649 return m_float32_id.id();
650 }
651 hid_t saveType(double) const
652 {
653 return m_real_id.id();
654 }
655 hid_t saveType(Real2) const
656 {
657 return m_real2_id.id();
658 }
659 hid_t saveType(Real3) const
660 {
661 return m_real3_id.id();
662 }
663 hid_t saveType(Real2x2) const
664 {
665 return m_real2x2_id.id();
666 }
667 hid_t saveType(Real3x3) const
668 {
669 return m_real3x3_id.id();
670 }
671 hid_t saveType(long double) const
672 {
673 return m_real_id.id();
674 }
675 hid_t saveType(short) const
676 {
677 return m_short_id.id();
678 }
679 hid_t saveType(unsigned short) const
680 {
681 return m_ushort_id.id();
682 }
683 hid_t saveType(unsigned int) const
684 {
685 return m_uint_id.id();
686 }
687 hid_t saveType(unsigned long) const
688 {
689 return m_ulong_id.id();
690 }
691 hid_t saveType(unsigned long long) const
692 {
693 return m_ulong_id.id();
694 }
695 hid_t saveType(int) const
696 {
697 return m_int_id.id();
698 }
699 hid_t saveType(long) const
700 {
701 return m_long_id.id();
702 }
703 hid_t saveType(long long) const
704 {
705 return m_long_id.id();
706 }
707 hid_t saveType(char) const
708 {
709 return m_char_id.id();
710 }
711 hid_t saveType(unsigned char) const
712 {
713 return m_uchar_id.id();
714 }
715 hid_t saveType(signed char) const
716 {
717 return m_schar_id.id();
718 }
719 hid_t saveType(BFloat16) const
720 {
721 return m_bfloat16_id.id();
722 }
723 hid_t saveType(Float16) const
724 {
725 return m_float16_id.id();
726 }
727#ifdef ARCANE_REAL_NOT_BUILTIN
728 hid_t saveType(Real) const
729 {
730 return m_real_id.id();
731 }
732#endif
733 hid_t saveType(eDataType sd) const;
734
735 private:
736
737 /*!
738 * \brief Class initializing HDF.
739 *
740 * \warning This instance must always be defined before members that
741 * use HDF5 so that initialization happens first and resource release
742 * happens last.
743 */
744 HInit m_init;
745
746 public:
747
748 HType m_char_id; //!< HDF identifier for characters
749 HType m_uchar_id; //!< HDF identifier for unsigned characters
750 HType m_schar_id; //!< HDF identifier for signed characters
751 HType m_short_id; //!< HDF identifier for signed shorts
752 HType m_ushort_id; //!< HDF identifier for unsigned shorts
753 HType m_int_id; //!< HDF identifier for signed integers
754 HType m_long_id; //!< HDF identifier for signed longs
755 HType m_uint_id; //!< HDF identifier for unsigned integers
756 HType m_ulong_id; //!< HDF identifier for unsigned longs
757 HType m_real_id; //!< HDF identifier for reals
758 HType m_real2_id; //!< HDF identifier for Real2
759 HType m_real3_id; //!< HDF identifier for Real3
760 HType m_real2x2_id; //!< HDF identifier for Real2x2
761 HType m_real3x3_id; //!< HDF identifier for Real3x3
762 HType m_float16_id; //!< HDF identifier for Float16
763 HType m_bfloat16_id; //!< HDF identifier for BFloat16
764 HType m_float32_id; //!< HDF identifier for Float32
765
766 private:
767
768 void _H5Tinsert(hid_t type, const char* name, Integer offset, hid_t field_id);
769};
770
771/*---------------------------------------------------------------------------*/
772/*---------------------------------------------------------------------------*/
773/*!
774 * \brief Encapsulates a simple dataset from an HDF5 file that represents
775 * an array.
776 */
777class ARCANE_HDF5_EXPORT StandardArray
778{
779 public:
780
781 StandardArray(hid_t hfile, const String& hpath);
782 virtual ~StandardArray() {}
783
784 public:
785
786 /*!
787 * \brief When reading, positions the path in \a hfile to the dataset containing the unique_ids.
788 *
789 * This call is optional but if used, it must be done before
790 * reading the values.
791 */
792 void setIdsPath(const String& ids_path);
793 void readDim();
794 Int64ConstArrayView dimensions() const { return m_dimensions; }
795 virtual bool exists() const;
796
797 protected:
798
799 void _write(const void* buffer, Integer nb_element, hid_t save_type, hid_t native_type);
800
801 protected:
802
803 hid_t m_hfile;
804 String m_hpath;
805 String m_ids_hpath;
806 HDataset m_hdataset;
807 HDataset m_ids_dataset;
808 Int64UniqueArray m_dimensions;
809 bool m_is_init;
810};
811
812/*---------------------------------------------------------------------------*/
813/*---------------------------------------------------------------------------*/
814/*!
815 * \brief Encapsulates a simple dataset from an HDF5 file that represents
816 * an array.
817 */
818template <typename DataType>
819class ARCANE_HDF5_EXPORT StandardArrayT
820: public StandardArray
821{
822 private:
823
824 struct ValueWithUid
825 {
826 public:
827
828 Int64 m_uid;
829 Integer m_index;
830
831 public:
832
833 bool operator<(const ValueWithUid& rhs) const
834 {
835 return m_uid < rhs.m_uid;
836 }
837 };
838
839 public:
840
841 StandardArrayT(hid_t hfile, const String& hpath);
842
843 public:
844
845 /*!
846 * \brief Reads the dataset of a 1D array.
847 * This operation is only valid after calling readDim().
848 * \a buffer must have been allocated.
849 * To read directly, use directRead()
850 */
851 void read(StandardTypes& st, ArrayView<DataType> buffer);
852 /*!
853 * \brief Reads the dataset of a 1D array.
854 */
855 void directRead(StandardTypes& st, Array<DataType>& buffer);
856 void parallelRead(IParallelMng* pm, StandardTypes& st,
857 Array<DataType>& buffer, Int64Array& unique_ids);
858 void write(StandardTypes& st, ConstArrayView<DataType> buffer);
859 void parallelWrite(IParallelMng* pm, StandardTypes& st,
861 Int64ConstArrayView unique_ids);
862
863 private:
864
865 void _writeSortedValues(ITraceMng* tm, StandardTypes& st, ConstArrayView<DataType> buffer,
866 Int64ConstArrayView unique_ids);
867};
868
869/*---------------------------------------------------------------------------*/
870/*---------------------------------------------------------------------------*/
871
872/*!
873 * \brief Encapsulates a simple dataset from an HDF5 file that represents a scalar (possibly String).
874 */
875template <typename DataType>
876class ARCANE_HDF5_EXPORT StandardScalarT
877{
878 public:
879
880 //! Constructor
881 StandardScalarT(hid_t hfile, const String& hpath)
882 : m_hfile(hfile)
883 , m_hpath(hpath)
884 {}
885
886 public:
887
888 //! Reads a data item
889 DataType read(Hdf5Utils::StandardTypes& st);
890
891 //! Writes a data item
892 void write(Hdf5Utils::StandardTypes& st, const DataType& t);
893
894 protected:
895
896 hid_t m_hfile;
897 String m_hpath;
898};
899
900/*---------------------------------------------------------------------------*/
901/*---------------------------------------------------------------------------*/
902
903} // namespace Arcane::Hdf5Utils
904
905/*---------------------------------------------------------------------------*/
906/*---------------------------------------------------------------------------*/
907
908#endif
Declarations of types used in Arcane.
Modifiable view of an array of type T.
Base class for 1D data vectors.
Constant view of an array of type T.
Half-precision floating-point type.
Encapsulates a hid_t for a dataset.
Definition Hdf5Utils.h:448
Class serving as an initializer for HDF.
Definition Hdf5Utils.h:111
static constexpr bool hasParallelHdf5()
True HDF5 is compiled with MPI support.
Definition Hdf5Utils.h:119
Encapsulates a hid_t for a property (H5P*).
Definition Hdf5Utils.h:190
void createDatasetTransfertCollectiveMPIIO()
Creates a dataset property for MPIIO.
Definition Hdf5Utils.cc:896
void createFilePropertyMPIIO(IParallelMng *pm)
Creates a file property for MPIIO.
Definition Hdf5Utils.cc:875
void createDatasetTransfertIndependentMPIIO()
Creates a dataset property for MPIIO.
Definition Hdf5Utils.cc:915
Encapsulates a hid_t for a dataspace.
Definition Hdf5Utils.h:405
Encapsulates a hid_t for a type.
Definition Hdf5Utils.h:547
void read(StandardTypes &st, ArrayView< DataType > buffer)
Reads the dataset of a 1D array. This operation is only valid after calling readDim()....
void directRead(StandardTypes &st, Array< DataType > &buffer)
Reads the dataset of a 1D array.
void setIdsPath(const String &ids_path)
When reading, positions the path in hfile to the dataset containing the unique_ids.
StandardScalarT(hid_t hfile, const String &hpath)
Constructor.
Definition Hdf5Utils.h:881
Definition of standard Arcane types for hdf5.
Definition Hdf5Utils.h:593
HType m_bfloat16_id
HDF identifier for BFloat16.
Definition Hdf5Utils.h:763
HType m_real2x2_id
HDF identifier for Real2x2.
Definition Hdf5Utils.h:760
StandardTypes()
Creates an instance by initializing the types.
Definition Hdf5Utils.cc:937
HType m_long_id
HDF identifier for signed longs.
Definition Hdf5Utils.h:754
HType m_int_id
HDF identifier for signed integers.
Definition Hdf5Utils.h:753
HType m_short_id
HDF identifier for signed shorts.
Definition Hdf5Utils.h:751
HType m_real3x3_id
HDF identifier for Real3x3.
Definition Hdf5Utils.h:761
HType m_real_id
HDF identifier for reals.
Definition Hdf5Utils.h:757
HType m_ushort_id
HDF identifier for unsigned shorts.
Definition Hdf5Utils.h:752
HType m_ulong_id
HDF identifier for unsigned longs.
Definition Hdf5Utils.h:756
HType m_schar_id
HDF identifier for signed characters.
Definition Hdf5Utils.h:750
HType m_uchar_id
HDF identifier for unsigned characters.
Definition Hdf5Utils.h:749
HType m_real2_id
HDF identifier for Real2.
Definition Hdf5Utils.h:758
HType m_float32_id
HDF identifier for Float32.
Definition Hdf5Utils.h:764
HType m_float16_id
HDF identifier for Float16.
Definition Hdf5Utils.h:762
HType m_char_id
HDF identifier for characters.
Definition Hdf5Utils.h:748
void initialize()
Initializes the types.
Definition Hdf5Utils.cc:956
HType m_uint_id
HDF identifier for unsigned integers.
Definition Hdf5Utils.h:755
HType m_real3_id
HDF identifier for Real3.
Definition Hdf5Utils.h:759
Interface of the parallelism manager for a subdomain.
Class managing a 2-dimensional real vector.
Definition Real2.h:122
Class managing a 2x2 matrix of reals.
Definition Real2x2.h:55
Class managing a 3-dimensional real vector.
Definition Real3.h:132
Class managing a 3x3 real matrix.
Definition Real3x3.h:67
Utility functions for Hdf5.
Definition Hdf5Utils.cc:34
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --
Array< Int64 > Int64Array
Dynamic one-dimensional array of 64-bit integers.
Definition UtilsTypes.h:125
UniqueArray< Int64 > Int64UniqueArray
Dynamic 1D array of 64-bit integers.
Definition UtilsTypes.h:339
std::int64_t Int64
Signed integer type of 64 bits.
Int32 Integer
Type representing an integer.
bool operator<(const Item &item1, const Item &item2)
Compare two entities.
Definition Item.h:566
ConstArrayView< Int64 > Int64ConstArrayView
C equivalent of a 1D array of 64-bit integers.
Definition UtilsTypes.h:480
double Real
Type representing a real number.
eDataType
Data type.
Definition DataTypes.h:41