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
d59b0373
Commit
d59b0373
authored
Feb 08, 2018
by
peastman
Browse files
Convert OpenCLArray to use RAII
parent
44daf269
Changes
8
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
705 additions
and
801 deletions
+705
-801
platforms/opencl/include/OpenCLArray.h
platforms/opencl/include/OpenCLArray.h
+68
-3
platforms/opencl/include/OpenCLCompact.h
platforms/opencl/include/OpenCLCompact.h
+1
-0
platforms/opencl/include/OpenCLContext.h
platforms/opencl/include/OpenCLContext.h
+22
-22
platforms/opencl/include/OpenCLKernels.h
platforms/opencl/include/OpenCLKernels.h
+91
-105
platforms/opencl/include/OpenCLSort.h
platforms/opencl/include/OpenCLSort.h
+1
-0
platforms/opencl/src/OpenCLArray.cpp
platforms/opencl/src/OpenCLArray.cpp
+53
-11
platforms/opencl/src/OpenCLContext.cpp
platforms/opencl/src/OpenCLContext.cpp
+70
-93
platforms/opencl/src/OpenCLKernels.cpp
platforms/opencl/src/OpenCLKernels.cpp
+399
-567
No files found.
platforms/opencl/include/OpenCLArray.h
View file @
d59b0373
...
...
@@ -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
2
Stanford University and the Authors. *
* Portions copyright (c) 2009-201
8
Stanford University and the Authors. *
* Authors: Peter Eastman *
* Contributors: *
* *
...
...
@@ -27,15 +27,19 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
* -------------------------------------------------------------------------- */
#include "OpenCLContext.h"
#define __CL_ENABLE_EXCEPTIONS
#define CL_USE_DEPRECATED_OPENCL_1_1_APIS
#include "openmm/OpenMMException.h"
#include "windowsExportOpenCL.h"
#include <cl.hpp>
#include <iostream>
#include <sstream>
#include <vector>
namespace
OpenMM
{
class
OpenCLContext
;
/**
* This class encapsulates an OpenCL Buffer. It provides a simplified API for working with it,
* and for copying data to and from the OpenCL Buffer.
...
...
@@ -69,6 +73,11 @@ public:
static
OpenCLArray
*
create
(
OpenCLContext
&
context
,
cl
::
Buffer
*
buffer
,
int
size
,
const
std
::
string
&
name
)
{
return
new
OpenCLArray
(
context
,
buffer
,
size
,
sizeof
(
T
),
name
);
}
/**
* Create an uninitialized OpenCLArray object. It does not point to any OpenCL Buffer,
* and cannot be used until initialize() is called on it.
*/
OpenCLArray
();
/**
* Create an OpenCLArray object.
*
...
...
@@ -90,6 +99,61 @@ public:
*/
OpenCLArray
(
OpenCLContext
&
context
,
cl
::
Buffer
*
buffer
,
int
size
,
int
elementSize
,
const
std
::
string
&
name
);
~
OpenCLArray
();
/**
* Initialize this object.
*
* @param context the context for which to create the array
* @param size the number of elements in the array
* @param elementSize the size of each element in bytes
* @param name the name of the array
* @param flags the set of flags to specify when creating the OpenCL Buffer
*/
void
initialize
(
OpenCLContext
&
context
,
int
size
,
int
elementSize
,
const
std
::
string
&
name
,
cl_int
flags
=
CL_MEM_READ_WRITE
);
/**
* Initialize this object to use a preexisting Buffer.
*
* @param context the context for which to create the array
* @param buffer the OpenCL Buffer this object encapsulates
* @param size the number of elements in the array
* @param elementSize the size of each element in bytes
* @param name the name of the array
*/
void
initialize
(
OpenCLContext
&
context
,
cl
::
Buffer
*
buffer
,
int
size
,
int
elementSize
,
const
std
::
string
&
name
);
/**
* Initialize this object. The template argument is the data type of each array element.
*
* @param context the context for which to create the array
* @param size the number of elements in the array
* @param name the name of the array
* @param flags the set of flags to specify when creating the OpenCL Buffer
*/
template
<
class
T
>
void
initialize
(
OpenCLContext
&
context
,
int
size
,
const
std
::
string
&
name
,
cl_int
flags
=
CL_MEM_READ_WRITE
)
{
initialize
(
context
,
size
,
sizeof
(
T
),
name
,
flags
);
}
/**
* Initialize this object to use a preexisting Buffer. The template argument
* is the data type of each array element.
*
* @param context the context for which to create the array
* @param buffer the OpenCL Buffer this object encapsulates
* @param size the number of elements in the array
* @param name the name of the array
*/
template
<
class
T
>
void
initialize
(
OpenCLContext
&
context
,
cl
::
Buffer
*
buffer
,
int
size
,
const
std
::
string
&
name
)
{
initialize
(
context
,
buffer
,
size
,
sizeof
(
T
),
name
);
}
/**
* Recreate the internal storage to have a different size.
*/
void
resize
(
int
size
);
/**
* Get whether this array has been initialized.
*/
bool
isInitialized
()
const
{
return
(
buffer
!=
NULL
);
}
/**
* Get the size of the array.
*/
...
...
@@ -155,9 +219,10 @@ public:
*/
void
copyTo
(
OpenCLArray
&
dest
)
const
;
private:
OpenCLContext
&
context
;
OpenCLContext
*
context
;
cl
::
Buffer
*
buffer
;
int
size
,
elementSize
;
cl_int
flags
;
bool
ownsBuffer
;
std
::
string
name
;
};
...
...
platforms/opencl/include/OpenCLCompact.h
View file @
d59b0373
...
...
@@ -26,6 +26,7 @@
*/
#include "OpenCLArray.h"
#include "OpenCLContext.h"
namespace
OpenMM
{
...
...
platforms/opencl/include/OpenCLContext.h
View file @
d59b0373
...
...
@@ -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
8
Stanford University and the Authors. *
* Authors: Peter Eastman *
* Contributors: *
* *
...
...
@@ -39,11 +39,11 @@
#include <pthread.h>
#include <cl.hpp>
#include "windowsExportOpenCL.h"
#include "OpenCLArray.h"
#include "OpenCLPlatform.h"
namespace
OpenMM
{
class
OpenCLArray
;
class
OpenCLForceInfo
;
class
OpenCLIntegrationUtilities
;
class
OpenCLExpressionUtilities
;
...
...
@@ -227,49 +227,49 @@ public:
* Get the array which contains the position (the xyz components) and charge (the w component) of each atom.
*/
OpenCLArray
&
getPosq
()
{
return
*
posq
;
return
posq
;
}
/**
* Get the array which contains a correction to the position of each atom. This only exists if getUseMixedPrecision() returns true.
*/
OpenCLArray
&
getPosqCorrection
()
{
return
*
posqCorrection
;
return
posqCorrection
;
}
/**
* Get the array which contains the velocity (the xyz components) and inverse mass (the w component) of each atom.
*/
OpenCLArray
&
getVelm
()
{
return
*
velm
;
return
velm
;
}
/**
* Get the array which contains the force on each atom.
*/
OpenCLArray
&
getForce
()
{
return
*
force
;
return
force
;
}
/**
* Get the array which contains the buffers in which forces are computed.
*/
OpenCLArray
&
getForceBuffers
()
{
return
*
forceBuffers
;
return
forceBuffers
;
}
/**
* Get the array which contains a contribution to each force represented as 64 bit fixed point.
*/
OpenCLArray
&
getLongForceBuffer
()
{
return
*
longForceBuffer
;
return
longForceBuffer
;
}
/**
* Get the array which contains the buffer in which energy is computed.
*/
OpenCLArray
&
getEnergyBuffer
()
{
return
*
energyBuffer
;
return
energyBuffer
;
}
/**
* Get the array which contains the buffer in which derivatives of the energy with respect to parameters are computed.
*/
OpenCLArray
&
getEnergyParamDerivBuffer
()
{
return
*
energyParamDerivBuffer
;
return
energyParamDerivBuffer
;
}
/**
* Get a pointer to a block of pinned memory that can be used for efficient transfers between host and device.
...
...
@@ -288,7 +288,7 @@ public:
* Get the array which contains the index of each atom.
*/
OpenCLArray
&
getAtomIndexArray
()
{
return
*
atomIndexDevice
;
return
atomIndexDevice
;
}
/**
* Get the number of cells by which the positions are offset.
...
...
@@ -762,17 +762,17 @@ private:
std
::
vector
<
mm_int4
>
posCellOffsets
;
cl
::
Buffer
*
pinnedBuffer
;
void
*
pinnedMemory
;
OpenCLArray
*
posq
;
OpenCLArray
*
posqCorrection
;
OpenCLArray
*
velm
;
OpenCLArray
*
force
;
OpenCLArray
*
forceBuffers
;
OpenCLArray
*
longForceBuffer
;
OpenCLArray
*
energyBuffer
;
OpenCLArray
*
energySum
;
OpenCLArray
*
energyParamDerivBuffer
;
OpenCLArray
*
atomIndexDevice
;
OpenCLArray
*
chargeBuffer
;
OpenCLArray
posq
;
OpenCLArray
posqCorrection
;
OpenCLArray
velm
;
OpenCLArray
force
;
OpenCLArray
forceBuffers
;
OpenCLArray
longForceBuffer
;
OpenCLArray
energyBuffer
;
OpenCLArray
energySum
;
OpenCLArray
energyParamDerivBuffer
;
OpenCLArray
atomIndexDevice
;
OpenCLArray
chargeBuffer
;
std
::
vector
<
std
::
string
>
energyParamDerivNames
;
std
::
map
<
std
::
string
,
double
>
energyParamDerivWorkspace
;
std
::
vector
<
int
>
atomIndex
;
...
...
platforms/opencl/include/OpenCLKernels.h
View file @
d59b0373
...
...
@@ -243,9 +243,8 @@ private:
class
OpenCLCalcHarmonicBondForceKernel
:
public
CalcHarmonicBondForceKernel
{
public:
OpenCLCalcHarmonicBondForceKernel
(
std
::
string
name
,
const
Platform
&
platform
,
OpenCLContext
&
cl
,
const
System
&
system
)
:
CalcHarmonicBondForceKernel
(
name
,
platform
),
hasInitializedKernel
(
false
),
cl
(
cl
),
system
(
system
)
,
params
(
NULL
)
{
hasInitializedKernel
(
false
),
cl
(
cl
),
system
(
system
)
{
}
~
OpenCLCalcHarmonicBondForceKernel
();
/**
* Initialize the kernel.
*
...
...
@@ -276,7 +275,7 @@ private:
OpenCLContext
&
cl
;
ForceInfo
*
info
;
const
System
&
system
;
OpenCLArray
*
params
;
OpenCLArray
params
;
};
/**
...
...
@@ -285,7 +284,7 @@ private:
class
OpenCLCalcCustomBondForceKernel
:
public
CalcCustomBondForceKernel
{
public:
OpenCLCalcCustomBondForceKernel
(
std
::
string
name
,
const
Platform
&
platform
,
OpenCLContext
&
cl
,
const
System
&
system
)
:
CalcCustomBondForceKernel
(
name
,
platform
),
hasInitializedKernel
(
false
),
cl
(
cl
),
system
(
system
),
params
(
NULL
)
,
globals
(
NULL
)
{
hasInitializedKernel
(
false
),
cl
(
cl
),
system
(
system
),
params
(
NULL
)
{
}
~
OpenCLCalcCustomBondForceKernel
();
/**
...
...
@@ -319,7 +318,7 @@ private:
ForceInfo
*
info
;
const
System
&
system
;
OpenCLParameterSet
*
params
;
OpenCLArray
*
globals
;
OpenCLArray
globals
;
std
::
vector
<
std
::
string
>
globalParamNames
;
std
::
vector
<
cl_float
>
globalParamValues
;
};
...
...
@@ -330,9 +329,8 @@ private:
class
OpenCLCalcHarmonicAngleForceKernel
:
public
CalcHarmonicAngleForceKernel
{
public:
OpenCLCalcHarmonicAngleForceKernel
(
std
::
string
name
,
const
Platform
&
platform
,
OpenCLContext
&
cl
,
const
System
&
system
)
:
CalcHarmonicAngleForceKernel
(
name
,
platform
),
hasInitializedKernel
(
false
),
cl
(
cl
),
system
(
system
)
,
params
(
NULL
)
{
hasInitializedKernel
(
false
),
cl
(
cl
),
system
(
system
)
{
}
~
OpenCLCalcHarmonicAngleForceKernel
();
/**
* Initialize the kernel.
*
...
...
@@ -363,7 +361,7 @@ private:
OpenCLContext
&
cl
;
ForceInfo
*
info
;
const
System
&
system
;
OpenCLArray
*
params
;
OpenCLArray
params
;
};
/**
...
...
@@ -372,7 +370,7 @@ private:
class
OpenCLCalcCustomAngleForceKernel
:
public
CalcCustomAngleForceKernel
{
public:
OpenCLCalcCustomAngleForceKernel
(
std
::
string
name
,
const
Platform
&
platform
,
OpenCLContext
&
cl
,
const
System
&
system
)
:
CalcCustomAngleForceKernel
(
name
,
platform
),
hasInitializedKernel
(
false
),
cl
(
cl
),
system
(
system
),
params
(
NULL
)
,
globals
(
NULL
)
{
hasInitializedKernel
(
false
),
cl
(
cl
),
system
(
system
),
params
(
NULL
)
{
}
~
OpenCLCalcCustomAngleForceKernel
();
/**
...
...
@@ -406,7 +404,7 @@ private:
ForceInfo
*
info
;
const
System
&
system
;
OpenCLParameterSet
*
params
;
OpenCLArray
*
globals
;
OpenCLArray
globals
;
std
::
vector
<
std
::
string
>
globalParamNames
;
std
::
vector
<
cl_float
>
globalParamValues
;
};
...
...
@@ -417,9 +415,8 @@ private:
class
OpenCLCalcPeriodicTorsionForceKernel
:
public
CalcPeriodicTorsionForceKernel
{
public:
OpenCLCalcPeriodicTorsionForceKernel
(
std
::
string
name
,
const
Platform
&
platform
,
OpenCLContext
&
cl
,
const
System
&
system
)
:
CalcPeriodicTorsionForceKernel
(
name
,
platform
),
hasInitializedKernel
(
false
),
cl
(
cl
),
system
(
system
)
,
params
(
NULL
)
{
hasInitializedKernel
(
false
),
cl
(
cl
),
system
(
system
)
{
}
~
OpenCLCalcPeriodicTorsionForceKernel
();
/**
* Initialize the kernel.
*
...
...
@@ -450,7 +447,7 @@ private:
OpenCLContext
&
cl
;
ForceInfo
*
info
;
const
System
&
system
;
OpenCLArray
*
params
;
OpenCLArray
params
;
};
/**
...
...
@@ -459,9 +456,8 @@ private:
class
OpenCLCalcRBTorsionForceKernel
:
public
CalcRBTorsionForceKernel
{
public:
OpenCLCalcRBTorsionForceKernel
(
std
::
string
name
,
const
Platform
&
platform
,
OpenCLContext
&
cl
,
const
System
&
system
)
:
CalcRBTorsionForceKernel
(
name
,
platform
),
hasInitializedKernel
(
false
),
cl
(
cl
),
system
(
system
)
,
params
(
NULL
)
{
hasInitializedKernel
(
false
),
cl
(
cl
),
system
(
system
)
{
}
~
OpenCLCalcRBTorsionForceKernel
();
/**
* Initialize the kernel.
*
...
...
@@ -492,7 +488,7 @@ private:
OpenCLContext
&
cl
;
ForceInfo
*
info
;
const
System
&
system
;
OpenCLArray
*
params
;
OpenCLArray
params
;
};
/**
...
...
@@ -501,9 +497,8 @@ private:
class
OpenCLCalcCMAPTorsionForceKernel
:
public
CalcCMAPTorsionForceKernel
{
public:
OpenCLCalcCMAPTorsionForceKernel
(
std
::
string
name
,
const
Platform
&
platform
,
OpenCLContext
&
cl
,
const
System
&
system
)
:
CalcCMAPTorsionForceKernel
(
name
,
platform
),
hasInitializedKernel
(
false
),
cl
(
cl
),
system
(
system
)
,
coefficients
(
NULL
),
mapPositions
(
NULL
),
torsionMaps
(
NULL
)
{
hasInitializedKernel
(
false
),
cl
(
cl
),
system
(
system
)
{
}
~
OpenCLCalcCMAPTorsionForceKernel
();
/**
* Initialize the kernel.
*
...
...
@@ -535,9 +530,9 @@ private:
ForceInfo
*
info
;
const
System
&
system
;
std
::
vector
<
mm_int2
>
mapPositionsVec
;
OpenCLArray
*
coefficients
;
OpenCLArray
*
mapPositions
;
OpenCLArray
*
torsionMaps
;
OpenCLArray
coefficients
;
OpenCLArray
mapPositions
;
OpenCLArray
torsionMaps
;
};
/**
...
...
@@ -546,7 +541,7 @@ private:
class
OpenCLCalcCustomTorsionForceKernel
:
public
CalcCustomTorsionForceKernel
{
public:
OpenCLCalcCustomTorsionForceKernel
(
std
::
string
name
,
const
Platform
&
platform
,
OpenCLContext
&
cl
,
const
System
&
system
)
:
CalcCustomTorsionForceKernel
(
name
,
platform
),
hasInitializedKernel
(
false
),
cl
(
cl
),
system
(
system
),
params
(
NULL
)
,
globals
(
NULL
)
{
hasInitializedKernel
(
false
),
cl
(
cl
),
system
(
system
),
params
(
NULL
)
{
}
~
OpenCLCalcCustomTorsionForceKernel
();
/**
...
...
@@ -580,7 +575,7 @@ private:
ForceInfo
*
info
;
const
System
&
system
;
OpenCLParameterSet
*
params
;
OpenCLArray
*
globals
;
OpenCLArray
globals
;
std
::
vector
<
std
::
string
>
globalParamNames
;
std
::
vector
<
cl_float
>
globalParamValues
;
};
...
...
@@ -591,10 +586,7 @@ private:
class
OpenCLCalcNonbondedForceKernel
:
public
CalcNonbondedForceKernel
{
public:
OpenCLCalcNonbondedForceKernel
(
std
::
string
name
,
const
Platform
&
platform
,
OpenCLContext
&
cl
,
const
System
&
system
)
:
CalcNonbondedForceKernel
(
name
,
platform
),
hasInitializedKernel
(
false
),
cl
(
cl
),
sigmaEpsilon
(
NULL
),
exceptionParams
(
NULL
),
cosSinSums
(
NULL
),
pmeGrid
(
NULL
),
pmeGrid2
(
NULL
),
pmeBsplineModuliX
(
NULL
),
pmeBsplineModuliY
(
NULL
),
pmeBsplineModuliZ
(
NULL
),
pmeDispersionBsplineModuliX
(
NULL
),
pmeDispersionBsplineModuliY
(
NULL
),
pmeDispersionBsplineModuliZ
(
NULL
),
pmeBsplineTheta
(
NULL
),
pmeAtomRange
(
NULL
),
pmeAtomGridIndex
(
NULL
),
pmeEnergyBuffer
(
NULL
),
sort
(
NULL
),
fft
(
NULL
),
dispersionFft
(
NULL
),
pmeio
(
NULL
)
{
hasInitializedKernel
(
false
),
cl
(
cl
),
sort
(
NULL
),
fft
(
NULL
),
dispersionFft
(
NULL
),
pmeio
(
NULL
)
{
}
~
OpenCLCalcNonbondedForceKernel
();
/**
...
...
@@ -660,21 +652,21 @@ private:
OpenCLContext
&
cl
;
ForceInfo
*
info
;
bool
hasInitializedKernel
;
OpenCLArray
*
sigmaEpsilon
;
OpenCLArray
*
exceptionParams
;
OpenCLArray
*
cosSinSums
;
OpenCLArray
*
pmeGrid
;
OpenCLArray
*
pmeGrid2
;
OpenCLArray
*
pmeBsplineModuliX
;
OpenCLArray
*
pmeBsplineModuliY
;
OpenCLArray
*
pmeBsplineModuliZ
;
OpenCLArray
*
pmeDispersionBsplineModuliX
;
OpenCLArray
*
pmeDispersionBsplineModuliY
;
OpenCLArray
*
pmeDispersionBsplineModuliZ
;
OpenCLArray
*
pmeBsplineTheta
;
OpenCLArray
*
pmeAtomRange
;
OpenCLArray
*
pmeAtomGridIndex
;
OpenCLArray
*
pmeEnergyBuffer
;
OpenCLArray
sigmaEpsilon
;
OpenCLArray
exceptionParams
;
OpenCLArray
cosSinSums
;
OpenCLArray
pmeGrid
;
OpenCLArray
pmeGrid2
;
OpenCLArray
pmeBsplineModuliX
;
OpenCLArray
pmeBsplineModuliY
;
OpenCLArray
pmeBsplineModuliZ
;
OpenCLArray
pmeDispersionBsplineModuliX
;
OpenCLArray
pmeDispersionBsplineModuliY
;
OpenCLArray
pmeDispersionBsplineModuliZ
;
OpenCLArray
pmeBsplineTheta
;
OpenCLArray
pmeAtomRange
;
OpenCLArray
pmeAtomGridIndex
;
OpenCLArray
pmeEnergyBuffer
;
OpenCLSort
*
sort
;
cl
::
CommandQueue
pmeQueue
;
cl
::
Event
pmeSyncEvent
;
...
...
@@ -717,7 +709,7 @@ private:
class
OpenCLCalcCustomNonbondedForceKernel
:
public
CalcCustomNonbondedForceKernel
{
public:
OpenCLCalcCustomNonbondedForceKernel
(
std
::
string
name
,
const
Platform
&
platform
,
OpenCLContext
&
cl
,
const
System
&
system
)
:
CalcCustomNonbondedForceKernel
(
name
,
platform
),
cl
(
cl
),
params
(
NULL
),
globals
(
NULL
),
interactionGroupData
(
NULL
),
forceCopy
(
NULL
),
system
(
system
),
hasInitializedKernel
(
false
)
{
cl
(
cl
),
params
(
NULL
),
forceCopy
(
NULL
),
system
(
system
),
hasInitializedKernel
(
false
)
{
}
~
OpenCLCalcCustomNonbondedForceKernel
();
/**
...
...
@@ -749,13 +741,13 @@ private:
OpenCLContext
&
cl
;
ForceInfo
*
info
;
OpenCLParameterSet
*
params
;
OpenCLArray
*
globals
;
OpenCLArray
*
interactionGroupData
;
OpenCLArray
globals
;
OpenCLArray
interactionGroupData
;
cl
::
Kernel
interactionGroupKernel
;
std
::
vector
<
void
*>
interactionGroupArgs
;
std
::
vector
<
std
::
string
>
globalParamNames
;
std
::
vector
<
cl_float
>
globalParamValues
;
std
::
vector
<
OpenCLArray
*
>
tabulatedFunctions
;
std
::
vector
<
OpenCLArray
>
tabulatedFunctions
;
double
longRangeCoefficient
;
std
::
vector
<
double
>
longRangeCoefficientDerivs
;
bool
hasInitializedLongRangeCorrection
,
hasInitializedKernel
,
hasParamDerivs
;
...
...
@@ -770,10 +762,8 @@ private:
class
OpenCLCalcGBSAOBCForceKernel
:
public
CalcGBSAOBCForceKernel
{
public:
OpenCLCalcGBSAOBCForceKernel
(
std
::
string
name
,
const
Platform
&
platform
,
OpenCLContext
&
cl
)
:
CalcGBSAOBCForceKernel
(
name
,
platform
),
cl
(
cl
),
hasCreatedKernels
(
false
),
params
(
NULL
),
bornSum
(
NULL
),
longBornSum
(
NULL
),
bornRadii
(
NULL
),
bornForce
(
NULL
),
longBornForce
(
NULL
),
obcChain
(
NULL
)
{
hasCreatedKernels
(
false
)
{
}
~
OpenCLCalcGBSAOBCForceKernel
();
/**
* Initialize the kernel.
*
...
...
@@ -804,13 +794,13 @@ private:
int
maxTiles
;
OpenCLContext
&
cl
;
ForceInfo
*
info
;
OpenCLArray
*
params
;
OpenCLArray
*
bornSum
;
OpenCLArray
*
longBornSum
;
OpenCLArray
*
bornRadii
;
OpenCLArray
*
bornForce
;
OpenCLArray
*
longBornForce
;
OpenCLArray
*
obcChain
;
OpenCLArray
params
;
OpenCLArray
bornSum
;
OpenCLArray
longBornSum
;
OpenCLArray
bornRadii
;
OpenCLArray
bornForce
;
OpenCLArray
longBornForce
;
OpenCLArray
obcChain
;
cl
::
Kernel
computeBornSumKernel
;
cl
::
Kernel
reduceBornSumKernel
;
cl
::
Kernel
force1Kernel
;
...
...
@@ -823,8 +813,7 @@ private:
class
OpenCLCalcCustomGBForceKernel
:
public
CalcCustomGBForceKernel
{
public:
OpenCLCalcCustomGBForceKernel
(
std
::
string
name
,
const
Platform
&
platform
,
OpenCLContext
&
cl
,
const
System
&
system
)
:
CalcCustomGBForceKernel
(
name
,
platform
),
hasInitializedKernels
(
false
),
cl
(
cl
),
params
(
NULL
),
computedValues
(
NULL
),
energyDerivs
(
NULL
),
energyDerivChain
(
NULL
),
longEnergyDerivs
(
NULL
),
globals
(
NULL
),
valueBuffers
(
NULL
),
longValueBuffers
(
NULL
),
system
(
system
)
{
hasInitializedKernels
(
false
),
cl
(
cl
),
params
(
NULL
),
computedValues
(
NULL
),
energyDerivs
(
NULL
),
energyDerivChain
(
NULL
),
system
(
system
)
{
}
~
OpenCLCalcCustomGBForceKernel
();
/**
...
...
@@ -862,14 +851,14 @@ private:
OpenCLParameterSet
*
energyDerivs
;
OpenCLParameterSet
*
energyDerivChain
;
std
::
vector
<
OpenCLParameterSet
*>
dValuedParam
;
std
::
vector
<
OpenCLArray
*
>
dValue0dParam
;
OpenCLArray
*
longEnergyDerivs
;
OpenCLArray
*
globals
;
OpenCLArray
*
valueBuffers
;
OpenCLArray
*
longValueBuffers
;
std
::
vector
<
OpenCLArray
>
dValue0dParam
;
OpenCLArray
longEnergyDerivs
;
OpenCLArray
globals
;
OpenCLArray
valueBuffers
;
OpenCLArray
longValueBuffers
;
std
::
vector
<
std
::
string
>
globalParamNames
;
std
::
vector
<
cl_float
>
globalParamValues
;
std
::
vector
<
OpenCLArray
*
>
tabulatedFunctions
;
std
::
vector
<
OpenCLArray
>
tabulatedFunctions
;
std
::
vector
<
bool
>
pairValueUsesParam
,
pairEnergyUsesParam
,
pairEnergyUsesValue
;
const
System
&
system
;
cl
::
Kernel
pairValueKernel
,
perParticleValueKernel
,
pairEnergyKernel
,
perParticleEnergyKernel
,
gradientChainRuleKernel
;
...
...
@@ -883,7 +872,7 @@ private:
class
OpenCLCalcCustomExternalForceKernel
:
public
CalcCustomExternalForceKernel
{
public:
OpenCLCalcCustomExternalForceKernel
(
std
::
string
name
,
const
Platform
&
platform
,
OpenCLContext
&
cl
,
const
System
&
system
)
:
CalcCustomExternalForceKernel
(
name
,
platform
),
hasInitializedKernel
(
false
),
cl
(
cl
),
system
(
system
),
params
(
NULL
)
,
globals
(
NULL
)
{
hasInitializedKernel
(
false
),
cl
(
cl
),
system
(
system
),
params
(
NULL
)
{
}
~
OpenCLCalcCustomExternalForceKernel
();
/**
...
...
@@ -917,7 +906,7 @@ private:
ForceInfo
*
info
;
const
System
&
system
;
OpenCLParameterSet
*
params
;
OpenCLArray
*
globals
;
OpenCLArray
globals
;
std
::
vector
<
std
::
string
>
globalParamNames
;
std
::
vector
<
cl_float
>
globalParamValues
;
};
...
...
@@ -928,8 +917,7 @@ private:
class
OpenCLCalcCustomHbondForceKernel
:
public
CalcCustomHbondForceKernel
{
public:
OpenCLCalcCustomHbondForceKernel
(
std
::
string
name
,
const
Platform
&
platform
,
OpenCLContext
&
cl
,
const
System
&
system
)
:
CalcCustomHbondForceKernel
(
name
,
platform
),
hasInitializedKernel
(
false
),
cl
(
cl
),
donorParams
(
NULL
),
acceptorParams
(
NULL
),
donors
(
NULL
),
acceptors
(
NULL
),
donorBufferIndices
(
NULL
),
acceptorBufferIndices
(
NULL
),
globals
(
NULL
),
donorExclusions
(
NULL
),
acceptorExclusions
(
NULL
),
system
(
system
)
{
hasInitializedKernel
(
false
),
cl
(
cl
),
donorParams
(
NULL
),
acceptorParams
(
NULL
),
system
(
system
)
{
}
~
OpenCLCalcCustomHbondForceKernel
();
/**
...
...
@@ -963,16 +951,16 @@ private:
ForceInfo
*
info
;
OpenCLParameterSet
*
donorParams
;
OpenCLParameterSet
*
acceptorParams
;
OpenCLArray
*
globals
;
OpenCLArray
*
donors
;
OpenCLArray
*
acceptors
;
OpenCLArray
*
donorBufferIndices
;
OpenCLArray
*
acceptorBufferIndices
;
OpenCLArray
*
donorExclusions
;
OpenCLArray
*
acceptorExclusions
;
OpenCLArray
globals
;
OpenCLArray
donors
;
OpenCLArray
acceptors
;
OpenCLArray
donorBufferIndices
;
OpenCLArray
acceptorBufferIndices
;
OpenCLArray
donorExclusions
;
OpenCLArray
acceptorExclusions
;
std
::
vector
<
std
::
string
>
globalParamNames
;
std
::
vector
<
cl_float
>
globalParamValues
;
std
::
vector
<
OpenCLArray
*
>
tabulatedFunctions
;
std
::
vector
<
OpenCLArray
>
tabulatedFunctions
;
const
System
&
system
;
cl
::
Kernel
donorKernel
,
acceptorKernel
;
};
...
...
@@ -983,7 +971,7 @@ private:
class
OpenCLCalcCustomCentroidBondForceKernel
:
public
CalcCustomCentroidBondForceKernel
{
public:
OpenCLCalcCustomCentroidBondForceKernel
(
std
::
string
name
,
const
Platform
&
platform
,
OpenCLContext
&
cl
,
const
System
&
system
)
:
CalcCustomCentroidBondForceKernel
(
name
,
platform
),
cl
(
cl
),
params
(
NULL
),
globals
(
NULL
),
groupParticles
(
NULL
),
groupWeights
(
NULL
),
groupOffsets
(
NULL
),
groupForces
(
NULL
),
bondGroups
(
NULL
),
centerPositions
(
NULL
),
system
(
system
)
{
cl
(
cl
),
params
(
NULL
),
system
(
system
)
{
}
~
OpenCLCalcCustomCentroidBondForceKernel
();
/**
...
...
@@ -1017,16 +1005,16 @@ private:
OpenCLContext
&
cl
;
ForceInfo
*
info
;
OpenCLParameterSet
*
params
;
OpenCLArray
*
globals
;
OpenCLArray
*
groupParticles
;
OpenCLArray
*
groupWeights
;
OpenCLArray
*
groupOffsets
;
OpenCLArray
*
groupForces
;
OpenCLArray
*
bondGroups
;
OpenCLArray
*
centerPositions
;
OpenCLArray
globals
;
OpenCLArray
groupParticles
;
OpenCLArray
groupWeights
;
OpenCLArray
groupOffsets
;
OpenCLArray
groupForces
;
OpenCLArray
bondGroups
;
OpenCLArray
centerPositions
;
std
::
vector
<
std
::
string
>
globalParamNames
;
std
::
vector
<
cl_float
>
globalParamValues
;
std
::
vector
<
OpenCLArray
*
>
tabulatedFunctions
;
std
::
vector
<
OpenCLArray
>
tabulatedFunctions
;
cl
::
Kernel
computeCentersKernel
,
groupForcesKernel
,
applyForcesKernel
;
const
System
&
system
;
};
...
...
@@ -1037,7 +1025,7 @@ private:
class
OpenCLCalcCustomCompoundBondForceKernel
:
public
CalcCustomCompoundBondForceKernel
{
public:
OpenCLCalcCustomCompoundBondForceKernel
(
std
::
string
name
,
const
Platform
&
platform
,
OpenCLContext
&
cl
,
const
System
&
system
)
:
CalcCustomCompoundBondForceKernel
(
name
,
platform
),
cl
(
cl
),
params
(
NULL
),
globals
(
NULL
),
system
(
system
)
{
cl
(
cl
),
params
(
NULL
),
system
(
system
)
{
}
~
OpenCLCalcCustomCompoundBondForceKernel
();
/**
...
...
@@ -1070,10 +1058,10 @@ private:
OpenCLContext
&
cl
;
ForceInfo
*
info
;
OpenCLParameterSet
*
params
;
OpenCLArray
*
globals
;
OpenCLArray
globals
;
std
::
vector
<
std
::
string
>
globalParamNames
;
std
::
vector
<
cl_float
>
globalParamValues
;
std
::
vector
<
OpenCLArray
*
>
tabulatedFunctions
;
std
::
vector
<
OpenCLArray
>
tabulatedFunctions
;
const
System
&
system
;
};
...
...
@@ -1083,9 +1071,7 @@ private:
class
OpenCLCalcCustomManyParticleForceKernel
:
public
CalcCustomManyParticleForceKernel
{
public:
OpenCLCalcCustomManyParticleForceKernel
(
std
::
string
name
,
const
Platform
&
platform
,
OpenCLContext
&
cl
,
const
System
&
system
)
:
CalcCustomManyParticleForceKernel
(
name
,
platform
),
hasInitializedKernel
(
false
),
cl
(
cl
),
params
(
NULL
),
globals
(
NULL
),
particleTypes
(
NULL
),
orderIndex
(
NULL
),
particleOrder
(
NULL
),
exclusions
(
NULL
),
exclusionStartIndex
(
NULL
),
blockCenter
(
NULL
),
blockBoundingBox
(
NULL
),
neighborPairs
(
NULL
),
numNeighborPairs
(
NULL
),
neighborStartIndex
(
NULL
),
numNeighborsForAtom
(
NULL
),
neighbors
(
NULL
),
system
(
system
)
{
hasInitializedKernel
(
false
),
cl
(
cl
),
params
(
NULL
),
system
(
system
)
{
}
~
OpenCLCalcCustomManyParticleForceKernel
();
/**
...
...
@@ -1120,22 +1106,22 @@ private:
NonbondedMethod
nonbondedMethod
;
int
maxNeighborPairs
,
forceWorkgroupSize
,
findNeighborsWorkgroupSize
;
OpenCLParameterSet
*
params
;
OpenCLArray
*
globals
;
OpenCLArray
*
particleTypes
;
OpenCLArray
*
orderIndex
;
OpenCLArray
*
particleOrder
;
OpenCLArray
*
exclusions
;
OpenCLArray
*
exclusionStartIndex
;
OpenCLArray
*
blockCenter
;
OpenCLArray
*
blockBoundingBox
;
OpenCLArray
*
neighborPairs
;
OpenCLArray
*
numNeighborPairs
;
OpenCLArray
*
neighborStartIndex
;
OpenCLArray
*
numNeighborsForAtom
;
OpenCLArray
*
neighbors
;
OpenCLArray
globals
;
OpenCLArray
particleTypes
;
OpenCLArray
orderIndex
;
OpenCLArray
particleOrder
;
OpenCLArray
exclusions
;
OpenCLArray
exclusionStartIndex
;
OpenCLArray
blockCenter
;
OpenCLArray
blockBoundingBox
;
OpenCLArray
neighborPairs
;
OpenCLArray
numNeighborPairs
;
OpenCLArray
neighborStartIndex
;
OpenCLArray
numNeighborsForAtom
;
OpenCLArray
neighbors
;
std
::
vector
<
std
::
string
>
globalParamNames
;
std
::
vector
<
float
>
globalParamValues
;
std
::
vector
<
OpenCLArray
*
>
tabulatedFunctions
;
std
::
vector
<
OpenCLArray
>
tabulatedFunctions
;
const
System
&
system
;
cl
::
Kernel
forceKernel
,
blockBoundsKernel
,
neighborsKernel
,
startIndicesKernel
,
copyPairsKernel
;
};
...
...
platforms/opencl/include/OpenCLSort.h
View file @
d59b0373
...
...
@@ -28,6 +28,7 @@
* -------------------------------------------------------------------------- */
#include "OpenCLArray.h"
#include "OpenCLContext.h"
#include "windowsExportOpenCL.h"
namespace
OpenMM
{
...
...
platforms/opencl/src/OpenCLArray.cpp
View file @
d59b0373
...
...
@@ -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) 2012 Stanford University and the Authors.
*
* Portions copyright (c) 2012
-2018
Stanford University and the Authors. *
* Authors: Peter Eastman *
* Contributors: *
* *
...
...
@@ -25,14 +25,38 @@
* -------------------------------------------------------------------------- */
#include "OpenCLArray.h"
#include "OpenCLContext.h"
#include <iostream>
#include <sstream>
#include <vector>
using
namespace
OpenMM
;
OpenCLArray
::
OpenCLArray
(
OpenCLContext
&
context
,
int
size
,
int
elementSize
,
const
std
::
string
&
name
,
cl_int
flags
)
:
context
(
context
),
size
(
size
),
elementSize
(
elementSize
),
name
(
name
),
ownsBuffer
(
true
)
{
OpenCLArray
::
OpenCLArray
()
:
buffer
(
NULL
),
ownsBuffer
(
false
)
{
}
OpenCLArray
::
OpenCLArray
(
OpenCLContext
&
context
,
int
size
,
int
elementSize
,
const
std
::
string
&
name
,
cl_int
flags
)
:
buffer
(
NULL
)
{
initialize
(
context
,
size
,
elementSize
,
name
,
flags
);
}
OpenCLArray
::
OpenCLArray
(
OpenCLContext
&
context
,
cl
::
Buffer
*
buffer
,
int
size
,
int
elementSize
,
const
std
::
string
&
name
)
:
buffer
(
NULL
)
{
initialize
(
context
,
buffer
,
size
,
elementSize
,
name
);
}
OpenCLArray
::~
OpenCLArray
()
{
if
(
buffer
!=
NULL
&&
ownsBuffer
)
delete
buffer
;
}
void
OpenCLArray
::
initialize
(
OpenCLContext
&
context
,
int
size
,
int
elementSize
,
const
std
::
string
&
name
,
cl_int
flags
)
{
if
(
buffer
!=
NULL
)
throw
OpenMMException
(
"OpenCLArray has already been initialized"
);
this
->
context
=
&
context
;
this
->
size
=
size
;
this
->
elementSize
=
elementSize
;
this
->
name
=
name
;
this
->
flags
=
flags
;
ownsBuffer
=
true
;
try
{
buffer
=
new
cl
::
Buffer
(
context
.
getContext
(),
flags
,
size
*
elementSize
);
}
...
...
@@ -43,18 +67,32 @@ OpenCLArray::OpenCLArray(OpenCLContext& context, int size, int elementSize, cons
}
}
OpenCLArray
::
OpenCLArray
(
OpenCLContext
&
context
,
cl
::
Buffer
*
buffer
,
int
size
,
int
elementSize
,
const
std
::
string
&
name
)
:
context
(
context
),
buffer
(
buffer
),
size
(
size
),
elementSize
(
elementSize
),
name
(
name
),
ownsBuffer
(
false
)
{
void
OpenCLArray
::
initialize
(
OpenCLContext
&
context
,
cl
::
Buffer
*
buffer
,
int
size
,
int
elementSize
,
const
std
::
string
&
name
)
{
if
(
this
->
buffer
!=
NULL
)
throw
OpenMMException
(
"OpenCLArray has already been initialized"
);
this
->
context
=
&
context
;
this
->
buffer
=
buffer
;
this
->
size
=
size
;
this
->
elementSize
=
elementSize
;
this
->
name
=
name
;
ownsBuffer
=
false
;
}
OpenCLArray
::~
OpenCLArray
()
{
if
(
ownsBuffer
)
void
OpenCLArray
::
resize
(
int
size
)
{
if
(
buffer
==
NULL
)
throw
OpenMMException
(
"OpenCLArray has not been initialized"
);
if
(
!
ownsBuffer
)
throw
OpenMMException
(
"Cannot resize an array that does not own its storage"
);
delete
buffer
;
buffer
=
NULL
;
initialize
(
*
context
,
size
,
elementSize
,
name
,
flags
);
}
void
OpenCLArray
::
upload
(
const
void
*
data
,
bool
blocking
)
{
if
(
buffer
==
NULL
)
throw
OpenMMException
(
"OpenCLArray has not been initialized"
);
try
{
context
.
getQueue
().
enqueueWriteBuffer
(
*
buffer
,
blocking
?
CL_TRUE
:
CL_FALSE
,
0
,
size
*
elementSize
,
data
);
context
->
getQueue
().
enqueueWriteBuffer
(
*
buffer
,
blocking
?
CL_TRUE
:
CL_FALSE
,
0
,
size
*
elementSize
,
data
);
}
catch
(
cl
::
Error
err
)
{
std
::
stringstream
str
;
...
...
@@ -64,8 +102,10 @@ void OpenCLArray::upload(const void* data, bool blocking) {
}
void
OpenCLArray
::
download
(
void
*
data
,
bool
blocking
)
const
{
if
(
buffer
==
NULL
)
throw
OpenMMException
(
"OpenCLArray has not been initialized"
);
try
{
context
.
getQueue
().
enqueueReadBuffer
(
*
buffer
,
blocking
?
CL_TRUE
:
CL_FALSE
,
0
,
size
*
elementSize
,
data
);
context
->
getQueue
().
enqueueReadBuffer
(
*
buffer
,
blocking
?
CL_TRUE
:
CL_FALSE
,
0
,
size
*
elementSize
,
data
);
}
catch
(
cl
::
Error
err
)
{
std
::
stringstream
str
;
...
...
@@ -75,10 +115,12 @@ void OpenCLArray::download(void* data, bool blocking) const {
}
void
OpenCLArray
::
copyTo
(
OpenCLArray
&
dest
)
const
{
if
(
buffer
==
NULL
)
throw
OpenMMException
(
"OpenCLArray has not been initialized"
);
if
(
dest
.
getSize
()
!=
size
||
dest
.
getElementSize
()
!=
elementSize
)
throw
OpenMMException
(
"Error copying array "
+
name
+
" to "
+
dest
.
getName
()
+
": The destination array does not match the size of the array"
);
try
{
context
.
getQueue
().
enqueueCopyBuffer
(
*
buffer
,
dest
.
getDeviceBuffer
(),
0
,
0
,
size
*
elementSize
);
context
->
getQueue
().
enqueueCopyBuffer
(
*
buffer
,
dest
.
getDeviceBuffer
(),
0
,
0
,
size
*
elementSize
);
}
catch
(
cl
::
Error
err
)
{
std
::
stringstream
str
;
...
...
platforms/opencl/src/OpenCLContext.cpp
View file @
d59b0373
...
...
@@ -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
7
Stanford University and the Authors. *
* Portions copyright (c) 2009-201
8
Stanford University and the Authors. *
* Authors: Peter Eastman *
* Contributors: *
* *
...
...
@@ -68,9 +68,8 @@ static void CL_CALLBACK errorCallback(const char* errinfo, const void* private_i
}
OpenCLContext
::
OpenCLContext
(
const
System
&
system
,
int
platformIndex
,
int
deviceIndex
,
const
string
&
precision
,
OpenCLPlatform
::
PlatformData
&
platformData
,
OpenCLContext
*
originalContext
)
:
system
(
system
),
time
(
0.0
),
platformData
(
platformData
),
stepCount
(
0
),
computeForceCount
(
0
),
stepsSinceReorder
(
99999
),
atomsWereReordered
(
false
),
posq
(
NULL
),
posqCorrection
(
NULL
),
velm
(
NULL
),
forceBuffers
(
NULL
),
longForceBuffer
(
NULL
),
energyBuffer
(
NULL
),
energySum
(
NULL
),
energyParamDerivBuffer
(
NULL
),
atomIndexDevice
(
NULL
),
chargeBuffer
(
NULL
),
integration
(
NULL
),
expression
(
NULL
),
bonded
(
NULL
),
nonbonded
(
NULL
),
thread
(
NULL
)
{
system
(
system
),
time
(
0.0
),
platformData
(
platformData
),
stepCount
(
0
),
computeForceCount
(
0
),
stepsSinceReorder
(
99999
),
atomsWereReordered
(
false
),
integration
(
NULL
),
expression
(
NULL
),
bonded
(
NULL
),
nonbonded
(
NULL
),
thread
(
NULL
)
{
if
(
precision
==
"single"
)
{
useDoublePrecision
=
false
;
useMixedPrecision
=
false
;
...
...
@@ -275,23 +274,23 @@ OpenCLContext::OpenCLContext(const System& system, int platformIndex, int device
numAtomBlocks
=
(
paddedNumAtoms
+
(
TileSize
-
1
))
/
TileSize
;
numThreadBlocks
=
numThreadBlocksPerComputeUnit
*
device
.
getInfo
<
CL_DEVICE_MAX_COMPUTE_UNITS
>
();
if
(
useDoublePrecision
)
{
posq
=
OpenCLArray
::
creat
e
<
mm_double4
>
(
*
this
,
paddedNumAtoms
,
"posq"
);
velm
=
OpenCLArray
::
creat
e
<
mm_double4
>
(
*
this
,
paddedNumAtoms
,
"velm"
);
posq
.
initializ
e
<
mm_double4
>
(
*
this
,
paddedNumAtoms
,
"posq"
);
velm
.
initializ
e
<
mm_double4
>
(
*
this
,
paddedNumAtoms
,
"velm"
);
compilationDefines
[
"USE_DOUBLE_PRECISION"
]
=
"1"
;
compilationDefines
[
"convert_real4"
]
=
"convert_double4"
;
compilationDefines
[
"convert_mixed4"
]
=
"convert_double4"
;
}
else
if
(
useMixedPrecision
)
{
posq
=
OpenCLArray
::
creat
e
<
mm_float4
>
(
*
this
,
paddedNumAtoms
,
"posq"
);
posqCorrection
=
OpenCLArray
::
creat
e
<
mm_float4
>
(
*
this
,
paddedNumAtoms
,
"posq"
);
velm
=
OpenCLArray
::
creat
e
<
mm_double4
>
(
*
this
,
paddedNumAtoms
,
"velm"
);
posq
.
initializ
e
<
mm_float4
>
(
*
this
,
paddedNumAtoms
,
"posq"
);
posqCorrection
.
initializ
e
<
mm_float4
>
(
*
this
,
paddedNumAtoms
,
"posq"
);
velm
.
initializ
e
<
mm_double4
>
(
*
this
,
paddedNumAtoms
,
"velm"
);
compilationDefines
[
"USE_MIXED_PRECISION"
]
=
"1"
;
compilationDefines
[
"convert_real4"
]
=
"convert_float4"
;
compilationDefines
[
"convert_mixed4"
]
=
"convert_double4"
;
}
else
{
posq
=
OpenCLArray
::
creat
e
<
mm_float4
>
(
*
this
,
paddedNumAtoms
,
"posq"
);
velm
=
OpenCLArray
::
creat
e
<
mm_float4
>
(
*
this
,
paddedNumAtoms
,
"velm"
);
posq
.
initializ
e
<
mm_float4
>
(
*
this
,
paddedNumAtoms
,
"posq"
);
velm
.
initializ
e
<
mm_float4
>
(
*
this
,
paddedNumAtoms
,
"velm"
);
compilationDefines
[
"convert_real4"
]
=
"convert_float4"
;
compilationDefines
[
"convert_mixed4"
]
=
"convert_float4"
;
}
...
...
@@ -429,28 +428,6 @@ OpenCLContext::~OpenCLContext() {
delete
computation
;
if
(
pinnedBuffer
!=
NULL
)
delete
pinnedBuffer
;
if
(
posq
!=
NULL
)
delete
posq
;
if
(
posqCorrection
!=
NULL
)
delete
posqCorrection
;
if
(
velm
!=
NULL
)
delete
velm
;
if
(
force
!=
NULL
)
delete
force
;
if
(
forceBuffers
!=
NULL
)
delete
forceBuffers
;
if
(
longForceBuffer
!=
NULL
)
delete
longForceBuffer
;
if
(
energyBuffer
!=
NULL
)
delete
energyBuffer
;
if
(
energySum
!=
NULL
)
delete
energySum
;
if
(
energyParamDerivBuffer
!=
NULL
)
delete
energyParamDerivBuffer
;
if
(
atomIndexDevice
!=
NULL
)
delete
atomIndexDevice
;
if
(
chargeBuffer
!=
NULL
)
delete
chargeBuffer
;
if
(
integration
!=
NULL
)
delete
integration
;
if
(
expression
!=
NULL
)
...
...
@@ -471,42 +448,42 @@ void OpenCLContext::initialize() {
numForceBuffers
=
std
::
max
(
numForceBuffers
,
force
->
getRequiredForceBuffers
());
int
energyBufferSize
=
max
(
numThreadBlocks
*
ThreadBlockSize
,
nonbonded
->
getNumEnergyBuffers
());
if
(
useDoublePrecision
)
{
forceBuffers
=
OpenCLArray
::
creat
e
<
mm_double4
>
(
*
this
,
paddedNumAtoms
*
numForceBuffers
,
"forceBuffers"
);
force
=
OpenCLArray
::
creat
e
<
mm_double4
>
(
*
this
,
&
forceBuffers
->
getDeviceBuffer
(),
paddedNumAtoms
,
"force"
);
energyBuffer
=
OpenCLArray
::
creat
e
<
cl_double
>
(
*
this
,
energyBufferSize
,
"energyBuffer"
);
energySum
=
OpenCLArray
::
creat
e
<
cl_double
>
(
*
this
,
1
,
"energySum"
);
forceBuffers
.
initializ
e
<
mm_double4
>
(
*
this
,
paddedNumAtoms
*
numForceBuffers
,
"forceBuffers"
);
force
.
initializ
e
<
mm_double4
>
(
*
this
,
&
forceBuffers
.
getDeviceBuffer
(),
paddedNumAtoms
,
"force"
);
energyBuffer
.
initializ
e
<
cl_double
>
(
*
this
,
energyBufferSize
,
"energyBuffer"
);
energySum
.
initializ
e
<
cl_double
>
(
*
this
,
1
,
"energySum"
);
}
else
if
(
useMixedPrecision
)
{
forceBuffers
=
OpenCLArray
::
creat
e
<
mm_float4
>
(
*
this
,
paddedNumAtoms
*
numForceBuffers
,
"forceBuffers"
);
force
=
OpenCLArray
::
creat
e
<
mm_float4
>
(
*
this
,
&
forceBuffers
->
getDeviceBuffer
(),
paddedNumAtoms
,
"force"
);
energyBuffer
=
OpenCLArray
::
creat
e
<
cl_double
>
(
*
this
,
energyBufferSize
,
"energyBuffer"
);
energySum
=
OpenCLArray
::
creat
e
<
cl_double
>
(
*
this
,
1
,
"energySum"
);
forceBuffers
.
initializ
e
<
mm_float4
>
(
*
this
,
paddedNumAtoms
*
numForceBuffers
,
"forceBuffers"
);
force
.
initializ
e
<
mm_float4
>
(
*
this
,
&
forceBuffers
.
getDeviceBuffer
(),
paddedNumAtoms
,
"force"
);
energyBuffer
.
initializ
e
<
cl_double
>
(
*
this
,
energyBufferSize
,
"energyBuffer"
);
energySum
.
initializ
e
<
cl_double
>
(
*
this
,
1
,
"energySum"
);
}
else
{
forceBuffers
=
OpenCLArray
::
creat
e
<
mm_float4
>
(
*
this
,
paddedNumAtoms
*
numForceBuffers
,
"forceBuffers"
);
force
=
OpenCLArray
::
creat
e
<
mm_float4
>
(
*
this
,
&
forceBuffers
->
getDeviceBuffer
(),
paddedNumAtoms
,
"force"
);
energyBuffer
=
OpenCLArray
::
creat
e
<
cl_float
>
(
*
this
,
energyBufferSize
,
"energyBuffer"
);
energySum
=
OpenCLArray
::
creat
e
<
cl_float
>
(
*
this
,
1
,
"energySum"
);
forceBuffers
.
initializ
e
<
mm_float4
>
(
*
this
,
paddedNumAtoms
*
numForceBuffers
,
"forceBuffers"
);
force
.
initializ
e
<
mm_float4
>
(
*
this
,
&
forceBuffers
.
getDeviceBuffer
(),
paddedNumAtoms
,
"force"
);
energyBuffer
.
initializ
e
<
cl_float
>
(
*
this
,
energyBufferSize
,
"energyBuffer"
);
energySum
.
initializ
e
<
cl_float
>
(
*
this
,
1
,
"energySum"
);
}
if
(
supports64BitGlobalAtomics
)
{
longForceBuffer
=
OpenCLArray
::
creat
e
<
cl_long
>
(
*
this
,
3
*
paddedNumAtoms
,
"longForceBuffer"
);
reduceForcesKernel
.
setArg
<
cl
::
Buffer
>
(
0
,
longForceBuffer
->
getDeviceBuffer
());
reduceForcesKernel
.
setArg
<
cl
::
Buffer
>
(
1
,
forceBuffers
->
getDeviceBuffer
());
longForceBuffer
.
initializ
e
<
cl_long
>
(
*
this
,
3
*
paddedNumAtoms
,
"longForceBuffer"
);
reduceForcesKernel
.
setArg
<
cl
::
Buffer
>
(
0
,
longForceBuffer
.
getDeviceBuffer
());
reduceForcesKernel
.
setArg
<
cl
::
Buffer
>
(
1
,
forceBuffers
.
getDeviceBuffer
());
reduceForcesKernel
.
setArg
<
cl_int
>
(
2
,
paddedNumAtoms
);
reduceForcesKernel
.
setArg
<
cl_int
>
(
3
,
numForceBuffers
);
addAutoclearBuffer
(
*
longForceBuffer
);
addAutoclearBuffer
(
longForceBuffer
);
}
addAutoclearBuffer
(
*
forceBuffers
);
addAutoclearBuffer
(
*
energyBuffer
);
addAutoclearBuffer
(
forceBuffers
);
addAutoclearBuffer
(
energyBuffer
);
int
numEnergyParamDerivs
=
energyParamDerivNames
.
size
();
if
(
numEnergyParamDerivs
>
0
)
{
if
(
useDoublePrecision
||
useMixedPrecision
)
energyParamDerivBuffer
=
OpenCLArray
::
creat
e
<
cl_double
>
(
*
this
,
numEnergyParamDerivs
*
energyBufferSize
,
"energyParamDerivBuffer"
);
energyParamDerivBuffer
.
initializ
e
<
cl_double
>
(
*
this
,
numEnergyParamDerivs
*
energyBufferSize
,
"energyParamDerivBuffer"
);
else
energyParamDerivBuffer
=
OpenCLArray
::
creat
e
<
cl_float
>
(
*
this
,
numEnergyParamDerivs
*
energyBufferSize
,
"energyParamDerivBuffer"
);
addAutoclearBuffer
(
*
energyParamDerivBuffer
);
energyParamDerivBuffer
.
initializ
e
<
cl_float
>
(
*
this
,
numEnergyParamDerivs
*
energyBufferSize
,
"energyParamDerivBuffer"
);
addAutoclearBuffer
(
energyParamDerivBuffer
);
}
int
bufferBytes
=
max
(
velm
->
getSize
()
*
velm
->
getElementSize
(),
energyBufferSize
*
energyBuffer
->
getElementSize
());
int
bufferBytes
=
max
(
velm
.
getSize
()
*
velm
.
getElementSize
(),
energyBufferSize
*
energyBuffer
.
getElementSize
());
pinnedBuffer
=
new
cl
::
Buffer
(
context
,
CL_MEM_ALLOC_HOST_PTR
,
bufferBytes
);
pinnedMemory
=
currentQueue
.
enqueueMapBuffer
(
*
pinnedBuffer
,
CL_TRUE
,
CL_MAP_READ
|
CL_MAP_WRITE
,
0
,
bufferBytes
);
for
(
int
i
=
0
;
i
<
numAtoms
;
i
++
)
{
...
...
@@ -516,12 +493,12 @@ void OpenCLContext::initialize() {
else
((
mm_float4
*
)
pinnedMemory
)[
i
]
=
mm_float4
(
0.0
f
,
0.0
f
,
0.0
f
,
mass
==
0.0
?
0.0
f
:
(
cl_float
)
(
1.0
/
mass
));
}
velm
->
upload
(
pinnedMemory
);
atomIndexDevice
=
OpenCLArray
::
creat
e
<
cl_int
>
(
*
this
,
paddedNumAtoms
,
"atomIndexDevice"
);
velm
.
upload
(
pinnedMemory
);
atomIndexDevice
.
initializ
e
<
cl_int
>
(
*
this
,
paddedNumAtoms
,
"atomIndexDevice"
);
atomIndex
.
resize
(
paddedNumAtoms
);
for
(
int
i
=
0
;
i
<
paddedNumAtoms
;
++
i
)
atomIndex
[
i
]
=
i
;
atomIndexDevice
->
upload
(
atomIndex
);
atomIndexDevice
.
upload
(
atomIndex
);
findMoleculeGroups
();
nonbonded
->
initialize
(
system
);
}
...
...
@@ -756,7 +733,7 @@ void OpenCLContext::reduceForces() {
if
(
supports64BitGlobalAtomics
)
executeKernel
(
reduceForcesKernel
,
paddedNumAtoms
,
128
);
else
reduceBuffer
(
*
forceBuffers
,
numForceBuffers
);
reduceBuffer
(
forceBuffers
,
numForceBuffers
);
}
void
OpenCLContext
::
reduceBuffer
(
OpenCLArray
&
array
,
int
numBuffers
)
{
...
...
@@ -771,42 +748,42 @@ double OpenCLContext::reduceEnergy() {
int
workGroupSize
=
device
.
getInfo
<
CL_DEVICE_MAX_WORK_GROUP_SIZE
>
();
if
(
workGroupSize
>
512
)
workGroupSize
=
512
;
reduceEnergyKernel
.
setArg
<
cl
::
Buffer
>
(
0
,
energyBuffer
->
getDeviceBuffer
());
reduceEnergyKernel
.
setArg
<
cl
::
Buffer
>
(
1
,
energySum
->
getDeviceBuffer
());
reduceEnergyKernel
.
setArg
<
cl_int
>
(
2
,
energyBuffer
->
getSize
());
reduceEnergyKernel
.
setArg
<
cl
::
Buffer
>
(
0
,
energyBuffer
.
getDeviceBuffer
());
reduceEnergyKernel
.
setArg
<
cl
::
Buffer
>
(
1
,
energySum
.
getDeviceBuffer
());
reduceEnergyKernel
.
setArg
<
cl_int
>
(
2
,
energyBuffer
.
getSize
());
reduceEnergyKernel
.
setArg
<
cl_int
>
(
3
,
workGroupSize
);
reduceEnergyKernel
.
setArg
(
4
,
workGroupSize
*
energyBuffer
->
getElementSize
(),
NULL
);
reduceEnergyKernel
.
setArg
(
4
,
workGroupSize
*
energyBuffer
.
getElementSize
(),
NULL
);
executeKernel
(
reduceEnergyKernel
,
workGroupSize
,
workGroupSize
);
if
(
getUseDoublePrecision
()
||
getUseMixedPrecision
())
{
double
energy
;
energySum
->
download
(
&
energy
);
energySum
.
download
(
&
energy
);
return
energy
;
}
else
{
float
energy
;
energySum
->
download
(
&
energy
);
energySum
.
download
(
&
energy
);
return
energy
;
}
}
void
OpenCLContext
::
setCharges
(
const
vector
<
double
>&
charges
)
{
if
(
chargeBuffer
==
NULL
)
chargeBuffer
=
new
OpenCLArray
(
*
this
,
numAtoms
,
useDoublePrecision
?
sizeof
(
double
)
:
sizeof
(
float
),
"chargeBuffer"
);
if
(
!
chargeBuffer
.
isInitialized
()
)
chargeBuffer
.
initialize
(
*
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
);
chargeBuffer
.
upload
(
c
);
}
else
{
float
*
c
=
(
float
*
)
getPinnedBuffer
();
for
(
int
i
=
0
;
i
<
charges
.
size
();
i
++
)
c
[
i
]
=
(
float
)
charges
[
i
];
chargeBuffer
->
upload
(
c
);
chargeBuffer
.
upload
(
c
);
}
setChargesKernel
.
setArg
<
cl
::
Buffer
>
(
0
,
chargeBuffer
->
getDeviceBuffer
());
setChargesKernel
.
setArg
<
cl
::
Buffer
>
(
1
,
posq
->
getDeviceBuffer
());
setChargesKernel
.
setArg
<
cl
::
Buffer
>
(
2
,
atomIndexDevice
->
getDeviceBuffer
());
setChargesKernel
.
setArg
<
cl
::
Buffer
>
(
0
,
chargeBuffer
.
getDeviceBuffer
());
setChargesKernel
.
setArg
<
cl
::
Buffer
>
(
1
,
posq
.
getDeviceBuffer
());
setChargesKernel
.
setArg
<
cl
::
Buffer
>
(
2
,
atomIndexDevice
.
getDeviceBuffer
());
setChargesKernel
.
setArg
<
cl_int
>
(
3
,
numAtoms
);
executeKernel
(
setChargesKernel
,
numAtoms
);
}
...
...
@@ -1069,16 +1046,16 @@ bool OpenCLContext::invalidateMolecules(OpenCLForceInfo* force) {
vector
<
mm_double4
>
newPosq
(
paddedNumAtoms
,
mm_double4
(
0
,
0
,
0
,
0
));
vector
<
mm_double4
>
oldVelm
(
paddedNumAtoms
);
vector
<
mm_double4
>
newVelm
(
paddedNumAtoms
,
mm_double4
(
0
,
0
,
0
,
0
));
posq
->
download
(
oldPosq
);
velm
->
download
(
oldVelm
);
posq
.
download
(
oldPosq
);
velm
.
download
(
oldVelm
);
for
(
int
i
=
0
;
i
<
numAtoms
;
i
++
)
{
int
index
=
atomIndex
[
i
];
newPosq
[
index
]
=
oldPosq
[
i
];
newVelm
[
index
]
=
oldVelm
[
i
];
newCellOffsets
[
index
]
=
posCellOffsets
[
i
];
}
posq
->
upload
(
newPosq
);
velm
->
upload
(
newVelm
);
posq
.
upload
(
newPosq
);
velm
.
upload
(
newVelm
);
}
else
if
(
useMixedPrecision
)
{
vector
<
mm_float4
>
oldPosq
(
paddedNumAtoms
);
...
...
@@ -1087,8 +1064,8 @@ bool OpenCLContext::invalidateMolecules(OpenCLForceInfo* force) {
vector
<
mm_float4
>
newPosqCorrection
(
paddedNumAtoms
,
mm_float4
(
0
,
0
,
0
,
0
));
vector
<
mm_double4
>
oldVelm
(
paddedNumAtoms
);
vector
<
mm_double4
>
newVelm
(
paddedNumAtoms
,
mm_double4
(
0
,
0
,
0
,
0
));
posq
->
download
(
oldPosq
);
velm
->
download
(
oldVelm
);
posq
.
download
(
oldPosq
);
velm
.
download
(
oldVelm
);
for
(
int
i
=
0
;
i
<
numAtoms
;
i
++
)
{
int
index
=
atomIndex
[
i
];
newPosq
[
index
]
=
oldPosq
[
i
];
...
...
@@ -1096,31 +1073,31 @@ bool OpenCLContext::invalidateMolecules(OpenCLForceInfo* force) {
newVelm
[
index
]
=
oldVelm
[
i
];
newCellOffsets
[
index
]
=
posCellOffsets
[
i
];
}
posq
->
upload
(
newPosq
);
posqCorrection
->
upload
(
newPosqCorrection
);
velm
->
upload
(
newVelm
);
posq
.
upload
(
newPosq
);
posqCorrection
.
upload
(
newPosqCorrection
);
velm
.
upload
(
newVelm
);
}
else
{
vector
<
mm_float4
>
oldPosq
(
paddedNumAtoms
);
vector
<
mm_float4
>
newPosq
(
paddedNumAtoms
,
mm_float4
(
0
,
0
,
0
,
0
));
vector
<
mm_float4
>
oldVelm
(
paddedNumAtoms
);
vector
<
mm_float4
>
newVelm
(
paddedNumAtoms
,
mm_float4
(
0
,
0
,
0
,
0
));
posq
->
download
(
oldPosq
);
velm
->
download
(
oldVelm
);
posq
.
download
(
oldPosq
);
velm
.
download
(
oldVelm
);
for
(
int
i
=
0
;
i
<
numAtoms
;
i
++
)
{
int
index
=
atomIndex
[
i
];
newPosq
[
index
]
=
oldPosq
[
i
];
newVelm
[
index
]
=
oldVelm
[
i
];
newCellOffsets
[
index
]
=
posCellOffsets
[
i
];
}
posq
->
upload
(
newPosq
);
velm
->
upload
(
newVelm
);
posq
.
upload
(
newPosq
);
velm
.
upload
(
newVelm
);
}
for
(
int
i
=
0
;
i
<
numAtoms
;
i
++
)
{
atomIndex
[
i
]
=
i
;
posCellOffsets
[
i
]
=
newCellOffsets
[
i
];
}
atomIndexDevice
->
upload
(
atomIndex
);
atomIndexDevice
.
upload
(
atomIndex
);
findMoleculeGroups
();
for
(
auto
listener
:
reorderListeners
)
listener
->
execute
();
...
...
@@ -1152,10 +1129,10 @@ void OpenCLContext::reorderAtomsImpl() {
vector
<
Real4
>
oldPosq
(
paddedNumAtoms
);
vector
<
Real4
>
oldPosqCorrection
(
paddedNumAtoms
);
vector
<
Mixed4
>
oldVelm
(
paddedNumAtoms
);
posq
->
download
(
oldPosq
);
velm
->
download
(
oldVelm
);
posq
.
download
(
oldPosq
);
velm
.
download
(
oldVelm
);
if
(
useMixedPrecision
)
posqCorrection
->
download
(
oldPosqCorrection
);
posqCorrection
.
download
(
oldPosqCorrection
);
Real
minx
=
oldPosq
[
0
].
x
,
maxx
=
oldPosq
[
0
].
x
;
Real
miny
=
oldPosq
[
0
].
y
,
maxy
=
oldPosq
[
0
].
y
;
Real
minz
=
oldPosq
[
0
].
z
,
maxz
=
oldPosq
[
0
].
z
;
...
...
@@ -1299,11 +1276,11 @@ void OpenCLContext::reorderAtomsImpl() {
atomIndex
[
i
]
=
originalIndex
[
i
];
posCellOffsets
[
i
]
=
newCellOffsets
[
i
];
}
posq
->
upload
(
newPosq
);
posq
.
upload
(
newPosq
);
if
(
useMixedPrecision
)
posqCorrection
->
upload
(
newPosqCorrection
);
velm
->
upload
(
newVelm
);
atomIndexDevice
->
upload
(
atomIndex
);
posqCorrection
.
upload
(
newPosqCorrection
);
velm
.
upload
(
newVelm
);
atomIndexDevice
.
upload
(
atomIndex
);
for
(
auto
listener
:
reorderListeners
)
listener
->
execute
();
}
...
...
platforms/opencl/src/OpenCLKernels.cpp
View file @
d59b0373
This diff is collapsed.
Click to expand it.
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