CpuPlatform.cpp 6.57 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.               *
 *                                                                            *
9
 * Portions copyright (c) 2013-2014 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
33
34
 * Authors: Peter Eastman                                                     *
 * Contributors:                                                              *
 *                                                                            *
 * Permission is hereby granted, free of charge, to any person obtaining a    *
 * copy of this software and associated documentation files (the "Software"), *
 * to deal in the Software without restriction, including without limitation  *
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,   *
 * and/or sell copies of the Software, and to permit persons to whom the      *
 * Software is furnished to do so, subject to the following conditions:       *
 *                                                                            *
 * The above copyright notice and this permission notice shall be included in *
 * all copies or substantial portions of the Software.                        *
 *                                                                            *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,   *
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL    *
 * THE AUTHORS, CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,    *
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR      *
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE  *
 * USE OR OTHER DEALINGS IN THE SOFTWARE.                                     *
 * -------------------------------------------------------------------------- */

#include "CpuPlatform.h"
#include "CpuKernelFactory.h"
#include "CpuKernels.h"
peastman's avatar
peastman committed
35
36
#include "CpuSETTLE.h"
#include "ReferenceConstraints.h"
37
#include "openmm/internal/hardware.h"
38
#include <sstream>
39
40

using namespace OpenMM;
41
using namespace std;
42

43
44
45
46
47
48
#ifdef OPENMM_CPU_BUILDING_STATIC_LIBRARY
extern "C" void registerCpuPlatform() {
    if (CpuPlatform::isProcessorSupported())
        Platform::registerPlatform(new CpuPlatform());
}
#else
49
extern "C" OPENMM_EXPORT_CPU void registerPlatforms() {
50
51
    // Only register this platform if the CPU supports SSE 4.1.

peastman's avatar
peastman committed
52
53
    if (CpuPlatform::isProcessorSupported())
        Platform::registerPlatform(new CpuPlatform());
54
}
55
#endif
56

57
map<const ContextImpl*, CpuPlatform::PlatformData*> CpuPlatform::contextData;
58

59
60
CpuPlatform::CpuPlatform() {
    CpuKernelFactory* factory = new CpuKernelFactory();
61
    registerKernelFactory(CalcForcesAndEnergyKernel::Name(), factory);
62
63
    registerKernelFactory(CalcPeriodicTorsionForceKernel::Name(), factory);
    registerKernelFactory(CalcRBTorsionForceKernel::Name(), factory);
64
    registerKernelFactory(CalcNonbondedForceKernel::Name(), factory);
65
    registerKernelFactory(CalcCustomNonbondedForceKernel::Name(), factory);
66
    registerKernelFactory(CalcGBSAOBCForceKernel::Name(), factory);
67
    registerKernelFactory(IntegrateLangevinStepKernel::Name(), factory);
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
    platformProperties.push_back(CpuThreads());
    int threads = getNumProcessors();
    char* threadsEnv = getenv("OPENMM_CPU_THREADS");
    if (threadsEnv != NULL)
        stringstream(threadsEnv) >> threads;
    stringstream defaultThreads;
    defaultThreads << threads;
    setPropertyDefaultValue(CpuThreads(), defaultThreads.str());
}

const string& CpuPlatform::getPropertyValue(const Context& context, const string& property) const {
    const ContextImpl& impl = getContextImpl(context);
    const PlatformData& data = getPlatformData(impl);
    map<string, string>::const_iterator value = data.propertyValues.find(property);
    if (value != data.propertyValues.end())
        return value->second;
    return ReferencePlatform::getPropertyValue(context, property);
85
86
87
88
89
90
91
92
93
}

double CpuPlatform::getSpeed() const {
    return 10;
}

bool CpuPlatform::supportsDoublePrecision() const {
    return false;
}
peastman's avatar
peastman committed
94
95
96
97
98
99
100
101
102
103
104
105

bool CpuPlatform::isProcessorSupported() {
    // Make sure the CPU supports SSE 4.1.
    
    int cpuInfo[4];
    cpuid(cpuInfo, 0);
    if (cpuInfo[0] >= 1) {
        cpuid(cpuInfo, 1);
        return ((cpuInfo[2] & ((int) 1 << 19)) != 0);
    }
    return false;
}
106
107
108

void CpuPlatform::contextCreated(ContextImpl& context, const map<string, string>& properties) const {
    ReferencePlatform::contextCreated(context, properties);
109
110
111
112
113
    const string& threadsPropValue = (properties.find(CpuThreads()) == properties.end() ?
            getPropertyDefaultValue(CpuThreads()) : properties.find(CpuThreads())->second);
    int numThreads;
    stringstream(threadsPropValue) >> numThreads;
    PlatformData* data = new PlatformData(context.getSystem().getNumParticles(), numThreads);
114
    contextData[&context] = data;
peastman's avatar
peastman committed
115
116
117
118
119
120
    ReferenceConstraints& constraints = *(ReferenceConstraints*) reinterpret_cast<ReferencePlatform::PlatformData*>(context.getPlatformData())->constraints;
    if (constraints.settle != NULL) {
        CpuSETTLE* parallelSettle = new CpuSETTLE(context.getSystem(), *(ReferenceSETTLEAlgorithm*) constraints.settle, data->threads);
        delete constraints.settle;
        constraints.settle = parallelSettle;
    }
121
122
123
124
125
126
127
128
129
130
131
132
}

void CpuPlatform::contextDestroyed(ContextImpl& context) const {
    PlatformData* data = contextData[&context];
    delete data;
    contextData.erase(&context);
}

CpuPlatform::PlatformData& CpuPlatform::getPlatformData(ContextImpl& context) {
    return *contextData[&context];
}

133
134
135
136
137
138
const CpuPlatform::PlatformData& CpuPlatform::getPlatformData(const ContextImpl& context) {
    return *contextData[&context];
}

CpuPlatform::PlatformData::PlatformData(int numParticles, int numThreads) : posq(4*numParticles), threads(numThreads) {
    numThreads = threads.getNumThreads();
139
140
141
142
    threadForce.resize(numThreads);
    for (int i = 0; i < numThreads; i++)
        threadForce[i].resize(4*numParticles);
    isPeriodic = false;
143
144
145
    stringstream threadsProperty;
    threadsProperty << numThreads;
    propertyValues[CpuThreads()] = threadsProperty.str();
146
}