ViennaCL - The Vienna Computing Library: examples/benchmarks/opencl.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
opencl.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 /*
19 *
20 * Benchmark: Profiling performance of current OpenCL implementation
21 *
22 */
23
24
25 #ifndef NDEBUG
26 #define NDEBUG
27 #endif
28
29 #ifndef VIENNACL_WITH_OPENCL
30 #define VIENNACL_WITH_OPENCL
31 #endif
32
33 #include "viennacl/scalar.hpp"
34 #include "viennacl/vector.hpp"
35 #include "viennacl/matrix.hpp"
36 #include "viennacl/compressed_matrix.hpp"
37 #include "viennacl/tools/timer.hpp"
38
39 #include <iostream>
40 #include <vector>
41
42 using std::cout;
43 using std::cin;
44 using std::endl;
45
46
47 #define BENCHMARK_VECTOR_SIZE 100000
48
49
50 template<typename ScalarType>
51 int run_benchmark()
52 {
53
54 viennacl::tools::timer timer;
55 double exec_time;
56
57 std::vector<ScalarType> std_vec1(BENCHMARK_VECTOR_SIZE);
58
59
60 viennacl::ocl::get_queue().finish();
61
62 timer.start();
63 viennacl::scalar<ScalarType> vcl_s1;
64 exec_time = timer.get();
65 std::cout << "Time for building scalar kernels: " << exec_time << std::endl;
66
67 timer.start();
68 viennacl::vector<ScalarType> vcl_vec1(BENCHMARK_VECTOR_SIZE);
69 exec_time = timer.get();
70 viennacl::vector<ScalarType> vcl_vec2(BENCHMARK_VECTOR_SIZE);
71 std::cout << "Time for building vector kernels: " << exec_time << std::endl;
72
73 timer.start();
74 viennacl::matrix<ScalarType> vcl_matrix(BENCHMARK_VECTOR_SIZE/100, BENCHMARK_VECTOR_SIZE/100);
75 exec_time = timer.get();
76 std::cout << "Time for building matrix kernels: " << exec_time << std::endl;
77
78 timer.start();
79 viennacl::compressed_matrix<ScalarType> vcl_compressed_matrix(BENCHMARK_VECTOR_SIZE, BENCHMARK_VECTOR_SIZE);
80 exec_time = timer.get();
81 std::cout << "Time for building compressed_matrix kernels: " << exec_time << std::endl;
82
83
84
86
87 std_vec1[0] = 1.0;
88 for (std::size_t i=1; i<BENCHMARK_VECTOR_SIZE; ++i)
89 std_vec1[i] = std_vec1[i-1] * ScalarType(1.000001);
90
91 viennacl::copy(std_vec1, vcl_vec1);
92
93 double std_accumulate = 0;
94 double vcl_accumulate = 0;
95
96 timer.start();
97 for (std::size_t i=0; i<BENCHMARK_VECTOR_SIZE; ++i)
98 std_accumulate += std_vec1[i];
99 exec_time = timer.get();
100 std::cout << "Time for " << BENCHMARK_VECTOR_SIZE << " entry accesses on host: " << exec_time << std::endl;
101 std::cout << "Time per entry: " << exec_time / BENCHMARK_VECTOR_SIZE << std::endl;
102 std::cout << "Result of operation on host: " << std_accumulate << std::endl;
103
104 vcl_accumulate = vcl_vec1[0];
105 viennacl::ocl::get_queue().finish();
106 vcl_accumulate = 0;
107 timer.start();
108 for (std::size_t i=0; i<BENCHMARK_VECTOR_SIZE; ++i)
109 vcl_accumulate += vcl_vec1[i];
110 exec_time = timer.get();
111 std::cout << "Time for " << BENCHMARK_VECTOR_SIZE << " entry accesses via OpenCL: " << exec_time << std::endl;
112 std::cout << "Time per entry: " << exec_time / BENCHMARK_VECTOR_SIZE << std::endl;
113 std::cout << "Result of operation via OpenCL: " << vcl_accumulate << std::endl;
114
115 return 0;
116 }
117
118 int main()
119 {
120 std::cout << std::endl;
121 std::cout << "----------------------------------------------" << std::endl;
122 std::cout << " Device Info" << std::endl;
123 std::cout << "----------------------------------------------" << std::endl;
124
125 std::cout << viennacl::ocl::current_device().info() << std::endl;
126
127 std::cout << std::endl;
128 std::cout << "----------------------------------------------" << std::endl;
129 std::cout << "----------------------------------------------" << std::endl;
130 std::cout << "## Benchmark :: OpenCL performance" << std::endl;
131 std::cout << "----------------------------------------------" << std::endl;
132 std::cout << std::endl;
133 std::cout << " -------------------------------" << std::endl;
134 std::cout << " # benchmarking single-precision" << std::endl;
135 std::cout << " -------------------------------" << std::endl;
136 run_benchmark<float>();
137 if ( viennacl::ocl::current_device().double_support() )
138 {
139 std::cout << std::endl;
140 std::cout << " -------------------------------" << std::endl;
141 std::cout << " # benchmarking double-precision" << std::endl;
142 std::cout << " -------------------------------" << std::endl;
143 run_benchmark<double>();
144 }
145 return 0;
146 }
147
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::ocl::command_queue::finish
void finish() const
Waits until all kernels in the queue have finished their execution.
Definition: command_queue.hpp:70
run_benchmark
int run_benchmark()
Definition: opencl.cpp:51
matrix.hpp
Implementation of the dense matrix class.
viennacl::matrix
A dense matrix class.
Definition: forwards.h:375
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
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
viennacl::ocl::get_queue
viennacl::ocl::command_queue & get_queue()
Convenience function for getting the default queue for the currently active device in the active cont...
Definition: backend.hpp:320
compressed_matrix.hpp
Implementation of the compressed_matrix class.
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::vector< ScalarType >
timer.hpp
A simple, yet (mostly) sufficiently accurate timer for benchmarking and profiling.
BENCHMARK_VECTOR_SIZE
#define BENCHMARK_VECTOR_SIZE
Definition: opencl.cpp:47
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
main
int main()
Definition: opencl.cpp:118
viennacl::compressed_matrix
A sparse square matrix in compressed sparse rows format.
Definition: compressed_matrix.hpp:559
viennacl::tools::timer::get
double get() const
Definition: timer.hpp:104
viennacl::tools::timer::start
void start()
Definition: timer.hpp:97
scalar.hpp
Implementation of the ViennaCL scalar class.