OpenCLKernelFactory.cpp 10.7 KB
Newer Older
1
2
3
/* -------------------------------------------------------------------------- *
 *                                   OpenMM                                   *
 * -------------------------------------------------------------------------- *
Evan Pretti's avatar
Evan Pretti committed
4
5
 * This is part of the OpenMM molecular simulation toolkit.                   *
 * See https://openmm.org/development.                                        *
6
 *                                                                            *
7
 * Portions copyright (c) 2008-2025 Stanford University and the Authors.      *
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
 * 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 "OpenCLKernelFactory.h"
26
#include "OpenCLParallelKernels.h"
27
#include "openmm/common/CommonKernels.h"
Peter Eastman's avatar
Peter Eastman committed
28
#include "openmm/common/CommonParallelKernels.h"
29
30
31
32
33
34
#include "openmm/common/CommonCalcCustomGBForceKernel.h"
#include "openmm/common/CommonCalcCustomHbondForceKernel.h"
#include "openmm/common/CommonCalcCustomManyParticleForceKernel.h"
#include "openmm/common/CommonCalcCustomNonbondedForceKernel.h"
#include "openmm/common/CommonIntegrateCustomStepKernel.h"
#include "openmm/common/CommonIntegrateNoseHooverStepKernel.h"
35
#include "openmm/common/CommonIntegrateQTBStepKernel.h"
36
37
38
39
40
41
42
#include "openmm/internal/ContextImpl.h"
#include "openmm/OpenMMException.h"

using namespace OpenMM;

KernelImpl* OpenCLKernelFactory::createKernelImpl(std::string name, const Platform& platform, ContextImpl& context) const {
    OpenCLPlatform::PlatformData& data = *static_cast<OpenCLPlatform::PlatformData*>(context.getPlatformData());
Peter Eastman's avatar
Peter Eastman committed
43
    OpenCLContext& cl = *data.contexts[0];
Peter Eastman's avatar
Bug fix  
Peter Eastman committed
44
    if (data.contexts.size() > 1) {
45
46
47
48
49
        // We are running in parallel on multiple devices, so we may want to create a parallel kernel.
        
        if (name == CalcForcesAndEnergyKernel::Name())
            return new OpenCLParallelCalcForcesAndEnergyKernel(name, platform, data);
        if (name == CalcHarmonicBondForceKernel::Name())
Peter Eastman's avatar
Peter Eastman committed
50
            return new CommonParallelCalcHarmonicBondForceKernel(name, platform, cl, context.getSystem());
51
        if (name == CalcCustomBondForceKernel::Name())
Peter Eastman's avatar
Peter Eastman committed
52
            return new CommonParallelCalcCustomBondForceKernel(name, platform, cl, context.getSystem());
53
        if (name == CalcHarmonicAngleForceKernel::Name())
Peter Eastman's avatar
Peter Eastman committed
54
            return new CommonParallelCalcHarmonicAngleForceKernel(name, platform, cl, context.getSystem());
55
        if (name == CalcCustomAngleForceKernel::Name())
Peter Eastman's avatar
Peter Eastman committed
56
            return new CommonParallelCalcCustomAngleForceKernel(name, platform, cl, context.getSystem());
57
        if (name == CalcPeriodicTorsionForceKernel::Name())
Peter Eastman's avatar
Peter Eastman committed
58
            return new CommonParallelCalcPeriodicTorsionForceKernel(name, platform, cl, context.getSystem());
59
        if (name == CalcRBTorsionForceKernel::Name())
Peter Eastman's avatar
Peter Eastman committed
60
            return new CommonParallelCalcRBTorsionForceKernel(name, platform, cl, context.getSystem());
61
        if (name == CalcCMAPTorsionForceKernel::Name())
Peter Eastman's avatar
Peter Eastman committed
62
            return new CommonParallelCalcCMAPTorsionForceKernel(name, platform, cl, context.getSystem());
63
        if (name == CalcCustomTorsionForceKernel::Name())
Peter Eastman's avatar
Peter Eastman committed
64
            return new CommonParallelCalcCustomTorsionForceKernel(name, platform, cl, context.getSystem());
65
66
        if (name == CalcNonbondedForceKernel::Name())
            return new OpenCLParallelCalcNonbondedForceKernel(name, platform, data, context.getSystem());
67
        if (name == CalcCustomNonbondedForceKernel::Name())
Peter Eastman's avatar
Peter Eastman committed
68
            return new CommonParallelCalcCustomNonbondedForceKernel(name, platform, cl, context.getSystem());
69
        if (name == CalcCustomExternalForceKernel::Name())
Peter Eastman's avatar
Peter Eastman committed
70
            return new CommonParallelCalcCustomExternalForceKernel(name, platform, cl, context.getSystem());
71
        if (name == CalcCustomHbondForceKernel::Name())
Peter Eastman's avatar
Peter Eastman committed
72
            return new CommonParallelCalcCustomHbondForceKernel(name, platform, cl, context.getSystem());
73
        if (name == CalcCustomCompoundBondForceKernel::Name())
Peter Eastman's avatar
Peter Eastman committed
74
            return new CommonParallelCalcCustomCompoundBondForceKernel(name, platform, cl, context.getSystem());
75
    }
76
    if (name == CalcForcesAndEnergyKernel::Name())
77
        return new OpenCLCalcForcesAndEnergyKernel(name, platform, cl);
78
    if (name == UpdateStateDataKernel::Name())
79
        return new CommonUpdateStateDataKernel(name, platform, cl);
80
    if (name == ApplyConstraintsKernel::Name())
81
        return new CommonApplyConstraintsKernel(name, platform, cl);
82
    if (name == VirtualSitesKernel::Name())
83
        return new CommonVirtualSitesKernel(name, platform, cl);
84
    if (name == CalcHarmonicBondForceKernel::Name())
85
        return new CommonCalcHarmonicBondForceKernel(name, platform, cl, context.getSystem());
86
    if (name == CalcCustomBondForceKernel::Name())
87
        return new CommonCalcCustomBondForceKernel(name, platform, cl, context.getSystem());
88
    if (name == CalcHarmonicAngleForceKernel::Name())
89
        return new CommonCalcHarmonicAngleForceKernel(name, platform, cl, context.getSystem());
90
    if (name == CalcCustomAngleForceKernel::Name())
91
        return new CommonCalcCustomAngleForceKernel(name, platform, cl, context.getSystem());
92
    if (name == CalcPeriodicTorsionForceKernel::Name())
93
        return new CommonCalcPeriodicTorsionForceKernel(name, platform, cl, context.getSystem());
94
    if (name == CalcRBTorsionForceKernel::Name())
95
        return new CommonCalcRBTorsionForceKernel(name, platform, cl, context.getSystem());
96
    if (name == CalcCMAPTorsionForceKernel::Name())
97
        return new CommonCalcCMAPTorsionForceKernel(name, platform, cl, context.getSystem());
98
    if (name == CalcCustomTorsionForceKernel::Name())
99
        return new CommonCalcCustomTorsionForceKernel(name, platform, cl, context.getSystem());
100
101
    if (name == CalcNonbondedForceKernel::Name())
        return new OpenCLCalcNonbondedForceKernel(name, platform, cl, context.getSystem());
102
103
    if (name == CalcConstantPotentialForceKernel::Name())
        return new OpenCLCalcConstantPotentialForceKernel(name, platform, cl, context.getSystem());
104
    if (name == CalcCustomNonbondedForceKernel::Name())
105
        return new CommonCalcCustomNonbondedForceKernel(name, platform, cl, context.getSystem());
106
    if (name == CalcGBSAOBCForceKernel::Name())
107
        return new CommonCalcGBSAOBCForceKernel(name, platform, cl);
108
    if (name == CalcCustomGBForceKernel::Name())
109
        return new CommonCalcCustomGBForceKernel(name, platform, cl, context.getSystem());
110
    if (name == CalcCustomExternalForceKernel::Name())
111
        return new CommonCalcCustomExternalForceKernel(name, platform, cl, context.getSystem());
112
    if (name == CalcCustomHbondForceKernel::Name())
113
        return new CommonCalcCustomHbondForceKernel(name, platform, cl, context.getSystem());
114
    if (name == CalcCustomCentroidBondForceKernel::Name())
115
        return new CommonCalcCustomCentroidBondForceKernel(name, platform, cl, context.getSystem());
116
    if (name == CalcCustomCompoundBondForceKernel::Name())
117
        return new CommonCalcCustomCompoundBondForceKernel(name, platform, cl, context.getSystem());
118
119
    if (name == CalcCustomCVForceKernel::Name())
        return new OpenCLCalcCustomCVForceKernel(name, platform, cl);
120
    if (name == CalcATMForceKernel::Name())
121
122
123
        return new OpenCLCalcATMForceKernel(name, platform, cl);
    if (name == CalcCustomCPPForceKernel::Name())
        return new CommonCalcCustomCPPForceKernel(name, platform, context, cl);
124
125
    if (name == CalcOrientationRestraintForceKernel::Name())
        return new CommonCalcOrientationRestraintForceKernel(name, platform, cl);
Peter Eastman's avatar
Peter Eastman committed
126
127
    if (name == CalcPythonForceKernel::Name())
        return new CommonCalcPythonForceKernel(name, platform, context, cl);
128
129
    if (name == CalcRGForceKernel::Name())
        return new CommonCalcRGForceKernel(name, platform, cl);
peastman's avatar
peastman committed
130
    if (name == CalcRMSDForceKernel::Name())
131
        return new CommonCalcRMSDForceKernel(name, platform, cl);
132
    if (name == CalcCustomManyParticleForceKernel::Name())
133
        return new CommonCalcCustomManyParticleForceKernel(name, platform, cl, context.getSystem());
134
    if (name == CalcGayBerneForceKernel::Name())
135
        return new CommonCalcGayBerneForceKernel(name, platform, cl);
136
    if (name == IntegrateVerletStepKernel::Name())
137
        return new CommonIntegrateVerletStepKernel(name, platform, cl);
138
139
    if (name == IntegrateLangevinMiddleStepKernel::Name())
        return new CommonIntegrateLangevinMiddleStepKernel(name, platform, cl);
140
    if (name == IntegrateBrownianStepKernel::Name())
141
        return new CommonIntegrateBrownianStepKernel(name, platform, cl);
142
    if (name == IntegrateVariableVerletStepKernel::Name())
143
        return new CommonIntegrateVariableVerletStepKernel(name, platform, cl);
144
    if (name == IntegrateVariableLangevinStepKernel::Name())
145
        return new CommonIntegrateVariableLangevinStepKernel(name, platform, cl);
146
    if (name == IntegrateCustomStepKernel::Name())
147
        return new CommonIntegrateCustomStepKernel(name, platform, cl);
Peter Eastman's avatar
Peter Eastman committed
148
149
    if (name == IntegrateDPDStepKernel::Name())
        return new CommonIntegrateDPDStepKernel(name, platform, cl);
150
151
    if (name == IntegrateQTBStepKernel::Name())
        return new CommonIntegrateQTBStepKernel(name, platform, cl);
152
    if (name == ApplyAndersenThermostatKernel::Name())
153
        return new CommonApplyAndersenThermostatKernel(name, platform, cl);
154
155
    if (name == IntegrateNoseHooverStepKernel::Name())
        return new CommonIntegrateNoseHooverStepKernel(name, platform, cl);
156
    if (name == ApplyMonteCarloBarostatKernel::Name())
157
        return new CommonApplyMonteCarloBarostatKernel(name, platform, cl);
158
    if (name == RemoveCMMotionKernel::Name())
159
        return new CommonRemoveCMMotionKernel(name, platform, cl);
160
161
    throw OpenMMException((std::string("Tried to create kernel with illegal kernel name '")+name+"'").c_str());
}