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,
16 std::string
const log_level)
20 log.SetLevel(log_level);
23 if(algo_name.empty() || bank_names.empty()) {
24 log.Error(
"need algorithm name and banks");
27 if(data_file.empty()) {
28 log.Error(
"need a data file for command {:?}", command);
33 if(!concurrency_model.empty())
34 iguana::GlobalConcurrencyModel = concurrency_model;
37 std::optional<hipo::banklist::size_type> run_config_bank_idx{};
39 for(hipo::banklist::size_type idx = 0; idx < bank_names.size(); idx++) {
40 if(bank_names.at(idx) ==
"RUN::config") {
41 run_config_bank_idx = idx;
48 int const default_frame_size = 50;
49 int num_events_per_thread = (int)std::round((
double)num_events / num_threads);
50 int num_events_per_frame = num_events > 0 ? std::min(num_events_per_thread, default_frame_size) : default_frame_size;
51 int num_frames_per_thread = num_events > 0 ? (int)std::ceil((
double)num_events_per_thread / num_events_per_frame) : 0;
52 int num_events_actual = num_events_per_frame * num_frames_per_thread * num_threads;
53 log.Info(
"num_events_per_thread = {}", num_events_per_thread);
54 log.Info(
"num_events_per_frame = {}", num_events_per_frame);
55 log.Info(
"num_frames_per_thread = {}", num_frames_per_thread);
57 log.Info(
"=> will actually process num_events = {}", num_events_actual);
58 if(num_events != num_events_actual)
59 log.Warn(
"argument's num_events ({}) differs from the actual num_events that will be processed ({})",
60 num_events, num_events_actual);
63 log.Info(
"=> will actually process num_events = ALL OF THEM");
67 hipo::readerstream stream;
68 stream.open(data_file.c_str());
77 num_events_per_thread,
79 run_config_bank_idx](
int order) {
81 std::vector<hipo::event> events;
82 for(
int i = 0; i < num_events_per_frame; i++)
83 events.push_back(hipo::event());
87 for(
auto const& bank_name : bank_names)
88 banks.push_back(hipo::bank(stream.dictionary().getSchema(bank_name.c_str()), 48));
92 for(
auto const& prerequisite_algo : prerequisite_algos)
93 seq.
Add(prerequisite_algo);
95 seq.
SetName(
"TEST thread " + std::to_string(order));
104 while(nProcessed < num_events_per_thread || num_events_per_thread == 0) {
109 for(
auto& event : events) {
110 if(event.getSize() > 16) {
115 for(
auto& bank : banks)
121 if(vary_run && run_config_bank_idx.has_value()) {
128 if(std::rand() % 10 == 0) {
129 auto runnum = banks[run_config_bank_idx.value()].getInt(
"run", 0);
130 runnum += (std::rand() % 2 == 0) ? 1000 : -1000;
131 runnum = std::max(runnum, 0);
132 banks[run_config_bank_idx.value()].putInt(
"run", 0, runnum);
134 else if(std::rand() % 10 == 1) {
135 banks[run_config_bank_idx.value()].putInt(
"run", 0, 1);
151 seq.
GetLog()->Info(
"nProcessed = {}", nProcessed);
156 stream.run(ftn, num_threads);
Algorithm: An algorithm that can run a sequence of algorithms
void Add(std::string const &algo_class_name, std::string const &algo_instance_name="")
void SetName(std::string_view name)
void PrintSequence(Logger::Level level=Logger::info) const
void SetLogLevel(std::string const &algo_instance_name, std::string const &lev)
Set an algorithm log level.
virtual bool Run(hipo::banklist &banks) const final
Run Function: Process an event's hipo::banklist
virtual void Stop() final
Stop Function: Finalize this algorithm after all events are processed.
virtual void Start(hipo::banklist &banks) final
Start Function: Initialize this algorithm before any events are processed, with the intent to process...
std::unique_ptr< Logger > & GetLog()