Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
ImplControl.hpp
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
10#pragma once
11
20#include "shamcomm/logs.hpp"
21#include <string>
22
25 public:
26 virtual ~ImplControl() = default;
27
28 // public API
29 inline std::string get_alg_name() const { return impl_get_alg_name(); }
30
31 inline bool was_configured(const sham::DeviceScheduler_ptr &dev_sched) const {
32 return impl_was_configured(dev_sched);
33 }
34
35 inline void ensure_init(const sham::DeviceScheduler_ptr &dev_sched) {
36 if (!impl_was_configured(dev_sched)) {
37 set_config(dev_sched, get_default_config(dev_sched));
38 }
39 }
40
41 inline std::string get_config(const sham::DeviceScheduler_ptr &dev_sched) {
42 ensure_init(dev_sched);
43 return impl_get_config(dev_sched);
44 }
45
46 inline void set_config(const sham::DeviceScheduler_ptr &dev_sched, const std::string &cfg) {
47 logger::info_ln(
48 "Algs",
49 shambase::format(
50 "switching config for alg {} to cfg={}", impl_get_alg_name(), cfg));
51 impl_set_config(dev_sched, cfg);
52 }
53
54 inline std::string get_default_config(const sham::DeviceScheduler_ptr &dev_sched) {
55 if (auto cfg = impl_autotune(dev_sched)) {
56 return *cfg;
57 } else {
58 return impl_get_default_config(dev_sched);
59 }
60 }
61
62 inline std::vector<std::string> get_avail_configs(
63 const sham::DeviceScheduler_ptr &dev_sched) {
64 return impl_get_avail_configs(dev_sched);
65 };
66
67 protected:
68 // required overrides
69 virtual std::string impl_get_alg_name() const = 0;
70 virtual bool impl_was_configured(const sham::DeviceScheduler_ptr &) const = 0;
71 virtual std::string impl_get_config(const sham::DeviceScheduler_ptr &) const = 0;
72 virtual std::string impl_get_default_config(const sham::DeviceScheduler_ptr &) const = 0;
73 virtual void impl_set_config(const sham::DeviceScheduler_ptr &, const std::string &cfg) = 0;
74 virtual std::vector<std::string> impl_get_avail_configs(const sham::DeviceScheduler_ptr &)
75 = 0;
76
77 // optional override
78 inline virtual std::optional<std::string> impl_autotune(const sham::DeviceScheduler_ptr &) {
79 logger::info_ln("Algs", "no autotuning registered for", impl_get_alg_name());
80 return std::nullopt;
81 }
82 };
83} // namespace shamalgs::primitives
namespace for primitive algorithm (e.g. sort, scan, reductions, ...)