OpenCLPlatform.cpp 5.77 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
40
/* -------------------------------------------------------------------------- *
 *                                   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;

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

OpenCLPlatform::OpenCLPlatform() {
    OpenCLKernelFactory* factory = new OpenCLKernelFactory();
47
    registerKernelFactory(CalcForcesAndEnergyKernel::Name(), factory);
48
    registerKernelFactory(UpdateStateDataKernel::Name(), factory);
49
    registerKernelFactory(CalcHarmonicBondForceKernel::Name(), factory);
50
    registerKernelFactory(CalcCustomBondForceKernel::Name(), factory);
51
52
    registerKernelFactory(CalcHarmonicAngleForceKernel::Name(), factory);
    registerKernelFactory(CalcPeriodicTorsionForceKernel::Name(), factory);
53
    registerKernelFactory(CalcRBTorsionForceKernel::Name(), factory);
54
    registerKernelFactory(CalcNonbondedForceKernel::Name(), factory);
55
    registerKernelFactory(CalcCustomNonbondedForceKernel::Name(), factory);
56
    registerKernelFactory(CalcGBSAOBCForceKernel::Name(), factory);
57
    registerKernelFactory(CalcCustomGBForceKernel::Name(), factory);
58
    registerKernelFactory(CalcCustomExternalForceKernel::Name(), factory);
59
    registerKernelFactory(IntegrateVerletStepKernel::Name(), factory);
60
    registerKernelFactory(IntegrateLangevinStepKernel::Name(), factory);
61
    registerKernelFactory(IntegrateBrownianStepKernel::Name(), factory);
62
63
    registerKernelFactory(IntegrateVariableVerletStepKernel::Name(), factory);
    registerKernelFactory(IntegrateVariableLangevinStepKernel::Name(), factory);
64
    registerKernelFactory(ApplyAndersenThermostatKernel::Name(), factory);
65
    registerKernelFactory(CalcKineticEnergyKernel::Name(), factory);
66
    registerKernelFactory(RemoveCMMotionKernel::Name(), factory);
67
    platformProperties.push_back(OpenCLDeviceIndex());
68
    setPropertyDefaultValue(OpenCLDeviceIndex(), "");
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
}

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

87
void OpenCLPlatform::contextCreated(ContextImpl& context, const map<string, string>& properties) const {
88
    unsigned int deviceIndex = -1;
89
90
    const string& devicePropValue = (properties.find(OpenCLDeviceIndex()) == properties.end() ?
            getPropertyDefaultValue(OpenCLDeviceIndex()) : properties.find(OpenCLDeviceIndex())->second);
91
92
93
    if (devicePropValue.length() > 0)
        stringstream(devicePropValue) >> deviceIndex;
    int numParticles = context.getSystem().getNumParticles();
94
    context.setPlatformData(new PlatformData(numParticles, deviceIndex));
95
96
97
98
99
100
101
}

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

102
103
OpenCLPlatform::PlatformData::PlatformData(int numParticles, int deviceIndex) : removeCM(false), stepCount(0), computeForceCount(0), time(0.0)  {
    context = new OpenCLContext(numParticles, deviceIndex);
104
    stringstream device;
Peter Eastman's avatar
Peter Eastman committed
105
    device << context->getDeviceIndex();
106
107
    propertyValues[OpenCLPlatform::OpenCLDeviceIndex()] = device.str();
}
108
109
110
111

OpenCLPlatform::PlatformData::~PlatformData() {
    delete context;
}