openPMD-api
 
Loading...
Searching...
No Matches
HDF5IOHandlerImpl.hpp
1/* Copyright 2017-2025 Fabian Koller, Axel Huebl, Franz Poeschel, Jean Luca Bez,
2 * Junmin Gu, Ulrik Guenther
3 *
4 * This file is part of openPMD-api.
5 *
6 * openPMD-api is free software: you can redistribute it and/or modify
7 * it under the terms of of either the GNU General Public License or
8 * the GNU Lesser General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * openPMD-api is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License and the GNU Lesser General Public License
16 * for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * and the GNU Lesser General Public License along with openPMD-api.
20 * If not, see <http://www.gnu.org/licenses/>.
21 */
22#pragma once
23
24#include "openPMD/IO/AbstractIOHandler.hpp"
25#include "openPMD/config.hpp"
26#if openPMD_HAVE_HDF5
27#include "openPMD/IO/AbstractIOHandlerImpl.hpp"
28
29#include "openPMD/auxiliary/JSON_internal.hpp"
30
31#include <hdf5.h>
32#include <optional>
33#include <unordered_map>
34#include <unordered_set>
35#endif
36
37namespace openPMD
38{
39#if openPMD_HAVE_HDF5
40class HDF5IOHandlerImpl : public AbstractIOHandlerImpl
41{
42 friend class HDF5IOHandler;
43 friend class ParallelHDF5IOHandler;
44
45public:
46 HDF5IOHandlerImpl(AbstractIOHandler *, bool do_warn_unused_params = true);
47 ~HDF5IOHandlerImpl() override;
48
49 void
52 void
54 void createDataset(
56 void extendDataset(
58 void availableChunks(
61 void
63 void openPath(Writable *, Parameter<Operation::OPEN_PATH> const &) override;
65 void
67 void
69 void deleteDataset(
71 void deleteAttribute(
72 Writable *, Parameter<Operation::DELETE_ATT> const &) override;
73 void
75 void writeAttribute(
76 Writable *, Parameter<Operation::WRITE_ATT> const &) override;
80 void
83 void
85 void touch(Writable *, Parameter<Operation::TOUCH> const &) override;
86
87 std::unordered_map<Writable *, std::string> m_fileNames;
88 std::unordered_map<std::string, hid_t> m_fileNamesWithID;
89
90 std::unordered_set<hid_t> m_openFileIDs;
91
92 hid_t m_datasetTransferProperty;
93 hid_t m_fileAccessProperty;
94 hid_t m_fileCreateProperty;
95
96 hbool_t m_hdf5_collective_metadata = 1;
97
98 // h5py compatible types for bool and complex
99 hid_t m_H5T_BOOL_ENUM;
100 hid_t m_H5T_CFLOAT;
101 hid_t m_H5T_CDOUBLE;
102 hid_t m_H5T_CLONG_DOUBLE;
103 /* See https://github.com/openPMD/openPMD-api/issues/1363
104 * long double values written on AMD64 architectures cannot be read on
105 * ARM64/PPC64 architectures without doing some special tricks.
106 * We generally don't implement custom conversions, but instead pass-through
107 * the cross-platform support of HDF5.
108 * But this case is common and important enough to warrant a custom
109 * workaround.
110 */
111 hid_t m_H5T_LONG_DOUBLE_80_LE;
112 hid_t m_H5T_CLONG_DOUBLE_80_LE;
113
114 std::future<void> flush(internal::ParsedFlushParams &);
115
116protected:
117#if openPMD_HAVE_MPI
118 /*
119 * Not defined in ParallelHDF5IOHandlerImpl, so we don't have to write
120 * some methods twice.
121 */
122 std::optional<MPI_Comm> m_communicator;
123#endif
124
125 json::TracingJSON m_config;
126 nlohmann::json m_global_dataset_config;
127 nlohmann::json m_global_flush_config;
128
129private:
130 struct File
131 {
132 std::string name;
133 hid_t id;
134 };
135 std::optional<File> getFile(Writable *);
136 File
137 requireFile(std::string const &functionName, Writable *, bool checkParent);
138}; // HDF5IOHandlerImpl
139#else
141{}; // HDF5IOHandlerImpl
142#endif
143
144} // namespace openPMD
Interface for communicating between logical and physically persistent data.
Definition AbstractIOHandler.hpp:206
Definition HDF5IOHandlerImpl.hpp:41
void createDataset(Writable *, Parameter< Operation::CREATE_DATASET > const &) override
Create a new dataset of given type, extent and storage properties.
Definition HDF5IOHandler.cpp:825
void deleteAttribute(Writable *, Parameter< Operation::DELETE_ATT > const &) override
Delete an existing attribute.
Definition HDF5IOHandler.cpp:1797
void listPaths(Writable *, Parameter< Operation::LIST_PATHS > &) override
List all paths/sub-groups inside a group, non-recursively.
Definition HDF5IOHandler.cpp:3127
void openFile(Writable *, Parameter< Operation::OPEN_FILE > &) override
Open an existing file assuming it conforms to openPMD.
Definition HDF5IOHandler.cpp:1247
void closeFile(Writable *, Parameter< Operation::CLOSE_FILE > const &) override
Close the file corresponding with the writable and release file handles.
Definition HDF5IOHandler.cpp:1300
void deleteDataset(Writable *, Parameter< Operation::DELETE_DATASET > const &) override
Delete an existing dataset.
Definition HDF5IOHandler.cpp:1745
void extendDataset(Writable *, Parameter< Operation::EXTEND_DATASET > const &) override
Increase the extent of an existing dataset.
Definition HDF5IOHandler.cpp:1106
void availableChunks(Writable *, Parameter< Operation::AVAILABLE_CHUNKS > &) override
Report chunks that are available for loading from the dataset represented by this writable.
Definition HDF5IOHandler.cpp:1177
void deregister(Writable *, Parameter< Operation::DEREGISTER > const &) override
Notify the backend that the Writable has been / will be deallocated.
Definition HDF5IOHandler.cpp:3315
void readDataset(Writable *, Parameter< Operation::READ_DATASET > &) override
Read a chunk of data from an existing dataset.
Definition HDF5IOHandler.cpp:2321
void writeAttribute(Writable *, Parameter< Operation::WRITE_ATT > const &) override
Create a single attribute and fill the value, possibly overwriting an existing attribute.
Definition HDF5IOHandler.cpp:1979
void listDatasets(Writable *, Parameter< Operation::LIST_DATASETS > &) override
List all datasets inside a group, non-recursively.
Definition HDF5IOHandler.cpp:3182
void deleteFile(Writable *, Parameter< Operation::DELETE_FILE > const &) override
Delete an existing file from physical storage.
Definition HDF5IOHandler.cpp:1658
void createFile(Writable *, Parameter< Operation::CREATE_FILE > const &) override
Create a new file in physical storage, possibly overriding an existing file.
Definition HDF5IOHandler.cpp:278
void touch(Writable *, Parameter< Operation::TOUCH > const &) override
Treat this writable's file as open/active/dirty.
Definition HDF5IOHandler.cpp:3321
void checkFile(Writable *, Parameter< Operation::CHECK_FILE > &) override
Check if the file specified by the parameter is already present on disk.
Definition HDF5IOHandler.cpp:349
void listAttributes(Writable *, Parameter< Operation::LIST_ATTS > &) override
List all attributes associated with an object.
Definition HDF5IOHandler.cpp:3238
void openDataset(Writable *, Parameter< Operation::OPEN_DATASET > &) override
Open an existing dataset and determine its datatype and extent.
Definition HDF5IOHandler.cpp:1406
void deletePath(Writable *, Parameter< Operation::DELETE_PATH > const &) override
Delete all objects within an existing path.
Definition HDF5IOHandler.cpp:1693
void readAttribute(Writable *, Parameter< Operation::READ_ATT > &) override
Read the value of an existing attribute.
Definition HDF5IOHandler.cpp:2488
void createPath(Writable *, Parameter< Operation::CREATE_PATH > const &) override
Create all necessary groups for a path, possibly recursively.
Definition HDF5IOHandler.cpp:383
void openPath(Writable *, Parameter< Operation::OPEN_PATH > const &) override
Open all contained groups in a path, possibly recursively.
Definition HDF5IOHandler.cpp:1318
void writeDataset(Writable *, Parameter< Operation::WRITE_DATASET > &) override
Write a chunk of data into an existing dataset.
Definition HDF5IOHandler.cpp:1832
Layer to mirror structure of logical data and persistent data in file.
Definition Writable.hpp:76
Extend nlohmann::json with tracing of which keys have been accessed by operator[]().
Definition JSON_internal.hpp:69
Public definitions of openPMD-api.
Definition Date.cpp:29
Typesafe description of all required arguments for a specified Operation.
Definition IOTask.hpp:148
Definition FlushParametersInternal.hpp:32