CpuPlatform.cpp 9.31 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-2022 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 <algorithm>
41
#include <sstream>
42
#include <stdlib.h>
43
44

using namespace OpenMM;
45
using namespace std;
46

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

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

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

63
CpuPlatform::CpuPlatform() {
Peter Eastman's avatar
Peter Eastman committed
64
    deprecatedPropertyReplacements["CpuThreads"] = CpuThreads();
65
    CpuKernelFactory* factory = new CpuKernelFactory();
66
    registerKernelFactory(CalcForcesAndEnergyKernel::Name(), factory);
peastman's avatar
peastman committed
67
    registerKernelFactory(CalcHarmonicAngleForceKernel::Name(), factory);
68
69
    registerKernelFactory(CalcPeriodicTorsionForceKernel::Name(), factory);
    registerKernelFactory(CalcRBTorsionForceKernel::Name(), factory);
70
    registerKernelFactory(CalcNonbondedForceKernel::Name(), factory);
71
    registerKernelFactory(CalcCustomNonbondedForceKernel::Name(), factory);
72
    registerKernelFactory(CalcCustomManyParticleForceKernel::Name(), factory);
73
    registerKernelFactory(CalcGBSAOBCForceKernel::Name(), factory);
74
    registerKernelFactory(CalcCustomGBForceKernel::Name(), factory);
75
    registerKernelFactory(CalcGayBerneForceKernel::Name(), factory);
76
    registerKernelFactory(IntegrateLangevinStepKernel::Name(), factory);
77
    registerKernelFactory(IntegrateLangevinMiddleStepKernel::Name(), factory);
78
    platformProperties.push_back(CpuThreads());
79
    platformProperties.push_back(CpuDeterministicForces());
80
81
82
83
84
85
86
    int threads = getNumProcessors();
    char* threadsEnv = getenv("OPENMM_CPU_THREADS");
    if (threadsEnv != NULL)
        stringstream(threadsEnv) >> threads;
    stringstream defaultThreads;
    defaultThreads << threads;
    setPropertyDefaultValue(CpuThreads(), defaultThreads.str());
87
    setPropertyDefaultValue(CpuDeterministicForces(), "false");
88
89
90
91
92
}

const string& CpuPlatform::getPropertyValue(const Context& context, const string& property) const {
    const ContextImpl& impl = getContextImpl(context);
    const PlatformData& data = getPlatformData(impl);
93
94
95
96
    string propertyName = property;
    if (deprecatedPropertyReplacements.find(property) != deprecatedPropertyReplacements.end())
        propertyName = deprecatedPropertyReplacements.find(property)->second;
    map<string, string>::const_iterator value = data.propertyValues.find(propertyName);
97
98
99
    if (value != data.propertyValues.end())
        return value->second;
    return ReferencePlatform::getPropertyValue(context, property);
100
101
102
103
104
105
106
107
108
}

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

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

bool CpuPlatform::isProcessorSupported() {
111
    return isVec4Supported();
peastman's avatar
peastman committed
112
}
113
114
115

void CpuPlatform::contextCreated(ContextImpl& context, const map<string, string>& properties) const {
    ReferencePlatform::contextCreated(context, properties);
116
117
    const string& threadsPropValue = (properties.find(CpuThreads()) == properties.end() ?
            getPropertyDefaultValue(CpuThreads()) : properties.find(CpuThreads())->second);
118
119
    string deterministicForcesValue = (properties.find(CpuDeterministicForces()) == properties.end() ?
            getPropertyDefaultValue(CpuDeterministicForces()) : properties.find(CpuDeterministicForces())->second);
120
121
    int numThreads;
    stringstream(threadsPropValue) >> numThreads;
122
123
124
    transform(deterministicForcesValue.begin(), deterministicForcesValue.end(), deterministicForcesValue.begin(), ::tolower);
    bool deterministicForces = (deterministicForcesValue == "true");
    PlatformData* data = new PlatformData(context.getSystem().getNumParticles(), numThreads, deterministicForces);
125
    contextData[&context] = data;
peastman's avatar
peastman committed
126
127
128
129
130
131
    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;
    }
132
133
134
135
136
137
}

void CpuPlatform::contextDestroyed(ContextImpl& context) const {
    PlatformData* data = contextData[&context];
    delete data;
    contextData.erase(&context);
peastman's avatar
peastman committed
138
139
    ReferencePlatform::PlatformData* refPlatformData = reinterpret_cast<ReferencePlatform::PlatformData*>(context.getPlatformData());
    delete refPlatformData;
140
141
142
143
144
145
}

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

146
147
148
149
const CpuPlatform::PlatformData& CpuPlatform::getPlatformData(const ContextImpl& context) {
    return *contextData[&context];
}

150
CpuPlatform::PlatformData::PlatformData(int numParticles, int numThreads, bool deterministicForces) : posq(4*numParticles), threads(numThreads),
151
152
        deterministicForces(deterministicForces), numParticles(numParticles), neighborList(NULL), cutoff(0.0), paddedCutoff(0.0), anyExclusions(false),
        currentPosqIndex(-1), nextPosqIndex(0) {
153
    numThreads = threads.getNumThreads();
154
155
156
157
    threadForce.resize(numThreads);
    for (int i = 0; i < numThreads; i++)
        threadForce[i].resize(4*numParticles);
    isPeriodic = false;
158
159
160
    stringstream threadsProperty;
    threadsProperty << numThreads;
    propertyValues[CpuThreads()] = threadsProperty.str();
161
    propertyValues[CpuDeterministicForces()] = deterministicForces ? "true" : "false";
162
}
163
164
165
166
167
168

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

169
void CpuPlatform::PlatformData::requestNeighborList(double cutoffDistance, double padding, bool useExclusions, const vector<set<int> >& exclusionList) {
170
    if (neighborList == NULL) {
171
        neighborList = new CpuNeighborList(getVectorWidth());
172
173
174
175
176
        if (cutoffDistance == 0.0)
            neighborList->createDenseNeighborList(numParticles, exclusionList);
    }
    else if ((cutoffDistance == 0.0) != (cutoff == 0.0))
        throw OpenMMException("All nonbonded Forces must agree on whether to apply a cutoff");
177
178
179
180
181
182
183
184
185
186
187
188
    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
189
190
    else if (!anyExclusions)
        exclusions = exclusionList;
191
}
192
193
194
195

int CpuPlatform::PlatformData::requestPosqIndex() {
    return nextPosqIndex++;
}