ViennaCL - The Vienna Computing Library  1.2.0
vandermonde_matrix.hpp
Go to the documentation of this file.
1 #ifndef VIENNACL_VANDERMONDE_MATRIX_HPP
2 #define VIENNACL_VANDERMONDE_MATRIX_HPP
3 
4 /* =========================================================================
5  Copyright (c) 2010-2011, Institute for Microelectronics,
6  Institute for Analysis and Scientific Computing,
7  TU Wien.
8 
9  -----------------
10  ViennaCL - The Vienna Computing Library
11  -----------------
12 
13  Project Head: Karl Rupp rupp@iue.tuwien.ac.at
14 
15  (A list of authors and contributors can be found in the PDF manual)
16 
17  License: MIT (X11), see file LICENSE in the base directory
18 ============================================================================= */
19 
20 #include <cmath>
21 
26 #include "viennacl/forwards.h"
27 #include "viennacl/vector.hpp"
28 #include "viennacl/ocl/context.hpp"
29 
30 #include "viennacl/fft.hpp"
31 
33 
34 namespace viennacl {
40  template<class SCALARTYPE, unsigned int ALIGNMENT>
42  {
43  public:
48  explicit vandermonde_matrix()
49  {
50  viennacl::linalg::kernels::fft<SCALARTYPE, 1>::init();
51  }
52 
59  explicit vandermonde_matrix(std::size_t rows, std::size_t cols) : elements_(rows)
60  {
61  assert(rows == cols && "Vandermonde matrix must be square in this release!");
62  viennacl::linalg::kernels::fft<SCALARTYPE, 1>::init();
63  }
64 
71  void resize(std::size_t sz, bool preserve = true) {
72  elements_.resize(sz, preserve);
73  }
74 
79  viennacl::ocl::handle<cl_mem> handle() const { return elements_.handle(); }
80 
86  viennacl::vector<SCALARTYPE, ALIGNMENT> const & elements() const { return elements_; }
87 
91  std::size_t size1() const { return elements_.size(); }
92 
96  std::size_t size2() const { return elements_.size(); }
97 
103  std::size_t internal_size() const { return elements_.internal_size(); }
104 
111  entry_proxy<SCALARTYPE> operator()(std::size_t row_index)
112  {
113  return elements_[row_index];
114  }
115 
123  SCALARTYPE operator()(std::size_t row_index, std::size_t col_index) const
124  {
125  assert(row_index < size1() && col_index < size2() && "Invalid access");
126 
127  return pow(elements_[row_index], static_cast<int>(col_index));
128  }
129 
130  private:
132  vandermonde_matrix & operator=(vandermonde_matrix const & t) {}
133 
135  };
136 
143  template <typename SCALARTYPE, unsigned int ALIGNMENT>
144  void copy(std::vector<SCALARTYPE>& cpu_vec, vandermonde_matrix<SCALARTYPE, ALIGNMENT>& gpu_mat)
145  {
146  assert(cpu_vec.size() == gpu_mat.size1() && "Size mismatch");
147  copy(cpu_vec, gpu_mat.elements());
148  }
149 
156  template <typename SCALARTYPE, unsigned int ALIGNMENT>
157  void copy(vandermonde_matrix<SCALARTYPE, ALIGNMENT>& gpu_mat, std::vector<SCALARTYPE>& cpu_vec)
158  {
159  assert(cpu_vec.size() == gpu_mat.size1() && "Size mismatch");
160  copy(gpu_mat.elements(), cpu_vec);
161  }
162 
169  template <typename SCALARTYPE, unsigned int ALIGNMENT, typename MATRIXTYPE>
170  void copy(vandermonde_matrix<SCALARTYPE, ALIGNMENT>& vander_src, MATRIXTYPE& com_dst)
171  {
172  std::size_t size = vander_src.size1();
173  assert(size == com_dst.size1() && "Size mismatch");
174  assert(size == com_dst.size2() && "Size mismatch");
175  std::vector<SCALARTYPE> tmp(size);
176  copy(vander_src, tmp);
177 
178  for(std::size_t i = 0; i < size; i++) {
179  for(std::size_t j = 0; j < size; j++) {
180  com_dst(i, j) = pow(tmp[i], static_cast<int>(j));
181  }
182  }
183  }
184 
191  template <typename SCALARTYPE, unsigned int ALIGNMENT, typename MATRIXTYPE>
192  void copy(MATRIXTYPE& com_src, vandermonde_matrix<SCALARTYPE, ALIGNMENT>& vander_dst)
193  {
194  std::size_t size = vander_dst.size1();
195  assert(size == com_src.size1() && "Size mismatch");
196  assert(size == com_src.size2() && "Size mismatch");
197  std::vector<SCALARTYPE> tmp(size);
198 
199  for(std::size_t i = 0; i < size; i++)
200  tmp[i] = com_src(i, 1);
201 
202  copy(tmp, vander_dst);
203  }
204 
205  /*template <typename SCALARTYPE, unsigned int ALIGNMENT, unsigned int VECTOR_ALIGNMENT>
206  void prod_impl(vandermonde_matrix<SCALARTYPE, ALIGNMENT>& mat,
207  vector<SCALARTYPE, VECTOR_ALIGNMENT>& vec,
208  vector<SCALARTYPE, VECTOR_ALIGNMENT>& result) {
209  assert(mat.size1() == vec.size());
210 
211  fft::vandermonde_prod<SCALARTYPE>(mat.handle(), vec.handle(), result.handle(), mat.size1());
212  } */
213 
219  template<class SCALARTYPE, unsigned int ALIGNMENT>
220  std::ostream & operator<<(std::ostream& s, vandermonde_matrix<SCALARTYPE, ALIGNMENT>& gpu_matrix)
221  {
222  std::size_t size = gpu_matrix.size1();
223  std::vector<SCALARTYPE> tmp(size);
224  copy(gpu_matrix, tmp);
225  s << "[" << size << "," << size << "](\n";
226 
227  for(std::size_t i = 0; i < size; i++) {
228  s << "(";
229  for(std::size_t j = 0; j < size; j++) {
230  s << pow(tmp[i], j);
231  if(j < (size - 1)) s << ",";
232  }
233  s << ")";
234  }
235  s << ")";
236  return s;
237  }
238 
239 }
240 
241 #endif // _VIENNACL_VANDERMONDE_MATRIX_HPP