-
Notifications
You must be signed in to change notification settings - Fork 548
Expand file tree
/
Copy pathfft.cu
More file actions
163 lines (126 loc) · 5.44 KB
/
fft.cu
File metadata and controls
163 lines (126 loc) · 5.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
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
/*******************************************************
* Copyright (c) 2014, ArrayFire
* All rights reserved.
*
* This file is distributed under 3-clause BSD license.
* The complete license agreement can be obtained at:
* http://arrayfire.com/licenses/BSD-3-Clause
********************************************************/
#include <fft.hpp>
#include <Array.hpp>
#include <copy.hpp>
#include <cufft.hpp>
#include <debug_cuda.hpp>
#include <math.hpp>
#include <memory.hpp>
#include <af/dim4.hpp>
#include <array>
using af::dim4;
using std::array;
using std::string;
namespace arrayfire {
namespace cuda {
void setFFTPlanCacheSize(size_t numPlans) {
fftManager().setMaxCacheSize(numPlans);
}
template<typename T>
struct cufft_transform;
#define CUFFT_FUNC(T, TRANSFORM_TYPE) \
template<> \
struct cufft_transform<T> { \
enum { type = CUFFT_##TRANSFORM_TYPE }; \
cufftResult operator()(cufftHandle plan, T *in, T *out, int dir) { \
return cufftExec##TRANSFORM_TYPE(plan, in, out, dir); \
} \
};
CUFFT_FUNC(cfloat, C2C)
CUFFT_FUNC(cdouble, Z2Z)
template<typename To, typename Ti>
struct cufft_real_transform;
#define CUFFT_REAL_FUNC(To, Ti, TRANSFORM_TYPE) \
template<> \
struct cufft_real_transform<To, Ti> { \
enum { type = CUFFT_##TRANSFORM_TYPE }; \
cufftResult operator()(cufftHandle plan, Ti *in, To *out) { \
return cufftExec##TRANSFORM_TYPE(plan, in, out); \
} \
};
CUFFT_REAL_FUNC(cfloat, float, R2C)
CUFFT_REAL_FUNC(cdouble, double, D2Z)
CUFFT_REAL_FUNC(float, cfloat, C2R)
CUFFT_REAL_FUNC(double, cdouble, Z2D)
inline array<int, AF_MAX_DIMS> computeDims(const int rank, const dim4 &idims) {
array<int, AF_MAX_DIMS> retVal = {};
for (int i = 0; i < rank; i++) { retVal[i] = idims[(rank - 1) - i]; }
return retVal;
}
template<typename T>
void fft_inplace(Array<T> &in, const int rank, const bool direction) {
const dim4 idims = in.dims();
const dim4 istrides = in.strides();
auto t_dims = computeDims(rank, idims);
auto in_embed = computeDims(rank, in.getDataDims());
int batch = 1;
for (int i = rank; i < 4; i++) { batch *= idims[i]; }
SharedPlan plan =
findPlan(rank, t_dims.data(), in_embed.data(), istrides[0],
istrides[rank], in_embed.data(), istrides[0], istrides[rank],
(cufftType)cufft_transform<T>::type, batch);
cufft_transform<T> transform;
CUFFT_CHECK(cufftSetStream(*plan.get(), getActiveStream()));
CUFFT_CHECK(transform(*plan.get(), (T *)in.get(), in.get(),
direction ? CUFFT_FORWARD : CUFFT_INVERSE));
}
template<typename Tc, typename Tr>
Array<Tc> fft_r2c(const Array<Tr> &in, const int rank) {
dim4 idims = in.dims();
dim4 odims = in.dims();
odims[0] = odims[0] / 2 + 1;
Array<Tc> out = createEmptyArray<Tc>(odims);
auto t_dims = computeDims(rank, idims);
auto in_embed = computeDims(rank, in.getDataDims());
auto out_embed = computeDims(rank, out.getDataDims());
int batch = 1;
for (int i = rank; i < AF_MAX_DIMS; i++) { batch *= idims[i]; }
dim4 istrides = in.strides();
dim4 ostrides = out.strides();
SharedPlan plan =
findPlan(rank, t_dims.data(), in_embed.data(), istrides[0],
istrides[rank], out_embed.data(), ostrides[0], ostrides[rank],
(cufftType)cufft_real_transform<Tc, Tr>::type, batch);
cufft_real_transform<Tc, Tr> transform;
CUFFT_CHECK(cufftSetStream(*plan.get(), getActiveStream()));
CUFFT_CHECK(transform(*plan.get(), (Tr *)in.get(), out.get()));
return out;
}
template<typename Tr, typename Tc>
Array<Tr> fft_c2r(const Array<Tc> &in, const dim4 &odims, const int rank) {
Array<Tr> out = createEmptyArray<Tr>(odims);
auto t_dims = computeDims(rank, odims);
auto in_embed = computeDims(rank, in.getDataDims());
auto out_embed = computeDims(rank, out.getDataDims());
int batch = 1;
for (int i = rank; i < AF_MAX_DIMS; i++) { batch *= odims[i]; }
dim4 istrides = in.strides();
dim4 ostrides = out.strides();
cufft_real_transform<Tr, Tc> transform;
SharedPlan plan =
findPlan(rank, t_dims.data(), in_embed.data(), istrides[0],
istrides[rank], out_embed.data(), ostrides[0], ostrides[rank],
(cufftType)cufft_real_transform<Tr, Tc>::type, batch);
CUFFT_CHECK(cufftSetStream(*plan.get(), getActiveStream()));
CUFFT_CHECK(transform(*plan.get(), (Tc *)in.get(), out.get()));
return out;
}
#define INSTANTIATE(T) \
template void fft_inplace<T>(Array<T> &, const int, const bool);
INSTANTIATE(cfloat)
INSTANTIATE(cdouble)
#define INSTANTIATE_REAL(Tr, Tc) \
template Array<Tc> fft_r2c<Tc, Tr>(const Array<Tr> &, const int); \
template Array<Tr> fft_c2r<Tr, Tc>(const Array<Tc> &in, const dim4 &odims, \
const int);
INSTANTIATE_REAL(float, cfloat)
INSTANTIATE_REAL(double, cdouble)
} // namespace cuda
} // namespace arrayfire