CpuPlatform.cpp 8.13 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-2016 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/OpenMMException.h"
38
#include "openmm/internal/hardware.h"
39
#include "openmm/internal/vectorize.h"
40
#include <sstream>
41
#include <stdlib.h>
42
43

using namespace OpenMM;
44
using namespace std;
45

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

peastman's avatar
peastman committed
55
56
    if (CpuPlatform::isProcessorSupported())
        Platform::registerPlatform(new CpuPlatform());
57
}
58
#endif
59

60
map<const ContextImpl*, CpuPlatform::PlatformData*> CpuPlatform::contextData;
61

62
CpuPlatform::CpuPlatform() {
Peter Eastman's avatar
Peter Eastman committed
63
    deprecatedPropertyReplacements["CpuThreads"] = CpuThreads();
64
    CpuKernelFactory* factory = new CpuKernelFactory();
65
    registerKernelFactory(CalcForcesAndEnergyKernel::Name(), factory);
peastman's avatar
peastman committed
66
    registerKernelFactory(CalcHarmonicAngleForceKernel::Name(), factory);
67
68
    registerKernelFactory(CalcPeriodicTorsionForceKernel::Name(), factory);
    registerKernelFactory(CalcRBTorsionForceKernel::Name(), factory);
69
    registerKernelFactory(CalcNonbondedForceKernel::Name(), factory);
70
    registerKernelFactory(CalcCustomNonbondedForceKernel::Name(), factory);
71
    registerKernelFactory(CalcCustomManyParticleForceKernel::Name(), factory);
72
    registerKernelFactory(CalcGBSAOBCForceKernel::Name(), factory);
73
    registerKernelFactory(CalcCustomGBForceKernel::Name(), factory);
74
    registerKernelFactory(CalcGayBerneForceKernel::Name(), factory);
75
    registerKernelFactory(IntegrateLangevinStepKernel::Name(), factory);
76
77
78
79
80
81
82
83
84
85
86
87
88
    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);
89
90
91
92
    string propertyName = property;
    if (deprecatedPropertyReplacements.find(property) != deprecatedPropertyReplacements.end())
        propertyName = deprecatedPropertyReplacements.find(property)->second;
    map<string, string>::const_iterator value = data.propertyValues.find(propertyName);
93
94
95
    if (value != data.propertyValues.end())
        return value->second;
    return ReferencePlatform::getPropertyValue(context, property);
96
97
98
99
100
101
102
103
104
}

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

bool CpuPlatform::supportsDoublePrecision() const {
    return false;
}
peastman's avatar
peastman committed
105
106

bool CpuPlatform::isProcessorSupported() {
107
    return isVec4Supported();
peastman's avatar
peastman committed
108
}
109
110
111

void CpuPlatform::contextCreated(ContextImpl& context, const map<string, string>& properties) const {
    ReferencePlatform::contextCreated(context, properties);
112
113
114
115
116
    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);
117
    contextData[&context] = data;
peastman's avatar
peastman committed
118
119
120
121
122
123
    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;
    }
124
125
126
127
128
129
}

void CpuPlatform::contextDestroyed(ContextImpl& context) const {
    PlatformData* data = contextData[&context];
    delete data;
    contextData.erase(&context);
peastman's avatar
peastman committed
130
131
    ReferencePlatform::PlatformData* refPlatformData = reinterpret_cast<ReferencePlatform::PlatformData*>(context.getPlatformData());
    delete refPlatformData;
132
133
134
135
136
137
}

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

138
139
140
141
const CpuPlatform::PlatformData& CpuPlatform::getPlatformData(const ContextImpl& context) {
    return *contextData[&context];
}

142
143
CpuPlatform::PlatformData::PlatformData(int numParticles, int numThreads) : posq(4*numParticles), threads(numThreads),
        neighborList(NULL), cutoff(0.0), paddedCutoff(0.0), anyExclusions(false) {
144
    numThreads = threads.getNumThreads();
145
146
147
148
    threadForce.resize(numThreads);
    for (int i = 0; i < numThreads; i++)
        threadForce[i].resize(4*numParticles);
    isPeriodic = false;
149
150
151
    stringstream threadsProperty;
    threadsProperty << numThreads;
    propertyValues[CpuThreads()] = threadsProperty.str();
152
}
153
154
155
156
157
158
159
160

CpuPlatform::PlatformData::~PlatformData() {
    if (neighborList != NULL)
        delete neighborList;
}

bool isVec8Supported();

161
void CpuPlatform::PlatformData::requestNeighborList(double cutoffDistance, double padding, bool useExclusions, const vector<set<int> >& exclusionList) {
162
163
164
165
166
167
168
169
170
171
172
173
174
175
    if (neighborList == NULL)
        neighborList = new CpuNeighborList(isVec8Supported() ? 8 : 4);
    if (cutoffDistance > cutoff)
        cutoff = cutoffDistance;
    if (cutoffDistance+padding > paddedCutoff)
        paddedCutoff = cutoffDistance+padding;
    if (useExclusions) {
        if (anyExclusions && exclusions != exclusionList)
            throw OpenMMException("All Forces must have identical exclusions");
        else {
            exclusions = exclusionList;
            anyExclusions = true;
        }
    }
peastman's avatar
peastman committed
176
177
    else if (!anyExclusions)
        exclusions = exclusionList;
178
}