TRIQS/h5 2.0.0
C++ interface to HDF5
Loading...
Searching...
No Matches
array_interface.hpp
Go to the documentation of this file.
1// Copyright (c) 2019-2024 Simons Foundation
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0.txt
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14//
15// Authors: Thomas Hahn, Olivier Parcollet, Nils Wentzell
16
21
22#ifndef LIBH5_ARRAY_INTERFACE_HPP
23#define LIBH5_ARRAY_INTERFACE_HPP
24
25#include "./group.hpp"
26#include "./object.hpp"
27
28#include <algorithm>
29#include <numeric>
30#include <string>
31#include <utility>
32
33namespace h5::array_interface {
34
39
41 struct dataset_info {
44
47
50
52 [[nodiscard]] int rank() const { return static_cast<int>(lengths.size()); }
53 };
54
90 struct hyperslab {
93
96
99
102
113 hyperslab(int rank, bool has_cplx_trailing_dim)
114 : offset(rank + has_cplx_trailing_dim, 0),
115 stride(rank + has_cplx_trailing_dim, 1),
116 count(rank + has_cplx_trailing_dim, 0),
117 block(rank + has_cplx_trailing_dim, 1) {
118 if (has_cplx_trailing_dim) {
119 stride[rank] = 1;
120 count[rank] = 2;
121 }
122 }
123
125 hyperslab() = default;
126
128 [[nodiscard]] int rank() const { return static_cast<int>(count.size()); }
129
131 [[nodiscard]] bool empty() const { return count.empty(); }
132
134 [[nodiscard]] v_t shape() const {
135 v_t shape(rank());
136 std::ranges::transform(count, block, shape.begin(), std::multiplies<>());
137 return shape;
138 }
139
141 [[nodiscard]] auto size() const {
142 auto sh = shape();
143 return std::accumulate(sh.begin(), sh.end(), (hsize_t)1, std::multiplies<>());
144 }
145 };
146
159 struct array_view {
162
164 void *start;
165
168
171
175
196
198 [[nodiscard]] int rank() const { return slab.rank(); }
199 };
200
223 std::pair<v_t, v_t> get_parent_shape_and_h5_strides(long const *np_strides, int rank, long const *view_shape);
224
231 dataset_info get_dataset_info(dataset ds);
232
240 dataset_info get_dataset_info(group g, std::string const &name);
241
252 void write(group g, std::string const &name, array_view const &v, bool compress);
253
265 void write_slice(group g, std::string const &name, array_view const &v, hyperslab sl);
266
274 void write_attribute(object obj, std::string const &name, array_view v);
275
284 void read(group g, std::string const &name, array_view v, hyperslab sl = {});
285
293 void read_attribute(object obj, std::string const &name, array_view v);
294
296
297} // namespace h5::array_interface
298
299#endif // LIBH5_ARRAY_INTERFACE_HPP
A handle to an HDF5 group.
Definition group.hpp:44
Provides a handle to an HDF5 group and various methods to simplify the creation/opening of subgroups,...
object datatype
Type alias for an HDF5 datatype.
Definition object.hpp:123
object dataset
Type alias for an HDF5 dataset.
Definition object.hpp:120
void read_attribute(object obj, std::string const &name, array_view v)
Read from an HDF5 attribute into an array view.
void write_slice(group g, std::string const &name, array_view const &v, hyperslab sl)
Write an array view to a selected hyperslab of an existing HDF5 dataset.
void read(group g, std::string const &name, array_view v, hyperslab sl)
Read a given hyperslab from an HDF5 dataset into an array view.
void write_attribute(object obj, std::string const &name, array_view v)
Write an array view to an HDF5 attribute.
dataset_info get_dataset_info(dataset ds)
Retrieve the shape and the h5::datatype from a dataset.
void write(group g, std::string const &name, array_view const &v, bool compress)
Write an array view to an HDF5 dataset.
std::pair< v_t, v_t > get_parent_shape_and_h5_strides(long const *np_strides, int rank, long const *view_shape)
Given a view on an n-dimensional array (dataspace) by specifying its numpy/nda-style strides and its ...
std::vector< hsize_t > v_t
Vector of h5::hsize_t used throughout the h5 library.
Definition utils.hpp:59
unsigned long long hsize_t
Size type used in HDF5.
Definition utils.hpp:55
Provides a generic handle for HDF5 objects.
void * start
Pointer to the data of the array.
v_t parent_shape
Shape of the (contiguous) parent array.
datatype ty
h5::datatype stored in the array.
int rank() const
Get the rank of the view (including the possible added imaginary dimension).
array_view(datatype ty, void *start, int rank, bool has_cplx_trailing_dim)
Construct a new empty array view.
hyperslab slab
h5::array_interface::hyperslab specifying the selection of the view.
Simple struct to store basic information about an HDF5 dataset.
int rank() const
Get the rank of the dataspace in the dataset.
bool has_complex_attribute
Whether the stored values are complex.
v_t lengths
Shape of the dataspace in the dataset.
datatype ty
h5::datatype stored in the dataset.
Struct representing an HDF5 hyperslab.
auto size() const
Get the total number of elements in the hyperslab.
v_t block
Shape of a single block selected from the dataspace.
bool empty() const
Check whether the hyperslab is empty (has been initialized).
hyperslab(int rank, bool has_cplx_trailing_dim)
Construct a new empty hyperslab for a dataspace of a given rank.
v_t stride
Stride in each dimension (in the HDF5 sense).
v_t offset
Index offset for each dimension.
v_t count
Number of elements or blocks to select along each dimension.
hyperslab()=default
Default constructor leaves the hyperslab empty (uninitialized).
v_t shape() const
Get the shape of the selected hyperslab.
int rank() const
Get the rank of the hyperslab (including the possible added imaginary dimension).