ATLAS Offline Software
Loading...
Searching...
No Matches
HdfTuple.cxx
Go to the documentation of this file.
1/*
2Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
3*/
5#include "HDF5Utils/common.h"
6
7#include "H5Cpp.h"
8
9#include <cassert>
10#include <set>
11#include <stdexcept>
12
13namespace H5Utils {
14
15 namespace {
16
17 H5::CompType build_type(const VariableFillers& fillers) {
19 std::set<std::string> inserted; // check for repeat names
20 H5::CompType type(fillers.size() * sizeof(data_buffer_t));
21 size_t dt_offset = 0;
22 for (const auto& filler: fillers) {
23 const std::string& name = filler->name();
24 if (inserted.count(name)) {
25 throw std::logic_error(name + " inserted twice");
26 }
27 inserted.insert(name);
28 type.insertMember(filler->name(), dt_offset, filler->get_type());
29 dt_offset += sizeof(data_buffer_t);
30 }
31 return type;
32 }
33
34 H5::DSetCreatPropList getChunckedDatasetParams(
35 const std::vector<hsize_t>& extent,
36 hsize_t batch_size) {
37 H5::DSetCreatPropList params;
38 std::vector<hsize_t> chunk_size{batch_size};
39 chunk_size.insert(chunk_size.end(), extent.begin(), extent.end());
40 params.setChunk(chunk_size.size(), chunk_size.data());
41 params.setDeflate(7);
42 return params;
43 }
44
45 }
46
47// _______________________________________________________________________
48// Xd writter
49//
50
51 WriterXd::WriterXd(H5::Group& group, const std::string& name,
52 VariableFillers fillers,
53 std::vector<hsize_t> max_length,
54 hsize_t batch_size):
55 m_type(build_type(fillers)),
56 m_max_length(max_length),
57 m_dim_stride(max_length),
58 m_batch_size(batch_size),
59 m_offset(0),
60 m_fillers(std::move(fillers))
61 {
62 if (batch_size < 1) {
63 throw std::logic_error("batch size must be > 0");
64 }
65 // create space
66 H5::DataSpace space = internal::getUnlimitedSpace(max_length);
67
68 // create params
69 H5::DSetCreatPropList params = getChunckedDatasetParams(
70 max_length, batch_size);
71
72 // calculate striding
73 m_dim_stride = internal::getStriding(std::move(max_length));
74
75 // create ds
76 internal::throwIfExists(name, group);
77 m_ds = group.createDataSet(name, internal::packed(m_type), space, params);
78 }
79
81 try {
82 flush();
83 } catch (H5::Exception& err) {
84 internal::printDestructorError(err.getDetailMsg());
85 } catch (std::exception& err) {
87 }
88 }
89
90 void WriterXd::fillWhileIncrementing(std::vector<size_t>& indices) {
91 if (buffer_size() == m_batch_size) {
92 flush();
93 }
94 indices.resize(m_max_length.size());
95
96 // build buffer and _then_ insert it so that exceptions don't leave
97 // the buffer in a weird state
98 std::vector<internal::data_buffer_t> temp;
99
100 std::fill(indices.begin(), indices.end(), 0);
101 for (size_t gidx = 0; gidx < m_dim_stride.front(); gidx++) {
102
103 // we might be able to make this more efficient and less cryptic
104 for (size_t iii = 0; iii < indices.size(); iii++) {
105 indices.at(iii) = (gidx % m_dim_stride.at(iii)) /
106 m_dim_stride.at(iii+1);
107 }
108
109 for (const auto& filler: m_fillers) {
110 temp.push_back(filler->get_buffer());
111 }
112 }
113 m_buffer.insert(m_buffer.end(), temp.begin(), temp.end());
114 }
116 if (buffer_size() == 0) return;
117 // extend the ds
118 std::vector<hsize_t> slab_dims{buffer_size()};
119 slab_dims.insert(slab_dims.end(), m_max_length.begin(), m_max_length.end());
120 std::vector<hsize_t> total_dims{buffer_size() + m_offset};
121 total_dims.insert(total_dims.end(), m_max_length.begin(), m_max_length.end());
122 m_ds.extend(total_dims.data());
123
124 // setup dataspaces
125 H5::DataSpace file_space = m_ds.getSpace();
126 H5::DataSpace mem_space(slab_dims.size(), slab_dims.data());
127 std::vector<hsize_t> offset_dims{m_offset};
128 offset_dims.resize(slab_dims.size(), 0);
129 file_space.selectHyperslab(H5S_SELECT_SET,
130 slab_dims.data(), offset_dims.data());
131
132 // write out
133 assert(static_cast<size_t>(file_space.getSelectNpoints())
134 == m_buffer.size() / m_fillers.size());
135 m_ds.write(m_buffer.data(), m_type, mem_space, file_space);
137 m_buffer.clear();
138 }
139
140 size_t WriterXd::index() const {
141 return m_offset + buffer_size();
142 }
143
144 hsize_t WriterXd::buffer_size() const {
145 size_t n_entries = m_buffer.size() / m_fillers.size();
146 assert(n_entries % m_dim_stride.front() == 0);
147 return n_entries / m_dim_stride.front();
148 }
149
150}
Variable filler arrays.
Definition HdfTuple.h:118
void fillWhileIncrementing(std::vector< size_t > &indices)
Definition HdfTuple.cxx:90
H5::CompType m_type
Definition HdfTuple.h:169
VariableFillers m_fillers
Definition HdfTuple.h:175
WriterXd(H5::Group &group, const std::string &name, VariableFillers fillers, std::vector< hsize_t > dataset_dimensions, hsize_t chunk_size=2048)
Definition HdfTuple.cxx:51
std::vector< hsize_t > m_dim_stride
Definition HdfTuple.h:171
hsize_t buffer_size() const
Definition HdfTuple.cxx:144
std::vector< hsize_t > m_max_length
Definition HdfTuple.h:170
H5::DataSet m_ds
Definition HdfTuple.h:176
size_t index() const
Definition HdfTuple.cxx:140
std::vector< internal::data_buffer_t > m_buffer
Definition HdfTuple.h:174
hsize_t m_batch_size
Definition HdfTuple.h:172
const hsize_t batch_size
Definition defaults.h:9
H5::CompType packed(H5::CompType in)
Definition common.cxx:16
H5::DSetCreatPropList getChunckedDatasetParams(const WriterConfiguration< N > &)
std::vector< hsize_t > getStriding(std::vector< hsize_t > max_length)
Definition common.cxx:38
H5::DataSpace getUnlimitedSpace(const std::vector< hsize_t > &max_length)
Definition common.cxx:31
void printDestructorError(const std::string &msg)
Definition common.cxx:24
void throwIfExists(const std::string &name, const H5::Group &in_group)
Definition common.cxx:46
HDF5 Tuple Writer.
Definition common.h:20
STL namespace.