ViennaCL - The Vienna Computing Library: viennacl/fft.hpp Source File

ViennaCL - The Vienna Computing Library 1.7.1 Free open-source GPU-accelerated linear algebra and solver library. |

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
fft.hpp
Go to the documentation of this file.
1 #ifndef VIENNACL_FFT_HPP
2 #define VIENNACL_FFT_HPP
3
4 /* =========================================================================
5 Copyright (c) 2010-2016, Institute for Microelectronics,
6 Institute for Analysis and Scientific Computing,
7 TU Wien.
8 Portions of this software are copyright by UChicago Argonne, LLC.
9
10 -----------------
11 ViennaCL - The Vienna Computing Library
12 -----------------
13
14 Project Head: Karl Rupp rupp@iue.tuwien.ac.at
15
16 (A list of authors and contributors can be found in the manual)
17
18 License: MIT (X11), see file LICENSE in the base directory
19 ============================================================================= */
20
25 #include <viennacl/vector.hpp>
26 #include <viennacl/matrix.hpp>
27
28 #include "viennacl/linalg/fft_operations.hpp"
29 #include "viennacl/traits/handle.hpp"
30
31 #include <cmath>
32
33 #include <stdexcept>
35 namespace viennacl
36 {
37 namespace detail
38 {
39 namespace fft
40 {
41 inline bool is_radix2(vcl_size_t data_size)
42 {
43 return !((data_size > 2) && (data_size & (data_size - 1)));
44 }
45 } //namespace fft
46 } //namespace detail
47
55 template<class NumericT, unsigned int AlignmentV>
56 void inplace_fft(viennacl::vector<NumericT, AlignmentV>& input, vcl_size_t batch_num = 1,
57 NumericT sign = -1.0)
58 {
59 vcl_size_t size = (input.size() >> 1) / batch_num;
60
61 if (!viennacl::detail::fft::is_radix2(size))
62 {
63 viennacl::vector<NumericT, AlignmentV> output(input.size());
64 viennacl::linalg::direct(input, output, size, size, batch_num, sign);
65 viennacl::copy(output, input);
66 }
67 else
68 viennacl::linalg::radix2(input, size, size, batch_num, sign);
69 }
70
79 template<class NumericT, unsigned int AlignmentV>
80 void fft(viennacl::vector<NumericT, AlignmentV>& input,
81 viennacl::vector<NumericT, AlignmentV>& output, vcl_size_t batch_num = 1, NumericT sign = -1.0)
82 {
83 vcl_size_t size = (input.size() >> 1) / batch_num;
84 if (viennacl::detail::fft::is_radix2(size))
85 {
86 viennacl::copy(input, output);
87 viennacl::linalg::radix2(output, size, size, batch_num, sign);
88 }
89 else
90 viennacl::linalg::direct(input, output, size, size, batch_num, sign);
91 }
92
99 template<class NumericT, unsigned int AlignmentV>
100 void inplace_fft(viennacl::matrix<NumericT, viennacl::row_major, AlignmentV>& input,
101 NumericT sign = -1.0)
102 {
103 vcl_size_t rows_num = input.size1();
104 vcl_size_t cols_num = input.size2() >> 1;
105
106 vcl_size_t cols_int = input.internal_size2() >> 1;
107
108 // batch with rows
109 if (viennacl::detail::fft::is_radix2(cols_num))
110 viennacl::linalg::radix2(input, cols_num, cols_int, rows_num, sign,
111 viennacl::linalg::host_based::detail::fft::FFT_DATA_ORDER::ROW_MAJOR);
112 else
113 {
114 viennacl::matrix<NumericT, viennacl::row_major, AlignmentV> output(input.size1(),
115 input.size2());
116
117 viennacl::linalg::direct(input, output, cols_num, cols_int, rows_num, sign,
118 viennacl::linalg::host_based::detail::fft::FFT_DATA_ORDER::ROW_MAJOR);
119
120 input = output;
121 }
122
123 // batch with cols
124 if (viennacl::detail::fft::is_radix2(rows_num))
125 viennacl::linalg::radix2(input, rows_num, cols_int, cols_num, sign,
126 viennacl::linalg::host_based::detail::fft::FFT_DATA_ORDER::COL_MAJOR);
127 else
128 {
129 viennacl::matrix<NumericT, viennacl::row_major, AlignmentV> output(input.size1(),
130 input.size2());
131
132 viennacl::linalg::direct(input, output, rows_num, cols_int, cols_num, sign,
133 viennacl::linalg::host_based::detail::fft::FFT_DATA_ORDER::COL_MAJOR);
134
135 input = output;
136 }
137
138 }
139
147 template<class NumericT, unsigned int AlignmentV>
148 void fft(viennacl::matrix<NumericT, viennacl::row_major, AlignmentV>& input, //TODO
149 viennacl::matrix<NumericT, viennacl::row_major, AlignmentV>& output, NumericT sign = -1.0)
150 {
151
152 vcl_size_t rows_num = input.size1();
153 vcl_size_t cols_num = input.size2() >> 1;
154 vcl_size_t cols_int = input.internal_size2() >> 1;
155
156 // batch with rows
157 if (viennacl::detail::fft::is_radix2(cols_num))
158 {
159 output = input;
160 viennacl::linalg::radix2(output, cols_num, cols_int, rows_num, sign,
161 viennacl::linalg::host_based::detail::fft::FFT_DATA_ORDER::ROW_MAJOR);
162 }
163 else
164 viennacl::linalg::direct(input, output, cols_num, cols_int, rows_num, sign,
165 viennacl::linalg::host_based::detail::fft::FFT_DATA_ORDER::ROW_MAJOR);
166
167 // batch with cols
168 if (viennacl::detail::fft::is_radix2(rows_num))
169 {
170 //std::cout<<"output"<<output<<std::endl;
171
172 viennacl::linalg::radix2(output, rows_num, cols_int, cols_num, sign,
173 viennacl::linalg::host_based::detail::fft::FFT_DATA_ORDER::COL_MAJOR);
174 }
175 else
176 {
177 viennacl::matrix<NumericT, viennacl::row_major, AlignmentV> tmp(output.size1(),
178 output.size2());
179 tmp = output;
180 //std::cout<<"tmp"<<tmp<<std::endl;
181 viennacl::linalg::direct(tmp, output, rows_num, cols_int, cols_num, sign,
182 viennacl::linalg::host_based::detail::fft::FFT_DATA_ORDER::COL_MAJOR);
183 }
184 }
185
195 template<class NumericT, unsigned int AlignmentV>
196 void inplace_ifft(viennacl::vector<NumericT, AlignmentV>& input, vcl_size_t batch_num = 1)
197 {
198 viennacl::inplace_fft(input, batch_num, NumericT(1.0));
199 viennacl::linalg::normalize(input);
200 }
201
212 template<class NumericT, unsigned int AlignmentV>
213 void ifft(viennacl::vector<NumericT, AlignmentV>& input,
214 viennacl::vector<NumericT, AlignmentV>& output, vcl_size_t batch_num = 1)
215 {
216 viennacl::fft(input, output, batch_num, NumericT(1.0));
217 viennacl::linalg::normalize(output);
218 }
219
220 namespace linalg
221 {
231 template<class NumericT, unsigned int AlignmentV>
232 void convolve(viennacl::vector<NumericT, AlignmentV>& input1,
233 viennacl::vector<NumericT, AlignmentV>& input2,
234 viennacl::vector<NumericT, AlignmentV>& output)
235 {
236 assert(input1.size() == input2.size());
237 assert(input1.size() == output.size());
238 //temporal arrays
239 viennacl::vector<NumericT, AlignmentV> tmp1(input1.size());
240 viennacl::vector<NumericT, AlignmentV> tmp2(input2.size());
241 viennacl::vector<NumericT, AlignmentV> tmp3(output.size());
242
243 // align input arrays to equal size
244 // FFT of input data
245 viennacl::fft(input1, tmp1);
246 viennacl::fft(input2, tmp2);
247
248 // multiplication of input data
249 viennacl::linalg::multiply_complex(tmp1, tmp2, tmp3);
250 // inverse FFT of input data
251 viennacl::ifft(tmp3, output);
252 }
253
263 template<class NumericT, unsigned int AlignmentV>
264 void convolve_i(viennacl::vector<NumericT, AlignmentV>& input1,
265 viennacl::vector<NumericT, AlignmentV>& input2,
266 viennacl::vector<NumericT, AlignmentV>& output)
267 {
268 assert(input1.size() == input2.size());
269 assert(input1.size() == output.size());
270
271 viennacl::inplace_fft(input1);
272 viennacl::inplace_fft(input2);
273
274 viennacl::linalg::multiply_complex(input1, input2, output);
275
276 viennacl::inplace_ifft(output);
277 }
278 } //namespace linalg
279 } //namespace viennacl
280
282 #endif
matrix.hpp
Implementation of the dense matrix class.
viennacl::matrix
A dense matrix class.
Definition: forwards.h:375
viennacl::linalg::normalize
void normalize(viennacl::vector< NumericT, AlignmentV > &input)
Normalize vector on with his own size.
Definition: fft_operations.hpp:299
NumericT
float NumericT
Definition: bisect.cpp:40
fft_operations.hpp
Implementations of Fast Furier Transformation.
viennacl::linalg::host_based::detail::fft::FFT_DATA_ORDER::ROW_MAJOR
Definition: fft_operations.hpp:51
viennacl::traits::size
vcl_size_t size(VectorType const &vec)
Generic routine for obtaining the size of a vector (ViennaCL, uBLAS, etc.)
Definition: size.hpp:239
viennacl::linalg::multiply_complex
void multiply_complex(viennacl::vector< NumericT, AlignmentV > const &input1, viennacl::vector< NumericT, AlignmentV > const &input2, viennacl::vector< NumericT, AlignmentV > &output)
Mutiply two complex vectors and store result in output.
Definition: fft_operations.hpp:267
viennacl::vcl_size_t
std::size_t vcl_size_t
Definition: forwards.h:75
viennacl::matrix_base< NumericT >::size2
size_type size2() const
Returns the number of columns.
Definition: matrix_def.hpp:226
viennacl::vector< NumericT, AlignmentV >
viennacl::matrix_base< NumericT >::size1
size_type size1() const
Returns the number of rows.
Definition: matrix_def.hpp:224
viennacl::linalg::convolve_i
void convolve_i(viennacl::vector< SCALARTYPE, ALIGNMENT > &input1, viennacl::vector< SCALARTYPE, ALIGNMENT > &input2, viennacl::vector< SCALARTYPE, ALIGNMENT > &output)
viennacl::linalg::host_based::detail::fft::FFT_DATA_ORDER::COL_MAJOR
Definition: fft_operations.hpp:51
viennacl::linalg::radix2
void radix2(viennacl::matrix< NumericT, viennacl::row_major, AlignmentV > &in, vcl_size_t size, vcl_size_t stride, vcl_size_t batch_num, NumericT sign=NumericT(-1), viennacl::linalg::host_based::detail::fft::FFT_DATA_ORDER::DATA_ORDER data_order=viennacl::linalg::host_based::detail::fft::FFT_DATA_ORDER::ROW_MAJOR)
Radix-2 1D algorithm for computing Fourier transformation.
Definition: fft_operations.hpp:162
vector.hpp
The vector type with operator-overloads and proxy classes is defined here. Linear algebra operations ...
viennacl::copy
void copy(std::vector< NumericT > &cpu_vec, circulant_matrix< NumericT, AlignmentV > &gpu_mat)
Copies a circulant matrix from the std::vector to the OpenCL device (either GPU or multi-core CPU) ...
Definition: circulant_matrix.hpp:150
viennacl::vector_base::size
size_type size() const
Returns the length of the vector (cf. std::vector)
Definition: vector_def.hpp:118
viennacl::matrix_base< NumericT >::internal_size2
size_type internal_size2() const
Returns the internal number of columns. Usually required for launching OpenCL kernels only...
Definition: matrix_def.hpp:240
convolve
ScalarType convolve(std::vector< ScalarType > &in1, std::vector< ScalarType > &in2, unsigned int, unsigned int, unsigned int)
Definition: fft_1d.cpp:944
handle.hpp
Extracts the underlying OpenCL handle from a vector, a matrix, an expression etc. ...
viennacl::linalg::direct
void direct(viennacl::vector< NumericT, AlignmentV > const &in, viennacl::vector< NumericT, AlignmentV > &out, vcl_size_t size, vcl_size_t stride, vcl_size_t batch_num, NumericT sign=NumericT(-1), viennacl::linalg::host_based::detail::fft::FFT_DATA_ORDER::DATA_ORDER data_order=viennacl::linalg::host_based::detail::fft::FFT_DATA_ORDER::ROW_MAJOR)
Direct 1D algorithm for computing Fourier transformation.
Definition: fft_operations.hpp:51
fft
ScalarType fft(std::vector< ScalarType > &in, std::vector< ScalarType > &out, unsigned int, unsigned int, unsigned int batch_size)
Definition: fft_1d.cpp:719
viennacl::linalg::detail::sign
SCALARTYPE sign(SCALARTYPE val)
Definition: qr-method-common.hpp:71