OpenCLPlatform.cpp 15.5 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.               *
 *                                                                            *
Peter Eastman's avatar
Peter Eastman committed
9
 * Portions copyright (c) 2008-2025 Stanford University and the Authors.      *
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
 * 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 "OpenCLContext.h"
#include "OpenCLPlatform.h"
#include "OpenCLKernelFactory.h"
#include "OpenCLKernels.h"
#include "openmm/Context.h"
#include "openmm/System.h"
33
34
#include "openmm/internal/ContextImpl.h"
#include "openmm/internal/hardware.h"
35
#include <algorithm>
36
#include <cctype>
37
#include <sstream>
38
39
40
41
#ifdef __APPLE__
#include "sys/sysctl.h"
#endif

42
43

using namespace OpenMM;
44
using namespace std;
45

46
#ifdef OPENMM_COMMON_BUILDING_STATIC_LIBRARY
47
extern "C" void registerOpenCLPlatform() {
48
49
    if (OpenCLPlatform::isPlatformSupported())
        Platform::registerPlatform(new OpenCLPlatform());
50
51
}
#else
52
extern "C" OPENMM_EXPORT_COMMON void registerPlatforms() {
53
54
    if (OpenCLPlatform::isPlatformSupported())
        Platform::registerPlatform(new OpenCLPlatform());
55
}
56
#endif
57
58

OpenCLPlatform::OpenCLPlatform() {
Peter Eastman's avatar
Peter Eastman committed
59
60
61
62
63
    deprecatedPropertyReplacements["OpenCLDeviceIndex"] = OpenCLDeviceIndex();
    deprecatedPropertyReplacements["OpenCLDeviceName"] = OpenCLDeviceName();
    deprecatedPropertyReplacements["OpenCLPrecision"] = OpenCLPrecision();
    deprecatedPropertyReplacements["OpenCLUseCpuPme"] = OpenCLUseCpuPme();
    deprecatedPropertyReplacements["OpenCLDisablePmeStream"] = OpenCLDisablePmeStream();
64
    OpenCLKernelFactory* factory = new OpenCLKernelFactory();
65
    registerKernelFactory(CalcForcesAndEnergyKernel::Name(), factory);
66
    registerKernelFactory(UpdateStateDataKernel::Name(), factory);
67
    registerKernelFactory(ApplyConstraintsKernel::Name(), factory);
68
    registerKernelFactory(VirtualSitesKernel::Name(), factory);
69
    registerKernelFactory(CalcHarmonicBondForceKernel::Name(), factory);
70
    registerKernelFactory(CalcCustomBondForceKernel::Name(), factory);
71
    registerKernelFactory(CalcHarmonicAngleForceKernel::Name(), factory);
72
    registerKernelFactory(CalcCustomAngleForceKernel::Name(), factory);
73
    registerKernelFactory(CalcPeriodicTorsionForceKernel::Name(), factory);
74
    registerKernelFactory(CalcRBTorsionForceKernel::Name(), factory);
75
    registerKernelFactory(CalcCMAPTorsionForceKernel::Name(), factory);
76
    registerKernelFactory(CalcCustomTorsionForceKernel::Name(), factory);
77
    registerKernelFactory(CalcNonbondedForceKernel::Name(), factory);
78
    registerKernelFactory(CalcConstantPotentialForceKernel::Name(), factory);
79
    registerKernelFactory(CalcCustomNonbondedForceKernel::Name(), factory);
80
    registerKernelFactory(CalcGBSAOBCForceKernel::Name(), factory);
81
    registerKernelFactory(CalcCustomGBForceKernel::Name(), factory);
82
    registerKernelFactory(CalcCustomExternalForceKernel::Name(), factory);
83
    registerKernelFactory(CalcCustomHbondForceKernel::Name(), factory);
84
    registerKernelFactory(CalcCustomCentroidBondForceKernel::Name(), factory);
85
    registerKernelFactory(CalcCustomCompoundBondForceKernel::Name(), factory);
86
    registerKernelFactory(CalcCustomCPPForceKernel::Name(), factory);
87
    registerKernelFactory(CalcCustomCVForceKernel::Name(), factory);
88
    registerKernelFactory(CalcATMForceKernel::Name(), factory);
89
    registerKernelFactory(CalcOrientationRestraintForceKernel::Name(), factory);
90
    registerKernelFactory(CalcRGForceKernel::Name(), factory);
peastman's avatar
peastman committed
91
    registerKernelFactory(CalcRMSDForceKernel::Name(), factory);
92
    registerKernelFactory(CalcCustomManyParticleForceKernel::Name(), factory);
93
    registerKernelFactory(CalcGayBerneForceKernel::Name(), factory);
94
    registerKernelFactory(IntegrateVerletStepKernel::Name(), factory);
95
    registerKernelFactory(IntegrateNoseHooverStepKernel::Name(), factory);
96
    registerKernelFactory(IntegrateLangevinMiddleStepKernel::Name(), factory);
97
    registerKernelFactory(IntegrateBrownianStepKernel::Name(), factory);
98
99
    registerKernelFactory(IntegrateVariableVerletStepKernel::Name(), factory);
    registerKernelFactory(IntegrateVariableLangevinStepKernel::Name(), factory);
100
    registerKernelFactory(IntegrateCustomStepKernel::Name(), factory);
Peter Eastman's avatar
Peter Eastman committed
101
    registerKernelFactory(IntegrateDPDStepKernel::Name(), factory);
102
    registerKernelFactory(IntegrateQTBStepKernel::Name(), factory);
103
    registerKernelFactory(ApplyAndersenThermostatKernel::Name(), factory);
104
    registerKernelFactory(ApplyMonteCarloBarostatKernel::Name(), factory);
105
    registerKernelFactory(RemoveCMMotionKernel::Name(), factory);
106
    platformProperties.push_back(OpenCLDeviceIndex());
107
    platformProperties.push_back(OpenCLDeviceName());
108
    platformProperties.push_back(OpenCLPlatformIndex());
109
    platformProperties.push_back(OpenCLPlatformName());
110
    platformProperties.push_back(OpenCLPrecision());
111
    platformProperties.push_back(OpenCLUseCpuPme());
112
    platformProperties.push_back(OpenCLDisablePmeStream());
113
    setPropertyDefaultValue(OpenCLDeviceIndex(), "");
114
    setPropertyDefaultValue(OpenCLDeviceName(), "");
115
    setPropertyDefaultValue(OpenCLPlatformIndex(), "");
116
    setPropertyDefaultValue(OpenCLPlatformName(), "");
117
    setPropertyDefaultValue(OpenCLPrecision(), "single");
118
    setPropertyDefaultValue(OpenCLUseCpuPme(), "false");
119
    setPropertyDefaultValue(OpenCLDisablePmeStream(), "false");
120
121
}

