OpenCLPlatform.cpp 11.6 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/* -------------------------------------------------------------------------- *
 *                                   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.               *
 *                                                                            *
 * Portions copyright (c) 2008 Stanford University and the Authors.           *
 * 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/internal/ContextImpl.h"
#include "openmm/Context.h"
#include "openmm/System.h"
34
#include <algorithm>
35
#include <cctype>
36
#include <sstream>
37
38
39
40
#ifdef __APPLE__
#include "sys/sysctl.h"
#endif

41
42

using namespace OpenMM;
43
using namespace std;
44

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

OpenCLPlatform::OpenCLPlatform() {
    OpenCLKernelFactory* factory = new OpenCLKernelFactory();
59
    registerKernelFactory(CalcForcesAndEnergyKernel::Name(), factory);
60
    registerKernelFactory(UpdateStateDataKernel::Name(), factory);
61
    registerKernelFactory(ApplyConstraintsKernel::Name(), factory);
62
    registerKernelFactory(VirtualSitesKernel::Name(), factory);
63
    registerKernelFactory(CalcHarmonicBondForceKernel::Name(), factory);
64
    registerKernelFactory(CalcCustomBondForceKernel::Name(), factory);
65
    registerKernelFactory(CalcHarmonicAngleForceKernel::Name(), factory);
66
    registerKernelFactory(CalcCustomAngleForceKernel::Name(), factory);
67
    registerKernelFactory(CalcPeriodicTorsionForceKernel::Name(), factory);
68
    registerKernelFactory(CalcRBTorsionForceKernel::Name(), factory);
69
    registerKernelFactory(CalcCMAPTorsionForceKernel::Name(), factory);
70
    registerKernelFactory(CalcCustomTorsionForceKernel::Name(), factory);
71
    registerKernelFactory(CalcNonbondedForceKernel::Name(), factory);
72
    registerKernelFactory(CalcCustomNonbondedForceKernel::Name(), factory);
73
    registerKernelFactory(CalcGBSAOBCForceKernel::Name(), factory);
74
    registerKernelFactory(CalcCustomGBForceKernel::Name(), factory);
75
    registerKernelFactory(CalcCustomExternalForceKernel::Name(), factory);
76
    registerKernelFactory(CalcCustomHbondForceKernel::Name(), factory);
77
    registerKernelFactory(CalcCustomCentroidBondForceKernel::Name(), factory);
78
    registerKernelFactory(CalcCustomCompoundBondForceKernel::Name(), factory);
79
    registerKernelFactory(CalcCustomManyParticleForceKernel::Name(), factory);
80
    registerKernelFactory(IntegrateVerletStepKernel::Name(), factory);
81
    registerKernelFactory(IntegrateLangevinStepKernel::Name(), factory);
82
    registerKernelFactory(IntegrateBrownianStepKernel::Name(), factory);
83
84
    registerKernelFactory(IntegrateVariableVerletStepKernel::Name(), factory);
    registerKernelFactory(IntegrateVariableLangevinStepKernel::Name(), factory);
85
    registerKernelFactory(IntegrateCustomStepKernel::Name(), factory);
86
    registerKernelFactory(ApplyAndersenThermostatKernel::Name(), factory);
87
    registerKernelFactory(ApplyMonteCarloBarostatKernel::Name(), factory);
88
    registerKernelFactory(RemoveCMMotionKernel::Name(), factory);
89
    platformProperties.push_back(OpenCLDeviceIndex());
90
    platformProperties.push_back(OpenCLDeviceName());
91
    platformProperties.push_back(OpenCLPlatformIndex());
92
    platformProperties.push_back(OpenCLPlatformName());
93
    platformProperties.push_back(OpenCLPrecision());
94
    platformProperties.push_back(OpenCLUseCpuPme());
95
    setPropertyDefaultValue(OpenCLDeviceIndex(), "");
96
    setPropertyDefaultValue(OpenCLDeviceName(), "");
97
    setPropertyDefaultValue(OpenCLPlatformIndex(), "");
98
    setPropertyDefaultValue(OpenCLPlatformName(), "");
99
    setPropertyDefaultValue(OpenCLPrecision(), "single");
100
    setPropertyDefaultValue(OpenCLUseCpuPme(), "false");
101
102
}

103
104
105
106
double OpenCLPlatform::getSpeed() const {
    return 50;
}

107
bool OpenCLPlatform::supportsDoublePrecision() const {
Peter Eastman's avatar
Peter Eastman committed
108
    return true;
109
110
}

111
112
bool OpenCLPlatform::isPlatformSupported() {
    // Return false for OpenCL implementations that are known
113
    // to be buggy (Apple OS X prior to 10.10).
114
115
116
117
118
119
120
121
122
123
124
125

#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;

126
127
    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
128
129
        // contained a number of serious bugs in the Apple OpenCL libraries.
        // (See https://github.com/SimTk/openmm/issues/395 for example.)
130
131
132
133
134
135
        return false;
#endif

    return true;
}

136
137
138
139
140
141
142
143
144
145
146
147
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());
    map<string, string>::const_iterator value = data->propertyValues.find(property);
    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 {
}

148
void OpenCLPlatform::contextCreated(ContextImpl& context, const map<string, string>& properties) const {
149
150
    const string& platformPropValue = (properties.find(OpenCLPlatformIndex()) == properties.end() ?
            getPropertyDefaultValue(OpenCLPlatformIndex()) : properties.find(OpenCLPlatformIndex())->second);
151
152
    const string& devicePropValue = (properties.find(OpenCLDeviceIndex()) == properties.end() ?
            getPropertyDefaultValue(OpenCLDeviceIndex()) : properties.find(OpenCLDeviceIndex())->second);
153
154
    string precisionPropValue = (properties.find(OpenCLPrecision()) == properties.end() ?
            getPropertyDefaultValue(OpenCLPrecision()) : properties.find(OpenCLPrecision())->second);
155
156
157
158
159
160
161
162
163
    string cpuPmePropValue = (properties.find(OpenCLUseCpuPme()) == properties.end() ?
            getPropertyDefaultValue(OpenCLUseCpuPme()) : properties.find(OpenCLUseCpuPme())->second);
    transform(precisionPropValue.begin(), precisionPropValue.end(), precisionPropValue.begin(), ::tolower);
    transform(cpuPmePropValue.begin(), cpuPmePropValue.end(), cpuPmePropValue.begin(), ::tolower);
    vector<string> pmeKernelName;
    pmeKernelName.push_back(CalcPmeReciprocalForceKernel::Name());
    if (!supportsKernels(pmeKernelName))
        cpuPmePropValue = "false";
    context.setPlatformData(new PlatformData(context.getSystem(), platformPropValue, devicePropValue, precisionPropValue, cpuPmePropValue));
164
165
166
167
168
169
170
}

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

171
OpenCLPlatform::PlatformData::PlatformData(const System& system, const string& platformPropValue, const string& deviceIndexProperty,
172
        const string& precisionProperty, const string& cpuPmeProperty) : removeCM(false), stepCount(0), computeForceCount(0), time(0.0), hasInitializedContexts(false)  {
Robert McGibbon's avatar
Robert McGibbon committed
173
    int platformIndex = -1;
174
175
    if (platformPropValue.length() > 0)
        stringstream(platformPropValue) >> platformIndex;
176
177
    vector<string> devices;
    size_t searchPos = 0, nextPos;
178
    while ((nextPos = deviceIndexProperty.find_first_of(", ", searchPos)) != string::npos) {
179
180
181
182
        devices.push_back(deviceIndexProperty.substr(searchPos, nextPos-searchPos));
        searchPos = nextPos+1;
    }
    devices.push_back(deviceIndexProperty.substr(searchPos));
183
184
185
    try {
        for (int i = 0; i < (int) devices.size(); i++) {
            if (devices[i].length() > 0) {
peastman's avatar
peastman committed
186
                int deviceIndex;
187
188
189
                stringstream(devices[i]) >> deviceIndex;
                contexts.push_back(new OpenCLContext(system, platformIndex, deviceIndex, precisionProperty, *this));
            }
190
        }
191
192
193
194
195
196
197
198
199
        if (contexts.size() == 0)
            contexts.push_back(new OpenCLContext(system, platformIndex, -1, precisionProperty, *this));
    }
    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;
200
    }
201
    stringstream deviceIndex, deviceName;
202
    for (int i = 0; i < (int) contexts.size(); i++) {
203
204
205
206
207
208
        if (i > 0) {
            deviceIndex << ',';
            deviceName << ',';
        }
        deviceIndex << contexts[i]->getDeviceIndex();
        deviceName << contexts[i]->getDevice().getInfo<CL_DEVICE_NAME>();
209
    }
Robert McGibbon's avatar
Robert McGibbon committed
210
211
    platformIndex = contexts[0]->getPlatformIndex();

212
    useCpuPme = (cpuPmeProperty == "true" && !contexts[0]->getUseDoublePrecision());
213
214
    propertyValues[OpenCLPlatform::OpenCLDeviceIndex()] = deviceIndex.str();
    propertyValues[OpenCLPlatform::OpenCLDeviceName()] = deviceName.str();
215
    propertyValues[OpenCLPlatform::OpenCLPlatformIndex()] = contexts[0]->intToString(platformIndex);
216
217
218
    std::vector<cl::Platform> platforms;
    cl::Platform::get(&platforms);
    propertyValues[OpenCLPlatform::OpenCLPlatformName()] = platforms[platformIndex].getInfo<CL_PLATFORM_NAME>();
219
    propertyValues[OpenCLPlatform::OpenCLPrecision()] = precisionProperty;
220
    propertyValues[OpenCLPlatform::OpenCLUseCpuPme()] = useCpuPme ? "true" : "false";
221
    contextEnergy.resize(contexts.size());
222
}
223
224

OpenCLPlatform::PlatformData::~PlatformData() {
225
226
227
228
229
    for (int i = 0; i < (int) contexts.size(); i++)
        delete contexts[i];
}

void OpenCLPlatform::PlatformData::initializeContexts(const System& system) {
230
231
    if (hasInitializedContexts)
        return;
232
    for (int i = 0; i < (int) contexts.size(); i++)
233
        contexts[i]->initialize();
234
    hasInitializedContexts = true;
235
}
236
237
238
239
240

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