Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
impl_registry.cpp
Go to the documentation of this file.
1// -------------------------------------------------------//
2//
3// SHAMROCK code for hydrodynamics
4// Copyright (c) 2021-2026 Timothée David--Cléris <tim.shamrock@proton.me>
5// SPDX-License-Identifier: CeCILL Free Software License Agreement v2.1
6// Shamrock is licensed under the CeCILL 2.1 License, see LICENSE for more information
7//
8// -------------------------------------------------------//
9
15
18#include "shambase/memory.hpp"
19#include "shamcomm/logs.hpp"
21#include <functional>
22#include <map>
23#include <stdexcept>
24#include <utility>
25
26namespace {
27
28 using Registry = std::map<std::string, shamalgs::IImplVariant *, std::less<>>;
29
31 Registry &get_registry() {
32 static Registry registry;
33 return registry;
34 }
35
37 std::string registered_names_str() {
38 std::string ret;
39 for (const auto &[name, impl] : get_registry()) {
40 if (!ret.empty()) {
41 ret += ", ";
42 }
43 ret += name;
44 }
45 return ret;
46 }
47
49 shamalgs::IImplVariant &get_impl(std::string_view alg) {
50 Registry &registry = get_registry();
51 auto it = registry.find(alg);
52 if (it == registry.end()) {
54 "no implementation registered under the name \"" + std::string(alg)
55 + "\", registered names are: [" + registered_names_str() + "]");
56 }
57 return *it->second;
58 }
59
60} // namespace
61
62namespace shamalgs::impl_registry {
63
64 void register_impl(std::string name, IImplVariant &impl) {
65 Registry &registry = get_registry();
66 if (registry.find(name) != registry.end()) {
68 "an implementation is already registered under the name \"" + name + "\"");
69 }
70 registry.emplace(std::move(name), &impl);
71 }
72
73 std::vector<std::string> get_registered_algs() {
74 std::vector<std::string> ret;
75 ret.reserve(get_registry().size());
76 for (const auto &[name, impl] : get_registry()) {
77 ret.push_back(name); // std::map iterates in sorted order
78 }
79 return ret;
80 }
81
82 std::vector<std::string> get_default_impl_list(std::string_view alg) {
83 return get_impl(alg).get_default_config_list();
84 }
85
86 std::string get_current_impl(std::string_view alg) {
87 return get_impl(alg).get_current_config();
88 }
89
90 bool is_impl_set(std::string_view alg) { return get_impl(alg).is_set(); }
91
92 void set_impl(std::string_view alg, std::string_view impl) {
93 get_impl(alg).set(impl);
94 if (shamcomm::world_rank() == 0) {
95 shamlog_info_ln("algs", "setting", alg, "implementation to impl :", impl);
96 }
97 }
98
99 void autoselect_impl(std::string_view alg, const sham::DeviceScheduler_ptr &sched) {
100 IImplVariant &impl = get_impl(alg);
102 impl.autoselect(sched);
103 if (shamcomm::world_rank() == 0) {
104 shamlog_info_ln(
105 "algs", "defaulting", alg, "implementation to impl :", impl.get_current_config());
106 }
107 }
108
109} // namespace shamalgs::impl_registry
Non-template virtual interface exposed by ImplVariantGlobal, for code that needs to hold or pass arou...
constexpr auto registry
This header file contains utility functions related to exception handling in the code.
Name-keyed registry of the implementation selectors (IImplVariant) of every algorithm.
std::vector< std::string > get_registered_algs()
Get the names of every registered algorithm, sorted.
void set_impl(std::string_view alg, std::string_view impl)
Select the implementation of alg from a config json string.
void register_impl(std::string name, IImplVariant &impl)
Register an implementation selector under name.
void autoselect_impl(std::string_view alg, const sham::DeviceScheduler_ptr &sched)
Select the default implementation of alg for the device behind sched.
std::string get_current_impl(std::string_view alg)
Get the current implementation of alg as a config json string, "null" if unset.
bool is_impl_set(std::string_view alg)
Whether an implementation of alg has been selected yet.
std::vector< std::string > get_default_impl_list(std::string_view alg)
Get the list of available implementations of alg, as config json strings.
T & get_check_ref(const std::unique_ptr< T > &ptr, SourceLocation loc=SourceLocation())
Takes a std::unique_ptr and returns a reference to the object it holds. It throws a std::runtime_erro...
Definition memory.hpp:112
ExcptTypes make_except_with_loc(std::string message, SourceLocation loc=SourceLocation{})
Create an exception with a message and a location.
i32 world_rank()
Gives the rank of the current process in the MPI communicator.
Definition worldInfo.cpp:41
Functions related to the MPI communicator.