Loading...
Searching...
No Matches
EnginePotential.h
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// Copyright (c) gpridge developers
3// See LICENSE for details.
4
5#ifndef GPR_POTENTIALS_ENGINEPOTENTIAL_H_
6#define GPR_POTENTIALS_ENGINEPOTENTIAL_H_
7
8#include <cstddef>
9#include <cstdint>
10#include <memory>
11#include <stdexcept>
12#include <string>
13#include <vector>
14
15#include "PotentialWrapper.h"
17#include "rgpot_engine_c_abi.h"
18
19namespace pot {
20
24class EngineError : public std::runtime_error {
25public:
26 EngineError(std::string library, int status, const std::string& what);
27
28 [[nodiscard]] int status() const noexcept
29 {
30 return status_;
31 }
32 [[nodiscard]] bool fatal() const noexcept
33 {
34 return status_ < 0;
35 }
36 [[nodiscard]] const std::string& library() const noexcept
37 {
38 return library_;
39 }
40
41private:
42 std::string library_;
43 int status_;
44};
45
54 unsigned short major = 1;
55 unsigned short minor = 0;
56 unsigned short layout = 1;
57};
58
62struct EngineCaps {
63 int reentrancy = 0;
65 int batched = 0;
66 int periodic = 0;
67 int stress = 0;
70 int orbital_copy = 0;
71 int analytic_hvp = 0;
72 int jet_order = 0;
73};
74
78 int tracked = 0;
79 long long operations = 0;
80 double scale = 0;
81};
82
88public:
97 static std::unique_ptr<EnginePotential> open(
98 const std::string& library, const std::vector<unsigned char>& config,
99 std::string& err, bool torch_cuda = false, int oracle_threads = 1);
100
104
107 int force(long n_atoms, const double* positions, const int* atomic_nrs,
108 const double* box, double* forces, double* energy,
109 double* variance);
110
112 [[nodiscard]] bool hasBatch() const noexcept
113 {
114 return batch_ != nullptr;
115 }
116
119 int forceBatch(long n_systems, long n_atoms, const double* positions,
120 const int* atomic_nrs, const double* boxes, double* forces,
121 double* energies, double* variances);
122
124 [[nodiscard]] bool hasCallbacks() const noexcept
125 {
126 return set_callbacks_ != nullptr;
127 }
128
130 int setCallbacks(const RgpotEngineCallbacks& callbacks);
131
133 [[nodiscard]] bool hasHessianChecked() const noexcept
134 {
135 return hessian_checked_ != nullptr;
136 }
137
139 int hessianChecked(long n_atoms, const double* positions,
140 std::size_t positions_len, const int* atomic_nrs,
141 double* hessian_out, std::size_t hessian_len,
142 const double* box);
143
145 [[nodiscard]] bool hasHessianActionChecked() const noexcept
146 {
147 return hessian_action_checked_ != nullptr;
148 }
149
152 int hessianActionChecked(long n_atoms, const double* positions,
153 std::size_t positions_len, const int* atomic_nrs,
154 const double* direction, std::size_t direction_len,
155 double* hv_out, std::size_t hv_len,
156 const double* box, unsigned long long* ops_out,
157 double* scale_out);
158
161 [[nodiscard]] bool hasPredictionSupportedChecked() const noexcept
162 {
163 return prediction_supported_checked_ != nullptr;
164 }
165
167 int predictionSupportedChecked(long n_atoms, const double* positions,
168 std::size_t positions_len,
169 const int* atomic_nrs, const double* box,
170 int* supported_out);
171
173 [[nodiscard]] bool hasGprStats() const noexcept
174 {
175 return gpr_stats_ != nullptr;
176 }
177
179 int gprStats(RgpotEngineGprStats& stats_out) const;
180
182 [[nodiscard]] unsigned long long modelState() const;
183
186 int hint(const std::vector<unsigned char>& message, std::string& err);
187
189 [[nodiscard]] EngineAbiStamp abiStamp() const noexcept
190 {
191 return stamp_;
192 }
193
196 int caps(EngineCaps& out, std::string& err);
197
201 int hvp(long n_atoms, const double* positions, const int* atomic_nrs,
202 const double* direction, double* hv_out, const double* box,
203 std::string& err);
204
211 int taylorJet(long n_systems, long n_atoms, const double* positions,
212 const int* atomic_nrs, const double* directions, long order,
213 double* jet_out, EngineDerivativeReport* report,
214 const double* boxes, std::string& err);
215
218 int setCache(const char* path, std::string& err);
219
222 int cacheCounts(std::uint64_t& computed, std::uint64_t& served,
223 std::string& err);
224
227 [[nodiscard]] std::string lastError() const;
228
231 [[nodiscard]] bool unloadSafe() const;
232
233 [[nodiscard]] int abiVersion() const noexcept
234 {
235 return abi_version_;
236 }
237
238 [[nodiscard]] int threadCount() const noexcept
239 {
240 return oracle_threads_;
241 }
242 void setThreadCount(int threads);
243 [[nodiscard]] const std::string& library() const noexcept
244 {
245 return library_;
246 }
247
248 [[nodiscard]] const std::string& resolvedPath() const noexcept
249 {
250 return resolved_path_;
251 }
252
253 [[nodiscard]] bool dead() const noexcept
254 {
255 return dead_;
256 }
257
261 static PotentialWrapper wrap(std::shared_ptr<EnginePotential> engine);
262
263private:
264 EnginePotential() = default;
265
268 int noteCall(int status, const char* operation, std::string& err);
269
270 void* handle_ = nullptr;
271 void* pot_ = nullptr;
272 void* force_ = nullptr;
273 void* batch_ = nullptr;
274 void* set_callbacks_ = nullptr;
275 void* hessian_checked_ = nullptr;
276 void* hessian_action_checked_ = nullptr;
277 void* prediction_supported_checked_ = nullptr;
278 void* gpr_stats_ = nullptr;
280 void* torch_cuda_ = nullptr;
281 void* destroy_ = nullptr;
282 void* model_state_ = nullptr;
283 void* hint_ = nullptr;
284 void* set_num_threads_ = nullptr;
285 void* caps_ = nullptr;
286 void* hvp_ = nullptr;
287 void* taylor_jet_ = nullptr;
288 void* set_cache_ = nullptr;
289 void* cache_counts_ = nullptr;
290 void* unload_policy_ = nullptr;
291 void* last_error_ = nullptr;
292 EngineAbiStamp stamp_{};
293 int abi_version_ = 0;
294 int oracle_threads_ = 1;
295 bool dead_ = false;
296 std::string library_;
297 std::string resolved_path_;
298};
299
300} // namespace pot
301
302#endif
EngineError(std::string library, int status, const std::string &what)
const std::string & library() const noexcept
bool fatal() const noexcept
int status() const noexcept
std::string lastError() const
int hessianChecked(long n_atoms, const double *positions, std::size_t positions_len, const int *atomic_nrs, double *hessian_out, std::size_t hessian_len, const double *box)
Extent-checked dense Hessian at one geometry.
const std::string & resolvedPath() const noexcept
Absolute path of the loaded object, from the dynamic loader.
int gprStats(RgpotEngineGprStats &stats_out) const
Read learning-engine operation counters.
void setThreadCount(int threads)
int setCache(const char *path, std::string &err)
EnginePotential & operator=(const EnginePotential &)=delete
bool hasBatch() const noexcept
True when the engine exports rgpot_engine_force_batch.
int force(long n_atoms, const double *positions, const int *atomic_nrs, const double *box, double *forces, double *energy, double *variance)
static PotentialWrapper wrap(std::shared_ptr< EnginePotential > engine)
int abiVersion() const noexcept
static std::unique_ptr< EnginePotential > open(const std::string &library, const std::vector< unsigned char > &config, std::string &err, bool torch_cuda=false, int oracle_threads=1)
int caps(EngineCaps &out, std::string &err)
bool hasHessianActionChecked() const noexcept
True when the engine exports rgpot_engine_hessian_action_checked.
bool hasGprStats() const noexcept
True when the engine exports rgpot_engine_gpr_stats.
int cacheCounts(std::uint64_t &computed, std::uint64_t &served, std::string &err)
int setCallbacks(const RgpotEngineCallbacks &callbacks)
Register host callbacks for a learning engine.
bool hasCallbacks() const noexcept
True when the engine exports rgpot_engine_set_callbacks.
int hessianActionChecked(long n_atoms, const double *positions, std::size_t positions_len, const int *atomic_nrs, const double *direction, std::size_t direction_len, double *hv_out, std::size_t hv_len, const double *box, unsigned long long *ops_out, double *scale_out)
const std::string & library() const noexcept
int hvp(long n_atoms, const double *positions, const int *atomic_nrs, const double *direction, double *hv_out, const double *box, std::string &err)
EngineAbiStamp abiStamp() const noexcept
The stamp the plugin reported, or {1, 0, 1} when it exports none.
int taylorJet(long n_systems, long n_atoms, const double *positions, const int *atomic_nrs, const double *directions, long order, double *jet_out, EngineDerivativeReport *report, const double *boxes, std::string &err)
EnginePotential(const EnginePotential &)=delete
int predictionSupportedChecked(long n_atoms, const double *positions, std::size_t positions_len, const int *atomic_nrs, const double *box, int *supported_out)
Extent-checked prediction-support query.
int hint(const std::vector< unsigned char > &message, std::string &err)
int threadCount() const noexcept
OpenMP team requested explicitly by the host for every engine call.
bool dead() const noexcept
True once a negative status was returned: the handle is unusable.
unsigned long long modelState() const
Monotone model-state counter; zero for an engine without one.
int forceBatch(long n_systems, long n_atoms, const double *positions, const int *atomic_nrs, const double *boxes, double *forces, double *energies, double *variances)
bool hasPredictionSupportedChecked() const noexcept
bool hasHessianChecked() const noexcept
True when the engine exports rgpot_engine_hessian_checked.
Type-erased wrapper around any potential that provides a force() interface.
eon_relax_band_t eon_relax_surface_fn void eon_relax_outcome_t * out
int status
Potential wrappers and the batched oracle interface.
unsigned short layout
unsigned short major
unsigned short minor