CpuPmeKernels.h 10.4 KB
Newer Older
1
2
#ifndef OPENMM_CPU_PME_KERNELS_H_
#define OPENMM_CPU_PME_KERNELS_H_
peastman's avatar
peastman committed
3
4
5
6
7
8
9
10
11

/* -------------------------------------------------------------------------- *
 *                                   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-2025 Stanford University and the Authors.      *
peastman's avatar
peastman committed
13
 * Authors: Peter Eastman                                                     *
14
 * Contributors: Evan Pretti                                                  *
peastman's avatar
peastman committed
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
 *                                                                            *
 * 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
#define NOMINMAX
36
37
#include "internal/windowsExportPme.h"
#include "openmm/kernels.h"
peastman's avatar
peastman committed
38
#include "openmm/Vec3.h"
39
#include "openmm/internal/ThreadPool.h"
peastman's avatar
peastman committed
40
#include <atomic>
Peter Eastman's avatar
Peter Eastman committed
41
#include <complex>
42
43
44
#include <condition_variable>
#include <mutex>
#include <thread>
peastman's avatar
peastman committed
45
46
47
48
49
#include <vector>

namespace OpenMM {

/**
50
 * This is an optimized CPU implementation of CalcPmeReciprocalForceKernel.  It is both
Peter Eastman's avatar
Peter Eastman committed
51
 * vectorized (requiring SSE 4.1) and multithreaded.  It uses PocketFFT to perform the FFTs.
peastman's avatar
peastman committed
52
53
 */

54
class OPENMM_EXPORT_PME CpuCalcPmeReciprocalForceKernel : public CalcPmeReciprocalForceKernel {
peastman's avatar
peastman committed
55
public:
56
    CpuCalcPmeReciprocalForceKernel(const std::string& name, const Platform& platform) : CalcPmeReciprocalForceKernel(name, platform),
Peter Eastman's avatar
Peter Eastman committed
57
            isDeleted(false) {
58
    }
59
60
61
62
63
64
65
    /**
     * Initialize the kernel.
     * 
     * @param gridx        the x size of the PME grid
     * @param gridy        the y size of the PME grid
     * @param gridz        the z size of the PME grid
     * @param numParticles the number of particles in the system
66
     * @param indices      indices of particles to compute charge derivatives for
67
     * @param alpha        the Ewald blending parameter
68
     * @param deterministic whether it should attempt to make the resulting forces deterministic
69
     */
70
    void initialize(int xsize, int ysize, int zsize, int numParticles, const std::vector<int>& indices, double alpha, bool deterministic);
71
    ~CpuCalcPmeReciprocalForceKernel();
72
73
    /**
     * Begin computing the force and energy.
74
75
76
77
78
79
     *
     * @param io                        an object that coordinates data transfer
     * @param periodicBoxVectors        the vectors defining the periodic box (measured in nm)
     * @param includeEnergy             true if potential energy should be computed
     * @param includeForces             true if forces should be computed
     * @param includeChargeDerivatives  true if charge derivatives should be computed
80
     */
81
    void beginComputation(IO& io, const Vec3* periodicBoxVectors, bool includeEnergy, bool includeForces, bool includeChargeDerivatives);
82
83
84
85
86
87
    /**
     * Finish computing the force and energy.
     * 
     * @param io   an object that coordinates data transfer
     * @return the potential energy due to the PME reciprocal space interactions
     */
88
    double finishComputation(IO& io);
89
    /**
90
     * This routine contains the code executed by the main thread.
91
     */
92
93
94
95
96
    void runMainThread();
    /**
     * This routine contains the code executed by each worker thread.
     */
    void runWorkerThread(ThreadPool& threads, int index);
97
98
99
    /**
     * Get whether the current CPU supports all features needed by this kernel.
     */
100
    static bool isProcessorSupported();
101
102
103
104
105
106
107
108
109
    /**
     * Get the parameters being used for PME.
     * 
     * @param alpha   the separation parameter
     * @param nx      the number of grid points along the X axis
     * @param ny      the number of grid points along the Y axis
     * @param nz      the number of grid points along the Z axis
     */
    void getPMEParameters(double& alpha, int& nx, int& ny, int& nz) const;
110
111
private:
    /**
Peter Eastman's avatar
Peter Eastman committed
112
     * Select a size for one grid dimension that PocketFFT can handle efficiently.
113
     */
Peter Eastman's avatar
Peter Eastman committed
114
    int findFFTDimension(int minimum);
115
116
    static bool hasInitializedThreads;
    static int numThreads;
117
    int gridx, gridy, gridz, numParticles, numIndices;
118
    double alpha;
119
    bool deterministic;
Peter Eastman's avatar
Peter Eastman committed
120
    bool isFinished, isDeleted;
121
    std::vector<int> chargeIndices;
122
    std::vector<float> force;
123
    std::vector<float> chargeDerivatives;
124
125
126
127
    std::vector<float> bsplineModuli[3];
    std::vector<float> recipEterm;
    Vec3 lastBoxVectors[3];
    std::vector<float> threadEnergy;
Peter Eastman's avatar
Peter Eastman committed
128
129
130
131
    std::vector<std::vector<float> > realGrids;
    std::vector<std::complex<float> > complexGrid;
    std::vector<std::size_t> gridShape, fftAxes;
    std::vector<std::ptrdiff_t> realGridStride, complexGridStride;
132
    int waitCount;
133
134
135
    std::condition_variable startCondition, endCondition;
    std::mutex lock;
    std::thread mainThread;
136
137
138
139
140
    // The following variables are used to store information about the calculation currently being performed.
    IO* io;
    float energy;
    float* posq;
    Vec3 periodicBoxVectors[3], recipBoxVectors[3];
141
    bool includeEnergy, includeForces, includeChargeDerivatives;
peastman's avatar
peastman committed
142
    std::atomic<int> atomicCounter;
143
144
145
146
147
148
};



