Loading...
Searching...
No Matches
RgpotPot Class Referencefinal

Potential backed by rgpot NWChemPot / CPMDPot (in-process dlopen of libnwchemc / libcpmdc). More...

#include <RgpotPot.h>

Inheritance diagram for RgpotPot:

Public Member Functions

 RgpotPot (const eonc::Parameters &p)
 ~RgpotPot () override
 RgpotPot (const RgpotPot &)=delete
RgpotPot & operator= (const RgpotPot &)=delete
void force (long N, const double *R, const int *atomicNrs, double *F, double *U, double *variance, const double *box) override
bool isThreadSafe () const noexcept override
 Whether this potential's force() can be called from multiple threads on the SAME instance.
bool supportsBatchEvaluation () const noexcept override
 With cpmdc calculator groups ([RgpotPot] ranks_per_image), a batch is spread over the groups by eonc::GroupSchedule: a system stays on the group that holds its orbitals, at most ceil(M / G) systems per group, then every rank receives every result, so all ranks keep the same band.
void forceBatch (long nSystems, long nAtoms, const double *const *positions, const int *const *atomicNrs, double *const *forces, double *energies, double *variances, const double *const *boxes) override
void forceBatchOwned (long nSystems, long nAtoms, const double *const *positions, const int *const *atomicNrs, double *const *forces, double *energies, double *variances, const double *const *boxes, const long *owners) override
 owners[j] is system j's identity (a NEB image, a ring bead): it names the orbitals the engine starts from and keeps the system on one group across steps.
bool requiresIsolatedMoleculeLayout () const noexcept override
 Molecular tasks retain Cartesian coordinates across storage-cell faces.
const std::string & backend () const noexcept
bool engineAvailable () const
const eonc::CalculatorGroupUse & groupUse () const noexcept
 Calculator-group accounting on the driver.
Public Member Functions inherited from eonc::Potential
void recordForceCall () noexcept
 Charge one evaluated geometry to this potential and its owning registry.
IPotRegistry & registry () const noexcept
 The registry this potential charges its force calls to.
 Potential (PotType a_ptype)
 Production default: construction-scope registry, else PotRegistry::get().
 Potential (PotType a_ptype, IPotRegistry &registry)
 Test seam: injected registry, no process-default get() counters.
 Potential (PotType a_ptype, const Parameters &p)
 Potential (const Parameters &a_params)
virtual ~Potential ()
void force (std::span< const double > positions, std::span< const int > atomicNrs, std::span< double > forces, double *energy, double *variance, std::span< const double > box)
 C++ call site: size-checked view over the raw FFI force().
virtual void setFixedMask (long nAtoms, const double *isFixed)
 Optional frozen-atom mask (nAtoms*3, 1.0 = fixed).
std::tuple< double, AtomMatrix > get_ef (const AtomMatrix &pos, const VectorXi &atmnrs, const Matrix3d &box)
PotType getType () const
virtual unsigned long long surfaceEpoch () const noexcept
 Host-visible surface generation.
virtual double finiteCutoff () const noexcept
 Finite interaction range in position length units.
virtual bool isSurrogate () const noexcept
 Whether this is a surrogate (GP) potential.
virtual unsigned layoutFlags () const noexcept
virtual bool isSharedInstanceThreadSafe () const noexcept
 Conservative gate for sharing one Potential instance across threads.
virtual bool needsPerImageInstance () const noexcept
 Whether NEB should create separate Potential instances per image for true parallel force evaluation.
virtual std::shared_ptr< Potential > clonePotential () const
 Independent instance that does not reload from disk.
virtual void bindBand (const std::vector< std::shared_ptr< Matter > > &)
 NEB calls this with the finished band before the first force evaluation, so a host surface can name each image.
virtual bool forwardsStoredCell () const noexcept
 A host that owns the cell reads the stored matrix.
virtual bool computesStress () const noexcept
 True when force() leaves a Cauchy stress that cauchyStress() can read until the next force() on this instance.
virtual Matrix3d cauchyStress () const
 Cauchy stress in eV/Angstrom^3.
virtual double hessianRange () const noexcept
 Distance beyond which every Hessian block between two atoms is exactly zero, in position length units.

Private Member Functions

