![]() |
Shamrock 2025.10.0
Astrophysical Code
|
Drop-in replacement for the hand-rolled "global variable + enum + name mapping. More...
#include <shamalgs/include/shamalgs/ImplVariant.hpp>
Public Types | |
| using | Variant = std::variant<Alts...> |
| using | AutoselectFn = std::function<void(const sham::DeviceScheduler_ptr &, ImplVariantGlobal &)> |
| Callable selecting the default implementation, by calling set() on the selector. | |
Public Member Functions | |
| ImplVariantGlobal (AutoselectFn fn) | |
| Construct an unset selector, whose default implementation is picked by fn. | |
| ImplVariantGlobal (const ImplVariantGlobal &)=delete | |
| ImplVariantGlobal & | operator= (const ImplVariantGlobal &)=delete |
| ImplVariantGlobal (ImplVariantGlobal &&)=delete | |
| ImplVariantGlobal & | operator= (ImplVariantGlobal &&)=delete |
| bool | is_set () const override |
| Whether an implementation has been selected yet. | |
| void | autoselect (const sham::DeviceScheduler_ptr &sched) override |
| Select the algorithm's default implementation for the device behind sched. | |
| const Variant & | get () const |
| Get the currently selected implementation. Requires is_set(). | |
| std::string | get_current_config () const override |
| std::vector< std::string > | get_default_config_list () const override |
| void | set (Variant v) |
| Directly select an alternative (e.g. to seed a default at the call site). | |
| void | set (std::string_view config_json) override |
| Select an implementation from a {"implementation": ..., "parameters": ...} json string. | |
Drop-in replacement for the hand-rolled "global variable + enum + name mapping.
json string ABI.
Each instance is registered by name in shamalgs::impl_registry (see impl_registry.hpp), which exposes every algorithm's selection through that interface, so no per-algorithm free function or Python binding is needed.
No implementation is selected at construction: is_set() reports whether one has been picked yet. The rule picking the algorithm's default is however given at construction, as a callable of signature void(const sham::DeviceScheduler_ptr &, ImplVariantGlobal &) that calls set() on the selector it is handed. autoselect(sched) runs it, so that the default may depend on the device behind sched (see e.g. compute_histogram.cpp), or ignore it when it is a compile-time choice:
Call sites typically check is_set() and, when unset, call shamalgs::impl_registry::autoselect_impl right before dispatching (see e.g. segmented_sort_in_place.cpp). get() assumes is_set(); get_current_config() is the one exception and safely returns a json null instead of dereferencing an unset selection.
Instances are registered by name in shamalgs::impl_registry (which stores their address), so they are neither copyable nor movable.
| Alts | the alternative types, each requiring a static constexpr std::string_view variant_type_name |
Definition at line 272 of file ImplVariant.hpp.
| using shamalgs::ImplVariantGlobal< Alts >::AutoselectFn = std::function<void(const sham::DeviceScheduler_ptr &, ImplVariantGlobal &)> |
Callable selecting the default implementation, by calling set() on the selector.
Definition at line 277 of file ImplVariant.hpp.
| using shamalgs::ImplVariantGlobal< Alts >::Variant = std::variant<Alts...> |
Definition at line 274 of file ImplVariant.hpp.
|
inlineexplicit |
Construct an unset selector, whose default implementation is picked by fn.
Definition at line 280 of file ImplVariant.hpp.
|
inlineoverridevirtual |
Select the algorithm's default implementation for the device behind sched.
Implements shamalgs::IImplVariant.
Definition at line 297 of file ImplVariant.hpp.
|
inline |
Get the currently selected implementation. Requires is_set().
Definition at line 302 of file ImplVariant.hpp.
|
inlineoverridevirtual |
Get the currently selected implementation as a single config json string, or a json null if no implementation has been selected yet (see is_set())
Implements shamalgs::IImplVariant.
Definition at line 306 of file ImplVariant.hpp.
|
inlineoverridevirtual |
List the available implementations as config json strings. Most alternatives contribute exactly one entry; alternatives opting into HasCustomDefaults contribute one entry per instance in their variant_custom_defaults() list.
Implements shamalgs::IImplVariant.
Definition at line 316 of file ImplVariant.hpp.
|
inlineoverridevirtual |
Whether an implementation has been selected yet.
Implements shamalgs::IImplVariant.
Definition at line 294 of file ImplVariant.hpp.
|
inlineoverridevirtual |
Select an implementation from a {"implementation": ..., "parameters": ...} json string.
Implements shamalgs::IImplVariant.
Definition at line 324 of file ImplVariant.hpp.
|
inline |
Directly select an alternative (e.g. to seed a default at the call site).
Definition at line 321 of file ImplVariant.hpp.