CudaPlatform.cpp 6.35 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
/* -------------------------------------------------------------------------- *
 *                                   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:                                                              *
 *                                                                            *
13
14
15
16
 * 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.                                        *
17
 *                                                                            *
18
19
20
21
 * 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.                        *
22
 *                                                                            *
23
24
 * 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/>.      *
25
26
27
28
29
 * -------------------------------------------------------------------------- */

#include "CudaPlatform.h"
#include "CudaKernelFactory.h"
#include "CudaKernels.h"
30
#include "openmm/internal/ContextImpl.h"
31
#include "kernels/gputypes.h"
32
#include "openmm/Context.h"
33
#include "openmm/System.h"
34
#include <sstream>
35

36
using namespace OpenMM;
37
38
39
using std::map;
using std::string;
using std::stringstream;
40

41
extern "C" OPENMM_EXPORT void registerPlatforms() {
Peter Eastman's avatar
Peter Eastman committed
42
43
    if (gpuIsAvailable())
        Platform::registerPlatform(new CudaPlatform());
44
45
}

46
47
CudaPlatform::CudaPlatform() {
    CudaKernelFactory* factory = new CudaKernelFactory();
48
    registerKernelFactory(CalcForcesAndEnergyKernel::Name(), factory);
49
    registerKernelFactory(UpdateStateDataKernel::Name(), factory);
50
    registerKernelFactory(CalcHarmonicBondForceKernel::Name(), factory);
51
    registerKernelFactory(CalcCustomBondForceKernel::Name(), factory);
52
    registerKernelFactory(CalcHarmonicAngleForceKernel::Name(), factory);
53
    registerKernelFactory(CalcCustomAngleForceKernel::Name(), factory);
54
55
56
    registerKernelFactory(CalcPeriodicTorsionForceKernel::Name(), factory);
    registerKernelFactory(CalcRBTorsionForceKernel::Name(), factory);
    registerKernelFactory(CalcNonbondedForceKernel::Name(), factory);
57
    registerKernelFactory(CalcCustomNonbondedForceKernel::Name(), factory);
58
    registerKernelFactory(CalcGBSAOBCForceKernel::Name(), factory);
59
60
    registerKernelFactory(CalcGBVIForceKernel::Name(), factory);
    registerKernelFactory(CalcCustomExternalForceKernel::Name(), factory);
61
    registerKernelFactory(IntegrateVerletStepKernel::Name(), factory);
62
    registerKernelFactory(IntegrateLangevinStepKernel::Name(), factory);
63
    registerKernelFactory(IntegrateBrownianStepKernel::Name(), factory);
64
    registerKernelFactory(IntegrateVariableVerletStepKernel::Name(), factory);
65
    registerKernelFactory(IntegrateVariableLangevinStepKernel::Name(), factory);
66
    registerKernelFactory(ApplyAndersenThermostatKernel::Name(), factory);
67
    registerKernelFactory(CalcKineticEnergyKernel::Name(), factory);
68
    registerKernelFactory(RemoveCMMotionKernel::Name(), factory);
69
    platformProperties.push_back(CudaDevice());
70
    platformProperties.push_back(CudaUseBlockingSync());
71
    setPropertyDefaultValue(CudaDevice(), "0");
72
    setPropertyDefaultValue(CudaUseBlockingSync(), "true");
73
74
75
76
77
78
}

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

79
80
81
82
83
84
85
86
87
88
89
90
const string& CudaPlatform::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 CudaPlatform::setPropertyValue(Context& context, const string& property, const string& value) const {
}

91
void CudaPlatform::contextCreated(ContextImpl& context, const map<string, string>& properties) const {
92
    unsigned int device = 0;
93
94
    const string& devicePropValue = (properties.find(CudaDevice()) == properties.end() ?
            getPropertyDefaultValue(CudaDevice()) : properties.find(CudaDevice())->second);
95
96
    if (devicePropValue.length() > 0)
        stringstream(devicePropValue) >> device;
Peter Eastman's avatar
Peter Eastman committed
97
    int numParticles = context.getSystem().getNumParticles();
98
99
100
    const string& blockingSync = (properties.find(CudaUseBlockingSync()) == properties.end() ?
            getPropertyDefaultValue(CudaUseBlockingSync()) : properties.find(CudaUseBlockingSync())->second);
    _gpuContext* gpu = (_gpuContext*) gpuInit(numParticles, device, blockingSync == "true");
101
    context.setPlatformData(new PlatformData(gpu));
102
103
}

104
void CudaPlatform::contextDestroyed(ContextImpl& context) const {
105
106
107
    PlatformData* data = reinterpret_cast<PlatformData*>(context.getPlatformData());
    gpuShutDown(data->gpu);
    delete data;
108
}
109

110
CudaPlatform::PlatformData::PlatformData(_gpuContext* gpu) : gpu(gpu), removeCM(false), nonbondedMethod(0), customNonbondedMethod(0), hasBonds(false), hasAngles(false),
111
        hasPeriodicTorsions(false), hasRB(false), hasNonbonded(false), hasCustomNonbonded(false), stepCount(0), computeForceCount(0), time(0.0),
112
        ewaldSelfEnergy(0.0) {
113
114
115
    stringstream device;
    device << gpu->device;
    propertyValues[CudaPlatform::CudaDevice()] = device.str();
116
    propertyValues[CudaPlatform::CudaUseBlockingSync()] = (gpu->useBlockingSync ? "true" : "false");
117
}