OpenCLPlatform.cpp 8.07 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
34
35
36
37
38
39
/* -------------------------------------------------------------------------- *
 *                                   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"
#include <sstream>

using namespace OpenMM;
using std::map;
using std::string;
using std::stringstream;
40
using std::vector;
41

42
extern "C" OPENMM_EXPORT void registerPlatforms() {
43
44
45
46
47
    Platform::registerPlatform(new OpenCLPlatform());
}

OpenCLPlatform::OpenCLPlatform() {
    OpenCLKernelFactory* factory = new OpenCLKernelFactory();
48
    registerKernelFactory(CalcForcesAndEnergyKernel::Name(), factory);
49
    registerKernelFactory(UpdateStateDataKernel::Name(), factory);
50
    registerKernelFactory(ApplyConstraintsKernel::Name(), factory);
51
    registerKernelFactory(VirtualSitesKernel::Name(), factory);
52
    registerKernelFactory(CalcHarmonicBondForceKernel::Name(), factory);
53
    registerKernelFactory(CalcCustomBondForceKernel::Name(), factory);
54
    registerKernelFactory(CalcHarmonicAngleForceKernel::Name(), factory);
55
    registerKernelFactory(CalcCustomAngleForceKernel::Name(), factory);
56
    registerKernelFactory(CalcPeriodicTorsionForceKernel::Name(), factory);
57
    registerKernelFactory(CalcRBTorsionForceKernel::Name(), factory);
58
    registerKernelFactory(CalcCMAPTorsionForceKernel::Name(), factory);
59
    registerKernelFactory(CalcCustomTorsionForceKernel::Name(), factory);
60
    registerKernelFactory(CalcNonbondedForceKernel::Name(), factory);
61
    registerKernelFactory(CalcCustomNonbondedForceKernel::Name(), factory);
62
    registerKernelFactory(CalcGBSAOBCForceKernel::Name(), factory);
63
    registerKernelFactory(CalcCustomGBForceKernel::Name(), factory);
64
    registerKernelFactory(CalcCustomExternalForceKernel::Name(), factory);
65
    registerKernelFactory(CalcCustomHbondForceKernel::Name(), factory);
66
    registerKernelFactory(CalcCustomCompoundBondForceKernel::Name(), factory);
67
    registerKernelFactory(IntegrateVerletStepKernel::Name(), factory);
68
    registerKernelFactory(IntegrateLangevinStepKernel::Name(), factory);
69
    registerKernelFactory(IntegrateBrownianStepKernel::Name(), factory);
70
71
    registerKernelFactory(IntegrateVariableVerletStepKernel::Name(), factory);
    registerKernelFactory(IntegrateVariableLangevinStepKernel::Name(), factory);
72
    registerKernelFactory(IntegrateCustomStepKernel::Name(), factory);
73
    registerKernelFactory(ApplyAndersenThermostatKernel::Name(), factory);
74
    registerKernelFactory(ApplyMonteCarloBarostatKernel::Name(), factory);
75
    registerKernelFactory(CalcKineticEnergyKernel::Name(), factory);
76
    registerKernelFactory(RemoveCMMotionKernel::Name(), factory);
77
    platformProperties.push_back(OpenCLDeviceIndex());
78
    platformProperties.push_back(OpenCLPlatformIndex());
79
    setPropertyDefaultValue(OpenCLDeviceIndex(), "");
80
    setPropertyDefaultValue(OpenCLPlatformIndex(), "");
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
}

bool OpenCLPlatform::supportsDoublePrecision() const {
    return false;
}

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 {
}

99
void OpenCLPlatform::contextCreated(ContextImpl& context, const map<string, string>& properties) const {
100
101
    const string& platformPropValue = (properties.find(OpenCLPlatformIndex()) == properties.end() ?
            getPropertyDefaultValue(OpenCLPlatformIndex()) : properties.find(OpenCLPlatformIndex())->second);
102
103
    const string& devicePropValue = (properties.find(OpenCLDeviceIndex()) == properties.end() ?
            getPropertyDefaultValue(OpenCLDeviceIndex()) : properties.find(OpenCLDeviceIndex())->second);
Peter Eastman's avatar
Peter Eastman committed
104
    context.setPlatformData(new PlatformData(context.getSystem(), platformPropValue, devicePropValue));
105
106
107
108
109
110
111
}

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

Peter Eastman's avatar
Peter Eastman committed
112
OpenCLPlatform::PlatformData::PlatformData(const System& system, const string& platformPropValue, const string& deviceIndexProperty) : removeCM(false), stepCount(0), computeForceCount(0), time(0.0)  {
113
114
115
    int platformIndex = 0;
    if (platformPropValue.length() > 0)
        stringstream(platformPropValue) >> platformIndex;
116
117
    vector<string> devices;
    size_t searchPos = 0, nextPos;
118
    while ((nextPos = deviceIndexProperty.find_first_of(", ", searchPos)) != string::npos) {
119
120
121
122
123
124
125
        devices.push_back(deviceIndexProperty.substr(searchPos, nextPos-searchPos));
        searchPos = nextPos+1;
    }
    devices.push_back(deviceIndexProperty.substr(searchPos));
    for (int i = 0; i < (int) devices.size(); i++) {
        if (devices[i].length() > 0) {
            unsigned int deviceIndex;
126
            stringstream(devices[i]) >> deviceIndex;
Peter Eastman's avatar
Peter Eastman committed
127
            contexts.push_back(new OpenCLContext(system, platformIndex, deviceIndex, *this));
128
129
130
        }
    }
    if (contexts.size() == 0)
Peter Eastman's avatar
Peter Eastman committed
131
        contexts.push_back(new OpenCLContext(system, platformIndex, -1, *this));
132
    stringstream device;
133
134
135
136
137
    for (int i = 0; i < (int) contexts.size(); i++) {
        if (i > 0)
            device << ',';
        device << contexts[i]->getDeviceIndex();
    }
138
    propertyValues[OpenCLPlatform::OpenCLDeviceIndex()] = device.str();
139
    propertyValues[OpenCLPlatform::OpenCLPlatformIndex()] = OpenCLExpressionUtilities::intToString(platformIndex);
140
    contextEnergy.resize(contexts.size());
141
}
142
143

OpenCLPlatform::PlatformData::~PlatformData() {
144
145
146
147
148
149
150
    for (int i = 0; i < (int) contexts.size(); i++)
        delete contexts[i];
}

void OpenCLPlatform::PlatformData::initializeContexts(const System& system) {
    for (int i = 0; i < (int) contexts.size(); i++)
        contexts[i]->initialize(system);
151
}
152
153
154
155
156

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