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
10#include "H5Cpp.h"
11#include "sanisizer/sanisizer.hpp"
12
13#include "get_name.hpp"
15
21namespace ritsuko {
22
23namespace hdf5 {
24
34template<typename Type_, class DataSetPointer_ = const H5::DataSet*>
36public:
45 Stream1dNumericDataset(DataSetPointer_ data_ptr, hsize_t length) :
46 my_data_ptr(std::move(data_ptr)),
47 my_full_length(length),
48 my_block_size([&]{
49 assert(my_data_ptr->getSpace().getSimpleExtentNdims() == 1);
50 hsize_t output;
51 const auto& plist = my_data_ptr->getCreatePlist();
52 if (plist.getLayout() == H5D_CHUNKED) {
53 plist.getChunk(1, &output);
54 } else {
55 // Hard-coding the mock chunk size for contiguous datasets,
56 // not worth complicating the constructor with an extra argument.
57 output = sanisizer::min(length, 10000);
58 }
59 return output;
60 }()),
61 my_mspace(1, &my_block_size),
62 my_fspace(1, &my_full_length)
63 {
64 assert([&]{
65 const auto cls = my_data_ptr->getDataType().getClass();
66 return cls == H5T_INTEGER || cls == H5T_FLOAT;
67 }());
68 }
69
70public:
74 hsize_t chunk_size() const {
75 return my_block_size;
76 }
77
88 hsize_t load(Type_* buffer) {
89 static_assert(std::is_arithmetic<Type_>::value);
90
91 my_last_loaded += my_available;
92 my_available = std::min(my_full_length - my_last_loaded, my_block_size);
93 if (my_available == 0) {
94 return 0;
95 }
96
97 constexpr hsize_t zero = 0;
98 my_mspace.selectHyperslab(H5S_SELECT_SET, &my_available, &zero);
99 my_fspace.selectHyperslab(H5S_SELECT_SET, &my_available, &my_last_loaded);
100 my_data_ptr->read(buffer, as_numeric_datatype<Type_>(), my_mspace, my_fspace);
101 return my_available;
102 }
103
111 hsize_t start() const {
112 return my_last_loaded;
113 }
114
115private:
116 DataSetPointer_ my_data_ptr;
117 hsize_t my_full_length, my_block_size;
118 H5::DataSpace my_mspace;
119 H5::DataSpace my_fspace;
120 hsize_t my_last_loaded = 0;
121 hsize_t my_available = 0;
122
123 static_assert(std::is_base_of<H5::DataSet, I<decltype(*my_data_ptr)> >::value);
124};
125
126}
127
128}
129
130#endif
Choose a HDF5 datatype.
Stream a 1-dimensional HDF5 numeric dataset into memory.
Definition Stream1dNumericDataset.hpp:35
hsize_t start() const
Definition Stream1dNumericDataset.hpp:111
Stream1dNumericDataset(DataSetPointer_ data_ptr, hsize_t length)
Definition Stream1dNumericDataset.hpp:45
hsize_t chunk_size() const
Definition Stream1dNumericDataset.hpp:74
hsize_t load(Type_ *buffer)
Definition Stream1dNumericDataset.hpp:88
Get the name of a HDF5 object.
const H5::PredType & as_numeric_datatype()
Definition as_numeric_datatype.hpp:27
Helper functions for ArtifactDB parsing and validation.
Definition Pointer.hpp:21