ViennaCL - The Vienna Computing Library  1.6.2
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-2014, 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 #include "benchmark-utils.hpp"
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  Timer timer;
59  double elapsed;
60 
61  std::size_t rows = 1800;
62  std::size_t cols = 1800;
63  double num_ops_qr = 2.0 * cols * cols * (rows - cols/3.0);
64  double num_ops_recovery = 4.0 * (rows*rows*cols - rows*cols*cols + cols*cols*cols);
65 
66  MatrixType A(rows, cols);
67  MatrixType Q(rows, rows);
68  MatrixType R(rows, cols);
69 
70  for (std::size_t i=0; i<rows; ++i)
71  {
72  for (std::size_t j=0; j<cols; ++j)
73  {
74  A(i,j) = 1.0 + (i + 1)*(j+1);
75  R(i,j) = 0.0;
76  }
77  for (std::size_t j=0; j<rows; ++j)
78  {
79  Q(i,j) = 0.0;
80  }
81  }
82 
83  //std::cout << "A: " << A << std::endl;
84  timer.start();
85  std::vector<ScalarType> betas = viennacl::linalg::block_qr(A);
86  //std::vector<ScalarType> betas = viennacl::linalg::qr(A);
87  elapsed = timer.get();
88  std::cout << "Time for QR on CPU: " << elapsed << std::endl;
89  std::cout << "Estimated GFLOPs: " << 2e-9 * num_ops_qr/ elapsed << std::endl;
90 
91 
92  //std::cout << "Inplace QR-factored A: " << A << std::endl;
93 
94  timer.start();
95  viennacl::linalg::recoverQ(A, betas, Q, R);
96  elapsed = timer.get();
97  std::cout << "Time for Q-recovery on CPU: " << elapsed << std::endl;
98  std::cout << "Estimated GFLOPs: " << 2e-9 * num_ops_recovery / elapsed << std::endl;
99 
100  /*std::cout << "R after recovery: " << R << std::endl;
101  std::cout << "Q after recovery: " << Q << std::endl;
102  std::cout << "Q*Q^T: " << prod(Q, trans(Q)) << std::endl;
103 
104  std::cout << "Q * R: " << prod(Q, R) << std::endl;*/
105 
106  return EXIT_SUCCESS;
107 }
108 
int main(int argc, const char *argv[])
Definition: qr.cpp:53
void start()
double get() const
void recoverQ(MatrixType const &A, VectorType const &betas, MatrixType &Q, MatrixType &R)
Definition: qr.hpp:564
Provides a QR factorization using a block-based approach.
float ScalarType
Definition: fft_1d.cpp:42
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