CpuPlatform.h 5.95 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
#ifndef OPENMM_CPUPLATFORM_H_
#define OPENMM_CPUPLATFORM_H_

/* -------------------------------------------------------------------------- *
 *                                   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.               *
 *                                                                            *
12
 * Portions copyright (c) 2013-2022 Stanford University and the Authors.      *
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.                                     *
 * -------------------------------------------------------------------------- */

35
#include "AlignedArray.h"
36
#include "CpuRandom.h"
37
#include "CpuNeighborList.h"
38
#include "ReferencePlatform.h"
39
40
#include "openmm/internal/ContextImpl.h"
#include "openmm/internal/ThreadPool.h"
41
#include "windowsExportCpu.h"
42
#include <map>
43
44
45
46
47
48
49
50
51

namespace OpenMM {
    
/**
 * This Platform subclass uses CPU implementations of the OpenMM kernels.
 */

class OPENMM_EXPORT_CPU CpuPlatform : public ReferencePlatform {
public:
52
    class PlatformData;
53
54
55
56
57
58
    CpuPlatform();
    const std::string& getName() const {
        static const std::string name = "CPU";
        return name;
    }
    double getSpeed() const;
59
    const std::string& getPropertyValue(const Context& context, const std::string& property) const;
60
    bool supportsDoublePrecision() const;
peastman's avatar
peastman committed
61
    static bool isProcessorSupported();
62
63
    void contextCreated(ContextImpl& context, const std::map<std::string, std::string>& properties) const;
    void contextDestroyed(ContextImpl& context) const;
64
65
66
67
    /**
     * This is the name of the parameter for selecting the number of threads to use.
     */
    static const std::string& CpuThreads() {
68
        static const std::string key = "Threads";
69
70
        return key;
    }
71
72
73
74
75
76
77
78
79
    /**
     * This is the name of the parameter for requesting that force computations be deterministic.  Setting
     * this to "true" DOES NOT GUARANTEE that the forces will actually be fully deterministic, but it does
     * try to reduce the variation in them at the cost of a small loss in performance.
     */
    static const std::string& CpuDeterministicForces() {
        static const std::string key = "DeterministicForces";
        return key;
    }
80
81
82
83
84
    /**
     * We cannot use the standard mechanism for platform data, because that is already used by the superclass.
     * Instead, we maintain a table of ContextImpls to PlatformDatas.
     */
    static PlatformData& getPlatformData(ContextImpl& context);
85
    static const PlatformData& getPlatformData(const ContextImpl& context);
86
private:
87
    static std::map<const ContextImpl*, PlatformData*> contextData;
88
89
90
91
};

class CpuPlatform::PlatformData {
public:
92
    PlatformData(int numParticles, int numThreads, bool deterministicForces);
93
    ~PlatformData();
94
95
96
97
98
99
100
101
102
103
104
105
    /**
     * Request that a neighbor list be built and maintained.
     * 
     * @param cutoffDistance  the cutoff distance for particle pairs to be included in the neighbor list.
     *                        If this is 0, a dense neighbor list is built that includes all particle pairs
     *                        regardless of distance.
     * @param padding         a padding distance that should be added to the cutoff so the neighbor list
     *                        does not need to be rebuilt every step
     * @param useExclusions   whether to omit specific excluded interactions
     * @param exclusionList   if useExclusions is true, exclusionList[i] should contain the indices of all
     *                        particles with which particle i should not interact
     */
106
    void requestNeighborList(double cutoffDistance, double padding, bool useExclusions, const std::vector<std::set<int> >& exclusionList);
107
    int requestPosqIndex();
108
109
    AlignedArray<float> posq;
    std::vector<AlignedArray<float> > threadForce;
110
111
    ThreadPool threads;
    bool isPeriodic;
112
    CpuRandom random;
113
    std::map<std::string, std::string> propertyValues;
114
    int numParticles;
115
116
    CpuNeighborList* neighborList;
    double cutoff, paddedCutoff;
117
    bool anyExclusions, deterministicForces;
118
    int currentPosqIndex, nextPosqIndex;
119
    std::vector<std::set<int> > exclusions;
120
121
122
123
124
};

} // namespace OpenMM

#endif /*OPENMM_CPUPLATFORM_H_*/