void serveWorker ()
void computeSingle (long N, const double *R, const int *atomicNrs, double *F, double *U, const double *box, int group)
void computeBatch (long nSystems, long nAtoms, const double *const *positions, const int *const *atomicNrs, double *const *forces, double *energies, const double *const *boxes, const std::int64_t *owners, const std::int64_t *route)
void sendStop ()
void stopAndDrop ()
void releaseWorkersAtExit ()
void exchangeGroupUse ()
bool evaluate (long N, const double *R, const int *atomicNrs, double *F, double *U, const double *box, std::string &error)

Private Attributes

std::unique_ptr< RGPotEngine > impl_
std::string backend_
bool isolatedMolecule_ {false}
bool driver_ {true}
bool stopped_ {false}
bool dropped_ {false}
bool acked_ {false}
double busy_ {0.0}
double systemsDone_ {0.0}
eonc::CalculatorGroupUse use_
std::unique_ptr< eonc::GroupSchedule > schedule_

Additional Inherited Members

Public Types inherited from eonc::Potential
enum class  PotLayout : unsigned { InProcess = 1u << 0 , NeedsWorkingDirectory = 1u << 1 , Subprocess = 1u << 2 }
 How the pot is executed. Combine with bitwise or. More...
Public Attributes inherited from eonc::Potential
std::atomic< size_t > forceCallCounter
Protected Attributes inherited from eonc::Potential
PotType ptype

Detailed Description

Potential backed by rgpot NWChemPot / CPMDPot (in-process dlopen of libnwchemc / libcpmdc).

Not potserv RPC. Configure via [RgpotPot] INI; energy eV, forces eV/Angstrom.

Definition at line 28 of file RgpotPot.h.

Constructor & Destructor Documentation

◆ RgpotPot() [1/2]

RgpotPot::RgpotPot ( const eonc::Parameters & p)
explicit

Definition at line 19 of file RgpotPot.cpp.

