Architecture¶
This page explains how the layers of rgpot fit together and why errors and
capabilities cross them the way they do.
Why the wire uses three carriers is in The RPC Model.
The directory layout and the list of potentials are in
Architecture reference.
The C entry points are in C ABI of rgpot-core,
and the C++ wrappers in C++ API.
rgpot follows a metatensor-style layout.
A Rust crate, rgpot-core, defines the data exchange types and the RPC client
and server.
It exports them through a C ABI whose header cbindgen generates.
Hand-written C++ headers wrap that ABI in move-only handles.
The C++ potentials in CppCore/rgpot/ plug into the Rust core through a
callback.
Layer diagram¶
+------------------------------------------------+
| C++ wrappers (move-only handles) |
| include/rgpot/*.hpp |
+------------------------------------------------+
| C ABI (generated by cbindgen) |
| rgpot-core/include/rgpot.h |
+------------------------------------------------+
| Rust core |
| rgpot-core/src/ |
| - DLPack input and output structs |
| - callback dispatch (rgpot_potential_t) |
| - Hessian-vector products |
| - status codes, thread-local message |
| - capability check (schema feature) |
| - profile loader (profile feature) |
| - Cap'n Proto RPC client and server (rpc) |
+------------------------------------------------+
| callbacks
+------------------------------------------------+
| C++ potentials, CppCore/rgpot/<Name>/ |
| classes derived from rgpot::Potential<Derived>|
+------------------------------------------------+
Layers¶
Rust core (rgpot-core/)¶
The crate owns the public C types.
Every exported struct is #[repr(C)] and every exported function is
extern "C".
rgpot_force_input_tandrgpot_force_out_tthe data exchange structs. The input holds three borrowed DLPack tensors: positions, atomic numbers and the box matrix. The output holds a forces tensor that the callback allocates and the caller frees with
rgpot_tensor_free, plus the energy and the variance as plain doubles. DLPack keeps the structs independent of the device that holds the data.rgpot_status_tthe status codes
RGPOT_SUCCESS,RGPOT_INVALID_PARAMETER,RGPOT_INTERNAL_ERROR,RGPOT_RPC_ERRORandRGPOT_BUFFER_SIZE_ERROR. Calls that can fail return one of them. Constructors such asrgpot_potential_new_eindirandrgpot_rpc_client_newreturn a pointer instead, and NULL on failure.rgpot_potential_ta
#[repr(C)]struct whose first member is aneindir_objective_t, so a pointer to it is also a pointer to an eindir objective. It carries the callback, itsvoid*user data and an optional destructor for that data. The core calls the callback without knowing the concrete C++ type behind it. Callers create it withrgpot_potential_neworrgpot_potential_new_eindirand release it withrgpot_potential_free.rgpot_last_errorreturns the thread-local message of the last failure. The pointer stays valid until the next
rgpot_*call on the same thread.
Feature gates¶
rgpot-core/Cargo.toml keeps the default feature set empty.
schemacompiles
Potentials.capnpwithcapnpcand adds the capability check andrgpot_potential_set_peer_capabilities.profileenables
schemaand adds the profile loader (ProfileSession), which opens a backend library withlibloading.rpcenables
schemaand adds the Cap’n Proto RPC client and server, withcapnp-rpc,tokio,tokio-utilandfutures.cachedeclared with no code behind it; the Meson option
-Dwith_cache=truepasses it to Cargo.gen-headerregenerates
rgpot-core/include/rgpot.hwithcbindgen;pixi r gen-headerrunscargo build --features gen-header.
The Meson build compiles the crate when -Dwith_rust_core=true is set.
It passes rpc when RPC is enabled and cache when -Dwith_cache=true is set.
The schema copy¶
The crate bundles rgpot-core/schema/Potentials.capnp, so a build from
crates.io has the schema.
build.rs prefers that copy and falls back to
CppCore/rgpot/rpc/Potentials.capnp when it is missing.
The Cap’n Proto wire format does not depend on the language, so a Rust client
can call a C++ server and the reverse.
C ABI (rgpot-core/include/rgpot.h)¶
The header is the contract between the Rust core and every C or C++ consumer.
cbindgen generates it from the Rust source, so nobody edits it by hand.
A change to the public API is made in the Rust source and the header is
regenerated.
The header holds the #[repr(C)] structs and enums, the function declarations,
and the Rust doc comments as Doxygen comments.
rgpot_fused_cache_t and rgpot_rpc_client_t are opaque; rgpot_potential_t
is a full struct.
Declarations that need the rpc feature sit inside
#if defined(RGPOT_HAS_RPC).
C++ wrappers (include/rgpot/)¶
The headers give C++ callers handles that free themselves and exceptions in place of status codes.
errors.hpprgpot::Error, astd::runtime_error, andrgpot::detail::check_status.check_statusreadsrgpot_last_errorand throwsrgpot::Errorwith a prefix that names the status category.types.hpprgpot::InputSpecwraps caller buffers in non-owning DLPack tensors, andrgpot::CalcResultowns the forces tensor that a calculation returns. The names differ fromrgpot::ForceInputandrgpot::ForceOutinForceStructs.hpp, so both sets can appear in one translation unit.potential.hpprgpot::PotentialHandle, a move-only owner of onergpot_potential_t.from_implwraps any object with aforceImpl(const ForceInput&, ForceOut*)method through a template trampoline.from_callbacktakes a C function pointer, its user data and an optional destructor.calculateruns the potential and returns aCalcResult.rpc_client.hpprgpot::RpcClient, a move-only owner of onergpot_rpc_client_t, with the samecalculateinterface. The constructor does not connect; the Rust client opens a connection for eachcalculate. The header compiles only whenRGPOT_HAS_RPCis defined.rgpot.hppincludes the headers above, and
rpc_client.hppwhenRGPOT_HAS_RPCis defined.
C++ potentials (CppCore/rgpot/)¶
Each potential derives from the CRTP template rgpot::Potential<Derived> and
implements forceImpl(const ForceInput&, ForceOut*) const.
rgpot::PotentialHandle::from_impl turns such an object into an rgpot_potential_t
without copying it.
The caller keeps the object alive for as long as the handle exists.
Error handling¶
Errors change form at each boundary, because each language has its own convention and unwinding across a C boundary is not allowed.
Inside Rust, code uses
Result, and theextern "C"entry points turn errors into status codes.Across the C ABI, a call returns a status code, or NULL for a constructor, and stores a message that
rgpot_last_errorreturns. No exception or panic crosses the boundary.rgpot_potential_calculate,rgpot_potential_hvp,rgpot_potential_hvp_centre,rgpot_rpc_calculateandrgpot_rpc_server_startrun their body insidecatch_unwind, which turns a panic intoRGPOT_INTERNAL_ERRORwith the panic message. These are the entry points that run a user callback or the RPC runtime.Inside C++, the wrappers pass every status to
rgpot::detail::check_status, which throwsrgpot::Error. In the other direction, thefrom_impltrampoline catches any exception fromforceImpland returnsRGPOT_INTERNAL_ERROR. It does not store the exception text, so the message thatrgpot_last_errorreturns afterwards does not describe that failure.
Backend capability handshake¶
A backend can be incompatible with the host in ways a single call does not show: another protocol, another schema, or another eindir bridge layout. The capability handshake refuses such a backend before any work is dispatched. It runs where a backend meets the host: when a library is loaded through the potential profile, when an RPC client connects to a server, and when a callback handle receives its peer’s capabilities. The fields it compares, the rules for unset metadata, and the error each path returns are in Architecture reference, under Capability handshake.