ATLAS Offline Software
Loading...
Searching...
No Matches
test-hdf5-threads.cxx File Reference
#include "HDF5Utils/Writer.h"
#include <format>
#include <iostream>
#include <thread>
#include <chrono>
Include dependency graph for test-hdf5-threads.cxx:

Go to the source code of this file.

Classes

struct  out_t

Typedefs

using consumer_t = H5Utils::Consumers<const out_t&>

Functions

int main (int nargs, char *argv[])

Typedef Documentation

◆ consumer_t

Definition at line 17 of file test-hdf5-threads.cxx.

Function Documentation

◆ main()

int main ( int nargs,
char * argv[] )

Definition at line 20 of file test-hdf5-threads.cxx.

20 {
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
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}
std::shared_ptr< HepMC3::Writer > writer
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
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
size_t thread_number
size_t event_number
H5Utils::Consumers< const out_t & > consumer_t