Loading...
Searching...
No Matches
LAMMPSPot.h
Go to the documentation of this file.
1/*
2** This file is part of eOn.
3**
4** SPDX-License-Identifier: BSD-3-Clause
5**
6** Copyright (c) 2010--present, eOn Development Team
7** All rights reserved.
8**
9** Repo:
10** https://github.com/TheochemUI/eOn
11*/
12// serves as an interface between LAMMPS potentials maintained by SANDIA
13
14#pragma once
15
16#include "eon/Parameters.h"
17#include "eon/Potential.h"
18
19#include <mutex>
20
21class LAMMPSPot : public Potential {
22
23public:
24 [[nodiscard]] bool needsPerImageInstance() const noexcept override {
25 return true;
26 }
27 LAMMPSPot(const Parameters &p);
28 ~LAMMPSPot();
29 void cleanMemory();
30 void force(long N, const double *R, const int *atomicNrs, double *F,
31 double *U, double *variance, const double *box) override;
32
33private:
34 int lammpsThr{0};
35#ifdef EONMPI
36 MPI_Comm mpiComm;
37#endif
39 double oldBox[9]{};
40 void *LAMMPSObj{nullptr};
41 void makeNewLAMMPS(long N, const double *R, const int *atomicNrs,
42 const double *box);
43 bool realunits{false};
44
45#if !defined(EONMPI) && !defined(IS_WINDOWS)
46 // Process-per-image evaluation. NEB drives intermediate images on separate
47 // std::threads; if each thread opened LAMMPS in this process they would all
48 // share one MPI_COMM_WORLD and their concurrent reduction collectives would
49 // collide (heap corruption / MPI_ERR_OP). Instead each LAMMPSPot forks a
50 // dedicated worker process that owns its LAMMPS instance, so every image runs
51 // in its own process with its own MPI_COMM_WORLD and true parallelism.
52 // Not available on Windows (no fork/pipe).
53 // Respawns allowed after a worker times out, dies, or reports an error.
54 // A single transient failure must not poison the job: every later
55 // evaluation would return the impassable wall, no minimisation could ever
56 // meet its force criterion, and the search would be discarded as a minimum
57 // that failed to converge. Respawning is safe now that the teardown is
58 // bounded rather than waiting on a wedged child forever. The budget is
59 // finite so a worker that cannot be revived still ends the search instead
60 // of looping.
62 // Serialises the request/response exchange with the worker. eOn minimises
63 // the two endpoints of a saddle concurrently, and when both share this
64 // instance the two threads interleave writes and reads on the same pipe.
65 // The protocol is a bare byte stream with no framing, so an interleaved
66 // exchange is read as corrupt: the worker reports an evaluation error, the
67 // next send finds a closed pipe, and the worker dies, all within the first
68 // three force calls of the minimisation. Uncontended when instances really
69 // are per-image.
70 std::mutex workerMutex;
71 int workerPid{-1};
72 int reqFd{-1}; // parent writes requests here (child stdin side)
73 int resFd{-1}; // parent reads results here (child stdout side)
74 bool workerSpawned{false};
75
76 // Fork the worker child on first use; child enters runWorkerLoop().
77 void ensureWorker();
78 // Child main loop: read requests, evaluate, write results; never returns.
79 [[noreturn]] void runWorkerLoop();
80 void stopWorker();
81#endif
82 // In-process LAMMPS force evaluation (used directly on Windows/MPI, and
83 // inside the worker child on POSIX).
84 void forceLocal(long N, const double *R, const int *atomicNrs, double *F,
85 double *U, const double *box);
86};
double oldBox[9]
Definition LAMMPSPot.h:39
int reqFd
Definition LAMMPSPot.h:72
int workerPid
Definition LAMMPSPot.h:71
void makeNewLAMMPS(long N, const double *R, const int *atomicNrs, const double *box)
LAMMPSPot(const Parameters &p)
Definition LAMMPSPot.cpp:40
int lammpsThr
Definition LAMMPSPot.h:34
bool workerSpawned
Definition LAMMPSPot.h:74
void cleanMemory()
Definition LAMMPSPot.cpp:63
int workerRespawnsLeft
Definition LAMMPSPot.h:61
bool realunits
Definition LAMMPSPot.h:43
void stopWorker()
void * LAMMPSObj
Definition LAMMPSPot.h:40
bool needsPerImageInstance() const noexcept override
Whether NEB should create separate Potential instances per image for true parallel force evaluation.
Definition LAMMPSPot.h:24
void ensureWorker()
void runWorkerLoop()
long numberOfAtoms
Definition LAMMPSPot.h:38
void force(long N, const double *R, const int *atomicNrs, double *F, double *U, double *variance, const double *box) override
void forceLocal(long N, const double *R, const int *atomicNrs, double *F, double *U, const double *box)
std::mutex workerMutex
Definition LAMMPSPot.h:70
int resFd
Definition LAMMPSPot.h:73
Potential(PotType a_ptype)
Definition Potential.h:35