-
Notifications
You must be signed in to change notification settings - Fork 555
Expand file tree
/
Copy pathfft.cpp
More file actions
171 lines (134 loc) · 5.07 KB
/
Copy pathfft.cpp
File metadata and controls
171 lines (134 loc) · 5.07 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
164
165
166
167
168
169
170
171
/*******************************************************
* 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 <clfft.hpp>
#include <copy.hpp>
#include <err_opencl.hpp>
#include <math.hpp>
#include <memory.hpp>
#include <af/dim4.hpp>
using af::dim4;
namespace arrayfire {
namespace opencl {
void setFFTPlanCacheSize(size_t numPlans) {
fftManager().setMaxCacheSize(numPlans);
}
template<typename T>
struct Precision;
template<>
struct Precision<cfloat> {
enum { type = CLFFT_SINGLE };
};
template<>
struct Precision<cdouble> {
enum { type = CLFFT_DOUBLE };
};
void computeDims(size_t rdims[AF_MAX_DIMS], const dim4 &idims) {
for (int i = 0; i < AF_MAX_DIMS; i++) {
rdims[i] = static_cast<size_t>(idims[i]);
}
}
//(currently) true is in clFFT if length is a power of 2,3,5
inline bool isSupLen(dim_t length) {
while (length > 1) {
if (length % 2 == 0) {
length /= 2;
} else if (length % 3 == 0) {
length /= 3;
} else if (length % 5 == 0) {
length /= 5;
} else if (length % 7 == 0) {
length /= 7;
} else if (length % 11 == 0) {
length /= 11;
} else if (length % 13 == 0) {
length /= 13;
} else {
return false;
}
}
return true;
}
void verifySupported(const int rank, const dim4 &dims) {
for (int i = 0; i < rank; i++) { ARG_ASSERT(1, isSupLen(dims[i])); }
}
template<typename T>
void fft_inplace(Array<T> &in, const int rank, const bool direction) {
verifySupported(rank, in.dims());
size_t tdims[AF_MAX_DIMS], istrides[AF_MAX_DIMS];
computeDims(tdims, in.dims());
computeDims(istrides, in.strides());
int batch = 1;
for (int i = rank; i < AF_MAX_DIMS; i++) { batch *= tdims[i]; }
SharedPlan plan = findPlan(
CLFFT_COMPLEX_INTERLEAVED, CLFFT_COMPLEX_INTERLEAVED,
static_cast<clfftDim>(rank), tdims, istrides, istrides[rank], istrides,
istrides[rank], static_cast<clfftPrecision>(Precision<T>::type), batch);
cl_mem imem = (*in.get())();
cl_command_queue queue = getQueue()();
CLFFT_CHECK(clfftEnqueueTransform(
*plan.get(), direction ? CLFFT_FORWARD : CLFFT_BACKWARD, 1, &queue, 0,
NULL, NULL, &imem, &imem, NULL));
}
template<typename Tc, typename Tr>
Array<Tc> fft_r2c(const Array<Tr> &in, const int rank) {
dim4 odims = in.dims();
odims[0] = odims[0] / 2 + 1;
Array<Tc> out = createEmptyArray<Tc>(odims);
verifySupported(rank, in.dims());
size_t tdims[AF_MAX_DIMS], istrides[AF_MAX_DIMS], ostrides[AF_MAX_DIMS];
computeDims(tdims, in.dims());
computeDims(istrides, in.strides());
computeDims(ostrides, out.strides());
int batch = 1;
for (int i = rank; i < AF_MAX_DIMS; i++) { batch *= tdims[i]; }
SharedPlan plan = findPlan(
CLFFT_REAL, CLFFT_HERMITIAN_INTERLEAVED, static_cast<clfftDim>(rank),
tdims, istrides, istrides[rank], ostrides, ostrides[rank],
static_cast<clfftPrecision>(Precision<Tc>::type), batch);
cl_mem imem = (*in.get())();
cl_mem omem = (*out.get())();
cl_command_queue queue = getQueue()();
CLFFT_CHECK(clfftEnqueueTransform(*plan.get(), CLFFT_FORWARD, 1, &queue, 0,
NULL, NULL, &imem, &omem, NULL));
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);
verifySupported(rank, odims);
size_t tdims[AF_MAX_DIMS], istrides[AF_MAX_DIMS], ostrides[AF_MAX_DIMS];
computeDims(tdims, odims);
computeDims(istrides, in.strides());
computeDims(ostrides, out.strides());
int batch = 1;
for (int i = rank; i < AF_MAX_DIMS; i++) { batch *= tdims[i]; }
SharedPlan plan = findPlan(
CLFFT_HERMITIAN_INTERLEAVED, CLFFT_REAL, static_cast<clfftDim>(rank),
tdims, istrides, istrides[rank], ostrides, ostrides[rank],
static_cast<clfftPrecision>(Precision<Tc>::type), batch);
cl_mem imem = (*in.get())();
cl_mem omem = (*out.get())();
cl_command_queue queue = getQueue()();
CLFFT_CHECK(clfftEnqueueTransform(*plan.get(), CLFFT_BACKWARD, 1, &queue, 0,
NULL, NULL, &imem, &omem, NULL));
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> &, const dim4 &, \
const int);
INSTANTIATE_REAL(float, cfloat)
INSTANTIATE_REAL(double, cdouble)
} // namespace opencl
} // namespace arrayfire