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
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-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/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,
94  ScalarType alpha,
96  ScalarType beta)
97  {
98  assert( (mat.size1() == result.size()) && bool("Size check failed for compressed matrix-vector product: size1(mat) != size(result)"));
99  assert( (mat.size2() == vec.size()) && bool("Size check failed for compressed matrix-vector product: size2(mat) != size(x)"));
100 
102  {
104  viennacl::linalg::host_based::prod_impl(mat, vec, alpha, result, beta);
105  break;
106 #ifdef VIENNACL_WITH_OPENCL
108  viennacl::linalg::opencl::prod_impl(mat, vec, alpha, result, beta);
109  break;
110 #endif
111 #ifdef VIENNACL_WITH_CUDA
113  viennacl::linalg::cuda::prod_impl(mat, vec, alpha, result, beta);
114  break;
115 #endif
117  throw memory_exception("not initialised!");
118  default:
119  throw memory_exception("not implemented");
120  }
121  }
122 
123 
124  // A * B
133  template<typename SparseMatrixType, class ScalarType>
135  prod_impl(const SparseMatrixType & sp_mat,
136  const viennacl::matrix_base<ScalarType> & d_mat,
138  {
139  assert( (sp_mat.size1() == result.size1()) && bool("Size check failed for compressed matrix - dense matrix product: size1(sp_mat) != size1(result)"));
140  assert( (sp_mat.size2() == d_mat.size1()) && bool("Size check failed for compressed matrix - dense matrix product: size2(sp_mat) != size1(d_mat)"));
141 
143  {
145  viennacl::linalg::host_based::prod_impl(sp_mat, d_mat, result);
146  break;
147 #ifdef VIENNACL_WITH_OPENCL
149  viennacl::linalg::opencl::prod_impl(sp_mat, d_mat, result);
150  break;
151 #endif
152 #ifdef VIENNACL_WITH_CUDA
154  viennacl::linalg::cuda::prod_impl(sp_mat, d_mat, result);
155  break;
156 #endif
158  throw memory_exception("not initialised!");
159  default:
160  throw memory_exception("not implemented");
161  }
162  }
163 
164  // A * transpose(B)
173  template<typename SparseMatrixType, class ScalarType>
175  prod_impl(const SparseMatrixType & sp_mat,
178  viennacl::op_trans>& d_mat,
180  {
181  assert( (sp_mat.size1() == result.size1()) && bool("Size check failed for compressed matrix - dense matrix product: size1(sp_mat) != size1(result)"));
182  assert( (sp_mat.size2() == d_mat.size1()) && bool("Size check failed for compressed matrix - dense matrix product: size2(sp_mat) != size1(d_mat)"));
183 
185  {
187  viennacl::linalg::host_based::prod_impl(sp_mat, d_mat, result);
188  break;
189 #ifdef VIENNACL_WITH_OPENCL
191  viennacl::linalg::opencl::prod_impl(sp_mat, d_mat, result);
192  break;
193 #endif
194 #ifdef VIENNACL_WITH_CUDA
196  viennacl::linalg::cuda::prod_impl(sp_mat, d_mat, result);
197  break;
198 #endif
200  throw memory_exception("not initialised!");
201  default:
202  throw memory_exception("not implemented");
203  }
204  }
205 
206  // A * B with both A and B sparse
207 
217  template<typename NumericT>
218  void
222  {
223  assert( (A.size2() == B.size1()) && bool("Size check failed for sparse matrix-matrix product: size2(A) != size1(B)"));
224  assert( (C.size1() == 0 || C.size1() == A.size1()) && bool("Size check failed for sparse matrix-matrix product: size1(A) != size1(C)"));
225  assert( (C.size2() == 0 || C.size2() == B.size2()) && bool("Size check failed for sparse matrix-matrix product: size2(B) != size2(B)"));
226 
228  {
231  break;
232 #ifdef VIENNACL_WITH_OPENCL
235  break;
236 #endif
237 #ifdef VIENNACL_WITH_CUDA
240  break;
241 #endif
243  throw memory_exception("not initialised!");
244  default:
245  throw memory_exception("not implemented");
246  }
247  }
248 
249 
256  template<typename SparseMatrixType, class ScalarType, typename SOLVERTAG>
258  inplace_solve(const SparseMatrixType & mat,
260  SOLVERTAG tag)
261  {
262  assert( (mat.size1() == mat.size2()) && bool("Size check failed for triangular solve on compressed matrix: size1(mat) != size2(mat)"));
263  assert( (mat.size2() == vec.size()) && bool("Size check failed for compressed matrix-vector product: size2(mat) != size(x)"));
264 
266  {
269  break;
270 #ifdef VIENNACL_WITH_OPENCL
273  break;
274 #endif
275 #ifdef VIENNACL_WITH_CUDA
278  break;
279 #endif
281  throw memory_exception("not initialised!");
282  default:
283  throw memory_exception("not implemented");
284  }
285  }
286 
287 
294  template<typename SparseMatrixType, class ScalarType, typename SOLVERTAG>
298  SOLVERTAG tag)
299  {
300  assert( (mat.size1() == mat.size2()) && bool("Size check failed for triangular solve on transposed compressed matrix: size1(mat) != size2(mat)"));
301  assert( (mat.size1() == vec.size()) && bool("Size check failed for transposed compressed matrix triangular solve: size1(mat) != size(x)"));
302 
303  switch (viennacl::traits::handle(mat.lhs()).get_active_handle_id())
304  {
307  break;
308 #ifdef VIENNACL_WITH_OPENCL
311  break;
312 #endif
313 #ifdef VIENNACL_WITH_CUDA
316  break;
317 #endif
319  throw memory_exception("not initialised!");
320  default:
321  throw memory_exception("not implemented");
322  }
323  }
324 
325 
326 
327  namespace detail
328  {
329 
330  template<typename SparseMatrixType, class ScalarType, typename SOLVERTAG>
333  viennacl::backend::mem_handle const & block_index_array, vcl_size_t num_blocks,
334  viennacl::vector_base<ScalarType> const & mat_diagonal,
336  SOLVERTAG tag)
337  {
338  assert( (mat.size1() == mat.size2()) && bool("Size check failed for triangular solve on transposed compressed matrix: size1(mat) != size2(mat)"));
339  assert( (mat.size1() == vec.size()) && bool("Size check failed for transposed compressed matrix triangular solve: size1(mat) != size(x)"));
340 
341  switch (viennacl::traits::handle(mat.lhs()).get_active_handle_id())
342  {
344  viennacl::linalg::host_based::detail::block_inplace_solve(mat, block_index_array, num_blocks, mat_diagonal, vec, tag);
345  break;
346  #ifdef VIENNACL_WITH_OPENCL
348  viennacl::linalg::opencl::detail::block_inplace_solve(mat, block_index_array, num_blocks, mat_diagonal, vec, tag);
349  break;
350  #endif
351  #ifdef VIENNACL_WITH_CUDA
353  viennacl::linalg::cuda::detail::block_inplace_solve(mat, block_index_array, num_blocks, mat_diagonal, vec, tag);
354  break;
355  #endif
357  throw memory_exception("not initialised!");
358  default:
359  throw memory_exception("not implemented");
360  }
361  }
362 
363 
364  }
365 
366 
367 
368  } //namespace linalg
369 
370 
372  template<typename M1>
374  matrix_expression< const M1, const M1, op_trans>
375  >::type
376  trans(const M1 & mat)
377  {
379  }
380 
381  //free functions:
387  template<typename SCALARTYPE, typename SparseMatrixType>
391  const viennacl::vector_expression< const SparseMatrixType, const viennacl::vector_base<SCALARTYPE>, viennacl::op_prod> & proxy)
392  {
393  assert(proxy.lhs().size1() == result.size() && bool("Dimensions for addition of sparse matrix-vector product to vector don't match!"));
394  vector<SCALARTYPE> temp(proxy.lhs().size1());
395  viennacl::linalg::prod_impl(proxy.lhs(), proxy.rhs(), temp);
396  result += temp;
397  return result;
398  }
399 
405  template<typename SCALARTYPE, typename SparseMatrixType>
409  const viennacl::vector_expression< const SparseMatrixType, const viennacl::vector_base<SCALARTYPE>, viennacl::op_prod> & proxy)
410  {
411  assert(proxy.lhs().size1() == result.size() && bool("Dimensions for addition of sparse matrix-vector product to vector don't match!"));
412  vector<SCALARTYPE> temp(proxy.lhs().size1());
413  viennacl::linalg::prod_impl(proxy.lhs(), proxy.rhs(), temp);
414  result += temp;
415  return result;
416  }
417 
418 } //namespace viennacl
419 
420 
421 #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.
void prod_impl(const matrix_base< NumericT > &mat, bool trans_A, const vector_base< NumericT > &vec, vector_base< NumericT > &result)
Carries out matrix-vector multiplication.
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.
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
Common base class for dense vectors, vector ranges, and vector slices.
Definition: vector_def.hpp:104
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
float ScalarType
Definition: fft_1d.cpp:42
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 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)