Skip to content

Commit 4a86457

Browse files
feat(Examples): Add thread id to AlgorithmContext (acts-project#4236)
This can help with debugging output, as it can print a more readable value than the native thread id from the standard library.
1 parent e8a670c commit 4a86457

8 files changed

Lines changed: 27 additions & 12 deletions

File tree

Examples/Framework/include/ActsExamples/Framework/AlgorithmContext.hpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,16 @@ struct AlgorithmContext {
2424
/// @param alg is the algorithm/service/writer number
2525
/// @param event is the event number
2626
/// @param store is the event-wise event store
27+
/// @param thread is the thread number
2728
///
2829
/// @note the event dependent contexts are to be added by the
2930
/// Sequencer::m_decorators list
30-
AlgorithmContext(std::size_t alg, std::size_t event, WhiteBoard& store)
31-
: algorithmNumber(alg), eventNumber(event), eventStore(store) {}
31+
AlgorithmContext(std::size_t alg, std::size_t event, WhiteBoard& store,
32+
std::size_t thread)
33+
: algorithmNumber(alg),
34+
eventNumber(event),
35+
eventStore(store),
36+
threadId{thread} {}
3237

3338
/// @brief ++operator overload to increase the algorithm number
3439
AlgorithmContext& operator++() {
@@ -43,6 +48,7 @@ struct AlgorithmContext {
4348
Acts::MagneticFieldContext
4449
magFieldContext; ///< Per-event magnetic Field context
4550
Acts::CalibrationContext calibContext; ///< Per-event calibration context
51+
std::size_t threadId; ///< Thread ID
4652

4753
Acts::FpeMonitor* fpeMonitor = nullptr;
4854
};

Examples/Framework/include/ActsExamples/Framework/IWriter.hpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,11 @@ class IWriter : public SequenceElement {
3232

3333
/// Informs the writer that the sequencer will start processing the next
3434
/// event.
35-
virtual ProcessCode beginEvent() { return ProcessCode::SUCCESS; }
35+
/// @param threadId The thread ID
36+
virtual ProcessCode beginEvent(std::size_t threadId) {
37+
static_cast<void>(threadId);
38+
return ProcessCode::SUCCESS;
39+
}
3640

3741
/// Fulfill the algorithm interface
3842
ProcessCode initialize() override { return ProcessCode::SUCCESS; }

Examples/Framework/src/Framework/BufferedReader.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ BufferedReader::BufferedReader(const Config &config, Acts::Logging::Level level)
4343
m_buffer.reserve(eend - ebegin);
4444
for (auto i = ebegin; i < ebegin + m_cfg.bufferSize; ++i) {
4545
auto board = std::make_unique<ActsExamples::WhiteBoard>(m_logger->clone());
46-
ActsExamples::AlgorithmContext ctx(0, i, *board);
46+
ActsExamples::AlgorithmContext ctx(0, i, *board, 0);
4747

4848
ACTS_DEBUG("Read event " << i << " into buffer");
4949
m_cfg.upstreamReader->read(ctx);

Examples/Framework/src/Framework/Sequencer.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#include <stdexcept>
3737
#include <string>
3838
#include <string_view>
39+
#include <thread>
3940
#include <typeinfo>
4041

4142
#include <TROOT.h>
@@ -390,8 +391,8 @@ int Sequencer::run() {
390391
for (std::size_t n = r.begin(); n != r.end(); ++n) {
391392
ACTS_VERBOSE("Thread about to pick next event");
392393

393-
for (auto& writer : m_writers) {
394-
if (writer->beginEvent() != ProcessCode::SUCCESS) {
394+
for (const auto& writer : m_writers) {
395+
if (writer->beginEvent(threadId) != ProcessCode::SUCCESS) {
395396
throw std::runtime_error("Failed to process event data");
396397
}
397398
}
@@ -408,7 +409,7 @@ int Sequencer::run() {
408409
m_whiteboardObjectAliases);
409410
// If we ever wanted to run algorithms in parallel, this needs to
410411
// be changed to Algorithm context copies
411-
AlgorithmContext context(0, event, eventStore);
412+
AlgorithmContext context(0, event, eventStore, threadId);
412413
std::size_t ialgo = 0;
413414

414415
/// Decorate the context

Examples/Python/src/Framework.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <pybind11/stl.h>
2222

2323
namespace py = pybind11;
24+
using namespace py::literals;
2425
using namespace ActsExamples;
2526
using namespace Acts::Python;
2627

@@ -120,13 +121,15 @@ void addFramework(Context& ctx) {
120121
.def_property_readonly("keys", &WhiteBoard::getKeys);
121122

122123
py::class_<AlgorithmContext>(mex, "AlgorithmContext")
123-
.def(py::init<std::size_t, std::size_t, WhiteBoard&>())
124+
.def(py::init<std::size_t, std::size_t, WhiteBoard&, std::size_t>(),
125+
"alg"_a, "event"_a, "store"_a, "thread"_a)
124126
.def_readonly("algorithmNumber", &AlgorithmContext::algorithmNumber)
125127
.def_readonly("eventNumber", &AlgorithmContext::eventNumber)
126128
.def_property_readonly("eventStore",
127129
[](const AlgorithmContext& self) -> WhiteBoard& {
128130
return self.eventStore;
129131
})
132+
.def_readonly("threadId", &AlgorithmContext::threadId)
130133
.def_readonly("magFieldContext", &AlgorithmContext::magFieldContext)
131134
.def_readonly("geoContext", &AlgorithmContext::geoContext)
132135
.def_readonly("calibContext", &AlgorithmContext::calibContext)

Examples/Scripts/Python/geometry.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,9 @@ def runGeometry(
3030
for ievt in range(events):
3131
eventStore = WhiteBoard(name=f"EventStore#{ievt}", level=acts.logging.INFO)
3232
ialg = 0
33+
ithread = 0
3334

34-
context = AlgorithmContext(ialg, ievt, eventStore)
35+
context = AlgorithmContext(ialg, ievt, eventStore, ithread)
3536

3637
for cdr in decorators:
3738
r = cdr.decorate(context)

Examples/Scripts/Python/material_mapping.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def runMaterialMapping(
4646

4747
wb = WhiteBoard(acts.logging.INFO)
4848

49-
context = AlgorithmContext(0, 0, wb)
49+
context = AlgorithmContext(0, 0, wb, 0)
5050

5151
for decorator in decorators:
5252
assert decorator.decorate(context) == ProcessCode.SUCCESS

Tests/CommonHelpers/include/Acts/Tests/CommonHelpers/WhiteBoardUtilities.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ struct GenericReadWriteTool {
6969
template <typename writer_t>
7070
auto write(writer_t &writer, std::size_t eventId = 0) {
7171
ActsExamples::WhiteBoard board;
72-
ActsExamples::AlgorithmContext ctx(0, eventId, board);
72+
ActsExamples::AlgorithmContext ctx(0, eventId, board, 0);
7373

7474
auto add = [&](auto &self, auto N) {
7575
if constexpr (N() < kSize) {
@@ -86,7 +86,7 @@ struct GenericReadWriteTool {
8686
template <typename reader_t>
8787
auto read(reader_t &reader, std::size_t eventId = 0) {
8888
ActsExamples::WhiteBoard board;
89-
ActsExamples::AlgorithmContext ctx(0, eventId, board);
89+
ActsExamples::AlgorithmContext ctx(0, eventId, board, 0);
9090

9191
reader.internalExecute(ctx);
9292

0 commit comments

Comments
 (0)