OpenCLFFT3D.h 6.58 KB
Newer Older
Peter Eastman's avatar
Peter Eastman committed
1
2
3
4
5
6
7
8
9
10
11
#ifndef __OPENMM_OPENCLFFT3D_H__
#define __OPENMM_OPENCLFFT3D_H__

/* -------------------------------------------------------------------------- *
 *                                   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) 2009-2025 Stanford University and the Authors.      *
Peter Eastman's avatar
Peter Eastman committed
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
 * Authors: Peter Eastman                                                     *
 * Contributors:                                                              *
 *                                                                            *
 * This program is free software: you can redistribute it and/or modify       *
 * it under the terms of the GNU Lesser General Public License as published   *
 * by the Free Software Foundation, either version 3 of the License, or       *
 * (at your option) any later version.                                        *
 *                                                                            *
 * This program is distributed in the hope that it will be useful,            *
 * but WITHOUT ANY WARRANTY; without even the implied warranty of             *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *
 * GNU Lesser General Public License for more details.                        *
 *                                                                            *
 * You should have received a copy of the GNU Lesser General Public License   *
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.      *
 * -------------------------------------------------------------------------- */

Peter Eastman's avatar
Peter Eastman committed
30
31
32
33
#define USE_VKFFT

#ifdef USE_VKFFT
#define VKFFT_BACKEND 3
34
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
Peter Eastman's avatar
Peter Eastman committed
35
36
#include "vkFFT.h"
#endif
37
38
#include "openmm/common/FFT3D.h"
#include "openmm/common/ArrayInterface.h"
Peter Eastman's avatar
Peter Eastman committed
39
40
41

