TRIQS/h5 1.3.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
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
41 struct dataset_info {
44
47
50
52 [[nodiscard]] int rank() const { return static_cast<int>(lengths.size()); }
53 };
54
91 struct hyperslab {
94
97
100
103
113 hyperslab(int rank, bool is_complex)
114 : offset(rank + is_complex, 0), stride(rank + is_complex, 1), count(rank + is_complex, 0), block(rank + is_complex, 1) {
115 if (is_complex) {
116 stride[rank] = 1;
117 count[rank] = 2;
118 }
119 }
120
122 hyperslab() = default;
123
125 [[nodiscard]] int rank() const { return static_cast<int>(count.size()); }
126
128 [[nodiscard]] bool empty() const { return count.empty(); }
129
131 [[nodiscard]] v_t shape() const {
132 v_t shape(rank());
133 std::transform(count.begin(), count.end(), block.begin(), shape.begin(), std::multiplies<>());
134 return shape;
135 }
136
138 [[nodiscard]] auto size() const {
139 auto sh = shape();
140 return std::accumulate(sh.begin(), sh.end(), (hsize_t)1, std::multiplies<>());
141 }
142 };
143
155 struct array_view {
158
160 void *start;
161
164
167
170
186
188 [[nodiscard]] int rank() const { return slab.rank(); }
189 };
190
213 std::pair<v_t, v_t> get_parent_shape_and_h5_strides(long const *np_strides, int rank, long view_size);
214
221 dataset_info get_dataset_info(dataset ds);
222
230 dataset_info get_dataset_info(group g, std::string const &name);
231
242 void write(group g, std::string const &name, array_view const &v, bool compress);
243
255 void write_slice(group g, std::string const &name, array_view const &v, hyperslab sl);
256
264 void write_attribute(object obj, std::string const &name, array_view v);
265
274 void read(group g, std::string const &name, array_view v, hyperslab sl = {});
275
283 void read_attribute(object obj, std::string const &name, array_view v);
284
287} // namespace h5::array_interface
288
289#endif // LIBH5_ARRAY_INTERFACE_HPP
A handle to an HDF5 group.
Definition group.hpp:44
A generic handle for HDF5 objects.
Definition object.hpp:49
Provides a handle to an HDF5 group and various methods to simplify the creation/opening of subgroups,...
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 view_size)
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.
Struct representing a view on an n-dimensional array/dataspace.
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).