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("librgpot_metatomic_engine.so");
45 paths.emplace_back("libmetatomic_engine.so");
46 auto add_dirs = [&](const char *env) {
47 if (!env)
48 return;
49 std::string s(env);
50 size_t i = 0;
51 while (i < s.size()) {
52 auto j = s.find(':', i);
53 if (j == std::string::npos)
54 j = s.size();
55 if (j > i) {
56 std::string d = s.substr(i, j - i);
57 if (!d.empty() && d.back() != '/')
58 d += '/';
59 paths.push_back(d + "librgpot_metatomic_engine.so");
60 paths.push_back(d + "libmetatomic_engine.so");
61 }
62 i = j + 1;
63 }
64 };
65 add_dirs(std::getenv("EON_POTENTIALS_PATH"));
66 add_dirs(std::getenv("RGPOT_ENGINE_PATH"));
67
68 std::string last_dlerr;
69 for (const auto &p : paths) {
70 m_lib = open_lib(p.c_str());
71 if (m_lib)
72 break;
73#ifndef _WIN32
74 if (const char *e = dlerror())
75 last_dlerr = e;
76#endif
77 }
78 if (!m_lib) {
79 std::string msg =
80 "RGPOT(metatomic): librgpot_metatomic_engine.so not found "
81 "(set RGPOT_METATOMIC_ENGINE or EON_POTENTIALS_PATH)";
82 if (!last_dlerr.empty())
83 msg += std::string("; last dlerror: ") + last_dlerr;
84 throw std::runtime_error(msg);
85 }
86
87 auto abi =
88 reinterpret_cast<int (*)()>(load_sym(m_lib, "rgpot_mta_abi_version"));
89 m_create = reinterpret_cast<create_fn>(load_sym(m_lib, "rgpot_mta_create"));
90 m_destroy =
91 reinterpret_cast<destroy_fn>(load_sym(m_lib, "rgpot_mta_destroy"));
92 m_force = reinterpret_cast<force_fn>(load_sym(m_lib, "rgpot_mta_force"));
93 if (!abi || !m_create || !m_destroy || !m_force ||
94 abi() != RGPOT_MTA_ABI_VERSION) {
95 close_lib(m_lib);
96 m_lib = nullptr;
97 throw std::runtime_error("RGPOT(metatomic): engine C ABI missing/mismatch");
98 }
99 char err[1024]{};
100 RgpotMtaConfig cfg{};
101 cfg.model_path = opt.model_path.c_str();
102 cfg.device = opt.device.c_str();
103 cfg.length_unit = opt.length_unit.c_str();
105 cfg.check_consistency = opt.check_consistency ? 1 : 0;
108 m_pot = m_create(&cfg, err, sizeof err);
109 if (!m_pot) {
110 close_lib(m_lib);
111 m_lib = nullptr;
112 throw std::runtime_error(std::string("RGPOT(metatomic): create failed: ") +
113 err);
114 }
115}
116
118 // Destroy the pot while the engine is still mapped. Do not dlclose
119 // libmetatomic_engine.so: torch/metatomic static teardown after dlclose
120 // routinely SEGV on process exit.
121 if (m_pot && m_destroy)
123 m_pot = nullptr;
124 m_lib = nullptr;
125}
126
127void MetatomicEngineLoader::force(long N, const double *R, const int *atomicNrs,
128 double *F, double *U, double *variance,
129 const double *box) const {
130 if (!m_pot || !m_force)
131 throw std::runtime_error("RGPOT(metatomic): engine not available");
132 double var = 0.0;
133 const int rc =
134 m_force(m_pot, N, R, atomicNrs, F, U, variance ? variance : &var, box);
135 if (rc != 0)
136 throw std::runtime_error("RGPOT(metatomic): force failed");
137}
#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