Arcane  v3.14.10.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-2023 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-2023 */
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/Real2.h"
21#include "arcane/utils/Real3.h"
22#include "arcane/utils/Real2x2.h"
23#include "arcane/utils/Real3x3.h"
24#include "arcane/datatype/DataTypes.h"
25
26#include "arcane/hdf5/ArcaneHdf5Global.h"
27
28// Cette macro pour MSVC avec les dll, pour eviter des symbols externes
29// indéfinis avec H5T_NATIVE*
30#define _HDF5USEDLL_
31#include <hdf5.h>
32
33/*---------------------------------------------------------------------------*/
34/*---------------------------------------------------------------------------*/
35
36// Il faut au moins hdf5 1.8
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// Garde ces macros pour compatibilité mais il faudra les supprimer.
42#define ARCANE_HDF5_1_6_AND_AFTER
43#define ARCANE_HDF5_1_8_AND_AFTER
44
45/*---------------------------------------------------------------------------*/
46/*---------------------------------------------------------------------------*/
47
48namespace Arcane
49{
50
51/*---------------------------------------------------------------------------*/
52/*---------------------------------------------------------------------------*/
53
54class IParallelMng;
55
56/*---------------------------------------------------------------------------*/
57/*---------------------------------------------------------------------------*/
61namespace Hdf5Utils
62{
63extern "C"
64{
65 ARCANE_HDF5_EXPORT herr_t _ArcaneHdf5UtilsGroupIterateMe(hid_t,const char*,void*);
66}
67
68/*---------------------------------------------------------------------------*/
69/*---------------------------------------------------------------------------*/
75class ARCANE_HDF5_EXPORT HInit
76{
77 public:
78
79 HInit();
80
81 public:
82
84 static bool hasParallelHdf5();
85};
86
87/*---------------------------------------------------------------------------*/
88/*---------------------------------------------------------------------------*/
94class ARCANE_HDF5_EXPORT Hid
95{
96 public:
97
98 Hid() = default;
99 Hid(hid_t id)
100 : m_id(id)
101 {}
102 virtual ~Hid() {}
103
104 protected:
105
106 // Il faudra interdire ce constructeur de recopie à terme
107 Hid(const Hid& hid)
108 : m_id(hid.id())
109 {}
110 void _setId(hid_t id) { m_id = id; }
111 void _setNullId() { m_id = -1; }
112
113 private:
114
115 Hid& operator=(const Hid& hid) = delete;
116
117 public:
118
119 hid_t id() const { return m_id; }
120 bool isBad() const { return m_id < 0; }
121
122 private:
123
124 hid_t m_id = -1;
125};
126
127/*---------------------------------------------------------------------------*/
128/*---------------------------------------------------------------------------*/
132class ARCANE_HDF5_EXPORT HProperty
133: public Hid
134{
135 public:
136
137 HProperty() { _setId(H5P_DEFAULT); }
138 ~HProperty()
139 {
140 close();
141 }
143 : Hid(rhs.id())
144 {
145 rhs._setNullId();
146 }
147 HProperty& operator=(HProperty&& rhs)
148 {
149 _setId(rhs.id());
150 rhs._setNullId();
151 return (*this);
152 }
153
154 public:
155
156 HProperty(const HProperty& v) = delete;
157 HProperty& operator=(const HProperty& hid) = delete;
158
159 public:
160
161 void close()
162 {
163 if (id() > 0) {
164 H5Pclose(id());
165 _setNullId();
166 }
167 }
168
169 void create(hid_t cls_id);
170 void setId(hid_t new_id)
171 {
172 _setId(new_id);
173 }
174
187 void createFilePropertyMPIIO(IParallelMng* pm);
188
200 void createDatasetTransfertCollectiveMPIIO();
201};
202
203/*---------------------------------------------------------------------------*/
204/*---------------------------------------------------------------------------*/
208class ARCANE_HDF5_EXPORT HFile
209: public Hid
210{
211 public:
212
213 HFile() = default;
214 ~HFile() { _close(); }
215 HFile(HFile&& rhs)
216 : Hid(rhs.id())
217 {
218 rhs._setNullId();
219 }
220 HFile& operator=(HFile&& rhs)
221 {
222 _setId(rhs.id());
223 rhs._setNullId();
224 return (*this);
225 }
226 HFile& operator=(const HFile& hid) = delete;
227
228 public:
229
230 ARCANE_DEPRECATED_REASON("Y2023: Copy constructor is deprecated. This class has unique ownership")
231 HFile(const HFile& rhs)
232 : Hid(rhs)
233 {}
234
235 public:
236
237 void openTruncate(const String& var);
238 void openAppend(const String& var);
239 void openRead(const String& var);
240 void openTruncate(const String& var, hid_t plist_id);
241 void openAppend(const String& var, hid_t plist_id);
242 void openRead(const String& var, hid_t plist_id);
243 void close();
244
245 private:
246
247 herr_t _close();
248};
249
250/*---------------------------------------------------------------------------*/
251/*---------------------------------------------------------------------------*/
255class ARCANE_HDF5_EXPORT HGroupSearch
256{
257 public:
259 : m_group_name(group_name)
260 {
261 }
262 public:
263 herr_t iterateMe(const char* member_name)
264 {
265 //cerr << "** ITERATE <" << member_name << ">\n";
266 if (m_group_name==member_name)
267 return 1;
268 return 0;
269 }
270 private:
271 String m_group_name;
272};
273
274/*---------------------------------------------------------------------------*/
275/*---------------------------------------------------------------------------*/
279class ARCANE_HDF5_EXPORT HGroup
280: public Hid
281{
282 public:
283
284 HGroup() {}
285 ~HGroup() { close(); }
286 HGroup(HGroup&& rhs)
287 : Hid(rhs.id())
288 {
289 rhs._setNullId();
290 }
291 HGroup& operator=(HGroup&& rhs)
292 {
293 _setId(rhs.id());
294 rhs._setNullId();
295 return (*this);
296 }
297 HGroup& operator=(const HGroup& hid) = delete;
298
299 public:
300
301 ARCANE_DEPRECATED_REASON("Y2023: Copy constructor is deprecated. This class has unique ownership")
302 HGroup(const HGroup& rhs)
303 : Hid(rhs)
304 {}
305
306 public:
307
308 void create(const Hid& loc_id, const String& group_name);
309 void openOrCreate(const Hid& loc_id, const String& group_name);
310 void recursiveCreate(const Hid& loc_id, const String& var);
311 void recursiveCreate(const Hid& loc_id, const Array<String>& paths);
312 void checkDelete(const Hid& loc_id, const String& var);
313 void recursiveOpen(const Hid& loc_id, const String& var);
314 void open(const Hid& loc_id, const String& var);
315 void openIfExists(const Hid& loc_id, const Array<String>& var);
316 bool hasChildren(const String& var);
317 void close();
318 static bool hasChildren(hid_t loc_id, const String& var);
319
320 private:
321
322 hid_t _checkOrCreate(hid_t loc_id, const String& group_name);
323 hid_t _checkExist(hid_t loc_id, const String& group_name);
324
325 public:
326};
327
328/*---------------------------------------------------------------------------*/
329/*---------------------------------------------------------------------------*/
333class ARCANE_HDF5_EXPORT HSpace
334: public Hid
335{
336 public:
337
338 HSpace() {}
339 explicit HSpace(hid_t id)
340 : Hid(id)
341 {}
342 HSpace(HSpace&& rhs)
343 : Hid(rhs.id())
344 {
345 rhs._setNullId();
346 }
347 ~HSpace()
348 {
349 if (id() > 0)
350 H5Sclose(id());
351 }
352 HSpace& operator=(HSpace&& rhs)
353 {
354 _setId(rhs.id());
355 rhs._setNullId();
356 return (*this);
357 }
358 HSpace& operator=(const HSpace& hid) = delete;
359
360 public:
361
362 ARCANE_DEPRECATED_REASON("Y2023: Copy constructor is deprecated. This class has unique ownership")
363 HSpace(const HSpace& v)
364 : Hid(v)
365 {}
366
367 public:
368
369 void createSimple(int nb, hsize_t dims[]);
370 void createSimple(int nb, hsize_t dims[], hsize_t max_dims[]);
371 int nbDimension();
372 herr_t getDimensions(hsize_t dims[], hsize_t max_dims[]);
373};
374
375/*---------------------------------------------------------------------------*/
376/*---------------------------------------------------------------------------*/
380class ARCANE_HDF5_EXPORT HDataset
381: public Hid
382{
383 public:
384
385 HDataset() {}
386 ~HDataset() { close(); }
388 : Hid(rhs.id())
389 {
390 rhs._setNullId();
391 }
392 HDataset& operator=(HDataset&& rhs)
393 {
394 _setId(rhs.id());
395 rhs._setNullId();
396 return (*this);
397 }
398 HDataset& operator=(const HDataset& hid) = delete;
399
400 public:
401
402 ARCANE_DEPRECATED_REASON("Y2023: Copy constructor is deprecated. This class has unique ownership")
403 HDataset(const HDataset& v)
404 : Hid(v)
405 {}
406
407 public:
408
409 void close()
410 {
411 if (id() > 0)
412 H5Dclose(id());
413 _setNullId();
414 }
415 void create(const Hid& loc_id, const String& var, hid_t save_type, const HSpace& space_id, hid_t plist);
416 void create(const Hid& loc_id,const String& var,hid_t save_type,
417 const HSpace& space_id,const HProperty& link_plist,
419 void recursiveCreate(const Hid& loc_id, const String& var, hid_t save_type, const HSpace& space_id, hid_t plist);
420 void open(const Hid& loc_id, const String& var);
421 void openIfExists(const Hid& loc_id, const String& var);
422 herr_t write(hid_t native_type, const void* array);
423 herr_t write(hid_t native_type, const void* array, const HSpace& memspace_id,
425 herr_t write(hid_t native_type, const void* array, const HSpace& memspace_id,
426 const HSpace& filespace_id, const HProperty& plist);
427 herr_t read(hid_t native_type, void* array)
428 {
429 return H5Dread(id(), native_type, H5S_ALL, H5S_ALL, H5P_DEFAULT, array);
430 }
431 void readWithException(hid_t native_type, void* array);
432 HSpace getSpace();
433 herr_t setExtent(const hsize_t new_dims[]);
434
435 private:
436
437 void _remove(hid_t hid, const String& var);
438};
439
440/*---------------------------------------------------------------------------*/
441/*---------------------------------------------------------------------------*/
445class ARCANE_HDF5_EXPORT HAttribute
446: public Hid
447{
448 public:
449
450 HAttribute() {}
452 {
453 if (id() > 0)
454 H5Aclose(id());
455 }
457 : Hid(rhs.id())
458 {
459 rhs._setNullId();
460 }
461 HAttribute& operator=(HAttribute&& rhs)
462 {
463 _setId(rhs.id());
464 rhs._setNullId();
465 return (*this);
466 }
467 HAttribute& operator=(const HAttribute& hid) = delete;
468
469 public:
470
471 ARCANE_DEPRECATED_REASON("Y2023: Copy constructor is deprecated. This class has unique ownership")
472 HAttribute(const HAttribute& v)
473 : Hid(v)
474 {}
475
476 public:
477
478 void remove(const Hid& loc_id, const String& var)
479 {
480 _setId(H5Adelete(loc_id.id(), var.localstr()));
481 }
482 void create(const Hid& loc_id, const String& var, hid_t save_type, const HSpace& space_id)
483 {
484 _setId(H5Acreate2(loc_id.id(), var.localstr(), save_type, space_id.id(), H5P_DEFAULT, H5P_DEFAULT));
485 }
486 void open(const Hid& loc_id, const String& var)
487 {
488 _setId(H5Aopen_name(loc_id.id(), var.localstr()));
489 }
490 herr_t write(hid_t native_type, void* array)
491 {
492 return H5Awrite(id(), native_type, array);
493 }
494 herr_t read(hid_t native_type, void* array)
495 {
496 return H5Aread(id(), native_type, array);
497 }
498 HSpace getSpace()
499 {
500 return HSpace(H5Aget_space(id()));
501 }
502};
503
504
505/*---------------------------------------------------------------------------*/
506/*---------------------------------------------------------------------------*/
510class ARCANE_HDF5_EXPORT HType
511: public Hid
512{
513 public:
514
515 HType() {}
516 ~HType()
517 {
518 if (id() > 0)
519 H5Tclose(id());
520 }
521 HType(HType&& rhs)
522 : Hid(rhs.id())
523 {
524 rhs._setNullId();
525 }
526 HType& operator=(HType&& rhs)
527 {
528 _setId(rhs.id());
529 rhs._setNullId();
530 return (*this);
531 }
532 HType& operator=(const HType& hid) = delete;
533
534 public:
535
536 ARCANE_DEPRECATED_REASON("Y2023: Copy constructor is deprecated. This class has unique ownership")
537 HType(const HType& v)
538 : Hid(v)
539 {}
540
541 public:
542
543 void setId(hid_t new_id)
544 {
545 _setId(new_id);
546 }
547};
548
549/*---------------------------------------------------------------------------*/
550/*---------------------------------------------------------------------------*/
561class ARCANE_HDF5_EXPORT StandardTypes
562{
563 public:
564
571
573 explicit StandardTypes(bool do_init);
574
575 ARCANE_DEPRECATED_REASON("Y2023: Copy constructor is deprecated. This class has unique ownership")
576 StandardTypes(const StandardTypes& rhs) = default;
577
579
580 ARCANE_DEPRECATED_REASON("Y2023: Copy operator is deprecated. This class has unique ownership")
581 StandardTypes& operator=(const StandardTypes& rhs) = default;
582
583 public:
584
586 void initialize();
587
588 public:
589
590 hid_t nativeType(float) const { return H5T_NATIVE_FLOAT; }
591 hid_t nativeType(double) const { return H5T_NATIVE_DOUBLE; }
592 hid_t nativeType(Real2) const { return m_real2_id.id(); }
593 hid_t nativeType(Real3) const { return m_real3_id.id(); }
594 hid_t nativeType(Real2x2) const { return m_real2x2_id.id(); }
595 hid_t nativeType(Real3x3) const { return m_real3x3_id.id(); }
596 hid_t nativeType(long double) const { return H5T_NATIVE_LDOUBLE; }
597 hid_t nativeType(unsigned int) const { return H5T_NATIVE_UINT; }
598 hid_t nativeType(unsigned long) const { return H5T_NATIVE_ULONG; }
599 hid_t nativeType(unsigned long long) const { return H5T_NATIVE_ULLONG; }
600 hid_t nativeType(int) const { return H5T_NATIVE_INT; }
601 hid_t nativeType(long long) const { return H5T_NATIVE_LLONG; }
602 hid_t nativeType(long) const { return H5T_NATIVE_LONG; }
603 hid_t nativeType(char) const { return H5T_NATIVE_CHAR; }
604 hid_t nativeType(unsigned char) const { return H5T_NATIVE_UCHAR; }
605 hid_t nativeType(signed char) const { return H5T_NATIVE_SCHAR; }
606 hid_t nativeType(unsigned short) const { return H5T_NATIVE_USHORT; }
607 hid_t nativeType(short) const { return H5T_NATIVE_SHORT; }
608#ifdef ARCANE_REAL_NOT_BUILTIN
609 hid_t nativeType(Real) const;
610#endif
611 hid_t nativeType(eDataType sd) const;
612
613 public:
614
615 hid_t saveType(float) const
616 {
617 return m_real_id.id();
618 }
619 hid_t saveType(double) const
620 {
621 return m_real_id.id();
622 }
623 hid_t saveType(Real2) const
624 {
625 return m_real2_id.id();
626 }
627 hid_t saveType(Real3) const
628 {
629 return m_real3_id.id();
630 }
631 hid_t saveType(Real2x2) const
632 {
633 return m_real2x2_id.id();
634 }
635 hid_t saveType(Real3x3) const
636 {
637 return m_real3x3_id.id();
638 }
639 hid_t saveType(long double) const
640 {
641 return m_real_id.id();
642 }
643 hid_t saveType(short) const
644 {
645 return m_short_id.id();
646 }
647 hid_t saveType(unsigned short) const
648 {
649 return m_ushort_id.id();
650 }
651 hid_t saveType(unsigned int) const
652 {
653 return m_uint_id.id();
654 }
655 hid_t saveType(unsigned long) const
656 {
657 return m_ulong_id.id();
658 }
659 hid_t saveType(unsigned long long) const
660 {
661 return m_ulong_id.id();
662 }
663 hid_t saveType(int) const
664 {
665 return m_int_id.id();
666 }
667 hid_t saveType(long) const
668 {
669 return m_long_id.id();
670 }
671 hid_t saveType(long long) const
672 {
673 return m_long_id.id();
674 }
675 hid_t saveType(char) const
676 {
677 return m_char_id.id();
678 }
679 hid_t saveType(unsigned char) const
680 {
681 return m_uchar_id.id();
682 }
683 hid_t saveType(signed char) const
684 {
685 return m_char_id.id();
686 }
687#ifdef ARCANE_REAL_NOT_BUILTIN
688 hid_t saveType(Real) const
689 {
690 return m_real_id.id();
691 }
692#endif
693 hid_t saveType(eDataType sd) const;
694
695 private:
696
705
706 public:
707
721
722 private:
723
724 void _H5Tinsert(hid_t type, const char* name, Integer offset, hid_t field_id);
725};
726
727/*---------------------------------------------------------------------------*/
728/*---------------------------------------------------------------------------*/
733class ARCANE_HDF5_EXPORT StandardArray
734{
735 public:
736
738 virtual ~StandardArray() {}
739
740 public:
741
748 void setIdsPath(const String& ids_path);
749 void readDim();
750 Int64ConstArrayView dimensions() const { return m_dimensions; }
751 virtual bool exists() const;
752
753 protected:
754
755 void _write(const void* buffer, Integer nb_element, hid_t save_type, hid_t native_type);
756
757 protected:
758
759 hid_t m_hfile;
760 String m_hpath;
761 String m_ids_hpath;
762 HDataset m_hdataset;
763 HDataset m_ids_dataset;
764 Int64UniqueArray m_dimensions;
765 bool m_is_init;
766};
767
768/*---------------------------------------------------------------------------*/
769/*---------------------------------------------------------------------------*/
774template<typename DataType>
775class ARCANE_HDF5_EXPORT StandardArrayT
776: public StandardArray
777{
778 private:
780 {
781 public:
782 Int64 m_uid;
783 Integer m_index;
784 public:
785 bool operator<(const ValueWithUid& rhs) const
786 {
787 return m_uid < rhs.m_uid;
788 }
789 };
790 public:
792 public:
799 void read(StandardTypes& st,ArrayView<DataType> buffer);
803 void directRead(StandardTypes& st,Array<DataType>& buffer);
804 void parallelRead(IParallelMng* pm,StandardTypes& st,
806 void write(StandardTypes& st,ConstArrayView<DataType> buffer);
807 void parallelWrite(IParallelMng* pm,StandardTypes& st,
810 private:
811 void _writeSortedValues(ITraceMng* tm,StandardTypes& st,ConstArrayView<DataType> buffer,
813};
814
815/*---------------------------------------------------------------------------*/
816/*---------------------------------------------------------------------------*/
817
821template<typename DataType>
822class ARCANE_HDF5_EXPORT StandardScalarT
823{
824 public:
826 StandardScalarT(hid_t hfile,const String& hpath) : m_hfile(hfile), m_hpath(hpath) { }
827 public:
829 DataType read(Hdf5Utils::StandardTypes& st);
830
832 void write(Hdf5Utils::StandardTypes& st, const DataType & t);
833
834 protected:
835 hid_t m_hfile;
836 String m_hpath;
837};
838
839/*---------------------------------------------------------------------------*/
840/*---------------------------------------------------------------------------*/
841
842}
843
844/*---------------------------------------------------------------------------*/
845/*---------------------------------------------------------------------------*/
846
847} // End namespace Arcane
848
849/*---------------------------------------------------------------------------*/
850/*---------------------------------------------------------------------------*/
851
852#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_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_uchar_id
Identifiant HDF des caractères non-signés.
HType m_real2_id
Identifiant HDF pour les Real2.
HType m_char_id
Identifiant HDF des entiers signés.
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:120
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.
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