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
blas2.cpp
Go to the documentation of this file.
1 /* =========================================================================
2  Copyright (c) 2010-2016, Institute for Microelectronics,
3  Institute for Analysis and Scientific Computing,
4  TU Wien.
5  Portions of this software are copyright by UChicago Argonne, LLC.
6 
7  -----------------
8  ViennaCL - The Vienna Computing Library
9  -----------------
10 
11  Project Head: Karl Rupp rupp@iue.tuwien.ac.at
12 
13  (A list of authors and contributors can be found in the PDF manual)
14 
15  License: MIT (X11), see file LICENSE in the base directory
16 ============================================================================= */
17 
25 // System headers
26 #include <iostream>
27 
28 // uBLAS headers
29 #include <boost/numeric/ublas/io.hpp>
30 #include <boost/numeric/ublas/triangular.hpp>
31 #include <boost/numeric/ublas/matrix_sparse.hpp>
32 #include <boost/numeric/ublas/matrix.hpp>
33 #include <boost/numeric/ublas/matrix_proxy.hpp>
34 #include <boost/numeric/ublas/vector_proxy.hpp>
35 #include <boost/numeric/ublas/lu.hpp>
36 #include <boost/numeric/ublas/io.hpp>
37 
38 
39 // Must be set if you want to use ViennaCL algorithms on ublas objects
40 #define VIENNACL_WITH_UBLAS 1
41 
42 // ViennaCL headers
43 #include "viennacl/scalar.hpp"
44 #include "viennacl/vector.hpp"
45 #include "viennacl/matrix.hpp"
47 #include "viennacl/linalg/prod.hpp" //generic matrix-vector product
48 #include "viennacl/linalg/norm_2.hpp" //generic l2-norm for vectors
49 #include "viennacl/linalg/lu.hpp" //LU substitution routines
51 
52 // Make `boost::numeric::ublas` available under the shortcut `ublas`:
53 using namespace boost::numeric;
54 
58 int main()
59 {
60  typedef float ScalarType;
61 
63 
68  ublas::vector<ScalarType> rhs(12);
69  for (unsigned int i = 0; i < rhs.size(); ++i)
70  rhs(i) = randomNumber();
71  ublas::vector<ScalarType> rhs2 = rhs;
72  ublas::vector<ScalarType> result = ublas::zero_vector<ScalarType>(10);
73  ublas::vector<ScalarType> result2 = result;
74  ublas::vector<ScalarType> rhs_trans = rhs;
75  rhs_trans.resize(result.size(), true);
76  ublas::vector<ScalarType> result_trans = ublas::zero_vector<ScalarType>(rhs.size());
77 
78  ublas::matrix<ScalarType> matrix(result.size(),rhs.size());
79 
83  for (unsigned int i = 0; i < matrix.size1(); ++i)
84  for (unsigned int j = 0; j < matrix.size2(); ++j)
85  matrix(i,j) = randomNumber();
86 
90  std::vector< ScalarType > stl_result(result.size());
91  std::vector< ScalarType > stl_rhs(rhs.size());
92  std::vector< std::vector<ScalarType> > stl_matrix(result.size());
93  for (unsigned int i=0; i < result.size(); ++i)
94  {
95  stl_matrix[i].resize(rhs.size());
96  for (unsigned int j = 0; j < matrix.size2(); ++j)
97  {
98  stl_rhs[j] = rhs[j];
99  stl_matrix[i][j] = matrix(i,j);
100  }
101  }
102 
106  viennacl::vector<ScalarType> vcl_rhs(rhs.size());
107  viennacl::vector<ScalarType> vcl_result(result.size());
108  viennacl::matrix<ScalarType> vcl_matrix(result.size(), rhs.size());
109  viennacl::matrix<ScalarType> vcl_matrix2(result.size(), rhs.size());
110 
111  viennacl::copy(rhs.begin(), rhs.end(), vcl_rhs.begin());
112  viennacl::copy(matrix, vcl_matrix); //copy from ublas dense matrix type to ViennaCL type
113 
117  vcl_matrix2 = vcl_matrix;
118  vcl_matrix2 += vcl_matrix;
119  vcl_matrix2 -= vcl_matrix;
120  vcl_matrix2 = vcl_matrix2 + vcl_matrix;
121  vcl_matrix2 = vcl_matrix2 - vcl_matrix;
122 
123  viennacl::scalar<ScalarType> vcl_3(3.0);
124  vcl_matrix2 *= ScalarType(2.0);
125  vcl_matrix2 /= ScalarType(2.0);
126  vcl_matrix2 *= vcl_3;
127  vcl_matrix2 /= vcl_3;
128 
132  vcl_matrix.clear();
133 
137  viennacl::copy(stl_matrix, vcl_matrix); //alternative: copy from STL vector< vector<> > type to ViennaCL type
138 
139  //for demonstration purposes (no effect):
140  viennacl::copy(vcl_matrix, matrix); //copy back from ViennaCL to ublas type.
141  viennacl::copy(vcl_matrix, stl_matrix); //copy back from ViennaCL to STL type.
142 
148  std::cout << "----- Matrix-Vector product -----" << std::endl;
149  result = ublas::prod(matrix, rhs); //the ublas way
150  stl_result = viennacl::linalg::prod(stl_matrix, stl_rhs); //using STL
151  vcl_result = viennacl::linalg::prod(vcl_matrix, vcl_rhs); //the ViennaCL way
152 
156  std::cout << "----- Transposed Matrix-Vector product -----" << std::endl;
157  result_trans = prod(trans(matrix), rhs_trans);
158 
159  viennacl::vector<ScalarType> vcl_rhs_trans(rhs_trans.size());
160  viennacl::vector<ScalarType> vcl_result_trans(result_trans.size());
161  viennacl::copy(rhs_trans.begin(), rhs_trans.end(), vcl_rhs_trans.begin());
162  vcl_result_trans = viennacl::linalg::prod(trans(vcl_matrix), vcl_rhs_trans);
163 
164 
165 
172  ublas::matrix<ScalarType> tri_matrix(10,10);
173  for (std::size_t i=0; i<tri_matrix.size1(); ++i)
174  {
175  for (std::size_t j=0; j<i; ++j)
176  tri_matrix(i,j) = 0.0;
177 
178  for (std::size_t j=i; j<tri_matrix.size2(); ++j)
179  tri_matrix(i,j) = matrix(i,j);
180  }
181 
182  viennacl::matrix<ScalarType> vcl_tri_matrix = viennacl::identity_matrix<ScalarType>(tri_matrix.size1());
183  viennacl::copy(tri_matrix, vcl_tri_matrix);
184 
185  // Bring vectors to correct size:
186  rhs.resize(tri_matrix.size1(), true);
187  rhs2.resize(tri_matrix.size1(), true);
188  vcl_rhs.resize(tri_matrix.size1(), true);
189 
190  viennacl::copy(rhs.begin(), rhs.end(), vcl_rhs.begin());
191  vcl_result.resize(10);
192 
193 
197  std::cout << "----- Upper Triangular solve -----" << std::endl;
198  result = ublas::solve(tri_matrix, rhs, ublas::upper_tag()); //ublas
199  vcl_result = viennacl::linalg::solve(vcl_tri_matrix, vcl_rhs, viennacl::linalg::upper_tag()); //ViennaCL
200 
204  ublas::inplace_solve(tri_matrix, rhs, ublas::upper_tag()); //ublas
205  viennacl::linalg::inplace_solve(vcl_tri_matrix, vcl_rhs, viennacl::linalg::upper_tag()); //ViennaCL
206 
207 
211  std::cout << "----- LU factorization -----" << std::endl;
212  std::size_t lu_dim = 300;
213  ublas::matrix<ScalarType> square_matrix(lu_dim, lu_dim);
214  ublas::vector<ScalarType> lu_rhs(lu_dim);
215  viennacl::matrix<ScalarType> vcl_square_matrix(lu_dim, lu_dim);
216  viennacl::vector<ScalarType> vcl_lu_rhs(lu_dim);
217 
218  for (std::size_t i=0; i<lu_dim; ++i)
219  for (std::size_t j=0; j<lu_dim; ++j)
220  square_matrix(i,j) = randomNumber();
221 
222  //put some more weight on diagonal elements:
223  for (std::size_t j=0; j<lu_dim; ++j)
224  {
225  square_matrix(j,j) += ScalarType(10.0);
226  lu_rhs(j) = randomNumber();
227  }
228 
229  viennacl::copy(square_matrix, vcl_square_matrix);
230  viennacl::copy(lu_rhs, vcl_lu_rhs);
231  viennacl::linalg::lu_factorize(vcl_square_matrix);
232  viennacl::linalg::lu_substitute(vcl_square_matrix, vcl_lu_rhs);
233  viennacl::copy(square_matrix, vcl_square_matrix);
234  viennacl::copy(lu_rhs, vcl_lu_rhs);
235 
236 
240  ublas::lu_factorize(square_matrix);
241  ublas::inplace_solve (square_matrix, lu_rhs, ublas::unit_lower_tag ());
242  ublas::inplace_solve (square_matrix, lu_rhs, ublas::upper_tag ());
243 
244 
248  viennacl::linalg::lu_factorize(vcl_square_matrix);
249  viennacl::linalg::lu_substitute(vcl_square_matrix, vcl_lu_rhs);
250 
254  std::cout << "!!!! TUTORIAL COMPLETED SUCCESSFULLY !!!!" << std::endl;
255 
256  return EXIT_SUCCESS;
257 }
258 
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...
This class represents a single scalar value on the GPU and behaves mostly like a built-in scalar type...
Definition: forwards.h:227
Generic interface for the l^2-norm. See viennacl/linalg/vector_operations.hpp for implementations...
std::vector< std::vector< NumericT > > trans(std::vector< std::vector< NumericT > > const &A)
Generic interface for matrix-vector and matrix-matrix products. See viennacl/linalg/vector_operations...
Implementation of the dense matrix class.
int main()
Definition: bisect.cpp:91
void lu_substitute(matrix< NumericT, F1, AlignmentV1 > const &A, matrix< NumericT, F2, AlignmentV2 > &B)
LU substitution for the system LU = rhs.
Definition: lu.hpp:201
A dense matrix class.
Definition: forwards.h:375
Represents a vector consisting of 1 at a given index and zeros otherwise. To be used as an initialize...
Definition: matrix_def.hpp:69
VectorT solve(MatrixT const &matrix, VectorT const &rhs, bicgstab_tag const &tag, PreconditionerT const &precond)
Definition: bicgstab.hpp:496
VectorT prod(std::vector< std::vector< T, A1 >, A2 > const &matrix, VectorT const &vector)
Definition: prod.hpp:102
Random number generator for returning uniformly distributed values in the closed interval [0...
Definition: random.hpp:44
A tag class representing an upper triangular matrix.
Definition: forwards.h:854
Implementations of LU factorization for row-major and column-major dense matrices.
Implementations of dense direct solvers are found here.
void prod(std::vector< std::map< IndexT, NumericT > > const &stl_A, std::vector< std::map< IndexT, NumericT > > const &stl_B, std::vector< std::map< IndexT, NumericT > > &stl_C)
The vector type with operator-overloads and proxy classes is defined here. Linear algebra operations ...
void copy(std::vector< NumericT > &cpu_vec, circulant_matrix< NumericT, AlignmentV > &gpu_mat)
Copies a circulant matrix from the std::vector to the OpenCL device (either GPU or multi-core CPU) ...
A small collection of sequential random number generators.
float ScalarType
Definition: fft_1d.cpp:42
Implementation of the ViennaCL scalar class.
void lu_factorize(matrix< NumericT, viennacl::row_major > &A)
LU factorization of a row-major dense matrix.
Definition: lu.hpp:42