44 return [
pot = std::move(
pot)](
long nAtoms,
const double *positions,
45 const int *atomicNrs,
double *forces,
46 double *energy,
const double *box) {
47 double variance = 0.0;
48 pot->force(nAtoms, positions, atomicNrs, forces, energy, &variance, box);
62 std::string(magic_enum::enum_name(pot_type)));
68 std::string(magic_enum::enum_name(pot_type)));
72 auto callback = makeForceCallback(std::move(eon_pot));
84 if (endpoints.empty()) {
90 if (endpoints.size() == 1) {
91 auto params = base_params;
93 endpoints[0].potential;
94 serveMode(params, endpoints[0].host, endpoints[0].port);
99 EONC_LOG_INFO(
"Starting {} concurrent RPC servers", endpoints.size());
101 std::vector<std::thread> threads;
102 threads.reserve(endpoints.size());
104 for (
const auto &ep : endpoints) {
105 threads.emplace_back([&base_params, ep]() {
106 auto params = base_params;
108 auto pot_name = std::string(magic_enum::enum_name(ep.potential));
110 EONC_LOG_INFO(
"[{}:{}] Creating potential: {}", ep.host, ep.port,
121 auto callback = makeForceCallback(std::move(eon_pot));
127 for (
auto &t : threads) {
139 uint16_t base_port,
size_t replicas) {
149 EONC_LOG_INFO(
"Starting {} replicated servers on ports {}-{}", replicas,
150 base_port, base_port + replicas - 1);
152 std::vector<std::thread> threads;
153 threads.reserve(replicas);
155 for (
size_t i = 0; i < replicas; ++i) {
156 uint16_t port =
static_cast<uint16_t
>(base_port + i);
157 threads.emplace_back(
158 [¶ms, &host, port]() {
serveMode(params, host, port); });
161 for (
auto &t : threads) {
173 uint16_t port,
size_t pool_size) {
174 if (pool_size == 0) {
180 EONC_LOG_INFO(
"Creating pool of {} {} instances for gateway on {}:{}",
181 pool_size, std::string(magic_enum::enum_name(pot_type)), host,
184 std::vector<ForceCallback> pool;
185 pool.reserve(pool_size);
187 for (
size_t i = 0; i < pool_size; ++i) {
191 EONC_LOG_ERROR(
"Failed to create potential instance {}/{}", i + 1,
195 pool.push_back(makeForceCallback(std::move(eon_pot)));
210 if (!opts.endpoints.empty()) {
212 if (endpoints.empty()) {
221 if (opts.gateway_port > 0) {
222 size_t pool = (opts.replicas > 0) ? opts.replicas : 1;
223 serveGateway(params, opts.host, opts.gateway_port, pool);
236 std::vector<ServeEndpoint> endpoints;
237 std::istringstream stream(spec);
240 while (std::getline(stream, token,
',')) {
242 token.erase(0, token.find_first_not_of(
" \t"));
243 token.erase(token.find_last_not_of(
" \t") + 1);
248 size_t first_colon = token.find(
':');
249 if (first_colon == std::string::npos) {
250 EONC_LOG_ERROR(
"Invalid serve spec '{}': expected 'potential:port'",
255 std::string pot_str = token.substr(0, first_colon);
256 std::string rest = token.substr(first_colon + 1);
259 pot_str.erase(0, pot_str.find_first_not_of(
" \t"));
260 pot_str.erase(pot_str.find_last_not_of(
" \t") + 1);
261 rest.erase(0, rest.find_first_not_of(
" \t"));
262 rest.erase(rest.find_last_not_of(
" \t") + 1);
265 std::ranges::transform(pot_str, pot_str.begin(), [](
unsigned char c) {
266 return static_cast<char>(std::tolower(c));
271 magic_enum::enum_cast<PotType>(pot_str, magic_enum::case_insensitive)
279 size_t second_colon = rest.find(
':');
280 if (second_colon != std::string::npos) {
282 ep.
host = rest.substr(0, second_colon);
283 ep.
host.erase(0, ep.
host.find_first_not_of(
" \t"));
284 ep.
host.erase(ep.
host.find_last_not_of(
" \t") + 1);
285 std::string port_str = rest.substr(second_colon + 1);
286 port_str.erase(0, port_str.find_first_not_of(
" \t"));
287 port_str.erase(port_str.find_last_not_of(
" \t") + 1);
288 ep.
port =
static_cast<uint16_t
>(std::stoi(port_str));
291 ep.
host =
"localhost";
292 ep.
port =
static_cast<uint16_t
>(std::stoi(rest));
298 endpoints.push_back(ep);
#define EONC_LOG_ERROR(...)
#define EONC_LOG_INFO(...)
const potential_options_t & potential_options() const
const serve_options_t & serve_options() const
std::unique_ptr< Potential > makePotential(const Parameters ¶ms)
Exclusive ownership.
std::shared_ptr< Potential > sharePotential(std::unique_ptr< Potential > pot)
Multi-owner boundary: one instance observed by several Matters, images, or a binding.
quill::Logger * get() noexcept
Get or create the default "combi" logger.
RAII resource manager for the ARTn C library with global synchronization.
std::vector< ServeEndpoint > parseServeSpec(const std::string &spec)
Parse a serve configuration string into endpoints.
void serveGateway(const Parameters ¶ms, const std::string &host, uint16_t port, size_t pool_size)
Start a gateway server backed by a pool of potential instances.
void serveMode(const Parameters ¶ms, const std::string &host, uint16_t port)
Start a single rgpot-compatible Cap'n Proto RPC server.
void serveFromConfig(const Parameters ¶ms)
Start serve mode from config-file parameters.
std::function< void(long nAtoms, const double *positions, const int *atomicNrs, double *forces, double *energy, const double *box)> ForceCallback
Callback type for potential energy/force evaluation.
void startPooledRpcServer(std::vector< ForceCallback > pool, const std::string &host, uint16_t port)
Start a blocking Cap'n Proto RPC server backed by a pool of force callbacks dispatched round-robin.
void serveMultiple(const std::vector< ServeEndpoint > &endpoints, const Parameters &base_params)
Serve multiple potentials concurrently on different ports.
void serveReplicated(const Parameters ¶ms, const std::string &host, uint16_t base_port, size_t replicas)
Serve N replicas of the same potential across sequential ports.
void startRpcServer(ForceCallback callback, const std::string &host, uint16_t port)
Start a blocking Cap'n Proto RPC server using a force callback.
static potential_options_t & potential_options(Parameters &p)
Configuration for a single serve endpoint.