20 : eonc::Potential(eonc::PotType::RGPOT, p) {
21 RGPotEngineOptions opt;
22 const auto &o = p.rgpot_options();
23 opt.backend = o.backend;
24 opt.basis = o.basis;
25 opt.theory = o.theory;
26 opt.scf_type = o.scf_type;
27 opt.functional = o.functional;
28 opt.cutoff_ry = o.cutoff_ry;
29 opt.charge = o.charge;
30 opt.multiplicity = o.multiplicity;
31 opt.engine_path = o.engine_path;
32 opt.engine_library = o.engine_library;
33 opt.engine_root = o.engine_root;
34 opt.title = o.title;
35 opt.memory_mb = o.memory_mb;
36 opt.scratch_dir = o.scratch_dir;
37 opt.input_block = o.input_block;
38 opt.permanent_dir = o.permanent_dir;
39 opt.params_path = o.params_path;
40 opt.ranks_per_image = o.ranks_per_image;
41 opt.model_path = o.model_path;
42 opt.device = o.device;
43 opt.length_unit = o.length_unit;
44 opt.extensions_directory = o.extensions_directory;
45 opt.check_consistency = o.check_consistency;
46 opt.uncertainty_threshold = o.uncertainty_threshold;
47 opt.torch_determinism_strict = o.torch_determinism_strict;
48 opt.xtb_paramset = o.xtb_paramset;
49 opt.xtb_accuracy = o.xtb_accuracy;
50 opt.xtb_electronic_temperature = o.xtb_electronic_temperature;
51 opt.xtb_max_iterations = o.xtb_max_iterations;
52 opt.xtb_charge = o.xtb_charge;
53 opt.xtb_uhf = o.xtb_uhf;
54 opt.task_name = o.task_name;
55
56 // Env overrides (CI / benchmarks)
57 if (const char *e = std::getenv("RGPOT_BACKEND"))
58 opt.backend = e;
59 if (const char *e = std::getenv("RGPOT_NWCHEM_BASIS"))
60 opt.basis = e;
61 if (const char *e = std::getenv("RGPOT_NWCHEM_THEORY"))
62 opt.theory = e;
63 if (const char *e = std::getenv("RGPOT_NWCHEM_SCF_TYPE"))
64 opt.scf_type = e;
65 if (const char *e = std::getenv("RGPOT_PARAMS_PATH"))
66 opt.params_path = e;
67 // Engine-path env overrides are backend-scoped: NWCHEMC_LIBRARY must not
68 // leak into a cpmdc configure (CPMDPot resolves CPMDC_LIBRARY itself).
69 std::string backend_lc = opt.backend;
70 std::ranges::transform(backend_lc, backend_lc.begin(),
71 [](unsigned char c) { return std::tolower(c); });
72 if (backend_lc.rfind("nwchem", 0) == 0) {
73 if (const char *e = std::getenv("NWCHEMC_LIBRARY"))
74 opt.engine_path = e;
75 else if (const char *e = std::getenv("RGPOT_NWCHEMC_ENGINE"))
76 opt.engine_path = e;
77 else if (const char *e = std::getenv("RGPOT_NWCHEM_ENGINE"))
78 opt.engine_path = e;
79 } else if (backend_lc.rfind("cpmd", 0) == 0) {
80 if (const char *e = std::getenv("CPMDC_LIBRARY"))
81 opt.engine_path = e;
82 else if (const char *e = std::getenv("RGPOT_CPMDC_ENGINE"))
83 opt.engine_path = e;
84 } else if (backend_lc.rfind("meta", 0) == 0 || backend_lc == "mta") {
85 if (const char *e = std::getenv("RGPOT_METATOMIC_ENGINE"))
86 opt.engine_path = e;
87 else if (const char *e = std::getenv("METATOMIC_ENGINE"))
88 opt.engine_path = e;
89 if (const char *e = std::getenv("RGPOT_METATOMIC_MODEL"))
90 opt.model_path = e;
91 } else if (backend_lc == "xtb" || backend_lc == "xtbpot" ||
92 backend_lc == "gfn" || backend_lc == "gfnxtb") {
93 if (const char *e = std::getenv("RGPOT_XTB_ENGINE"))
94 opt.engine_path = e;
95 else if (const char *e = std::getenv("XTB_ENGINE"))
96 opt.engine_path = e;
97 if (opt.xtb_paramset.empty() || opt.xtb_paramset == "GFN2xTB") {
98 if (!p.xtb_options().paramset.empty())
100 }
101 }
102
103 // Dual-read [Metatomic] when RGPOT backend is metatomic
104 if ((backend_lc.rfind("meta", 0) == 0 || backend_lc == "mta") &&
105 opt.model_path.empty())
107 if ((backend_lc.rfind("meta", 0) == 0 || backend_lc == "mta") &&
108 opt.device == "cpu" && !p.metatomic_options().device.empty())
110
111 impl_ = std::make_unique<RGPotEngine>(opt);
112 backend_ = impl_->backend();
113 isolatedMolecule_ = backend_ == "uma" && opt.task_name == "omol";
114 driver_ = impl_->worldRank() == 0;
115 std::cout
116 << "RgpotPot: in-process rgpot backend=" << backend_
117 << " (dlopen: "
118 "libnwchemc/libcpmdc/librgpot_metatomic_engine/librgpot_xtb_engine)"
119 << '\n';
120 // Finalize is registered first. The grouped-exit handler is next, and
121 // the stop handler is last, so exit runs stop, then Finalize, then _Exit.
122 impl_->finalizeMpiAtExit();
123 impl_->armGroupedExit();
125 if (!driver_)
126 serveWorker();
127}
std::string backend_
Definition RgpotPot.h:92
std::unique_ptr< RGPotEngine > impl_
Definition RgpotPot.h:91
bool isolatedMolecule_
Definition RgpotPot.h:93
bool driver_
Definition RgpotPot.h:94
void serveWorker()
Definition RgpotPot.cpp:322
void releaseWorkersAtExit()
Definition RgpotPot.cpp:204
const metatomic_options_t & metatomic_options() const
const xtb_options_t & xtb_options() const
const rgpot_options_t & rgpot_options() const
std::string scf_type
Definition RGPotEngine.h:12
std::string title
Definition RGPotEngine.h:20
std::string xtb_paramset
Definition RGPotEngine.h:38
std::string basis
Definition RGPotEngine.h:10
std::string scratch_dir
Definition RGPotEngine.h:22
std::string engine_path
Definition RGPotEngine.h:17
std::string params_path
Definition RGPotEngine.h:27
std::string backend
Definition RGPotEngine.h:9
std::string extensions_directory
Definition RGPotEngine.h:33
std::string engine_library
Definition RGPotEngine.h:18
double xtb_electronic_temperature
Definition RGPotEngine.h:40
std::string permanent_dir
Definition RGPotEngine.h:25
std::string functional
Definition RGPotEngine.h:13
std::string length_unit
Definition RGPotEngine.h:32
bool torch_determinism_strict
Definition RGPotEngine.h:36
std::string device
Definition RGPotEngine.h:31
std::string model_path
Definition RGPotEngine.h:30
std::string task_name
Definition RGPotEngine.h:47
std::string engine_root
Definition RGPotEngine.h:19
double uncertainty_threshold
Definition RGPotEngine.h:35
std::string theory
Definition RGPotEngine.h:11
std::string input_block
Definition RGPotEngine.h:24

