ViennaCL - The Vienna Computing Library  1.7.0
Free open-source GPU-accelerated linear algebra and solver library.
iterative-custom.cpp
Go to the documentation of this file.
1 /* =========================================================================
2  Copyright (c) 2010-2015, 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 
114  std::size_t size = A.size1();
117 
119 
124  init_guess[0] = 0;
125 
129  monitor_user_data<viennacl::compressed_matrix<ScalarType>, viennacl::vector<ScalarType> > my_monitor_data(A, b, init_guess);
130 
131 
136 
137 
141  std::cout << "----- CG Method -----" << std::endl;
142 
147  viennacl::linalg::cg_tag my_cg_tag(1e-5, 20);
149 
150  my_cg_solver.set_monitor(my_custom_monitor<viennacl::vector<ScalarType>, ScalarType, viennacl::compressed_matrix<ScalarType> >, &my_monitor_data);
151  my_cg_solver.set_initial_guess(init_guess);
152 
153  my_cg_solver(A, b); // without preconditioner
154 
155 
159  std::cout << "----- BiCGStab Method -----" << std::endl;
160 
165  viennacl::linalg::bicgstab_tag my_bicgstab_tag(1e-5, 20);
166  viennacl::linalg::bicgstab_solver<viennacl::vector<ScalarType> > my_bicgstab_solver(my_bicgstab_tag);
167 
168  my_bicgstab_solver.set_monitor(my_custom_monitor<viennacl::vector<ScalarType>, ScalarType, viennacl::compressed_matrix<ScalarType> >, &my_monitor_data);
169  my_bicgstab_solver.set_initial_guess(init_guess);
170 
171  my_bicgstab_solver(A, b, jacobi); // with Jacobi preconditioner
172 
173 
177  std::cout << "----- GMRES Method -----" << std::endl;
178 
185  viennacl::linalg::gmres_tag my_gmres_tag(1e-5, 30, 10);
186  viennacl::linalg::gmres_solver<viennacl::vector<ScalarType> > my_gmres_solver(my_gmres_tag);
187 
188  my_gmres_solver.set_monitor(my_custom_monitor<viennacl::vector<ScalarType>, ScalarType, viennacl::compressed_matrix<ScalarType> >, &my_monitor_data);
189  my_gmres_solver.set_initial_guess(init_guess);
190 
191  my_gmres_solver(A, b);
192 
196  std::cout << "!!!! TUTORIAL COMPLETED SUCCESSFULLY !!!!" << std::endl;
197 
198  return EXIT_SUCCESS;
199 }
200 
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...
const vcl_size_t & size1() const
Returns the number of rows.
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
vcl_size_t size(VectorType const &vec)
Generic routine for obtaining the size of a vector (ViennaCL, uBLAS, etc.)
Definition: size.hpp:235
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.