3#include <hipo4/reader.h>
4#include <iguana/algorithms/AlgorithmSequence.h>
6inline int TestMultithreading(
7 std::string
const command,
8 std::string
const algo_name,
9 std::vector<std::string>
const prerequisite_algos,
10 std::vector<std::string>
const bank_names,
11 std::string
const data_file,
13 int const num_threads,
14 std::string
const concurrency_model,
19 iguana::Logger log(
"test", verbose ? iguana::Logger::Level::trace : iguana::Logger::Level::info);
22 if(algo_name.empty() || bank_names.empty()) {
23 log.Error(
"need algorithm name and banks");
26 if(data_file.empty()) {
27 log.Error(
"need a data file for command {:?}", command);
32 if(!concurrency_model.empty())
36 std::optional<hipo::banklist::size_type> run_config_bank_idx{};
38 for(hipo::banklist::size_type idx = 0; idx < bank_names.size(); idx++) {
39 if(bank_names.at(idx) ==
"RUN::config") {
40 run_config_bank_idx = idx;
47 int const default_frame_size = 50;
48 int num_events_per_thread = (int) std::round((
double) num_events / num_threads);
49 int num_events_per_frame = num_events > 0 ? std::min(num_events_per_thread, default_frame_size) : default_frame_size;
50 int num_frames_per_thread = num_events > 0 ? (int) std::ceil((
double) num_events_per_thread / num_events_per_frame) : 0;
51 int num_events_actual = num_events_per_frame * num_frames_per_thread * num_threads;
52 log.Info(
"num_events_per_thread = {}", num_events_per_thread);
53 log.Info(
"num_events_per_frame = {}", num_events_per_frame );
54 log.Info(
"num_frames_per_thread = {}", num_frames_per_thread);
56 log.Info(
"=> will actually process num_events = {}", num_events_actual);
57 if(num_events != num_events_actual)
58 log.Warn(
"argument's num_events ({}) differs from the actual num_events that will be processed ({})",
59 num_events, num_events_actual);
61 log.Info(
"=> will actually process num_events = ALL OF THEM");
65 hipo::readerstream stream;
66 stream.open(data_file.c_str());
76 num_events_per_thread,
82 std::vector<hipo::event> events;
83 for(
int i = 0; i < num_events_per_frame; i++)
84 events.push_back(hipo::event());
88 for(
auto const& bank_name : bank_names)
89 banks.push_back(hipo::bank(stream.dictionary().getSchema(bank_name.c_str()),48));
93 for(
auto const& prerequisite_algo : prerequisite_algos)
94 seq.
Add(prerequisite_algo);
96 seq.
SetName(
"TEST thread " + std::to_string(order));
98 seq.
SetOption(algo_name,
"log", verbose ?
"trace" :
"info");
105 while(nProcessed < num_events_per_thread || num_events_per_thread == 0) {
110 for(
auto& event : events) {
111 if(event.getSize() > 16) {
116 for(
auto& bank : banks)
122 if(vary_run && run_config_bank_idx.has_value()) {
129 if(std::rand() % 10 == 0) {
130 auto runnum = banks[run_config_bank_idx.value()].getInt(
"run", 0);
131 runnum += (std::rand() % 2 == 0) ? 1000 : -1000;
132 runnum = std::max(runnum, 0);
133 banks[run_config_bank_idx.value()].putInt(
"run", 0, runnum);
135 else if(std::rand() % 10 == 1) {
136 banks[run_config_bank_idx.value()].putInt(
"run", 0, 1);
152 seq.
GetLog()->Info(
"nProcessed = {}", nProcessed);
157 stream.run(ftn, num_threads);
User-level class for running a sequence of algorithms.
void SetOption(std::string const &algo_name, std::string const &key, const OPTION_TYPE val)
void Stop() override
Finalize this algorithm after all events are processed.
void Run(hipo::banklist &banks) const override
Run this algorithm for an event.
void Add(std::string const &class_name, std::string const &instance_name="")
void Start(hipo::banklist &banks) override
Initialize this algorithm before any events are processed, with the intent to process banks
void SetName(std::string_view name)
void PrintSequence(Logger::Level level=Logger::info) const
std::unique_ptr< Logger > & GetLog()
GlobalParam< std::string > GlobalConcurrencyModel
The concurrency model, for running certain algorithms in a thread-safe way.