Hyperiso 1.0.3
Modular flavour-physics calculations, Wilson coefficients and statistical inference
Loading...
Searching...
No Matches
HyperisoInterface.h
Go to the documentation of this file.
1#ifndef HYPERISO_INTERFACE_H
2#define HYPERISO_INTERFACE_H
3
4#include "MemoryManager.h"
5#include "WilsonInterface.h"
6#include <thread>
7#include <vector>
8#include <functional>
9#include <mutex>
10#include <queue>
11#include <condition_variable>
12#include <map>
13
15private:
16 struct ThreadContext {
17 std::unique_ptr<MemoryManager> memoryManager;
18 std::unique_ptr<WilsonInterface> wilsonInterface;
19 };
20
21 static std::mutex instanceMutex;
22 static std::map<std::thread::id, ThreadContext> threadInstances;
23
24 HyperisoInterface() = default;
25
26 static ThreadContext& getThreadContext(const std::string& lhaFile, const std::string& model, const std::vector<int>& models) {
27 auto threadId = std::this_thread::get_id();
28
29 std::lock_guard<std::mutex> lock(instanceMutex);
30
31 if (threadInstances.find(threadId) == threadInstances.end()) {
32 threadInstances[threadId] = ThreadContext{
33 std::make_unique<MemoryManager>(lhaFile, models),
34 std::make_unique<WilsonInterface>(model)
35 };
36 }
37
38 return threadInstances[threadId];
39 }
40
41 class ThreadPool {
42 private:
43 std::vector<std::thread> workers;
44 std::queue<std::function<void()>> tasks;
45
46 std::mutex queueMutex;
47 std::condition_variable condition;
48 bool stop;
49
50 public:
51 explicit ThreadPool(size_t threads) : stop(false) {
52 for (size_t i = 0; i < threads; ++i) {
53 workers.emplace_back([this] {
54 while (true) {
55 std::function<void()> task;
56
57 {
58 std::unique_lock<std::mutex> lock(this->queueMutex);
59 this->condition.wait(lock, [this] { return this->stop || !this->tasks.empty(); });
60
61 if (this->stop && this->tasks.empty())
62 return;
63
64 task = std::move(this->tasks.front());
65 this->tasks.pop();
66 }
67
68 task();
69 }
70 });
71 }
72 }
73
74 void enqueue(std::function<void()> task) {
75 {
76 std::lock_guard<std::mutex> lock(queueMutex);
77 tasks.emplace(std::move(task));
78 }
79 condition.notify_one();
80 }
81
82 ~ThreadPool() {
83 {
84 std::lock_guard<std::mutex> lock(queueMutex);
85 stop = true;
86 }
87 condition.notify_all();
88
89 for (std::thread& worker : workers) {
90 if (worker.joinable()) {
91 worker.join();
92 }
93 }
94 }
95 };
96
97public:
99 static HyperisoInterface instance;
100 return instance;
101 }
102
103 void Initialize(const std::string& lhaFile, const std::string& model, const std::vector<int>& models = {0}) {
104 getThreadContext(lhaFile, model, models);
105 }
106
108 return getThreadContext("", "", {}).memoryManager.get();
109 }
110
112 return getThreadContext("", "", {}).wilsonInterface.get();
113 }
114
115 void RunInParallel(const std::vector<std::string>& lhaFiles,
116 std::function<void(HyperisoInterface&, const std::string&)> task,
117 const std::string& model = "SM",
118 const std::vector<int>& models = {0},
119 size_t maxThreads = std::thread::hardware_concurrency()) {
120 ThreadPool threadPool(maxThreads);
121
122 for (const auto& lhaFile : lhaFiles) {
123 threadPool.enqueue([&, lhaFile]() {
124 this->Initialize(lhaFile, model, models);
125 task(*this, lhaFile);
126 });
127 }
128
129 }
130
132 std::lock_guard<std::mutex> lock(instanceMutex);
133 threadInstances.erase(std::this_thread::get_id());
134 }
135
138};
139
140std::mutex HyperisoInterface::instanceMutex;
141std::map<std::thread::id, HyperisoInterface::ThreadContext> HyperisoInterface::threadInstances;
142
143#endif // HYPERISO_INTERFACE_H
Manages memory caching, parameter blocks, and LHA reader instances.
static HyperisoInterface & GetInstance()
WilsonInterface * GetWilsonInterface()
HyperisoInterface & operator=(const HyperisoInterface &)=delete
void Initialize(const std::string &lhaFile, const std::string &model, const std::vector< int > &models={0})
HyperisoInterface(const HyperisoInterface &)=delete
MemoryManager * GetMemoryManager()
void RunInParallel(const std::vector< std::string > &lhaFiles, std::function< void(HyperisoInterface &, const std::string &)> task, const std::string &model="SM", const std::vector< int > &models={0}, size_t maxThreads=std::thread::hardware_concurrency())
Singleton class responsible for initializing and managing memory, input files, and parameter blocks.
User-facing API to build and query Wilson coefficients at matching and hadronic scales.