Anthony Barbier | 6e43349 | 2017-11-09 15:52:00 +0000 | [diff] [blame^] | 1 | /* |
| 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 | |
| 35 | namespace arm_compute |
| 36 | { |
| 37 | namespace test |
| 38 | { |
| 39 | namespace framework |
| 40 | { |
| 41 | std::string OpenCLTimer::id() const |
| 42 | { |
| 43 | return "OpenCLTimer"; |
| 44 | } |
| 45 | |
| 46 | /* Function to be used to intercept kernel enqueues and store their OpenCL Event */ |
| 47 | class Interceptor |
| 48 | { |
| 49 | public: |
| 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 | |
| 89 | private: |
| 90 | OpenCLTimer &_timer; |
| 91 | }; |
| 92 | |
| 93 | OpenCLTimer::OpenCLTimer() |
| 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 | |
| 104 | void OpenCLTimer::start() |
| 105 | { |
| 106 | kernels.clear(); |
| 107 | // Start intercepting enqueues: |
| 108 | CLSymbols::get().clEnqueueNDRangeKernel_ptr = Interceptor(*this); |
| 109 | } |
| 110 | |
| 111 | void OpenCLTimer::stop() |
| 112 | { |
| 113 | // Restore real function |
| 114 | CLSymbols::get().clEnqueueNDRangeKernel_ptr = real_function; |
| 115 | } |
| 116 | |
| 117 | Instrument::MeasurementsMap OpenCLTimer::measurements() const |
| 118 | { |
| 119 | MeasurementsMap measurements; |
| 120 | unsigned int kernel_number = 0; |
| 121 | for(auto kernel : kernels) |
| 122 | { |
| 123 | //cl_int status = kernel.event.getInfo(<CL_EVENT_COMMAND_EXECUTION_STATUS>(); |
| 124 | cl_ulong queued = kernel.event.getProfilingInfo<CL_PROFILING_COMMAND_QUEUED>(); |
| 125 | cl_ulong submit = kernel.event.getProfilingInfo<CL_PROFILING_COMMAND_SUBMIT>(); |
| 126 | cl_ulong start = kernel.event.getProfilingInfo<CL_PROFILING_COMMAND_START>(); |
| 127 | cl_ulong end = kernel.event.getProfilingInfo<CL_PROFILING_COMMAND_END>(); |
| 128 | |
| 129 | std::list<std::string> raw_data = |
| 130 | { |
| 131 | "queued", support::cpp11::to_string(queued), |
| 132 | "submit", support::cpp11::to_string(submit), |
| 133 | "start", support::cpp11::to_string(start), |
| 134 | "end", support::cpp11::to_string(end), |
| 135 | }; |
| 136 | measurements.emplace(kernel.name + " #" + support::cpp11::to_string(kernel_number++), Measurement(end - start, "ns", raw_data)); |
| 137 | } |
| 138 | |
| 139 | return measurements; |
| 140 | } |
| 141 | } // namespace framework |
| 142 | } // namespace test |
| 143 | } // namespace arm_compute |