Commit 36589f7c authored by peastman's avatar peastman
Browse files

Merge pull request #1057 from peastman/optimize

Very minor optimizations
parents 6431f0ac ef627240
...@@ -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) 2011-2012 Stanford University and the Authors. * * Portions copyright (c) 2011-2015 Stanford University and the Authors. *
* Authors: Peter Eastman * * Authors: Peter Eastman *
* Contributors: * * Contributors: *
* * * *
...@@ -130,7 +130,7 @@ private: ...@@ -130,7 +130,7 @@ private:
std::vector<std::vector<CudaArray*> > atomIndices; std::vector<std::vector<CudaArray*> > atomIndices;
std::vector<std::string> prefixCode; std::vector<std::string> prefixCode;
std::vector<void*> kernelArgs; std::vector<void*> kernelArgs;
int numForceBuffers, maxBonds; int numForceBuffers, maxBonds, allGroups;
bool hasInitializedKernels, hasInteractions; bool hasInitializedKernels, hasInteractions;
}; };
......
...@@ -1294,7 +1294,7 @@ private: ...@@ -1294,7 +1294,7 @@ private:
double prevStepSize, energy; double prevStepSize, energy;
float energyFloat; float energyFloat;
int numGlobalVariables; int numGlobalVariables;
bool hasInitializedKernels, deviceValuesAreCurrent, deviceGlobalsAreCurrent, modifiesParameters, keNeedsForce; bool hasInitializedKernels, deviceValuesAreCurrent, deviceGlobalsAreCurrent, modifiesParameters, keNeedsForce, hasAnyConstraints;
mutable bool localValuesAreCurrent; mutable bool localValuesAreCurrent;
CudaArray* globalValues; CudaArray* globalValues;
CudaArray* sumBuffer; CudaArray* sumBuffer;
......
...@@ -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) 2011-2012 Stanford University and the Authors. * * Portions copyright (c) 2011-2015 Stanford University and the Authors. *
* Authors: Peter Eastman * * Authors: Peter Eastman *
* Contributors: * * Contributors: *
* * * *
...@@ -34,7 +34,7 @@ ...@@ -34,7 +34,7 @@
using namespace OpenMM; using namespace OpenMM;
using namespace std; using namespace std;
CudaBondedUtilities::CudaBondedUtilities(CudaContext& context) : context(context), numForceBuffers(0), maxBonds(0), hasInitializedKernels(false) { CudaBondedUtilities::CudaBondedUtilities(CudaContext& context) : context(context), numForceBuffers(0), maxBonds(0), allGroups(0), hasInitializedKernels(false) {
} }
CudaBondedUtilities::~CudaBondedUtilities() { CudaBondedUtilities::~CudaBondedUtilities() {
...@@ -48,6 +48,7 @@ void CudaBondedUtilities::addInteraction(const vector<vector<int> >& atoms, cons ...@@ -48,6 +48,7 @@ void CudaBondedUtilities::addInteraction(const vector<vector<int> >& atoms, cons
forceAtoms.push_back(atoms); forceAtoms.push_back(atoms);
forceSource.push_back(source); forceSource.push_back(source);
forceGroup.push_back(group); forceGroup.push_back(group);
allGroups |= 1<<group;
} }
} }
...@@ -152,6 +153,8 @@ string CudaBondedUtilities::createForceSource(int forceIndex, int numBonds, int ...@@ -152,6 +153,8 @@ string CudaBondedUtilities::createForceSource(int forceIndex, int numBonds, int
} }
void CudaBondedUtilities::computeInteractions(int groups) { void CudaBondedUtilities::computeInteractions(int groups) {
if ((groups&allGroups) == 0)
return;
if (!hasInitializedKernels) { if (!hasInitializedKernels) {
hasInitializedKernels = true; hasInitializedKernels = true;
kernelArgs.push_back(&context.getForce().getDevicePointer()); kernelArgs.push_back(&context.getForce().getDevicePointer());
......
...@@ -5877,22 +5877,25 @@ void CudaIntegrateCustomStepKernel::prepareForComputation(ContextImpl& context, ...@@ -5877,22 +5877,25 @@ void CudaIntegrateCustomStepKernel::prepareForComputation(ContextImpl& context,
// Determine how each step will represent the position (as just a value, or a value plus a delta). // Determine how each step will represent the position (as just a value, or a value plus a delta).
hasAnyConstraints = (context.getSystem().getNumConstraints() > 0);
vector<bool> storePosAsDelta(numSteps, false); vector<bool> storePosAsDelta(numSteps, false);
vector<bool> loadPosAsDelta(numSteps, false); vector<bool> loadPosAsDelta(numSteps, false);
bool beforeConstrain = false; if (hasAnyConstraints) {
for (int step = numSteps-1; step >= 0; step--) { bool beforeConstrain = false;
if (stepType[step] == CustomIntegrator::ConstrainPositions) for (int step = numSteps-1; step >= 0; step--) {
beforeConstrain = true; if (stepType[step] == CustomIntegrator::ConstrainPositions)
else if (stepType[step] == CustomIntegrator::ComputePerDof && variable[step] == "x" && beforeConstrain) beforeConstrain = true;
storePosAsDelta[step] = true; else if (stepType[step] == CustomIntegrator::ComputePerDof && variable[step] == "x" && beforeConstrain)
} storePosAsDelta[step] = true;
bool storedAsDelta = false; }
for (int step = 0; step < numSteps; step++) { bool storedAsDelta = false;
loadPosAsDelta[step] = storedAsDelta; for (int step = 0; step < numSteps; step++) {
if (storePosAsDelta[step] == true) loadPosAsDelta[step] = storedAsDelta;
storedAsDelta = true; if (storePosAsDelta[step] == true)
if (stepType[step] == CustomIntegrator::ConstrainPositions) storedAsDelta = true;
storedAsDelta = false; if (stepType[step] == CustomIntegrator::ConstrainPositions)
storedAsDelta = false;
}
} }
// Identify steps that can be merged into a single kernel. // Identify steps that can be merged into a single kernel.
...@@ -6214,9 +6217,11 @@ void CudaIntegrateCustomStepKernel::execute(ContextImpl& context, CustomIntegrat ...@@ -6214,9 +6217,11 @@ void CudaIntegrateCustomStepKernel::execute(ContextImpl& context, CustomIntegrat
context.updateContextState(); context.updateContextState();
} }
else if (stepType[step] == CustomIntegrator::ConstrainPositions) { else if (stepType[step] == CustomIntegrator::ConstrainPositions) {
cu.getIntegrationUtilities().applyConstraints(integrator.getConstraintTolerance()); if (hasAnyConstraints) {
kernelArgs[step][0][1] = &posCorrection; cu.getIntegrationUtilities().applyConstraints(integrator.getConstraintTolerance());
cu.executeKernel(kernels[step][0], &kernelArgs[step][0][0], numAtoms); kernelArgs[step][0][1] = &posCorrection;
cu.executeKernel(kernels[step][0], &kernelArgs[step][0][0], numAtoms);
}
cu.getIntegrationUtilities().computeVirtualSites(); cu.getIntegrationUtilities().computeVirtualSites();
} }
else if (stepType[step] == CustomIntegrator::ConstrainVelocities) { else if (stepType[step] == CustomIntegrator::ConstrainVelocities) {
......
...@@ -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) 2011 Stanford University and the Authors. * * Portions copyright (c) 2011-2015 Stanford University and the Authors. *
* Authors: Peter Eastman * * Authors: Peter Eastman *
* Contributors: * * Contributors: *
* * * *
...@@ -137,7 +137,7 @@ private: ...@@ -137,7 +137,7 @@ private:
std::vector<OpenCLArray*> atomIndices; std::vector<OpenCLArray*> atomIndices;
std::vector<OpenCLArray*> bufferIndices; std::vector<OpenCLArray*> bufferIndices;
std::vector<std::string> prefixCode; std::vector<std::string> prefixCode;
int numForceBuffers, maxBonds; int numForceBuffers, maxBonds, allGroups;
bool hasInitializedKernels; bool hasInitializedKernels;
}; };
......
...@@ -1283,7 +1283,7 @@ private: ...@@ -1283,7 +1283,7 @@ private:
double prevStepSize, energy; double prevStepSize, energy;
float energyFloat; float energyFloat;
int numGlobalVariables; int numGlobalVariables;
bool hasInitializedKernels, deviceValuesAreCurrent, deviceGlobalsAreCurrent, modifiesParameters, keNeedsForce; bool hasInitializedKernels, deviceValuesAreCurrent, deviceGlobalsAreCurrent, modifiesParameters, keNeedsForce, hasAnyConstraints;
mutable bool localValuesAreCurrent; mutable bool localValuesAreCurrent;
OpenCLArray* globalValues; OpenCLArray* globalValues;
OpenCLArray* sumBuffer; OpenCLArray* sumBuffer;
......
...@@ -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) 2011-2012 Stanford University and the Authors. * * Portions copyright (c) 2011-2015 Stanford University and the Authors. *
* Authors: Peter Eastman * * Authors: Peter Eastman *
* Contributors: * * Contributors: *
* * * *
...@@ -33,7 +33,7 @@ ...@@ -33,7 +33,7 @@
using namespace OpenMM; using namespace OpenMM;
using namespace std; using namespace std;
OpenCLBondedUtilities::OpenCLBondedUtilities(OpenCLContext& context) : context(context), numForceBuffers(0), maxBonds(0), hasInitializedKernels(false) { OpenCLBondedUtilities::OpenCLBondedUtilities(OpenCLContext& context) : context(context), numForceBuffers(0), maxBonds(0), allGroups(0), hasInitializedKernels(false) {
} }
OpenCLBondedUtilities::~OpenCLBondedUtilities() { OpenCLBondedUtilities::~OpenCLBondedUtilities() {
...@@ -48,6 +48,7 @@ void OpenCLBondedUtilities::addInteraction(const vector<vector<int> >& atoms, co ...@@ -48,6 +48,7 @@ void OpenCLBondedUtilities::addInteraction(const vector<vector<int> >& atoms, co
forceAtoms.push_back(atoms); forceAtoms.push_back(atoms);
forceSource.push_back(source); forceSource.push_back(source);
forceGroup.push_back(group); forceGroup.push_back(group);
allGroups |= 1<<group;
int width = 1; int width = 1;
while (width < (int) atoms[0].size()) while (width < (int) atoms[0].size())
width *= 2; width *= 2;
...@@ -73,7 +74,7 @@ void OpenCLBondedUtilities::initialize(const System& system) { ...@@ -73,7 +74,7 @@ void OpenCLBondedUtilities::initialize(const System& system) {
if (numForces == 0) if (numForces == 0)
return; return;
// Build the lists of atom indicse and buffer indices. // Build the lists of atom indices and buffer indices.
vector<vector<cl_uint> > bufferVec(numForces); vector<vector<cl_uint> > bufferVec(numForces);
vector<vector<int> > bufferCounter(numForces, vector<int>(system.getNumParticles(), 0)); vector<vector<int> > bufferCounter(numForces, vector<int>(system.getNumParticles(), 0));
...@@ -253,6 +254,8 @@ string OpenCLBondedUtilities::createForceSource(int forceIndex, int numBonds, in ...@@ -253,6 +254,8 @@ string OpenCLBondedUtilities::createForceSource(int forceIndex, int numBonds, in
} }
void OpenCLBondedUtilities::computeInteractions(int groups) { void OpenCLBondedUtilities::computeInteractions(int groups) {
if ((groups&allGroups) == 0)
return;
if (!hasInitializedKernels) { if (!hasInitializedKernels) {
hasInitializedKernels = true; hasInitializedKernels = true;
for (int i = 0; i < (int) forceSets.size(); i++) { for (int i = 0; i < (int) forceSets.size(); i++) {
......
...@@ -6141,22 +6141,25 @@ void OpenCLIntegrateCustomStepKernel::prepareForComputation(ContextImpl& context ...@@ -6141,22 +6141,25 @@ void OpenCLIntegrateCustomStepKernel::prepareForComputation(ContextImpl& context
// Determine how each step will represent the position (as just a value, or a value plus a delta). // Determine how each step will represent the position (as just a value, or a value plus a delta).
hasAnyConstraints = (context.getSystem().getNumConstraints() > 0);
vector<bool> storePosAsDelta(numSteps, false); vector<bool> storePosAsDelta(numSteps, false);
vector<bool> loadPosAsDelta(numSteps, false); vector<bool> loadPosAsDelta(numSteps, false);
bool beforeConstrain = false; if (hasAnyConstraints) {
for (int step = numSteps-1; step >= 0; step--) { bool beforeConstrain = false;
if (stepType[step] == CustomIntegrator::ConstrainPositions) for (int step = numSteps-1; step >= 0; step--) {
beforeConstrain = true; if (stepType[step] == CustomIntegrator::ConstrainPositions)
else if (stepType[step] == CustomIntegrator::ComputePerDof && variable[step] == "x" && beforeConstrain) beforeConstrain = true;
storePosAsDelta[step] = true; else if (stepType[step] == CustomIntegrator::ComputePerDof && variable[step] == "x" && beforeConstrain)
} storePosAsDelta[step] = true;
bool storedAsDelta = false; }
for (int step = 0; step < numSteps; step++) { bool storedAsDelta = false;
loadPosAsDelta[step] = storedAsDelta; for (int step = 0; step < numSteps; step++) {
if (storePosAsDelta[step] == true) loadPosAsDelta[step] = storedAsDelta;
storedAsDelta = true; if (storePosAsDelta[step] == true)
if (stepType[step] == CustomIntegrator::ConstrainPositions) storedAsDelta = true;
storedAsDelta = false; if (stepType[step] == CustomIntegrator::ConstrainPositions)
storedAsDelta = false;
}
} }
// Identify steps that can be merged into a single kernel. // Identify steps that can be merged into a single kernel.
...@@ -6478,8 +6481,10 @@ void OpenCLIntegrateCustomStepKernel::execute(ContextImpl& context, CustomIntegr ...@@ -6478,8 +6481,10 @@ void OpenCLIntegrateCustomStepKernel::execute(ContextImpl& context, CustomIntegr
context.updateContextState(); context.updateContextState();
} }
else if (stepType[step] == CustomIntegrator::ConstrainPositions) { else if (stepType[step] == CustomIntegrator::ConstrainPositions) {
cl.getIntegrationUtilities().applyConstraints(integrator.getConstraintTolerance()); if (hasAnyConstraints) {
cl.executeKernel(kernels[step][0], numAtoms); cl.getIntegrationUtilities().applyConstraints(integrator.getConstraintTolerance());
cl.executeKernel(kernels[step][0], numAtoms);
}
cl.getIntegrationUtilities().computeVirtualSites(); cl.getIntegrationUtilities().computeVirtualSites();
} }
else if (stepType[step] == CustomIntegrator::ConstrainVelocities) { else if (stepType[step] == CustomIntegrator::ConstrainVelocities) {
......
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