OpenCLKernelFactory.cpp 9.73 KB
Newer Older
1
2
3
4
5
6
7
8
/* -------------------------------------------------------------------------- *
 *                                   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.               *
 *                                                                            *
9
 * Portions copyright (c) 2008-2024 Stanford University and the Authors.      *
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
 * 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"
28
#include "OpenCLParallelKernels.h"
29
#include "openmm/common/CommonKernels.h"
Peter Eastman's avatar
Peter Eastman committed
30
#include "openmm/common/CommonParallelKernels.h"
31
32
33
34
35
36
37
#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
38
    OpenCLContext& cl = *data.contexts[0];
Peter Eastman's avatar
Bug fix  
Peter Eastman committed
39
    if (data.contexts.size() > 1) {
40
41
42
43
44
        // 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
45
            return new CommonParallelCalcHarmonicBondForceKernel(name, platform, cl, context.getSystem());
46
        if (name == CalcCustomBondForceKernel::Name())
Peter Eastman's avatar
Peter Eastman committed
47
            return new CommonParallelCalcCustomBondForceKernel(name, platform, cl, context.getSystem());
48
        if (name == CalcHarmonicAngleForceKernel::Name())
Peter Eastman's avatar
Peter Eastman committed
49
            return new CommonParallelCalcHarmonicAngleForceKernel(name, platform, cl, context.getSystem());
50
        if (name == CalcCustomAngleForceKernel::Name())
Peter Eastman's avatar
Peter Eastman committed
51
            return new CommonParallelCalcCustomAngleForceKernel(name, platform, cl, context.getSystem());
52
        if (name == CalcPeriodicTorsionForceKernel::Name())
Peter Eastman's avatar
Peter Eastman committed
53
            return new CommonParallelCalcPeriodicTorsionForceKernel(name, platform, cl, context.getSystem());
54
        if (name == CalcRBTorsionForceKernel::Name())
Peter Eastman's avatar
Peter Eastman committed
55
            return new CommonParallelCalcRBTorsionForceKernel(name, platform, cl, context.getSystem());
56
        if (name == CalcCMAPTorsionForceKernel::Name())
Peter Eastman's avatar
Peter Eastman committed
57
            return new CommonParallelCalcCMAPTorsionForceKernel(name, platform, cl, context.getSystem());
58
        if (name == CalcCustomTorsionForceKernel::Name())
Peter Eastman's avatar
Peter Eastman committed
59
            return new CommonParallelCalcCustomTorsionForceKernel(name, platform, cl, context.getSystem());
60
61
        if (name == CalcNonbondedForceKernel::Name())
            return new OpenCLParallelCalcNonbondedForceKernel(name, platform, data, context.getSystem());
62
        if (name == CalcCustomNonbondedForceKernel::Name())
Peter Eastman's avatar
Peter Eastman committed
63
            return new CommonParallelCalcCustomNonbondedForceKernel(name, platform, cl, context.getSystem());
64
        if (name == CalcCustomExternalForceKernel::Name())
Peter Eastman's avatar
Peter Eastman committed
65
            return new CommonParallelCalcCustomExternalForceKernel(name, platform, cl, context.getSystem());
66
        if (name == CalcCustomHbondForceKernel::Name())
Peter Eastman's avatar
Peter Eastman committed
67
            return new CommonParallelCalcCustomHbondForceKernel(name, platform, cl, context.getSystem());
68
        if (name == CalcCustomCompoundBondForceKernel::Name())
Peter Eastman's avatar
Peter Eastman committed
69
            return new CommonParallelCalcCustomCompoundBondForceKernel(name, platform, cl, context.getSystem());
70
    }
71
    if (name == CalcForcesAndEnergyKernel::Name())
72
        return new OpenCLCalcForcesAndEnergyKernel(name, platform, cl);
73
    if (name == UpdateStateDataKernel::Name())
74
        return new CommonUpdateStateDataKernel(name, platform, cl);
75
    if (name == ApplyConstraintsKernel::Name())
76
        return new CommonApplyConstraintsKernel(name, platform, cl);
77
    if (name == VirtualSitesKernel::Name())
78
        return new CommonVirtualSitesKernel(name, platform, cl);
79
    if (name == CalcHarmonicBondForceKernel::Name())
80
        return new CommonCalcHarmonicBondForceKernel(name, platform, cl, context.getSystem());
81
    if (name == CalcCustomBondForceKernel::Name())
82
        return new CommonCalcCustomBondForceKernel(name, platform, cl, context.getSystem());
83
    if (name == CalcHarmonicAngleForceKernel::Name())
84
        return new CommonCalcHarmonicAngleForceKernel(name, platform, cl, context.getSystem());
85
    if (name == CalcCustomAngleForceKernel::Name())
86
        return new CommonCalcCustomAngleForceKernel(name, platform, cl, context.getSystem());
87
    if (name == CalcPeriodicTorsionForceKernel::Name())
88
        return new CommonCalcPeriodicTorsionForceKernel(name, platform, cl, context.getSystem());
89
    if (name == CalcRBTorsionForceKernel::Name())
90
        return new CommonCalcRBTorsionForceKernel(name, platform, cl, context.getSystem());
91
    if (name == CalcCMAPTorsionForceKernel::Name())
92
        return new CommonCalcCMAPTorsionForceKernel(name, platform, cl, context.getSystem());
93
    if (name == CalcCustomTorsionForceKernel::Name())
94
        return new CommonCalcCustomTorsionForceKernel(name, platform, cl, context.getSystem());
95
96
    if (name == CalcNonbondedForceKernel::Name())
        return new OpenCLCalcNonbondedForceKernel(name, platform, cl, context.getSystem());
97
    if (name == CalcCustomNonbondedForceKernel::Name())
98
        return new CommonCalcCustomNonbondedForceKernel(name, platform, cl, context.getSystem());
99
    if (name == CalcGBSAOBCForceKernel::Name())
100
        return new CommonCalcGBSAOBCForceKernel(name, platform, cl);
101
    if (name == CalcCustomGBForceKernel::Name())
102
        return new CommonCalcCustomGBForceKernel(name, platform, cl, context.getSystem());
103
    if (name == CalcCustomExternalForceKernel::Name())
104
        return new CommonCalcCustomExternalForceKernel(name, platform, cl, context.getSystem());
105
    if (name == CalcCustomHbondForceKernel::Name())
106
        return new CommonCalcCustomHbondForceKernel(name, platform, cl, context.getSystem());
107
    if (name == CalcCustomCentroidBondForceKernel::Name())
108
        return new CommonCalcCustomCentroidBondForceKernel(name, platform, cl, context.getSystem());
109
    if (name == CalcCustomCompoundBondForceKernel::Name())
110
        return new CommonCalcCustomCompoundBondForceKernel(name, platform, cl, context.getSystem());
111
112
    if (name == CalcCustomCVForceKernel::Name())
        return new OpenCLCalcCustomCVForceKernel(name, platform, cl);
113
    if (name == CalcATMForceKernel::Name())
114
115
116
        return new OpenCLCalcATMForceKernel(name, platform, cl);
    if (name == CalcCustomCPPForceKernel::Name())
        return new CommonCalcCustomCPPForceKernel(name, platform, context, cl);
peastman's avatar
peastman committed
117
    if (name == CalcRMSDForceKernel::Name())
118
        return new CommonCalcRMSDForceKernel(name, platform, cl);
119
    if (name == CalcCustomManyParticleForceKernel::Name())
120
        return new CommonCalcCustomManyParticleForceKernel(name, platform, cl, context.getSystem());
121
    if (name == CalcGayBerneForceKernel::Name())
122
        return new CommonCalcGayBerneForceKernel(name, platform, cl);
123
    if (name == IntegrateVerletStepKernel::Name())
124
        return new CommonIntegrateVerletStepKernel(name, platform, cl);
125
126
    if (name == IntegrateLangevinMiddleStepKernel::Name())
        return new CommonIntegrateLangevinMiddleStepKernel(name, platform, cl);
127
    if (name == IntegrateBrownianStepKernel::Name())
128
        return new CommonIntegrateBrownianStepKernel(name, platform, cl);
129
    if (name == IntegrateVariableVerletStepKernel::Name())
130
        return new CommonIntegrateVariableVerletStepKernel(name, platform, cl);
131
    if (name == IntegrateVariableLangevinStepKernel::Name())
132
        return new CommonIntegrateVariableLangevinStepKernel(name, platform, cl);
133
    if (name == IntegrateCustomStepKernel::Name())
134
        return new CommonIntegrateCustomStepKernel(name, platform, cl);
135
    if (name == ApplyAndersenThermostatKernel::Name())
136
        return new CommonApplyAndersenThermostatKernel(name, platform, cl);
137
138
    if (name == IntegrateNoseHooverStepKernel::Name())
        return new CommonIntegrateNoseHooverStepKernel(name, platform, cl);
139
    if (name == ApplyMonteCarloBarostatKernel::Name())
140
        return new CommonApplyMonteCarloBarostatKernel(name, platform, cl);
141
    if (name == RemoveCMMotionKernel::Name())
142
        return new CommonRemoveCMMotionKernel(name, platform, cl);
143
144
    throw OpenMMException((std::string("Tried to create kernel with illegal kernel name '")+name+"'").c_str());
}