ViennaCL - The Vienna Computing Library: examples/tutorial/power-iter.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
power-iter.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
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"
41 #include "viennacl/compressed_matrix.hpp"
42
43 #include "viennacl/linalg/power_iter.hpp"
44 #include "viennacl/io/matrix_market.hpp"
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
88 viennacl::linalg::power_iter_tag ptag(1e-6);
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
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
main
int main()
Definition: bisect.cpp:91
viennacl::linalg::power_iter_tag
A tag for the power iteration algorithm.
Definition: power_iter.hpp:38
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.
power_iter.hpp
Defines a tag for the configuration of the power iteration method.
viennacl::vector< ScalarType >
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::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.