9#include <unordered_map>
11#include "rgpot/Potential.hpp"
16std::atomic<long> oracle_evaluations{0};
17thread_local int oracle_evaluation_depth = 0;
22 oracle_evaluations.store(0, std::memory_order_relaxed);
27 return oracle_evaluations.load(std::memory_order_relaxed);
32 if (oracle_evaluation_depth == 0 && evaluations > 0)
33 oracle_evaluations.fetch_add(evaluations, std::memory_order_relaxed);
34 ++oracle_evaluation_depth;
39 if (oracle_evaluation_depth > 0) --oracle_evaluation_depth;
43std::shared_ptr<std::mutex> processSerialMutex()
45 static auto mutex = std::make_shared<std::mutex>();
49std::shared_ptr<std::mutex> instanceMutex(
const rgpot::PotentialBase* potential)
51 static std::mutex registry_mutex;
52 static std::unordered_map<
const rgpot::PotentialBase*,
53 std::weak_ptr<std::mutex>>
55 std::lock_guard lock(registry_mutex);
56 for (
auto entry = mutexes.begin(); entry != mutexes.end();) {
57 if (entry->second.expired())
58 entry = mutexes.erase(entry);
62 auto mutex = mutexes[potential].lock();
64 mutex = std::make_shared<std::mutex>();
65 mutexes[potential] = mutex;
71 std::shared_ptr<rgpot::PotentialBase> potential;
72 std::shared_ptr<std::mutex> mutex;
74 explicit OwnedRgpot(std::shared_ptr<rgpot::PotentialBase> value)
75 : potential(std::move(value))
77 const auto reentrancy = potential->caps().reentrancy;
78 if (reentrancy == rgpot::Reentrancy::ProcessSerial)
79 mutex = processSerialMutex();
80 else if (reentrancy == rgpot::Reentrancy::PerInstance)
81 mutex = instanceMutex(potential.get());
84 auto operator()(
const rgpot::types::AtomMatrix& positions,
85 const std::vector<int>& types,
86 const std::array<std::array<double, 3>, 3>& cell)
88 std::unique_lock<std::mutex> lock;
89 if (mutex) lock = std::unique_lock<std::mutex>(*mutex);
90 return (*potential)(positions, types, cell);
93 void forceBatch(
const rgpot::ForceBatch& batch)
95 std::unique_lock<std::mutex> lock;
96 if (mutex) lock = std::unique_lock<std::mutex>(*mutex);
97 if (potential->caps().batched) {
98 potential->forceBatch(batch);
103 for (std::size_t b = 0; b < batch.nSystems; ++b) {
104 const auto& in = batch.in[b];
105 rgpot::types::AtomMatrix positions(in.nAtoms, 3);
106 for (std::size_t a = 0; a < in.nAtoms; ++a)
107 for (std::size_t d = 0; d < 3; ++d)
108 positions(a, d) = in.pos[3 * a + d];
109 std::vector<int> types(in.atmnrs, in.atmnrs + in.nAtoms);
110 std::array<std::array<double, 3>, 3> cell{};
112 for (std::size_t r = 0; r < 3; ++r)
113 for (std::size_t c = 0; c < 3; ++c)
114 cell[r][c] = in.box[3 * r + c];
116 (*potential)(positions, types, cell);
117 auto&
out = batch.out[b];
121 for (std::size_t a = 0; a < in.nAtoms; ++a)
122 for (std::size_t d = 0; d < 3; ++d)
123 out.F[3 * a + d] = forces(a, d);
130 std::shared_ptr<rgpot::PotentialBase>
pot)
133 throw std::invalid_argument(
134 "PotentialWrapper: rgpot shared pointer must not be null");
135 return from_rgpot(std::make_shared<OwnedRgpot>(std::move(
pot)));
PotentialWrapper(ForceFn fn)
static PotentialWrapper from_rgpot(RgPot &pot)
Factory: adapt an rgpot PotentialBase subclass.
eon_relax_band_t eon_relax_surface_fn void eon_relax_outcome_t * out
double variance(const Weights &w, const Matrix &covariance)
Potential wrappers and the batched oracle interface.
void resetOracleEvaluations() noexcept
long oracleEvaluations() noexcept
void pushOracleEvaluations(long evaluations) noexcept
void popOracleEvaluations() noexcept