Adaptyst
A comprehensive and architecture-agnostic performance analysis tool
Loading...
Searching...
No Matches
system.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2025 CERN
2// SPDX-License-Identifier: GPL-3.0-or-later
3
4#ifndef SYSTEM_HPP_
5#define SYSTEM_HPP_
6
7#include <string>
8#include <memory>
9#include <unordered_set>
10#include <unordered_map>
11#include <vector>
12#include <filesystem>
13#include <future>
14#include "adaptyst/output.hpp"
15#include "adaptyst/process.hpp"
16
17#define ADAPTYST_INTERNAL
18#include "adaptyst/hw.h"
19
20namespace adaptyst {
21 namespace fs = std::filesystem;
22
24 private:
25 std::string name;
26 Identifiable *parent;
27 std::unordered_map<fs::path, fs::path> paths;
28
29 protected:
30 Identifiable(std::string name) {
31 this->name = name;
32 this->parent = nullptr;
33 }
34
35 inline void throw_error(std::string msg) {
36 throw std::runtime_error(name + ": " + msg);
37 }
38
39 public:
40 std::string &get_name() {
41 return this->name;
42 }
43
44 void set_parent(Identifiable *identifiable) {
45 this->parent = identifiable;
46 }
47
48 std::string get_parent_name() {
49 if (this->parent) {
50 return this->parent->get_name();
51 } else {
52 return "(N/A)";
53 }
54 }
55
56 fs::path &get_path(fs::path start) {
57 if (this->paths.find(start) == this->paths.end()) {
58 std::vector<std::string> chain;
59
60 chain.push_back(this->name);
61
62 Identifiable *current = this->parent;
63
64 while (current) {
65 chain.push_back(current->name);
66 current = current->parent;
67 }
68
69 fs::path result = start;
70
71 for (int i = chain.size() - 1; i >= 0; i--) {
72 result /= chain[i];
73 }
74
75 this->paths[start] = result;
76 }
77
78 return this->paths[start];
79 }
80
81 virtual std::vector<std::string> get_log_types() = 0;
82 virtual std::string get_type() = 0;
83 };
84
85 class Node;
86 class Entity;
87 class System;
88
89 class Module : public Identifiable {
90 public:
91 static std::unordered_map<amod_t, Module *> all_modules;
93
102
103 static std::vector<std::unique_ptr<Module> > get_all_modules(fs::path library_path);
104
105 Module(std::string backend_name,
106 fs::path library_path);
107 Module(std::string backend_name,
108 std::unordered_map<std::string, std::string> &options,
109 std::unordered_map<std::string, std::vector<std::string>> &array_options,
110 fs::path library_path,
111 bool never_directing);
112 ~Module();
113 bool init();
114 void process(std::string sdfg);
115 bool wait();
116 void close();
117 void set_will_profile(bool will_profile);
118 bool get_will_profile();
119 void set_error(std::string error);
120 std::unordered_map<std::string, option> &get_options();
121 std::unique_ptr<Path> &get_dir();
122 std::unordered_set<std::string> &get_tags();
123 std::unordered_map<std::string, OptionMetadata> &get_all_options();
124 void set_dir(fs::path dir);
125 void profile_notify();
126 int profile_wait();
127 std::vector<std::string> get_log_types();
128 std::string get_type();
129 std::string &get_node_name();
130 void set_api_error(std::string msg, int code);
131 int get_api_error_code();
132 std::string &get_api_error_msg();
133 bool is_directing_node();
134 void add_src_code_path(fs::path path);
135 void set_node(Node *node);
136 fs::path &get_tmp_dir();
137 fs::path &get_local_config_dir();
138 bool has_in_tag(std::string tag);
139 bool has_out_tag(std::string tag);
142 bool is_initialising();
143 const char *get_cpu_mask();
144 std::unordered_set<fs::path> &get_src_code_paths();
145 std::string get_name();
146 std::string get_version();
147 std::vector<int> get_version_nums();
148 fs::path get_lib_path();
149 unsigned int get_max_count_per_entity();
150
151 private:
152 amod_t id;
153 std::unordered_map<std::string, option> options;
154 std::unordered_map<std::string, OptionMetadata> option_metadata;
155 std::unique_ptr<Path> dir;
156 bool will_profile;
157 std::string error;
158 void *context;
159 void *handle;
160 std::future<bool> process_future;
161 std::vector<std::string> log_types;
162 std::vector<void *> malloced;
163 std::unordered_set<std::string> tags;
164 Node *node;
165 bool initialised;
166 bool never_directing;
167 int api_error_code;
168 std::string api_error_msg;
169 bool initialising;
170 std::unordered_set<fs::path> src_code_paths;
171 fs::path lib_path;
172 unsigned int max_count_per_entity;
173
174 void construct(std::string backend_name,
175 std::unordered_map<std::string, std::string> &options,
176 std::unordered_map<std::string, std::vector<std::string>> &array_options,
177 fs::path library_path, bool never_directing);
178 };
179
180 class Node : public Identifiable {
181 public:
182 Node(std::string name,
183 std::shared_ptr<Entity> &entity);
184 bool init();
185 void process(std::string &sdfg);
186 bool wait();
187 void close();
188 std::unordered_set<std::string> &get_tags();
189 void add_in_tags(std::unordered_set<std::string> &tags);
190 void add_out_tags(std::unordered_set<std::string> &tags);
191 bool has_in_tag(std::string tag);
192 bool has_out_tag(std::string tag);
193 void add_module(std::unique_ptr<Module> &mod);
194 void profile_notify();
195 int profile_wait();
196 bool get_will_profile();
197 void set_will_profile(bool will_profile);
198 void set_dir(fs::path path);
199 std::vector<std::string> get_log_types();
200 std::string get_type();
201 bool is_directing();
204 const char *get_cpu_mask();
205 fs::path &get_tmp_dir();
206 fs::path &get_local_config_dir();
207 std::unordered_set<fs::path> get_src_code_paths();
208
209 private:
210 std::unique_ptr<Path> dir;
211 std::vector<std::unique_ptr<Module> > modules;
212 std::shared_ptr<Entity> entity;
213 std::unordered_set<std::string> tags;
214 std::unordered_set<std::string> in_tags;
215 std::unordered_set<std::string> out_tags;
216 bool will_profile;
217 };
218
220 private:
221 std::shared_ptr<Node> departure_node;
222 std::shared_ptr<Node> arrival_node;
223
224 public:
225 NodeConnection(std::string id,
226 std::shared_ptr<Node> &departure_node,
227 std::shared_ptr<Node> &arrival_node);
228 std::shared_ptr<Node> &get_departure_node();
229 std::shared_ptr<Node> &get_arrival_node();
230 std::vector<std::string> get_log_types();
231 std::string get_type();
232 };
233
234 class Entity : public Identifiable {
235 public:
242
243 Entity(std::string id, AccessMode access_mode,
244 unsigned int processing_threads,
245 fs::path local_config_path,
246 fs::path tmp_dir);
247 void add_node(std::shared_ptr<Node> &node);
248 void add_connection(std::string id,
249 std::string departure_node,
250 std::string arrival_node);
251 std::shared_ptr<Node> &get_node(std::string id);
252 void set_directing_node(std::string node);
253 std::string get_directing_node();
255 void set_profile_info(profile_info &info);
256 void init();
257 void process(bool save_src_code_paths);
258 void close();
259 void set_entity_dir(fs::path &entity_dir);
260 void profile_notify();
261 int profile_wait();
262 const char *get_cpu_mask();
263 fs::path &get_tmp_dir();
264 fs::path &get_local_config_dir();
265 std::vector<std::shared_ptr<Node> > get_all_nodes();
266 std::vector<std::string> get_log_types();
267 std::string get_type();
268 void set_sdfg(std::string sdfg);
269 std::unordered_set<fs::path> &get_src_code_paths();
270
271 private:
272 AccessMode access_mode;
273 std::unordered_map<std::string, std::shared_ptr<Node> > nodes;
274 std::unordered_map<std::string, std::shared_ptr<NodeConnection> > connections;
275 std::string directing_node;
276 profile_info profiling_info;
277 std::unique_ptr<Path> entity_dir;
278 unsigned int processing_threads;
279 fs::path local_config_path;
280 fs::path tmp_dir;
281 std::string cpu_mask;
282 std::string sdfg;
283 bool will_profile;
284 std::unique_ptr<Process> profiled_process;
285 std::unordered_set<fs::path> src_code_paths;
286 bool src_code_paths_collected;
287 bool workflow_finish_printed;
288 long long workflow_start_time;
289 std::mutex workflow_finish_print_mutex;
290 };
291
292 class System {
293 private:
294 std::unordered_map<std::string, std::shared_ptr<Entity> > entities;
295 std::unordered_map<std::string,
296 std::shared_ptr<NodeConnection> > connections;
297 std::unique_ptr<Path> root_dir;
298 std::variant<fs::path, int> codes_dst;
299 bool custom_src_code_paths_save;
300
301 void init(fs::path def_file, fs::path root_dir,
302 fs::path library_path, fs::path local_config_path,
303 fs::path tmp_dir);
304 public:
305 System(fs::path def_file, fs::path root_dir,
306 fs::path library_path, fs::path local_config_path,
307 fs::path tmp_dir);
308 System(fs::path def_file, fs::path root_dir,
309 fs::path library_path, fs::path local_config_path,
310 fs::path tmp_dir, std::variant<fs::path, int> codes_dst);
311 ~System();
312 void set_sdfg(std::string sdfg);
313 void process();
315 };
316};
317
318#endif
Definition system.hpp:234
void init()
Definition system.cpp:1216
void set_directing_node(std::string node)
Definition system.cpp:1200
int profile_wait()
Definition system.cpp:1362
std::string get_directing_node()
Definition system.cpp:1204
void add_node(std::shared_ptr< Node > &node)
Definition system.cpp:1175
void set_profile_info(profile_info &info)
Definition system.cpp:1212
std::vector< std::shared_ptr< Node > > get_all_nodes()
Definition system.cpp:1511
fs::path & get_local_config_dir()
Definition system.cpp:1507
void profile_notify()
Definition system.cpp:1356
const char * get_cpu_mask()
Definition system.cpp:1424
std::unordered_set< fs::path > & get_src_code_paths()
Definition system.cpp:1533
void process(bool save_src_code_paths)
Definition system.cpp:1226
Entity(std::string id, AccessMode access_mode, unsigned int processing_threads, fs::path local_config_path, fs::path tmp_dir)
Definition system.cpp:1163
profile_info & get_profile_info()
Definition system.cpp:1208
std::vector< std::string > get_log_types()
Definition system.cpp:1521
void set_sdfg(std::string sdfg)
Definition system.cpp:1529
void set_entity_dir(fs::path &entity_dir)
Definition system.cpp:1347
void add_connection(std::string id, std::string departure_node, std::string arrival_node)
Definition system.cpp:1180
fs::path & get_tmp_dir()
Definition system.cpp:1503
AccessMode
Definition system.hpp:236
@ REMOTE
Definition system.hpp:238
@ CUSTOM_REMOTE
Definition system.hpp:240
@ LOCAL
Definition system.hpp:237
@ CUSTOM
Definition system.hpp:239
void close()
Definition system.cpp:1341
std::shared_ptr< Node > & get_node(std::string id)
Definition system.cpp:1192
std::string get_type()
Definition system.cpp:1525
void set_parent(Identifiable *identifiable)
Definition system.hpp:44
Identifiable(std::string name)
Definition system.hpp:30
fs::path & get_path(fs::path start)
Definition system.hpp:56
virtual std::string get_type()=0
std::string & get_name()
Definition system.hpp:40
virtual std::vector< std::string > get_log_types()=0
void throw_error(std::string msg)
Definition system.hpp:35
std::string get_parent_name()
Definition system.hpp:48
void close()
Definition system.cpp:856
void add_src_code_path(fs::path path)
Definition system.cpp:951
std::vector< int > get_version_nums()
Definition system.cpp:468
std::string get_type()
Definition system.cpp:925
const char * get_cpu_mask()
Definition system.cpp:983
std::unordered_map< std::string, option > & get_options()
Definition system.cpp:888
std::unordered_set< std::string > & get_tags()
Definition system.cpp:896
bool has_in_tag(std::string tag)
Definition system.cpp:963
unsigned int get_max_count_per_entity()
Definition system.cpp:492
std::string & get_node_name()
Definition system.cpp:917
std::unordered_map< std::string, OptionMetadata > & get_all_options()
Definition system.cpp:900
void set_will_profile(bool will_profile)
Definition system.cpp:872
void set_profile_info(profile_info info)
Definition system.cpp:975
int profile_wait()
Definition system.cpp:913
bool is_initialising()
Definition system.cpp:979
void set_error(std::string error)
Definition system.cpp:884
void profile_notify()
Definition system.cpp:909
std::vector< std::string > get_log_types()
Definition system.cpp:921
std::string & get_api_error_msg()
Definition system.cpp:938
~Module()
Definition system.cpp:509
void process(std::string sdfg)
Definition system.cpp:832
bool get_will_profile()
Definition system.cpp:880
static amod_t next_module_id
Definition system.hpp:92
std::unique_ptr< Path > & get_dir()
Definition system.cpp:892
std::string get_name()
Definition system.cpp:428
fs::path get_lib_path()
Definition system.cpp:488
bool wait()
Definition system.cpp:846
Module(std::string backend_name, fs::path library_path)
Definition system.cpp:496
static std::unordered_map< amod_t, Module * > all_modules
Definition system.hpp:91
fs::path & get_local_config_dir()
Definition system.cpp:959
int get_api_error_code()
Definition system.cpp:942
void set_api_error(std::string msg, int code)
Definition system.cpp:933
bool has_out_tag(std::string tag)
Definition system.cpp:967
static std::vector< std::unique_ptr< Module > > get_all_modules(fs::path library_path)
Definition system.cpp:403
fs::path & get_tmp_dir()
Definition system.cpp:955
bool init()
Definition system.cpp:809
void set_dir(fs::path dir)
Definition system.cpp:904
bool is_directing_node()
Definition system.cpp:946
std::unordered_set< fs::path > & get_src_code_paths()
Definition system.cpp:987
profile_info & get_profile_info()
Definition system.cpp:971
std::string get_version()
Definition system.cpp:448
void set_node(Node *node)
Definition system.cpp:929
std::string get_type()
Definition system.cpp:1159
std::shared_ptr< Node > & get_departure_node()
Definition system.cpp:1147
std::vector< std::string > get_log_types()
Definition system.cpp:1155
NodeConnection(std::string id, std::shared_ptr< Node > &departure_node, std::shared_ptr< Node > &arrival_node)
Definition system.cpp:1140
std::shared_ptr< Node > & get_arrival_node()
Definition system.cpp:1151
Definition system.hpp:180
std::unordered_set< fs::path > get_src_code_paths()
Definition system.cpp:1120
void profile_notify()
Definition system.cpp:1046
std::string get_type()
Definition system.cpp:1136
void set_dir(fs::path path)
Definition system.cpp:1088
fs::path & get_tmp_dir()
Definition system.cpp:1112
const char * get_cpu_mask()
Definition system.cpp:1108
int profile_wait()
Definition system.cpp:1050
void close()
Definition system.cpp:1036
bool wait()
Definition system.cpp:1025
std::unordered_set< std::string > & get_tags()
Definition system.cpp:1042
void add_module(std::unique_ptr< Module > &mod)
Definition system.cpp:1074
bool has_out_tag(std::string tag)
Definition system.cpp:1070
void add_in_tags(std::unordered_set< std::string > &tags)
Definition system.cpp:1054
bool is_directing()
Definition system.cpp:1096
void set_profile_info(profile_info info)
Definition system.cpp:1104
void process(std::string &sdfg)
Definition system.cpp:1019
profile_info & get_profile_info()
Definition system.cpp:1100
std::vector< std::string > get_log_types()
Definition system.cpp:1132
fs::path & get_local_config_dir()
Definition system.cpp:1116
bool has_in_tag(std::string tag)
Definition system.cpp:1066
void add_out_tags(std::unordered_set< std::string > &tags)
Definition system.cpp:1060
bool get_will_profile()
Definition system.cpp:1080
void set_will_profile(bool will_profile)
Definition system.cpp:1084
bool init()
Definition system.cpp:996
Node(std::string name, std::shared_ptr< Entity > &entity)
Definition system.cpp:991
Definition system.hpp:292
System(fs::path def_file, fs::path root_dir, fs::path library_path, fs::path local_config_path, fs::path tmp_dir)
Definition system.cpp:1892
void process()
Definition system.cpp:1926
bool with_custom_src_code_paths()
~System()
Definition system.cpp:1914
void set_sdfg(std::string sdfg)
Definition system.cpp:1920
option_type
Definition hw.h:59
unsigned int amod_t
Definition hw.h:115
Definition output.hpp:12
Definition system.hpp:94
std::string help
Definition system.hpp:95
option_type array_type
Definition system.hpp:97
unsigned int default_array_value_size
Definition system.hpp:100
void * default_value
Definition system.hpp:98
option_type type
Definition system.hpp:96
void * default_array_value
Definition system.hpp:99
Definition hw.h:102