◆ ~RgpotPot()

RgpotPot::~RgpotPot ( )
override

Definition at line 129 of file RgpotPot.cpp.

129 {
130 const bool grouped = impl_ && impl_->calculatorWorld() > 1;
131 stopAndDrop();
132 // ~CPMDPot dlcloses libcpmdc. On several ranks that runs Fortran
133 // destructors while another rank may already be in MPI_Finalize.
134 // The process reclaims the engine at _Exit.
135 if (grouped)
136 static_cast<void>(impl_.release());
137}
void stopAndDrop()
Definition RgpotPot.cpp:177

◆ RgpotPot() [2/2]

RgpotPot::RgpotPot ( const RgpotPot & )
delete

Member Function Documentation

◆ backend()

const std::string & RgpotPot::backend ( ) const
inlinenodiscardnoexcept

Definition at line 63 of file RgpotPot.h.

63{ return backend_; }

◆ computeBatch()

void RgpotPot::computeBatch ( long nSystems,
long nAtoms,
const double *const * positions,
const int *const * atomicNrs,
double *const * forces,
double * energies,
const double *const * boxes,
const std::int64_t * owners,
const std::int64_t * route )
private

Definition at line 282 of file RgpotPot.cpp.

287 {
288 const int mine = impl_->calculatorIndex();
289 auto ownerOf = [&](long j) { return static_cast<int>(route[j]); };
290 if (impl_->calculatorWorld() <= 1) {
291 for (long j = 0; j < nSystems; j++) {
292 impl_->selectOrbitals(orbital_key(owners, j));
293 impl_->force(nAtoms, positions[j], atomicNrs[j], forces[j], &energies[j],
294 boxes[j]);
295 }
296 return;
297 }
298 std::vector<char> ok(static_cast<size_t>(nSystems), 1);
299 std::vector<std::string> errors(static_cast<size_t>(nSystems));
300 for (long j = 0; j < nSystems; j++) {
301 if (ownerOf(j) == mine) {
302 impl_->selectOrbitals(orbital_key(owners, j));
303 ok[static_cast<size_t>(j)] =
304 evaluate(nAtoms, positions[j], atomicNrs[j], forces[j], &energies[j],
305 boxes[j], errors[static_cast<size_t>(j)]);
306 }
307 }
308 // Every share runs before any rank raises, so all ranks leave together.
309 long failed = -1;
310 for (long j = 0; j < nSystems; j++) {
311 const int owner = ownerOf(j);
312 if (!impl_->shareResult(owner, nAtoms, forces[j], &energies[j],
313 ok[static_cast<size_t>(j)] != 0,
314 errors[static_cast<size_t>(j)]) &&
315 failed < 0)
316 failed = j;
317 }
318 if (failed >= 0)
319 raise_failure(failed, ownerOf(failed), errors[static_cast<size_t>(failed)]);
320}
bool evaluate(long N, const double *R, const int *atomicNrs, double *F, double *U, const double *box, std::string &error)
Definition RgpotPot.cpp:255

◆ computeSingle()

void RgpotPot::computeSingle ( long N,
const double * R,
const int * atomicNrs,
double * F,
double * U,
const double * box,
int group )
private

Definition at line 266 of file RgpotPot.cpp.

268 {
269 // `group`, the one whose stored orbitals are nearest, computes; its
270 // first rank's result reaches every rank. rgpot keeps the engine's error
271 // agreement inside a calculator, so the other groups stay out of it.
272 std::string error;
273 bool ok = true;
274 if (impl_->calculatorIndex() == group) {
275 impl_->selectOrbitals(kSingleKey);
276 ok = evaluate(N, R, atomicNrs, F, U, box, error);
277 }
278 if (!impl_->shareResult(group, N, F, U, ok, error))
279 raise_failure(0, group, error);
280}
std::string error()
Definition DynLib.h:82

◆ engineAvailable()

bool RgpotPot::engineAvailable ( ) const
nodiscard

Definition at line 139 of file RgpotPot.cpp.

139{ return impl_ && impl_->available(); }

◆ evaluate()

