OpenCLKernels.h 6.42 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
36
37
38
#include "openmm/common/CommonCalcNonbondedForce.h"
#include "openmm/common/ComputeArray.h"
#include "openmm/common/ComputeSort.h"
#include "openmm/common/FFT3D.h"
39
40
41

namespace OpenMM {

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

86
87
88
/**
 * This kernel is invoked by NonbondedForce to calculate the forces acting on the system.
 */
89
class OpenCLCalcNonbondedForceKernel : public CommonCalcNonbondedForceKernel {
90
public:
91
92
    OpenCLCalcNonbondedForceKernel(std::string name, const Platform& platform, OpenCLContext& cl, const System& system) :
            CommonCalcNonbondedForceKernel(name, platform, cl, system), cl(cl) {
93
94
95
96
97
98
99
100
101
    }
    /**
     * 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:
102
   OpenCLContext& cl;
103
104
};

105
/**
106
 * This kernel is invoked by CustomCVForce to calculate the forces acting on the system and the energy of the system.
107
 */
108
class OpenCLCalcCustomCVForceKernel : public CommonCalcCustomCVForceKernel {
109
public:
110
    OpenCLCalcCustomCVForceKernel(std::string name, const Platform& platform, ComputeContext& cc) : CommonCalcCustomCVForceKernel(name, platform, cc) {
111
    }
112
113
    ComputeContext& getInnerComputeContext(ContextImpl& innerContext) {
        return *reinterpret_cast<OpenCLPlatform::PlatformData*>(innerContext.getPlatformData())->contexts[0];
114
    }
115
};
116

117
118
119
120
121
122
123
124
125
126
127
128
129
/**
 * 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];
    }
};

  
130
131
132
} // namespace OpenMM

#endif /*OPENMM_OPENCLKERNELS_H_*/