ViennaCL - The Vienna Computing Library: examples/benchmarks/qr.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
qr.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
18 #define VIENNACL_WITH_UBLAS
19 #ifndef NDEBUG
20 #define NDEBUG
21 #endif
22
23 #include <utility>
24 #include <iostream>
25 #include <fstream>
26 #include <string>
27 #include <cmath>
28 #include <algorithm>
29 #include <stdio.h>
30 #include <sys/time.h>
31 #include <time.h>
32
33 /*#include "viennacl/scalar.hpp"
34 #include "viennacl/matrix.hpp"
35 #include "viennacl/compressed_matrix.hpp"
36 #include "viennacl/linalg/cg.hpp"
37 #include "viennacl/linalg/prod.hpp"
38 #include "viennacl/linalg/inner_prod.hpp"
39 #include "viennacl/linalg/ilu.hpp"
40 #include "viennacl/linalg/norm_2.hpp"
41 #include "viennacl/io/matrix_market.hpp"*/
42 #include "viennacl/linalg/qr.hpp"
43 #include "boost/numeric/ublas/vector.hpp"
44 #include "boost/numeric/ublas/matrix.hpp"
45 #include "boost/numeric/ublas/io.hpp"
46
47
48 //typedef viennacl::compressed_matrix<float> SparseMatrix;
49 using namespace boost::numeric::ublas;
50 //using namespace viennacl::linalg;
51
52
53 int main (int argc, const char * argv[])
54 {
55 typedef float ScalarType;
56 typedef boost::numeric::ublas::matrix<ScalarType, boost::numeric::ublas::column_major> MatrixType;
57 typedef boost::numeric::ublas::vector<ScalarType> VectorType;
58
59 viennacl::tools::timer timer;
60 double elapsed;
61
62 std::size_t rows = 1800;
63 std::size_t cols = 1800;
64 double num_ops_qr = 2.0 * cols * cols * (rows - cols/3.0);
65 double num_ops_recovery = 4.0 * (rows*rows*cols - rows*cols*cols + cols*cols*cols);
66
67 MatrixType A(rows, cols);
68 MatrixType Q(rows, rows);
69 MatrixType R(rows, cols);
70
71 for (std::size_t i=0; i<rows; ++i)
72 {
73 for (std::size_t j=0; j<cols; ++j)
74 {
75 A(i,j) = 1.0 + (i + 1)*(j+1);
76 R(i,j) = 0.0;
77 }
78 for (std::size_t j=0; j<rows; ++j)
79 {
80 Q(i,j) = 0.0;
81 }
82 }
83
84 //std::cout << "A: " << A << std::endl;
85 timer.start();
86 std::vector<ScalarType> betas = viennacl::linalg::block_qr(A);
87 //std::vector<ScalarType> betas = viennacl::linalg::qr(A);
88 elapsed = timer.get();
89 std::cout << "Time for QR on CPU: " << elapsed << std::endl;
90 std::cout << "Estimated GFLOPs: " << 2e-9 * num_ops_qr/ elapsed << std::endl;
91
92
93 //std::cout << "Inplace QR-factored A: " << A << std::endl;
94
95 timer.start();
96 viennacl::linalg::recoverQ(A, betas, Q, R);
97 elapsed = timer.get();
98 std::cout << "Time for Q-recovery on CPU: " << elapsed << std::endl;
99 std::cout << "Estimated GFLOPs: " << 2e-9 * num_ops_recovery / elapsed << std::endl;
100
101 /*std::cout << "R after recovery: " << R << std::endl;
102 std::cout << "Q after recovery: " << Q << std::endl;
103 std::cout << "Q*Q^T: " << prod(Q, trans(Q)) << std::endl;
104
105 std::cout << "Q * R: " << prod(Q, R) << std::endl;*/
106
107 return EXIT_SUCCESS;
108 }
109
viennacl::tools::timer
Simple timer class based on gettimeofday (POSIX) or QueryPerformanceCounter (Windows).
Definition: timer.hpp:90
main
int main(int argc, const char *argv[])
Definition: qr.cpp:53
viennacl::linalg::recoverQ
void recoverQ(MatrixType const &A, VectorType const &betas, MatrixType &Q, MatrixType &R)
Definition: qr.hpp:564
qr.hpp
Provides a QR factorization using a block-based approach.
ScalarType
float ScalarType
Definition: fft_1d.cpp:42
viennacl::tools::timer::get
double get() const
Definition: timer.hpp:104
viennacl::linalg::detail::spai::block_qr
void block_qr(std::vector< std::vector< unsigned int > > &g_I, std::vector< std::vector< unsigned int > > &g_J, block_matrix &g_A_I_J_vcl, block_vector &g_bv_vcl, std::vector< cl_uint > &g_is_update, viennacl::context ctx)
Inplace QR factorization via Householder reflections c.f. Gene H. Golub, Charles F. Van Loan "Matrix Computations" 3rd edition p.224 performed on GPU.
Definition: qr.hpp:428
viennacl::tools::timer::start
void start()
Definition: timer.hpp:97