ViennaCL - The Vienna Computing Library  1.7.0
Free open-source GPU-accelerated linear algebra and solver library.
fft.cpp
Go to the documentation of this file.
1 /* =========================================================================
2  Copyright (c) 2010-2015, Institute for Microelectronics,
3  Institute for Analysis and Scientific Computing,
4  TU Wien.
5  Portions of this software are copyright by UChicago Argonne, LLC.
6 
7  -----------------
8  ViennaCL - The Vienna Computing Library
9  -----------------
10 
11  Project Head: Karl Rupp rupp@iue.tuwien.ac.at
12 
13  (A list of authors and contributors can be found in the PDF manual)
14 
15  License: MIT (X11), see file LICENSE in the base directory
16  ============================================================================= */
17 
27 // include necessary system headers
28 #include <iostream>
29 #include <vector>
30 #include <cmath>
31 #include <complex>
32 #include <fstream>
33 
34 // include basic scalar and vector types of ViennaCL
35 #include "viennacl/vector.hpp"
36 #include "viennacl/matrix.hpp"
37 
38 // include FFT routines
39 #include "viennacl/fft.hpp"
41 
45 int main()
46 {
47  // Feel free to change this type definition to double if your gpu supports that
48  typedef float ScalarType;
49 
50  // Create vectors of eight complex values (represented as pairs of floating point values: [real_0, imag_0, real_1, imag_1, etc.])
51  viennacl::vector<ScalarType> input_vec(16);
52  viennacl::vector<ScalarType> output_vec(16);
53  viennacl::vector<ScalarType> input2_vec(16);
54 
57 
58  for (std::size_t i = 0; i < m.size1(); i++)
59  for (std::size_t s = 0; s < m.size2(); s++)
60  m(i, s) = ScalarType((i + s) / 2);
61 
65  for (std::size_t i = 0; i < input_vec.size(); ++i)
66  {
67  if (i % 2 == 0)
68  {
69  input_vec(i) = ScalarType(i / 2); // even indices represent real part
70  input2_vec(i) = ScalarType(i / 2);
71  } else
72  input_vec(i) = 0; // odd indices represent imaginary part
73  }
74 
78  std::cout << "Computing FFT Matrix" << std::endl;
79  std::cout << "m: " << m << std::endl;
80  std::cout << "o: " << o << std::endl;
81  viennacl::fft(m, o);
82  std::cout << "Done" << std::endl;
83  std::cout << "m: " << m << std::endl;
84  std::cout << "o: " << o << std::endl;
85  std::cout << "Transpose" << std::endl;
86 
88  //viennacl::linalg::transpose(m);
89  std::cout << "m: " << m << std::endl;
90  std::cout << "o: " << o << std::endl;
91 
92  std::cout << "---------------------" << std::endl;
93 
97  std::cout << "Computing FFT bluestein" << std::endl;
98  // Print the vector
99  std::cout << "input_vec: " << input_vec << std::endl;
100  std::cout << "Done" << std::endl;
101  viennacl::linalg::bluestein(input_vec, output_vec, 0);
102  std::cout << "input_vec: " << input_vec << std::endl;
103  std::cout << "output_vec: " << output_vec << std::endl;
104  std::cout << "---------------------" << std::endl;
105 
109  std::cout << "Computing FFT " << std::endl;
110  // Print the vector
111  std::cout << "input_vec: " << input_vec << std::endl;
112  std::cout << "Done" << std::endl;
113  viennacl::fft(input_vec, output_vec);
114  std::cout << "input_vec: " << input_vec << std::endl;
115  std::cout << "output_vec: " << output_vec << std::endl;
116  std::cout << "---------------------" << std::endl;
117 
121  std::cout << "Computing inverse FFT..." << std::endl;
122  //viennacl::ifft(output_vec, input_vec); // either store result into output_vec
123  viennacl::inplace_ifft(output_vec); // or compute in-place
124  std::cout << "input_vec: " << input_vec << std::endl;
125  std::cout << "output_vec: " << output_vec << std::endl;
126  std::cout << "---------------------" << std::endl;
127 
132  std::cout << "Computing real to complex..." << std::endl;
133  std::cout << "input_vec: " << input_vec << std::endl;
134  viennacl::linalg::real_to_complex(input_vec, output_vec, input_vec.size() / 2); // or compute in-place
135  std::cout << "output_vec: " << output_vec << std::endl;
136  std::cout << "---------------------" << std::endl;
137 
138  std::cout << "Computing complex to real..." << std::endl;
139  std::cout << "input_vec: " << input_vec << std::endl;
140  //viennacl::ifft(output_vec, input_vec); // either store result into output_vec
141  viennacl::linalg::complex_to_real(input_vec, output_vec, input_vec.size() / 2); // or compute in-place
142  std::cout << "output_vec: " << output_vec << std::endl;
143  std::cout << "---------------------" << std::endl;
144 
148  std::cout << "Computing multiply complex" << std::endl;
149  // Print the vector
150  std::cout << "input_vec: " << input_vec << std::endl;
151  std::cout << "input2_vec: " << input2_vec << std::endl;
152  viennacl::linalg::multiply_complex(input_vec, input2_vec, output_vec);
153  std::cout << "Done" << std::endl;
154  std::cout << "output_vec: " << output_vec << std::endl;
155  std::cout << "---------------------" << std::endl;
156 
160  std::cout << "!!!! TUTORIAL COMPLETED SUCCESSFULLY !!!!" << std::endl;
161  return EXIT_SUCCESS;
162 }
Implementation of the dense matrix class.
int main()
Definition: bisect.cpp:91
void real_to_complex(viennacl::vector_base< NumericT > const &in, viennacl::vector_base< NumericT > &out, vcl_size_t size)
Create complex vector from real vector (even elements(2*k) = real part, odd elements(2*k+1) = imagina...
void complex_to_real(viennacl::vector_base< NumericT > const &in, viennacl::vector_base< NumericT > &out, vcl_size_t size)
Create real vector from complex vector (even elements(2*k) = real part, odd elements(2*k+1) = imagina...
A dense matrix class.
Definition: forwards.h:375
void bluestein(viennacl::vector< NumericT, AlignmentV > &in, viennacl::vector< NumericT, AlignmentV > &out, vcl_size_t)
Bluestein's algorithm for computing Fourier transformation.
Implementations of Fast Furier Transformation.
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.
void transpose(viennacl::matrix< NumericT, viennacl::row_major, AlignmentV > &input)
Inplace_transpose matrix.
The vector type with operator-overloads and proxy classes is defined here. Linear algebra operations ...
float ScalarType
Definition: fft_1d.cpp:42
All routines related to the Fast Fourier Transform. Experimental.
ScalarType fft(std::vector< ScalarType > &in, std::vector< ScalarType > &out, unsigned int, unsigned int, unsigned int batch_size)
Definition: fft_1d.cpp:719