ViennaCL - The Vienna Computing Library  1.7.0
Free open-source GPU-accelerated linear algebra and solver library.
direct_solve.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 
18 
19 /*
20 * Benchmark: Direct solve matrix-matrix and matrix-vecotor
21 *
22 */
23 #include <iostream>
24 
25 
26 #include "viennacl/scalar.hpp"
27 #include "viennacl/matrix.hpp"
29 #include "viennacl/vector.hpp"
30 #include "viennacl/linalg/prod.hpp"
34 #include "viennacl/tools/timer.hpp"
35 
36 #define BENCHMARK_RUNS 10
37 
38 
39 inline void printOps(double num_ops, double exec_time)
40 {
41  std::cout << "GFLOPs: " << num_ops / (1000000 * exec_time * 1000) << std::endl;
42 }
43 
44 
45 template<typename NumericT>
47 {
49 
50  for (std::size_t i = 0; i < mat.size1(); ++i)
51  {
52  for (std::size_t j = 0; j < mat.size2(); ++j)
53  mat(i, j) = static_cast<NumericT>(-0.5) * randomNumber();
54  mat(i, i) = NumericT(1.0) + NumericT(2.0) * randomNumber(); //some extra weight on diagonal for stability
55  }
56 }
57 
58 template<typename NumericT>
60 {
62 
63  for (std::size_t i = 0; i < vec.size(); ++i)
64  vec(i) = NumericT(1.0) + NumericT(2.0) * randomNumber(); //some extra weight on diagonal for stability
65 }
66 
67 template<typename NumericT,typename MatrixT1, typename MatrixT2,typename MatrixT3, typename SolverTag>
68 void run_solver_matrix(MatrixT1 const & matrix1, MatrixT2 const & matrix2,MatrixT3 & result, SolverTag)
69 {
70  std::cout << "------- Solver tag: " <<SolverTag::name()<<" ----------" << std::endl;
71  result = viennacl::linalg::solve(matrix1, matrix2, SolverTag());
72 
75 
76  timer.start();
77  for (int runs=0; runs<BENCHMARK_RUNS; ++runs)
78  result = viennacl::linalg::solve(matrix1, matrix2, SolverTag());
79 
80  double exec_time = timer.get();
82  std::cout << "GPU: ";printOps(double(matrix1.size1() * matrix1.size1() * matrix2.size2()),(static_cast<double>(exec_time) / static_cast<double>(BENCHMARK_RUNS)));
83  std::cout << "GPU: " << double(matrix1.size1() * matrix1.size1() * matrix2.size2() * sizeof(NumericT)) / (static_cast<double>(exec_time) / static_cast<double>(BENCHMARK_RUNS)) / 1e9 << " GB/sec" << std::endl;
84  std::cout << "Execution time: " << exec_time/BENCHMARK_RUNS << std::endl;
85  std::cout << "------- Finnished: " << SolverTag::name() << " ----------" << std::endl;
86 }
87 
88 template<typename NumericT,typename VectorT, typename VectorT2,typename MatrixT, typename SolverTag>
89 void run_solver_vector(MatrixT const & matrix, VectorT2 const & vector2,VectorT & result, SolverTag)
90 {
91  std::cout << "------- Solver tag: " <<SolverTag::name()<<" ----------" << std::endl;
92  result = viennacl::linalg::solve(matrix, vector2, SolverTag());
93 
96 
97  timer.start();
98  for (int runs=0; runs<BENCHMARK_RUNS; ++runs)
99  {
100  result = viennacl::linalg::solve(matrix, vector2, SolverTag());
101  }
102  double exec_time = timer.get();
104  std::cout << "GPU: ";printOps(double(matrix.size1() * matrix.size1()),(static_cast<double>(exec_time) / static_cast<double>(BENCHMARK_RUNS)));
105  std::cout << "GPU: "<< double(matrix.size1() * matrix.size1() * sizeof(NumericT)) / (static_cast<double>(exec_time) / static_cast<double>(BENCHMARK_RUNS)) / 1e9 << " GB/sec" << std::endl;
106  std::cout << "Execution time: " << exec_time/BENCHMARK_RUNS << std::endl;
107  std::cout << "------- Finished: " << SolverTag::name() << " ----------" << std::endl;
108 }
109 
110 template<typename NumericT,typename F_A, typename F_B>
112 {
113  std::size_t matrix_size = 1500; //some odd number, not too large
114  std::size_t rhs_num = 153;
115 
116  viennacl::matrix<NumericT, F_A> vcl_A(matrix_size, matrix_size);
117  viennacl::matrix<NumericT, F_B> vcl_B(matrix_size, rhs_num);
118  viennacl::matrix<NumericT, F_B> result(matrix_size, rhs_num);
119 
120  viennacl::vector<NumericT> vcl_vec_B(matrix_size);
121  viennacl::vector<NumericT> vcl_vec_result(matrix_size);
122 
123  fill_matrix(vcl_A);
124  fill_matrix(vcl_B);
125 
126  fill_vector(vcl_vec_B);
127  std::cout << "------- Solve Matrix-Matrix: ----------\n" << std::endl;
128  run_solver_matrix<NumericT>(vcl_A,vcl_B,result,viennacl::linalg::lower_tag());
129  run_solver_matrix<NumericT>(vcl_A,vcl_B,result,viennacl::linalg::unit_lower_tag());
130  run_solver_matrix<NumericT>(vcl_A,vcl_B,result,viennacl::linalg::upper_tag());
131  run_solver_matrix<NumericT>(vcl_A,vcl_B,result,viennacl::linalg::unit_upper_tag());
132  std::cout << "------- End Matrix-Matrix: ----------\n" << std::endl;
133 
134  std::cout << "------- Solve Matrix-Vector: ----------\n" << std::endl;
135  run_solver_vector<NumericT>(vcl_A,vcl_vec_B,vcl_vec_result,viennacl::linalg::lower_tag());
136  run_solver_vector<NumericT>(vcl_A,vcl_vec_B,vcl_vec_result,viennacl::linalg::unit_lower_tag());
137  run_solver_vector<NumericT>(vcl_A,vcl_vec_B,vcl_vec_result,viennacl::linalg::upper_tag());
138  run_solver_vector<NumericT>(vcl_A,vcl_vec_B,vcl_vec_result,viennacl::linalg::unit_upper_tag());
139  std::cout << "------- End Matrix-Vector: ----------\n" << std::endl;
140 }
141 
142 int main()
143 {
144  std::cout << std::endl;
145  std::cout << "----------------------------------------------" << std::endl;
146  std::cout << " Device Info" << std::endl;
147  std::cout << "----------------------------------------------" << std::endl;
148 
149 #ifdef VIENNACL_WITH_OPENCL
150  std::cout << viennacl::ocl::current_device().info() << std::endl;
151 #endif
152  std::cout << std::endl;
153  std::cout << "----------------------------------------------" << std::endl;
154  std::cout << "----------------------------------------------" << std::endl;
155  std::cout << "## Benchmark :: Direct solve" << std::endl;
156  std::cout << "----------------------------------------------" << std::endl;
157  std::cout << std::endl;
158  std::cout << " -------------------------------" << std::endl;
159  std::cout << " # benchmarking single-precision" << std::endl;
160  std::cout << " -------------------------------" << std::endl;
161  run_benchmark<float,viennacl::row_major,viennacl::row_major>();
162 #ifdef VIENNACL_WITH_OPENCL
164 #endif
165  {
166  std::cout << std::endl;
167  std::cout << " -------------------------------" << std::endl;
168  std::cout << " # benchmarking double-precision" << std::endl;
169  std::cout << " -------------------------------" << std::endl;
170  run_benchmark<double,viennacl::row_major,viennacl::row_major>();
171  }
172  return 0;
173 }
Simple timer class based on gettimeofday (POSIX) or QueryPerformanceCounter (Windows).
Definition: timer.hpp:90
void run_solver_vector(MatrixT const &matrix, VectorT2 const &vector2, VectorT &result, SolverTag)
Generic interface for the l^2-norm. See viennacl/linalg/vector_operations.hpp for implementations...
void run_solver_matrix(MatrixT1 const &matrix1, MatrixT2 const &matrix2, MatrixT3 &result, SolverTag)
Generic interface for matrix-vector and matrix-matrix products. See viennacl/linalg/vector_operations...
Implementation of the dense matrix class.
A tag class representing a lower triangular matrix.
Definition: forwards.h:849
void finish()
Synchronizes the execution. finish() will only return after all compute kernels (CUDA, OpenCL) have completed.
Definition: memory.hpp:54
#define BENCHMARK_RUNS
viennacl::ocl::device const & current_device()
Convenience function for returning the active device in the current context.
Definition: backend.hpp:351
void fill_matrix(viennacl::matrix< NumericT > &mat)
VectorT solve(MatrixT const &matrix, VectorT const &rhs, bicgstab_tag const &tag, PreconditionerT const &precond)
Definition: bicgstab.hpp:496
std::string info(vcl_size_t indent=0, char indent_char= ' ') const
Returns an info string with a few properties of the device. Use full_info() to get all details...
Definition: device.hpp:995
float NumericT
Definition: bisect.cpp:40
Random number generator for returning uniformly distributed values in the closed interval [0...
Definition: random.hpp:44
A tag class representing an upper triangular matrix.
Definition: forwards.h:854
void fill_vector(viennacl::vector< NumericT > &vec)
bool double_support() const
ViennaCL convenience function: Returns true if the device supports double precision.
Definition: device.hpp:956
size_type size2() const
Returns the number of columns.
Definition: matrix_def.hpp:226
int main()
void printOps(double num_ops, double exec_time)
size_type size1() const
Returns the number of rows.
Definition: matrix_def.hpp:224
Implementations of dense direct solvers are found here.
A simple, yet (mostly) sufficiently accurate timer for benchmarking and profiling.
Proxy classes for matrices.
The vector type with operator-overloads and proxy classes is defined here. Linear algebra operations ...
A small collection of sequential random number generators.
void run_benchmark()
size_type size() const
Returns the length of the vector (cf. std::vector)
Definition: vector_def.hpp:118
A tag class representing a lower triangular matrix with unit diagonal.
Definition: forwards.h:859
double get() const
Definition: timer.hpp:104
Implementation of the ViennaCL scalar class.
A tag class representing an upper triangular matrix with unit diagonal.
Definition: forwards.h:864