122
123
124
125
double OpenCLPlatform::getSpeed() const {
    return 50;
}

126
bool OpenCLPlatform::supportsDoublePrecision() const {
Peter Eastman's avatar
Peter Eastman committed
127
    return true;
128
129
}

130
131
bool OpenCLPlatform::isPlatformSupported() {
    // Return false for OpenCL implementations that are known
132
    // to be buggy (Apple OS X prior to 10.10).
133
134
135
136
137
138
139
140
141
142
143
144

#ifdef __APPLE__
    char str[256];
    size_t size = sizeof(str);
    int ret = sysctlbyname("kern.osrelease", str, &size, NULL, 0);
    if (ret != 0)
        return false;

    int major, minor, micro;
    if (sscanf(str, "%d.%d.%d", &major, &minor, &micro) != 3)
        return false;

145
146
    if (major < 14 || (major == 14 && minor < 3))
        // 14.3.0 is the darwin release corresponding to OS X 10.10.3. Versions prior to that
147
        // contained a number of serious bugs in the Apple OpenCL libraries.
Evan Pretti's avatar
Evan Pretti committed
148
        // (See https://github.com/openmm/openmm/issues/395 for example.)
149
150
151
        return false;
#endif

152
153
154
    // Make sure at least one OpenCL implementation is installed.

    std::vector<cl::Platform> platforms;
155
156
157
158
159
160
    try {
        cl::Platform::get(&platforms);
        if (platforms.size() == 0)
            return false;
    }
    catch (...) {
161
        return false;
162
    }
163
164
165
    return true;
}

