include/pops/runtime/builders/compiled/native_loader.hpp Source File¶
|
adc_cpp 0.3.0
Model-free C++23 core for coupled hyperbolic-elliptic systems on adaptive (AMR) meshes, with MPI and GPU (Kokkos) backends
|
native_loader.hpp
Go to the documentation of this file.
5#include <pops/core/state/variables.hpp> // VariableSet + VariableKind + VariableRole + role_from_name
9#include <pops/mesh/boundary/physical_bc.hpp> // AuxHaloPolicy (ADC-369: per-field aux halo tail marshaling)
11#include <pops/runtime/dynamic/dynamic_model.hpp> // IModel: model loaded at runtime (dynamic block)
13#include <pops/runtime/system.hpp> // pops::System (install_block / grid_context / ensure_aux_width)
18#include <pops/runtime/dynamic/dynlib.hpp> // portable dlopen<->LoadLibraryW layer (ADC-99); includes <dlfcn.h> on POSIX
226 auto roles_fn = reinterpret_cast<const char* (*)()>(pops::dynlib::sym(h, "pops_compiled_roles"));
273 // physics. Without this guard, copy_state(U) / write_state(R) would dereference a nonexistent fab(0).
282 // Same MPI guard: empty rank -> local speed 0 (the downstream all_reduce_max takes the global max,
338 // array and treats it as replicated. On a rank without a local box (System distributes ONE box,
359 // gamma 1.4). PRIORITY to the explicit name (names=), then meta, then fallback; roles + primitive
394 // the virtual interface IModel<NV> (ModelAdapter delegates to M.to_primitive/to_conservative when
419void add_dynamic_block(System* self, ImplT* P, const std::string& name, const std::string& so_path,
468void add_compiled_block(System* self, ImplT* P, const std::string& name, const std::string& so_path,
477 // NB: the AOT path (.so) marshals time->imex without an explicit RK scheme: only SSPRK2 is wired
479 // advance would remain SSPRK2, silently) -> we reject them; exposing them would require extending the
480 // ABI of the .so (out of scope ADC-174). They are carried by the native add_block AND backend='production'
488 // .so allocates block_n_ghost(limiter) (compiled_block_abi.hpp), 3 for weno5, so assemble_rhs does not read
499 // extern "C" ABI of the compiled block (compiled_block_abi.hpp). The .so runs the production path
521 // Width of the aux channel that the compiled model READS (B_z, T_e...). Symmetric to the JIT path
522 // (IModel::n_aux). Optional: an old .so without this symbol falls back on the base contract (3).
527 // generated before this work (or a model without runtime params) does not expose them / declares nparams=0 ->
542 auto max_p_fn = reinterpret_cast<max_p_fn_t>(pops::dynlib::sym(h, "pops_compiled_max_speed_p"));
543 auto poi_p_fn = reinterpret_cast<poi_p_fn_t>(pops::dynlib::sym(h, "pops_compiled_poisson_rhs_p"));
545 // (set_block_params writes there -> the closures see the new value at the next step). Empty if the
550 // POSITIVITY LIMITER (ADC-76): pos_floor > 0 goes through the `_p` variants (the only signatures
563 } // registration in P->block_params_ DEFERRED to just before push_back (after the validations that
566 // from an old .so -> empty meta, we fall back on the fallback (names u0.. / no roles / gamma 1.4).
579 std::function<void(MultiFab&, MultiFab&)> rhs_into = [P, lib, res_fn, res_p_fn, pv, nv, naux, n,
613 std::function<Real(const MultiFab&)> max_speed = [P, lib, max_fn, max_p_fn, pv, nv, naux, n, dx,
615 // Same MPI guard: empty rank -> local speed 0. The downstream all_reduce_max takes the global max.
623 std::function<void(const MultiFab&, MultiFab&)> add_poisson = [P, lib, poi_fn, poi_p_fn, pv, nv,
627 // rank without a local box (local_size()==0 at np>1) there is nothing to marshal -> we skip, the
645 // Variable descriptors: PRIORITY to the explicit name passed by the caller (names=), otherwise to the
646 // NAMES carried by the extended ABI of the .so (meta), otherwise fallback u0.. . The ROLES and the PRIMITIVE do not
647 // transit EXCEPT through the ABI (the API has no way to provide them): we take them from meta as-is.
664 // gamma: carried by the ABI if the model declares it (pops_compiled_gamma), otherwise historical default 1.4.
679 // cons <-> prim conversions OF THE MODEL via the extended ABI of the .so (set/get_primitive_state). The
682 // OPTIONAL: a .so generated before this work does not expose them -> empty conversion -> identity (the
688 // conversion can read a runtime param). We then prefer the `_p` variants with the SHARED block.
692 auto c2p_p_fn = reinterpret_cast<cv_p_fn_t>(pops::dynlib::sym(h, "pops_compiled_to_primitive_p"));
706 // (pops_compiled_has_projection / pops_compiled_project_p). Un .so genere avant ce chantier ne les
708 // fonction de projection = .so incoherent -> erreur EXPLICITE plutot qu'un hook silencieusement
709 // ignore. Le stepper applique la fermeture a la FIN de chaque macro-pas entier (cf. SystemStepper).
710 auto has_proj_fn = reinterpret_cast<nv_fn_t>(pops::dynlib::sym(h, "pops_compiled_has_projection"));
714 // Pas de close(h) ici : le shared_ptr `lib` possede deja le handle (fermeture a sa destruction).
743void add_native_block(System* self, ImplT* P, const std::string& name, const std::string& so_path,
752 // UPFRONT validation of the scheme (like add_block / add_compiled_block): add_compiled_model interprets
754 // method = ssprk3 | euler | ssprk2 according to time; an unknown string would fall back SILENTLY on
756 // scheme different from the one requested. "ssprk3" (order 3, to pair with weno5) and "euler" (ForwardEuler,
757 // order 1: fidelity to first-order references, validation -- ADC-174) are ACCEPTED: the template
758 // add_compiled_model marshals them down to make_block of the .so, like the native path add_block. limiter/riemann are validated by make_block in the loader (clear
761 throw std::runtime_error("System::add_native_block: recon 'conservative' | 'primitive' (got '" +
771 // add_compiled_model which, after install_block, reallocates the block state to block_n_ghost(limiter) (3
772 // for weno5) -- SAME mechanism as add_block. assemble_rhs thus does not read out of bounds. limiter is
774 // "production" path of the DSL: the generated .so loader (emit_cpp_native_loader) inlines the header
776 // System (grid_context) and installs the block via install_block -- NATIVE path, zero-copy, SAME
781 // the global scope and the loader could not resolve them. We thus PROMOTE the current module to
783 // flags of the already-loaded object). The module path is found by dladdr on an exported symbol
788 // kokkoscore.lib (Kokkos runtime SHARED as a DLL). Its undefineds are thus resolved by the OS loader
816 install(static_cast<void*>(self), name.c_str(), limiter.c_str(), riemann.c_str(), recon.c_str(),
819 // .dll left loaded for the duration of the process (the block points to code that lives in it).
827 // its undefineds (the exported System methods POPS_EXPORT) against the already-loaded images. RTLD_NOW:
828 // immediate resolution -> a missing System symbol (module without POPS_EXPORT) fails HERE, not in flight.
858 // Native installer of the loader: reinterpret_cast<System*>(this) then add_compiled_model<ProdModel>.
859 // Scheme (limiter/riemann/recon/time/gamma/substeps) marshaled as flat extern "C" arguments: the
860 // loader reconstructs imex/recon_prim and calls the template. evolve is passed so a fixed-background
872 // ADC-76) as the final flat argument. A loader generated BEFORE this addition has a different C
873 // signature -- it is REJECTED upstream by the ABI key (POPS_HEADER_SIG changes with the headers), never
875 install(static_cast<void*>(self), name.c_str(), limiter.c_str(), riemann.c_str(), recon.c_str(),
ABI key of the pops core: stable string identifying the (compiler, C++ standard, header tree signatur...
BoxArray: the set of boxes tiling a level (disjoint, covering).
Field distributed over a level: decomposition (BoxArray) + distribution (DistributionMapping) + ncomp...
Definition multifab.hpp:33
Coupled multi-species system, composed at runtime from generic bricks.
Definition system.hpp:89
#define POPS_AUX_MARSHAL(name, idx)
TYPE-ERASED model interface: runtime dispatch of a model (via vtable).
PORTABLE dynamic loading: dlopen/dlsym/dlclose (POSIX) <-> LoadLibraryW/ GetProcAddress/FreeLibrary (...
Geometry: index-space (Box2D) <-> Cartesian physical-space mapping; PolarGeometry: SIBLING for a glob...
Block grid context plus closures, shared between System (which installs them) and block_builder....
MultiFab: a field DISTRIBUTED over a level (equivalent of AMReX's MultiFab).
std::string last_error()
Last error message (best-effort, for diagnostics).
Definition dynlib.hpp:87
handle open(const std::string &path)
Opens a dynamic library (path in UTF-8). Returns a null handle on failure.
Definition dynlib.hpp:48
void * sym(handle h, const char *name)
Resolves name in h. Returns nullptr if absent.
Definition dynlib.hpp:62
void add_dynamic_block(System *self, ImplT *P, const std::string &name, const std::string &so_path, int substeps, const std::vector< std::string > &names, const std::string &recon)
Body of System::add_dynamic_block. VERBATIM; self = the calling System, P = self->p_....
Definition native_loader.hpp:419
void add_native_block(System *self, ImplT *P, const std::string &name, const std::string &so_path, const std::string &limiter, const std::string &riemann, const std::string &recon, const std::string &time, double gamma, int substeps, bool evolve, int stride, double pos_floor=0)
Body of System::add_native_block.
Definition native_loader.hpp:743
VariableSet parse_var_set(VariableKind kind, const std::string &names_csv, const std::string &roles_csv)
Builds a VariableSet from the CSV names / roles (parallel to names; EMPTY roles = not provided -> Var...
Definition native_loader.hpp:208
POPS_HD double limited_slope(double am, double ap, int recon)
MUSCL limited slope of a cell from backward difference am and forward ap.
Definition native_loader.hpp:67
void push_dynamic(ImplT *P, const std::string &name, pops::dynlib::handle h, int substeps, std::vector< std::string > names, int recon)
Builds a DYNAMIC block (IModel<NV> model loaded from the .so h) and adds it.
Definition native_loader.hpp:249
std::vector< double > marshal_aux_halo(Impl *P, int naux)
ADC-369: marshal the aux for a COMPILED block – the naux valid components (component-major,...
Definition native_loader.hpp:49
std::vector< std::string > split(const std::string &s, char sep)
Split s on sep (empty fields kept).
Definition native_loader.hpp:181
std::vector< double > host_residual(const IModel< NV > &m, const std::vector< double > &U, const std::vector< double > &AUX, int n, double dx, int recon)
Host residual R = -div F* + S(U, aux) (Rusanov, GLOBAL a_max like pops.PythonFlux,...
Definition native_loader.hpp:90
void add_compiled_block(System *self, ImplT *P, const std::string &name, const std::string &so_path, const std::string &limiter, const std::string &riemann, const std::string &recon, const std::string &time, int substeps, const std::vector< std::string > &names, double pos_floor=0)
Body of System::add_compiled_block. VERBATIM; self = the calling System, P = self->p_....
Definition native_loader.hpp:468
BlockMeta read_block_meta(pops::dynlib::handle h)
Reads (by dlsym, all OPTIONAL) the metadata symbols of an already-open .so (h).
Definition native_loader.hpp:222
Definition amr_hierarchy.hpp:29
VariableKind
Kind of a variable set: conserved (U) or primitive (W).
Definition variables.hpp:22
@ Primitive
@ Conservative
std::string roles_csv(const VariableSet &vs)
CSV of a VariableSet's roles (role_name, separator ',').
Definition variables.hpp:172
void parse_roles_into(VariableSet &vs, const std::string &csv)
Inverse of roles_csv: fill vs.roles (and vs.user_roles for any NON-canonical token) from a roles CSV.
Definition variables.hpp:190
std::string names_csv(const VariableSet &vs)
CSV of a VariableSet's names (separator ',').
Definition variables.hpp:158
PHYSICAL boundary conditions at the domain edge (BCType, BCRec, fill_physical_bc, fill_ghosts).
Pointwise types of the physics layer: StateVec<N> (conserved state) and Aux (auxiliary fields from th...
#define POPS_AUX_FIELDS(X)
SINGLE SOURCE of the layout of the EXTRA aux fields (X-macro).
Definition state.hpp:92
WRITE POD handle (raw pointer + strides) over a Fab2D buffer, indexed by (i, j, c) IN GLOBAL INDICES ...
Definition fab2d.hpp:29
POINTWISE auxiliary fields shared with the physics: single coupling channel.
Definition state.hpp:123
Hyperbolic model seen behind a virtual interface (runtime dispatch).
Definition dynamic_model.hpp:27
virtual State source(const State &, const Aux &) const
Source term S(U, aux) (default: zero, model without source).
Definition dynamic_model.hpp:35
virtual Real max_wave_speed(const State &u, const Aux &a, int dir) const =0
virtual State flux(const State &u, const Aux &a, int dir) const =0
virtual State to_primitive(const State &u) const
Conservative -> primitive conversion (P = M.to_primitive(U)).
Definition dynamic_model.hpp:42
A model's variable set: kind (cons/prim), names, size, canonical roles (optional, parallel to names; ...
Definition variables.hpp:58
OPTIONAL metadata read by dlsym on a generated .so (names / roles / gamma).
Definition native_loader.hpp:199
Runtime multi-species composition: one coupled system, block by block.
Base scalar types and the POPS_HD macro (host+device portability).
Descriptor of a model's variables (Vars).
Generated by