bool RgpotPot::evaluate ( long N,
const double * R,
const int * atomicNrs,
double * F,
double * U,
const double * box,
std::string & error )
private

Definition at line 255 of file RgpotPot.cpp.

257 {
258 const auto t0 = std::chrono::steady_clock::now();
259 const bool ok = try_force(*impl_, N, R, atomicNrs, F, U, box, error);
260 busy_ += std::chrono::duration<double>(std::chrono::steady_clock::now() - t0)
261 .count();
262 systemsDone_ += 1.0;
263 return ok;
264}
double busy_
Definition RgpotPot.h:99
double systemsDone_
Definition RgpotPot.h:100

◆ exchangeGroupUse()

void RgpotPot::exchangeGroupUse ( )
private

Definition at line 163 of file RgpotPot.cpp.

163 {
164 const int groups = impl_->calculatorGroups();
165 use_.busy.assign(static_cast<size_t>(groups), 0.0);
166 use_.systems.assign(static_cast<size_t>(groups), 0.0);
167 for (int g = 0; g < groups; g++) {
168 double tally[3] = {busy_, systemsDone_, 0.0};
169 double unused = 0.0;
170 std::string error;
171 static_cast<void>(impl_->shareResult(g, 1, tally, &unused, true, error));
172 use_.busy[static_cast<size_t>(g)] = tally[0];
173 use_.systems[static_cast<size_t>(g)] = tally[1];
174 }
175}
eonc::CalculatorGroupUse use_
Definition RgpotPot.h:101

◆ force()

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

Implements eonc::Potential.

Definition at line 385 of file RgpotPot.cpp.

386 {
387 if (variance)
388 *variance = 0.0;
389 if (impl_->calculatorWorld() <= 1) {
390 impl_->selectOrbitals(kSingleKey);
391 impl_->force(N, R, atomicNrs, F, U, box);
392 return;
393 }
394 const auto t0 = std::chrono::steady_clock::now();
395 if (!schedule_)
396 schedule_ =
397 std::make_unique<eonc::GroupSchedule>(impl_->calculatorGroups());
398 // The group whose last geometry is nearest holds the warmest orbitals.
399 const int group = schedule_->nearest(R, 3 * N, box);
400 schedule_->settle(kSingleKey, group);
401 schedule_->record(kSingleKey, R, 3 * N, box);
402 std::int64_t hdr[4] = {kSingle, N, 1, group};
403 impl_->broadcastFromDriver(hdr, sizeof(hdr));
404 impl_->broadcastFromDriver(const_cast<double *>(R), 3 * N * sizeof(double));
405 impl_->broadcastFromDriver(const_cast<int *>(atomicNrs), N * sizeof(int));
406 impl_->broadcastFromDriver(const_cast<double *>(box), 9 * sizeof(double));
407 // Before the evaluation, so a failure that ends the job still stops
408 // the workers.
410 computeSingle(N, R, atomicNrs, F, U, box, group);
411 const double dt =
412 std::chrono::duration<double>(std::chrono::steady_clock::now() - t0)
413 .count();
414 use_.wall += dt;
415 use_.singleWall += dt;
416 use_.singles++;
417}
std::unique_ptr< eonc::GroupSchedule > schedule_
Definition RgpotPot.h:103
void computeSingle(long N, const double *R, const int *atomicNrs, double *F, double *U, const double *box, int group)
Definition RgpotPot.cpp:266

◆ forceBatch()

void RgpotPot::forceBatch ( long nSystems,
long nAtoms,
const double *const * positions,
const int *const * atomicNrs,
double *const * forces,
double * energies,
double * variances,
const double *const * boxes )
overridevirtual

Reimplemented from eonc::Potential.

Definition at line 427 of file RgpotPot.cpp.

431 {
432 forceBatchOwned(nSystems, nAtoms, positions, atomicNrs, forces, energies,
433 variances, boxes, nullptr);
434}
void forceBatchOwned(long nSystems, long nAtoms, const double *const *positions, const int *const *atomicNrs, double *const *forces, double *energies, double *variances, const double *const *boxes, const long *owners) override
owners[j] is system j's identity (a NEB image, a ring bead): it names the orbitals the engine starts ...
Definition RgpotPot.cpp:436

◆ forceBatchOwned()

void RgpotPot::forceBatchOwned ( long nSystems,
long nAtoms,
const double *const * positions,
const int *const * atomicNrs,
double *const * forces,
double * energies,
double * variances,
const double *const * boxes,
const long * owners )
overridevirtual

