CpuPmeKernels.h 5.8 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-2014 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 <fftw3.h>
peastman's avatar
peastman committed
40
41
42
43
44
45
#include <pthread.h>
#include <vector>

namespace OpenMM {

/**
46
47
 * 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
48
49
 */

50
class OPENMM_EXPORT_PME CpuCalcPmeReciprocalForceKernel : public CalcPmeReciprocalForceKernel {
peastman's avatar
peastman committed
51
public:
52
    class ThreadData;
53
54
55
    CpuCalcPmeReciprocalForceKernel(std::string name, const Platform& platform) : CalcPmeReciprocalForceKernel(name, platform),
            hasCreatedPlan(false), isDeleted(false), realGrid(NULL), complexGrid(NULL) {
    }
56
57
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
     * @param alpha        the Ewald blending parameter
     */
    void initialize(int xsize, int ysize, int zsize, int numParticles, double alpha);
66
    ~CpuCalcPmeReciprocalForceKernel();
67
68
69
    /**
     * Begin computing the force and energy.
     * 
peastman's avatar
peastman committed
70
71
72
     * @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
73
     */
peastman's avatar
peastman committed
74
    void beginComputation(IO& io, const Vec3* periodicBoxVectors, bool includeEnergy);
75
76
77
78
79
80
    /**
     * 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
     */
81
    double finishComputation(IO& io);
82
83
84
    /**
     * This routine contains the code executed by each thread.
     */
85
    void runThread(int index);
86
87
88
    /**
     * Get whether the current CPU supports all features needed by this kernel.
     */
89
    static bool isProcessorSupported();
peastman's avatar
peastman committed
90
private:
91
92
93
    /**
     * This is called by the worker threads to wait until the master thread instructs them to advance.
     */
94
    void threadWait();
95
96
97
    /**
     * This is called by the master thread to instruct all the worker threads to advance.
     */
98
    void advanceThreads();
99
100
101
    /**
     * Select a size for one grid dimension that FFTW can handle efficiently.
     */
peastman's avatar
peastman committed
102
    int findFFTDimension(int minimum, bool isZ);
103
104
    static bool hasInitializedThreads;
    static int numThreads;
peastman's avatar
peastman committed
105
106
    int gridx, gridy, gridz, numParticles;
    double alpha;
107
108
    bool hasCreatedPlan, isFinished, isDeleted;
    std::vector<float> force;
109
    std::vector<float> bsplineModuli[3];
110
    std::vector<float> recipEterm;
peastman's avatar
peastman committed
111
    Vec3 lastBoxVectors[3];
112
113
114
    float* realGrid;
    fftwf_complex* complexGrid;
    fftwf_plan forwardFFT, backwardFFT;
115
116
    int waitCount;
    pthread_cond_t startCondition, endCondition;
117
    pthread_cond_t mainThreadStartCondition, mainThreadEndCondition;
118
    pthread_mutex_t lock;
119
    pthread_t mainThread;
peastman's avatar
peastman committed
120
    std::vector<pthread_t> thread;
121
122
    std::vector<ThreadData*> threadData;
    // The following variables are used to store information about the calculation currently being performed.
123
    IO* io;
124
125
    float energy;
    float* posq;
peastman's avatar
peastman committed
126
    Vec3 periodicBoxVectors[3], recipBoxVectors[3];
127
    bool includeEnergy;
peastman's avatar
peastman committed
128
129
130
131
};

} // namespace OpenMM

132
#endif /*OPENMM_CPU_PME_KERNELS_H_*/