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

#include <MetatomicEngineLoader.h>

Public Member Functions

 MetatomicEngineLoader (const MetatomicEngineOptions &opt)
 ~MetatomicEngineLoader ()
 MetatomicEngineLoader (const MetatomicEngineLoader &)=delete
MetatomicEngineLoaderoperator= (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}
RgpotMtaPotm_pot {nullptr}
create_fn m_create {nullptr}
destroy_fn m_destroy {nullptr}
force_fn m_force {nullptr}

Detailed Description

Definition at line 20 of file MetatomicEngineLoader.h.

Member Typedef Documentation

◆ create_fn

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

Definition at line 34 of file MetatomicEngineLoader.h.

◆ destroy_fn

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

Definition at line 35 of file MetatomicEngineLoader.h.

◆ force_fn

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

Definition at line 36 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("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}
#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 114 of file MetatomicEngineLoader.cpp.

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

◆ MetatomicEngineLoader() [2/2]

MetatomicEngineLoader::MetatomicEngineLoader ( const MetatomicEngineLoader & )
delete

Member Function Documentation

◆ available()

bool MetatomicEngineLoader::available ( ) const
inlinenodiscardnoexcept

Definition at line 27 of file MetatomicEngineLoader.h.

27{ 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 124 of file MetatomicEngineLoader.cpp.

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

◆ operator=()

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

Member Data Documentation

◆ m_create

create_fn MetatomicEngineLoader::m_create {nullptr}
private

Definition at line 38 of file MetatomicEngineLoader.h.

38{nullptr};

◆ m_destroy

destroy_fn MetatomicEngineLoader::m_destroy {nullptr}
private

Definition at line 39 of file MetatomicEngineLoader.h.

39{nullptr};

◆ m_force

force_fn MetatomicEngineLoader::m_force {nullptr}
private

Definition at line 40 of file MetatomicEngineLoader.h.

40{nullptr};

◆ m_lib

void* MetatomicEngineLoader::m_lib {nullptr}
private

Definition at line 32 of file MetatomicEngineLoader.h.

32{nullptr};

◆ m_pot

RgpotMtaPot* MetatomicEngineLoader::m_pot {nullptr}
private

Definition at line 33 of file MetatomicEngineLoader.h.

33{nullptr};

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