owners[j] is system j's identity (a NEB image, a ring bead): it names the orbitals the engine starts from and keeps the system on one group across steps.

Without it, or below zero, the batch position stands in.

Reimplemented from eonc::Potential.

Definition at line 436 of file RgpotPot.cpp.

441 {
442 std::vector<std::int64_t> ids(static_cast<size_t>(nSystems), -1);
443 if (owners) {
444 for (long j = 0; j < nSystems; j++)
445 ids[static_cast<size_t>(j)] = owners[j];
446 }
447 const bool grouped = impl_->calculatorWorld() > 1;
448 const auto t0 = std::chrono::steady_clock::now();
449 std::vector<std::int64_t> route(static_cast<size_t>(nSystems), 0);
450 if (grouped) {
451 if (!schedule_)
452 schedule_ =
453 std::make_unique<eonc::GroupSchedule>(impl_->calculatorGroups());
454 std::vector<std::int64_t> keys(static_cast<size_t>(nSystems));
455 for (long j = 0; j < nSystems; j++)
456 keys[static_cast<size_t>(j)] = orbital_key(ids.data(), j);
457 const std::vector<int> groupOf = schedule_->assign(keys);
458 for (long j = 0; j < nSystems; j++) {
459 route[static_cast<size_t>(j)] = groupOf[static_cast<size_t>(j)];
460 schedule_->record(keys[static_cast<size_t>(j)], positions[j], 3 * nAtoms,
461 boxes[j]);
462 }
463 std::int64_t hdr[4] = {kBatch, nAtoms, nSystems, 0};
464 impl_->broadcastFromDriver(hdr, sizeof(hdr));
465 std::vector<double> R(static_cast<size_t>(3 * nAtoms * nSystems));
466 std::vector<int> Z(static_cast<size_t>(nAtoms * nSystems));
467 std::vector<double> box(static_cast<size_t>(9 * nSystems));
468 for (long j = 0; j < nSystems; j++) {
469 std::copy(positions[j], positions[j] + 3 * nAtoms,
470 R.begin() + 3 * nAtoms * j);
471 std::copy(atomicNrs[j], atomicNrs[j] + nAtoms, Z.begin() + nAtoms * j);
472 std::copy(boxes[j], boxes[j] + 9, box.begin() + 9 * j);
473 }
474 impl_->broadcastFromDriver(R.data(), R.size() * sizeof(double));
475 impl_->broadcastFromDriver(Z.data(), Z.size() * sizeof(int));
476 impl_->broadcastFromDriver(box.data(), box.size() * sizeof(double));
477 impl_->broadcastFromDriver(ids.data(), ids.size() * sizeof(std::int64_t));
478 impl_->broadcastFromDriver(route.data(),
479 route.size() * sizeof(std::int64_t));
480 }
482 computeBatch(nSystems, nAtoms, positions, atomicNrs, forces, energies, boxes,
483 ids.data(), route.data());
484 if (grouped) {
485 use_.wall +=
486 std::chrono::duration<double>(std::chrono::steady_clock::now() - t0)
487 .count();
488 use_.batches++;
489 }
490 for (long j = 0; j < nSystems; j++) {
491 if (variances)
492 variances[j] = 0.0;
494 }
495}
void computeBatch(long nSystems, long nAtoms, const double *const *positions, const int *const *atomicNrs, double *const *forces, double *energies, const double *const *boxes, const std::int64_t *owners, const std::int64_t *route)
Definition RgpotPot.cpp:282
void recordForceCall() noexcept
Charge one evaluated geometry to this potential and its owning registry.

◆ groupUse()

const eonc::CalculatorGroupUse & RgpotPot::groupUse ( ) const
inlinenodiscardnoexcept

Calculator-group accounting on the driver.

The per-group columns are filled when the driver stops the workers; the driver prints them then.

Definition at line 67 of file RgpotPot.h.

67 {
68 return use_;
69 }

◆ isThreadSafe()

bool RgpotPot::isThreadSafe ( ) const
inlinenodiscardoverridevirtualnoexcept

Whether this potential's force() can be called from multiple threads on the SAME instance.

Python-based potentials return false. Potentials with internal mutex (MetatomicPotential) return true but serialize internally – use needsPerImageInstance() to check if separate instances would enable true parallelism.

Reimplemented from eonc::Potential.

