ViennaCL - The Vienna Computing Library  1.7.0
Free open-source GPU-accelerated linear algebra and solver library.
sparse_matrix_operations.hpp
Go to the documentation of this file.
1 #ifndef VIENNACL_LINALG_SPARSE_MATRIX_OPERATIONS_HPP_
2 #define VIENNACL_LINALG_SPARSE_MATRIX_OPERATIONS_HPP_
3 
4 /* =========================================================================
5  Copyright (c) 2010-2015, 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/forwards.h"
26 #include "viennacl/scalar.hpp"
27 #include "viennacl/vector.hpp"
28 #include "viennacl/matrix.hpp"
29 #include "viennacl/tools/tools.hpp"
31 
32 #ifdef VIENNACL_WITH_OPENCL
34 #endif
35 
36 #ifdef VIENNACL_WITH_CUDA
38 #endif
39 
40 namespace viennacl
41 {
42  namespace linalg
43  {
44 
45  namespace detail
46  {
47 
48  template<typename SparseMatrixType, typename SCALARTYPE, unsigned int VEC_ALIGNMENT>
50  row_info(SparseMatrixType const & mat,
52  row_info_types info_selector)
53  {
54  switch (viennacl::traits::handle(mat).get_active_handle_id())
55  {
57  viennacl::linalg::host_based::detail::row_info(mat, vec, info_selector);
58  break;
59 #ifdef VIENNACL_WITH_OPENCL
61  viennacl::linalg::opencl::detail::row_info(mat, vec, info_selector);
62  break;
63 #endif
64 #ifdef VIENNACL_WITH_CUDA
66  viennacl::linalg::cuda::detail::row_info(mat, vec, info_selector);
67  break;
68 #endif
70  throw memory_exception("not initialised!");
71  default:
72  throw memory_exception("not implemented");
73  }
74  }
75 
76  }
77 
78 
79 
80  // A * x
81 
90  template<typename SparseMatrixType, class ScalarType>
92  prod_impl(const SparseMatrixType & mat,
95  {
96  assert( (mat.size1() == result.size()) && bool("Size check failed for compressed matrix-vector product: size1(mat) != size(result)"));
97  assert( (mat.size2() == vec.size()) && bool("Size check failed for compressed matrix-vector product: size2(mat) != size(x)"));
98 
100  {
102  viennacl::linalg::host_based::prod_impl(mat, vec, result);
103  break;
104 #ifdef VIENNACL_WITH_OPENCL
106  viennacl::linalg::opencl::prod_impl(mat, vec, result);
107  break;
108 #endif
109 #ifdef VIENNACL_WITH_CUDA
111  viennacl::linalg::cuda::prod_impl(mat, vec, result);
112  break;
113 #endif
115  throw memory_exception("not initialised!");
116  default:
117  throw memory_exception("not implemented");
118  }
119  }
120 
121 
122  // A * B
131  template<typename SparseMatrixType, class ScalarType>
133  prod_impl(const SparseMatrixType & sp_mat,
134  const viennacl::matrix_base<ScalarType> & d_mat,
136  {
137  assert( (sp_mat.size1() == result.size1()) && bool("Size check failed for compressed matrix - dense matrix product: size1(sp_mat) != size1(result)"));
138  assert( (sp_mat.size2() == d_mat.size1()) && bool("Size check failed for compressed matrix - dense matrix product: size2(sp_mat) != size1(d_mat)"));
139 
141  {
143  viennacl::linalg::host_based::prod_impl(sp_mat, d_mat, result);
144  break;
145 #ifdef VIENNACL_WITH_OPENCL
147  viennacl::linalg::opencl::prod_impl(sp_mat, d_mat, result);
148  break;
149 #endif
150 #ifdef VIENNACL_WITH_CUDA
152  viennacl::linalg::cuda::prod_impl(sp_mat, d_mat, result);
153  break;
154 #endif
156  throw memory_exception("not initialised!");
157  default:
158  throw memory_exception("not implemented");
159  }
160  }
161 
162  // A * transpose(B)
171  template<typename SparseMatrixType, class ScalarType>
173  prod_impl(const SparseMatrixType & sp_mat,
176  viennacl::op_trans>& d_mat,
178  {
179  assert( (sp_mat.size1() == result.size1()) && bool("Size check failed for compressed matrix - dense matrix product: size1(sp_mat) != size1(result)"));
180  assert( (sp_mat.size2() == d_mat.size1()) && bool("Size check failed for compressed matrix - dense matrix product: size2(sp_mat) != size1(d_mat)"));
181 
183  {
185  viennacl::linalg::host_based::prod_impl(sp_mat, d_mat, result);
186  break;
187 #ifdef VIENNACL_WITH_OPENCL
189  viennacl::linalg::opencl::prod_impl(sp_mat, d_mat, result);
190  break;
191 #endif
192 #ifdef VIENNACL_WITH_CUDA
194  viennacl::linalg::cuda::prod_impl(sp_mat, d_mat, result);
195  break;
196 #endif
198  throw memory_exception("not initialised!");
199  default:
200  throw memory_exception("not implemented");
201  }
202  }
203 
204  // A * B with both A and B sparse
205 
215  template<typename NumericT>
216  void
220  {
221  assert( (A.size2() == B.size1()) && bool("Size check failed for sparse matrix-matrix product: size2(A) != size1(B)"));
222  assert( (C.size1() == 0 || C.size1() == A.size1()) && bool("Size check failed for sparse matrix-matrix product: size1(A) != size1(C)"));
223  assert( (C.size2() == 0 || C.size2() == B.size2()) && bool("Size check failed for sparse matrix-matrix product: size2(B) != size2(B)"));
224 
226  {
229  break;
230 #ifdef VIENNACL_WITH_OPENCL
233  break;
234 #endif
235 #ifdef VIENNACL_WITH_CUDA
238  break;
239 #endif
241  throw memory_exception("not initialised!");
242  default:
243  throw memory_exception("not implemented");
244  }
245  }
246 
247 
254  template<typename SparseMatrixType, class ScalarType, typename SOLVERTAG>
256  inplace_solve(const SparseMatrixType & mat,
258  SOLVERTAG tag)
259  {
260  assert( (mat.size1() == mat.size2()) && bool("Size check failed for triangular solve on compressed matrix: size1(mat) != size2(mat)"));
261  assert( (mat.size2() == vec.size()) && bool("Size check failed for compressed matrix-vector product: size2(mat) != size(x)"));
262 
264  {
267  break;
268 #ifdef VIENNACL_WITH_OPENCL
271  break;
272 #endif
273 #ifdef VIENNACL_WITH_CUDA
276  break;
277 #endif
279  throw memory_exception("not initialised!");
280  default:
281  throw memory_exception("not implemented");
282  }
283  }
284 
285 
292  template<typename SparseMatrixType, class ScalarType, typename SOLVERTAG>
296  SOLVERTAG tag)
297  {
298  assert( (mat.size1() == mat.size2()) && bool("Size check failed for triangular solve on transposed compressed matrix: size1(mat) != size2(mat)"));
299  assert( (mat.size1() == vec.size()) && bool("Size check failed for transposed compressed matrix triangular solve: size1(mat) != size(x)"));
300 
301  switch (viennacl::traits::handle(mat.lhs()).get_active_handle_id())
302  {
305  break;
306 #ifdef VIENNACL_WITH_OPENCL
309  break;
310 #endif
311 #ifdef VIENNACL_WITH_CUDA
314  break;
315 #endif
317  throw memory_exception("not initialised!");
318  default:
319  throw memory_exception("not implemented");
320  }
321  }
322 
323 
324 
325  namespace detail
326  {
327 
328  template<typename SparseMatrixType, class ScalarType, typename SOLVERTAG>
331  viennacl::backend::mem_handle const & block_index_array, vcl_size_t num_blocks,
332  viennacl::vector_base<ScalarType> const & mat_diagonal,
334  SOLVERTAG tag)
335  {
336  assert( (mat.size1() == mat.size2()) && bool("Size check failed for triangular solve on transposed compressed matrix: size1(mat) != size2(mat)"));
337  assert( (mat.size1() == vec.size()) && bool("Size check failed for transposed compressed matrix triangular solve: size1(mat) != size(x)"));
338 
339  switch (viennacl::traits::handle(mat.lhs()).get_active_handle_id())
340  {
342  viennacl::linalg::host_based::detail::block_inplace_solve(mat, block_index_array, num_blocks, mat_diagonal, vec, tag);
343  break;
344  #ifdef VIENNACL_WITH_OPENCL
346  viennacl::linalg::opencl::detail::block_inplace_solve(mat, block_index_array, num_blocks, mat_diagonal, vec, tag);
347  break;
348  #endif
349  #ifdef VIENNACL_WITH_CUDA
351  viennacl::linalg::cuda::detail::block_inplace_solve(mat, block_index_array, num_blocks, mat_diagonal, vec, tag);
352  break;
353  #endif
355  throw memory_exception("not initialised!");
356  default:
357  throw memory_exception("not implemented");
358  }
359  }
360 
361 
362  }
363 
364 
365 
366  } //namespace linalg
367 
368 
370  template<typename M1>
372  matrix_expression< const M1, const M1, op_trans>
373  >::type
374  trans(const M1 & mat)
375  {
377  }
378 
379  //free functions:
385  template<typename SCALARTYPE, typename SparseMatrixType>
389  const viennacl::vector_expression< const SparseMatrixType, const viennacl::vector_base<SCALARTYPE>, viennacl::op_prod> & proxy)
390  {
391  assert(proxy.lhs().size1() == result.size() && bool("Dimensions for addition of sparse matrix-vector product to vector don't match!"));
392  vector<SCALARTYPE> temp(proxy.lhs().size1());
393  viennacl::linalg::prod_impl(proxy.lhs(), proxy.rhs(), temp);
394  result += temp;
395  return result;
396  }
397 
403  template<typename SCALARTYPE, typename SparseMatrixType>
407  const viennacl::vector_expression< const SparseMatrixType, const viennacl::vector_base<SCALARTYPE>, viennacl::op_prod> & proxy)
408  {
409  assert(proxy.lhs().size1() == result.size() && bool("Dimensions for addition of sparse matrix-vector product to vector don't match!"));
410  vector<SCALARTYPE> temp(proxy.lhs().size1());
411  viennacl::linalg::prod_impl(proxy.lhs(), proxy.rhs(), temp);
412  result += temp;
413  return result;
414  }
415 
416 } //namespace viennacl
417 
418 
419 #endif
const vcl_size_t & size2() const
Returns the number of columns.
Simple enable-if variant that uses the SFINAE pattern.
Definition: enable_if.hpp:30
void inplace_solve(matrix_base< NumericT > const &A, matrix_base< NumericT > &B, SolverTagT tag)
Direct inplace solver for triangular systems with multiple right hand sides, i.e. A \ B (MATLAB notat...
void inplace_solve(const matrix_base< NumericT > &A, matrix_base< NumericT > &B, SolverTagT)
Direct inplace solver for triangular systems with multiple right hand sides, i.e. A \ B (MATLAB notat...
viennacl::enable_if< viennacl::is_any_sparse_matrix< M1 >::value, matrix_expression< const M1, const M1, op_trans > >::type trans(const M1 &mat)
Returns an expression template class representing a transposed matrix.
Exception class in case of memory errors.
Definition: forwards.h:572
const vcl_size_t & size1() const
Returns the number of rows.
Implementation of the dense matrix class.
Various little tools used here and there in ViennaCL.
Implementations of operations using sparse matrices on the CPU using a single thread or OpenMP...
void inplace_solve(matrix_base< NumericT > const &A, matrix_base< NumericT > &B, SolverTagT)
Direct inplace solver for triangular systems with multiple right hand sides, i.e. A \ B (MATLAB notat...
Expression template class for representing a tree of expressions which ultimately result in a matrix...
Definition: forwards.h:341
This file provides the forward declarations for the main types used within ViennaCL.
void row_info(compressed_matrix< NumericT, AligmentV > const &mat, vector_base< NumericT > &vec, viennacl::linalg::detail::row_info_types info_selector)
void prod_impl(const matrix_base< NumericT > &mat, bool mat_transpose, const vector_base< NumericT > &vec, vector_base< NumericT > &result)
Carries out matrix-vector multiplication.
An expression template class that represents a binary operation that yields a vector.
Definition: forwards.h:239
vcl_size_t size1() const
Returns the size of the result vector.
Definition: matrix.hpp:72
viennacl::vector< NumericT > operator-(const vector_base< NumericT > &v1, const vector_expression< const matrix_base< NumericT >, const vector_base< NumericT >, op_prod > &proxy)
Implementation of the operation 'result = v1 - A * v2', where A is a matrix.
Main namespace in ViennaCL. Holds all the basic types such as vector, matrix, etc. and defines operations upon them.
Definition: cpu_ram.hpp:34
Definition: blas3.hpp:36
void prod_impl(const matrix_base< NumericT > &mat, bool trans, const vector_base< NumericT > &vec, vector_base< NumericT > &result)
Carries out matrix-vector multiplication.
Implementations of operations using sparse matrices using CUDA.
vcl_size_t size2() const
Definition: matrix.hpp:73
std::size_t vcl_size_t
Definition: forwards.h:75
void block_inplace_solve(const matrix_expression< const compressed_matrix< NumericT, AlignmentV >, const compressed_matrix< NumericT, AlignmentV >, op_trans > &L, viennacl::backend::mem_handle const &, vcl_size_t, vector_base< NumericT > const &, vector_base< NumericT > &vec, viennacl::linalg::unit_lower_tag)
void block_inplace_solve(const matrix_expression< const compressed_matrix< NumericT, AlignmentV >, const compressed_matrix< NumericT, AlignmentV >, op_trans > &L, viennacl::backend::mem_handle const &block_indices, vcl_size_t num_blocks, vector_base< NumericT > const &, vector_base< NumericT > &vec, viennacl::linalg::unit_lower_tag)
size_type size1() const
Returns the number of rows.
Definition: matrix_def.hpp:224
Implementations of operations using sparse matrices and OpenCL.
void inplace_solve(matrix_base< NumericT > const &A, matrix_base< NumericT > &B, SolverTagT)
Direct inplace solver for dense triangular systems. Matlab notation: A \ B.
viennacl::vector< NumericT > operator+(const vector_base< NumericT > &v1, const vector_expression< const matrix_base< NumericT >, const vector_base< NumericT >, op_prod > &proxy)
Implementation of the operation 'result = v1 + A * v2', where A is a matrix.
A tag class representing matrix-vector products and element-wise multiplications. ...
Definition: forwards.h:94
The vector type with operator-overloads and proxy classes is defined here. Linear algebra operations ...
size_type size() const
Returns the length of the vector (cf. std::vector)
Definition: vector_def.hpp:118
Main abstraction class for multiple memory domains. Represents a buffer in either main RAM...
Definition: mem_handle.hpp:89
A tag class representing transposed matrices.
Definition: forwards.h:220
void row_info(compressed_matrix< NumericT, AlignmentV > const &mat, vector_base< NumericT > &vec, viennacl::linalg::detail::row_info_types info_selector)
void prod_impl(const matrix_base< NumericT > &mat, const vector_base< NumericT > &vec, vector_base< NumericT > &result)
Carries out matrix-vector multiplication.
viennacl::backend::mem_handle & handle(T &obj)
Returns the generic memory handle of an object. Non-const version.
Definition: handle.hpp:41
void block_inplace_solve(const matrix_expression< const compressed_matrix< NumericT, AlignmentV >, const compressed_matrix< NumericT, AlignmentV >, op_trans > &L, viennacl::backend::mem_handle const &block_indices, vcl_size_t num_blocks, vector_base< NumericT > const &, vector_base< NumericT > &x, viennacl::linalg::unit_lower_tag)
LHS & lhs() const
Get left hand side operand.
Definition: matrix.hpp:66
viennacl::enable_if< viennacl::is_any_sparse_matrix< SparseMatrixType >::value >::type row_info(SparseMatrixType const &mat, vector< SCALARTYPE, VEC_ALIGNMENT > &vec, row_info_types info_selector)
Implementation of the ViennaCL scalar class.
void prod_impl(const matrix_base< NumericT > &A, bool trans_A, const vector_base< NumericT > &vec, vector_base< NumericT > &result)
Carries out matrix-vector multiplication.
void row_info(compressed_matrix< NumericT, AlignmentV > const &A, vector_base< NumericT > &x, viennacl::linalg::detail::row_info_types info_selector)
memory_types get_active_handle_id() const
Returns an ID for the currently active memory buffer. Other memory buffers might contain old or no da...
Definition: mem_handle.hpp:118
viennacl::enable_if< viennacl::is_any_sparse_matrix< SparseMatrixType >::value >::type block_inplace_solve(const matrix_expression< const SparseMatrixType, const SparseMatrixType, op_trans > &mat, viennacl::backend::mem_handle const &block_index_array, vcl_size_t num_blocks, viennacl::vector_base< ScalarType > const &mat_diagonal, viennacl::vector_base< ScalarType > &vec, SOLVERTAG tag)