66 * Biological Structures at Stanford, funded under the NIH Roadmap for *
77 * Medical Research, grant U54 GM072970. See https://simtk.org. *
88 * *
9- * Portions copyright (c) 2014 Stanford University and the Authors. *
9+ * Portions copyright (c) 2014-2021 Stanford University and the Authors. *
1010 * Authors: Peter Eastman *
1111 * Contributors: *
1212 * *
2929 * USE OR OTHER DEALINGS IN THE SOFTWARE. *
3030 * -------------------------------------------------------------------------- */
3131
32- #include " OpenCLExampleKernels.h"
33- #include " OpenCLExampleKernelSources.h"
32+ #include " CommonExampleKernels.h"
33+ #include " CommonExampleKernelSources.h"
34+ #include " openmm/common/BondedUtilities.h"
35+ #include " openmm/common/ComputeForceInfo.h"
3436#include " openmm/internal/ContextImpl.h"
35- #include " openmm/opencl/OpenCLBondedUtilities.h"
36- #include " openmm/opencl/OpenCLForceInfo.h"
3737
3838using namespace ExamplePlugin ;
3939using namespace OpenMM ;
4040using namespace std ;
4141
42- class OpenCLExampleForceInfo : public OpenCLForceInfo {
42+ class CommonExampleForceInfo : public ComputeForceInfo {
4343public:
44- OpenCLExampleForceInfo (const ExampleForce& force) : OpenCLForceInfo( 0 ), force(force) {
44+ CommonExampleForceInfo (const ExampleForce& force) : force(force) {
4545 }
4646 int getNumParticleGroups () {
4747 return force.getNumBonds ();
@@ -65,41 +65,36 @@ class OpenCLExampleForceInfo : public OpenCLForceInfo {
6565 const ExampleForce& force;
6666};
6767
68- OpenCLCalcExampleForceKernel::~OpenCLCalcExampleForceKernel () {
69- if (params != NULL )
70- delete params;
71- }
72-
73- void OpenCLCalcExampleForceKernel::initialize (const System& system, const ExampleForce& force) {
74- int numContexts = cl.getPlatformData ().contexts .size ();
75- int startIndex = cl.getContextIndex ()*force.getNumBonds ()/numContexts;
76- int endIndex = (cl.getContextIndex ()+1 )*force.getNumBonds ()/numContexts;
68+ void CommonCalcExampleForceKernel::initialize (const System& system, const ExampleForce& force) {
69+ int numContexts = cc.getNumContexts ();
70+ int startIndex = cc.getContextIndex ()*force.getNumBonds ()/numContexts;
71+ int endIndex = (cc.getContextIndex ()+1 )*force.getNumBonds ()/numContexts;
7772 numBonds = endIndex-startIndex;
7873 if (numBonds == 0 )
7974 return ;
8075 vector<vector<int > > atoms (numBonds, vector<int >(2 ));
81- params = OpenCLArray::create <mm_float2>(cl , numBonds, " bondParams" );
76+ params. initialize <mm_float2>(cc , numBonds, " bondParams" );
8277 vector<mm_float2> paramVector (numBonds);
8378 for (int i = 0 ; i < numBonds; i++) {
8479 double length, k;
8580 force.getBondParameters (startIndex+i, atoms[i][0 ], atoms[i][1 ], length, k);
86- paramVector[i] = mm_float2 ((cl_float ) length, (cl_float ) k);
81+ paramVector[i] = mm_float2 ((float ) length, (float ) k);
8782 }
88- params-> upload (paramVector);
83+ params. upload (paramVector);
8984 map<string, string> replacements;
90- replacements[" PARAMS" ] = cl .getBondedUtilities ().addArgument (params-> getDeviceBuffer () , " float2" );
91- cl .getBondedUtilities ().addInteraction (atoms, cl .replaceStrings (OpenCLExampleKernelSources ::exampleForce, replacements), force.getForceGroup ());
92- cl .addForce (new OpenCLExampleForceInfo (force));
85+ replacements[" PARAMS" ] = cc .getBondedUtilities ().addArgument (params, " float2" );
86+ cc .getBondedUtilities ().addInteraction (atoms, cc .replaceStrings (CommonExampleKernelSources ::exampleForce, replacements), force.getForceGroup ());
87+ cc .addForce (new CommonExampleForceInfo (force));
9388}
9489
95- double OpenCLCalcExampleForceKernel ::execute (ContextImpl& context, bool includeForces, bool includeEnergy) {
90+ double CommonCalcExampleForceKernel ::execute (ContextImpl& context, bool includeForces, bool includeEnergy) {
9691 return 0.0 ;
9792}
9893
99- void OpenCLCalcExampleForceKernel ::copyParametersToContext (ContextImpl& context, const ExampleForce& force) {
100- int numContexts = cl. getPlatformData (). contexts . size ();
101- int startIndex = cl .getContextIndex ()*force.getNumBonds ()/numContexts;
102- int endIndex = (cl .getContextIndex ()+1 )*force.getNumBonds ()/numContexts;
94+ void CommonCalcExampleForceKernel ::copyParametersToContext (ContextImpl& context, const ExampleForce& force) {
95+ int numContexts = cc. getNumContexts ();
96+ int startIndex = cc .getContextIndex ()*force.getNumBonds ()/numContexts;
97+ int endIndex = (cc .getContextIndex ()+1 )*force.getNumBonds ()/numContexts;
10398 if (numBonds != endIndex-startIndex)
10499 throw OpenMMException (" updateParametersInContext: The number of bonds has changed" );
105100 if (numBonds == 0 )
@@ -112,12 +107,12 @@ void OpenCLCalcExampleForceKernel::copyParametersToContext(ContextImpl& context,
112107 int atom1, atom2;
113108 double length, k;
114109 force.getBondParameters (startIndex+i, atom1, atom2, length, k);
115- paramVector[i] = mm_float2 ((cl_float ) length, (cl_float ) k);
110+ paramVector[i] = mm_float2 ((float ) length, (float ) k);
116111 }
117- params-> upload (paramVector);
112+ params. upload (paramVector);
118113
119114 // Mark that the current reordering may be invalid.
120115
121- cl .invalidateMolecules ();
116+ cc .invalidateMolecules ();
122117}
123118
0 commit comments