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
112 hyperslab(int rank, bool is_complex)
113 : offset(rank + is_complex, 0), stride(rank + is_complex, 1), count(rank + is_complex, 0), block(rank + is_complex, 1) {
114 if (is_complex) {
115 stride[rank] = 1;
116 count[rank] = 2;
117 }
118 }
119
121 hyperslab() = default;
122
124 [[nodiscard]] int rank() const { return static_cast<int>(count.size()); }
125
127 [[nodiscard]] bool empty() const { return count.empty(); }
128
130 [[nodiscard]] v_t shape() const {
131 v_t shape(rank());
132 std::ranges::transform(count, block, shape.begin(), std::multiplies<>());
133 return shape;
134 }
135
137 [[nodiscard]] auto size() const {
138 auto sh = shape();
139 return std::accumulate(sh.begin(), sh.end(), (hsize_t)1, std::multiplies<>());
140 }
141 };
142
154 struct array_view {
157
159 void *start;
160
163
166
169
185
187 [[nodiscard]] int rank() const { return slab.rank(); }
188 };
189
212 std::pair<v_t, v_t> get_parent_shape_and_h5_strides(long const *np_strides, int rank, long const *view_shape);
213
220 dataset_info get_dataset_info(dataset ds);
221
229 dataset_info get_dataset_info(group g, std::string const &name);
230
241 void write(group g, std::string const &name, array_view const &v, bool compress);
242
254 void write_slice(group g, std::string const &name, array_view const &v, hyperslab sl);
255
263 void write_attribute(object obj, std::string const &name, array_view v);
264
273 void read(group g, std::string const &name, array_view v, hyperslab sl = {});
274
282 void read_attribute(object obj, std::string const &name, array_view v);
283
285
286} // namespace h5::array_interface
287
288#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.
array_view(datatype ty, void *start, int rank, bool is_complex)
Construct a new empty array view.
v_t parent_shape
Shape of the (contiguous) parent array.
datatype ty
h5::datatype stored in the array.
bool is_complex
Whether the data is complex valued.
int rank() const
Get the rank of the view (including the possible added imaginary dimension).
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).
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).
hyperslab(int rank, bool is_complex)
Construct a new empty hyperslab for a dataspace of a given rank.
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).