blob: 3de953fbe7eba535b093304d8df197599723f26d [file] [log] [blame]
Anthony Barbier8140e1e2017-12-14 23:48:46 +00001/*
2 * Copyright (c) 2017 ARM Limited.
3 *
4 * SPDX-License-Identifier: MIT
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to
8 * deal in the Software without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24#include "OpenCLTimer.h"
25
26#include "../Framework.h"
27#include "../Utils.h"
28
29#include "arm_compute/runtime/CL/CLScheduler.h"
30
31#ifndef ARM_COMPUTE_CL
32#error "You can't use OpenCLTimer without OpenCL"
33#endif /* ARM_COMPUTE_CL */
34
35namespace arm_compute
36{
37namespace test
38{
39namespace framework
40{
41std::string OpenCLTimer::id() const
42{
43 return "OpenCLTimer";
44}
45
46/* Function to be used to intercept kernel enqueues and store their OpenCL Event */
47class Interceptor
48{
49public:
50 explicit Interceptor(OpenCLTimer &timer)
51 : _timer(timer)
52 {
53 }
54
55 cl_int operator()(
56 cl_command_queue command_queue,
57 cl_kernel kernel,
58 cl_uint work_dim,
59 const size_t *gwo,
60 const size_t *gws,
61 const size_t *lws,
62 cl_uint num_events_in_wait_list,
63 const cl_event *event_wait_list,
64 cl_event *event)
65 {
66 ARM_COMPUTE_ERROR_ON_MSG(event != nullptr, "Not supported");
67 ARM_COMPUTE_UNUSED(event);
68
69 OpenCLTimer::kernel_info info;
70 cl::Kernel cpp_kernel(kernel, true);
71 std::stringstream ss;
72 ss << cpp_kernel.getInfo<CL_KERNEL_FUNCTION_NAME>();
73 if(gws != nullptr)
74 {
75 ss << " GWS[" << gws[0] << "," << gws[1] << "," << gws[2] << "]";
76 }
77 if(lws != nullptr)
78 {
79 ss << " LWS[" << lws[0] << "," << lws[1] << "," << lws[2] << "]";
80 }
81 info.name = ss.str();
82 cl_event tmp;
83 cl_int retval = _timer.real_function(command_queue, kernel, work_dim, gwo, gws, lws, num_events_in_wait_list, event_wait_list, &tmp);
84 info.event = tmp;
85 _timer.kernels.push_back(std::move(info));
86 return retval;
87 }
88
89private:
90 OpenCLTimer &_timer;
91};
92
93OpenCLTimer::OpenCLTimer(ScaleFactor scale_factor)
94 : real_function(CLSymbols::get().clEnqueueNDRangeKernel_ptr)
95{
96 auto q = CLScheduler::get().queue();
97 cl_command_queue_properties props = q.getInfo<CL_QUEUE_PROPERTIES>();
98 if((props & CL_QUEUE_PROFILING_ENABLE) == 0)
99 {
100 CLScheduler::get().set_queue(cl::CommandQueue(CLScheduler::get().context(), props | CL_QUEUE_PROFILING_ENABLE));
101 }
102
103 switch(scale_factor)
104 {
105 case ScaleFactor::NONE:
106 _scale_factor = 1.f;
107 _unit = "ns";
108 break;
109 case ScaleFactor::TIME_US:
110 _scale_factor = 1000.f;
111 _unit = "us";
112 break;
113 case ScaleFactor::TIME_MS:
114 _scale_factor = 1000000.f;
115 _unit = "ms";
116 break;
117 case ScaleFactor::TIME_S:
118 _scale_factor = 1000000000.f;
119 _unit = "s";
120 break;
121 default:
122 ARM_COMPUTE_ERROR("Invalid scale");
123 }
124}
125
126void OpenCLTimer::start()
127{
128 kernels.clear();
129 // Start intercepting enqueues:
130 CLSymbols::get().clEnqueueNDRangeKernel_ptr = Interceptor(*this);
131}
132
133void OpenCLTimer::stop()
134{
135 // Restore real function
136 CLSymbols::get().clEnqueueNDRangeKernel_ptr = real_function;
137}
138
139Instrument::MeasurementsMap OpenCLTimer::measurements() const
140{
141 MeasurementsMap measurements;
142 unsigned int kernel_number = 0;
143 for(auto kernel : kernels)
144 {
145 //cl_int status = kernel.event.getInfo(<CL_EVENT_COMMAND_EXECUTION_STATUS>();
146 cl_ulong queued = kernel.event.getProfilingInfo<CL_PROFILING_COMMAND_QUEUED>();
147 cl_ulong submit = kernel.event.getProfilingInfo<CL_PROFILING_COMMAND_SUBMIT>();
148 cl_ulong start = kernel.event.getProfilingInfo<CL_PROFILING_COMMAND_START>();
149 cl_ulong end = kernel.event.getProfilingInfo<CL_PROFILING_COMMAND_END>();
150
151 std::list<std::string> raw_data =
152 {
153 "queued", support::cpp11::to_string(queued),
154 "submit", support::cpp11::to_string(submit),
155 "start", support::cpp11::to_string(start),
156 "end", support::cpp11::to_string(end),
157 };
158 measurements.emplace(kernel.name + " #" + support::cpp11::to_string(kernel_number++), Measurement((end - start) / _scale_factor, _unit, raw_data));
159 }
160
161 return measurements;
162}
163} // namespace framework
164} // namespace test
165} // namespace arm_compute