ViennaCL - The Vienna Computing Library: examples/benchmarks/dense_blas.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
dense_blas.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 #include "viennacl/matrix.hpp"
19 #include "viennacl/matrix_proxy.hpp"
20 #include "viennacl/vector.hpp"
21 #include "viennacl/vector_proxy.hpp"
22
23 #include "viennacl/linalg/inner_prod.hpp"
24 #include "viennacl/linalg/prod.hpp"
25 #include "viennacl/linalg/lu.hpp"
26 #include "viennacl/tools/timer.hpp"
27
28 #include <iomanip>
29 #include <stdlib.h>
30
31 template<class T, class F>
32 void init_random(viennacl::matrix<T, F> & M)
33 {
34 std::vector<T> cM(M.internal_size());
35 for (std::size_t i = 0; i < M.size1(); ++i)
36 for (std::size_t j = 0; j < M.size2(); ++j)
37 cM[F::mem_index(i, j, M.internal_size1(), M.internal_size2())] = T(rand())/T(RAND_MAX);
38 viennacl::fast_copy(&cM[0],&cM[0] + cM.size(),M);
39 }
40
41 template<class T>
42 void init_random(viennacl::vector<T> & x)
43 {
44 std::vector<T> cx(x.internal_size());
45 for (std::size_t i = 0; i < cx.size(); ++i)
46 cx[i] = T(rand())/T(RAND_MAX);
47 viennacl::fast_copy(&cx[0], &cx[0] + cx.size(), x.begin());
48 }
49
50 template<class T>
51 void bench(size_t BLAS1_N, size_t BLAS2_M, size_t BLAS2_N, size_t BLAS3_M, size_t BLAS3_N, size_t BLAS3_K, std::string const & prefix)
52 {
53 using viennacl::linalg::inner_prod;
54 using viennacl::linalg::prod;
55 using viennacl::linalg::lu_factorize;
56 using viennacl::trans;
57
58 viennacl::tools::timer timer;
59 double time_previous, time_spent;
60 size_t Nruns;
61 double time_per_benchmark = 1;
62
63 #define BENCHMARK_OP(OPERATION, NAME, PERF, INDEX) \
64 OPERATION; \
65 viennacl::backend::finish();\
66 timer.start(); \
67 Nruns = 0; \
68 time_spent = 0; \
69 while (time_spent < time_per_benchmark) \
70 { \
71 time_previous = timer.get(); \
72 OPERATION; \
73 viennacl::backend::finish(); \
74 time_spent += timer.get() - time_previous; \
75 Nruns+=1; \
76 } \
77 time_spent/=(double)Nruns; \
78 std::cout << prefix << NAME " : " << PERF << " " INDEX << std::endl; \
79
80 //BLAS1
81 {
82 viennacl::scalar<T> s(0);
83 T alpha = (T)2.4;
84 viennacl::vector<T> x(BLAS1_N);
85 viennacl::vector<T> y(BLAS1_N);
86 viennacl::vector<T> z(BLAS1_N);
87
88 init_random(x);
89 init_random(y);
90 init_random(z);
91
92 BENCHMARK_OP(x = y, "COPY", std::setprecision(3) << double(2*BLAS1_N*sizeof(T))/time_spent * 1e-9, "GB/s")
93 BENCHMARK_OP(x = y + alpha*x, "AXPY", std::setprecision(3) << double(3*BLAS1_N*sizeof(T))/time_spent * 1e-9, "GB/s")
94 BENCHMARK_OP(s = inner_prod(x, y), "DOT", std::setprecision(3) << double(2*BLAS1_N*sizeof(T))/time_spent * 1e-9, "GB/s")
95 }
96
97
98 //BLAS2
99 {
100 viennacl::matrix<T,viennacl::column_major> A(BLAS2_M, BLAS2_N);
101 viennacl::vector<T> x(BLAS2_N);
102 viennacl::vector<T> y(BLAS2_M);
103 init_random(A);
104 init_random(x);
105 init_random(y);
106
107 BENCHMARK_OP(y = prod(A, x), "GEMV-N", std::setprecision(3) << double((BLAS2_M + BLAS2_N + BLAS2_M*BLAS2_N)*sizeof(T))/time_spent * 1e-9, "GB/s")
108 BENCHMARK_OP(x = prod(trans(A), y), "GEMV-T", std::setprecision(3) << double((BLAS2_M + BLAS2_N + BLAS2_M*BLAS2_N)*sizeof(T))/time_spent * 1e-9, "GB/s")
109 }
110
111 //BLAS3
112 {
113 viennacl::matrix<T,viennacl::column_major> C(BLAS3_M, BLAS3_N);
114 viennacl::matrix<T,viennacl::column_major> A(BLAS3_M, BLAS3_K);
115 viennacl::matrix<T,viennacl::column_major> B(BLAS3_K, BLAS3_N);
116 viennacl::matrix<T,viennacl::column_major> AT = trans(A);
117 viennacl::matrix<T,viennacl::column_major> BT = trans(B);
118 init_random(A);
119 init_random(B);
120
121 BENCHMARK_OP(C = prod(A, B), "GEMM-NN", double(2*BLAS3_M*BLAS3_N*BLAS3_K)/time_spent*1e-9, "GFLOPs/s");
122 BENCHMARK_OP(C = prod(A, trans(BT)), "GEMM-NT", double(2*BLAS3_M*BLAS3_N*BLAS3_K)/time_spent*1e-9, "GFLOPs/s");
123 BENCHMARK_OP(C = prod(trans(AT), B), "GEMM-TN", double(2*BLAS3_M*BLAS3_N*BLAS3_K)/time_spent*1e-9, "GFLOPs/s");
124 BENCHMARK_OP(C = prod(trans(AT), trans(BT)), "GEMM-TT", double(2*BLAS3_M*BLAS3_N*BLAS3_K)/time_spent*1e-9, "GFLOPs/s");
125 //BENCHMARK_OP(lu_factorize(A), "LU-FACTORIZE", double(2*BLAS3_M*BLAS3_K*BLAS3_K)/time_spent*1e-9, "GFLOPs/s");
126 }
127
128
129 }
130
131 int main()
132 {
133 #ifdef VIENNACL_WITH_OPENCL
134 std::cout << std::endl;
135 std::cout << "----------------------------------------------" << std::endl;
136 std::cout << " Device Info" << std::endl;
137 std::cout << "----------------------------------------------" << std::endl;
138 std::cout << std::endl;
139 std::cout << viennacl::ocl::current_device().info() << std::endl;
140 std::cout << std::endl;
141 #endif
142
143 std::size_t BLAS1_N = 10000000;
144
145 std::size_t BLAS2_M = 3840;
146 std::size_t BLAS2_N = 3840;
147
148 std::size_t BLAS3_M = 1976;
149 std::size_t BLAS3_N = 1976;
150 std::size_t BLAS3_K = 1976;
151
152 std::cout << "Benchmark : BLAS" << std::endl;
153 std::cout << "----------------" << std::endl;
154 bench<float>(BLAS1_N, BLAS2_M, BLAS2_N, BLAS3_M, BLAS3_N, BLAS3_K, "s");
155 std::cout << "----" << std::endl;
156 #ifdef VIENNACL_WITH_OPENCL
157 if ( viennacl::ocl::current_device().double_support() )
158 #endif
159 bench<double>(BLAS1_N, BLAS2_M, BLAS2_N, BLAS3_M, BLAS3_N, BLAS3_K, "d");
160 }
init_random
void init_random(viennacl::matrix< T, F > &M)
Definition: dense_blas.cpp:32
viennacl::tools::timer
Simple timer class based on gettimeofday (POSIX) or QueryPerformanceCounter (Windows).
Definition: timer.hpp:90
viennacl::scalar
This class represents a single scalar value on the GPU and behaves mostly like a built-in scalar type...
Definition: forwards.h:227
viennacl::trans
viennacl::enable_if< viennacl::is_any_sparse_matrix< M1 >::value, matrix_expression< const M1, const M1, op_trans > >::type trans(const M1 &mat)
Returns an expression template class representing a transposed matrix.
Definition: sparse_matrix_operations.hpp:376
bench
void bench(size_t BLAS1_N, size_t BLAS2_M, size_t BLAS2_N, size_t BLAS3_M, size_t BLAS3_N, size_t BLAS3_K, std::string const &prefix)
Definition: dense_blas.cpp:51
viennacl::matrix_base< NumericT >::internal_size
size_type internal_size() const
Returns the total amount of allocated memory in multiples of sizeof(NumericT)
Definition: matrix_def.hpp:242
trans
std::vector< std::vector< NumericT > > trans(std::vector< std::vector< NumericT > > const &A)
Definition: blas3_solve.cpp:195
prod.hpp
Generic interface for matrix-vector and matrix-matrix products. See viennacl/linalg/vector_operations...
matrix.hpp
Implementation of the dense matrix class.
viennacl::matrix
A dense matrix class.
Definition: forwards.h:375
viennacl::linalg::inner_prod
viennacl::enable_if< viennacl::is_stl< typename viennacl::traits::tag_of< VectorT1 >::type >::value, typename VectorT1::value_type >::type inner_prod(VectorT1 const &v1, VectorT2 const &v2)
Definition: inner_prod.hpp:100
viennacl::ocl::current_device
viennacl::ocl::device const & current_device()
Convenience function for returning the active device in the current context.
Definition: backend.hpp:351
inner_prod.hpp
Generic interface for the computation of inner products. See viennacl/linalg/vector_operations.hpp for implementations.
viennacl::ocl::device::info
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
BENCHMARK_OP
#define BENCHMARK_OP(OPERATION, NAME, PERF, INDEX)
viennacl::linalg::prod
VectorT prod(std::vector< std::vector< T, A1 >, A2 > const &matrix, VectorT const &vector)
Definition: prod.hpp:102
viennacl::vector_base< NumericT >::begin
iterator begin()
Returns an iterator pointing to the beginning of the vector (STL like)
viennacl::ocl::device::double_support
bool double_support() const
ViennaCL convenience function: Returns true if the device supports double precision.
Definition: device.hpp:956
viennacl::matrix_base< NumericT >::size2
size_type size2() const
Returns the number of columns.
Definition: matrix_def.hpp:226
viennacl::vector
Definition: forwards.h:266
lu.hpp
Implementations of LU factorization for row-major and column-major dense matrices.
viennacl::matrix_base< NumericT >::size1
size_type size1() const
Returns the number of rows.
Definition: matrix_def.hpp:224
vector_proxy.hpp
Proxy classes for vectors.
timer.hpp
A simple, yet (mostly) sufficiently accurate timer for benchmarking and profiling.
matrix_proxy.hpp
Proxy classes for matrices.
prod
void prod(std::vector< std::map< IndexT, NumericT > > const &stl_A, std::vector< std::map< IndexT, NumericT > > const &stl_B, std::vector< std::map< IndexT, NumericT > > &stl_C)
Definition: sparse_prod.cpp:114
vector.hpp
The vector type with operator-overloads and proxy classes is defined here. Linear algebra operations ...
viennacl::matrix_base< NumericT >::internal_size2
size_type internal_size2() const
Returns the internal number of columns. Usually required for launching OpenCL kernels only...
Definition: matrix_def.hpp:240
main
int main()
Definition: dense_blas.cpp:131
viennacl::matrix_base< NumericT >::internal_size1
size_type internal_size1() const
Returns the internal number of rows. Usually required for launching OpenCL kernels only...
Definition: matrix_def.hpp:238
viennacl::vector_base< NumericT >::internal_size
size_type internal_size() const
Returns the internal length of the vector, which is given by size() plus the extra memory due to padd...
Definition: vector_def.hpp:120
viennacl::linalg::lu_factorize
void lu_factorize(matrix< NumericT, viennacl::row_major > &A)
LU factorization of a row-major dense matrix.
Definition: lu.hpp:42
viennacl::fast_copy
void fast_copy(const const_vector_iterator< SCALARTYPE, ALIGNMENT > &gpu_begin, const const_vector_iterator< SCALARTYPE, ALIGNMENT > &gpu_end, CPU_ITERATOR cpu_begin)