ATLAS Offline Software
Toggle main menu visibility
Loading...
Searching...
No Matches
PhysicsAnalysis
AnalysisCommon
HDF5Utils
util
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
11
struct
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
};
27
using
consumer_t
=
H5Utils::Consumers<const out_t&>
;
28
29
// a non-movable member silently deletes a defaulted move constructor
30
static_assert
(
31
std::is_nothrow_move_constructible_v<H5Utils::Writer<0, consumer_t::input_type>>,
32
"H5Utils::Writer must stay nothrow move constructible"
);
33
34
consumer_t
getConsumers
() {
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
51
H5Utils::Compression
half =
H5Utils::Compression::HALF_PRECISION
;
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
60
std::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
84
template
<
size_t
N>
85
auto
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
}
94
template
<>
95
auto
nestOutputs<1>
(
int
offset,
size_t
length
) {
96
return
getOutputs
(offset,
length
, 0.5);
97
}
98
99
//-------------------------------------------------------------------------
100
// main routine
101
102
void
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
124
using
d1_t =
H5Utils::Writer<1, consumer_t::input_type>
;
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
136
using
d4_t =
H5Utils::Writer<4, consumer_t::input_type>
;
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]
152
int
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
}
length
double length(const pvec &v)
Definition
FPGATrackSimLLPDoubletHoughTransformTool.cxx:26
Writer.h
H5Utils::Consumers
Definition
Writer.h:131
H5Utils::Consumers::add
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
H5Utils::Writer
Writer.
Definition
Writer.h:352
main
int main()
Definition
hello.cxx:18
H5Utils::Compression
Compression
Definition
CompressionEnums.h:11
H5Utils::Compression::HALF_PRECISION
@ HALF_PRECISION
Definition
CompressionEnums.h:11
out_t
Definition
test-half-precision.cxx:12
out_t::itype
int itype
Definition
test-hdf5-writer.cxx:17
out_t::btype
bool btype
Definition
test-half-precision.cxx:15
out_t::ulltype
unsigned long long ulltype
Definition
test-hdf5-writer.cxx:24
out_t::lltype
long long lltype
Definition
test-hdf5-writer.cxx:19
out_t::ctype
char ctype
Definition
test-hdf5-writer.cxx:15
out_t::ltype
long ltype
Definition
test-hdf5-writer.cxx:18
out_t::ultype
unsigned long ultype
Definition
test-hdf5-writer.cxx:23
out_t::uctype
unsigned char uctype
Definition
test-hdf5-writer.cxx:20
out_t::uitype
unsigned int uitype
Definition
test-hdf5-writer.cxx:22
out_t::ftype
float ftype
Definition
test-half-precision.cxx:14
out_t::stype
short stype
Definition
test-hdf5-writer.cxx:16
out_t::dtype
double dtype
Definition
test-half-precision.cxx:13
out_t::ustype
unsigned short ustype
Definition
test-hdf5-writer.cxx:21
consumer_t
H5Utils::Consumers< const out_t & > consumer_t
Definition
test-half-precision.cxx:17
getConsumers
consumer_t getConsumers()
Definition
test-hdf5-writer.cxx:34
fill
void fill(H5::Group &out_file, size_t iterations)
Definition
test-hdf5-writer.cxx:102
ADD
#define ADD(NAME)
nestOutputs< 1 >
auto nestOutputs< 1 >(int offset, size_t length)
Definition
test-hdf5-writer.cxx:95
nestOutputs
auto nestOutputs(int offset, size_t length)
Definition
test-hdf5-writer.cxx:85
getOutputs
std::vector< out_t > getOutputs(int offset, size_t length, float factor)
Definition
test-hdf5-writer.cxx:60
Generated on
for ATLAS Offline Software by
1.17.0