ViennaCL - The Vienna Computing Library: viennacl/linalg/qr-method-common.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
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"
27 #include "viennacl/linalg/opencl/kernels/svd.hpp"
28 #endif
29
30 #ifdef VIENNACL_WITH_CUDA
31 #include "viennacl/linalg/cuda/matrix_operations.hpp"
32 #endif
33 #include "viennacl/meta/result_of.hpp"
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>
107 void transpose(matrix_base<SCALARTYPE> & A)
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 {
115 viennacl::linalg::opencl::kernels::svd<SCALARTYPE, row_major>::init(ctx);
116 viennacl::ocl::kernel & kernel = viennacl::ocl::get_kernel(viennacl::linalg::opencl::kernels::svd<SCALARTYPE, row_major>::program_name(), SVD_MATRIX_TRANSPOSE_KERNEL);
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 {
126 viennacl::linalg::opencl::kernels::svd<SCALARTYPE, row_major>::init(ctx);
127 viennacl::ocl::kernel & kernel = viennacl::ocl::get_kernel(viennacl::linalg::opencl::kernels::svd<SCALARTYPE, column_major>::program_name(), SVD_MATRIX_TRANSPOSE_KERNEL);
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>
165 void prepare_householder_vector(
166 matrix_base<SCALARTYPE>& A,
167 vector_base<SCALARTYPE>& D,
168 vcl_size_t size,
169 vcl_size_t row_start,
170 vcl_size_t col_start,
171 vcl_size_t 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
EPS
#define EPS
Definition: bisect.cpp:38
device.hpp
Represents an OpenCL device within ViennaCL.
viennacl::ocl::get_kernel
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
viennacl::linalg::SVD_INVERSE_SIGNS_KERNEL
const std::string SVD_INVERSE_SIGNS_KERNEL
Definition: qr-method-common.hpp:51
viennacl::linalg::copy_vec
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.
Definition: matrix_operations.hpp:942
viennacl::ocl::kernel
Represents an OpenCL kernel within ViennaCL.
Definition: kernel.hpp:58
matrix.hpp
Implementation of the dense matrix class.
viennacl::ocl::context
Manages an OpenCL context and provides the respective convenience functions for creating buffers...
Definition: context.hpp:55
viennacl::linalg::SVD_UPDATE_QR_COLUMN_KERNEL
const std::string SVD_UPDATE_QR_COLUMN_KERNEL
Definition: qr-method-common.hpp:54
viennacl::matrix_base
Definition: matrix_def.hpp:103
viennacl::linalg::SVD_HOUSEHOLDER_UPDATE_A_RIGHT_KERNEL
const std::string SVD_HOUSEHOLDER_UPDATE_A_RIGHT_KERNEL
Definition: qr-method-common.hpp:56
viennacl::linalg::detail::cdiv
void cdiv(T xr, T xi, T yr, T yi, T &cdivr, T &cdivi)
Definition: qr-method-common.hpp:142
viennacl::linalg::SVD_HOUSEHOLDER_UPDATE_QR_KERNEL
const std::string SVD_HOUSEHOLDER_UPDATE_QR_KERNEL
Definition: qr-method-common.hpp:49
viennacl::linalg::detail::transpose
void transpose(matrix_base< SCALARTYPE > &A)
Definition: qr-method-common.hpp:107
viennacl::linalg::opencl::kernels::svd::init
static void init(viennacl::ocl::context &ctx)
Definition: svd.hpp:652
viennacl::linalg::detail::prepare_householder_vector
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)
Definition: qr-method-common.hpp:165
viennacl::linalg::detail::normalize
void normalize(VectorType &x, vcl_size_t size)
Definition: qr-method-common.hpp:87
svd.hpp
OpenCL kernel file for singular value decomposition.
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::opencl::kernels::svd
Main kernel class for generating OpenCL kernels for singular value decomposition of dense matrices...
Definition: svd.hpp:644
viennacl::vector_base::begin
iterator begin()
Returns an iterator pointing to the beginning of the vector (STL like)
Definition: vector.hpp:841
handle.hpp
Implementation of a smart-pointer-like class for handling OpenCL handles.
viennacl::traits::start
result_of::size_type< T >::type start(T const &obj)
Definition: start.hpp:44
viennacl::vector_base
Common base class for dense vectors, vector ranges, and vector slices.
Definition: vector_def.hpp:104
viennacl::linalg::detail::householder_vector
void householder_vector(VectorType &v, vcl_size_t start)
Definition: qr-method-common.hpp:97
viennacl::linalg::SVD_HOUSEHOLDER_UPDATE_A_LEFT_KERNEL
const std::string SVD_HOUSEHOLDER_UPDATE_A_LEFT_KERNEL
Definition: qr-method-common.hpp:55
viennacl::vcl_size_t
std::size_t vcl_size_t
Definition: forwards.h:75
viennacl::linalg::detail::pythag
SCALARTYPE pythag(SCALARTYPE a, SCALARTYPE b)
Definition: qr-method-common.hpp:65
matrix_operations.hpp
Implementations of dense matrix related operations, including matrix-vector products, using CUDA.
viennacl::ocl::enqueue
void enqueue(KernelType &k, viennacl::ocl::command_queue const &queue)
Enqueues a kernel in the provided queue.
Definition: enqueue.hpp:50
kernel.hpp
Representation of an OpenCL kernel in ViennaCL.
vector.hpp
The vector type with operator-overloads and proxy classes is defined here. Linear algebra operations ...
viennacl::matrix_base::row_major
bool row_major() const
Definition: matrix_def.hpp:248
viennacl::linalg::SVD_HOUSEHOLDER_UPDATE_QL_KERNEL
const std::string SVD_HOUSEHOLDER_UPDATE_QL_KERNEL
Definition: qr-method-common.hpp:57
ScalarType
float ScalarType
Definition: fft_1d.cpp:42
viennacl::linalg::SVD_FINAL_ITER_UPDATE_KERNEL
const std::string SVD_FINAL_ITER_UPDATE_KERNEL
Definition: qr-method-common.hpp:53
viennacl::linalg::SVD_MATRIX_TRANSPOSE_KERNEL
const std::string SVD_MATRIX_TRANSPOSE_KERNEL
Definition: qr-method-common.hpp:50
viennacl::matrix_base::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
viennacl::matrix_base::internal_size1
size_type internal_size1() const
Returns the internal number of rows. Usually required for launching OpenCL kernels only...
Definition: matrix_def.hpp:238
viennacl::linalg::detail::norm_lcl
VectorType::value_type norm_lcl(VectorType const &x, vcl_size_t size)
Definition: qr-method-common.hpp:78
result_of.hpp
A collection of compile time type deductions.
viennacl::linalg::SVD_GIVENS_PREV_KERNEL
const std::string SVD_GIVENS_PREV_KERNEL
Definition: qr-method-common.hpp:52
viennacl::vcl_ptrdiff_t
std::ptrdiff_t vcl_ptrdiff_t
Definition: forwards.h:76
viennacl::linalg::detail::sign
SCALARTYPE sign(SCALARTYPE val)
Definition: qr-method-common.hpp:71
viennacl::fast_copy
void fast_copy(const const_vector_iterator< SCALARTYPE, ALIGNMENT > &gpu_begin, const const_vector_iterator< SCALARTYPE, ALIGNMENT > &gpu_end, CPU_ITERATOR cpu_begin)