Anthony Barbier | 6e43349 | 2017-11-09 15:52:00 +0000 | [diff] [blame] | 1 | /* |
Anthony Barbier | 5d9d019 | 2018-01-26 16:38:07 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2018 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 | { |
| 42 | std::string OpenCLTimer::id() const |
| 43 | { |
| 44 | return "OpenCLTimer"; |
| 45 | } |
| 46 | |
Giorgio Arena | ce58a9f | 2017-10-31 17:59:17 +0000 | [diff] [blame] | 47 | OpenCLTimer::OpenCLTimer(ScaleFactor scale_factor) |
Georgios Pinitas | 5c2fb3f | 2018-05-01 15:26:20 +0100 | [diff] [blame^] | 48 | : _kernels(), _real_function(nullptr), _real_graph_function(nullptr), _prefix() |
Anthony Barbier | 6e43349 | 2017-11-09 15:52:00 +0000 | [diff] [blame] | 49 | { |
| 50 | auto q = CLScheduler::get().queue(); |
| 51 | cl_command_queue_properties props = q.getInfo<CL_QUEUE_PROPERTIES>(); |
| 52 | if((props & CL_QUEUE_PROFILING_ENABLE) == 0) |
| 53 | { |
| 54 | CLScheduler::get().set_queue(cl::CommandQueue(CLScheduler::get().context(), props | CL_QUEUE_PROFILING_ENABLE)); |
| 55 | } |
Giorgio Arena | ce58a9f | 2017-10-31 17:59:17 +0000 | [diff] [blame] | 56 | |
| 57 | switch(scale_factor) |
| 58 | { |
| 59 | case ScaleFactor::NONE: |
| 60 | _scale_factor = 1.f; |
| 61 | _unit = "ns"; |
| 62 | break; |
| 63 | case ScaleFactor::TIME_US: |
| 64 | _scale_factor = 1000.f; |
| 65 | _unit = "us"; |
| 66 | break; |
| 67 | case ScaleFactor::TIME_MS: |
| 68 | _scale_factor = 1000000.f; |
| 69 | _unit = "ms"; |
| 70 | break; |
| 71 | case ScaleFactor::TIME_S: |
| 72 | _scale_factor = 1000000000.f; |
| 73 | _unit = "s"; |
| 74 | break; |
| 75 | default: |
| 76 | ARM_COMPUTE_ERROR("Invalid scale"); |
| 77 | } |
Anthony Barbier | 6e43349 | 2017-11-09 15:52:00 +0000 | [diff] [blame] | 78 | } |
| 79 | |
Georgios Pinitas | 5c2fb3f | 2018-05-01 15:26:20 +0100 | [diff] [blame^] | 80 | void OpenCLTimer::test_start() |
Anthony Barbier | 6e43349 | 2017-11-09 15:52:00 +0000 | [diff] [blame] | 81 | { |
Anthony Barbier | 6e43349 | 2017-11-09 15:52:00 +0000 | [diff] [blame] | 82 | // Start intercepting enqueues: |
Georgios Pinitas | 5c2fb3f | 2018-05-01 15:26:20 +0100 | [diff] [blame^] | 83 | ARM_COMPUTE_ERROR_ON(_real_function != nullptr); |
| 84 | ARM_COMPUTE_ERROR_ON(_real_graph_function != nullptr); |
| 85 | _real_function = CLSymbols::get().clEnqueueNDRangeKernel_ptr; |
| 86 | _real_graph_function = graph::TaskExecutor::get().execute_function; |
| 87 | auto interceptor = [this]( |
| 88 | cl_command_queue command_queue, |
| 89 | cl_kernel kernel, |
| 90 | cl_uint work_dim, |
| 91 | const size_t *gwo, |
| 92 | const size_t *gws, |
| 93 | const size_t *lws, |
| 94 | cl_uint num_events_in_wait_list, |
| 95 | const cl_event * event_wait_list, |
| 96 | cl_event * event) |
Anthony Barbier | 48c19f1 | 2018-04-20 11:31:52 +0100 | [diff] [blame] | 97 | { |
| 98 | ARM_COMPUTE_ERROR_ON_MSG(event != nullptr, "Not supported"); |
| 99 | ARM_COMPUTE_UNUSED(event); |
| 100 | |
| 101 | OpenCLTimer::kernel_info info; |
| 102 | cl::Kernel cpp_kernel(kernel, true); |
| 103 | std::stringstream ss; |
Georgios Pinitas | 5c2fb3f | 2018-05-01 15:26:20 +0100 | [diff] [blame^] | 104 | ss << this->_prefix << cpp_kernel.getInfo<CL_KERNEL_FUNCTION_NAME>(); |
Anthony Barbier | 48c19f1 | 2018-04-20 11:31:52 +0100 | [diff] [blame] | 105 | if(gws != nullptr) |
| 106 | { |
| 107 | ss << " GWS[" << gws[0] << "," << gws[1] << "," << gws[2] << "]"; |
| 108 | } |
| 109 | if(lws != nullptr) |
| 110 | { |
| 111 | ss << " LWS[" << lws[0] << "," << lws[1] << "," << lws[2] << "]"; |
| 112 | } |
| 113 | info.name = ss.str(); |
| 114 | cl_event tmp; |
Georgios Pinitas | 5c2fb3f | 2018-05-01 15:26:20 +0100 | [diff] [blame^] | 115 | cl_int retval = this->_real_function(command_queue, kernel, work_dim, gwo, gws, lws, num_events_in_wait_list, event_wait_list, &tmp); |
Anthony Barbier | 48c19f1 | 2018-04-20 11:31:52 +0100 | [diff] [blame] | 116 | info.event = tmp; |
Georgios Pinitas | 5c2fb3f | 2018-05-01 15:26:20 +0100 | [diff] [blame^] | 117 | this->_kernels.push_back(std::move(info)); |
Anthony Barbier | 48c19f1 | 2018-04-20 11:31:52 +0100 | [diff] [blame] | 118 | return retval; |
| 119 | }; |
| 120 | |
Georgios Pinitas | 5c2fb3f | 2018-05-01 15:26:20 +0100 | [diff] [blame^] | 121 | // Start intercepting tasks: |
| 122 | auto task_interceptor = [this](graph::ExecutionTask & task) |
| 123 | { |
| 124 | if(task.node != nullptr && !task.node->name().empty()) |
| 125 | { |
| 126 | this->_prefix = task.node->name() + "/"; |
| 127 | } |
| 128 | else |
| 129 | { |
| 130 | this->_prefix = ""; |
| 131 | } |
| 132 | this->_real_graph_function(task); |
| 133 | this->_prefix = ""; |
| 134 | }; |
| 135 | |
Anthony Barbier | 48c19f1 | 2018-04-20 11:31:52 +0100 | [diff] [blame] | 136 | CLSymbols::get().clEnqueueNDRangeKernel_ptr = interceptor; |
Georgios Pinitas | 5c2fb3f | 2018-05-01 15:26:20 +0100 | [diff] [blame^] | 137 | graph::TaskExecutor::get().execute_function = task_interceptor; |
Anthony Barbier | 6e43349 | 2017-11-09 15:52:00 +0000 | [diff] [blame] | 138 | } |
| 139 | |
Georgios Pinitas | 5c2fb3f | 2018-05-01 15:26:20 +0100 | [diff] [blame^] | 140 | void OpenCLTimer::start() |
| 141 | { |
| 142 | _kernels.clear(); |
| 143 | } |
| 144 | |
| 145 | void OpenCLTimer::test_stop() |
Anthony Barbier | 6e43349 | 2017-11-09 15:52:00 +0000 | [diff] [blame] | 146 | { |
| 147 | // Restore real function |
Georgios Pinitas | 5c2fb3f | 2018-05-01 15:26:20 +0100 | [diff] [blame^] | 148 | CLSymbols::get().clEnqueueNDRangeKernel_ptr = _real_function; |
| 149 | graph::TaskExecutor::get().execute_function = _real_graph_function; |
| 150 | _real_graph_function = nullptr; |
| 151 | _real_function = nullptr; |
Anthony Barbier | 6e43349 | 2017-11-09 15:52:00 +0000 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | Instrument::MeasurementsMap OpenCLTimer::measurements() const |
| 155 | { |
| 156 | MeasurementsMap measurements; |
| 157 | unsigned int kernel_number = 0; |
Georgios Pinitas | 5c2fb3f | 2018-05-01 15:26:20 +0100 | [diff] [blame^] | 158 | for(auto kernel : _kernels) |
Anthony Barbier | 6e43349 | 2017-11-09 15:52:00 +0000 | [diff] [blame] | 159 | { |
Anthony Barbier | 5d9d019 | 2018-01-26 16:38:07 +0000 | [diff] [blame] | 160 | cl_ulong start = kernel.event.getProfilingInfo<CL_PROFILING_COMMAND_START>(); |
| 161 | cl_ulong end = kernel.event.getProfilingInfo<CL_PROFILING_COMMAND_END>(); |
Anthony Barbier | 6e43349 | 2017-11-09 15:52:00 +0000 | [diff] [blame] | 162 | |
Anthony Barbier | 5d9d019 | 2018-01-26 16:38:07 +0000 | [diff] [blame] | 163 | measurements.emplace(kernel.name + " #" + support::cpp11::to_string(kernel_number++), Measurement((end - start) / _scale_factor, _unit)); |
Anthony Barbier | 6e43349 | 2017-11-09 15:52:00 +0000 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | return measurements; |
| 167 | } |
| 168 | } // namespace framework |
| 169 | } // namespace test |
| 170 | } // namespace arm_compute |