CpuPmeKernels.h 9.74 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.               *
 *                                                                            *
peastman's avatar
peastman committed
12
 * Portions copyright (c) 2013-2017 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/gmx_atomic.h"
40
#include "openmm/internal/ThreadPool.h"
41
#include <fftw3.h>
peastman's avatar
peastman committed
42
43
44
45
46
47
#include <pthread.h>
#include <vector>

namespace OpenMM {

/**
48
49
 * This is an optimized CPU implementation of CalcPmeReciprocalForceKernel.  It is both
 * vectorized (requiring SSE 4.1) and multithreaded.  It uses FFTW 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(std::string name, const Platform& platform) : CalcPmeReciprocalForceKernel(name, platform),
55
            hasCreatedPlan(false), isDeleted(false), realGrid(NULL), complexGrid(NULL) {
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
107
108
109
110
111
112
113
private:
    /**
     * Select a size for one grid dimension that FFTW can handle efficiently.
     */
    int findFFTDimension(int minimum, bool isZ);
    static bool hasInitializedThreads;
    static int numThreads;
    int gridx, gridy, gridz, numParticles;
    double alpha;
114
    bool deterministic;
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
    bool hasCreatedPlan, isFinished, isDeleted;
    std::vector<float> force;
    std::vector<float> bsplineModuli[3];
    std::vector<float> recipEterm;
    Vec3 lastBoxVectors[3];
    std::vector<float> threadEnergy;
    std::vector<float*> tempGrid;
    float* realGrid;
    fftwf_complex* complexGrid;
    fftwf_plan forwardFFT, backwardFFT;
    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;
    gmx_atomic_t atomicCounter;
};



/**
 * This is an optimized CPU implementation of CalcDispersionPmeReciprocalForceKernel.  It is both
 * vectorized (requiring SSE 4.1) and multithreaded.  It uses FFTW to perform the FFTs.
 */

class OPENMM_EXPORT_PME CpuCalcDispersionPmeReciprocalForceKernel : public CalcPmeReciprocalForceKernel {
public:
    CpuCalcDispersionPmeReciprocalForceKernel(std::string name, const Platform& platform) : CalcPmeReciprocalForceKernel(name, platform),
            hasCreatedPlan(false), isDeleted(false), realGrid(NULL), complexGrid(NULL)  {
    }
    /**
     * 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
169
    ~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
     */
    void beginComputation(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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
    double finishComputation(IO& io);
    /**
     * 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
201
202
    /**
     * Select a size for one grid dimension that FFTW can handle efficiently.
     */
peastman's avatar
peastman committed
203
    int findFFTDimension(int minimum, bool isZ);
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;
209
210
    bool hasCreatedPlan, isFinished, isDeleted;
    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
215
    std::vector<float> threadEnergy;
    std::vector<float*> tempGrid;
216
217
218
    float* realGrid;
    fftwf_complex* complexGrid;
    fftwf_plan forwardFFT, backwardFFT;
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
    IO* io;
225
226
    float energy;
    float* posq;
peastman's avatar
peastman committed
227
    Vec3 periodicBoxVectors[3], recipBoxVectors[3];
228
    bool includeEnergy;
229
    gmx_atomic_t atomicCounter;
peastman's avatar
peastman committed
230
231
232
233
};

} // namespace OpenMM

234
#endif /*OPENMM_CPU_PME_KERNELS_H_*/