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
2a1e9acf
Commit
2a1e9acf
authored
Feb 15, 2017
by
Peter Eastman
Browse files
Optimizations to updateParametersInContext() for CUDA
parent
67091abf
Changes
5
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
181 additions
and
88 deletions
+181
-88
platforms/cuda/include/CudaContext.h
platforms/cuda/include/CudaContext.h
+13
-1
platforms/cuda/include/CudaKernels.h
platforms/cuda/include/CudaKernels.h
+37
-1
platforms/cuda/src/CudaContext.cpp
platforms/cuda/src/CudaContext.cpp
+43
-10
platforms/cuda/src/CudaKernels.cpp
platforms/cuda/src/CudaKernels.cpp
+81
-76
platforms/cuda/src/kernels/utilities.cu
platforms/cuda/src/kernels/utilities.cu
+7
-0
No files found.
platforms/cuda/include/CudaContext.h
View file @
2a1e9acf
...
...
@@ -9,7 +9,7 @@
* Biological Structures at Stanford, funded under the NIH Roadmap for *
* Medical Research, grant U54 GM072970. See https://simtk.org. *
* *
* Portions copyright (c) 2009-201
6
Stanford University and the Authors. *
* Portions copyright (c) 2009-201
7
Stanford University and the Authors. *
* Authors: Peter Eastman *
* Contributors: *
* *
...
...
@@ -494,6 +494,10 @@ public:
CudaNonbondedUtilities
&
getNonbondedUtilities
()
{
return
*
nonbonded
;
}
/**
* Set the particle charges. These are packed into the fourth element of the posq array.
*/
void
setCharges
(
const
std
::
vector
<
double
>&
charges
);
/**
* Get the thread used by this context for executing parallel computations.
*/
...
...
@@ -577,6 +581,12 @@ public:
* and order to be revalidated.
*/
void
invalidateMolecules
();
/**
* Mark that the current molecule definitions from one particular force (and hence the atom order)
* may be invalid. This should be called whenever force field parameters change. It will cause the
* definitions and order to be revalidated.
*/
bool
invalidateMolecules
(
CudaForceInfo
*
force
);
private:
/**
* Compute a sorted list of device indices in decreasing order of desirability
...
...
@@ -626,6 +636,7 @@ private:
CUfunction
clearFourBuffersKernel
;
CUfunction
clearFiveBuffersKernel
;
CUfunction
clearSixBuffersKernel
;
CUfunction
setChargesKernel
;
std
::
vector
<
CudaForceInfo
*>
forces
;
std
::
vector
<
Molecule
>
molecules
;
std
::
vector
<
MoleculeGroup
>
moleculeGroups
;
...
...
@@ -638,6 +649,7 @@ private:
CudaArray
*
energyBuffer
;
CudaArray
*
energyParamDerivBuffer
;
CudaArray
*
atomIndexDevice
;
CudaArray
*
chargeBuffer
;
std
::
vector
<
std
::
string
>
energyParamDerivNames
;
std
::
map
<
std
::
string
,
double
>
energyParamDerivWorkspace
;
std
::
vector
<
int
>
atomIndex
;
...
...
platforms/cuda/include/CudaKernels.h
View file @
2a1e9acf
...
...
@@ -9,7 +9,7 @@
* Biological Structures at Stanford, funded under the NIH Roadmap for *
* Medical Research, grant U54 GM072970. See https://simtk.org. *
* *
* Portions copyright (c) 2008-201
6
Stanford University and the Authors. *
* Portions copyright (c) 2008-201
7
Stanford University and the Authors. *
* Authors: Peter Eastman *
* Contributors: *
* *
...
...
@@ -292,9 +292,11 @@ public:
*/
void
copyParametersToContext
(
ContextImpl
&
context
,
const
HarmonicBondForce
&
force
);
private:
class
ForceInfo
;
int
numBonds
;
bool
hasInitializedKernel
;
CudaContext
&
cu
;
ForceInfo
*
info
;
const
System
&
system
;
CudaArray
*
params
;
};
...
...
@@ -332,9 +334,11 @@ public:
*/
void
copyParametersToContext
(
ContextImpl
&
context
,
const
CustomBondForce
&
force
);
private:
class
ForceInfo
;
int
numBonds
;
bool
hasInitializedKernel
;
CudaContext
&
cu
;
ForceInfo
*
info
;
const
System
&
system
;
CudaParameterSet
*
params
;
CudaArray
*
globals
;
...
...
@@ -375,9 +379,11 @@ public:
*/
void
copyParametersToContext
(
ContextImpl
&
context
,
const
HarmonicAngleForce
&
force
);
private:
class
ForceInfo
;
int
numAngles
;
bool
hasInitializedKernel
;
CudaContext
&
cu
;
ForceInfo
*
info
;
const
System
&
system
;
CudaArray
*
params
;
};
...
...
@@ -415,9 +421,11 @@ public:
*/
void
copyParametersToContext
(
ContextImpl
&
context
,
const
CustomAngleForce
&
force
);
private:
class
ForceInfo
;
int
numAngles
;
bool
hasInitializedKernel
;
CudaContext
&
cu
;
ForceInfo
*
info
;
const
System
&
system
;
CudaParameterSet
*
params
;
CudaArray
*
globals
;
...
...
@@ -458,9 +466,11 @@ public:
*/
void
copyParametersToContext
(
ContextImpl
&
context
,
const
PeriodicTorsionForce
&
force
);
private:
class
ForceInfo
;
int
numTorsions
;
bool
hasInitializedKernel
;
CudaContext
&
cu
;
ForceInfo
*
info
;
const
System
&
system
;
CudaArray
*
params
;
};
...
...
@@ -498,9 +508,11 @@ public:
*/
void
copyParametersToContext
(
ContextImpl
&
context
,
const
RBTorsionForce
&
force
);
private:
class
ForceInfo
;
int
numTorsions
;
bool
hasInitializedKernel
;
CudaContext
&
cu
;
ForceInfo
*
info
;
const
System
&
system
;
CudaArray
*
params1
;
CudaArray
*
params2
;
...
...
@@ -539,9 +551,11 @@ public:
*/
void
copyParametersToContext
(
ContextImpl
&
context
,
const
CMAPTorsionForce
&
force
);
private:
class
ForceInfo
;
int
numTorsions
;
bool
hasInitializedKernel
;
CudaContext
&
cu
;
ForceInfo
*
info
;
const
System
&
system
;
std
::
vector
<
int2
>
mapPositionsVec
;
CudaArray
*
coefficients
;
...
...
@@ -582,9 +596,11 @@ public:
*/
void
copyParametersToContext
(
ContextImpl
&
context
,
const
CustomTorsionForce
&
force
);
private:
class
ForceInfo
;
int
numTorsions
;
bool
hasInitializedKernel
;
CudaContext
&
cu
;
ForceInfo
*
info
;
const
System
&
system
;
CudaParameterSet
*
params
;
CudaArray
*
globals
;
...
...
@@ -647,12 +663,14 @@ private:
const
char
*
getMaxValue
()
const
{
return
"make_int2(2147483647, 2147483647)"
;}
const
char
*
getSortKey
()
const
{
return
"value.y"
;}
};
class
ForceInfo
;
class
PmeIO
;
class
PmePreComputation
;
class
PmePostComputation
;
class
SyncStreamPreComputation
;
class
SyncStreamPostComputation
;
CudaContext
&
cu
;
ForceInfo
*
info
;
bool
hasInitializedFFT
;
CudaArray
*
sigmaEpsilon
;
CudaArray
*
exceptionParams
;
...
...
@@ -724,8 +742,10 @@ public:
*/
void
copyParametersToContext
(
ContextImpl
&
context
,
const
CustomNonbondedForce
&
force
);
private:
class
ForceInfo
;
void
initInteractionGroups
(
const
CustomNonbondedForce
&
force
,
const
std
::
string
&
interactionSource
,
const
std
::
vector
<
std
::
string
>&
tableTypes
);
CudaContext
&
cu
;
ForceInfo
*
info
;
CudaParameterSet
*
params
;
CudaArray
*
globals
;
CudaArray
*
interactionGroupData
;
...
...
@@ -775,10 +795,12 @@ public:
*/
void
copyParametersToContext
(
ContextImpl
&
context
,
const
GBSAOBCForce
&
force
);
private:
class
ForceInfo
;
double
prefactor
,
surfaceAreaFactor
,
cutoff
;
bool
hasCreatedKernels
;
int
maxTiles
;
CudaContext
&
cu
;
ForceInfo
*
info
;
CudaArray
*
params
;
CudaArray
*
bornSum
;
CudaArray
*
bornRadii
;
...
...
@@ -825,10 +847,12 @@ public:
*/
void
copyParametersToContext
(
ContextImpl
&
context
,
const
CustomGBForce
&
force
);
private:
class
ForceInfo
;
double
cutoff
;
bool
hasInitializedKernels
,
needParameterGradient
,
needEnergyParamDerivs
;
int
maxTiles
,
numComputedValues
;
CudaContext
&
cu
;
ForceInfo
*
info
;
CudaParameterSet
*
params
;
CudaParameterSet
*
computedValues
;
CudaParameterSet
*
energyDerivs
;
...
...
@@ -882,9 +906,11 @@ public:
*/
void
copyParametersToContext
(
ContextImpl
&
context
,
const
CustomExternalForce
&
force
);
private:
class
ForceInfo
;
int
numParticles
;
bool
hasInitializedKernel
;
CudaContext
&
cu
;
ForceInfo
*
info
;
const
System
&
system
;
CudaParameterSet
*
params
;
CudaArray
*
globals
;
...
...
@@ -926,9 +952,11 @@ public:
*/
void
copyParametersToContext
(
ContextImpl
&
context
,
const
CustomHbondForce
&
force
);
private:
class
ForceInfo
;
int
numDonors
,
numAcceptors
;
bool
hasInitializedKernel
;
CudaContext
&
cu
;
ForceInfo
*
info
;
CudaParameterSet
*
donorParams
;
CudaParameterSet
*
acceptorParams
;
CudaArray
*
globals
;
...
...
@@ -978,9 +1006,11 @@ public:
void
copyParametersToContext
(
ContextImpl
&
context
,
const
CustomCentroidBondForce
&
force
);
private:
class
ForceInfo
;
int
numGroups
,
numBonds
;
bool
needEnergyParamDerivs
;
CudaContext
&
cu
;
ForceInfo
*
info
;
CudaParameterSet
*
params
;
CudaArray
*
globals
;
CudaArray
*
groupParticles
;
...
...
@@ -1031,8 +1061,10 @@ public:
void
copyParametersToContext
(
ContextImpl
&
context
,
const
CustomCompoundBondForce
&
force
);
private:
class
ForceInfo
;
int
numBonds
;
CudaContext
&
cu
;
ForceInfo
*
info
;
CudaParameterSet
*
params
;
CudaArray
*
globals
;
std
::
vector
<
std
::
string
>
globalParamNames
;
...
...
@@ -1077,7 +1109,9 @@ public:
void
copyParametersToContext
(
ContextImpl
&
context
,
const
CustomManyParticleForce
&
force
);
private:
class
ForceInfo
;
CudaContext
&
cu
;
ForceInfo
*
info
;
bool
hasInitializedKernel
;
NonbondedMethod
nonbondedMethod
;
int
maxNeighborPairs
,
forceWorkgroupSize
,
findNeighborsWorkgroupSize
;
...
...
@@ -1139,9 +1173,11 @@ public:
*/
void
copyParametersToContext
(
ContextImpl
&
context
,
const
GayBerneForce
&
force
);
private:
class
ForceInfo
;
class
ReorderListener
;
void
sortAtoms
();
CudaContext
&
cu
;
ForceInfo
*
info
;
bool
hasInitializedKernels
;
int
numRealParticles
,
numExceptions
,
maxNeighborBlocks
;
GayBerneForce
::
NonbondedMethod
nonbondedMethod
;
...
...
platforms/cuda/src/CudaContext.cpp
View file @
2a1e9acf
...
...
@@ -6,7 +6,7 @@
* Biological Structures at Stanford, funded under the NIH Roadmap for *
* Medical Research, grant U54 GM072970. See https://simtk.org. *
* *
* Portions copyright (c) 2009-201
6
Stanford University and the Authors. *
* Portions copyright (c) 2009-201
7
Stanford University and the Authors. *
* Authors: Peter Eastman *
* Contributors: *
* *
...
...
@@ -108,7 +108,8 @@ static int executeInWindows(const string &command) {
CudaContext
::
CudaContext
(
const
System
&
system
,
int
deviceIndex
,
bool
useBlockingSync
,
const
string
&
precision
,
const
string
&
compiler
,
const
string
&
tempDir
,
const
std
::
string
&
hostCompiler
,
CudaPlatform
::
PlatformData
&
platformData
)
:
system
(
system
),
currentStream
(
0
),
time
(
0.0
),
platformData
(
platformData
),
stepCount
(
0
),
computeForceCount
(
0
),
stepsSinceReorder
(
99999
),
contextIsValid
(
false
),
atomsWereReordered
(
false
),
hasCompilerKernel
(
false
),
isNvccAvailable
(
false
),
pinnedBuffer
(
NULL
),
posq
(
NULL
),
posqCorrection
(
NULL
),
velm
(
NULL
),
force
(
NULL
),
energyBuffer
(
NULL
),
energyParamDerivBuffer
(
NULL
),
atomIndexDevice
(
NULL
),
integration
(
NULL
),
expression
(
NULL
),
bonded
(
NULL
),
nonbonded
(
NULL
),
thread
(
NULL
)
{
pinnedBuffer
(
NULL
),
posq
(
NULL
),
posqCorrection
(
NULL
),
velm
(
NULL
),
force
(
NULL
),
energyBuffer
(
NULL
),
energyParamDerivBuffer
(
NULL
),
atomIndexDevice
(
NULL
),
chargeBuffer
(
NULL
),
integration
(
NULL
),
expression
(
NULL
),
bonded
(
NULL
),
nonbonded
(
NULL
),
thread
(
NULL
)
{
// Determine what compiler to use.
this
->
compiler
=
"
\"
"
+
compiler
+
"
\"
"
;
...
...
@@ -291,6 +292,7 @@ CudaContext::CudaContext(const System& system, int deviceIndex, bool useBlocking
clearFourBuffersKernel
=
getKernel
(
utilities
,
"clearFourBuffers"
);
clearFiveBuffersKernel
=
getKernel
(
utilities
,
"clearFiveBuffers"
);
clearSixBuffersKernel
=
getKernel
(
utilities
,
"clearSixBuffers"
);
setChargesKernel
=
getKernel
(
utilities
,
"setCharges"
);
// Set defines based on the requested precision.
...
...
@@ -407,6 +409,8 @@ CudaContext::~CudaContext() {
delete
energyParamDerivBuffer
;
if
(
atomIndexDevice
!=
NULL
)
delete
atomIndexDevice
;
if
(
chargeBuffer
!=
NULL
)
delete
chargeBuffer
;
if
(
integration
!=
NULL
)
delete
integration
;
if
(
expression
!=
NULL
)
...
...
@@ -860,6 +864,25 @@ void CudaContext::clearAutoclearBuffers() {
}
}
void
CudaContext
::
setCharges
(
const
vector
<
double
>&
charges
)
{
if
(
chargeBuffer
==
NULL
)
chargeBuffer
=
new
CudaArray
(
*
this
,
numAtoms
,
useDoublePrecision
?
sizeof
(
double
)
:
sizeof
(
float
),
"chargeBuffer"
);
if
(
getUseDoublePrecision
())
{
double
*
c
=
(
double
*
)
getPinnedBuffer
();
for
(
int
i
=
0
;
i
<
charges
.
size
();
i
++
)
c
[
i
]
=
charges
[
i
];
chargeBuffer
->
upload
(
c
);
}
else
{
float
*
c
=
(
float
*
)
getPinnedBuffer
();
for
(
int
i
=
0
;
i
<
charges
.
size
();
i
++
)
c
[
i
]
=
(
float
)
charges
[
i
];
chargeBuffer
->
upload
(
c
);
}
void
*
args
[]
=
{
&
chargeBuffer
->
getDevicePointer
(),
&
posq
->
getDevicePointer
(),
&
atomIndexDevice
->
getDevicePointer
(),
&
numAtoms
};
executeKernel
(
setChargesKernel
,
args
,
numAtoms
);
}
/**
* This class ensures that atom reordering doesn't break virtual sites.
*/
...
...
@@ -1058,9 +1081,19 @@ void CudaContext::findMoleculeGroups() {
}
void
CudaContext
::
invalidateMolecules
()
{
if
(
numAtoms
==
0
||
nonbonded
==
NULL
||
!
nonbonded
->
getUseCutoff
())
for
(
int
i
=
0
;
i
<
forces
.
size
();
i
++
)
if
(
invalidateMolecules
(
forces
[
i
]))
return
;
}
bool
CudaContext
::
invalidateMolecules
(
CudaForceInfo
*
force
)
{
if
(
numAtoms
==
0
||
nonbonded
==
NULL
||
!
nonbonded
->
getUseCutoff
())
return
false
;
bool
valid
=
true
;
int
forceIndex
=
-
1
;
for
(
int
i
=
0
;
i
<
forces
.
size
();
i
++
)
if
(
forces
[
i
]
==
force
)
forceIndex
=
i
;
for
(
int
group
=
0
;
valid
&&
group
<
(
int
)
moleculeGroups
.
size
();
group
++
)
{
MoleculeGroup
&
mol
=
moleculeGroups
[
group
];
vector
<
int
>&
instances
=
mol
.
instances
;
...
...
@@ -1075,22 +1108,21 @@ void CudaContext::invalidateMolecules() {
Molecule
&
m2
=
molecules
[
instances
[
j
]];
int
offset2
=
offsets
[
j
];
for
(
int
i
=
0
;
i
<
(
int
)
atoms
.
size
()
&&
valid
;
i
++
)
{
for
(
int
k
=
0
;
k
<
(
int
)
forces
.
size
();
k
++
)
if
(
!
forces
[
k
]
->
areParticlesIdentical
(
atoms
[
i
]
+
offset1
,
atoms
[
i
]
+
offset2
))
if
(
!
force
->
areParticlesIdentical
(
atoms
[
i
]
+
offset1
,
atoms
[
i
]
+
offset2
))
valid
=
false
;
}
// See if the force groups are identical.
for
(
int
i
=
0
;
i
<
(
int
)
forces
.
size
()
&&
valid
;
i
++
)
{
for
(
int
k
=
0
;
k
<
(
int
)
m1
.
groups
[
i
].
size
()
&&
valid
;
k
++
)
if
(
!
force
s
[
i
]
->
areGroupsIdentical
(
m1
.
groups
[
i
][
k
],
m2
.
groups
[
i
][
k
]))
if
(
valid
&&
forceIndex
>
-
1
)
{
for
(
int
k
=
0
;
k
<
(
int
)
m1
.
groups
[
forceIndex
].
size
()
&&
valid
;
k
++
)
if
(
!
force
->
areGroupsIdentical
(
m1
.
groups
[
forceIndex
][
k
],
m2
.
groups
[
forceIndex
][
k
]))
valid
=
false
;
}
}
}
if
(
valid
)
return
;
return
false
;
// The list of which molecules are identical is no longer valid. We need to restore the
// atoms to their original order, rebuild the list of identical molecules, and sort them
...
...
@@ -1158,6 +1190,7 @@ void CudaContext::invalidateMolecules() {
for
(
int
i
=
0
;
i
<
(
int
)
reorderListeners
.
size
();
i
++
)
reorderListeners
[
i
]
->
execute
();
reorderAtoms
();
return
true
;
}
void
CudaContext
::
reorderAtoms
()
{
...
...
platforms/cuda/src/CudaKernels.cpp
View file @
2a1e9acf
This diff is collapsed.
Click to expand it.
platforms/cuda/src/kernels/utilities.cu
View file @
2a1e9acf
...
...
@@ -73,4 +73,11 @@ __global__ void clearSixBuffers(int* __restrict__ buffer1, int size1, int* __res
clearSingleBuffer
(
buffer6
,
size6
);
}
/**
* Record the atomic charges into the posq array.
*/
__global__
void
setCharges
(
real
*
__restrict__
charges
,
real4
*
__restrict__
posq
,
int
*
__restrict__
atomOrder
,
int
numAtoms
)
{
for
(
int
i
=
blockDim
.
x
*
blockIdx
.
x
+
threadIdx
.
x
;
i
<
numAtoms
;
i
+=
blockDim
.
x
*
gridDim
.
x
)
posq
[
i
].
w
=
charges
[
atomOrder
[
i
]];
}
}
\ No newline at end of file
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