ViennaCL - The Vienna Computing Library: examples/tutorial/iterative-custom.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
iterative-custom.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
27 //
28 // include necessary system headers
29 //
30 #include <iostream>
31
32 //
33 // ViennaCL includes
34 //
35 #include "viennacl/scalar.hpp"
36 #include "viennacl/vector.hpp"
37 #include "viennacl/compressed_matrix.hpp"
38 #include "viennacl/linalg/prod.hpp"
39 #include "viennacl/linalg/jacobi_precond.hpp"
40 #include "viennacl/linalg/cg.hpp"
41 #include "viennacl/linalg/bicgstab.hpp"
42 #include "viennacl/linalg/gmres.hpp"
43 #include "viennacl/io/matrix_market.hpp"
44
54 template<typename MatrixT, typename VectorT>
55 struct monitor_user_data
56 {
57 monitor_user_data(MatrixT const & A, VectorT const & b, VectorT const & guess) : A_ptr(&A), b_ptr(&b), guess_ptr(&guess) {}
58
59 MatrixT const *A_ptr;
60 VectorT const *b_ptr;
61 VectorT const *guess_ptr;
62 };
63
73 template<typename VectorT, typename NumericT, typename MatrixT>
74 bool my_custom_monitor(VectorT const & current_approx, NumericT residual_estimate, void *user_data)
75 {
76 // Extract residual:
77 monitor_user_data<MatrixT, VectorT> const *data = reinterpret_cast<monitor_user_data<MatrixT, VectorT> const*>(user_data);
78
79 // Form residual r = b - A*x, taking an initial guess into account: r = b - A * (current_approx + x_initial)
80 VectorT x = current_approx + *data->guess_ptr;
81 VectorT residual = *data->b_ptr - viennacl::linalg::prod(*data->A_ptr, x);
82 VectorT initial_residual = *data->b_ptr - viennacl::linalg::prod(*data->A_ptr, *data->guess_ptr);
83
84 std::cout << "Residual estimate vs. true residual: " << residual_estimate << " vs. " << viennacl::linalg::norm_2(residual) / viennacl::linalg::norm_2(initial_residual) << std::endl;
85
86 return false; // no termination of iteration
87 }
88
89
94 int main()
95 {
96 typedef float ScalarType;
97
98
102 std::vector<std::map<unsigned int, ScalarType> > stl_A;
103 if (!viennacl::io::read_matrix_market_file(stl_A, "../examples/testdata/mat65k.mtx"))
104 {
105 std::cout << "Error reading Matrix file" << std::endl;
106 return EXIT_FAILURE;
107 }
108 viennacl::compressed_matrix<ScalarType> A;
109 viennacl::copy(stl_A, A);
110
114 viennacl::vector<ScalarType> ref_result = viennacl::scalar_vector<ScalarType>(A.size2(), ScalarType(1.0));
115 viennacl::vector<ScalarType> result(A.size2());
116
117 viennacl::vector<ScalarType> b = viennacl::linalg::prod(A, ref_result);
118
122 viennacl::vector<ScalarType> init_guess = viennacl::scalar_vector<ScalarType>(ref_result.size(), ScalarType(0.9));
123 init_guess[0] = 0;
124
128 monitor_user_data<viennacl::compressed_matrix<ScalarType>, viennacl::vector<ScalarType> > my_monitor_data(A, b, init_guess);
129
130
134 viennacl::linalg::jacobi_precond< viennacl::compressed_matrix<ScalarType> > jacobi(A, viennacl::linalg::jacobi_tag());
135
136
140 std::cout << "----- CG Method -----" << std::endl;
141
146 viennacl::linalg::cg_tag my_cg_tag(1e-5, 20);
147 viennacl::linalg::cg_solver<viennacl::vector<ScalarType> > my_cg_solver(my_cg_tag);
148
149 my_cg_solver.set_monitor(my_custom_monitor<viennacl::vector<ScalarType>, ScalarType, viennacl::compressed_matrix<ScalarType> >, &my_monitor_data);
150 my_cg_solver.set_initial_guess(init_guess);
151
152 my_cg_solver(A, b); // without preconditioner
153
154
158 std::cout << "----- BiCGStab Method -----" << std::endl;
159
164 viennacl::linalg::bicgstab_tag my_bicgstab_tag(1e-5, 20);
165 viennacl::linalg::bicgstab_solver<viennacl::vector<ScalarType> > my_bicgstab_solver(my_bicgstab_tag);
166
167 my_bicgstab_solver.set_monitor(my_custom_monitor<viennacl::vector<ScalarType>, ScalarType, viennacl::compressed_matrix<ScalarType> >, &my_monitor_data);
168 my_bicgstab_solver.set_initial_guess(init_guess);
169
170 my_bicgstab_solver(A, b, jacobi); // with Jacobi preconditioner
171
172
176 std::cout << "----- GMRES Method -----" << std::endl;
177
184 viennacl::linalg::gmres_tag my_gmres_tag(1e-5, 30, 10);
185 viennacl::linalg::gmres_solver<viennacl::vector<ScalarType> > my_gmres_solver(my_gmres_tag);
186
187 my_gmres_solver.set_monitor(my_custom_monitor<viennacl::vector<ScalarType>, ScalarType, viennacl::compressed_matrix<ScalarType> >, &my_monitor_data);
188 my_gmres_solver.set_initial_guess(init_guess);
189
190 my_gmres_solver(A, b);
191
195 std::cout << "!!!! TUTORIAL COMPLETED SUCCESSFULLY !!!!" << std::endl;
196
197 return EXIT_SUCCESS;
198 }
199
viennacl::compressed_matrix::size2
const vcl_size_t & size2() const
Returns the number of columns.
Definition: compressed_matrix.hpp:929
viennacl::linalg::norm_2
T norm_2(std::vector< T, A > const &v1)
Definition: norm_2.hpp:96
matrix_market.hpp
A reader and writer for the matrix market format is implemented here.
viennacl::linalg::jacobi_precond
Jacobi preconditioner class, can be supplied to solve()-routines. Generic version for non-ViennaCL ma...
Definition: jacobi_precond.hpp:50
prod.hpp
Generic interface for matrix-vector and matrix-matrix products. See viennacl/linalg/vector_operations...
main
int main()
Definition: bisect.cpp:91
bicgstab.hpp
The stabilized bi-conjugate gradient method is implemented here.
viennacl::linalg::cg_solver
Definition: cg.hpp:380
viennacl::linalg::bicgstab_solver
Definition: bicgstab.hpp:538
viennacl::linalg::jacobi_tag
A tag for a jacobi preconditioner.
Definition: jacobi_precond.hpp:43
jacobi_precond.hpp
Implementation of a simple Jacobi preconditioner.
viennacl::linalg::gmres_solver
Definition: gmres.hpp:678
viennacl::linalg::gmres_tag
A tag for the solver GMRES. Used for supplying solver parameters and for dispatching the solve() func...
Definition: gmres.hpp:49
NumericT
float NumericT
Definition: bisect.cpp:40
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.
compressed_matrix.hpp
Implementation of the compressed_matrix class.
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::scalar_vector
Represents a vector consisting of scalars 's' only, i.e. v[i] = s for all i. To be used as an initial...
Definition: vector_def.hpp:87
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::vector_base::size
size_type size() const
Returns the length of the vector (cf. std::vector)
Definition: vector_def.hpp:118
ScalarType
float ScalarType
Definition: fft_1d.cpp:42
viennacl::linalg::cg_tag
A tag for the conjugate gradient Used for supplying solver parameters and for dispatching the solve()...
Definition: cg.hpp:48
viennacl::compressed_matrix
A sparse square matrix in compressed sparse rows format.
Definition: compressed_matrix.hpp:559
viennacl::linalg::bicgstab_tag
A tag for the stabilized Bi-conjugate gradient solver. Used for supplying solver parameters and for d...
Definition: bicgstab.hpp:47
viennacl::io::read_matrix_market_file
long read_matrix_market_file(MatrixT &mat, const char *file, long index_base=1)
Reads a sparse matrix from a file (MatrixMarket format)
Definition: matrix_market.hpp:339
scalar.hpp
Implementation of the ViennaCL scalar class.