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
6dc2f9a8
Commit
6dc2f9a8
authored
Mar 16, 2015
by
peastman
Browse files
CUDA and OpenCL implementations of CMAPTorsionForce::updateParametersInContext()
parent
474bbf7a
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
200 additions
and
4 deletions
+200
-4
platforms/cuda/include/CudaKernels.h
platforms/cuda/include/CudaKernels.h
+1
-0
platforms/cuda/src/CudaKernels.cpp
platforms/cuda/src/CudaKernels.cpp
+41
-1
platforms/cuda/tests/TestCudaCMAPTorsionForce.cpp
platforms/cuda/tests/TestCudaCMAPTorsionForce.cpp
+58
-1
platforms/opencl/include/OpenCLKernels.h
platforms/opencl/include/OpenCLKernels.h
+1
-0
platforms/opencl/src/OpenCLKernels.cpp
platforms/opencl/src/OpenCLKernels.cpp
+41
-1
platforms/opencl/tests/TestOpenCLCMAPTorsionForce.cpp
platforms/opencl/tests/TestOpenCLCMAPTorsionForce.cpp
+58
-1
No files found.
platforms/cuda/include/CudaKernels.h
View file @
6dc2f9a8
...
...
@@ -532,6 +532,7 @@ private:
bool
hasInitializedKernel
;
CudaContext
&
cu
;
const
System
&
system
;
std
::
vector
<
int2
>
mapPositionsVec
;
CudaArray
*
coefficients
;
CudaArray
*
mapPositions
;
CudaArray
*
torsionMaps
;
...
...
platforms/cuda/src/CudaKernels.cpp
View file @
6dc2f9a8
...
...
@@ -1127,7 +1127,7 @@ void CudaCalcCMAPTorsionForceKernel::initialize(const System& system, const CMAP
return
;
int
numMaps
=
force
.
getNumMaps
();
vector
<
float4
>
coeffVec
;
vector
<
int2
>
mapPositionsVec
(
numMaps
);
mapPositionsVec
.
resize
(
numMaps
);
vector
<
double
>
energy
;
vector
<
vector
<
double
>
>
c
;
int
currentPosition
=
0
;
...
...
@@ -1167,6 +1167,46 @@ double CudaCalcCMAPTorsionForceKernel::execute(ContextImpl& context, bool includ
}
void
CudaCalcCMAPTorsionForceKernel
::
copyParametersToContext
(
ContextImpl
&
context
,
const
CMAPTorsionForce
&
force
)
{
int
numMaps
=
force
.
getNumMaps
();
int
numContexts
=
cu
.
getPlatformData
().
contexts
.
size
();
int
startIndex
=
cu
.
getContextIndex
()
*
force
.
getNumTorsions
()
/
numContexts
;
int
endIndex
=
(
cu
.
getContextIndex
()
+
1
)
*
force
.
getNumTorsions
()
/
numContexts
;
numTorsions
=
endIndex
-
startIndex
;
if
(
mapPositions
->
getSize
()
!=
numMaps
)
throw
OpenMMException
(
"updateParametersInContext: The number of maps has changed"
);
if
(
torsionMaps
->
getSize
()
!=
numTorsions
)
throw
OpenMMException
(
"updateParametersInContext: The number of CMAP torsions has changed"
);
// Update the maps.
vector
<
float4
>
coeffVec
;
vector
<
double
>
energy
;
vector
<
vector
<
double
>
>
c
;
int
currentPosition
=
0
;
for
(
int
i
=
0
;
i
<
numMaps
;
i
++
)
{
int
size
;
force
.
getMapParameters
(
i
,
size
,
energy
);
if
(
size
!=
mapPositionsVec
[
i
].
y
)
throw
OpenMMException
(
"updateParametersInContext: The size of a map has changed"
);
CMAPTorsionForceImpl
::
calcMapDerivatives
(
size
,
energy
,
c
);
currentPosition
+=
4
*
size
*
size
;
for
(
int
j
=
0
;
j
<
size
*
size
;
j
++
)
{
coeffVec
.
push_back
(
make_float4
((
float
)
c
[
j
][
0
],
(
float
)
c
[
j
][
1
],
(
float
)
c
[
j
][
2
],
(
float
)
c
[
j
][
3
]));
coeffVec
.
push_back
(
make_float4
((
float
)
c
[
j
][
4
],
(
float
)
c
[
j
][
5
],
(
float
)
c
[
j
][
6
],
(
float
)
c
[
j
][
7
]));
coeffVec
.
push_back
(
make_float4
((
float
)
c
[
j
][
8
],
(
float
)
c
[
j
][
9
],
(
float
)
c
[
j
][
10
],
(
float
)
c
[
j
][
11
]));
coeffVec
.
push_back
(
make_float4
((
float
)
c
[
j
][
12
],
(
float
)
c
[
j
][
13
],
(
float
)
c
[
j
][
14
],
(
float
)
c
[
j
][
15
]));
}
}
coefficients
->
upload
(
coeffVec
);
// Update the indices.
vector
<
int
>
torsionMapsVec
(
numTorsions
);
for
(
int
i
=
0
;
i
<
numTorsions
;
i
++
)
{
int
index
[
8
];
force
.
getTorsionParameters
(
i
,
torsionMapsVec
[
i
],
index
[
0
],
index
[
1
],
index
[
2
],
index
[
3
],
index
[
4
],
index
[
5
],
index
[
6
],
index
[
7
]);
}
torsionMaps
->
upload
(
torsionMapsVec
);
}
class
CudaCustomTorsionForceInfo
:
public
CudaForceInfo
{
...
...
platforms/cuda/tests/TestCudaCMAPTorsionForce.cpp
View file @
6dc2f9a8
...
...
@@ -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) 2010-201
2
Stanford University and the Authors. *
* Portions copyright (c) 2010-201
5
Stanford University and the Authors. *
* Authors: Peter Eastman *
* Contributors: *
* *
...
...
@@ -105,11 +105,68 @@ void testCMAPTorsions() {
}
}
void
testChangingParameters
()
{
// Create a system with two maps and one torsion.
const
int
mapSize
=
8
;
System
system
;
for
(
int
i
=
0
;
i
<
5
;
i
++
)
system
.
addParticle
(
1.0
);
CMAPTorsionForce
*
cmap
=
new
CMAPTorsionForce
();
vector
<
double
>
mapEnergy1
(
mapSize
*
mapSize
);
vector
<
double
>
mapEnergy2
(
mapSize
*
mapSize
);
for
(
int
i
=
0
;
i
<
mapSize
;
i
++
)
{
double
angle1
=
i
*
2
*
M_PI
/
mapSize
;
double
energy1
=
cos
(
angle1
);
for
(
int
j
=
0
;
j
<
mapSize
;
j
++
)
{
double
angle2
=
j
*
2
*
M_PI
/
mapSize
;
double
energy2
=
10
*
sin
(
angle2
);
mapEnergy1
[
i
+
j
*
mapSize
]
=
energy1
+
energy2
;
mapEnergy2
[
i
+
j
*
mapSize
]
=
energy1
-
energy2
;
}
}
cmap
->
addMap
(
mapSize
,
mapEnergy1
);
cmap
->
addMap
(
mapSize
,
mapEnergy2
);
cmap
->
addTorsion
(
0
,
0
,
1
,
2
,
3
,
1
,
2
,
3
,
4
);
system
.
addForce
(
cmap
);
// Set particle positions so angle1=0 and angle2=PI/4.
vector
<
Vec3
>
positions
(
5
);
positions
[
0
]
=
Vec3
(
0
,
0
,
1
);
positions
[
1
]
=
Vec3
(
0
,
0
,
0
);
positions
[
2
]
=
Vec3
(
1
,
0
,
0
);
positions
[
3
]
=
Vec3
(
1
,
0
,
1
);
positions
[
4
]
=
Vec3
(
0.5
,
-
0.5
,
1
);
VerletIntegrator
integrator
(
0.01
);
Context
context
(
system
,
integrator
,
platform
);
context
.
setPositions
(
positions
);
// Check that the energy is correct.
double
energy
=
context
.
getState
(
State
::
Energy
).
getPotentialEnergy
();
ASSERT_EQUAL_TOL
(
1
+
10
*
sin
(
M_PI
/
4
),
energy
,
1e-5
);
// Modify the parameters.
cmap
->
setTorsionParameters
(
0
,
1
,
0
,
1
,
2
,
3
,
1
,
2
,
3
,
4
);
for
(
int
i
=
0
;
i
<
mapSize
*
mapSize
;
i
++
)
mapEnergy2
[
i
]
*=
2.0
;
cmap
->
setMapParameters
(
1
,
mapSize
,
mapEnergy2
);
cmap
->
updateParametersInContext
(
context
);
// See if the results are correct.
energy
=
context
.
getState
(
State
::
Energy
).
getPotentialEnergy
();
ASSERT_EQUAL_TOL
(
2
-
20
*
sin
(
M_PI
/
4
),
energy
,
1e-5
);
}
int
main
(
int
argc
,
char
*
argv
[])
{
try
{
if
(
argc
>
1
)
platform
.
setPropertyDefaultValue
(
"CudaPrecision"
,
string
(
argv
[
1
]));
testCMAPTorsions
();
testChangingParameters
();
}
catch
(
const
exception
&
e
)
{
cout
<<
"exception: "
<<
e
.
what
()
<<
endl
;
...
...
platforms/opencl/include/OpenCLKernels.h
View file @
6dc2f9a8
...
...
@@ -510,6 +510,7 @@ private:
bool
hasInitializedKernel
;
OpenCLContext
&
cl
;
const
System
&
system
;
std
::
vector
<
mm_int2
>
mapPositionsVec
;
OpenCLArray
*
coefficients
;
OpenCLArray
*
mapPositions
;
OpenCLArray
*
torsionMaps
;
...
...
platforms/opencl/src/OpenCLKernels.cpp
View file @
6dc2f9a8
...
...
@@ -1125,7 +1125,7 @@ void OpenCLCalcCMAPTorsionForceKernel::initialize(const System& system, const CM
return;
int numMaps = force.getNumMaps();
vector<mm_float4> coeffVec;
vector
<
mm_int2
>
mapPositionsVec
(
numMaps
);
mapPositionsVec
.resize
(numMaps);
vector<double> energy;
vector<vector<double> > c;
int currentPosition = 0;
...
...
@@ -1165,6 +1165,46 @@ double OpenCLCalcCMAPTorsionForceKernel::execute(ContextImpl& context, bool incl
}
void OpenCLCalcCMAPTorsionForceKernel::copyParametersToContext(ContextImpl& context, const CMAPTorsionForce& force) {
int numMaps = force.getNumMaps();
int numContexts = cl.getPlatformData().contexts.size();
int startIndex = cl.getContextIndex()*force.getNumTorsions()/numContexts;
int endIndex = (cl.getContextIndex()+1)*force.getNumTorsions()/numContexts;
numTorsions = endIndex-startIndex;
if (mapPositions->getSize() != numMaps)
throw OpenMMException("updateParametersInContext: The number of maps has changed");
if (torsionMaps->getSize() != numTorsions)
throw OpenMMException("updateParametersInContext: The number of CMAP torsions has changed");
// Update the maps.
vector<mm_float4> coeffVec;
vector<double> energy;
vector<vector<double> > c;
int currentPosition = 0;
for (int i = 0; i < numMaps; i++) {
int size;
force.getMapParameters(i, size, energy);
if (size != mapPositionsVec[i].y)
throw OpenMMException("updateParametersInContext: The size of a map has changed");
CMAPTorsionForceImpl::calcMapDerivatives(size, energy, c);
currentPosition += 4*size*size;
for (int j = 0; j < size*size; j++) {
coeffVec.push_back(mm_float4((float) c[j][0], (float) c[j][1], (float) c[j][2], (float) c[j][3]));
coeffVec.push_back(mm_float4((float) c[j][4], (float) c[j][5], (float) c[j][6], (float) c[j][7]));
coeffVec.push_back(mm_float4((float) c[j][8], (float) c[j][9], (float) c[j][10], (float) c[j][11]));
coeffVec.push_back(mm_float4((float) c[j][12], (float) c[j][13], (float) c[j][14], (float) c[j][15]));
}
}
coefficients->upload(coeffVec);
// Update the indices.
vector<int> torsionMapsVec(numTorsions);
for (int i = 0; i < numTorsions; i++) {
int index[8];
force.getTorsionParameters(i, torsionMapsVec[i], index[0], index[1], index[2], index[3], index[4], index[5], index[6], index[7]);
}
torsionMaps->upload(torsionMapsVec);
}
class OpenCLCustomTorsionForceInfo : public OpenCLForceInfo {
...
...
platforms/opencl/tests/TestOpenCLCMAPTorsionForce.cpp
View file @
6dc2f9a8
...
...
@@ -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) 2010 Stanford University and the Authors.
*
* Portions copyright (c) 2010
-2015
Stanford University and the Authors. *
* Authors: Peter Eastman *
* Contributors: *
* *
...
...
@@ -105,11 +105,68 @@ void testCMAPTorsions() {
}
}
void
testChangingParameters
()
{
// Create a system with two maps and one torsion.
const
int
mapSize
=
8
;
System
system
;
for
(
int
i
=
0
;
i
<
5
;
i
++
)
system
.
addParticle
(
1.0
);
CMAPTorsionForce
*
cmap
=
new
CMAPTorsionForce
();
vector
<
double
>
mapEnergy1
(
mapSize
*
mapSize
);
vector
<
double
>
mapEnergy2
(
mapSize
*
mapSize
);
for
(
int
i
=
0
;
i
<
mapSize
;
i
++
)
{
double
angle1
=
i
*
2
*
M_PI
/
mapSize
;
double
energy1
=
cos
(
angle1
);
for
(
int
j
=
0
;
j
<
mapSize
;
j
++
)
{
double
angle2
=
j
*
2
*
M_PI
/
mapSize
;
double
energy2
=
10
*
sin
(
angle2
);
mapEnergy1
[
i
+
j
*
mapSize
]
=
energy1
+
energy2
;
mapEnergy2
[
i
+
j
*
mapSize
]
=
energy1
-
energy2
;
}
}
cmap
->
addMap
(
mapSize
,
mapEnergy1
);
cmap
->
addMap
(
mapSize
,
mapEnergy2
);
cmap
->
addTorsion
(
0
,
0
,
1
,
2
,
3
,
1
,
2
,
3
,
4
);
system
.
addForce
(
cmap
);
// Set particle positions so angle1=0 and angle2=PI/4.
vector
<
Vec3
>
positions
(
5
);
positions
[
0
]
=
Vec3
(
0
,
0
,
1
);
positions
[
1
]
=
Vec3
(
0
,
0
,
0
);
positions
[
2
]
=
Vec3
(
1
,
0
,
0
);
positions
[
3
]
=
Vec3
(
1
,
0
,
1
);
positions
[
4
]
=
Vec3
(
0.5
,
-
0.5
,
1
);
VerletIntegrator
integrator
(
0.01
);
Context
context
(
system
,
integrator
,
platform
);
context
.
setPositions
(
positions
);
// Check that the energy is correct.
double
energy
=
context
.
getState
(
State
::
Energy
).
getPotentialEnergy
();
ASSERT_EQUAL_TOL
(
1
+
10
*
sin
(
M_PI
/
4
),
energy
,
1e-5
);
// Modify the parameters.
cmap
->
setTorsionParameters
(
0
,
1
,
0
,
1
,
2
,
3
,
1
,
2
,
3
,
4
);
for
(
int
i
=
0
;
i
<
mapSize
*
mapSize
;
i
++
)
mapEnergy2
[
i
]
*=
2.0
;
cmap
->
setMapParameters
(
1
,
mapSize
,
mapEnergy2
);
cmap
->
updateParametersInContext
(
context
);
// See if the results are correct.
energy
=
context
.
getState
(
State
::
Energy
).
getPotentialEnergy
();
ASSERT_EQUAL_TOL
(
2
-
20
*
sin
(
M_PI
/
4
),
energy
,
1e-5
);
}
int
main
(
int
argc
,
char
*
argv
[])
{
try
{
if
(
argc
>
1
)
platform
.
setPropertyDefaultValue
(
"OpenCLPrecision"
,
string
(
argv
[
1
]));
testCMAPTorsions
();
testChangingParameters
();
}
catch
(
const
exception
&
e
)
{
cout
<<
"exception: "
<<
e
.
what
()
<<
endl
;
...
...
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