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
qr-method-common.hpp
Go to the documentation of this file.
1 #ifndef VIENNACL_LINALG_QR_METHOD_COMMON_HPP
2 #define VIENNACL_LINALG_QR_METHOD_COMMON_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 
21 #include <cmath>
22 
23 #ifdef VIENNACL_WITH_OPENCL
24 #include "viennacl/ocl/device.hpp"
25 #include "viennacl/ocl/handle.hpp"
26 #include "viennacl/ocl/kernel.hpp"
28 #endif
29 
30 #ifdef VIENNACL_WITH_CUDA
32 #endif
34 #include "viennacl/vector.hpp"
35 #include "viennacl/matrix.hpp"
36 
37 //#include <boost/numeric/ublas/vector.hpp>
38 //#include <boost/numeric/ublas/io.hpp>
39 
44 namespace viennacl
45 {
46 namespace linalg
47 {
48 
49 const std::string SVD_HOUSEHOLDER_UPDATE_QR_KERNEL = "house_update_QR";
50 const std::string SVD_MATRIX_TRANSPOSE_KERNEL = "transpose_inplace";
51 const std::string SVD_INVERSE_SIGNS_KERNEL = "inverse_signs";
52 const std::string SVD_GIVENS_PREV_KERNEL = "givens_prev";
53 const std::string SVD_FINAL_ITER_UPDATE_KERNEL = "final_iter_update";
54 const std::string SVD_UPDATE_QR_COLUMN_KERNEL = "update_qr_column";
55 const std::string SVD_HOUSEHOLDER_UPDATE_A_LEFT_KERNEL = "house_update_A_left";
56 const std::string SVD_HOUSEHOLDER_UPDATE_A_RIGHT_KERNEL = "house_update_A_right";
57 const std::string SVD_HOUSEHOLDER_UPDATE_QL_KERNEL = "house_update_QL";
58 
59 namespace detail
60 {
61 static const double EPS = 1e-10;
62 static const vcl_size_t ITER_MAX = 50;
63 
64 template <typename SCALARTYPE>
65 SCALARTYPE pythag(SCALARTYPE a, SCALARTYPE b)
66 {
67  return std::sqrt(a*a + b*b);
68 }
69 
70 template <typename SCALARTYPE>
71 SCALARTYPE sign(SCALARTYPE val)
72 {
73  return (val >= 0) ? SCALARTYPE(1) : SCALARTYPE(-1);
74 }
75 
76 // DEPRECATED: Replace with viennacl::linalg::norm_2
77 template <typename VectorType>
78 typename VectorType::value_type norm_lcl(VectorType const & x, vcl_size_t size)
79 {
80  typename VectorType::value_type x_norm = 0.0;
81  for(vcl_size_t i = 0; i < size; i++)
82  x_norm += std::pow(x[i], 2);
83  return std::sqrt(x_norm);
84 }
85 
86 template <typename VectorType>
87 void normalize(VectorType & x, vcl_size_t size)
88 {
89  typename VectorType::value_type x_norm = norm_lcl(x, size);
90  for(vcl_size_t i = 0; i < size; i++)
91  x[i] /= x_norm;
92 }
93 
94 
95 
96 template <typename VectorType>
97 void householder_vector(VectorType & v, vcl_size_t start)
98 {
99  typedef typename VectorType::value_type ScalarType;
100  ScalarType x_norm = norm_lcl(v, v.size());
101  ScalarType alpha = -sign(v[start]) * x_norm;
102  v[start] += alpha;
103  normalize(v, v.size());
104 }
105 
106 template <typename SCALARTYPE>
108 {
109  (void)A;
110 #ifdef VIENNACL_WITH_OPENCL
111 
112  viennacl::ocl::context & ctx = const_cast<viennacl::ocl::context &>(viennacl::traits::opencl_handle(A).context());
113  if(A.row_major())
114  {
117 
118  viennacl::ocl::enqueue(kernel(A,
119  static_cast<cl_uint>(A.internal_size1()),
120  static_cast<cl_uint>(A.internal_size2())
121  )
122  );
123  }
124  else
125  {
128 
129  viennacl::ocl::enqueue(kernel(A,
130  static_cast<cl_uint>(A.internal_size1()),
131  static_cast<cl_uint>(A.internal_size2())
132  )
133  );
134  }
135 
136 #endif
137 }
138 
139 
140 
141 template <typename T>
142 void cdiv(T xr, T xi, T yr, T yi, T& cdivr, T& cdivi)
143 {
144  // Complex scalar division.
145  T r;
146  T d;
147  if (std::fabs(yr) > std::fabs(yi))
148  {
149  r = yi / yr;
150  d = yr + r * yi;
151  cdivr = (xr + r * xi) / d;
152  cdivi = (xi - r * xr) / d;
153  }
154  else
155  {
156  r = yr / yi;
157  d = yi + r * yr;
158  cdivr = (r * xr + xi) / d;
159  cdivi = (r * xi - xr) / d;
160  }
161 }
162 
163 
164 template<typename SCALARTYPE>
169  vcl_size_t row_start,
170  vcl_size_t col_start,
172  bool is_column
173  )
174 {
175  //boost::numeric::ublas::vector<SCALARTYPE> tmp = boost::numeric::ublas::scalar_vector<SCALARTYPE>(size, 0);
176  std::vector<SCALARTYPE> tmp(size);
177  copy_vec(A, D, row_start, col_start, is_column);
178  fast_copy(D.begin(), D.begin() + vcl_ptrdiff_t(size - start), tmp.begin() + vcl_ptrdiff_t(start));
179 
180  detail::householder_vector(tmp, start);
181  fast_copy(tmp, D);
182 }
183 
184 } //detail
185 }
186 }
187 
188 #endif
#define EPS
Definition: bisect.cpp:38
Represents an OpenCL device within ViennaCL.
viennacl::ocl::kernel & get_kernel(std::string const &prog_name, std::string const &kernel_name)
Convenience function for getting the kernel for a particular program from the current active context...
Definition: backend.hpp:339
const std::string SVD_INVERSE_SIGNS_KERNEL
void copy_vec(matrix_base< SCALARTYPE > &A, vector_base< SCALARTYPE > &V, vcl_size_t row_start, vcl_size_t col_start, bool copy_col)
This function copies a row or a column from a matrix to a vector.
Represents an OpenCL kernel within ViennaCL.
Definition: kernel.hpp:58
Implementation of the dense matrix class.
Manages an OpenCL context and provides the respective convenience functions for creating buffers...
Definition: context.hpp:55
const std::string SVD_UPDATE_QR_COLUMN_KERNEL
const std::string SVD_HOUSEHOLDER_UPDATE_A_RIGHT_KERNEL
void cdiv(T xr, T xi, T yr, T yi, T &cdivr, T &cdivi)
const std::string SVD_HOUSEHOLDER_UPDATE_QR_KERNEL
void transpose(matrix_base< SCALARTYPE > &A)
static void init(viennacl::ocl::context &ctx)
Definition: svd.hpp:652
void prepare_householder_vector(matrix_base< SCALARTYPE > &A, vector_base< SCALARTYPE > &D, vcl_size_t size, vcl_size_t row_start, vcl_size_t col_start, vcl_size_t start, bool is_column)
void normalize(VectorType &x, vcl_size_t size)
OpenCL kernel file for singular value decomposition.
vcl_size_t size(VectorType const &vec)
Generic routine for obtaining the size of a vector (ViennaCL, uBLAS, etc.)
Definition: size.hpp:239
Main kernel class for generating OpenCL kernels for singular value decomposition of dense matrices...
Definition: svd.hpp:644
iterator begin()
Returns an iterator pointing to the beginning of the vector (STL like)
Definition: vector.hpp:841
Implementation of a smart-pointer-like class for handling OpenCL handles.
result_of::size_type< T >::type start(T const &obj)
Definition: start.hpp:44
Common base class for dense vectors, vector ranges, and vector slices.
Definition: vector_def.hpp:104
void householder_vector(VectorType &v, vcl_size_t start)
const std::string SVD_HOUSEHOLDER_UPDATE_A_LEFT_KERNEL
std::size_t vcl_size_t
Definition: forwards.h:75
SCALARTYPE pythag(SCALARTYPE a, SCALARTYPE b)
Implementations of dense matrix related operations, including matrix-vector products, using CUDA.
void enqueue(KernelType &k, viennacl::ocl::command_queue const &queue)
Enqueues a kernel in the provided queue.
Definition: enqueue.hpp:50
Representation of an OpenCL kernel in ViennaCL.
The vector type with operator-overloads and proxy classes is defined here. Linear algebra operations ...
bool row_major() const
Definition: matrix_def.hpp:248
const std::string SVD_HOUSEHOLDER_UPDATE_QL_KERNEL
float ScalarType
Definition: fft_1d.cpp:42
const std::string SVD_FINAL_ITER_UPDATE_KERNEL
const std::string SVD_MATRIX_TRANSPOSE_KERNEL
size_type internal_size2() const
Returns the internal number of columns. Usually required for launching OpenCL kernels only...
Definition: matrix_def.hpp:240
size_type internal_size1() const
Returns the internal number of rows. Usually required for launching OpenCL kernels only...
Definition: matrix_def.hpp:238
VectorType::value_type norm_lcl(VectorType const &x, vcl_size_t size)
A collection of compile time type deductions.
const std::string SVD_GIVENS_PREV_KERNEL
std::ptrdiff_t vcl_ptrdiff_t
Definition: forwards.h:76
SCALARTYPE sign(SCALARTYPE val)
void fast_copy(const const_vector_iterator< SCALARTYPE, ALIGNMENT > &gpu_begin, const const_vector_iterator< SCALARTYPE, ALIGNMENT > &gpu_end, CPU_ITERATOR cpu_begin)