Loading...
Searching...
No Matches
MetatomicEngineLoader Class Reference

#include <MetatomicEngineLoader.h>

Public Member Functions

 MetatomicEngineLoader (const MetatomicEngineOptions &opt)
 ~MetatomicEngineLoader ()
 MetatomicEngineLoader (const MetatomicEngineLoader &)=delete
MetatomicEngineLoader & operator= (const MetatomicEngineLoader &)=delete
bool available () const noexcept
void force (long N, const double *R, const int *atomicNrs, double *F, double *U, double *variance, const double *box) const

Private Types

using create_fn = RgpotMtaPot *(*)(const RgpotMtaConfig *, char *, size_t)
using destroy_fn = void (*)(RgpotMtaPot *)
using force_fn

Private Attributes

void * m_lib {nullptr}
RgpotMtaPot * m_pot {nullptr}
create_fn m_create {nullptr}
destroy_fn m_destroy {nullptr}
force_fn m_force {nullptr}

Detailed Description

Definition at line 21 of file MetatomicEngineLoader.h.

Member Typedef Documentation

◆ create_fn

using MetatomicEngineLoader::create_fn = RgpotMtaPot *(*)(const RgpotMtaConfig *, char *, size_t)
private

Definition at line 35 of file MetatomicEngineLoader.h.

◆ destroy_fn

using MetatomicEngineLoader::destroy_fn = void (*)(RgpotMtaPot *)
private

Definition at line 36 of file MetatomicEngineLoader.h.

◆ force_fn

Initial value:
int (*)(RgpotMtaPot *, long, const double *, const int *,
double *, double *, double *, const double *)

Definition at line 37 of file MetatomicEngineLoader.h.

Constructor & Destructor Documentation

◆ MetatomicEngineLoader() [1/2]

MetatomicEngineLoader::MetatomicEngineLoader ( const MetatomicEngineOptions & opt)
explicit

Definition at line 33 of file MetatomicEngineLoader.cpp.

34 {
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}
#define RGPOT_MTA_ABI_VERSION
struct RgpotMtaConfig RgpotMtaConfig
Flat config (subset of MetatomicConfig).
void(*)(RgpotMtaPot *) destroy_fn
RgpotMtaPot *(*)(const RgpotMtaConfig *, char *, size_t) create_fn
int(*)(RgpotMtaPot *, long, const double *, const int *, double *, double *, double *, const double *) force_fn
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

◆ ~MetatomicEngineLoader()

MetatomicEngineLoader::~MetatomicEngineLoader ( )

Definition at line 117 of file MetatomicEngineLoader.cpp.

117 {
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}

◆ MetatomicEngineLoader() [2/2]

MetatomicEngineLoader::MetatomicEngineLoader ( const MetatomicEngineLoader & )
delete

Member Function Documentation

◆ available()

bool MetatomicEngineLoader::available ( ) const
inlinenodiscardnoexcept

Definition at line 28 of file MetatomicEngineLoader.h.

28{ return m_pot != nullptr; }

◆ force()

void MetatomicEngineLoader::force ( long N,
const double * R,
const int * atomicNrs,
double * F,
double * U,
double * variance,
const double * box ) const

Definition at line 127 of file MetatomicEngineLoader.cpp.

129 {
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}

◆ operator=()

MetatomicEngineLoader & MetatomicEngineLoader::operator= ( const MetatomicEngineLoader & )
delete

Member Data Documentation

◆ m_create

create_fn MetatomicEngineLoader::m_create {nullptr}
private

Definition at line 39 of file MetatomicEngineLoader.h.

39{nullptr};

◆ m_destroy

destroy_fn MetatomicEngineLoader::m_destroy {nullptr}
private

Definition at line 40 of file MetatomicEngineLoader.h.

40{nullptr};

◆ m_force

force_fn MetatomicEngineLoader::m_force {nullptr}
private

Definition at line 41 of file MetatomicEngineLoader.h.

41{nullptr};

◆ m_lib

void* MetatomicEngineLoader::m_lib {nullptr}
private

Definition at line 33 of file MetatomicEngineLoader.h.

33{nullptr};

◆ m_pot

RgpotMtaPot* MetatomicEngineLoader::m_pot {nullptr}
private

Definition at line 34 of file MetatomicEngineLoader.h.

34{nullptr};

The documentation for this class was generated from the following files: