ViennaCL - The Vienna Computing Library: examples/tutorial/lanczos.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
lanczos.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
28 // include necessary system headers
29 #include <iostream>
30
31 //include basic scalar and vector types of ViennaCL
32 #include "viennacl/scalar.hpp"
33 #include "viennacl/vector.hpp"
34 #include "viennacl/matrix.hpp"
35 #include "viennacl/matrix_proxy.hpp"
36 #include "viennacl/compressed_matrix.hpp"
37
38 #include "viennacl/linalg/lanczos.hpp"
39 #include "viennacl/io/matrix_market.hpp"
40
41 // Some helper functions for this tutorial:
42 #include <iostream>
43 #include <string>
44 #include <iomanip>
45
50 int main()
51 {
52 // If you GPU does not support double precision, use `float` instead of `double`:
53 typedef double ScalarType;
54
58 std::vector< std::map<unsigned int, ScalarType> > host_A;
59 if (!viennacl::io::read_matrix_market_file(host_A, "../examples/testdata/mat65k.mtx"))
60 {
61 std::cout << "Error reading Matrix file" << std::endl;
62 return EXIT_FAILURE;
63 }
64
65 viennacl::compressed_matrix<ScalarType> A;
66 viennacl::copy(host_A, A);
67
71 viennacl::linalg::lanczos_tag ltag(0.75, // Select a power of 0.75 as the tolerance for the machine precision.
72 10, // Compute (approximations to) the 10 largest eigenvalues
73 viennacl::linalg::lanczos_tag::partial_reorthogonalization, // use partial reorthogonalization
74 30); // Maximum size of the Krylov space
75
79 std::cout << "Running Lanczos algorithm (eigenvalues only). This might take a while..." << std::endl;
80 std::vector<ScalarType> lanczos_eigenvalues = viennacl::linalg::eig(A, ltag);
81
86 std::cout << "Running Lanczos algorithm (with eigenvectors). This might take a while..." << std::endl;
87 viennacl::matrix<ScalarType> approx_eigenvectors_A(A.size1(), ltag.num_eigenvalues());
88 lanczos_eigenvalues = viennacl::linalg::eig(A, approx_eigenvectors_A, ltag);
89
93 for (std::size_t i = 0; i< lanczos_eigenvalues.size(); i++)
94 {
95 std::cout << "Approx. eigenvalue " << std::setprecision(7) << lanczos_eigenvalues[i];
96
97 // test approximated eigenvector by computing A*v:
98 viennacl::vector<ScalarType> approx_eigenvector = viennacl::column(approx_eigenvectors_A, static_cast<unsigned int>(i));
99 viennacl::vector<ScalarType> Aq = viennacl::linalg::prod(A, approx_eigenvector);
100 std::cout << " (" << viennacl::linalg::inner_prod(Aq, approx_eigenvector) << " for <Av,v> with approx. eigenvector v)" << std::endl;
101 }
102
103 return EXIT_SUCCESS;
104 }
105
matrix_market.hpp
A reader and writer for the matrix market format is implemented here.
viennacl::linalg::eig
std::vector< typename viennacl::result_of::cpu_value_type< typename MatrixT::value_type >::type > eig(MatrixT const &matrix, DenseMatrixT &eigenvectors_A, lanczos_tag const &tag, bool compute_eigenvectors=true)
Implementation of the calculation of eigenvalues using lanczos (with and without reorthogonalization)...
Definition: lanczos.hpp:452
viennacl::compressed_matrix::size1
const vcl_size_t & size1() const
Returns the number of rows.
Definition: compressed_matrix.hpp:927
matrix.hpp
Implementation of the dense matrix class.
main
int main()
Definition: bisect.cpp:91
viennacl::matrix
A dense matrix class.
Definition: forwards.h:375
viennacl::linalg::inner_prod
viennacl::enable_if< viennacl::is_stl< typename viennacl::traits::tag_of< VectorT1 >::type >::value, typename VectorT1::value_type >::type inner_prod(VectorT1 const &v1, VectorT2 const &v2)
Definition: inner_prod.hpp:100
viennacl::linalg::lanczos_tag::partial_reorthogonalization
Definition: lanczos.hpp:51
viennacl::linalg::prod
VectorT prod(std::vector< std::vector< T, A1 >, A2 > const &matrix, VectorT const &vector)
Definition: prod.hpp:102
compressed_matrix.hpp
Implementation of the compressed_matrix class.
viennacl::vector< ScalarType >
matrix_proxy.hpp
Proxy classes for matrices.
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
ScalarType
float ScalarType
Definition: fft_1d.cpp:42
viennacl::compressed_matrix
A sparse square matrix in compressed sparse rows format.
Definition: compressed_matrix.hpp:559
viennacl::column
vector_expression< const matrix_base< NumericT, F >, const unsigned int, op_column > column(const matrix_base< NumericT, F > &A, unsigned int j)
Definition: matrix.hpp:918
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.
viennacl::linalg::lanczos_tag
A tag for the lanczos algorithm.
Definition: lanczos.hpp:45
lanczos.hpp
Generic interface for the Lanczos algorithm.