166
167
168
const string& OpenCLPlatform::getPropertyValue(const Context& context, const string& property) const {
    const ContextImpl& impl = getContextImpl(context);
    const PlatformData* data = reinterpret_cast<const PlatformData*>(impl.getPlatformData());
169
170
171
172
    string propertyName = property;
    if (deprecatedPropertyReplacements.find(property) != deprecatedPropertyReplacements.end())
        propertyName = deprecatedPropertyReplacements.find(property)->second;
    map<string, string>::const_iterator value = data->propertyValues.find(propertyName);
173
174
175
176
177
178
179
180
    if (value != data->propertyValues.end())
        return value->second;
    return Platform::getPropertyValue(context, property);
}

void OpenCLPlatform::setPropertyValue(Context& context, const string& property, const string& value) const {
}

181
void OpenCLPlatform::contextCreated(ContextImpl& context, const map<string, string>& properties) const {
182
183
    const string& platformPropValue = (properties.find(OpenCLPlatformIndex()) == properties.end() ?
            getPropertyDefaultValue(OpenCLPlatformIndex()) : properties.find(OpenCLPlatformIndex())->second);
184
185
    const string& devicePropValue = (properties.find(OpenCLDeviceIndex()) == properties.end() ?
            getPropertyDefaultValue(OpenCLDeviceIndex()) : properties.find(OpenCLDeviceIndex())->second);
186
187
    string precisionPropValue = (properties.find(OpenCLPrecision()) == properties.end() ?
            getPropertyDefaultValue(OpenCLPrecision()) : properties.find(OpenCLPrecision())->second);
188
189
    string cpuPmePropValue = (properties.find(OpenCLUseCpuPme()) == properties.end() ?
            getPropertyDefaultValue(OpenCLUseCpuPme()) : properties.find(OpenCLUseCpuPme())->second);
190
191
    string pmeStreamPropValue = (properties.find(OpenCLDisablePmeStream()) == properties.end() ?
            getPropertyDefaultValue(OpenCLDisablePmeStream()) : properties.find(OpenCLDisablePmeStream())->second);
192
193
    transform(precisionPropValue.begin(), precisionPropValue.end(), precisionPropValue.begin(), ::tolower);
    transform(cpuPmePropValue.begin(), cpuPmePropValue.end(), cpuPmePropValue.begin(), ::tolower);
194
    transform(pmeStreamPropValue.begin(), pmeStreamPropValue.end(), pmeStreamPropValue.begin(), ::tolower);
195
196
197
198
    vector<string> pmeKernelName;
    pmeKernelName.push_back(CalcPmeReciprocalForceKernel::Name());
    if (!supportsKernels(pmeKernelName))
        cpuPmePropValue = "false";
199
200
201
202
    int threads = getNumProcessors();
    char* threadsEnv = getenv("OPENMM_CPU_THREADS");
    if (threadsEnv != NULL)
        stringstream(threadsEnv) >> threads;
203
    context.setPlatformData(new PlatformData(context.getSystem(), &context, platformPropValue, devicePropValue, precisionPropValue, cpuPmePropValue,
204
205
206
207
208
209
210
211
212
213
214
            pmeStreamPropValue, threads, NULL));
}

void OpenCLPlatform::linkedContextCreated(ContextImpl& context, ContextImpl& originalContext) const {
    Platform& platform = originalContext.getPlatform();
    string platformPropValue = platform.getPropertyValue(originalContext.getOwner(), OpenCLPlatformIndex());
    string devicePropValue = platform.getPropertyValue(originalContext.getOwner(), OpenCLDeviceIndex());
    string precisionPropValue = platform.getPropertyValue(originalContext.getOwner(), OpenCLPrecision());
    string cpuPmePropValue = platform.getPropertyValue(originalContext.getOwner(), OpenCLUseCpuPme());
    string pmeStreamPropValue = platform.getPropertyValue(originalContext.getOwner(), OpenCLDisablePmeStream());
    int threads = reinterpret_cast<PlatformData*>(originalContext.getPlatformData())->threads.getNumThreads();
215
    context.setPlatformData(new PlatformData(context.getSystem(), &context, platformPropValue, devicePropValue, precisionPropValue, cpuPmePropValue,
216
            pmeStreamPropValue, threads, &originalContext));
217
218
219
220
221
222
223
}

void OpenCLPlatform::contextDestroyed(ContextImpl& context) const {
    PlatformData* data = reinterpret_cast<PlatformData*>(context.getPlatformData());
    delete data;
}

224
OpenCLPlatform::PlatformData::PlatformData(const System& system, ContextImpl* context, const string& platformPropValue, const string& deviceIndexProperty,
225
        const string& precisionProperty, const string& cpuPmeProperty, const string& pmeStreamProperty, int numThreads, ContextImpl* originalContext) :
