Hyperiso 1.0.3
Modular flavour-physics calculations, Wilson coefficients and statistical inference
Loading...
Searching...
No Matches
ParameterRuntimeContext.cpp
Go to the documentation of this file.
2
3#include "MemoryManager.h"
4#include "Parameters.h"
5
6#include <mutex>
7
8thread_local ParameterRuntimeContext* ParameterRuntimeContext::current_ = nullptr;
9
10namespace {
11std::mutex parameter_runtime_snapshot_mutex;
12}
13
15 std::lock_guard<std::mutex> lock(parameter_runtime_snapshot_mutex);
16 auto* mm = MemoryManager::GetInstance();
17 const auto& cache = mm->getMemoryCache();
18 parameter_types_ = cache.parameter_types;
19 local_input_cache_ = mm->clone_input_cache_deep();
20
21 // Preserve runtime edits made on global plain input blocks before entering
22 // the worker context, but do not copy dependent graph nodes. Dependent
23 // blocks are rebuilt locally by the Parameters strategies.
24 for (auto type : parameter_types_) {
25 auto global_params = ParametersFactory::GetParameters(type);
26 if (!global_params) {
27 continue;
28 }
29
30 for (const auto& block_name : global_params->get_blocks_list()) {
31 if (global_params->is_dependent_block(block_name)) {
32 continue;
33 }
34
35 auto global_blocks = global_params->get_block_accessor();
36 if (!global_blocks || !global_blocks->contains(block_name)) {
37 continue;
38 }
39
40 auto cloned_block = global_blocks->at(block_name)->deep_clone_plain();
41 local_input_cache_->emplace(block_name, cloned_block);
42 }
43 }
44}
45
47 auto it = local_instances_.find(id);
48 if (it != local_instances_.end()) {
49 return it->second;
50 }
51
52 // The repository must be registered in the local map before
53 // postInitialization runs. Post-initialization attaches dependent blocks via
54 // DependentBlockManager, and that path can recursively call
55 // Parameters::GetInstance(id) for the repository currently being built.
56 // Registering only after CreateUncached returns causes unbounded recursion
57 // and typically shows up as a segfault/stack overflow before MC accepts its
58 // first draw.
59 try {
61 id,
62 [this, id](const std::shared_ptr<Parameters>& params) {
63 local_instances_[id] = params;
64 }
65 );
66 } catch (...) {
67 local_instances_.erase(id);
68 throw;
69 }
70}
71
72std::shared_ptr<BlockAccessor> ParameterRuntimeContext::extract_blocks(
73 const std::unordered_set<BlockName>& block_names
74) const {
75 return (*local_input_cache_)[block_names];
76}
77
78std::unordered_set<BlockName> ParameterRuntimeContext::available_input_blocks() const {
79 return local_input_cache_->get_block_names();
80}
81
82const std::vector<ParameterType>& ParameterRuntimeContext::parameter_types() const {
83 return parameter_types_;
84}
85
89
91 : previous_(ParameterRuntimeContext::current_) {
92 ParameterRuntimeContext::current_ = &ctx;
93}
94
96 ParameterRuntimeContext::current_ = previous_;
97}
ParameterType
Manages memory caching, parameter blocks, and LHA reader instances.
Model-dependent parameter repository and initialization strategies.
static MemoryManager * GetInstance()
Retrieves the singleton instance of MemoryManager.
Per-thread parameter runtime used to isolate Monte-Carlo workers.
std::shared_ptr< Parameters > get_parameters(ParameterType id)
std::unordered_set< BlockName > available_input_blocks() const
const std::vector< ParameterType > & parameter_types() const
static ParameterRuntimeContext * current()
std::shared_ptr< BlockAccessor > extract_blocks(const std::unordered_set< BlockName > &block_names) const
static std::shared_ptr< Parameters > CreateUncachedRegistered(ParameterType id, const std::function< void(const std::shared_ptr< Parameters > &)> &register_before_postinit)
Creates a fresh uncached repository and registers it before post-initialization.
static std::shared_ptr< Parameters > GetParameters(ParameterType id)
Returns the cached repository for one parameter type.
ScopedParameterRuntimeContext(ParameterRuntimeContext &ctx)