ritsuko
Helper utilities for ArtifactDB C++ code
Loading...
Searching...
No Matches
Stream1dNumericDataset.hpp
Go to the documentation of this file.
1#ifndef RITSUKO_HDF5_STREAM_1D_NUMERIC_DATASET_HPP
2#define RITSUKO_HDF5_STREAM_1D_NUMERIC_DATASET_HPP
3
4#include <vector>
5#include <stdexcept>
6#include <cassert>
7#include <limits>
8#include <type_traits>
9#include <algorithm>
10
11#include "H5Cpp.h"
13
14#include "../utils.hpp"
16
22namespace ritsuko {
23
24namespace hdf5 {
25
36
46template<typename Type_, class DataSetPointer_ = const H5::DataSet*>
48public:
57 Stream1dNumericDataset(DataSetPointer_ data_ptr, hsize_t length, const Stream1dNumericDatasetOptions& options) :
58 my_data_ptr(std::move(data_ptr)),
59 my_full_length(length),
60 my_block_size([&]{
61 assert(my_data_ptr->getSpace().getSimpleExtentNdims() == 1);
62 hsize_t output;
63 const auto& plist = my_data_ptr->getCreatePlist();
64 if (plist.getLayout() == H5D_CHUNKED) {
65 plist.getChunk(1, &output);
66 } else {
67 output = std::min(length, options.contiguous_chunk_size);
68 }
69 return output;
70 }()),
71 my_mspace(1, &my_block_size),
72 my_fspace(1, &my_full_length)
73 {
74 assert([&]{
75 const auto cls = my_data_ptr->getDataType().getClass();
76 return cls == H5T_INTEGER || cls == H5T_FLOAT;
77 }());
78 }
79
80public:
84 hsize_t chunk_size() const {
85 return my_block_size;
86 }
87
98 hsize_t load(Type_* buffer) {
99 static_assert(std::is_arithmetic<Type_>::value);
100
101 my_last_loaded += my_available;
102 my_available = std::min(my_full_length - my_last_loaded, my_block_size);
103 if (my_available == 0) {
104 return 0;
105 }
106
107 constexpr hsize_t zero = 0;
108 my_mspace.selectHyperslab(H5S_SELECT_SET, &my_available, &zero);
109 my_fspace.selectHyperslab(H5S_SELECT_SET, &my_available, &my_last_loaded);
110 my_data_ptr->read(buffer, as_numeric_datatype<Type_>(), my_mspace, my_fspace);
111 return my_available;
112 }
113
121 hsize_t start() const {
122 return my_last_loaded;
123 }
124
125private:
126 DataSetPointer_ my_data_ptr;
127 hsize_t my_full_length, my_block_size;
128 H5::DataSpace my_mspace;
129 H5::DataSpace my_fspace;
130 hsize_t my_last_loaded = 0;
131 hsize_t my_available = 0;
132
133 static_assert(std::is_base_of<H5::DataSet, I<decltype(*my_data_ptr)> >::value);
134};
135
136}
137
138}
139
140#endif
Choose a HDF5 datatype.
Stream a 1-dimensional HDF5 numeric dataset into memory.
Definition Stream1dNumericDataset.hpp:47
Stream1dNumericDataset(DataSetPointer_ data_ptr, hsize_t length, const Stream1dNumericDatasetOptions &options)
Definition Stream1dNumericDataset.hpp:57
hsize_t start() const
Definition Stream1dNumericDataset.hpp:121
hsize_t chunk_size() const
Definition Stream1dNumericDataset.hpp:84
hsize_t load(Type_ *buffer)
Definition Stream1dNumericDataset.hpp:98
const H5::PredType & as_numeric_datatype()
Definition as_numeric_datatype.hpp:27
Helper functions for ArtifactDB parsing and validation.
Definition Pointer.hpp:22
constexpr Dest_ cap(Value_ x)
Options for Stream1dNumericDataset.
Definition Stream1dNumericDataset.hpp:29
hsize_t contiguous_chunk_size
Definition Stream1dNumericDataset.hpp:34