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"
38 #include "viennacl/linalg/prod.hpp"
40 #include "viennacl/linalg/cg.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  }
109  viennacl::copy(stl_A, A);
110 
116 
118 
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 
135 
136 
140  std::cout << "----- CG Method -----" << std::endl;
141 
146  viennacl::linalg::cg_tag my_cg_tag(1e-5, 20);
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 
const vcl_size_t & size2() const
Returns the number of columns.
T norm_2(std::vector< T, A > const &v1)
Definition: norm_2.hpp:96
A reader and writer for the matrix market format is implemented here.
Jacobi preconditioner class, can be supplied to solve()-routines. Generic version for non-ViennaCL ma...
Generic interface for matrix-vector and matrix-matrix products. See viennacl/linalg/vector_operations...
int main()
Definition: bisect.cpp:91
The stabilized bi-conjugate gradient method is implemented here.
A tag for a jacobi preconditioner.
Implementation of a simple Jacobi preconditioner.
A tag for the solver GMRES. Used for supplying solver parameters and for dispatching the solve() func...
Definition: gmres.hpp:49
float NumericT
Definition: bisect.cpp:40
VectorT prod(std::vector< std::vector< T, A1 >, A2 > const &matrix, VectorT const &vector)
Definition: prod.hpp:102
Implementations of the generalized minimum residual method are in this file.
Implementation of the compressed_matrix class.
The conjugate gradient method is implemented here.
The vector type with operator-overloads and proxy classes is defined here. Linear algebra operations ...
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
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) ...
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
A tag for the conjugate gradient Used for supplying solver parameters and for dispatching the solve()...
Definition: cg.hpp:48
A sparse square matrix in compressed sparse rows format.
A tag for the stabilized Bi-conjugate gradient solver. Used for supplying solver parameters and for d...
Definition: bicgstab.hpp:47
long read_matrix_market_file(MatrixT &mat, const char *file, long index_base=1)
Reads a sparse matrix from a file (MatrixMarket format)
Implementation of the ViennaCL scalar class.