"platforms/cuda/src/kernels/nonbonded.cu" did not exist on "ce74c6aeec22573db368d05c1e57eee051229dbe"
CpuPmeKernels.h 6.44 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
55
    enum CalculationType { Electrostatic=0, Dispersion=1 };

56
    CpuCalcPmeReciprocalForceKernel(std::string name, const Platform& platform) : CalcPmeReciprocalForceKernel(name, platform),
57
            hasCreatedPlan(false), isDeleted(false), realGrid(NULL), complexGrid(NULL), calculationType(Electrostatic) {
58
    }
59
60
61
62
63
64
65
66
67
68
    /**
     * 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);
69
    ~CpuCalcPmeReciprocalForceKernel();
70
71
72
    /**
     * Begin computing the force and energy.
     * 
peastman's avatar
peastman committed
73
74
75
     * @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
76
     */
peastman's avatar
peastman committed
77
    void beginComputation(IO& io, const Vec3* periodicBoxVectors, bool includeEnergy);
78
79
80
81
82
83
    /**
     * 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
     */
84
    double finishComputation(IO& io);
85
    /**
86
     * This routine contains the code executed by the main thread.
87
     */
88
89
90
91
92
    void runMainThread();
    /**
     * This routine contains the code executed by each worker thread.
     */
    void runWorkerThread(ThreadPool& threads, int index);
93
94
95
    /**
     * Get whether the current CPU supports all features needed by this kernel.
     */
96
    static bool isProcessorSupported();
97
98
99
100
101
102
103
104
105
    /**
     * 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;
106
107
108
109
110
    /**
     * Sets the type of reciprocal space computation to perform (Electrostatic or Dispersion).
     * @param type The type of computation
     */
    void setCalculationType(CalculationType type) { calculationType = type; }
peastman's avatar
peastman committed
111
private:
112
    class ComputeTask;
113
114
115
    /**
     * Select a size for one grid dimension that FFTW can handle efficiently.
     */
peastman's avatar
peastman committed
116
    int findFFTDimension(int minimum, bool isZ);
117
118
    static bool hasInitializedThreads;
    static int numThreads;
peastman's avatar
peastman committed
119
120
    int gridx, gridy, gridz, numParticles;
    double alpha;
121
122
    bool hasCreatedPlan, isFinished, isDeleted;
    std::vector<float> force;
123
    std::vector<float> bsplineModuli[3];
124
    std::vector<float> recipEterm;
peastman's avatar
peastman committed
125
    Vec3 lastBoxVectors[3];
126
127
    std::vector<float> threadEnergy;
    std::vector<float*> tempGrid;
128
129
130
    float* realGrid;
    fftwf_complex* complexGrid;
    fftwf_plan forwardFFT, backwardFFT;
131
132
133
    int waitCount;
    pthread_cond_t startCondition, endCondition;
    pthread_mutex_t lock;
134
    pthread_t mainThread;
135
    // The following variables are used to store information about the calculation currently being performed.
136
    IO* io;
137
138
    float energy;
    float* posq;
peastman's avatar
peastman committed
139
    Vec3 periodicBoxVectors[3], recipBoxVectors[3];
140
    bool includeEnergy;
141
    CalculationType calculationType;
142
    gmx_atomic_t atomicCounter;
peastman's avatar
peastman committed
143
144
145
146
};

} // namespace OpenMM

147
#endif /*OPENMM_CPU_PME_KERNELS_H_*/