/**
 * This is an optimized CPU implementation of CalcDispersionPmeReciprocalForceKernel.  It is both
Peter Eastman's avatar
Peter Eastman committed
149
 * vectorized (requiring SSE 4.1) and multithreaded.  It uses PocketFFT to perform the FFTs.
150
151
 */

152
class OPENMM_EXPORT_PME CpuCalcDispersionPmeReciprocalForceKernel : public CalcDispersionPmeReciprocalForceKernel {
153
public:
154
    CpuCalcDispersionPmeReciprocalForceKernel(const std::string& name, const Platform& platform) : CalcDispersionPmeReciprocalForceKernel(name, platform),
Peter Eastman's avatar
Peter Eastman committed
155
            isDeleted(false) {
156
157
158
159
160
161
162
163
164
    }
    /**
     * Initialize the kernel.
     * 
     * @param gridx        the x size of the PME grid
     * @param gridy        the y size of the PME grid
     * @param gridz        the z size of the PME grid
     * @param numParticles the number of particles in the system
     * @param alpha        the Ewald blending parameter
165
     * @param deterministic whether it should attempt to make the resulting forces deterministic
166
     */
167
    void initialize(int xsize, int ysize, int zsize, int numParticles, double alpha, bool deterministic);
168
169
170
171
172
173
174
175
    ~CpuCalcDispersionPmeReciprocalForceKernel();
    /**
     * Begin computing the force and energy.
     * 
     * @param io                  an object that coordinates data transfer
     * @param periodicBoxVectors  the vectors defining the periodic box (measured in nm)
     * @param includeEnergy       true if potential energy should be computed
     */
176
    void beginComputation(CalcPmeReciprocalForceKernel::IO& io, const Vec3* periodicBoxVectors, bool includeEnergy);
177
    /**
178
179
180
181
     * Finish computing the force and energy.
     * 
     * @param io   an object that coordinates data transfer
     * @return the potential energy due to the PME reciprocal space interactions
182
     */
183
    double finishComputation(CalcPmeReciprocalForceKernel::IO& io);
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
    /**
     * This routine contains the code executed by the main thread.
     */
    void runMainThread();
    /**
     * This routine contains the code executed by each worker thread.
     */
    void runWorkerThread(ThreadPool& threads, int index);
    /**
     * Get whether the current CPU supports all features needed by this kernel.
     */
    static bool isProcessorSupported();
    /**
     * Get the parameters being used for PME.
     * 
     * @param alpha   the separation parameter
     * @param nx      the number of grid points along the X axis
     * @param ny      the number of grid points along the Y axis
     * @param nz      the number of grid points along the Z axis
     */
    void getPMEParameters(double& alpha, int& nx, int& ny, int& nz) const;
peastman's avatar
peastman committed
205
private:
206
    class ComputeTask;
207
    /**
Peter Eastman's avatar
Peter Eastman committed
208
     * Select a size for one grid dimension that PocketFFT can handle efficiently.
209
     */
Peter Eastman's avatar
Peter Eastman committed
210
    int findFFTDimension(int minimum);
211
212
    static bool hasInitializedThreads;
    static int numThreads;
peastman's avatar
peastman committed
213
214
    int gridx, gridy, gridz, numParticles;
    double alpha;
215
    bool deterministic;
Peter Eastman's avatar
Peter Eastman committed
216
    bool isFinished, isDeleted;
217
    std::vector<float> force;
218
    std::vector<float> bsplineModuli[3];
219
    std::vector<float> recipEterm;
peastman's avatar
peastman committed
220
    Vec3 lastBoxVectors[3];
221
    std::vector<float> threadEnergy;
Peter Eastman's avatar
Peter Eastman committed
222
223
224
225
    std::vector<std::vector<float> > realGrids;
    std::vector<std::complex<float> > complexGrid;
    std::vector<std::size_t> gridShape, fftAxes;
    std::vector<std::ptrdiff_t> realGridStride, complexGridStride;
226
    int waitCount;
227
228
229
    std::condition_variable startCondition, endCondition;
    std::mutex lock;
    std::thread mainThread;
230
    // The following variables are used to store information about the calculation currently being performed.
231
    CalcPmeReciprocalForceKernel::IO* io;
232
233
    float energy;
    float* posq;
peastman's avatar
peastman committed
234
    Vec3 periodicBoxVectors[3], recipBoxVectors[3];
235
    bool includeEnergy;
peastman's avatar
peastman committed
236
    std::atomic<int> atomicCounter;
peastman's avatar
peastman committed
237
238
239
240
};

} // namespace OpenMM

241
#endif /*OPENMM_CPU_PME_KERNELS_H_*/