cf5b5bce2db31d3de66d4ebded0b209d8b400ecf
[platform/upstream/armcl.git] / src / runtime / CL / CLTuner.cpp
1 /*
2  * Copyright (c) 2017-2018 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 "arm_compute/runtime/CL/CLTuner.h"
25
26 #include "arm_compute/core/CL/ICLKernel.h"
27 #include "arm_compute/core/Error.h"
28 #include "arm_compute/runtime/CL/CLScheduler.h"
29
30 #include <limits>
31 #include <string>
32
33 using namespace arm_compute;
34
35 CLTuner::CLTuner()
36     : real_function(nullptr), _lws_table(), _queue(), _queue_profiler(), _kernel_event()
37 {
38 }
39
40 void CLTuner::set_cl_kernel_event(cl_event kernel_event)
41 {
42     _kernel_event = kernel_event;
43 }
44
45 void CLTuner::tune_kernel(ICLKernel &kernel)
46 {
47     if(real_function == nullptr)
48     {
49         real_function = CLSymbols::get().clEnqueueNDRangeKernel_ptr;
50
51         // Get the default queue
52         _queue = CLScheduler::get().queue();
53
54         // Check if we can use the OpenCL timer with the default queue
55         cl_command_queue_properties props = _queue.getInfo<CL_QUEUE_PROPERTIES>();
56
57         if((props & CL_QUEUE_PROFILING_ENABLE) == 0)
58         {
59             // Set the queue for profiling
60             _queue_profiler = cl::CommandQueue(CLScheduler::get().context(), props | CL_QUEUE_PROFILING_ENABLE);
61         }
62         else
63         {
64             _queue_profiler = _queue;
65         }
66     }
67
68     // Get the configuration ID from the kernel
69     const std::string &config_id = kernel.config_id();
70
71     // Check if we need to find the Optimal LWS. If config_id is equal to default_config_id, the kernel does not require to be tuned
72     if(config_id != arm_compute::default_config_id)
73     {
74         auto p = _lws_table.find(config_id);
75
76         if(p == _lws_table.end())
77         {
78             // Set profiler queue
79             CLScheduler::get().set_queue(_queue_profiler);
80
81             // Find the optimal LWS for the kernel
82             cl::NDRange opt_lws = find_optimal_lws(kernel);
83
84             // Insert the optimal LWS in the table
85             _lws_table.emplace(config_id, opt_lws);
86
87             // Set Local-Workgroup-Size
88             kernel.set_lws_hint(opt_lws);
89
90             // Restore queue
91             CLScheduler::get().set_queue(_queue);
92         }
93         else
94         {
95             // Set Local-Workgroup-Size
96             kernel.set_lws_hint(p->second);
97         }
98     }
99 }
100
101 cl::NDRange CLTuner::find_optimal_lws(ICLKernel &kernel)
102 {
103     // Start intercepting enqueues:
104     CLSymbols::get().clEnqueueNDRangeKernel_ptr = Interceptor(*this);
105
106     cl_ulong min_exec_time = std::numeric_limits<cl_ulong>::max();
107
108     cl::NDRange opt_lws = cl::NullRange;
109
110     const int x_step = std::max(1, kernel.window().x().step());
111     const int y_step = std::max(1, kernel.window().y().step());
112     const int z_step = std::max(1, kernel.window().z().step());
113     const int x_end  = kernel.window().x().end() - kernel.window().x().start() / x_step > 1 ? 16 : 1;
114     const int y_end  = kernel.window().y().end() - kernel.window().y().start() / y_step > 1 ? 16 : 1;
115     const int z_end  = kernel.window().z().end() - kernel.window().z().start() / z_step > 1 ? 8 : 1;
116
117     // First run using the default LWS
118     {
119         cl::NDRange lws_test = cl::NullRange;
120
121         kernel.set_lws_hint(lws_test);
122
123         // Run the kernel
124         kernel.run(kernel.window(), _queue_profiler);
125
126         CLScheduler::get().sync();
127
128         const cl_ulong start = _kernel_event.getProfilingInfo<CL_PROFILING_COMMAND_START>();
129         const cl_ulong end   = _kernel_event.getProfilingInfo<CL_PROFILING_COMMAND_END>();
130         const cl_ulong diff  = end - start;
131
132         min_exec_time = diff;
133     }
134
135     for(int z = 1; z <= z_end; ++z)
136     {
137         for(int y = 1; y <= y_end; ++y)
138         {
139             for(int x = 1; x <= x_end; ++x)
140             {
141                 cl::NDRange lws_test = cl::NDRange(x, y, z);
142
143                 const bool invalid_lws = (x * y * z > static_cast<int>(kernel.get_max_workgroup_size())) || (x == 1 && y == 1 && z == 1);
144
145                 if(invalid_lws)
146                 {
147                     continue;
148                 }
149
150                 //Set the Local-Workgroup-Size
151                 kernel.set_lws_hint(lws_test);
152
153                 // Run the kernel
154                 kernel.run(kernel.window(), _queue_profiler);
155
156                 CLScheduler::get().sync();
157
158                 const cl_ulong start = _kernel_event.getProfilingInfo<CL_PROFILING_COMMAND_START>();
159                 const cl_ulong end   = _kernel_event.getProfilingInfo<CL_PROFILING_COMMAND_END>();
160                 const cl_ulong diff  = end - start;
161
162                 // Check the execution time
163                 if(diff < min_exec_time)
164                 {
165                     min_exec_time = diff;
166                     opt_lws       = cl::NDRange(x, y, z);
167                 }
168             }
169         }
170     }
171
172     // Restore real function
173     CLSymbols::get().clEnqueueNDRangeKernel_ptr = real_function;
174
175     return opt_lws;
176 }
177
178 void CLTuner::import_lws_table(const std::unordered_map<std::string, cl::NDRange> &lws_table)
179 {
180     _lws_table.clear();
181     _lws_table = lws_table;
182 }
183
184 const std::unordered_map<std::string, cl::NDRange> &CLTuner::export_lws_table()
185 {
186     return _lws_table;
187 }
188
189 Interceptor::Interceptor(CLTuner &tuner)
190     : _tuner(tuner)
191 {
192 }
193
194 cl_int Interceptor::operator()(cl_command_queue command_queue, cl_kernel kernel, cl_uint work_dim, const size_t *gwo, const size_t *gws, const size_t *lws, cl_uint num_events_in_wait_list,
195                                const cl_event *event_wait_list, cl_event *event)
196 {
197     ARM_COMPUTE_ERROR_ON_MSG(event != nullptr, "Not supported");
198     ARM_COMPUTE_UNUSED(event);
199
200     cl_event tmp;
201     cl_int   retval = _tuner.real_function(command_queue, kernel, work_dim, gwo, gws, lws, num_events_in_wait_list, event_wait_list, &tmp);
202
203     // Set OpenCL event
204     _tuner.set_cl_kernel_event(tmp);
205
206     return retval;
207 }