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())
33 iguana::GlobalConcurrencyModel = concurrency_model;
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);
62 log.Info(
"=> will actually process num_events = ALL OF THEM");
66 hipo::readerstream stream;
67 stream.open(data_file.c_str());
76 num_events_per_thread,
78 run_config_bank_idx](
int order) {
80 std::vector<hipo::event> events;
81 for(
int i = 0; i < num_events_per_frame; i++)
82 events.push_back(hipo::event());
86 for(
auto const& bank_name : bank_names)
87 banks.push_back(hipo::bank(stream.dictionary().getSchema(bank_name.c_str()), 48));
91 for(
auto const& prerequisite_algo : prerequisite_algos)
92 seq.
Add(prerequisite_algo);
94 seq.
SetName(
"TEST thread " + std::to_string(order));
96 seq.
SetOption(algo_name,
"log", verbose ?
"trace" :
"info");
103 while(nProcessed < num_events_per_thread || num_events_per_thread == 0) {
108 for(
auto& event : events) {
109 if(event.getSize() > 16) {
114 for(
auto& bank : banks)
120 if(vary_run && run_config_bank_idx.has_value()) {
127 if(std::rand() % 10 == 0) {
128 auto runnum = banks[run_config_bank_idx.value()].getInt(
"run", 0);
129 runnum += (std::rand() % 2 == 0) ? 1000 : -1000;
130 runnum = std::max(runnum, 0);
131 banks[run_config_bank_idx.value()].putInt(
"run", 0, runnum);
133 else if(std::rand() % 10 == 1) {
134 banks[run_config_bank_idx.value()].putInt(
"run", 0, 1);
150 seq.
GetLog()->Info(
"nProcessed = {}", nProcessed);
155 stream.run(ftn, num_threads);
Algorithm: An algorithm that can run a sequence of algorithms
void Stop() override
Finalize this algorithm after all events are processed.
void Add(std::string const &algo_class_name, std::string const &algo_instance_name="")
void SetOption(std::string const &algo_instance_name, std::string const &key, const OPTION_TYPE val)
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
bool Run(hipo::banklist &banks) const override
Run Function: Process an event's hipo::banklist
std::unique_ptr< Logger > & GetLog()