Definition at line 39 of file RgpotPot.h.

39{ return false; }

◆ operator=()

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

◆ releaseWorkersAtExit()

void RgpotPot::releaseWorkersAtExit ( )
private

Definition at line 204 of file RgpotPot.cpp.

204 {
205 if (g_driver || !impl_ || impl_->calculatorWorld() <= 1)
206 return;
207 // Runs before the MPI_Finalize handler registered in the constructor.
208 // After a failed engine call the workers can sit in a collective that
209 // the stop broadcast never meets; the grouped exit handler aborts the
210 // world instead.
211 g_driver = this;
212 std::atexit([] {
213 if (g_driver && !RGPotEngine::mpiAbortRequested())
214 g_driver->stopAndDrop();
215 });
216}
static bool mpiAbortRequested() noexcept
True once rgpot asked for MPI_Abort at exit in this process.

◆ requiresIsolatedMoleculeLayout()

bool RgpotPot::requiresIsolatedMoleculeLayout ( ) const
inlinenodiscardoverridevirtualnoexcept

Molecular tasks retain Cartesian coordinates across storage-cell faces.

Reimplemented from eonc::Potential.

Definition at line 60 of file RgpotPot.h.

60 {
61 return backend_ == "nwchemc" || isolatedMolecule_;
62 }

◆ sendStop()

void RgpotPot::sendStop ( )
private

Definition at line 153 of file RgpotPot.cpp.

153 {
154 if (!impl_ || !driver_ || stopped_ || impl_->calculatorWorld() <= 1)
155 return;
156 std::int64_t hdr[4] = {kStop, 0, 0, 0};
157 impl_->broadcastFromDriver(hdr, sizeof(hdr));
158 stopped_ = true;
159 if (g_driver == this)
160 g_driver = nullptr;
161}
bool stopped_
Definition RgpotPot.h:95

◆ serveWorker()

void RgpotPot::serveWorker ( )
private

Definition at line 322 of file RgpotPot.cpp.

322 {
323 for (;;) {
324 std::int64_t hdr[4] = {kStop, 0, 0, 0};
325 impl_->broadcastFromDriver(hdr, sizeof(hdr));
326 if (hdr[0] == kStop) {
327 // Drop CPMD, wait until the driver has dropped it too, then exit 0.
328 // MPI_Finalize is collective and runs from the exit handler.
329 try {
331 } catch (const std::exception &) {
332 }
333 if (!dropped_) {
334 impl_->shutdownModule();
335 dropped_ = true;
336 }
337 std::int64_t ack[4] = {kDown, 0, 0, 0};
338 impl_->broadcastFromDriver(ack, sizeof(ack));
339 impl_->armGroupedExit();
340 std::exit(0);
341 }
342 const long n = static_cast<long>(hdr[1]);
343 const long m = hdr[0] == kBatch ? static_cast<long>(hdr[2]) : 1;
344 std::vector<double> R(static_cast<size_t>(3 * n * m));
345 std::vector<int> Z(static_cast<size_t>(n * m));
346 std::vector<double> box(static_cast<size_t>(9 * m));
347 impl_->broadcastFromDriver(R.data(), R.size() * sizeof(double));
348 impl_->broadcastFromDriver(Z.data(), Z.size() * sizeof(int));
349 impl_->broadcastFromDriver(box.data(), box.size() * sizeof(double));
350 std::vector<std::int64_t> ids(static_cast<size_t>(m), -1);
351 std::vector<std::int64_t> route(static_cast<size_t>(m), 0);
352 if (hdr[0] == kBatch) {
353 impl_->broadcastFromDriver(ids.data(), ids.size() * sizeof(std::int64_t));
354 impl_->broadcastFromDriver(route.data(),
355 route.size() * sizeof(std::int64_t));
356 }
357 std::vector<double> F(R.size()), U(static_cast<size_t>(m));
358 // A failed request raised on the driver too; the driver decides
359 // whether the job goes on, so a worker keeps serving.
360 if (hdr[0] == kSingle) {
361 try {
362 computeSingle(n, R.data(), Z.data(), F.data(), U.data(), box.data(),
363 static_cast<int>(hdr[3]));
364 } catch (const std::runtime_error &) {
365 }
366 continue;
367 }
368 std::vector<const double *> pos(static_cast<size_t>(m)), bx(pos.size());
369 std::vector<const int *> nrs(pos.size());
370 std::vector<double *> frc(pos.size());
371 for (long j = 0; j < m; j++) {
372 pos[static_cast<size_t>(j)] = R.data() + 3 * n * j;
373 nrs[static_cast<size_t>(j)] = Z.data() + n * j;
374 frc[static_cast<size_t>(j)] = F.data() + 3 * n * j;
375 bx[static_cast<size_t>(j)] = box.data() + 9 * j;
376 }
377 try {
378 computeBatch(m, n, pos.data(), nrs.data(), frc.data(), U.data(),
379 bx.data(), ids.data(), route.data());
380 } catch (const std::runtime_error &) {
381 }
382 }
383}
bool dropped_
Definition RgpotPot.h:96
void exchangeGroupUse()
Definition RgpotPot.cpp:163

