Anthony Barbier | 6e43349 | 2017-11-09 15:52:00 +0000 | [diff] [blame] | 1 | /* |
Georgios Pinitas | b6af482 | 2021-09-14 12:33:34 +0100 | [diff] [blame] | 2 | * Copyright (c) 2017-2019, 2021 Arm Limited. |
Anthony Barbier | 6e43349 | 2017-11-09 15:52:00 +0000 | [diff] [blame] | 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 | |
Georgios Pinitas | 5c2fb3f | 2018-05-01 15:26:20 +0100 | [diff] [blame] | 29 | #include "arm_compute/graph/INode.h" |
Anthony Barbier | 6e43349 | 2017-11-09 15:52:00 +0000 | [diff] [blame] | 30 | #include "arm_compute/runtime/CL/CLScheduler.h" |
| 31 | |
| 32 | #ifndef ARM_COMPUTE_CL |
| 33 | #error "You can't use OpenCLTimer without OpenCL" |
| 34 | #endif /* ARM_COMPUTE_CL */ |
| 35 | |
| 36 | namespace arm_compute |
| 37 | { |
| 38 | namespace test |
| 39 | { |
| 40 | namespace framework |
| 41 | { |
Anthony Barbier | 72f4ae5 | 2018-11-07 17:33:54 +0000 | [diff] [blame] | 42 | template <bool output_timestamps> |
| 43 | std::string OpenCLClock<output_timestamps>::id() const |
Anthony Barbier | 6e43349 | 2017-11-09 15:52:00 +0000 | [diff] [blame] | 44 | { |
Anthony Barbier | 72f4ae5 | 2018-11-07 17:33:54 +0000 | [diff] [blame] | 45 | if(output_timestamps) |
| 46 | { |
| 47 | return "OpenCLTimestamps"; |
| 48 | } |
| 49 | else |
| 50 | { |
| 51 | return "OpenCLTimer"; |
| 52 | } |
Anthony Barbier | 6e43349 | 2017-11-09 15:52:00 +0000 | [diff] [blame] | 53 | } |
| 54 | |
Anthony Barbier | 72f4ae5 | 2018-11-07 17:33:54 +0000 | [diff] [blame] | 55 | template <bool output_timestamps> |
| 56 | OpenCLClock<output_timestamps>::OpenCLClock(ScaleFactor scale_factor) |
Georgios Pinitas | b6af482 | 2021-09-14 12:33:34 +0100 | [diff] [blame] | 57 | : _kernels(), |
| 58 | _real_function(nullptr), |
| 59 | #ifdef ARM_COMPUTE_GRAPH_ENABLED |
| 60 | _real_graph_function(nullptr), |
| 61 | #endif /* ARM_COMPUTE_GRAPH_ENABLED */ |
| 62 | _prefix(), |
| 63 | _timer_enabled(false) |
Anthony Barbier | 6e43349 | 2017-11-09 15:52:00 +0000 | [diff] [blame] | 64 | { |
| 65 | auto q = CLScheduler::get().queue(); |
| 66 | cl_command_queue_properties props = q.getInfo<CL_QUEUE_PROPERTIES>(); |
| 67 | if((props & CL_QUEUE_PROFILING_ENABLE) == 0) |
| 68 | { |
| 69 | CLScheduler::get().set_queue(cl::CommandQueue(CLScheduler::get().context(), props | CL_QUEUE_PROFILING_ENABLE)); |
| 70 | } |
Giorgio Arena | ce58a9f | 2017-10-31 17:59:17 +0000 | [diff] [blame] | 71 | |
| 72 | switch(scale_factor) |
| 73 | { |
| 74 | case ScaleFactor::NONE: |
| 75 | _scale_factor = 1.f; |
| 76 | _unit = "ns"; |
| 77 | break; |
| 78 | case ScaleFactor::TIME_US: |
| 79 | _scale_factor = 1000.f; |
| 80 | _unit = "us"; |
| 81 | break; |
| 82 | case ScaleFactor::TIME_MS: |
| 83 | _scale_factor = 1000000.f; |
| 84 | _unit = "ms"; |
| 85 | break; |
| 86 | case ScaleFactor::TIME_S: |
| 87 | _scale_factor = 1000000000.f; |
| 88 | _unit = "s"; |
| 89 | break; |
| 90 | default: |
| 91 | ARM_COMPUTE_ERROR("Invalid scale"); |
| 92 | } |
Anthony Barbier | 6e43349 | 2017-11-09 15:52:00 +0000 | [diff] [blame] | 93 | } |
| 94 | |
Anthony Barbier | 72f4ae5 | 2018-11-07 17:33:54 +0000 | [diff] [blame] | 95 | template <bool output_timestamps> |
| 96 | void OpenCLClock<output_timestamps>::test_start() |
Anthony Barbier | 6e43349 | 2017-11-09 15:52:00 +0000 | [diff] [blame] | 97 | { |
Anthony Barbier | 6e43349 | 2017-11-09 15:52:00 +0000 | [diff] [blame] | 98 | // Start intercepting enqueues: |
Georgios Pinitas | 5c2fb3f | 2018-05-01 15:26:20 +0100 | [diff] [blame] | 99 | ARM_COMPUTE_ERROR_ON(_real_function != nullptr); |
Georgios Pinitas | b6af482 | 2021-09-14 12:33:34 +0100 | [diff] [blame] | 100 | _real_function = CLSymbols::get().clEnqueueNDRangeKernel_ptr; |
| 101 | auto interceptor = [this]( |
| 102 | cl_command_queue command_queue, |
| 103 | cl_kernel kernel, |
| 104 | cl_uint work_dim, |
| 105 | const size_t *gwo, |
| 106 | const size_t *gws, |
| 107 | const size_t *lws, |
| 108 | cl_uint num_events_in_wait_list, |
| 109 | const cl_event * event_wait_list, |
| 110 | cl_event * event) |
Anthony Barbier | 48c19f1 | 2018-04-20 11:31:52 +0100 | [diff] [blame] | 111 | { |
Anthony Barbier | c483521 | 2018-05-16 14:20:04 +0100 | [diff] [blame] | 112 | if(this->_timer_enabled) |
| 113 | { |
Anthony Barbier | 72f4ae5 | 2018-11-07 17:33:54 +0000 | [diff] [blame] | 114 | kernel_info info; |
| 115 | cl::Kernel cpp_kernel(kernel, true); |
| 116 | std::stringstream ss; |
Anthony Barbier | c483521 | 2018-05-16 14:20:04 +0100 | [diff] [blame] | 117 | ss << this->_prefix << cpp_kernel.getInfo<CL_KERNEL_FUNCTION_NAME>(); |
| 118 | if(gws != nullptr) |
| 119 | { |
| 120 | ss << " GWS[" << gws[0] << "," << gws[1] << "," << gws[2] << "]"; |
| 121 | } |
| 122 | if(lws != nullptr) |
| 123 | { |
| 124 | ss << " LWS[" << lws[0] << "," << lws[1] << "," << lws[2] << "]"; |
| 125 | } |
| 126 | info.name = ss.str(); |
| 127 | cl_event tmp; |
| 128 | cl_int retval = this->_real_function(command_queue, kernel, work_dim, gwo, gws, lws, num_events_in_wait_list, event_wait_list, &tmp); |
| 129 | info.event = tmp; |
| 130 | this->_kernels.push_back(std::move(info)); |
Vidhya Sudhan Loganathan | ca65af3 | 2019-02-07 11:14:42 +0000 | [diff] [blame] | 131 | |
| 132 | if(event != nullptr) |
| 133 | { |
| 134 | //return cl_event from the intercepted call |
| 135 | clRetainEvent(tmp); |
| 136 | *event = tmp; |
| 137 | } |
Anthony Barbier | c483521 | 2018-05-16 14:20:04 +0100 | [diff] [blame] | 138 | return retval; |
Anthony Barbier | 48c19f1 | 2018-04-20 11:31:52 +0100 | [diff] [blame] | 139 | } |
Anthony Barbier | c483521 | 2018-05-16 14:20:04 +0100 | [diff] [blame] | 140 | else |
Anthony Barbier | 48c19f1 | 2018-04-20 11:31:52 +0100 | [diff] [blame] | 141 | { |
Anthony Barbier | c483521 | 2018-05-16 14:20:04 +0100 | [diff] [blame] | 142 | return this->_real_function(command_queue, kernel, work_dim, gwo, gws, lws, num_events_in_wait_list, event_wait_list, event); |
Anthony Barbier | 48c19f1 | 2018-04-20 11:31:52 +0100 | [diff] [blame] | 143 | } |
Anthony Barbier | 48c19f1 | 2018-04-20 11:31:52 +0100 | [diff] [blame] | 144 | }; |
Georgios Pinitas | b6af482 | 2021-09-14 12:33:34 +0100 | [diff] [blame] | 145 | CLSymbols::get().clEnqueueNDRangeKernel_ptr = interceptor; |
Anthony Barbier | 48c19f1 | 2018-04-20 11:31:52 +0100 | [diff] [blame] | 146 | |
Georgios Pinitas | b6af482 | 2021-09-14 12:33:34 +0100 | [diff] [blame] | 147 | #ifdef ARM_COMPUTE_GRAPH_ENABLED |
| 148 | ARM_COMPUTE_ERROR_ON(_real_graph_function != nullptr); |
| 149 | _real_graph_function = graph::TaskExecutor::get().execute_function; |
Georgios Pinitas | 5c2fb3f | 2018-05-01 15:26:20 +0100 | [diff] [blame] | 150 | // Start intercepting tasks: |
| 151 | auto task_interceptor = [this](graph::ExecutionTask & task) |
| 152 | { |
| 153 | if(task.node != nullptr && !task.node->name().empty()) |
| 154 | { |
| 155 | this->_prefix = task.node->name() + "/"; |
| 156 | } |
| 157 | else |
| 158 | { |
| 159 | this->_prefix = ""; |
| 160 | } |
| 161 | this->_real_graph_function(task); |
| 162 | this->_prefix = ""; |
| 163 | }; |
Georgios Pinitas | 5c2fb3f | 2018-05-01 15:26:20 +0100 | [diff] [blame] | 164 | graph::TaskExecutor::get().execute_function = task_interceptor; |
Georgios Pinitas | b6af482 | 2021-09-14 12:33:34 +0100 | [diff] [blame] | 165 | #endif /* ARM_COMPUTE_GRAPH_ENABLED */ |
Anthony Barbier | 6e43349 | 2017-11-09 15:52:00 +0000 | [diff] [blame] | 166 | } |
| 167 | |
Anthony Barbier | 72f4ae5 | 2018-11-07 17:33:54 +0000 | [diff] [blame] | 168 | template <bool output_timestamps> |
| 169 | void OpenCLClock<output_timestamps>::start() |
Georgios Pinitas | 5c2fb3f | 2018-05-01 15:26:20 +0100 | [diff] [blame] | 170 | { |
| 171 | _kernels.clear(); |
Anthony Barbier | c483521 | 2018-05-16 14:20:04 +0100 | [diff] [blame] | 172 | _timer_enabled = true; |
| 173 | } |
Anthony Barbier | 72f4ae5 | 2018-11-07 17:33:54 +0000 | [diff] [blame] | 174 | template <bool output_timestamps> |
| 175 | void OpenCLClock<output_timestamps>::stop() |
Anthony Barbier | c483521 | 2018-05-16 14:20:04 +0100 | [diff] [blame] | 176 | { |
| 177 | _timer_enabled = false; |
Georgios Pinitas | 5c2fb3f | 2018-05-01 15:26:20 +0100 | [diff] [blame] | 178 | } |
| 179 | |
Anthony Barbier | 72f4ae5 | 2018-11-07 17:33:54 +0000 | [diff] [blame] | 180 | template <bool output_timestamps> |
| 181 | void OpenCLClock<output_timestamps>::test_stop() |
Anthony Barbier | 6e43349 | 2017-11-09 15:52:00 +0000 | [diff] [blame] | 182 | { |
| 183 | // Restore real function |
Georgios Pinitas | 5c2fb3f | 2018-05-01 15:26:20 +0100 | [diff] [blame] | 184 | CLSymbols::get().clEnqueueNDRangeKernel_ptr = _real_function; |
Georgios Pinitas | b6af482 | 2021-09-14 12:33:34 +0100 | [diff] [blame] | 185 | _real_function = nullptr; |
| 186 | #ifdef ARM_COMPUTE_GRAPH_ENABLED |
Georgios Pinitas | 5c2fb3f | 2018-05-01 15:26:20 +0100 | [diff] [blame] | 187 | graph::TaskExecutor::get().execute_function = _real_graph_function; |
| 188 | _real_graph_function = nullptr; |
Georgios Pinitas | b6af482 | 2021-09-14 12:33:34 +0100 | [diff] [blame] | 189 | #endif /* ARM_COMPUTE_GRAPH_ENABLED */ |
Anthony Barbier | 6e43349 | 2017-11-09 15:52:00 +0000 | [diff] [blame] | 190 | } |
| 191 | |
Anthony Barbier | 72f4ae5 | 2018-11-07 17:33:54 +0000 | [diff] [blame] | 192 | template <bool output_timestamps> |
| 193 | Instrument::MeasurementsMap OpenCLClock<output_timestamps>::measurements() const |
Anthony Barbier | 6e43349 | 2017-11-09 15:52:00 +0000 | [diff] [blame] | 194 | { |
| 195 | MeasurementsMap measurements; |
| 196 | unsigned int kernel_number = 0; |
Michalis Spyrou | bcfd09a | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 197 | for(auto const &kernel : _kernels) |
Anthony Barbier | 6e43349 | 2017-11-09 15:52:00 +0000 | [diff] [blame] | 198 | { |
Michalis Spyrou | bcfd09a | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 199 | cl_ulong queued; |
| 200 | cl_ulong flushed; |
| 201 | cl_ulong start; |
| 202 | cl_ulong end; |
Anthony Barbier | 1e1bef1 | 2018-11-20 16:54:42 +0000 | [diff] [blame] | 203 | kernel.event.getProfilingInfo(CL_PROFILING_COMMAND_QUEUED, &queued); |
| 204 | kernel.event.getProfilingInfo(CL_PROFILING_COMMAND_SUBMIT, &flushed); |
Anthony Barbier | 72f4ae5 | 2018-11-07 17:33:54 +0000 | [diff] [blame] | 205 | kernel.event.getProfilingInfo(CL_PROFILING_COMMAND_START, &start); |
| 206 | kernel.event.getProfilingInfo(CL_PROFILING_COMMAND_END, &end); |
| 207 | std::string name = kernel.name + " #" + support::cpp11::to_string(kernel_number++); |
Anthony Barbier | 6e43349 | 2017-11-09 15:52:00 +0000 | [diff] [blame] | 208 | |
Anthony Barbier | 72f4ae5 | 2018-11-07 17:33:54 +0000 | [diff] [blame] | 209 | if(output_timestamps) |
| 210 | { |
| 211 | measurements.emplace("[start]" + name, Measurement(start / static_cast<cl_ulong>(_scale_factor), _unit)); |
Anthony Barbier | 1e1bef1 | 2018-11-20 16:54:42 +0000 | [diff] [blame] | 212 | measurements.emplace("[queued]" + name, Measurement(queued / static_cast<cl_ulong>(_scale_factor), _unit)); |
| 213 | measurements.emplace("[flushed]" + name, Measurement(flushed / static_cast<cl_ulong>(_scale_factor), _unit)); |
Anthony Barbier | 72f4ae5 | 2018-11-07 17:33:54 +0000 | [diff] [blame] | 214 | measurements.emplace("[end]" + name, Measurement(end / static_cast<cl_ulong>(_scale_factor), _unit)); |
| 215 | } |
| 216 | else |
| 217 | { |
| 218 | measurements.emplace(name, Measurement((end - start) / _scale_factor, _unit)); |
| 219 | } |
Anthony Barbier | 6e43349 | 2017-11-09 15:52:00 +0000 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | return measurements; |
| 223 | } |
Anthony Barbier | 72f4ae5 | 2018-11-07 17:33:54 +0000 | [diff] [blame] | 224 | |
Anthony Barbier | e7f4a43 | 2018-11-09 10:58:40 +0000 | [diff] [blame] | 225 | template <bool output_timestamps> |
| 226 | Instrument::MeasurementsMap OpenCLClock<output_timestamps>::test_measurements() const |
| 227 | { |
| 228 | MeasurementsMap measurements; |
| 229 | |
| 230 | if(output_timestamps) |
| 231 | { |
| 232 | // The OpenCL clock and the wall clock are not in sync, so we use |
| 233 | // this trick to calculate the offset between the two clocks: |
| 234 | ::cl::Event event; |
| 235 | cl_ulong now_gpu; |
| 236 | |
| 237 | // Enqueue retrieve current CPU clock and enqueue a dummy marker |
| 238 | std::chrono::high_resolution_clock::time_point now_cpu = std::chrono::high_resolution_clock::now(); |
| 239 | CLScheduler::get().queue().enqueueMarker(&event); |
| 240 | |
| 241 | CLScheduler::get().queue().finish(); |
| 242 | //Access the time at which the marker was enqueued: |
| 243 | event.getProfilingInfo(CL_PROFILING_COMMAND_QUEUED, &now_gpu); |
| 244 | |
| 245 | measurements.emplace("Now Wall clock", Measurement(now_cpu.time_since_epoch().count() / 1000, "us")); |
| 246 | measurements.emplace("Now OpenCL", Measurement(now_gpu / static_cast<cl_ulong>(_scale_factor), _unit)); |
| 247 | } |
| 248 | |
| 249 | return measurements; |
| 250 | } |
| 251 | |
Anthony Barbier | 6e43349 | 2017-11-09 15:52:00 +0000 | [diff] [blame] | 252 | } // namespace framework |
| 253 | } // namespace test |
| 254 | } // namespace arm_compute |
Anthony Barbier | cc225be | 2018-11-09 15:35:20 +0000 | [diff] [blame] | 255 | |
| 256 | template class arm_compute::test::framework::OpenCLClock<true>; |
| 257 | template class arm_compute::test::framework::OpenCLClock<false>; |