Loading...
Searching...
No Matches
MetatomicEngineLoader.cpp
Go to the documentation of this file.
2#include <cstdlib>
3#include <stdexcept>
4#include <string>
5#include <vector>
6#ifndef _WIN32
7#include <dlfcn.h>
8#endif
9
10namespace {
11void *open_lib(const char *path) {
12#ifndef _WIN32
13 return dlopen(path, RTLD_NOW | RTLD_GLOBAL);
14#else
15 return nullptr;
16#endif
17}
18void close_lib(void *h) {
19#ifndef _WIN32
20 if (h)
21 dlclose(h);
22#endif
23}
24void *load_sym(void *h, const char *n) {
25#ifndef _WIN32
26 return dlsym(h, n);
27#else
28 return nullptr;
29#endif
30}
31} // namespace
32
34 const MetatomicEngineOptions &opt) {
35 std::vector<std::string> paths;
36 if (!opt.engine_path.empty())
37 paths.push_back(opt.engine_path);
38 if (const char *e = std::getenv("RGPOT_METATOMIC_ENGINE"))
39 if (e && *e)
40 paths.emplace_back(e);
41 if (const char *e = std::getenv("METATOMIC_ENGINE"))
42 if (e && *e)
43 paths.emplace_back(e);
44 paths.emplace_back("libmetatomic_engine.so");
45 auto add_dirs = [&](const char *env) {
46 if (!env)
47 return;
48 std::string s(env);
49 size_t i = 0;
50 while (i < s.size()) {
51 auto j = s.find(':', i);
52 if (j == std::string::npos)
53 j = s.size();
54 if (j > i) {
55 std::string d = s.substr(i, j - i);
56 if (!d.empty() && d.back() != '/')
57 d += '/';
58 paths.push_back(d + "libmetatomic_engine.so");
59 }
60 i = j + 1;
61 }
62 };
63 add_dirs(std::getenv("EON_POTENTIALS_PATH"));
64 add_dirs(std::getenv("RGPOT_ENGINE_PATH"));
65
66 std::string last_dlerr;
67 for (const auto &p : paths) {
68 m_lib = open_lib(p.c_str());
69 if (m_lib)
70 break;
71#ifndef _WIN32
72 if (const char *e = dlerror())
73 last_dlerr = e;
74#endif
75 }
76 if (!m_lib) {
77 std::string msg = "RGPOT(metatomic): libmetatomic_engine.so not found "
78 "(set RGPOT_METATOMIC_ENGINE or EON_POTENTIALS_PATH)";
79 if (!last_dlerr.empty())
80 msg += std::string("; last dlerror: ") + last_dlerr;
81 throw std::runtime_error(msg);
82 }
83
84 auto abi =
85 reinterpret_cast<int (*)()>(load_sym(m_lib, "rgpot_mta_abi_version"));
86 m_create = reinterpret_cast<create_fn>(load_sym(m_lib, "rgpot_mta_create"));
87 m_destroy =
88 reinterpret_cast<destroy_fn>(load_sym(m_lib, "rgpot_mta_destroy"));
89 m_force = reinterpret_cast<force_fn>(load_sym(m_lib, "rgpot_mta_force"));
90 if (!abi || !m_create || !m_destroy || !m_force ||
91 abi() != RGPOT_MTA_ABI_VERSION) {
92 close_lib(m_lib);
93 m_lib = nullptr;
94 throw std::runtime_error("RGPOT(metatomic): engine C ABI missing/mismatch");
95 }
96 char err[1024]{};
97 RgpotMtaConfig cfg{};
98 cfg.model_path = opt.model_path.c_str();
99 cfg.device = opt.device.c_str();
100 cfg.length_unit = opt.length_unit.c_str();
102 cfg.check_consistency = opt.check_consistency ? 1 : 0;
105 m_pot = m_create(&cfg, err, sizeof err);
106 if (!m_pot) {
107 close_lib(m_lib);
108 m_lib = nullptr;
109 throw std::runtime_error(std::string("RGPOT(metatomic): create failed: ") +
110 err);
111 }
112}
113
115 // Destroy the pot while the engine is still mapped. Do not dlclose
116 // libmetatomic_engine.so: torch/metatomic static teardown after dlclose
117 // routinely SEGV on process exit.
118 if (m_pot && m_destroy)
120 m_pot = nullptr;
121 m_lib = nullptr;
122}
123
124void MetatomicEngineLoader::force(long N, const double *R, const int *atomicNrs,
125 double *F, double *U, double *variance,
126 const double *box) const {
127 if (!m_pot || !m_force)
128 throw std::runtime_error("RGPOT(metatomic): engine not available");
129 double var = 0.0;
130 const int rc =
131 m_force(m_pot, N, R, atomicNrs, F, U, variance ? variance : &var, box);
132 if (rc != 0)
133 throw std::runtime_error("RGPOT(metatomic): force failed");
134}
#define RGPOT_MTA_ABI_VERSION
void(*)(RgpotMtaPot *) destroy_fn
RgpotMtaPot *(*)(const RgpotMtaConfig *, char *, size_t) create_fn
void force(long N, const double *R, const int *atomicNrs, double *F, double *U, double *variance, const double *box) const
int(*)(RgpotMtaPot *, long, const double *, const int *, double *, double *, double *, const double *) force_fn
MetatomicEngineLoader(const MetatomicEngineOptions &opt)
Flat config (subset of MetatomicConfig).
const char * extensions_directory
double uncertainty_threshold
const char * device
const char * model_path
const char * length_unit
int torch_determinism_strict
0=Fast, 1=Strict