ViennaCL - The Vienna Computing Library: examples/tutorial/mtl4-with-viennacl.cpp 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
mtl4-with-viennacl.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
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"
42 #include "viennacl/compressed_matrix.hpp"
43
44 #include "viennacl/linalg/ilu.hpp"
45 #include "viennacl/linalg/cg.hpp"
46 #include "viennacl/linalg/bicgstab.hpp"
47 #include "viennacl/linalg/gmres.hpp"
48
49
50 // Some helper functions for this tutorial:
51 #include "vector-io.hpp"
52
63 template<typename ScalarType>
64 void run_tutorial()
65 {
66 typedef mtl::dense2D<ScalarType> MTL4DenseMatrix;
67 typedef mtl::compressed2D<ScalarType> MTL4SparseMatrix;
68
72 mtl::dense2D<ScalarType> mtl4_densemat(5, 5);
73 mtl::dense2D<ScalarType> mtl4_densemat2(5, 5);
74 mtl4_densemat(0,0) = 2.0; mtl4_densemat(0,1) = -1.0;
75 mtl4_densemat(1,0) = -1.0; mtl4_densemat(1,1) = 2.0; mtl4_densemat(1,2) = -1.0;
76 mtl4_densemat(2,1) = -1.0; mtl4_densemat(2,2) = -1.0; mtl4_densemat(2,3) = -1.0;
77 mtl4_densemat(3,2) = -1.0; mtl4_densemat(3,3) = 2.0; mtl4_densemat(3,4) = -1.0;
78 mtl4_densemat(4,4) = -1.0; mtl4_densemat(4,4) = -1.0;
79
80
84 MTL4SparseMatrix mtl4_sparsemat;
85 set_to_zero(mtl4_sparsemat);
86 mtl4_sparsemat.change_dim(5, 5);
87
88 MTL4SparseMatrix mtl4_sparsemat2;
89 set_to_zero(mtl4_sparsemat2);
90 mtl4_sparsemat2.change_dim(5, 5);
91
92 {
93 mtl::matrix::inserter< MTL4SparseMatrix > ins(mtl4_sparsemat);
94 typedef typename mtl::Collection<MTL4SparseMatrix>::value_type ValueType;
95 ins(0,0) << ValueType(2.0); ins(0,1) << ValueType(-1.0);
96 ins(1,1) << ValueType(2.0); ins(1,2) << ValueType(-1.0);
97 ins(2,2) << ValueType(-1.0); ins(2,3) << ValueType(-1.0);
98 ins(3,3) << ValueType(2.0); ins(3,4) << ValueType(-1.0);
99 ins(4,4) << ValueType(-1.0);
100 }
101
105 mtl::dense_vector<ScalarType> mtl4_rhs(5, 0.0);
106 mtl::dense_vector<ScalarType> mtl4_result(5, 0.0);
107 mtl::dense_vector<ScalarType> mtl4_temp(5, 0.0);
108
109
110 mtl4_rhs(0) = 10.0;
111 mtl4_rhs(1) = 11.0;
112 mtl4_rhs(2) = 12.0;
113 mtl4_rhs(3) = 13.0;
114 mtl4_rhs(4) = 14.0;
115
119 viennacl::vector<ScalarType> vcl_rhs(5);
120 viennacl::vector<ScalarType> vcl_result(5);
121 viennacl::matrix<ScalarType> vcl_densemat(5, 5);
122 viennacl::compressed_matrix<ScalarType> vcl_sparsemat(5, 5);
123
127 viennacl::copy(&(mtl4_rhs[0]), &(mtl4_rhs[0]) + 5, vcl_rhs.begin()); //method 1: via iterator interface (cf. std::copy())
128 viennacl::copy(mtl4_rhs, vcl_rhs); //method 2: via built-in wrappers (convenience layer)
129
130 viennacl::copy(mtl4_densemat, vcl_densemat);
131 viennacl::copy(mtl4_sparsemat, vcl_sparsemat);
132
133 // For completeness: Copy matrices from ViennaCL back to Eigen:
134 viennacl::copy(vcl_densemat, mtl4_densemat2);
135 viennacl::copy(vcl_sparsemat, mtl4_sparsemat2);
136
140 mtl4_result = mtl4_densemat * mtl4_rhs;
141 vcl_result = viennacl::linalg::prod(vcl_densemat, vcl_rhs);
142 viennacl::copy(vcl_result, mtl4_temp);
143 mtl4_result -= mtl4_temp;
144 std::cout << "Difference for dense matrix-vector product: " << mtl::two_norm(mtl4_result) << std::endl;
145 mtl4_result = mtl4_densemat2 * mtl4_rhs - mtl4_temp;
146 std::cout << "Difference for dense matrix-vector product (MTL4->ViennaCL->MTL4): "
147 << mtl::two_norm(mtl4_result) << std::endl;
148
152 mtl4_result = mtl4_sparsemat * mtl4_rhs;
153 vcl_result = viennacl::linalg::prod(vcl_sparsemat, vcl_rhs);
154 viennacl::copy(vcl_result, mtl4_temp);
155 mtl4_result -= mtl4_temp;
156 std::cout << "Difference for sparse matrix-vector product: " << mtl::two_norm(mtl4_result) << std::endl;
157 mtl4_result = mtl4_sparsemat2 * mtl4_rhs - mtl4_temp;
158 std::cout << "Difference for sparse matrix-vector product (MTL4->ViennaCL->MTL4): "
159 << mtl::two_norm(mtl4_result) << std::endl;
160
161 }
162
163
167 int main(int, char *[])
168 {
169 std::cout << "----------------------------------------------" << std::endl;
170 std::cout << "## Single precision" << std::endl;
171 std::cout << "----------------------------------------------" << std::endl;
172 run_tutorial<float>();
173
174 #ifdef VIENNACL_HAVE_OPENCL
175 if ( viennacl::ocl::current_device().double_support() )
176 #endif
177 {
178 std::cout << "----------------------------------------------" << std::endl;
179 std::cout << "## Double precision" << std::endl;
180 std::cout << "----------------------------------------------" << std::endl;
181 run_tutorial<double>();
182 }
183
187 std::cout << std::endl;
188 std::cout << "!!!! TUTORIAL COMPLETED SUCCESSFULLY !!!!" << std::endl;
189 std::cout << std::endl;
190 }
matrix.hpp
Implementation of the dense matrix class.
main
int main()
Definition: bisect.cpp:91
bicgstab.hpp
The stabilized bi-conjugate gradient method is implemented here.
vector-io.hpp
viennacl::matrix
A dense matrix class.
Definition: forwards.h:375
viennacl::ocl::current_device
viennacl::ocl::device const & current_device()
Convenience function for returning the active device in the current context.
Definition: backend.hpp:351
viennacl::linalg::prod
VectorT prod(std::vector< std::vector< T, A1 >, A2 > const &matrix, VectorT const &vector)
Definition: prod.hpp:102
gmres.hpp
Implementations of the generalized minimum residual method are in this file.
ilu.hpp
Implementations of incomplete factorization preconditioners. Convenience header file.
compressed_matrix.hpp
Implementation of the compressed_matrix class.
viennacl::ocl::device::double_support
bool double_support() const
ViennaCL convenience function: Returns true if the device supports double precision.
Definition: device.hpp:956
cg.hpp
The conjugate gradient method is implemented here.
viennacl::vector< ScalarType >
vector.hpp
The vector type with operator-overloads and proxy classes is defined here. Linear algebra operations ...
viennacl::copy
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) ...
Definition: circulant_matrix.hpp:150
viennacl::compressed_matrix
A sparse square matrix in compressed sparse rows format.
Definition: compressed_matrix.hpp:559