CpuPlatform.cpp 9.33 KB
Newer Older
1
2
3
/* -------------------------------------------------------------------------- *
 *                                   OpenMM                                   *
 * -------------------------------------------------------------------------- *
Evan Pretti's avatar
Evan Pretti committed
4
5
 * This is part of the OpenMM molecular simulation toolkit.                   *
 * See https://openmm.org/development.                                        *
6
 *                                                                            *
7
 * Portions copyright (c) 2013-2025 Stanford University and the Authors.      *
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
 * 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
33
34
#include "CpuSETTLE.h"
#include "ReferenceConstraints.h"
35
#include "openmm/OpenMMException.h"
36
#include "openmm/internal/hardware.h"
37
#include "openmm/internal/vectorize.h"
38
#include <algorithm>
39
#include <sstream>
40
#include <stdlib.h>
41
42

using namespace OpenMM;
43
using namespace std;
44

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

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

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

61
CpuPlatform::CpuPlatform() {
Peter Eastman's avatar
Peter Eastman committed
62
    deprecatedPropertyReplacements["CpuThreads"] = CpuThreads();
63
    CpuKernelFactory* factory = new CpuKernelFactory();
64
    registerKernelFactory(CalcForcesAndEnergyKernel::Name(), factory);
65
    registerKernelFactory(UpdateStateDataKernel::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(CalcConstantPotentialForceKernel::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(IntegrateLangevinMiddleStepKernel::Name(), factory);
77
    platformProperties.push_back(CpuThreads());
78
    platformProperties.push_back(CpuDeterministicForces());
79
80
81
82
83
84
85
    int threads = getNumProcessors();
    char* threadsEnv = getenv("OPENMM_CPU_THREADS");
    if (threadsEnv != NULL)
        stringstream(threadsEnv) >> threads;
    stringstream defaultThreads;
    defaultThreads << threads;
    setPropertyDefaultValue(CpuThreads(), defaultThreads.str());
86
    setPropertyDefaultValue(CpuDeterministicForces(), "false");
87
88
89
90
91
}

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

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

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

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

void CpuPlatform::contextCreated(ContextImpl& context, const map<string, string>& properties) const {
114
115
    const string& threadsPropValue = (properties.find(CpuThreads()) == properties.end() ?
            getPropertyDefaultValue(CpuThreads()) : properties.find(CpuThreads())->second);
116
117
118
    map<string, string> refProperties = properties;
    refProperties["Threads"] = threadsPropValue;
    ReferencePlatform::contextCreated(context, refProperties);
119
120
121
122
    string deterministicForcesValue = (properties.find(CpuDeterministicForces()) == properties.end() ?
            getPropertyDefaultValue(CpuDeterministicForces()) : properties.find(CpuDeterministicForces())->second);
    transform(deterministicForcesValue.begin(), deterministicForcesValue.end(), deterministicForcesValue.begin(), ::tolower);
    bool deterministicForces = (deterministicForcesValue == "true");
123
124
    ReferencePlatform::PlatformData* refData = reinterpret_cast<ReferencePlatform::PlatformData*>(context.getPlatformData());
    PlatformData* data = new PlatformData(context.getSystem().getNumParticles(), refData->threads, deterministicForces);
125
    contextData[&context] = data;
126
    ReferenceConstraints& constraints = *(ReferenceConstraints*) refData->constraints;
peastman's avatar
peastman committed
127
128
129
130
131
    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, ThreadPool& threads, bool deterministicForces) : posq(4*numParticles), threads(threads),
151
152
        deterministicForces(deterministicForces), numParticles(numParticles), neighborList(NULL), cutoff(0.0), paddedCutoff(0.0), anyExclusions(false),
        currentPosqIndex(-1), nextPosqIndex(0) {
153
    int 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++;
}