ViennaCL - The Vienna Computing Library  1.7.0
Free open-source GPU-accelerated linear algebra and solver library.
power-iter.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 
25 // Sparse matrices in uBLAS are *very* slow if debug mode is enabled. Disable it:
26 #ifndef NDEBUG
27  #define BOOST_UBLAS_NDEBUG
28 #endif
29 
30 // Include necessary system headers
31 #include <iostream>
32 #include <fstream>
33 #include <limits>
34 #include <string>
35 
36 #define VIENNACL_WITH_UBLAS
37 
38 // Include basic scalar and vector types of ViennaCL
39 #include "viennacl/scalar.hpp"
40 #include "viennacl/vector.hpp"
42 
45 
46 // Some helper functions for this tutorial:
47 #include <iostream>
48 
49 // Boost includes:
50 #include <boost/numeric/ublas/matrix.hpp>
51 #include <boost/numeric/ublas/matrix_proxy.hpp>
52 #include <boost/numeric/ublas/matrix_expression.hpp>
53 #include <boost/numeric/ublas/matrix_sparse.hpp>
54 #include <boost/numeric/ublas/vector.hpp>
55 #include <boost/numeric/ublas/operation.hpp>
56 #include <boost/numeric/ublas/vector_expression.hpp>
57 
58 
62 int main()
63 {
64  // This example relies on double precision to be available and will provide only poor results with single precision
65  typedef double ScalarType;
66 
70  boost::numeric::ublas::compressed_matrix<ScalarType> ublas_A;
71 
72  if (!viennacl::io::read_matrix_market_file(ublas_A, "../examples/testdata/mat65k.mtx"))
73  {
74  std::cout << "Error reading Matrix file" << std::endl;
75  return EXIT_FAILURE;
76  }
77 
81  viennacl::compressed_matrix<ScalarType> vcl_A(ublas_A.size1(), ublas_A.size2());
82  viennacl::copy(ublas_A, vcl_A);
83 
89 
90  std::cout << "Starting computation of eigenvalue with largest modulus (might take about a minute)..." << std::endl;
91  std::cout << "Result of power iteration with ublas matrix (single-threaded): " << viennacl::linalg::eig(ublas_A, ptag) << std::endl;
92  std::cout << "Result of power iteration with ViennaCL (OpenCL accelerated): " << viennacl::linalg::eig(vcl_A, ptag) << std::endl;
93 
98  viennacl::vector<ScalarType> eigenvector(vcl_A.size1());
99  viennacl::linalg::eig(vcl_A, ptag, eigenvector);
100  std::cout << "First three entries in eigenvector: " << eigenvector[0] << " " << eigenvector[1] << " " << eigenvector[2] << std::endl;
101  viennacl::vector<ScalarType> Ax = viennacl::linalg::prod(vcl_A, eigenvector);
102  std::cout << "First three entries in A*eigenvector: " << Ax[0] << " " << Ax[1] << " " << Ax[2] << std::endl;
103 
104  return EXIT_SUCCESS;
105 }
106 
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
int main()
Definition: bisect.cpp:91
A tag for the power iteration algorithm.
Definition: power_iter.hpp:38
VectorT prod(std::vector< std::vector< T, A1 >, A2 > const &matrix, VectorT const &vector)
Definition: prod.hpp:102
Implementation of the compressed_matrix class.
Defines a tag for the configuration of the power iteration method.
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.
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.