Arcane  v3.15.0.0
Documentation développeur
Chargement...
Recherche...
Aucune correspondance
hdf5/Hdf5Utils.h
1// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
2//-----------------------------------------------------------------------------
3// Copyright 2000-2024 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-2024 */
9/* */
10/* Fonctions utilitaires pour 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// Cette macro pour MSVC avec les dll, pour eviter des symbols externes
27// indéfinis avec H5T_NATIVE*
28#define _HDF5USEDLL_
29#include <hdf5.h>
30
31/*---------------------------------------------------------------------------*/
32/*---------------------------------------------------------------------------*/
33
34// Il faut au moins hdf5 1.8
35#if (H5_VERS_MAJOR<2) && (H5_VERS_MAJOR==1 && H5_VERS_MINOR<10)
36#error "This version of HDF5 is too old. Version 1.10+ is required"
37#endif
38
39// Garde ces macros pour compatibilité mais il faudra les supprimer.
40#define ARCANE_HDF5_1_6_AND_AFTER
41#define ARCANE_HDF5_1_8_AND_AFTER
42
43/*---------------------------------------------------------------------------*/
44/*---------------------------------------------------------------------------*/
45
46namespace Arcane
47{
48
49/*---------------------------------------------------------------------------*/
50/*---------------------------------------------------------------------------*/
51
52class IParallelMng;
53
54/*---------------------------------------------------------------------------*/
55/*---------------------------------------------------------------------------*/
59namespace Hdf5Utils
60{
61extern "C"
62{
63 ARCANE_HDF5_EXPORT herr_t _ArcaneHdf5UtilsGroupIterateMe(hid_t,const char*,void*);
64}
65
66/*---------------------------------------------------------------------------*/
67/*---------------------------------------------------------------------------*/
73class ARCANE_HDF5_EXPORT HInit
74{
75 public:
76
77 HInit();
78
79 public:
80
82 static bool hasParallelHdf5();
83};
84
85/*---------------------------------------------------------------------------*/
86/*---------------------------------------------------------------------------*/
92class ARCANE_HDF5_EXPORT Hid
93{
94 public:
95
96 Hid() = default;
97 Hid(hid_t id)
98 : m_id(id)
99 {}
100 virtual ~Hid() {}
101
102 protected:
103
104 // Il faudra interdire ce constructeur de recopie à terme
105 Hid(const Hid& hid)
106 : m_id(hid.id())
107 {}
108 void _setId(hid_t id) { m_id = id; }
109 void _setNullId() { m_id = -1; }
110
111 private:
112
113 Hid& operator=(const Hid& hid) = delete;
114
115 public:
116
117 hid_t id() const { return m_id; }
118 bool isBad() const { return m_id < 0; }
119
120 private:
121
122 hid_t m_id = -1;
123};
124
125/*---------------------------------------------------------------------------*/
126/*---------------------------------------------------------------------------*/
130class ARCANE_HDF5_EXPORT HProperty
131: public Hid
132{
133 public:
134
135 HProperty() { _setId(H5P_DEFAULT); }
136 ~HProperty()
137 {
138 close();
139 }
141 : Hid(rhs.id())
142 {
143 rhs._setNullId();
144 }
145 HProperty& operator=(HProperty&& rhs)
146 {
147 _setId(rhs.id());
148 rhs._setNullId();
149 return (*this);
150 }
151
152 public:
153
154 HProperty(const HProperty& v) = delete;
155 HProperty& operator=(const HProperty& hid) = delete;
156
157 public:
158
159 void close()
160 {
161 if (id() > 0) {
162 H5Pclose(id());
163 _setNullId();
164 }
165 }
166
167 void create(hid_t cls_id);
168 void setId(hid_t new_id)
169 {
170 _setId(new_id);
171 }
172
185 void createFilePropertyMPIIO(IParallelMng* pm);
186
198 void createDatasetTransfertCollectiveMPIIO();
199};
200
201/*---------------------------------------------------------------------------*/
202/*---------------------------------------------------------------------------*/
206class ARCANE_HDF5_EXPORT HFile
207: public Hid
208{
209 public:
210
211 HFile() = default;
212 ~HFile() { _close(); }
213 HFile(HFile&& rhs)
214 : Hid(rhs.id())
215 {
216 rhs._setNullId();
217 }
218 HFile& operator=(HFile&& rhs)
219 {
220 _setId(rhs.id());
221 rhs._setNullId();
222 return (*this);
223 }
224 HFile& operator=(const HFile& hid) = delete;
225
226 public:
227
228 ARCANE_DEPRECATED_REASON("Y2023: Copy constructor is deprecated. This class has unique ownership")
229 HFile(const HFile& rhs)
230 : Hid(rhs)
231 {}
232
233 public:
234
235 void openTruncate(const String& var);
236 void openAppend(const String& var);
237 void openRead(const String& var);
238 void openTruncate(const String& var, hid_t plist_id);
239 void openAppend(const String& var, hid_t plist_id);
240 void openRead(const String& var, hid_t plist_id);
241 void close();
242
243 private:
244
245 herr_t _close();
246};
247
248/*---------------------------------------------------------------------------*/
249/*---------------------------------------------------------------------------*/
253class ARCANE_HDF5_EXPORT HGroupSearch
254{
255 public:
257 : m_group_name(group_name)
258 {
259 }
260 public:
261 herr_t iterateMe(const char* member_name)
262 {
263 //cerr << "** ITERATE <" << member_name << ">\n";
264 if (m_group_name==member_name)
265 return 1;
266 return 0;
267 }
268 private:
269 String m_group_name;
270};
271
272/*---------------------------------------------------------------------------*/
273/*---------------------------------------------------------------------------*/
277class ARCANE_HDF5_EXPORT HGroup
278: public Hid
279{
280 public:
281
282 HGroup() {}
283 ~HGroup() { close(); }
284 HGroup(HGroup&& rhs)
285 : Hid(rhs.id())
286 {
287 rhs._setNullId();
288 }
289 HGroup& operator=(HGroup&& rhs)
290 {
291 _setId(rhs.id());
292 rhs._setNullId();
293 return (*this);
294 }
295 HGroup& operator=(const HGroup& hid) = delete;
296
297 public:
298
299 ARCANE_DEPRECATED_REASON("Y2023: Copy constructor is deprecated. This class has unique ownership")
300 HGroup(const HGroup& rhs)
301 : Hid(rhs)
302 {}
303
304 public:
305
306 void create(const Hid& loc_id, const String& group_name);
307 void openOrCreate(const Hid& loc_id, const String& group_name);
308 void recursiveCreate(const Hid& loc_id, const String& var);
309 void recursiveCreate(const Hid& loc_id, const Array<String>& paths);
310 void checkDelete(const Hid& loc_id, const String& var);
311 void recursiveOpen(const Hid& loc_id, const String& var);
312 void open(const Hid& loc_id, const String& var);
313 void openIfExists(const Hid& loc_id, const Array<String>& var);
314 bool hasChildren(const String& var);
315 void close();
316 static bool hasChildren(hid_t loc_id, const String& var);
317
318 private:
319
320 hid_t _checkOrCreate(hid_t loc_id, const String& group_name);
321 hid_t _checkExist(hid_t loc_id, const String& group_name);
322
323 public:
324};
325
326/*---------------------------------------------------------------------------*/
327/*---------------------------------------------------------------------------*/
331class ARCANE_HDF5_EXPORT HSpace
332: public Hid
333{
334 public:
335
336 HSpace() {}
337 explicit HSpace(hid_t id)
338 : Hid(id)
339 {}
340 HSpace(HSpace&& rhs)
341 : Hid(rhs.id())
342 {
343 rhs._setNullId();
344 }
345 ~HSpace()
346 {
347 if (id() > 0)
348 H5Sclose(id());
349 }
350 HSpace& operator=(HSpace&& rhs)
351 {
352 _setId(rhs.id());
353 rhs._setNullId();
354 return (*this);
355 }
356 HSpace& operator=(const HSpace& hid) = delete;
357
358 public:
359
360 ARCANE_DEPRECATED_REASON("Y2023: Copy constructor is deprecated. This class has unique ownership")
361 HSpace(const HSpace& v)
362 : Hid(v)
363 {}
364
365 public:
366
367 void createSimple(int nb, hsize_t dims[]);
368 void createSimple(int nb, hsize_t dims[], hsize_t max_dims[]);
369 int nbDimension();
370 herr_t getDimensions(hsize_t dims[], hsize_t max_dims[]);
371};
372
373/*---------------------------------------------------------------------------*/
374/*---------------------------------------------------------------------------*/
378class ARCANE_HDF5_EXPORT HDataset
379: public Hid
380{
381 public:
382
383 HDataset() {}
384 ~HDataset() { close(); }
386 : Hid(rhs.id())
387 {
388 rhs._setNullId();
389 }
390 HDataset& operator=(HDataset&& rhs)
391 {
392 _setId(rhs.id());
393 rhs._setNullId();
394 return (*this);
395 }
396 HDataset& operator=(const HDataset& hid) = delete;
397
398 public:
399
400 ARCANE_DEPRECATED_REASON("Y2023: Copy constructor is deprecated. This class has unique ownership")
401 HDataset(const HDataset& v)
402 : Hid(v)
403 {}
404
405 public:
406
407 void close()
408 {
409 if (id() > 0)
410 H5Dclose(id());
411 _setNullId();
412 }
413 void create(const Hid& loc_id, const String& var, hid_t save_type, const HSpace& space_id, hid_t plist);
414 void create(const Hid& loc_id,const String& var,hid_t save_type,
415 const HSpace& space_id,const HProperty& link_plist,
417 void recursiveCreate(const Hid& loc_id, const String& var, hid_t save_type, const HSpace& space_id, hid_t plist);
418 void open(const Hid& loc_id, const String& var);
419 void openIfExists(const Hid& loc_id, const String& var);
420 herr_t write(hid_t native_type, const void* array);
421 herr_t write(hid_t native_type, const void* array, const HSpace& memspace_id,
423 herr_t write(hid_t native_type, const void* array, const HSpace& memspace_id,
424 const HSpace& filespace_id, const HProperty& plist);
425 herr_t read(hid_t native_type, void* array)
426 {
427 return H5Dread(id(), native_type, H5S_ALL, H5S_ALL, H5P_DEFAULT, array);
428 }
429 void readWithException(hid_t native_type, void* array);
430 HSpace getSpace();
431 herr_t setExtent(const hsize_t new_dims[]);
432
433 private:
434
435 void _remove(hid_t hid, const String& var);
436};
437
438/*---------------------------------------------------------------------------*/
439/*---------------------------------------------------------------------------*/
443class ARCANE_HDF5_EXPORT HAttribute
444: public Hid
445{
446 public:
447
448 HAttribute() {}
450 {
451 if (id() > 0)
452 H5Aclose(id());
453 }
455 : Hid(rhs.id())
456 {
457 rhs._setNullId();
458 }
459 HAttribute& operator=(HAttribute&& rhs)
460 {
461 _setId(rhs.id());
462 rhs._setNullId();
463 return (*this);
464 }
465 HAttribute& operator=(const HAttribute& hid) = delete;
466
467 public:
468
469 ARCANE_DEPRECATED_REASON("Y2023: Copy constructor is deprecated. This class has unique ownership")
470 HAttribute(const HAttribute& v)
471 : Hid(v)
472 {}
473
474 public:
475
476 void remove(const Hid& loc_id, const String& var)
477 {
478 _setId(H5Adelete(loc_id.id(), var.localstr()));
479 }
480 void create(const Hid& loc_id, const String& var, hid_t save_type, const HSpace& space_id)
481 {
482 _setId(H5Acreate2(loc_id.id(), var.localstr(), save_type, space_id.id(), H5P_DEFAULT, H5P_DEFAULT));
483 }
484 void open(const Hid& loc_id, const String& var)
485 {
486 _setId(H5Aopen_name(loc_id.id(), var.localstr()));
487 }
488 herr_t write(hid_t native_type, void* array)
489 {
490 return H5Awrite(id(), native_type, array);
491 }
492 herr_t read(hid_t native_type, void* array)
493 {
494 return H5Aread(id(), native_type, array);
495 }
496 HSpace getSpace()
497 {
498 return HSpace(H5Aget_space(id()));
499 }
500};
501
502
503/*---------------------------------------------------------------------------*/
504/*---------------------------------------------------------------------------*/
508class ARCANE_HDF5_EXPORT HType
509: public Hid
510{
511 public:
512
513 HType() {}
514 ~HType()
515 {
516 if (id() > 0)
517 H5Tclose(id());
518 }
519 HType(HType&& rhs)
520 : Hid(rhs.id())
521 {
522 rhs._setNullId();
523 }
524 HType& operator=(HType&& rhs)
525 {
526 _setId(rhs.id());
527 rhs._setNullId();
528 return (*this);
529 }
530 HType& operator=(const HType& hid) = delete;
531
532 public:
533
534 ARCANE_DEPRECATED_REASON("Y2023: Copy constructor is deprecated. This class has unique ownership")
535 HType(const HType& v)
536 : Hid(v)
537 {}
538
539 public:
540
541 void setId(hid_t new_id)
542 {
543 _setId(new_id);
544 }
545};
546
547/*---------------------------------------------------------------------------*/
548/*---------------------------------------------------------------------------*/
559class ARCANE_HDF5_EXPORT StandardTypes
560{
561 public:
562
569
571 explicit StandardTypes(bool do_init);
572
573 ARCANE_DEPRECATED_REASON("Y2023: Copy constructor is deprecated. This class has unique ownership")
574 StandardTypes(const StandardTypes& rhs) = default;
575
577
578 StandardTypes& operator=(const StandardTypes& rhs) = delete;
579
580 public:
581
583 void initialize();
584
585 public:
586
587 hid_t nativeType(float) const { return H5T_NATIVE_FLOAT; }
588 hid_t nativeType(double) const { return H5T_NATIVE_DOUBLE; }
589 hid_t nativeType(Real2) const { return m_real2_id.id(); }
590 hid_t nativeType(Real3) const { return m_real3_id.id(); }
591 hid_t nativeType(Real2x2) const { return m_real2x2_id.id(); }
592 hid_t nativeType(Real3x3) const { return m_real3x3_id.id(); }
593 hid_t nativeType(long double) const { return H5T_NATIVE_LDOUBLE; }
594 hid_t nativeType(unsigned int) const { return H5T_NATIVE_UINT; }
595 hid_t nativeType(unsigned long) const { return H5T_NATIVE_ULONG; }
596 hid_t nativeType(unsigned long long) const { return H5T_NATIVE_ULLONG; }
597 hid_t nativeType(int) const { return H5T_NATIVE_INT; }
598 hid_t nativeType(long long) const { return H5T_NATIVE_LLONG; }
599 hid_t nativeType(long) const { return H5T_NATIVE_LONG; }
600 hid_t nativeType(char) const { return H5T_NATIVE_CHAR; }
601 hid_t nativeType(unsigned char) const { return H5T_NATIVE_UCHAR; }
602 hid_t nativeType(signed char) const { return H5T_NATIVE_SCHAR; }
603 hid_t nativeType(unsigned short) const { return H5T_NATIVE_USHORT; }
604 hid_t nativeType(short) const { return H5T_NATIVE_SHORT; }
605#ifdef ARCANE_REAL_NOT_BUILTIN
606 hid_t nativeType(Real) const;
607#endif
608 hid_t nativeType(eDataType sd) const;
609 hid_t nativeType(BFloat16) const { return m_bfloat16_id.id(); }
610 hid_t nativeType(Float16) const { return m_float16_id.id(); }
611
612 public:
613
614 hid_t saveType(float) const
615 {
616 return m_float32_id.id();
617 }
618 hid_t saveType(double) const
619 {
620 return m_real_id.id();
621 }
622 hid_t saveType(Real2) const
623 {
624 return m_real2_id.id();
625 }
626 hid_t saveType(Real3) const
627 {
628 return m_real3_id.id();
629 }
630 hid_t saveType(Real2x2) const
631 {
632 return m_real2x2_id.id();
633 }
634 hid_t saveType(Real3x3) const
635 {
636 return m_real3x3_id.id();
637 }
638 hid_t saveType(long double) const
639 {
640 return m_real_id.id();
641 }
642 hid_t saveType(short) const
643 {
644 return m_short_id.id();
645 }
646 hid_t saveType(unsigned short) const
647 {
648 return m_ushort_id.id();
649 }
650 hid_t saveType(unsigned int) const
651 {
652 return m_uint_id.id();
653 }
654 hid_t saveType(unsigned long) const
655 {
656 return m_ulong_id.id();
657 }
658 hid_t saveType(unsigned long long) const
659 {
660 return m_ulong_id.id();
661 }
662 hid_t saveType(int) const
663 {
664 return m_int_id.id();
665 }
666 hid_t saveType(long) const
667 {
668 return m_long_id.id();
669 }
670 hid_t saveType(long long) const
671 {
672 return m_long_id.id();
673 }
674 hid_t saveType(char) const
675 {
676 return m_char_id.id();
677 }
678 hid_t saveType(unsigned char) const
679 {
680 return m_uchar_id.id();
681 }
682 hid_t saveType(signed char) const
683 {
684 return m_schar_id.id();
685 }
686 hid_t saveType(BFloat16) const
687 {
688 return m_bfloat16_id.id();
689 }
690 hid_t saveType(Float16) const
691 {
692 return m_float16_id.id();
693 }
694#ifdef ARCANE_REAL_NOT_BUILTIN
695 hid_t saveType(Real) const
696 {
697 return m_real_id.id();
698 }
699#endif
700 hid_t saveType(eDataType sd) const;
701
702 private:
703
712
713 public:
714
732
733 private:
734
735 void _H5Tinsert(hid_t type, const char* name, Integer offset, hid_t field_id);
736};
737
738/*---------------------------------------------------------------------------*/
739/*---------------------------------------------------------------------------*/
744class ARCANE_HDF5_EXPORT StandardArray
745{
746 public:
747
749 virtual ~StandardArray() {}
750
751 public:
752
759 void setIdsPath(const String& ids_path);
760 void readDim();
761 Int64ConstArrayView dimensions() const { return m_dimensions; }
762 virtual bool exists() const;
763
764 protected:
765
766 void _write(const void* buffer, Integer nb_element, hid_t save_type, hid_t native_type);
767
768 protected:
769
770 hid_t m_hfile;
771 String m_hpath;
772 String m_ids_hpath;
773 HDataset m_hdataset;
774 HDataset m_ids_dataset;
775 Int64UniqueArray m_dimensions;
776 bool m_is_init;
777};
778
779/*---------------------------------------------------------------------------*/
780/*---------------------------------------------------------------------------*/
785template<typename DataType>
786class ARCANE_HDF5_EXPORT StandardArrayT
787: public StandardArray
788{
789 private:
791 {
792 public:
793 Int64 m_uid;
794 Integer m_index;
795 public:
796 bool operator<(const ValueWithUid& rhs) const
797 {
798 return m_uid < rhs.m_uid;
799 }
800 };
801 public:
803 public:
810 void read(StandardTypes& st,ArrayView<DataType> buffer);
814 void directRead(StandardTypes& st,Array<DataType>& buffer);
815 void parallelRead(IParallelMng* pm,StandardTypes& st,
817 void write(StandardTypes& st,ConstArrayView<DataType> buffer);
818 void parallelWrite(IParallelMng* pm,StandardTypes& st,
821 private:
822 void _writeSortedValues(ITraceMng* tm,StandardTypes& st,ConstArrayView<DataType> buffer,
824};
825
826/*---------------------------------------------------------------------------*/
827/*---------------------------------------------------------------------------*/
828
832template<typename DataType>
833class ARCANE_HDF5_EXPORT StandardScalarT
834{
835 public:
837 StandardScalarT(hid_t hfile,const String& hpath) : m_hfile(hfile), m_hpath(hpath) { }
838 public:
840 DataType read(Hdf5Utils::StandardTypes& st);
841
843 void write(Hdf5Utils::StandardTypes& st, const DataType & t);
844
845 protected:
846 hid_t m_hfile;
847 String m_hpath;
848};
849
850/*---------------------------------------------------------------------------*/
851/*---------------------------------------------------------------------------*/
852
853}
854
855/*---------------------------------------------------------------------------*/
856/*---------------------------------------------------------------------------*/
857
858} // End namespace Arcane
859
860/*---------------------------------------------------------------------------*/
861/*---------------------------------------------------------------------------*/
862
863#endif
Déclarations des types utilisés dans Arcane.
Tableau d'items de types quelconques.
Encapsule un hid_t pour un attribute.
Encapsule un hid_t pour un dataset.
Encapsule un hid_t pour un fichier.
Classe d'aide pour rechercher un groupe.
Encapsule un hid_t pour un groupe.
Classe servant d'initialiseur pour HDF.
Encapsule un hid_t pour une propriété (H5P*).
Encapsule un hid_t pour un dataspace.
Encapsule un hid_t pour un type.
Encapsule un hid_t.
Encapsule un dataset simple d'un fichier HDF5 qui représente un tableau.
Encapsule un dataset simple d'un fichier HDF5 qui représente un tableau.
Encapsule un dataset simple d'un fichier HDF5 qui représente un scalaire (éventuellement String).
StandardScalarT(hid_t hfile, const String &hpath)
Constructeur.
Définition des types standards Arcane pour hdf5.
HType m_bfloat16_id
Identifiant HDF pour les BFloat16.
HType m_real2x2_id
Identifiant HDF pour les Real2x2.
HType m_long_id
Identifiant HDF des entiers long signés.
HType m_int_id
Identifiant HDF des entiers signés.
HType m_short_id
Identifiant HDF des entiers signés.
HType m_real3x3_id
Identifiant HDF pour les Real3x3.
HType m_real_id
Identifiant HDF des réels.
HType m_ushort_id
Identifiant HDF des entiers long signés.
HType m_ulong_id
Identifiant HDF des entiers long non signés.
HType m_schar_id
Identifiant HDF des caractères signés.
HType m_uchar_id
Identifiant HDF des caractères non-signés.
HType m_real2_id
Identifiant HDF pour les Real2.
HType m_float32_id
Identifiant HDF pour les Float16.
HType m_float16_id
Identifiant HDF pour les Float16.
HType m_char_id
Identifiant HDF des charactères.
HType m_uint_id
Identifiant HDF des entiers non signés.
HInit m_init
Classe initialisant HDF.
HType m_real3_id
Identifiant HDF pour les Real3.
Interface du gestionnaire de parallélisme pour un sous-domaine.
Lecteur des fichiers de maillage via la bibliothèque LIMA.
Definition Lima.cc:149
Classe gérant un vecteur de réel de dimension 2.
Definition Real2.h:121
Classe gérant une matrice de réel de dimension 2x2.
Definition Real2x2.h:53
Classe gérant un vecteur de réel de dimension 3.
Definition Real3.h:132
Classe gérant une matrice de réel de dimension 3x3.
Definition Real3x3.h:66
Vue constante d'un tableau de type T.
Type flottant demi-précision.
Interface du gestionnaire de traces.
Chaîne de caractères unicode.
Vecteur 1D de données avec sémantique par valeur (style STL).
-*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
eDataType
Type d'une donnée.
Definition DataTypes.h:39