Loading...
Searching...
No Matches
PotentialWrapper.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// Copyright (c) gpridge developers
3// See LICENSE for details.
4
5#include "PotentialWrapper.h"
6
7#include <atomic>
8#include <mutex>
9#include <unordered_map>
10
11#include "rgpot/Potential.hpp"
12
13namespace pot {
14
15namespace {
16std::atomic<long> oracle_evaluations{0};
17thread_local int oracle_evaluation_depth = 0;
18} // namespace
19
21{
22 oracle_evaluations.store(0, std::memory_order_relaxed);
23}
24
25long oracleEvaluations() noexcept
26{
27 return oracle_evaluations.load(std::memory_order_relaxed);
28}
29
30void pushOracleEvaluations(long evaluations) noexcept
31{
32 if (oracle_evaluation_depth == 0 && evaluations > 0)
33 oracle_evaluations.fetch_add(evaluations, std::memory_order_relaxed);
34 ++oracle_evaluation_depth;
35}
36
37void popOracleEvaluations() noexcept
38{
39 if (oracle_evaluation_depth > 0) --oracle_evaluation_depth;
40}
41
42namespace {
43std::shared_ptr<std::mutex> processSerialMutex()
44{
45 static auto mutex = std::make_shared<std::mutex>();
46 return mutex;
47}
48
49std::shared_ptr<std::mutex> instanceMutex(const rgpot::PotentialBase* potential)
50{
51 static std::mutex registry_mutex;
52 static std::unordered_map<const rgpot::PotentialBase*,
53 std::weak_ptr<std::mutex>>
54 mutexes;
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);
59 else
60 ++entry;
61 }
62 auto mutex = mutexes[potential].lock();
63 if (!mutex) {
64 mutex = std::make_shared<std::mutex>();
65 mutexes[potential] = mutex;
66 }
67 return mutex;
68}
69
70struct OwnedRgpot {
71 std::shared_ptr<rgpot::PotentialBase> potential;
72 std::shared_ptr<std::mutex> mutex;
73
74 explicit OwnedRgpot(std::shared_ptr<rgpot::PotentialBase> value)
75 : potential(std::move(value))
76 {
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());
82 }
83
84 auto operator()(const rgpot::types::AtomMatrix& positions,
85 const std::vector<int>& types,
86 const std::array<std::array<double, 3>, 3>& cell)
87 {
88 std::unique_lock<std::mutex> lock;
89 if (mutex) lock = std::unique_lock<std::mutex>(*mutex);
90 return (*potential)(positions, types, cell);
91 }
92
93 void forceBatch(const rgpot::ForceBatch& batch)
94 {
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);
99 return;
100 }
101 // PotentialBase permits scalar-only implementations. Preserve their
102 // uncertainty channel without assuming a forceBatch override exists.
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{};
111 if (in.box)
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];
115 auto [energy, forces, variance] =
116 (*potential)(positions, types, cell);
117 auto& out = batch.out[b];
118 out.energy = energy;
119 out.variance = variance;
120 if (out.F)
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);
124 }
125 }
126};
127} // namespace
128
130 std::shared_ptr<rgpot::PotentialBase> pot)
131{
132 if (!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)));
136}
137
138} /* End namespace pot */
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