Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
tsoc
openmm
Commits
6a0e1bd5
Commit
6a0e1bd5
authored
May 26, 2017
by
Peter Eastman
Browse files
OpenCL and CUDA CustomIntegrator avoid duplicate computations
parent
9d779a39
Changes
5
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
44 additions
and
23 deletions
+44
-23
openmmapi/src/CustomIntegratorUtilities.cpp
openmmapi/src/CustomIntegratorUtilities.cpp
+8
-10
platforms/cuda/include/CudaKernels.h
platforms/cuda/include/CudaKernels.h
+1
-0
platforms/cuda/src/CudaKernels.cpp
platforms/cuda/src/CudaKernels.cpp
+18
-7
platforms/opencl/include/OpenCLKernels.h
platforms/opencl/include/OpenCLKernels.h
+1
-0
platforms/opencl/src/OpenCLKernels.cpp
platforms/opencl/src/OpenCLKernels.cpp
+16
-6
No files found.
openmmapi/src/CustomIntegratorUtilities.cpp
View file @
6a0e1bd5
...
...
@@ -37,6 +37,7 @@
#include <algorithm>
#include <set>
#include <sstream>
#include <utility>
using
namespace
OpenMM
;
using
namespace
std
;
...
...
@@ -250,26 +251,23 @@ void CustomIntegratorUtilities::enumeratePaths(int firstStep, vector<int> steps,
void
CustomIntegratorUtilities
::
analyzeForceComputationsForPath
(
vector
<
int
>&
steps
,
const
vector
<
bool
>&
needsForces
,
const
vector
<
bool
>&
needsEnergy
,
const
vector
<
bool
>&
invalidatesForces
,
const
vector
<
int
>&
forceGroup
,
vector
<
bool
>&
computeBoth
)
{
vector
<
int
>
candidatePoints
;
int
currentGroup
=
-
1
;
vector
<
pair
<
int
,
int
>
>
candidatePoints
;
for
(
int
step
:
steps
)
{
if
(
invalidatesForces
[
step
]
||
((
needsForces
[
step
]
||
needsEnergy
[
step
])
&&
forceGroup
[
step
]
!=
currentGroup
))
{
// Forces and energies are invalidated at this step, or it changes to a different force group,
// so anything from this point on won't affect what we do at earlier steps.
if
(
invalidatesForces
[
step
])
{
// Forces and energies are invalidated at this step, so anything from this point on won't affect what we do at earlier steps.
candidatePoints
.
clear
();
}
if
(
needsForces
[
step
]
||
needsEnergy
[
step
])
{
// See if this step affects what we do at earlier points.
for
(
int
candidate
:
candidatePoints
)
if
((
needsForces
[
candidate
]
&&
needsEnergy
[
step
])
||
(
needsEnergy
[
candidate
]
&&
needsForces
[
step
]))
computeBoth
[
candidate
]
=
true
;
for
(
auto
candidate
:
candidatePoints
)
if
(
candidate
.
second
==
forceGroup
[
step
]
&&
((
needsForces
[
candidate
.
first
]
&&
needsEnergy
[
step
])
||
(
needsEnergy
[
candidate
.
first
]
&&
needsForces
[
step
]))
)
computeBoth
[
candidate
.
first
]
=
true
;
// Add this to the list of candidates that might be affected by later steps.
candidatePoints
.
push_back
(
step
);
currentGroup
=
forceGroup
[
step
];
candidatePoints
.
push_back
(
make_pair
(
step
,
forceGroup
[
step
]));
}
}
}
...
...
platforms/cuda/include/CudaKernels.h
View file @
6a0e1bd5
...
...
@@ -1507,6 +1507,7 @@ private:
CudaArray
*
randomSeed
;
CudaArray
*
perDofEnergyParamDerivs
;
std
::
vector
<
CudaArray
*>
tabulatedFunctions
;
std
::
map
<
int
,
double
>
savedEnergy
;
std
::
map
<
int
,
CudaArray
*>
savedForces
;
std
::
set
<
int
>
validSavedForces
;
CudaParameterSet
*
perDofValues
;
...
...
platforms/cuda/src/CudaKernels.cpp
View file @
6a0e1bd5
...
...
@@ -7594,6 +7594,8 @@ void CudaIntegrateCustomStepKernel::execute(ContextImpl& context, CustomIntegrat
CudaIntegrationUtilities
&
integration
=
cu
.
getIntegrationUtilities
();
int
numAtoms
=
cu
.
getNumAtoms
();
int
numSteps
=
integrator
.
getNumComputations
();
if
(
!
forcesAreValid
)
savedEnergy
.
clear
();
// Loop over computation steps in the integrator and execute them.
...
...
@@ -7602,8 +7604,11 @@ void CudaIntegrateCustomStepKernel::execute(ContextImpl& context, CustomIntegrat
CUdeviceptr
posCorrection
=
(
cu
.
getUseMixedPrecision
()
?
cu
.
getPosqCorrection
().
getDevicePointer
()
:
0
);
for
(
int
step
=
0
;
step
<
numSteps
;
)
{
int
nextStep
=
step
+
1
;
int
forceGroups
=
forceGroupFlags
[
step
];
int
lastForceGroups
=
context
.
getLastForceGroups
();
if
((
needsForces
[
step
]
||
needsEnergy
[
step
])
&&
(
!
forcesAreValid
||
lastForceGroups
!=
forceGroupFlags
[
step
]))
{
bool
haveForces
=
(
!
needsForces
[
step
]
||
(
forcesAreValid
&&
lastForceGroups
==
forceGroups
));
bool
haveEnergy
=
(
!
needsEnergy
[
step
]
||
savedEnergy
.
find
(
forceGroups
)
!=
savedEnergy
.
end
());
if
(
!
haveForces
||
!
haveEnergy
)
{
if
(
forcesAreValid
)
{
if
(
savedForces
.
find
(
lastForceGroups
)
!=
savedForces
.
end
()
&&
validSavedForces
.
find
(
lastForceGroups
)
==
validSavedForces
.
end
())
{
// The forces are still valid. We just need a different force group right now. Save the old
...
...
@@ -7621,16 +7626,16 @@ void CudaIntegrateCustomStepKernel::execute(ContextImpl& context, CustomIntegrat
bool
computeForce
=
(
needsForces
[
step
]
||
computeBothForceAndEnergy
[
step
]);
bool
computeEnergy
=
(
needsEnergy
[
step
]
||
computeBothForceAndEnergy
[
step
]);
if
(
!
computeEnergy
&&
validSavedForces
.
find
(
forceGroup
Flags
[
step
]
)
!=
validSavedForces
.
end
())
{
if
(
!
computeEnergy
&&
validSavedForces
.
find
(
forceGroup
s
)
!=
validSavedForces
.
end
())
{
// We can just restore the forces we saved earlier.
savedForces
[
forceGroup
Flags
[
step
]
]
->
copyTo
(
cu
.
getForce
());
context
.
getLastForceGroups
()
=
forceGroup
Flags
[
step
]
;
savedForces
[
forceGroup
s
]
->
copyTo
(
cu
.
getForce
());
context
.
getLastForceGroups
()
=
forceGroup
s
;
}
else
{
recordChangedParameters
(
context
);
energy
=
context
.
calcForcesAndEnergy
(
computeForce
,
computeEnergy
,
forceGroup
Flags
[
step
]
);
energyFloat
=
(
float
)
energy
;
energy
=
context
.
calcForcesAndEnergy
(
computeForce
,
computeEnergy
,
forceGroup
s
);
savedEnergy
[
forceGroups
]
=
energy
;
if
(
needsEnergyParamDerivs
)
{
context
.
getEnergyParameterDerivatives
(
energyParamDerivs
);
if
(
perDofEnergyParamDerivNames
.
size
()
>
0
)
{
...
...
@@ -7649,6 +7654,10 @@ void CudaIntegrateCustomStepKernel::execute(ContextImpl& context, CustomIntegrat
}
forcesAreValid
=
true
;
}
if
(
needsEnergy
[
step
])
{
energy
=
savedEnergy
[
forceGroups
];
energyFloat
=
(
float
)
energy
;
}
if
(
needsGlobals
[
step
]
&&
!
deviceGlobalsAreCurrent
)
{
// Upload the global values to the device.
...
...
@@ -7725,8 +7734,10 @@ void CudaIntegrateCustomStepKernel::execute(ContextImpl& context, CustomIntegrat
if
(
blockEnd
[
step
]
!=
-
1
)
nextStep
=
blockEnd
[
step
];
// Return to the start of a while block.
}
if
(
invalidatesForces
[
step
])
if
(
invalidatesForces
[
step
])
{
forcesAreValid
=
false
;
savedEnergy
.
clear
();
}
step
=
nextStep
;
}
recordChangedParameters
(
context
);
...
...
platforms/opencl/include/OpenCLKernels.h
View file @
6a0e1bd5
...
...
@@ -1494,6 +1494,7 @@ private:
OpenCLArray
*
randomSeed
;
OpenCLArray
*
perDofEnergyParamDerivs
;
std
::
vector
<
OpenCLArray
*>
tabulatedFunctions
;
std
::
map
<
int
,
double
>
savedEnergy
;
std
::
map
<
int
,
OpenCLArray
*>
savedForces
;
std
::
set
<
int
>
validSavedForces
;
OpenCLParameterSet
*
perDofValues
;
...
...
platforms/opencl/src/OpenCLKernels.cpp
View file @
6a0e1bd5
...
...
@@ -7937,13 +7937,18 @@ void OpenCLIntegrateCustomStepKernel::execute(ContextImpl& context, CustomIntegr
OpenCLIntegrationUtilities& integration = cl.getIntegrationUtilities();
int numAtoms = cl.getNumAtoms();
int numSteps = integrator.getNumComputations();
if (!forcesAreValid)
savedEnergy.clear();
// Loop over computation steps in the integrator and execute them.
for (int step = 0; step < numSteps; ) {
int nextStep = step+1;
int forceGroups = forceGroupFlags[step];
int lastForceGroups = context.getLastForceGroups();
if ((needsForces[step] || needsEnergy[step]) && (!forcesAreValid || lastForceGroups != forceGroupFlags[step])) {
bool haveForces = (!needsForces[step] || (forcesAreValid && lastForceGroups == forceGroups));
bool haveEnergy = (!needsEnergy[step] || savedEnergy.find(forceGroups) != savedEnergy.end());
if (!haveForces || !haveEnergy) {
if (forcesAreValid) {
if (savedForces.find(lastForceGroups) != savedForces.end() && validSavedForces.find(lastForceGroups) == validSavedForces.end()) {
// The forces are still valid. We just need a different force group right now. Save the old
...
...
@@ -7961,15 +7966,16 @@ void OpenCLIntegrateCustomStepKernel::execute(ContextImpl& context, CustomIntegr
bool computeForce = (needsForces[step] || computeBothForceAndEnergy[step]);
bool computeEnergy = (needsEnergy[step] || computeBothForceAndEnergy[step]);
if (!computeEnergy && validSavedForces.find(forceGroup
Flags[step]
) != validSavedForces.end()) {
if (!computeEnergy && validSavedForces.find(forceGroup
s
) != validSavedForces.end()) {
// We can just restore the forces we saved earlier.
savedForces[forceGroup
Flags[step]
]->copyTo(cl.getForce());
context.getLastForceGroups() = forceGroup
Flags[step]
;
savedForces[forceGroup
s
]->copyTo(cl.getForce());
context.getLastForceGroups() = forceGroup
s
;
}
else {
recordChangedParameters(context);
energy = context.calcForcesAndEnergy(computeForce, computeEnergy, forceGroupFlags[step]);
energy = context.calcForcesAndEnergy(computeForce, computeEnergy, forceGroups);
savedEnergy[forceGroups] = energy;
if (needsEnergyParamDerivs) {
context.getEnergyParameterDerivatives(energyParamDerivs);
if (perDofEnergyParamDerivNames.size() > 0) {
...
...
@@ -7988,6 +7994,8 @@ void OpenCLIntegrateCustomStepKernel::execute(ContextImpl& context, CustomIntegr
forcesAreValid = true;
}
}
if (needsEnergy[step])
energy = savedEnergy[forceGroups];
if (needsGlobals[step] && !deviceGlobalsAreCurrent) {
// Upload the global values to the device.
...
...
@@ -8067,8 +8075,10 @@ void OpenCLIntegrateCustomStepKernel::execute(ContextImpl& context, CustomIntegr
if (blockEnd[step] != -1)
nextStep = blockEnd[step]; // Return to the start of a while block.
}
if (invalidatesForces[step])
if (invalidatesForces[step])
{
forcesAreValid = false;
savedEnergy.clear();
}
step = nextStep;
}
recordChangedParameters(context);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment