ViennaCL - The Vienna Computing Library  1.6.2
Free open-source GPU-accelerated linear algebra and solver library.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
mtl4-with-viennacl.cpp
Go to the documentation of this file.
1 /* =========================================================================
2  Copyright (c) 2010-2014, 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 
18 
26 // System headers
27 #include <iostream>
28 
29 
30 // MTL4 headers
31 #include <boost/numeric/mtl/mtl.hpp>
32 #include <boost/numeric/itl/itl.hpp>
33 
34 
35 // Must be set prior to any ViennaCL includes if you want to use ViennaCL algorithms on MTL4 objects
36 #define VIENNACL_WITH_MTL4 1
37 
38 
39 // ViennaCL headers
40 #include "viennacl/vector.hpp"
41 #include "viennacl/matrix.hpp"
43 
44 #include "viennacl/linalg/ilu.hpp"
45 #include "viennacl/linalg/cg.hpp"
48 
49 
50 // Some helper functions for this tutorial:
51 #include "Random.hpp"
52 #include "vector-io.hpp"
53 #include "../benchmarks/benchmark-utils.hpp"
54 
65 template<typename ScalarType>
66 void run_tutorial()
67 {
68  typedef mtl::dense2D<ScalarType> MTL4DenseMatrix;
69  typedef mtl::compressed2D<ScalarType> MTL4SparseMatrix;
70 
74  mtl::dense2D<ScalarType> mtl4_densemat(5, 5);
75  mtl::dense2D<ScalarType> mtl4_densemat2(5, 5);
76  mtl4_densemat(0,0) = 2.0; mtl4_densemat(0,1) = -1.0;
77  mtl4_densemat(1,0) = -1.0; mtl4_densemat(1,1) = 2.0; mtl4_densemat(1,2) = -1.0;
78  mtl4_densemat(2,1) = -1.0; mtl4_densemat(2,2) = -1.0; mtl4_densemat(2,3) = -1.0;
79  mtl4_densemat(3,2) = -1.0; mtl4_densemat(3,3) = 2.0; mtl4_densemat(3,4) = -1.0;
80  mtl4_densemat(4,4) = -1.0; mtl4_densemat(4,4) = -1.0;
81 
82 
86  MTL4SparseMatrix mtl4_sparsemat;
87  set_to_zero(mtl4_sparsemat);
88  mtl4_sparsemat.change_dim(5, 5);
89 
90  MTL4SparseMatrix mtl4_sparsemat2;
91  set_to_zero(mtl4_sparsemat2);
92  mtl4_sparsemat2.change_dim(5, 5);
93 
94  {
95  mtl::matrix::inserter< MTL4SparseMatrix > ins(mtl4_sparsemat);
96  typedef typename mtl::Collection<MTL4SparseMatrix>::value_type ValueType;
97  ins(0,0) << ValueType(2.0); ins(0,1) << ValueType(-1.0);
98  ins(1,1) << ValueType(2.0); ins(1,2) << ValueType(-1.0);
99  ins(2,2) << ValueType(-1.0); ins(2,3) << ValueType(-1.0);
100  ins(3,3) << ValueType(2.0); ins(3,4) << ValueType(-1.0);
101  ins(4,4) << ValueType(-1.0);
102  }
103 
107  mtl::dense_vector<ScalarType> mtl4_rhs(5, 0.0);
108  mtl::dense_vector<ScalarType> mtl4_result(5, 0.0);
109  mtl::dense_vector<ScalarType> mtl4_temp(5, 0.0);
110 
111 
112  mtl4_rhs(0) = 10.0;
113  mtl4_rhs(1) = 11.0;
114  mtl4_rhs(2) = 12.0;
115  mtl4_rhs(3) = 13.0;
116  mtl4_rhs(4) = 14.0;
117 
121  viennacl::vector<ScalarType> vcl_rhs(5);
122  viennacl::vector<ScalarType> vcl_result(5);
123  viennacl::matrix<ScalarType> vcl_densemat(5, 5);
124  viennacl::compressed_matrix<ScalarType> vcl_sparsemat(5, 5);
125 
129  viennacl::copy(&(mtl4_rhs[0]), &(mtl4_rhs[0]) + 5, vcl_rhs.begin()); //method 1: via iterator interface (cf. std::copy())
130  viennacl::copy(mtl4_rhs, vcl_rhs); //method 2: via built-in wrappers (convenience layer)
131 
132  viennacl::copy(mtl4_densemat, vcl_densemat);
133  viennacl::copy(mtl4_sparsemat, vcl_sparsemat);
134 
135  // For completeness: Copy matrices from ViennaCL back to Eigen:
136  viennacl::copy(vcl_densemat, mtl4_densemat2);
137  viennacl::copy(vcl_sparsemat, mtl4_sparsemat2);
138 
142  mtl4_result = mtl4_densemat * mtl4_rhs;
143  vcl_result = viennacl::linalg::prod(vcl_densemat, vcl_rhs);
144  viennacl::copy(vcl_result, mtl4_temp);
145  mtl4_result -= mtl4_temp;
146  std::cout << "Difference for dense matrix-vector product: " << mtl::two_norm(mtl4_result) << std::endl;
147  mtl4_result = mtl4_densemat2 * mtl4_rhs - mtl4_temp;
148  std::cout << "Difference for dense matrix-vector product (MTL4->ViennaCL->MTL4): "
149  << mtl::two_norm(mtl4_result) << std::endl;
150 
154  mtl4_result = mtl4_sparsemat * mtl4_rhs;
155  vcl_result = viennacl::linalg::prod(vcl_sparsemat, vcl_rhs);
156  viennacl::copy(vcl_result, mtl4_temp);
157  mtl4_result -= mtl4_temp;
158  std::cout << "Difference for sparse matrix-vector product: " << mtl::two_norm(mtl4_result) << std::endl;
159  mtl4_result = mtl4_sparsemat2 * mtl4_rhs - mtl4_temp;
160  std::cout << "Difference for sparse matrix-vector product (MTL4->ViennaCL->MTL4): "
161  << mtl::two_norm(mtl4_result) << std::endl;
162 
163 }
164 
165 
169 int main(int, char *[])
170 {
171  std::cout << "----------------------------------------------" << std::endl;
172  std::cout << "## Single precision" << std::endl;
173  std::cout << "----------------------------------------------" << std::endl;
174  run_tutorial<float>();
175 
176 #ifdef VIENNACL_HAVE_OPENCL
178 #endif
179  {
180  std::cout << "----------------------------------------------" << std::endl;
181  std::cout << "## Double precision" << std::endl;
182  std::cout << "----------------------------------------------" << std::endl;
183  run_tutorial<double>();
184  }
185 
189  std::cout << std::endl;
190  std::cout << "!!!! TUTORIAL COMPLETED SUCCESSFULLY !!!!" << std::endl;
191  std::cout << std::endl;
192 }
Implementation of the dense matrix class.
int main()
Definition: bisect.cpp:160
The stabilized bi-conjugate gradient method is implemented here.
A dense matrix class.
Definition: forwards.h:374
viennacl::ocl::device const & current_device()
Convenience function for returning the active device in the current context.
Definition: backend.hpp:351
VectorT prod(std::vector< std::vector< T, A1 >, A2 > const &matrix, VectorT const &vector)
Definition: prod.hpp:91
Implementations of the generalized minimum residual method are in this file.
Implementations of incomplete factorization preconditioners. Convenience header file.
Implementation of the compressed_matrix class.
bool double_support() const
ViennaCL convenience function: Returns true if the device supports double precision.
Definition: device.hpp:956
The conjugate gradient method is implemented here.
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 sparse square matrix in compressed sparse rows format.