ATLAS Offline Software
Toggle main menu visibility
Loading...
Searching...
No Matches
PhysicsAnalysis
AnalysisCommon
HDF5Utils
util
test-hdf5-threads.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 <format>
8
#include <iostream>
9
#include <thread>
10
#include <chrono>
11
12
struct
out_t
13
{
14
size_t
thread_number
;
15
size_t
event_number
;
16
};
17
using
consumer_t
=
H5Utils::Consumers<const out_t&>
;
18
19
//coverity[UNCAUGHT_EXCEPT]
20
int
main
(
int
nargs,
char
* argv[]) {
21
H5::H5File h5_file(
"mt_output.h5"
, H5F_ACC_TRUNC);
22
size_t
n_threads = 5;
23
if
(nargs >= 2) {
24
n_threads = std::stoi(argv[1]);
25
}
26
size_t
n_events = 1;
27
if
(nargs >= 3) {
28
n_events = std::stoi(argv[2]);
29
}
30
size_t
ms_sleep = 0;
31
if
(nargs >= 4) {
32
ms_sleep = std::stoi(argv[3]);
33
}
34
if
(nargs > 4) {
35
std::cerr << std::format(
36
"usage: {} [n_threads] [n_events] [sleep (ms)]\n"
, argv[0]);
37
return
1;
38
}
39
40
// define outputs (consumers)
41
consumer_t
consumers;
42
consumers.
add
(
"thread"
, [ms_sleep](
const
out_t
& o){
43
std::cout <<
"writing event "
<< o.
event_number
<<
" to thread "
<< o.
thread_number
<<
"\n"
;
44
if
(ms_sleep) {
45
std::this_thread::sleep_for(std::chrono::milliseconds(ms_sleep));
46
}
47
return
o.
thread_number
;
48
});
49
consumers.
add
(
"event"
, [](
const
out_t
& o) {
50
return
o.
event_number
;
51
});
52
53
// set the batch size to something small to ensure some flushing
54
H5Utils::WriterConfiguration<0>
config;
55
config.batch_size = n_events > 10 ? n_events / 10 : 1;
56
config.name =
"out"
;
57
std::cout << std::format(
58
"writing {} events each to {} threads, in batches of {}\n"
,
59
n_events,
60
n_threads,
61
*config.batch_size);
62
63
auto
writer
=
H5Utils::makeWriter<0>
(h5_file, consumers, config);
64
65
std::vector<std::thread> threads;
66
threads.reserve(n_threads);
67
for
(
size_t
thread_n = 0; thread_n < n_threads; thread_n++) {
68
threads.emplace_back(
69
[thread_n, n_events, &
writer
](){
70
std::cout <<
"starting thread "
<< thread_n <<
"\n"
;
71
for
(
size_t
event_n = 0; event_n < n_events; event_n++) {
72
writer
.fill(
out_t
{thread_n, event_n});
73
}
74
});
75
}
76
for
(
auto
& thread: threads) thread.join();
77
return
0;
78
}
writer
std::shared_ptr< HepMC3::Writer > writer
Writer.h
H5Utils::Consumers
Definition
Writer.h:130
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:165
main
int main()
Definition
hello.cxx:18
H5Utils::makeWriter
Writer< N, I > makeWriter(H5::Group &group, const std::string &name, const Consumers< I > &consumers, const std::array< hsize_t, N > &extent=internal::uniform< N >(5), hsize_t batch_size=defaults::batch_size)
makeWriter
Definition
Writer.h:550
H5Utils::WriterConfiguration
Definition
WriterConfiguration.h:22
out_t
Definition
test-half-precision.cxx:12
out_t::thread_number
size_t thread_number
Definition
test-hdf5-threads.cxx:14
out_t::event_number
size_t event_number
Definition
test-hdf5-threads.cxx:15
consumer_t
H5Utils::Consumers< const out_t & > consumer_t
Definition
test-half-precision.cxx:17
Generated on
for ATLAS Offline Software by
1.17.0