OpenCLKernels.h 7.21 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
#ifndef OPENMM_OPENCLKERNELS_H_
#define OPENMM_OPENCLKERNELS_H_

/* -------------------------------------------------------------------------- *
 *                                   OpenMM                                   *
 * -------------------------------------------------------------------------- *
 * This is part of the OpenMM molecular simulation toolkit originating from   *
 * Simbios, the NIH National Center for Physics-Based Simulation of           *
 * Biological Structures at Stanford, funded under the NIH Roadmap for        *
 * Medical Research, grant U54 GM072970. See https://simtk.org.               *
 *                                                                            *
12
 * Portions copyright (c) 2008-2025 Stanford University and the Authors.      *
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
 * Authors: Peter Eastman                                                     *
 * Contributors:                                                              *
 *                                                                            *
 * This program is free software: you can redistribute it and/or modify       *
 * it under the terms of the GNU Lesser General Public License as published   *
 * by the Free Software Foundation, either version 3 of the License, or       *
 * (at your option) any later version.                                        *
 *                                                                            *
 * This program is distributed in the hope that it will be useful,            *
 * but WITHOUT ANY WARRANTY; without even the implied warranty of             *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *
 * GNU Lesser General Public License for more details.                        *
 *                                                                            *
 * You should have received a copy of the GNU Lesser General Public License   *
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.      *
 * -------------------------------------------------------------------------- */

#include "OpenCLPlatform.h"
31
#include "OpenCLContext.h"
32
33
#include "openmm/kernels.h"
#include "openmm/System.h"
34
#include "openmm/common/CommonKernels.h"
35
#include "openmm/common/CommonCalcNonbondedForce.h"
36
#include "openmm/common/CommonCalcConstantPotentialForce.h"
37
38
39
#include "openmm/common/ComputeArray.h"
#include "openmm/common/ComputeSort.h"
#include "openmm/common/FFT3D.h"
40
41
42

namespace OpenMM {

43
/**
44
45
46
 * This kernel is invoked at the beginning and end of force and energy computations.  It gives the
 * Platform a chance to clear buffers and do other initialization at the beginning, and to do any
 * necessary work at the end to determine the final results.
47
 */
48
class OpenCLCalcForcesAndEnergyKernel : public CalcForcesAndEnergyKernel {
49
public:
50
    OpenCLCalcForcesAndEnergyKernel(std::string name, const Platform& platform, OpenCLContext& cl) : CalcForcesAndEnergyKernel(name, platform), cl(cl) {
51
52
53
54
55
56
57
58
    }
    /**
     * Initialize the kernel.
     *
     * @param system     the System this kernel will be applied to
     */
    void initialize(const System& system);
    /**
59
     * This is called at the beginning of each force/energy computation, before calcForcesAndEnergy() has been called on
60
     * any ForceImpl.
61
     *
62
63
64
     * @param context       the context in which to execute this kernel
     * @param includeForce  true if forces should be computed
     * @param includeEnergy true if potential energy should be computed
65
     * @param groups        a set of bit flags for which force groups to include
66
     */
67
    void beginComputation(ContextImpl& context, bool includeForce, bool includeEnergy, int groups);
68
    /**
69
     * This is called at the end of each force/energy computation, after calcForcesAndEnergy() has been called on
70
71
     * every ForceImpl.
     *
72
73
74
     * @param context       the context in which to execute this kernel
     * @param includeForce  true if forces should be computed
     * @param includeEnergy true if potential energy should be computed
75
     * @param groups        a set of bit flags for which force groups to include
76
77
     * @param valid         the method may set this to false to indicate the results are invalid and the force/energy
     *                      calculation should be repeated
78
     * @return the potential energy of the system.  This value is added to all values returned by ForceImpls'
79
     * calcForcesAndEnergy() methods.  That is, each force kernel may <i>either</i> return its contribution to the
80
81
     * energy directly, <i>or</i> add it to an internal buffer so that it will be included here.
     */
82
    double finishComputation(ContextImpl& context, bool includeForce, bool includeEnergy, int groups, bool& valid);
83
private:
84
   OpenCLContext& cl;
85
86
};

87
88
89
/**
 * This kernel is invoked by NonbondedForce to calculate the forces acting on the system.
 */
90
class OpenCLCalcNonbondedForceKernel : public CommonCalcNonbondedForceKernel {
91
public:
92
93
    OpenCLCalcNonbondedForceKernel(std::string name, const Platform& platform, OpenCLContext& cl, const System& system) :
            CommonCalcNonbondedForceKernel(name, platform, cl, system), cl(cl) {
94
95
96
97
98
99
100
101
102
    }
    /**
     * Initialize the kernel.
     *
     * @param system     the System this kernel will be applied to
     * @param force      the NonbondedForce this kernel will be used for
     */
    void initialize(const System& system, const NonbondedForce& force);
private:
103
   OpenCLContext& cl;
104
105
};

106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/**
 * This kernel is invoked by ConstantPotentialForce to calculate the forces acting on the system.
 */
class OpenCLCalcConstantPotentialForceKernel : public CommonCalcConstantPotentialForceKernel {
public:
    OpenCLCalcConstantPotentialForceKernel(std::string name, const Platform& platform, OpenCLContext& cl, const System& system) :
            CommonCalcConstantPotentialForceKernel(name, platform, cl, system), cl(cl) {
    }
    /**
     * Initialize the kernel.
     *
     * @param system     the System this kernel will be applied to
     * @param force      the ConstantPotentialForce this kernel will be used for
     */
    void initialize(const System& system, const ConstantPotentialForce& force);
private:
   OpenCLContext& cl;
};

125
/**
126
 * This kernel is invoked by CustomCVForce to calculate the forces acting on the system and the energy of the system.
127
 */
128
class OpenCLCalcCustomCVForceKernel : public CommonCalcCustomCVForceKernel {
129
public:
130
    OpenCLCalcCustomCVForceKernel(std::string name, const Platform& platform, ComputeContext& cc) : CommonCalcCustomCVForceKernel(name, platform, cc) {
131
    }
132
133
    ComputeContext& getInnerComputeContext(ContextImpl& innerContext) {
        return *reinterpret_cast<OpenCLPlatform::PlatformData*>(innerContext.getPlatformData())->contexts[0];
134
    }
135
};
136

137
138
139
140
141
142
143
144
145
146
147
148
149
/**
 * This kernel is invoked by ATMForce to calculate the forces acting on the system and the energy of the system.
 */
class OpenCLCalcATMForceKernel : public CommonCalcATMForceKernel {
public:
    OpenCLCalcATMForceKernel(std::string name, const Platform& platform, ComputeContext& cc) : CommonCalcATMForceKernel(name, platform, cc) {
    }
    ComputeContext& getInnerComputeContext(ContextImpl& innerContext) {
        return *reinterpret_cast<OpenCLPlatform::PlatformData*>(innerContext.getPlatformData())->contexts[0];
    }
};

  
150
151
152
} // namespace OpenMM

#endif /*OPENMM_OPENCLKERNELS_H_*/