-
Notifications
You must be signed in to change notification settings - Fork 174
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into refactor/boundary-tolerance-explicit
- Loading branch information
Showing
8 changed files
with
277 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
Examples/Framework/include/ActsExamples/Framework/BufferedReader.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
// This file is part of the ACTS project. | ||
// | ||
// Copyright (C) 2016 CERN for the benefit of the ACTS project | ||
// | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
|
||
#pragma once | ||
|
||
#include "Acts/Utilities/Logger.hpp" | ||
#include "ActsExamples/Framework/AlgorithmContext.hpp" | ||
#include "ActsExamples/Framework/IReader.hpp" | ||
#include "ActsExamples/Framework/ProcessCode.hpp" | ||
|
||
#include <utility> | ||
|
||
namespace ActsExamples { | ||
|
||
class WhiteBoard; | ||
|
||
/// Event data reader that takes a concrete reader instance, reads a number of | ||
/// events in a buffer, and selects events from that buffer instead of directly | ||
/// reading them from disk. | ||
/// The purpose is to avoid IO bottlenecks in timing measurements | ||
class BufferedReader final : public IReader { | ||
public: | ||
struct Config { | ||
/// The upstream reader that should be used | ||
std::shared_ptr<IReader> upstreamReader; | ||
|
||
/// The seed for sampling events from the buffer | ||
std::size_t selectionSeed = 123456; | ||
|
||
/// Buffer size. The reader will throw and exception if the downstream | ||
/// reader does not provide enough events | ||
std::size_t bufferSize = 1; | ||
}; | ||
|
||
/// Constructed the reader | ||
BufferedReader(const Config& config, Acts::Logging::Level level); | ||
|
||
/// Return the config | ||
const Config& config() const { return m_cfg; } | ||
|
||
/// Give the reader a understandable name | ||
std::string name() const override { | ||
return "Buffered" + m_cfg.upstreamReader->name(); | ||
} | ||
|
||
/// The buffered reader provides the maximum available event range | ||
std::pair<std::size_t, std::size_t> availableEvents() const override { | ||
return {0, std::numeric_limits<std::size_t>::max()}; | ||
} | ||
|
||
/// Return a event from the buffer | ||
ProcessCode read(const AlgorithmContext& ctx) override; | ||
|
||
/// Fulfill the algorithm interface | ||
ProcessCode initialize() override { return ProcessCode::SUCCESS; } | ||
|
||
/// Fulfill the algorithm interface | ||
ProcessCode finalize() override { return ProcessCode::SUCCESS; } | ||
|
||
private: | ||
Config m_cfg; | ||
std::unique_ptr<const Acts::Logger> m_logger; | ||
std::vector<std::unique_ptr<ActsExamples::WhiteBoard>> m_buffer; | ||
|
||
const Acts::Logger& logger() const { return *m_logger; } | ||
}; | ||
|
||
} // namespace ActsExamples |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
// This file is part of the ACTS project. | ||
// | ||
// Copyright (C) 2016 CERN for the benefit of the ACTS project | ||
// | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
|
||
#include "ActsExamples/Framework/BufferedReader.hpp" | ||
|
||
#include "Acts/Utilities/Logger.hpp" | ||
#include "ActsExamples/Framework/AlgorithmContext.hpp" | ||
#include "ActsExamples/Framework/WhiteBoard.hpp" | ||
|
||
#include <random> | ||
#include <utility> | ||
|
||
namespace ActsExamples { | ||
|
||
BufferedReader::BufferedReader(const Config &config, Acts::Logging::Level level) | ||
: m_cfg(config), m_logger(Acts::getDefaultLogger(name(), level)) { | ||
if (!m_cfg.upstreamReader) { | ||
throw std::invalid_argument("No upstream reader provided!"); | ||
} | ||
|
||
// Register write and read handles of the upstream reader | ||
for (auto rh : m_cfg.upstreamReader->readHandles()) { | ||
registerReadHandle(*rh); | ||
} | ||
|
||
for (auto wh : m_cfg.upstreamReader->writeHandles()) { | ||
registerWriteHandle(*wh); | ||
} | ||
|
||
// Read the events | ||
auto [ebegin, eend] = m_cfg.upstreamReader->availableEvents(); | ||
if (eend - ebegin < m_cfg.bufferSize) { | ||
throw std::runtime_error("Reader does not provide enough events"); | ||
} | ||
|
||
ACTS_INFO("Start reading events into buffer..."); | ||
|
||
m_buffer.reserve(eend - ebegin); | ||
for (auto i = ebegin; i < ebegin + m_cfg.bufferSize; ++i) { | ||
auto board = std::make_unique<ActsExamples::WhiteBoard>(m_logger->clone()); | ||
ActsExamples::AlgorithmContext ctx(0, i, *board); | ||
|
||
ACTS_DEBUG("Read event " << i << " into buffer"); | ||
m_cfg.upstreamReader->read(ctx); | ||
m_buffer.emplace_back(std::move(board)); | ||
} | ||
|
||
ACTS_INFO("Filled " << m_buffer.size() << " events into the buffer"); | ||
} | ||
|
||
ProcessCode BufferedReader::read(const AlgorithmContext &ctx) { | ||
// Set up a random event selection that is consistent if multiple | ||
// BufferedReader are used within a workflow The linear congruential engine is | ||
// chosen since it is cheap to instantiate. For each eventNumber, it is put in | ||
// a reproducible state. | ||
std::minstd_rand rng(m_cfg.selectionSeed); | ||
rng.discard(ctx.eventNumber); | ||
|
||
/// Sample from the buffer and transfer the content | ||
std::uniform_int_distribution<std::size_t> dist(0, m_cfg.bufferSize - 1); | ||
|
||
const auto entry = dist(rng); | ||
ctx.eventStore.copyFrom(*m_buffer.at(entry)); | ||
|
||
ACTS_DEBUG("Use buffer entry " << entry << " for event " << ctx.eventNumber); | ||
|
||
return ProcessCode::SUCCESS; | ||
} | ||
|
||
} // namespace ActsExamples |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters