Commit ab92c8ed authored by Peter Eastman's avatar Peter Eastman
Browse files

CustomCVForce applies ForceInfos from inner context to the outer one

parent 0a4867a9
...@@ -9,7 +9,7 @@ ...@@ -9,7 +9,7 @@
* Biological Structures at Stanford, funded under the NIH Roadmap for * * Biological Structures at Stanford, funded under the NIH Roadmap for *
* Medical Research, grant U54 GM072970. See https://simtk.org. * * Medical Research, grant U54 GM072970. See https://simtk.org. *
* * * *
* Portions copyright (c) 2009-2018 Stanford University and the Authors. * * Portions copyright (c) 2009-2019 Stanford University and the Authors. *
* Authors: Peter Eastman * * Authors: Peter Eastman *
* Contributors: * * Contributors: *
* * * *
...@@ -86,9 +86,13 @@ public: ...@@ -86,9 +86,13 @@ public:
*/ */
void initialize(); void initialize();
/** /**
* Add a CudaForce to this context. * Add a CudaForceInfo to this context.
*/ */
void addForce(CudaForceInfo* force); void addForce(CudaForceInfo* force);
/**
* Get all CudaForceInfos that have been added to this context.
*/
std::vector<CudaForceInfo*>& getForceInfos();
/** /**
* Get the CUcontext associated with this object. * Get the CUcontext associated with this object.
*/ */
......
...@@ -9,7 +9,7 @@ ...@@ -9,7 +9,7 @@
* Biological Structures at Stanford, funded under the NIH Roadmap for * * Biological Structures at Stanford, funded under the NIH Roadmap for *
* Medical Research, grant U54 GM072970. See https://simtk.org. * * Medical Research, grant U54 GM072970. See https://simtk.org. *
* * * *
* Portions copyright (c) 2008-2018 Stanford University and the Authors. * * Portions copyright (c) 2008-2019 Stanford University and the Authors. *
* Authors: Peter Eastman * * Authors: Peter Eastman *
* Contributors: * * Contributors: *
* * * *
...@@ -1268,6 +1268,7 @@ public: ...@@ -1268,6 +1268,7 @@ public:
*/ */
void copyParametersToContext(ContextImpl& context, const CustomCVForce& force); void copyParametersToContext(ContextImpl& context, const CustomCVForce& force);
private: private:
class ForceInfo;
class ReorderListener; class ReorderListener;
CudaContext& cu; CudaContext& cu;
bool hasInitializedListeners; bool hasInitializedListeners;
......
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
* Biological Structures at Stanford, funded under the NIH Roadmap for * * Biological Structures at Stanford, funded under the NIH Roadmap for *
* Medical Research, grant U54 GM072970. See https://simtk.org. * * Medical Research, grant U54 GM072970. See https://simtk.org. *
* * * *
* Portions copyright (c) 2009-2018 Stanford University and the Authors. * * Portions copyright (c) 2009-2019 Stanford University and the Authors. *
* Authors: Peter Eastman * * Authors: Peter Eastman *
* Contributors: * * Contributors: *
* * * *
...@@ -488,6 +488,10 @@ void CudaContext::addForce(CudaForceInfo* force) { ...@@ -488,6 +488,10 @@ void CudaContext::addForce(CudaForceInfo* force) {
forces.push_back(force); forces.push_back(force);
} }
vector<CudaForceInfo*>& CudaContext::getForceInfos() {
return forces;
}
void CudaContext::setAsCurrent() { void CudaContext::setAsCurrent() {
if (contextIsValid) if (contextIsValid)
cuCtxSetCurrent(context); cuCtxSetCurrent(context);
......
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
* Biological Structures at Stanford, funded under the NIH Roadmap for * * Biological Structures at Stanford, funded under the NIH Roadmap for *
* Medical Research, grant U54 GM072970. See https://simtk.org. * * Medical Research, grant U54 GM072970. See https://simtk.org. *
* * * *
* Portions copyright (c) 2008-2018 Stanford University and the Authors. * * Portions copyright (c) 2008-2019 Stanford University and the Authors. *
* Authors: Peter Eastman * * Authors: Peter Eastman *
* Contributors: * * Contributors: *
* * * *
...@@ -6646,6 +6646,26 @@ void CudaCalcGayBerneForceKernel::sortAtoms() { ...@@ -6646,6 +6646,26 @@ void CudaCalcGayBerneForceKernel::sortAtoms() {
exclusionStartIndex.upload(startIndexVec); exclusionStartIndex.upload(startIndexVec);
} }
class CudaCalcCustomCVForceKernel::ForceInfo : public CudaForceInfo {
public:
ForceInfo(CudaForceInfo& force) : force(force) {
}
bool areParticlesIdentical(int particle1, int particle2) {
return force.areParticlesIdentical(particle1, particle2);
}
int getNumParticleGroups() {
return force.getNumParticleGroups();
}
void getParticlesInGroup(int index, std::vector<int>& particles) {
force.getParticlesInGroup(index, particles);
}
bool areGroupsIdentical(int group1, int group2) {
return force.areGroupsIdentical(group1, group2);
}
private:
CudaForceInfo& force;
};
class CudaCalcCustomCVForceKernel::ReorderListener : public CudaContext::ReorderListener { class CudaCalcCustomCVForceKernel::ReorderListener : public CudaContext::ReorderListener {
public: public:
ReorderListener(CudaContext& cu, CudaArray& invAtomOrder) : cu(cu), invAtomOrder(invAtomOrder) { ReorderListener(CudaContext& cu, CudaArray& invAtomOrder) : cu(cu), invAtomOrder(invAtomOrder) {
...@@ -6725,6 +6745,11 @@ void CudaCalcCustomCVForceKernel::initialize(const System& system, const CustomC ...@@ -6725,6 +6745,11 @@ void CudaCalcCustomCVForceKernel::initialize(const System& system, const CustomC
copyStateKernel = cu.getKernel(module, "copyState"); copyStateKernel = cu.getKernel(module, "copyState");
copyForcesKernel = cu.getKernel(module, "copyForces"); copyForcesKernel = cu.getKernel(module, "copyForces");
addForcesKernel = cu.getKernel(module, "addForces"); addForcesKernel = cu.getKernel(module, "addForces");
// This context needs to respect all forces in the inner context when reordering atoms.
for (CudaForceInfo* info : cu2.getForceInfos())
cu.addForce(new ForceInfo(*info));
} }
double CudaCalcCustomCVForceKernel::execute(ContextImpl& context, ContextImpl& innerContext, bool includeForces, bool includeEnergy) { double CudaCalcCustomCVForceKernel::execute(ContextImpl& context, ContextImpl& innerContext, bool includeForces, bool includeEnergy) {
......
...@@ -9,7 +9,7 @@ ...@@ -9,7 +9,7 @@
* Biological Structures at Stanford, funded under the NIH Roadmap for * * Biological Structures at Stanford, funded under the NIH Roadmap for *
* Medical Research, grant U54 GM072970. See https://simtk.org. * * Medical Research, grant U54 GM072970. See https://simtk.org. *
* * * *
* Portions copyright (c) 2009-2018 Stanford University and the Authors. * * Portions copyright (c) 2009-2019 Stanford University and the Authors. *
* Authors: Peter Eastman * * Authors: Peter Eastman *
* Contributors: * * Contributors: *
* * * *
...@@ -172,9 +172,13 @@ public: ...@@ -172,9 +172,13 @@ public:
*/ */
void initialize(); void initialize();
/** /**
* Add an OpenCLForce to this context. * Add an OpenCLForceInfo to this context.
*/ */
void addForce(OpenCLForceInfo* force); void addForce(OpenCLForceInfo* force);
/**
* Get all OpenCLForceInfos that have been added to this context.
*/
std::vector<OpenCLForceInfo*>& getForceInfos();
/** /**
* Get the cl::Context associated with this object. * Get the cl::Context associated with this object.
*/ */
......
...@@ -9,7 +9,7 @@ ...@@ -9,7 +9,7 @@
* Biological Structures at Stanford, funded under the NIH Roadmap for * * Biological Structures at Stanford, funded under the NIH Roadmap for *
* Medical Research, grant U54 GM072970. See https://simtk.org. * * Medical Research, grant U54 GM072970. See https://simtk.org. *
* * * *
* Portions copyright (c) 2008-2018 Stanford University and the Authors. * * Portions copyright (c) 2008-2019 Stanford University and the Authors. *
* Authors: Peter Eastman * * Authors: Peter Eastman *
* Contributors: * * Contributors: *
* * * *
...@@ -1245,6 +1245,7 @@ public: ...@@ -1245,6 +1245,7 @@ public:
*/ */
void copyParametersToContext(ContextImpl& context, const CustomCVForce& force); void copyParametersToContext(ContextImpl& context, const CustomCVForce& force);
private: private:
class ForceInfo;
class ReorderListener; class ReorderListener;
OpenCLContext& cl; OpenCLContext& cl;
bool hasInitializedKernels; bool hasInitializedKernels;
......
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
* Biological Structures at Stanford, funded under the NIH Roadmap for * * Biological Structures at Stanford, funded under the NIH Roadmap for *
* Medical Research, grant U54 GM072970. See https://simtk.org. * * Medical Research, grant U54 GM072970. See https://simtk.org. *
* * * *
* Portions copyright (c) 2009-2018 Stanford University and the Authors. * * Portions copyright (c) 2009-2019 Stanford University and the Authors. *
* Authors: Peter Eastman * * Authors: Peter Eastman *
* Contributors: * * Contributors: *
* * * *
...@@ -507,6 +507,10 @@ void OpenCLContext::addForce(OpenCLForceInfo* force) { ...@@ -507,6 +507,10 @@ void OpenCLContext::addForce(OpenCLForceInfo* force) {
forces.push_back(force); forces.push_back(force);
} }
vector<OpenCLForceInfo*>& OpenCLContext::getForceInfos() {
return forces;
}
string OpenCLContext::replaceStrings(const string& input, const std::map<std::string, std::string>& replacements) const { string OpenCLContext::replaceStrings(const string& input, const std::map<std::string, std::string>& replacements) const {
static set<char> symbolChars; static set<char> symbolChars;
if (symbolChars.size() == 0) { if (symbolChars.size() == 0) {
......
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
* Biological Structures at Stanford, funded under the NIH Roadmap for * * Biological Structures at Stanford, funded under the NIH Roadmap for *
* Medical Research, grant U54 GM072970. See https://simtk.org. * * Medical Research, grant U54 GM072970. See https://simtk.org. *
* * * *
* Portions copyright (c) 2008-2018 Stanford University and the Authors. * * Portions copyright (c) 2008-2019 Stanford University and the Authors. *
* Authors: Peter Eastman * * Authors: Peter Eastman *
* Contributors: * * Contributors: *
* * * *
...@@ -6925,6 +6925,26 @@ void OpenCLCalcGayBerneForceKernel::sortAtoms() { ...@@ -6925,6 +6925,26 @@ void OpenCLCalcGayBerneForceKernel::sortAtoms() {
exclusionStartIndex.upload(startIndexVec); exclusionStartIndex.upload(startIndexVec);
} }
class OpenCLCalcCustomCVForceKernel::ForceInfo : public OpenCLForceInfo {
public:
ForceInfo(OpenCLForceInfo& force) : OpenCLForceInfo(0), force(force) {
}
bool areParticlesIdentical(int particle1, int particle2) {
return force.areParticlesIdentical(particle1, particle2);
}
int getNumParticleGroups() {
return force.getNumParticleGroups();
}
void getParticlesInGroup(int index, std::vector<int>& particles) {
force.getParticlesInGroup(index, particles);
}
bool areGroupsIdentical(int group1, int group2) {
return force.areGroupsIdentical(group1, group2);
}
private:
OpenCLForceInfo& force;
};
class OpenCLCalcCustomCVForceKernel::ReorderListener : public OpenCLContext::ReorderListener { class OpenCLCalcCustomCVForceKernel::ReorderListener : public OpenCLContext::ReorderListener {
public: public:
ReorderListener(OpenCLContext& cl, OpenCLArray& invAtomOrder) : cl(cl), invAtomOrder(invAtomOrder) { ReorderListener(OpenCLContext& cl, OpenCLArray& invAtomOrder) : cl(cl), invAtomOrder(invAtomOrder) {
...@@ -7005,6 +7025,11 @@ void OpenCLCalcCustomCVForceKernel::initialize(const System& system, const Custo ...@@ -7005,6 +7025,11 @@ void OpenCLCalcCustomCVForceKernel::initialize(const System& system, const Custo
copyStateKernel = cl::Kernel(program, "copyState"); copyStateKernel = cl::Kernel(program, "copyState");
copyForcesKernel = cl::Kernel(program, "copyForces"); copyForcesKernel = cl::Kernel(program, "copyForces");
addForcesKernel = cl::Kernel(program, "addForces"); addForcesKernel = cl::Kernel(program, "addForces");
// This context needs to respect all forces in the inner context when reordering atoms.
for (OpenCLForceInfo* info : cl2.getForceInfos())
cl.addForce(new ForceInfo(*info));
} }
double OpenCLCalcCustomCVForceKernel::execute(ContextImpl& context, ContextImpl& innerContext, bool includeForces, bool includeEnergy) { double OpenCLCalcCustomCVForceKernel::execute(ContextImpl& context, ContextImpl& innerContext, bool includeForces, bool includeEnergy) {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment