ViennaCL - The Vienna Computing Library  1.6.1
Free open-source GPU-accelerated linear algebra and solver library.
iterative-ublas.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 
25 //
26 // include necessary system headers
27 //
28 #include <iostream>
29 
30 //
31 // Necessary to obtain a suitable performance in ublas
32 #ifndef NDEBUG
33  #define BOOST_UBLAS_NDEBUG
34 #endif
35 
36 
37 //
38 // ublas includes
39 //
40 #include <boost/numeric/ublas/io.hpp>
41 #include <boost/numeric/ublas/triangular.hpp>
42 #include <boost/numeric/ublas/matrix_sparse.hpp>
43 #include <boost/numeric/ublas/matrix.hpp>
44 #include <boost/numeric/ublas/matrix_proxy.hpp>
45 #include <boost/numeric/ublas/operation.hpp>
46 #include <boost/numeric/ublas/operation_sparse.hpp>
47 #include <boost/numeric/ublas/io.hpp>
48 #include <boost/numeric/ublas/lu.hpp>
49 
50 // Must be set if you want to use ViennaCL algorithms on ublas objects
51 #define VIENNACL_WITH_UBLAS 1
52 
53 //
54 // ViennaCL includes
55 //
56 #include "viennacl/linalg/ilu.hpp"
57 #include "viennacl/linalg/cg.hpp"
61 
62 // Some helper functions for this tutorial:
63 #include "Random.hpp"
64 #include "vector-io.hpp"
65 
69 using namespace boost::numeric;
70 
74 int main()
75 {
76  typedef float ScalarType;
77 
78  //
79  // Set up some ublas objects
80  //
81  ublas::vector<ScalarType> rhs;
82  ublas::vector<ScalarType> ref_result;
83  ublas::vector<ScalarType> result;
84  ublas::compressed_matrix<ScalarType> ublas_matrix;
85 
89  if (!viennacl::io::read_matrix_market_file(ublas_matrix, "../examples/testdata/mat65k.mtx"))
90  {
91  std::cout << "Error reading Matrix file" << std::endl;
92  return EXIT_FAILURE;
93  }
94 
98  if (!readVectorFromFile("../examples/testdata/rhs65025.txt", rhs))
99  {
100  std::cout << "Error reading RHS file" << std::endl;
101  return EXIT_FAILURE;
102  }
103  //std::cout << "done reading rhs" << std::endl;
104 
105  if (!readVectorFromFile("../examples/testdata/result65025.txt", ref_result))
106  {
107  std::cout << "Error reading Result file" << std::endl;
108  return EXIT_FAILURE;
109  }
110  //std::cout << "done reading result" << std::endl;
111 
112 
119  viennacl::linalg::ilu0_tag> ublas_block_ilu0(ublas_matrix, viennacl::linalg::ilu0_tag());
120 
124  std::cout << "----- CG Test -----" << std::endl;
125 
126  result = viennacl::linalg::solve(ublas_matrix, rhs, viennacl::linalg::cg_tag());
127  std::cout << "Residual norm: " << norm_2(prod(ublas_matrix, result) - rhs) << std::endl;
128  result = viennacl::linalg::solve(ublas_matrix, rhs, viennacl::linalg::cg_tag(1e-6, 20), ublas_ilut);
129  std::cout << "Residual norm: " << norm_2(prod(ublas_matrix, result) - rhs) << std::endl;
130  result = viennacl::linalg::solve(ublas_matrix, rhs, viennacl::linalg::cg_tag(1e-6, 20), ublas_ilu0);
131  std::cout << "Residual norm: " << norm_2(prod(ublas_matrix, result) - rhs) << std::endl;
132  result = viennacl::linalg::solve(ublas_matrix, rhs, viennacl::linalg::cg_tag(1e-6, 20), ublas_block_ilu0);
133  std::cout << "Residual norm: " << norm_2(prod(ublas_matrix, result) - rhs) << std::endl;
134 
138  std::cout << "----- BiCGStab Test -----" << std::endl;
139 
140  result = viennacl::linalg::solve(ublas_matrix, rhs, viennacl::linalg::bicgstab_tag()); //without preconditioner
141  result = viennacl::linalg::solve(ublas_matrix, rhs, viennacl::linalg::bicgstab_tag(1e-6, 20), ublas_ilut); //with preconditioner
142  result = viennacl::linalg::solve(ublas_matrix, rhs, viennacl::linalg::bicgstab_tag(1e-6, 20), ublas_ilu0); //with preconditioner
143 
147  std::cout << "----- GMRES Test -----" << std::endl;
148 
149  result = viennacl::linalg::solve(ublas_matrix, rhs, viennacl::linalg::gmres_tag()); //without preconditioner
150  result = viennacl::linalg::solve(ublas_matrix, rhs, viennacl::linalg::gmres_tag(1e-6, 20), ublas_ilut);//with preconditioner
151  result = viennacl::linalg::solve(ublas_matrix, rhs, viennacl::linalg::gmres_tag(1e-6, 20), ublas_ilu0);//with preconditioner
152 
156  std::cout << "!!!! TUTORIAL COMPLETED SUCCESSFULLY !!!!" << std::endl;
157 
158  return EXIT_SUCCESS;
159 }
160 
T norm_2(std::vector< T, A > const &v1)
Definition: norm_2.hpp:86
A reader and writer for the matrix market format is implemented here.
ILU0 preconditioner class, can be supplied to solve()-routines.
Definition: ilu0.hpp:146
int main()
Definition: bisect.cpp:160
A tag for incomplete LU factorization with static pattern (ILU0)
Definition: ilu0.hpp:58
The stabilized bi-conjugate gradient method is implemented here.
A block ILU preconditioner class, can be supplied to solve()-routines.
Definition: block_ilu.hpp:132
A tag for the solver GMRES. Used for supplying solver parameters and for dispatching the solve() func...
Definition: gmres.hpp:49
Implementations of the generalized minimum residual method are in this file.
Implementations of incomplete factorization preconditioners. Convenience header file.
A tag for incomplete LU factorization with threshold (ILUT)
Definition: ilut.hpp:45
bool readVectorFromFile(const std::string &filename, VectorType &vec)
Definition: vector-io.hpp:104
ILUT preconditioner class, can be supplied to solve()-routines.
Definition: ilut.hpp:252
The conjugate gradient method is implemented here.
void prod(const MatrixT1 &A, bool transposed_A, const MatrixT2 &B, bool transposed_B, MatrixT3 &C, ScalarT alpha, ScalarT beta)
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 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)
viennacl::vector< NumericT > solve(MatrixT const &A, viennacl::vector_base< NumericT > const &rhs, bicgstab_tag const &tag, viennacl::linalg::no_precond)
Implementation of a pipelined stabilized Bi-conjugate gradient solver.
Definition: bicgstab.hpp:88