CpuPmeKernels.h 9.48 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-2015 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
65
66
    /**
     * 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
     */
    void initialize(int xsize, int ysize, int zsize, int numParticles, double alpha);
67
    ~CpuCalcPmeReciprocalForceKernel();
68
69
70
    /**
     * Begin computing the force and energy.
     * 
peastman's avatar
peastman committed
71
72
73
     * @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
74
     */
peastman's avatar
peastman committed
75
    void beginComputation(IO& io, const Vec3* periodicBoxVectors, bool includeEnergy);
76
77
78
79
80
81
    /**
     * 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
     */
82
    double finishComputation(IO& io);
83
    /**
84
     * This routine contains the code executed by the main thread.
85
     */
86
87
88
89
90
    void runMainThread();
    /**
     * This routine contains the code executed by each worker thread.
     */
    void runWorkerThread(ThreadPool& threads, int index);
91
92
93
    /**
     * Get whether the current CPU supports all features needed by this kernel.
     */
94
    static bool isProcessorSupported();
95
96
97
98
99
100
101
102
103
    /**
     * 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;
104
105
106
107
108
109
110
111
112
113
114
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
158
159
160
161
162
163
164
165
166
167
private:
    class ComputeTask;
    /**
     * 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;
    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
     */
    void initialize(int xsize, int ysize, int zsize, int numParticles, double alpha);
    ~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);
168
    /**
169
170
171
172
     * 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
173
     */
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
    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
196
private:
197
    class ComputeTask;
198
199
200
    /**
     * Select a size for one grid dimension that FFTW can handle efficiently.
     */
peastman's avatar
peastman committed
201
    int findFFTDimension(int minimum, bool isZ);
202
203
    static bool hasInitializedThreads;
    static int numThreads;
peastman's avatar
peastman committed
204
205
    int gridx, gridy, gridz, numParticles;
    double alpha;
206
207
    bool hasCreatedPlan, isFinished, isDeleted;
    std::vector<float> force;
208
    std::vector<float> bsplineModuli[3];
209
    std::vector<float> recipEterm;
peastman's avatar
peastman committed
210
    Vec3 lastBoxVectors[3];
211
212
    std::vector<float> threadEnergy;
    std::vector<float*> tempGrid;
213
214
215
    float* realGrid;
    fftwf_complex* complexGrid;
    fftwf_plan forwardFFT, backwardFFT;
216
217
218
    int waitCount;
    pthread_cond_t startCondition, endCondition;
    pthread_mutex_t lock;
219
    pthread_t mainThread;
220
    // The following variables are used to store information about the calculation currently being performed.
221
    IO* io;
222
223
    float energy;
    float* posq;
peastman's avatar
peastman committed
224
    Vec3 periodicBoxVectors[3], recipBoxVectors[3];
225
    bool includeEnergy;
226
    gmx_atomic_t atomicCounter;
peastman's avatar
peastman committed
227
228
229
230
};

} // namespace OpenMM

231
#endif /*OPENMM_CPU_PME_KERNELS_H_*/