|
23 | 23 | */ |
24 | 24 |
|
25 | 25 | // gdata |
26 | | -#include "event/gEventDataCollection.h" |
| 26 | +#include "run/gRunDataCollection.h" |
| 27 | + |
27 | 28 |
|
28 | 29 | // gemc |
29 | 30 | #include "glogger.h" |
|
32 | 33 | auto run_simulation_in_threads(int nevents, |
33 | 34 | int nthreads, |
34 | 35 | const std::shared_ptr<GOptions>& gopt, |
35 | | - const std::shared_ptr<GLogger>& log) -> std::vector<std::shared_ptr<GEventDataCollection>> { |
36 | | - std::mutex collectorMtx; |
37 | | - std::vector<std::shared_ptr<GEventDataCollection>> collected; |
38 | | - |
39 | | - // thread-safe integer counter starts at 1. |
40 | | - // fetch_add returns the old value *and* bumps. |
41 | | - // zero contention: each thread fetches the next free event number. |
42 | | - std::atomic<int> next{1}; |
43 | | - |
44 | | - // pool of jthreads. jthread joins in its destructor so we don’t need an |
45 | | - // explicit loop at the end. |
46 | | - // each element represents one worker thread running your event-processing lambda. |
47 | | - // std::vector<std::jthread> pool; use this when C++20 is widely available |
48 | | - std::vector<jthread_alias> pool; // was std::vector<std::jthread> |
49 | | - |
50 | | - pool.reserve(nthreads); |
51 | | - |
52 | | - for (int tid = 0; tid < nthreads; ++tid) { |
53 | | - // The capture [&, tid] gives the thread references to variables like next, nevents, runDataMtx, etc. |
54 | | - pool.emplace_back([&, tid] // capture tid *by value* |
55 | | - { |
56 | | - // start the thread with a lambda |
57 | | - log->info(0, "worker ", tid, " started"); |
58 | | - |
59 | | - int localCount = 0; // events built by *this* worker |
60 | | - thread_local std::vector<std::shared_ptr<GEventDataCollection>> localRunData; |
61 | | - |
62 | | - while (true) { |
63 | | - // repeatedly asks the shared atomic counter for “the next unclaimed event |
64 | | - // number,” processes that event, stores the result, and goes back for more. |
65 | | - // memory_order_relaxed: we only need *atomicity*, no ordering |
66 | | - int evn = next.fetch_add(1, std::memory_order_relaxed); // atomically returns the current value and increments it by 1. |
67 | | - if (evn > nevents) break; // exit the while loop |
68 | | - |
69 | | - auto event_data_collection = GEventDataCollection::create(gopt); |
70 | | - localRunData.emplace_back(event_data_collection); |
71 | | - |
72 | | - ++localCount; // tally for this worker |
73 | | - } |
74 | | - |
75 | | - // braces to lock the mutex when it's constructed and unlocks when it is destroyed |
76 | | - { |
77 | | - std::scoped_lock lk(collectorMtx); |
78 | | - for (auto& evt : localRunData) { collected.emplace_back(evt); } |
79 | | - localRunData.clear(); |
80 | | - } |
81 | | - |
82 | | - log->info(0, "worker ", tid, " processed ", localCount, " events"); |
83 | | - }); // jthread constructor launches the thread immediately |
84 | | - } // pool’s destructor blocks until every jthread has joined |
85 | | - return collected; |
| 36 | + const std::shared_ptr<GLogger>& log) -> std::vector<std::shared_ptr< |
| 37 | + GEventDataCollection>> { |
| 38 | + std::mutex collectorMtx; |
| 39 | + std::vector<std::shared_ptr<GEventDataCollection>> collected; |
| 40 | + |
| 41 | + // thread-safe integer event counter starts at 1. |
| 42 | + // fetch_add returns the old value *and* bumps. |
| 43 | + // zero contention: each thread fetches the next free event number. |
| 44 | + std::atomic<int> next{1}; |
| 45 | + |
| 46 | + // pool of jthreads. jthread joins in its destructor so we don’t need an |
| 47 | + // explicit loop at the end. |
| 48 | + // each element represents one worker thread running your event-processing lambda. |
| 49 | + // std::vector<std::jthread> pool; use this when C++20 is widely available |
| 50 | + std::vector<jthread_alias> pool; // was std::vector<std::jthread> |
| 51 | + |
| 52 | + pool.reserve(nthreads); |
| 53 | + |
| 54 | + for (int tid = 0; tid < nthreads; ++tid) { |
| 55 | + // The capture [&, tid] gives the thread references to variables like next, nevents, runDataMtx, etc. |
| 56 | + pool.emplace_back([&, tid] // capture tid *by value* |
| 57 | + { |
| 58 | + // start the thread with a lambda |
| 59 | + log->info(0, "worker ", tid, " started"); |
| 60 | + |
| 61 | + int localCount = 0; // events built by *this* worker |
| 62 | + thread_local std::vector<std::shared_ptr<GEventDataCollection>> localRunData; |
| 63 | + |
| 64 | + while (true) { |
| 65 | + // repeatedly asks the shared atomic counter for “the next unclaimed event |
| 66 | + // number,” processes that event, stores the result, and goes back for more. |
| 67 | + // memory_order_relaxed: we only need *atomicity*, no ordering |
| 68 | + int evn = next.fetch_add(1, std::memory_order_relaxed); |
| 69 | + // atomically returns the current value and increments it by 1. |
| 70 | + if (evn > nevents) { break ;} // exit the while loop |
| 71 | + |
| 72 | + |
| 73 | + auto event_data_collection = GEventDataCollection::create(gopt); |
| 74 | + localRunData.emplace_back(event_data_collection); |
| 75 | + |
| 76 | + ++localCount; // tally for this worker |
| 77 | + } |
| 78 | + |
| 79 | + // braces to lock the mutex when it's constructed and unlocks when it is destroyed |
| 80 | + { |
| 81 | + std::scoped_lock lk(collectorMtx); |
| 82 | + for (auto& evt : localRunData) { collected.emplace_back(evt); } |
| 83 | + localRunData.clear(); |
| 84 | + } |
| 85 | + |
| 86 | + log->info(0, "worker ", tid, " processed ", localCount, " events"); |
| 87 | + }); // jthread constructor launches the thread immediately |
| 88 | + } // pool’s destructor blocks until every jthread has joined |
| 89 | + return collected; |
86 | 90 | } |
87 | 91 |
|
88 | 92 |
|
89 | 93 | // emulation of a run of events, collecting data in separate threads |
90 | 94 |
|
91 | 95 | int main(int argc, char* argv[]) { |
92 | | - // Create GOptions using gevent_data::defineOptions, which aggregates options from all gdata and gtouchable. |
93 | | - auto gopts = std::make_shared<GOptions>(argc, argv, gevent_data::defineOptions()); |
| 96 | + // Create GOptions using gevent_data::defineOptions, which aggregates options from all gdata and gtouchable. |
| 97 | + auto gopts = std::make_shared<GOptions>(argc, argv, gevent_data::defineOptions()); |
94 | 98 |
|
95 | | - // Create loggers: one for gdata and one for gtouchable. |
96 | | - auto log = std::make_shared<GLogger>(gopts, SFUNCTION_NAME, GEVENTDATA_LOGGER); |
| 99 | + // Create loggers: one for gdata and one for gtouchable. |
| 100 | + auto log = std::make_shared<GLogger>(gopts, SFUNCTION_NAME, GEVENTDATA_LOGGER); |
97 | 101 |
|
98 | | - constexpr int nevents = 10; |
99 | | - constexpr int nthreads = 8; |
| 102 | + constexpr int nevents = 10; |
| 103 | + constexpr int nthreads = 8; |
100 | 104 |
|
101 | | - auto runData = run_simulation_in_threads(nevents, nthreads, gopts, log); |
| 105 | + auto runData = run_simulation_in_threads(nevents, nthreads, gopts, log); |
102 | 106 |
|
103 | | - // For demonstration, we'll simply print the event numbers. |
104 | | - for (size_t i = 0; i < runData.size(); i++) { log->info("event n. ", i + 1, " collected with local event number: ", runData[i]->getEventNumber()); } |
| 107 | + // For demonstration, we'll simply print the event numbers. |
| 108 | + for (size_t i = 0; i < runData.size(); i++) { |
| 109 | + log->info("event n. ", i + 1, " collected with local event number: ", runData[i]->getEventNumber()); |
| 110 | + } |
105 | 111 |
|
106 | | - return EXIT_SUCCESS; |
| 112 | + return EXIT_SUCCESS; |
107 | 113 | } |
0 commit comments