CpuPmeKernels.h 9.87 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.               *
 *                                                                            *
Peter Eastman's avatar
Peter Eastman committed
12
 * Portions copyright (c) 2013-2022 Stanford University and the Authors.      *
peastman's avatar
peastman committed
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
#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>
peastman's avatar
peastman committed
42
43
44
45
46
47
#include <pthread.h>
#include <vector>

namespace OpenMM {

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

52
class OPENMM_EXPORT_PME CpuCalcPmeReciprocalForceKernel : public CalcPmeReciprocalForceKernel {
peastman's avatar
peastman committed
53
public:
54
    CpuCalcPmeReciprocalForceKernel(const std::string& name, const Platform& platform) : CalcPmeReciprocalForceKernel(name, platform),
Peter Eastman's avatar
Peter Eastman committed
55
            isDeleted(false) {
56
    }
57
58
59
60
61
62
63
64
    /**
     * 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
65
     * @param deterministic whether it should attempt to make the resulting forces deterministic
66
     */
67
    void initialize(int xsize, int ysize, int zsize, int numParticles, double alpha, bool deterministic);
68
    ~CpuCalcPmeReciprocalForceKernel();
69
70
71
    /**
     * Begin computing the force and energy.
     * 
peastman's avatar
peastman committed
72
73
74
     * @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
75
     */
peastman's avatar
peastman committed
76
    void beginComputation(IO& io, const Vec3* periodicBoxVectors, bool includeEnergy);
77
78
79
80
81
82
    /**
     * 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
     */
83
    double finishComputation(IO& io);
84
    /**
85
     * This routine contains the code executed by the main thread.
86
     */
87
88
89
90
91
    void runMainThread();
    /**
     * This routine contains the code executed by each worker thread.
     */
    void runWorkerThread(ThreadPool& threads, int index);
92
93
94
    /**
     * Get whether the current CPU supports all features needed by this kernel.
     */
95
    static bool isProcessorSupported();
96
97
98
99
100
101
102
103
104
    /**
     * 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;
105
106
private:
    /**
Peter Eastman's avatar
Peter Eastman committed
107
     * Select a size for one grid dimension that PocketFFT can handle efficiently.
108
     */
Peter Eastman's avatar
Peter Eastman committed
109
    int findFFTDimension(int minimum);
110
111
112
113
    static bool hasInitializedThreads;
    static int numThreads;
    int gridx, gridy, gridz, numParticles;
    double alpha;
114
    bool deterministic;
Peter Eastman's avatar
Peter Eastman committed
115
    bool isFinished, isDeleted;
116
117
118
119
120
    std::vector<float> force;
    std::vector<float> bsplineModuli[3];
    std::vector<float> recipEterm;
    Vec3 lastBoxVectors[3];
    std::vector<float> threadEnergy;
Peter Eastman's avatar
Peter Eastman committed
121
122
123
124
    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;
125
126
127
128
129
130
131
132
133
134
    int waitCount;
    pthread_cond_t startCondition, endCondition;
    pthread_mutex_t lock;
    pthread_t mainThread;
    // 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];
    bool includeEnergy;
peastman's avatar
peastman committed
135
    std::atomic<int> atomicCounter;
136
137
138
139
140
141
};



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

145
class OPENMM_EXPORT_PME CpuCalcDispersionPmeReciprocalForceKernel : public CalcDispersionPmeReciprocalForceKernel {
146
public:
147
    CpuCalcDispersionPmeReciprocalForceKernel(const std::string& name, const Platform& platform) : CalcDispersionPmeReciprocalForceKernel(name, platform),
Peter Eastman's avatar
Peter Eastman committed
148
            isDeleted(false) {
149
150
151
152
153
154
155
156
157
    }
    /**
     * 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
158
     * @param deterministic whether it should attempt to make the resulting forces deterministic
159
     */
160
    void initialize(int xsize, int ysize, int zsize, int numParticles, double alpha, bool deterministic);
161
162
163
164
165
166
167
168
    ~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
     */
169
    void beginComputation(CalcPmeReciprocalForceKernel::IO& io, const Vec3* periodicBoxVectors, bool includeEnergy);
170
    /**
171
172
173
174
     * 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
175
     */
176
    double finishComputation(CalcPmeReciprocalForceKernel::IO& io);
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
    /**
     * 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
198
private:
199
    class ComputeTask;
200
    /**
Peter Eastman's avatar
Peter Eastman committed
201
     * Select a size for one grid dimension that PocketFFT can handle efficiently.
202
     */
Peter Eastman's avatar
Peter Eastman committed
203
    int findFFTDimension(int minimum);
204
205
    static bool hasInitializedThreads;
    static int numThreads;
peastman's avatar
peastman committed
206
207
    int gridx, gridy, gridz, numParticles;
    double alpha;
208
    bool deterministic;
Peter Eastman's avatar
Peter Eastman committed
209
    bool isFinished, isDeleted;
210
    std::vector<float> force;
211
    std::vector<float> bsplineModuli[3];
212
    std::vector<float> recipEterm;
peastman's avatar
peastman committed
213
    Vec3 lastBoxVectors[3];
214
    std::vector<float> threadEnergy;
Peter Eastman's avatar
Peter Eastman committed
215
216
217
218
    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;
219
220
221
    int waitCount;
    pthread_cond_t startCondition, endCondition;
    pthread_mutex_t lock;
222
    pthread_t mainThread;
223
    // The following variables are used to store information about the calculation currently being performed.
224
    CalcPmeReciprocalForceKernel::IO* io;
225
226
    float energy;
    float* posq;
peastman's avatar
peastman committed
227
    Vec3 periodicBoxVectors[3], recipBoxVectors[3];
228
    bool includeEnergy;
peastman's avatar
peastman committed
229
    std::atomic<int> atomicCounter;
peastman's avatar
peastman committed
230
231
232
233
};

} // namespace OpenMM

234
#endif /*OPENMM_CPU_PME_KERNELS_H_*/