226
            context(context), removeCM(false), stepCount(0), computeForceCount(0), time(0.0), hasInitializedContexts(false), threads(numThreads)  {
Robert McGibbon's avatar
Robert McGibbon committed
227
    int platformIndex = -1;
228
229
    if (platformPropValue.length() > 0)
        stringstream(platformPropValue) >> platformIndex;
230
231
    vector<string> devices;
    size_t searchPos = 0, nextPos;
232
    while ((nextPos = deviceIndexProperty.find_first_of(", ", searchPos)) != string::npos) {
233
234
235
236
        devices.push_back(deviceIndexProperty.substr(searchPos, nextPos-searchPos));
        searchPos = nextPos+1;
    }
    devices.push_back(deviceIndexProperty.substr(searchPos));
237
238
239
    PlatformData* originalData = NULL;
    if (originalContext != NULL)
        originalData = reinterpret_cast<PlatformData*>(originalContext->getPlatformData());
240
241
242
    try {
        for (int i = 0; i < (int) devices.size(); i++) {
            if (devices[i].length() > 0) {
peastman's avatar
peastman committed
243
                int deviceIndex;
244
                stringstream(devices[i]) >> deviceIndex;
245
                contexts.push_back(new OpenCLContext(system, platformIndex, deviceIndex, precisionProperty, *this, (originalData == NULL ? NULL : originalData->contexts[i])));
246
            }
247
        }
248
        if (contexts.size() == 0)
249
            contexts.push_back(new OpenCLContext(system, platformIndex, -1, precisionProperty, *this, (originalData == NULL ? NULL : originalData->contexts[0])));
250
251
252
253
254
255
256
    }
    catch (...) {
        // If an exception was thrown, do our best to clean up memory.
        
        for (int i = 0; i < (int) contexts.size(); i++)
            delete contexts[i];
        throw;
257
    }
258
    stringstream deviceIndex, deviceName;
259
    for (int i = 0; i < (int) contexts.size(); i++) {
260
261
262
263
264
265
        if (i > 0) {
            deviceIndex << ',';
            deviceName << ',';
        }
        deviceIndex << contexts[i]->getDeviceIndex();
        deviceName << contexts[i]->getDevice().getInfo<CL_DEVICE_NAME>();
266
    }
Robert McGibbon's avatar
Robert McGibbon committed
267
268
    platformIndex = contexts[0]->getPlatformIndex();

269
    useCpuPme = (cpuPmeProperty == "true" && !contexts[0]->getUseDoublePrecision());
270
    disablePmeStream = (pmeStreamProperty == "true");
271
272
    propertyValues[OpenCLPlatform::OpenCLDeviceIndex()] = deviceIndex.str();
    propertyValues[OpenCLPlatform::OpenCLDeviceName()] = deviceName.str();
273
    propertyValues[OpenCLPlatform::OpenCLPlatformIndex()] = contexts[0]->intToString(platformIndex);
274
275
276
    std::vector<cl::Platform> platforms;
    cl::Platform::get(&platforms);
    propertyValues[OpenCLPlatform::OpenCLPlatformName()] = platforms[platformIndex].getInfo<CL_PLATFORM_NAME>();
277
    propertyValues[OpenCLPlatform::OpenCLPrecision()] = precisionProperty;
278
    propertyValues[OpenCLPlatform::OpenCLUseCpuPme()] = useCpuPme ? "true" : "false";
279
    propertyValues[OpenCLPlatform::OpenCLDisablePmeStream()] = disablePmeStream ? "true" : "false";
280
    contextEnergy.resize(contexts.size());
281
}
282
283

OpenCLPlatform::PlatformData::~PlatformData() {
284
285
286
287
288
    for (int i = 0; i < (int) contexts.size(); i++)
        delete contexts[i];
}

void OpenCLPlatform::PlatformData::initializeContexts(const System& system) {
289
290
    if (hasInitializedContexts)
        return;
291
    for (int i = 0; i < (int) contexts.size(); i++)
292
        contexts[i]->initialize();
293
    hasInitializedContexts = true;
294
}
295
296
297
298
299

void OpenCLPlatform::PlatformData::syncContexts() {
    for (int i = 0; i < (int) contexts.size(); i++)
        contexts[i]->getWorkThread().flush();
}