◆ stopAndDrop()

void RgpotPot::stopAndDrop ( )
private

Definition at line 177 of file RgpotPot.cpp.

177 {
178 if (!impl_)
179 return;
180 const bool grouped = driver_ && impl_->calculatorWorld() > 1;
181 if (grouped && !stopped_) {
182 sendStop();
183 try {
185 std::cout << "RgpotPot: " << use_.table() << std::flush;
186 } catch (const std::exception &ex) {
187 std::cerr << "RgpotPot: no calculator-group summary: " << ex.what()
188 << '\n';
189 }
190 }
191 if (!dropped_) {
192 impl_->shutdownModule();
193 dropped_ = true;
194 }
195 if (impl_->calculatorWorld() > 1)
196 impl_->armGroupedExit();
197 if (grouped && !acked_) {
198 std::int64_t hdr[4] = {kDown, 0, 0, 0};
199 impl_->broadcastFromDriver(hdr, sizeof(hdr));
200 acked_ = true;
201 }
202}
bool acked_
Definition RgpotPot.h:97
void sendStop()
Definition RgpotPot.cpp:153

◆ supportsBatchEvaluation()

bool RgpotPot::supportsBatchEvaluation ( ) const
nodiscardoverridevirtualnoexcept

With cpmdc calculator groups ([RgpotPot] ranks_per_image), a batch is spread over the groups by eonc::GroupSchedule: a system stays on the group that holds its orbitals, at most ceil(M / G) systems per group, then every rank receives every result, so all ranks keep the same band.

Reimplemented from eonc::Potential.

Definition at line 419 of file RgpotPot.cpp.

419 {
420 // One calculator gains from a batch too when the engine keeps orbitals
421 // per key: the batch carries each image's or bead's identity, a single
422 // force() does not.
423 return impl_ &&
424 (impl_->calculatorGroups() > 1 || impl_->keepsOrbitalsPerKey());
425}

Member Data Documentation

◆ acked_

bool RgpotPot::acked_ {false}
private

Definition at line 97 of file RgpotPot.h.

97{false};

◆ backend_

std::string RgpotPot::backend_
private

Definition at line 92 of file RgpotPot.h.

◆ busy_

double RgpotPot::busy_ {0.0}
private

Definition at line 99 of file RgpotPot.h.

99{0.0};

◆ driver_

bool RgpotPot::driver_ {true}
private

Definition at line 94 of file RgpotPot.h.

94{true};

◆ dropped_

bool RgpotPot::dropped_ {false}
private

Definition at line 96 of file RgpotPot.h.

96{false};

◆ impl_

std::unique_ptr<RGPotEngine> RgpotPot::impl_
private

Definition at line 91 of file RgpotPot.h.

◆ isolatedMolecule_

bool RgpotPot::isolatedMolecule_ {false}
private

Definition at line 93 of file RgpotPot.h.

93{false};

◆ schedule_

std::unique_ptr<eonc::GroupSchedule> RgpotPot::schedule_
private

Definition at line 103 of file RgpotPot.h.

◆ stopped_

bool RgpotPot::stopped_ {false}
private

Definition at line 95 of file RgpotPot.h.

95{false};

◆ systemsDone_

double RgpotPot::systemsDone_ {0.0}
private

Definition at line 100 of file RgpotPot.h.

100{0.0};

◆ use_

eonc::CalculatorGroupUse RgpotPot::use_
private

Definition at line 101 of file RgpotPot.h.


The documentation for this class was generated from the following files:
  • /home/runner/work/eOn/eOn/include/eon/potentials/Rgpot/RgpotPot.h
  • /home/runner/work/eOn/eOn/client/potentials/Rgpot/RgpotPot.cpp