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 
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  {
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>
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>
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,
112  else
113  {
115  input.size2());
116 
117  viennacl::linalg::direct(input, output, cols_num, cols_int, rows_num, sign,
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,
127  else
128  {
130  input.size2());
131 
132  viennacl::linalg::direct(input, output, rows_num, cols_int, cols_num, sign,
134 
135  input = output;
136  }
137 
138 }
139 
147 template<class NumericT, unsigned int AlignmentV>
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,
162  }
163  else
164  viennacl::linalg::direct(input, output, cols_num, cols_int, rows_num, sign,
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,
174  }
175  else
176  {
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,
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));
200 }
201 
212 template<class NumericT, unsigned int AlignmentV>
214  viennacl::vector<NumericT, AlignmentV>& output, vcl_size_t batch_num = 1)
215 {
216  viennacl::fft(input, output, batch_num, NumericT(1.0));
218 }
219 
220 namespace linalg
221 {
231  template<class NumericT, unsigned int AlignmentV>
235  {
236  assert(input1.size() == input2.size());
237  assert(input1.size() == output.size());
238  //temporal arrays
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>
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
Implementation of the dense matrix class.
A dense matrix class.
Definition: forwards.h:375
void normalize(viennacl::vector< NumericT, AlignmentV > &input)
Normalize vector on with his own size.
float NumericT
Definition: bisect.cpp:40
Implementations of Fast Furier Transformation.
vcl_size_t size(VectorType const &vec)
Generic routine for obtaining the size of a vector (ViennaCL, uBLAS, etc.)
Definition: size.hpp:239
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.
std::size_t vcl_size_t
Definition: forwards.h:75
size_type size2() const
Returns the number of columns.
Definition: matrix_def.hpp:226
size_type size1() const
Returns the number of rows.
Definition: matrix_def.hpp:224
void convolve_i(viennacl::vector< SCALARTYPE, ALIGNMENT > &input1, viennacl::vector< SCALARTYPE, ALIGNMENT > &input2, viennacl::vector< SCALARTYPE, ALIGNMENT > &output)
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.
The vector type with operator-overloads and proxy classes is defined here. Linear algebra operations ...
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) ...
size_type size() const
Returns the length of the vector (cf. std::vector)
Definition: vector_def.hpp:118
size_type internal_size2() const
Returns the internal number of columns. Usually required for launching OpenCL kernels only...
Definition: matrix_def.hpp:240
ScalarType convolve(std::vector< ScalarType > &in1, std::vector< ScalarType > &in2, unsigned int, unsigned int, unsigned int)
Definition: fft_1d.cpp:944
Extracts the underlying OpenCL handle from a vector, a matrix, an expression etc. ...
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.
ScalarType fft(std::vector< ScalarType > &in, std::vector< ScalarType > &out, unsigned int, unsigned int, unsigned int batch_size)
Definition: fft_1d.cpp:719
SCALARTYPE sign(SCALARTYPE val)