Loading...
Searching...
No Matches
GenericEngineLoader Class Reference

#include <GenericEngineLoader.h>

Public Member Functions

 GenericEngineLoader (const GenericEngineOptions &opt)
 ~GenericEngineLoader ()
 GenericEngineLoader (const GenericEngineLoader &)=delete
GenericEngineLoader & operator= (const GenericEngineLoader &)=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 = RgpotEnginePot *(*)(const void *, size_t, char *, size_t)
using destroy_fn = void (*)(RgpotEnginePot *)
using force_fn

Private Attributes

void * m_lib {nullptr}
RgpotEnginePot * m_pot {nullptr}
std::string m_tag
create_fn m_create {nullptr}
destroy_fn m_destroy {nullptr}
force_fn m_force {nullptr}

Detailed Description

Definition at line 38 of file GenericEngineLoader.h.

Member Typedef Documentation

◆ create_fn

using GenericEngineLoader::create_fn = RgpotEnginePot *(*)(const void *, size_t, char *, size_t)
private

Definition at line 53 of file GenericEngineLoader.h.

◆ destroy_fn

using GenericEngineLoader::destroy_fn = void (*)(RgpotEnginePot *)
private

Definition at line 54 of file GenericEngineLoader.h.

◆ force_fn

Initial value:
int (*)(RgpotEnginePot *, long, const double *, const int *,
double *, double *, double *, const double *,
struct RgpotEnginePot RgpotEnginePot
int(* rgpot_engine_coord_transform)(void *user, long nAtoms, double *positions, double *box)
Optional host-side transform applied by the engine to its scratch copy of (positions,...

Definition at line 55 of file GenericEngineLoader.h.

Constructor & Destructor Documentation

◆ GenericEngineLoader() [1/2]

GenericEngineLoader::GenericEngineLoader ( const GenericEngineOptions & opt)
explicit

Definition at line 43 of file GenericEngineLoader.cpp.

44 : m_tag(opt.tag.empty() ? opt.library : opt.tag) {
45 std::vector<std::string> paths;
46 if (!opt.engine_path.empty())
47 paths.push_back(opt.engine_path);
48 if (!opt.env_var.empty())
49 if (const char *e = std::getenv(opt.env_var.c_str()))
50 if (e && *e)
51 paths.emplace_back(e);
52 paths.push_back(opt.library);
53 auto add_dirs = [&](const char *env) {
54 if (!env)
55 return;
56 std::string s(env);
57 size_t i = 0;
58 while (i < s.size()) {
59 const auto j = s.find(':', i);
60 const auto dir = s.substr(i, j == std::string::npos ? j : j - i);
61 if (!dir.empty())
62 paths.push_back(dir + "/" + opt.library);
63 if (j == std::string::npos)
64 break;
65 i = j + 1;
66 }
67 };
68 add_dirs(std::getenv("EON_POTENTIALS_PATH"));
69
70 std::string last_dlerr;
71 for (const auto &p : paths) {
72 m_lib = open_lib(p.c_str());
73 if (m_lib)
74 break;
75#ifndef _WIN32
76 if (const char *e = dlerror())
77 last_dlerr = e;
78#endif
79 }
80 if (!m_lib) {
81 std::string msg =
82 "RGPOT(" + m_tag + "): " + opt.library + " not found (set " +
83 (opt.env_var.empty() ? std::string("engine_path") : opt.env_var) +
84 " or EON_POTENTIALS_PATH)";
85 if (!last_dlerr.empty())
86 msg += std::string("; last dlerror: ") + last_dlerr;
87 throw std::runtime_error(msg);
88 }
89
90 auto abi =
91 reinterpret_cast<int (*)()>(load_sym(m_lib, "rgpot_engine_abi_version"));
92 m_create =
93 reinterpret_cast<create_fn>(load_sym(m_lib, "rgpot_engine_create"));
94 m_destroy =
95 reinterpret_cast<destroy_fn>(load_sym(m_lib, "rgpot_engine_destroy"));
96 m_force = reinterpret_cast<force_fn>(load_sym(m_lib, "rgpot_engine_force"));
97 if (!abi || !m_create || !m_destroy || !m_force ||
98 abi() != RGPOT_ENGINE_ABI_VERSION) {
99 close_lib(m_lib);
100 m_lib = nullptr;
101 throw std::runtime_error("RGPOT(" + m_tag +
102 "): engine C ABI missing/mismatch");
103 }
104 char err[1024]{};
105 m_pot = m_create(opt.config.data(), opt.config.size(), err, sizeof err);
106 if (!m_pot) {
107 close_lib(m_lib);
108 m_lib = nullptr;
109 throw std::runtime_error("RGPOT(" + m_tag +
110 "): create failed: " + std::string(err));
111 }
112}
void(*)(RgpotEnginePot *) destroy_fn
int(*)(RgpotEnginePot *, long, const double *, const int *, double *, double *, double *, const double *, rgpot_engine_coord_transform, void *) force_fn
RgpotEnginePot *(*)(const void *, size_t, char *, size_t) create_fn
#define RGPOT_ENGINE_ABI_VERSION
std::string tag
Backend tag for error messages ("uma", ...).
std::string env_var
Optional env var naming an explicit .so path (checked first).
std::vector< unsigned char > config
Cap'n Proto flat-array message (the engine's params struct on the shared Potentials....
std::string engine_path
Optional explicit .so path from config.
std::string library
Library basename to search for, e.g. "libuma_engine.so".

◆ ~GenericEngineLoader()

GenericEngineLoader::~GenericEngineLoader ( )

Definition at line 114 of file GenericEngineLoader.cpp.

114 {
115 // Destroy the pot while the engine is still mapped. Do not dlclose the
116 // engine: torch static teardown after dlclose routinely SEGV on exit.
117 if (m_pot && m_destroy)
119 m_pot = nullptr;
120 m_lib = nullptr;
121}

◆ GenericEngineLoader() [2/2]

GenericEngineLoader::GenericEngineLoader ( const GenericEngineLoader & )
delete

Member Function Documentation

◆ available()

bool GenericEngineLoader::available ( ) const
inlinenodiscardnoexcept

Definition at line 45 of file GenericEngineLoader.h.

45{ return m_pot != nullptr; }

◆ force()

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

Definition at line 123 of file GenericEngineLoader.cpp.

125 {
126 if (!m_pot || !m_force)
127 throw std::runtime_error("RGPOT(" + m_tag + "): engine not available");
128 double var = 0.0;
129 const int rc = m_force(m_pot, N, R, atomicNrs, F, U,
130 variance ? variance : &var, box, nullptr, nullptr);
131 if (rc != 0)
132 throw std::runtime_error("RGPOT(" + m_tag + "): force failed");
133}

◆ operator=()

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

Member Data Documentation

◆ m_create

create_fn GenericEngineLoader::m_create {nullptr}
private

Definition at line 58 of file GenericEngineLoader.h.

58{nullptr};

◆ m_destroy

destroy_fn GenericEngineLoader::m_destroy {nullptr}
private

Definition at line 59 of file GenericEngineLoader.h.

59{nullptr};

◆ m_force

force_fn GenericEngineLoader::m_force {nullptr}
private

Definition at line 60 of file GenericEngineLoader.h.

60{nullptr};

◆ m_lib

void* GenericEngineLoader::m_lib {nullptr}
private

Definition at line 50 of file GenericEngineLoader.h.

50{nullptr};

◆ m_pot

RgpotEnginePot* GenericEngineLoader::m_pot {nullptr}
private

Definition at line 51 of file GenericEngineLoader.h.

51{nullptr};

◆ m_tag

std::string GenericEngineLoader::m_tag
private

Definition at line 52 of file GenericEngineLoader.h.


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