ViennaCL - The Vienna Computing Library: viennacl/linalg/opencl/direct_solve.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
direct_solve.hpp
Go to the documentation of this file.
1 #ifndef VIENNACL_LINALG_OPENCL_DIRECT_SOLVE_HPP
2 #define VIENNACL_LINALG_OPENCL_DIRECT_SOLVE_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 #include "viennacl/ocl/kernel.hpp"
28 #include "viennacl/ocl/device.hpp"
29 #include "viennacl/ocl/handle.hpp"
30 #include "viennacl/linalg/opencl/kernels/matrix_solve.hpp"
31 #include "viennacl/linalg/opencl/kernels/matrix.hpp"
32
33 namespace viennacl
34 {
35 namespace linalg
36 {
37 namespace opencl
38 {
39
40 namespace detail
41 {
42 inline cl_uint get_option_for_solver_tag(viennacl::linalg::upper_tag) { return 0; }
43 inline cl_uint get_option_for_solver_tag(viennacl::linalg::unit_upper_tag) { return (1 << 0); }
44 inline cl_uint get_option_for_solver_tag(viennacl::linalg::lower_tag) { return (1 << 2); }
45 inline cl_uint get_option_for_solver_tag(viennacl::linalg::unit_lower_tag) { return (1 << 2) | (1 << 0); }
46
47 template<typename MatrixT1, typename MatrixT2, typename KernelT>
48 void inplace_solve_impl(MatrixT1 const & A, MatrixT2 & B, KernelT & k)
49 {
50 viennacl::ocl::enqueue(k(viennacl::traits::opencl_handle(A),
51 cl_uint(viennacl::traits::start1(A)), cl_uint(viennacl::traits::start2(A)),
52 cl_uint(viennacl::traits::stride1(A)), cl_uint(viennacl::traits::stride2(A)),
53 cl_uint(viennacl::traits::size1(A)), cl_uint(viennacl::traits::size2(A)),
54 cl_uint(viennacl::traits::internal_size1(A)), cl_uint(viennacl::traits::internal_size2(A)),
55 viennacl::traits::opencl_handle(B),
56 cl_uint(viennacl::traits::start1(B)), cl_uint(viennacl::traits::start2(B)),
57 cl_uint(viennacl::traits::stride1(B)), cl_uint(viennacl::traits::stride2(B)),
58 cl_uint(viennacl::traits::size1(B)), cl_uint(viennacl::traits::size2(B)),
59 cl_uint(viennacl::traits::internal_size1(B)), cl_uint(viennacl::traits::internal_size2(B))
60 )
61 );
62 }
63 }
64
65
66 //
67 // Note: By convention, all size checks are performed in the calling frontend. No need to double-check here.
68 //
69
71
76 template<typename NumericT, typename SolverTagT>
77 void inplace_solve(matrix_base<NumericT> const & A,
78 matrix_base<NumericT> & B,
79 SolverTagT)
80 {
81 viennacl::ocl::context & ctx = const_cast<viennacl::ocl::context &>(viennacl::traits::opencl_handle(A).context());
82
83 std::string program_name;
84 if (A.row_major() && B.row_major())
85 {
86 typedef viennacl::linalg::opencl::kernels::matrix_solve<NumericT, row_major, row_major> KernelClass;
87 KernelClass::init(ctx);
88 program_name = KernelClass::program_name();
89 }
90 else if (A.row_major() && !B.row_major())
91 {
92 typedef viennacl::linalg::opencl::kernels::matrix_solve<NumericT, row_major, column_major> KernelClass;
93 KernelClass::init(ctx);
94 program_name = KernelClass::program_name();
95 }
96 else if (!A.row_major() && B.row_major())
97 {
98 typedef viennacl::linalg::opencl::kernels::matrix_solve<NumericT, column_major, row_major> KernelClass;
99 KernelClass::init(ctx);
100 program_name = KernelClass::program_name();
101 }
102 else
103 {
104 typedef viennacl::linalg::opencl::kernels::matrix_solve<NumericT, column_major, column_major> KernelClass;
105 KernelClass::init(ctx);
106 program_name = KernelClass::program_name();
107 }
108
109 std::stringstream ss;
110 ss << SolverTagT::name();
111 ss << "_solve";
112
113 viennacl::ocl::kernel & k = ctx.get_kernel(program_name, ss.str());
114
115 k.global_work_size(0, B.size2() * k.local_work_size());
116 detail::inplace_solve_impl(A, B, k);
117 }
118
119
120
121 //
122 // Solve on vector
123 //
124
125 template<typename NumericT, typename SOLVERTAG>
126 void inplace_solve(matrix_base<NumericT> const & A,
127 vector_base<NumericT> & x,
128 SOLVERTAG)
129 {
130 cl_uint options = detail::get_option_for_solver_tag(SOLVERTAG());
131
132 viennacl::ocl::kernel & k = detail::legacy_kernel_for_matrix(A, "triangular_substitute_inplace");
133
134 k.global_work_size(0, k.local_work_size());
135 viennacl::ocl::enqueue(k(viennacl::traits::opencl_handle(A),
136 cl_uint(viennacl::traits::start1(A)), cl_uint(viennacl::traits::start2(A)),
137 cl_uint(viennacl::traits::stride1(A)), cl_uint(viennacl::traits::stride2(A)),
138 cl_uint(viennacl::traits::size1(A)), cl_uint(viennacl::traits::size2(A)),
139 cl_uint(viennacl::traits::internal_size1(A)), cl_uint(viennacl::traits::internal_size2(A)),
140 viennacl::traits::opencl_handle(x),
141 cl_uint(viennacl::traits::start(x)),
142 cl_uint(viennacl::traits::stride(x)),
143 cl_uint(viennacl::traits::size(x)),
144 options
145 )
146 );
147 }
148
149 } //namespace opencl
150 } //namespace linalg
151 } //namespace viennacl
152
153 #endif
device.hpp
Represents an OpenCL device within ViennaCL.
viennacl::traits::stride1
result_of::size_type< matrix_base< NumericT > >::type stride1(matrix_base< NumericT > const &s)
Definition: stride.hpp:55
viennacl::ocl::kernel
Represents an OpenCL kernel within ViennaCL.
Definition: kernel.hpp:58
matrix.hpp
Implementation of the dense matrix class.
viennacl::traits::internal_size1
vcl_size_t internal_size1(matrix_base< NumericT > const &mat)
Helper routine for obtaining the internal number of entries per row of a ViennaCL matrix...
Definition: size.hpp:386
viennacl::ocl::kernel::local_work_size
size_type local_work_size(int index=0) const
Returns the local work size at the respective dimension.
Definition: kernel.hpp:742
viennacl::traits::size1
vcl_size_t size1(MatrixType const &mat)
Generic routine for obtaining the number of rows of a matrix (ViennaCL, uBLAS, etc.)
Definition: size.hpp:163
viennacl::linalg::lower_tag
A tag class representing a lower triangular matrix.
Definition: forwards.h:849
viennacl::ocl::context
Manages an OpenCL context and provides the respective convenience functions for creating buffers...
Definition: context.hpp:55
viennacl::traits::internal_size2
vcl_size_t internal_size2(matrix_base< NumericT > const &mat)
Helper routine for obtaining the internal number of entries per column of a ViennaCL matrix...
Definition: size.hpp:394
viennacl::matrix_base< NumericT >
viennacl::traits::stride
result_of::size_type< viennacl::vector_base< T > >::type stride(viennacl::vector_base< T > const &s)
Definition: stride.hpp:45
viennacl::traits::start1
result_of::size_type< T >::type start1(T const &obj)
Definition: start.hpp:65
viennacl::linalg::opencl::detail::get_option_for_solver_tag
cl_uint get_option_for_solver_tag(viennacl::linalg::upper_tag)
Definition: direct_solve.hpp:42
matrix_solve.hpp
OpenCL kernel file for dense matrix solves with multiple right hand side (BLAS level 3) ...
viennacl::traits::size2
result_of::size_type< MatrixType >::type size2(MatrixType const &mat)
Generic routine for obtaining the number of columns of a matrix (ViennaCL, uBLAS, etc...
Definition: size.hpp:201
viennacl::linalg::opencl::detail::inplace_solve_impl
void inplace_solve_impl(MatrixT1 const &A, MatrixT2 &B, KernelT &k)
Definition: direct_solve.hpp:48
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::traits::start2
result_of::size_type< T >::type start2(T const &obj)
Definition: start.hpp:84
viennacl::ocl::context::get_kernel
viennacl::ocl::kernel & get_kernel(std::string const &program_name, std::string const &kernel_name)
Convenience function for retrieving the kernel of a program directly from the context.
Definition: context.hpp:605
viennacl::linalg::upper_tag
A tag class representing an upper triangular matrix.
Definition: forwards.h:854
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< NumericT >
viennacl::linalg::opencl::kernels::matrix_solve
Main kernel class for the generation of matrix solve kernels.
Definition: matrix_solve.hpp:130
viennacl::matrix_base::size2
size_type size2() const
Returns the number of columns.
Definition: matrix_def.hpp:226
viennacl::traits::stride2
result_of::size_type< matrix_base< NumericT > >::type stride2(matrix_base< NumericT > const &s)
Definition: stride.hpp:65
viennacl::linalg::opencl::inplace_solve
void inplace_solve(matrix_base< NumericT > const &A, matrix_base< NumericT > &B, SolverTagT)
Direct inplace solver for dense triangular systems. Matlab notation: A \ B.
Definition: direct_solve.hpp:77
viennacl::linalg::opencl::detail::legacy_kernel_for_matrix
viennacl::ocl::kernel & legacy_kernel_for_matrix(matrix_base< NumericT > const &M, std::string const &kernel_name)
Definition: matrix_operations.hpp:100
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::unit_lower_tag
A tag class representing a lower triangular matrix with unit diagonal.
Definition: forwards.h:859
viennacl::ocl::kernel::global_work_size
size_type global_work_size(int index=0) const
Returns the global work size at the respective dimension.
Definition: kernel.hpp:751
viennacl::linalg::unit_upper_tag
A tag class representing an upper triangular matrix with unit diagonal.
Definition: forwards.h:864
matrix.hpp
Runtime generation of OpenCL kernels for matrix operations.