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"
37 
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 
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 
A reader and writer for the matrix market format is implemented here.
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
const vcl_size_t & size1() const
Returns the number of rows.
Implementation of the dense matrix class.
int main()
Definition: bisect.cpp:91
A dense matrix class.
Definition: forwards.h:375
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
VectorT prod(std::vector< std::vector< T, A1 >, A2 > const &matrix, VectorT const &vector)
Definition: prod.hpp:102
Implementation of the compressed_matrix class.
Proxy classes for matrices.
The vector type with operator-overloads and proxy classes is defined here. Linear algebra operations ...
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) ...
float ScalarType
Definition: fft_1d.cpp:42
A sparse square matrix in compressed sparse rows format.
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
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.
A tag for the lanczos algorithm.
Definition: lanczos.hpp:45
Generic interface for the Lanczos algorithm.