ATLAS Offline Software
Loading...
Searching...
No Matches
test-hdf5-writer.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
3*/
4
5#include "HDF5Utils/Writer.h"
6
7#include <type_traits>
8
9//-------------------------------------------------------------------------
10// output data structure
11struct out_t
12{
13 double dtype;
14 float ftype;
15 char ctype;
16 short stype;
17 int itype;
18 long ltype;
19 long long lltype;
20 unsigned char uctype;
21 unsigned short ustype;
22 unsigned int uitype;
23 unsigned long ultype;
24 unsigned long long ulltype;
25 bool btype;
26};
28
29// a non-movable member silently deletes a defaulted move constructor
30static_assert(
31 std::is_nothrow_move_constructible_v<H5Utils::Writer<0, consumer_t::input_type>>,
32 "H5Utils::Writer must stay nothrow move constructible");
33
35 consumer_t consumers;
36#define ADD(NAME) consumers.add(#NAME, [](const out_t& o){ return o.NAME;}, 0)
37 ADD(ctype);
38 ADD(stype);
39 ADD(itype);
40 ADD(ltype);
41 ADD(lltype);
42 ADD(uctype);
43 ADD(ustype);
44 ADD(uitype);
45 ADD(ultype);
46 ADD(ulltype);
47 ADD(ftype);
48 ADD(dtype);
49 ADD(btype);
50#undef ADD
52 consumers.add("half" , [](const out_t& o) { return o.ftype; }, 0, half);
53 consumers.add("dhalf", [](const out_t& o) { return o.dtype; }, 0, half);
54 return consumers;
55}
56
57//-------------------------------------------------------------------------
58// outputs
59
60std::vector<out_t> getOutputs(int offset, size_t length, float factor) {
61 std::vector<out_t> outvec;
62 for (size_t n = 0; n < length; n++) {
63 out_t out;
64 long long int shifted = n + offset;
65 double factored = shifted*factor;
66 out.dtype = factored;
67 out.ftype = factored;
68 out.ctype = shifted;
69 out.stype = shifted;
70 out.itype = shifted;
71 out.ltype = shifted;
72 out.lltype = shifted;
73 out.uctype = shifted;
74 out.ustype = shifted;
75 out.uitype = shifted;
76 out.ultype = shifted;
77 out.ulltype = shifted;
78 out.btype = n % 2;
79 outvec.push_back(out);
80 }
81 return outvec;
82}
83
84template <size_t N>
85auto nestOutputs(int offset, size_t length) {
86 using ret_t = decltype(
87 nestOutputs<N-1>(std::declval<int>(),std::declval<size_t>()));
88 std::vector<ret_t> ret;
89 for (size_t n = 0; n < length; n++) {
90 ret.push_back(nestOutputs<N-1>(n + offset, length + 1));
91 }
92 return ret;
93}
94template<>
95auto nestOutputs<1>(int offset, size_t length) {
96 return getOutputs(offset, length, 0.5);
97}
98
99//-------------------------------------------------------------------------
100// main routine
101
102void fill(H5::Group& out_file, size_t iterations) {
103
104 const int deflate = 7;
105
106 // scalar output
107 using scalar_writer_t = H5Utils::Writer<0, consumer_t::input_type>;
108 scalar_writer_t::configuration_type scalar_config{};
109 scalar_config.name = "scalar";
110 scalar_config.deflate = deflate;
111 consumer_t consumers = getConsumers();
112 // the moved-from writer holds buffered rows and is destroyed while
113 // the new one still writes to the dataset
114 scalar_writer_t scalar = [&] {
115 scalar_writer_t source(out_file, consumers, scalar_config);
116 source.fill(getOutputs(1, 1, 0.5).at(0));
117 return scalar_writer_t(std::move(source));
118 }();
119 for (size_t n = 1; n < iterations; n++) {
120 scalar.fill(getOutputs(1 + n, 1, 0.5).at(0));
121 }
122
123 // 1d output
125 d1_t::configuration_type d1_config{};
126 d1_config.name = "1d";
127 d1_config.extent = {10};
128 d1_config.chunks = {5};
129 d1_config.deflate = deflate;
130 d1_t d1(out_file, consumers, d1_config);
131 for (size_t n = 0; n < iterations; n++) {
132 d1.fill(getOutputs(n, 10, 0.5));
133 }
134
135 // 4d output
137 d4_t::configuration_type d4_config;
138 d4_config.name = "4d";
139 d4_config.extent = {2,3,4,5};
140 d4_config.chunks = {1,2,1,2};
141 d4_config.deflate = deflate;
142 d4_t d4(out_file, consumers, d4_config);
143 for (size_t n = 0; n < iterations; n++) {
144 auto vals = nestOutputs<4>(n, 2);
145 // store some specific value to make sure we have the indexing
146 // right
147 vals.at(1).at(2).at(3).at(4).stype = 86;
148 d4.fill(std::move(vals));
149 }
150}
151//coverity[root_function]
152int main(int nargs, char* argv[]) {
153 H5::H5File out_file("output.h5", H5F_ACC_TRUNC);
154 size_t iterations = 1;
155 if (nargs > 2) {
156 return 1;
157 }
158 if (nargs > 1) iterations = std::atoi(argv[1]);
159 //coverity[TAINTED_SCALAR]
160 fill(out_file, iterations);
161 return 0;
162}
double length(const pvec &v)
void add(const std::string &name, const std::function< T(I)> &, const T &default_value=T(), Compression=Compression::STANDARD)
This should be the only method you need in this class.
Definition Writer.h:166
Writer.
Definition Writer.h:352
int main()
Definition hello.cxx:18
unsigned long long ulltype
long long lltype
unsigned long ultype
unsigned char uctype
unsigned int uitype
unsigned short ustype
H5Utils::Consumers< const out_t & > consumer_t
consumer_t getConsumers()
void fill(H5::Group &out_file, size_t iterations)
#define ADD(NAME)
auto nestOutputs< 1 >(int offset, size_t length)
auto nestOutputs(int offset, size_t length)
std::vector< out_t > getOutputs(int offset, size_t length, float factor)