Loading...
Searching...
No Matches
ServeMode.cpp
Go to the documentation of this file.
1/*
2** This file is part of eOn.
3**
4** SPDX-License-Identifier: BSD-3-Clause
5**
6** Copyright (c) 2010--present, eOn Development Team
7** All rights reserved.
8**
9** Repo:
10** https://github.com/TheochemUI/eOn
11*/
12
26
27#include "eon/ServeMode.h"
28#include "eon/EonLogger.h"
29#include "eon/Potential.h"
30#include "eon/ServeRpcServer.h"
31
32#include <algorithm>
33#include <cctype>
34#include <ranges>
35#include <sstream>
36#include <thread>
37#include <vector>
38
39namespace eonc {
40namespace {
41
43ForceCallback makeForceCallback(std::shared_ptr<Potential> pot) {
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);
49 };
50}
51
52} // anonymous namespace
53
54// ---------------------------------------------------------------------------
55// Single-model serve
56// ---------------------------------------------------------------------------
57
58void serveMode(const Parameters &params, const std::string &host,
59 uint16_t port) {
60 auto pot_type = params.potential_options().potential;
61 EONC_LOG_INFO("Creating potential: {}",
62 std::string(magic_enum::enum_name(pot_type)));
63
64 auto eon_pot =
66 if (!eon_pot) {
67 EONC_LOG_ERROR("Failed to create potential of type {}",
68 std::string(magic_enum::enum_name(pot_type)));
69 return;
70 }
71
72 auto callback = makeForceCallback(std::move(eon_pot));
73
74 // Blocks until killed (runs Cap'n Proto event loop)
75 startRpcServer(std::move(callback), host, port);
76}
77
78// ---------------------------------------------------------------------------
79// Multi-model concurrent serve
80// ---------------------------------------------------------------------------
81
82void serveMultiple(const std::vector<ServeEndpoint> &endpoints,
83 const Parameters &base_params) {
84 if (endpoints.empty()) {
85 EONC_LOG_ERROR("No serve endpoints specified");
86 return;
87 }
88
89 // Single endpoint: run in the main thread (no extra overhead)
90 if (endpoints.size() == 1) {
91 auto params = base_params;
93 endpoints[0].potential;
94 serveMode(params, endpoints[0].host, endpoints[0].port);
95 return;
96 }
97
98 // Multiple endpoints: one thread per server
99 EONC_LOG_INFO("Starting {} concurrent RPC servers", endpoints.size());
100
101 std::vector<std::thread> threads;
102 threads.reserve(endpoints.size());
103
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));
109
110 EONC_LOG_INFO("[{}:{}] Creating potential: {}", ep.host, ep.port,
111 pot_name);
112
113 auto eon_pot =
115 if (!eon_pot) {
116 EONC_LOG_ERROR("[{}:{}] Failed to create potential {}", ep.host,
117 ep.port, pot_name);
118 return;
119 }
120
121 auto callback = makeForceCallback(std::move(eon_pot));
122 startRpcServer(std::move(callback), ep.host, ep.port);
123 });
124 }
125
126 // Wait for all threads (they block until killed)
127 for (auto &t : threads) {
128 if (t.joinable()) {
129 t.join();
130 }
131 }
132}
133
134// ---------------------------------------------------------------------------
135// Replicated serve: N copies of same potential on sequential ports
136// ---------------------------------------------------------------------------
137
138void serveReplicated(const Parameters &params, const std::string &host,
139 uint16_t base_port, size_t replicas) {
140 if (replicas == 0) {
141 EONC_LOG_ERROR("Replicas must be >= 1");
142 return;
143 }
144 if (replicas == 1) {
145 serveMode(params, host, base_port);
146 return;
147 }
148
149 EONC_LOG_INFO("Starting {} replicated servers on ports {}-{}", replicas,
150 base_port, base_port + replicas - 1);
151
152 std::vector<std::thread> threads;
153 threads.reserve(replicas);
154
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 [&params, &host, port]() { serveMode(params, host, port); });
159 }
160
161 for (auto &t : threads) {
162 if (t.joinable()) {
163 t.join();
164 }
165 }
166}
167
168// ---------------------------------------------------------------------------
169// Gateway serve: single port backed by a pool of potential instances
170// ---------------------------------------------------------------------------
171
172void serveGateway(const Parameters &params, const std::string &host,
173 uint16_t port, size_t pool_size) {
174 if (pool_size == 0) {
175 EONC_LOG_ERROR("Pool size must be >= 1");
176 return;
177 }
178
179 auto pot_type = params.potential_options().potential;
180 EONC_LOG_INFO("Creating pool of {} {} instances for gateway on {}:{}",
181 pool_size, std::string(magic_enum::enum_name(pot_type)), host,
182 port);
183
184 std::vector<ForceCallback> pool;
185 pool.reserve(pool_size);
186
187 for (size_t i = 0; i < pool_size; ++i) {
188 auto eon_pot =
190 if (!eon_pot) {
191 EONC_LOG_ERROR("Failed to create potential instance {}/{}", i + 1,
192 pool_size);
193 return;
194 }
195 pool.push_back(makeForceCallback(std::move(eon_pot)));
196 }
197
198 EONC_LOG_INFO("Pool ready, starting gateway server");
199 startPooledRpcServer(std::move(pool), host, port);
200}
201
202// ---------------------------------------------------------------------------
203// Config-driven dispatch
204// ---------------------------------------------------------------------------
205
206void serveFromConfig(const Parameters &params) {
207 const auto &opts = params.serve_options();
208
209 // Multi-model endpoints take priority
210 if (!opts.endpoints.empty()) {
211 auto endpoints = parseServeSpec(opts.endpoints);
212 if (endpoints.empty()) {
213 EONC_LOG_ERROR("No valid endpoints in spec: {}", opts.endpoints);
214 return;
215 }
216 serveMultiple(endpoints, params);
217 return;
218 }
219
220 // Gateway mode
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);
224 return;
225 }
226
227 // Replicated mode (default)
228 serveReplicated(params, opts.host, opts.port, opts.replicas);
229}
230
231// ---------------------------------------------------------------------------
232// Spec parser: "pot:port,pot:host:port,..."
233// ---------------------------------------------------------------------------
234
235std::vector<ServeEndpoint> parseServeSpec(const std::string &spec) {
236 std::vector<ServeEndpoint> endpoints;
237 std::istringstream stream(spec);
238 std::string token;
239
240 while (std::getline(stream, token, ',')) {
241 // Trim whitespace
242 token.erase(0, token.find_first_not_of(" \t"));
243 token.erase(token.find_last_not_of(" \t") + 1);
244 if (token.empty())
245 continue;
246
247 // Parse "potential:port" or "potential:host:port"
248 size_t first_colon = token.find(':');
249 if (first_colon == std::string::npos) {
250 EONC_LOG_ERROR("Invalid serve spec '{}': expected 'potential:port'",
251 token);
252 continue;
253 }
254
255 std::string pot_str = token.substr(0, first_colon);
256 std::string rest = token.substr(first_colon + 1);
257
258 // Trim parts after colon split
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);
263
264 // Lowercase the potential name
265 std::ranges::transform(pot_str, pot_str.begin(), [](unsigned char c) {
266 return static_cast<char>(std::tolower(c));
267 });
268
269 ServeEndpoint ep;
270 ep.potential =
271 magic_enum::enum_cast<PotType>(pot_str, magic_enum::case_insensitive)
272 .value_or(PotType::UNKNOWN);
273
274 if (ep.potential == PotType::UNKNOWN) {
275 EONC_LOG_ERROR("Unknown potential type '{}'", pot_str);
276 continue;
277 }
278
279 size_t second_colon = rest.find(':');
280 if (second_colon != std::string::npos) {
281 // "host:port" format
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));
289 } else {
290 // "port" only
291 ep.host = "localhost";
292 ep.port = static_cast<uint16_t>(std::stoi(rest));
293 }
294
295 QUILL_LOG_INFO(eonc::log::get(), "Parsed endpoint: {} on {}:{}",
296 std::string(magic_enum::enum_name(ep.potential)), ep.host,
297 ep.port);
298 endpoints.push_back(ep);
299 }
300
301 return endpoints;
302}
303
304} // namespace eonc
#define EONC_LOG_ERROR(...)
Definition EonLogger.h:328
#define EONC_LOG_INFO(...)
Definition EonLogger.h:316
const potential_options_t & potential_options() const
const serve_options_t & serve_options() const
std::unique_ptr< Potential > makePotential(const Parameters &params)
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.
Definition Potential.h:292
quill::Logger * get() noexcept
Get or create the default "combi" logger.
Definition EonLogger.h:111
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 &params, 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 &params, const std::string &host, uint16_t port)
Start a single rgpot-compatible Cap'n Proto RPC server.
Definition ServeMode.cpp:58
void serveFromConfig(const Parameters &params)
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.
Definition ServeMode.cpp:82
void serveReplicated(const Parameters &params, 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.
Definition ServeMode.h:27
std::string host
Definition ServeMode.h:29