namespace OpenMM {

42
43
class OpenCLContext;

Peter Eastman's avatar
Peter Eastman committed
44
45
46
47
#ifdef USE_VKFFT
/**
 * This class performs three dimensional Fast Fourier Transforms.  It uses the
 * VkFFT library (https://github.com/DTolm/VkFFT).
48
 *
Peter Eastman's avatar
Peter Eastman committed
49
50
51
52
 * This class is most efficient when the size of each dimension is a product of
 * small prime factors: 2, 3, 5, 7, 11, and 13.  You can call findLegalDimension()
 * to determine the smallest size that satisfies this requirement and is greater
 * than or equal to a specified minimum size.
53
 *
Peter Eastman's avatar
Peter Eastman committed
54
55
56
57
58
 * Note that this class performs an unnormalized transform.  That means that if you perform
 * a forward transform followed immediately by an inverse transform, the effect is to
 * multiply every value of the original data set by the total number of data points.
 */
#else
Peter Eastman's avatar
Peter Eastman committed
59
60
61
/**
 * This class performs three dimensional Fast Fourier Transforms.  It is based on the
 * mixed radix algorithm described in
62
 *
Peter Eastman's avatar
Peter Eastman committed
63
64
65
 * Takahashi, D. and Kanada, Y., "High-Performance Radix-2, 3 and 5 Parallel 1-D Complex
 * FFT Algorithms for Distributed-Memory Parallel Computers."  Journal of Supercomputing,
 * 15, 207–228 (2000).
66
 *
Peter Eastman's avatar
Peter Eastman committed
67
 * This class places certain restrictions on the allowed dimensions of the grid.  First,
68
 * the size of each dimension may have no prime factors other than 2, 3, 5, and 7.  You
Peter Eastman's avatar
Peter Eastman committed
69
70
71
72
73
 * can call findLegalDimension() to determine the smallest size that satisfies this
 * requirement and is greater than or equal to a specified minimum size.  Second, the size
 * of each dimension must be small enough to compute each 1D transform entirely in local
 * memory with one work unit per data point.  This will vary between platforms, but is
 * typically at least 512.
74
 *
Peter Eastman's avatar
Peter Eastman committed
75
76
77
78
 * Note that this class performs an unnormalized transform.  That means that if you perform
 * a forward transform followed immediately by an inverse transform, the effect is to
 * multiply every value of the original data set by the total number of data points.
 */
Peter Eastman's avatar
Peter Eastman committed
79
#endif
Peter Eastman's avatar
Peter Eastman committed
80

81
class OPENMM_EXPORT_COMMON OpenCLFFT3D : public FFT3D {
Peter Eastman's avatar
Peter Eastman committed
82
public:
Peter Eastman's avatar
Peter Eastman committed
83
84
85
86
87
88
89
    /**
     * Create an OpenCLFFT3D object for performing transforms of a particular size.
     *
     * @param context the context in which to perform calculations
     * @param xsize   the first dimension of the data sets on which FFTs will be performed
     * @param ysize   the second dimension of the data sets on which FFTs will be performed
     * @param zsize   the third dimension of the data sets on which FFTs will be performed
90
     * @param realToComplex  if true, a real-to-complex transform will be done.  Otherwise, it is complex-to-complex.
Peter Eastman's avatar
Peter Eastman committed
91
     */
92
    OpenCLFFT3D(OpenCLContext& context, int xsize, int ysize, int zsize, bool realToComplex=false);
Peter Eastman's avatar
Peter Eastman committed
93
94
95
#ifdef USE_VKFFT
    ~OpenCLFFT3D();
#endif
Peter Eastman's avatar
Peter Eastman committed
96
    /**
Peter Eastman's avatar
Peter Eastman committed
97
98
     * Perform a Fourier transform.  The transform cannot be done in-place: the input and output
     * arrays must be different.  Also, the input array is used as workspace, so its contents
99
100
     * are destroyed.  This also means that both arrays must be large enough to hold complex values,
     * even when performing a real-to-complex transform.
101
     *
102
103
     * When performing a real-to-complex transform, the output data is of size xsize*ysize*(zsize/2+1)
     * and contains only the non-redundant elements.
Peter Eastman's avatar
Peter Eastman committed
104
     *
Peter Eastman's avatar
Peter Eastman committed
105
106
     * @param in       the data to transform, ordered such that in[x*ysize*zsize + y*zsize + z] contains element (x, y, z)
     * @param out      on exit, this contains the transformed data
Peter Eastman's avatar
Peter Eastman committed
107
108
     * @param forward  true to perform a forward transform, false to perform an inverse transform
     */
109
    void execFFT(ArrayInterface& in, ArrayInterface& out, bool forward=true);
Peter Eastman's avatar
Peter Eastman committed
110
    /**
111
112
     * Get the smallest legal size for a dimension of the grid (that is, a size with no unsupported
     * prime factors).
Peter Eastman's avatar
Peter Eastman committed
113
114
115
116
     *
     * @param minimum   the minimum size the return value must be greater than or equal to
     */
    static int findLegalDimension(int minimum);
Peter Eastman's avatar
Peter Eastman committed
117
118
private:
    int xsize, ysize, zsize;
119
    int xthreads, ythreads, zthreads;
120
    bool packRealAsComplex;
Peter Eastman's avatar
Peter Eastman committed
121
    OpenCLContext& context;
Peter Eastman's avatar
Peter Eastman committed
122
123
124
125
#ifdef USE_VKFFT
    VkFFTApplication app;
#else
    cl::Kernel createKernel(int xsize, int ysize, int zsize, int& threads, int axis, bool forward, bool inputIsReal);
Peter Eastman's avatar
Peter Eastman committed
126
    cl::Kernel xkernel, ykernel, zkernel;
127
128
    cl::Kernel invxkernel, invykernel, invzkernel;
    cl::Kernel packForwardKernel, unpackForwardKernel, packBackwardKernel, unpackBackwardKernel;
Peter Eastman's avatar
Peter Eastman committed
129
#endif
Peter Eastman's avatar
Peter Eastman committed
130
131
132
133
};

} // namespace OpenMM

134
#endif // __OPENMM_OPENCLFFT3D_H__