Kaizen | 8938bd3 | 2017-09-28 14:38:23 +0100 | [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 "arm_compute/runtime/CL/CLTuner.h" |
| 25 | |
| 26 | #include "arm_compute/core/CL/ICLKernel.h" |
| 27 | #include "arm_compute/runtime/CL/CLScheduler.h" |
| 28 | |
| 29 | #include <chrono> |
| 30 | #include <limits> |
| 31 | #include <string> |
| 32 | |
| 33 | using namespace arm_compute; |
| 34 | |
| 35 | CLTuner::CLTuner() |
| 36 | : _lws_table() |
| 37 | { |
| 38 | } |
| 39 | |
| 40 | void CLTuner::tune_kernel(ICLKernel &kernel) |
| 41 | { |
| 42 | // Get the configuration ID from the kernel |
| 43 | const std::string &config_id = kernel.config_id(); |
| 44 | |
| 45 | // 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 |
| 46 | if(config_id != arm_compute::default_config_id) |
| 47 | { |
| 48 | auto p = _lws_table.find(config_id); |
| 49 | |
| 50 | if(p == _lws_table.end()) |
| 51 | { |
| 52 | // Find the optimal LWS for the kernel |
| 53 | cl::NDRange opt_lws = find_optimal_lws(kernel); |
| 54 | |
| 55 | // Insert the optimal LWS in the table |
| 56 | _lws_table.emplace(config_id, opt_lws); |
| 57 | |
| 58 | // Set Local-Workgroup-Size |
| 59 | kernel.set_lws_hint(opt_lws); |
| 60 | } |
| 61 | else |
| 62 | { |
| 63 | // Set Local-Workgroup-Size |
| 64 | kernel.set_lws_hint(p->second); |
| 65 | } |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | cl::NDRange CLTuner::find_optimal_lws(ICLKernel &kernel) |
| 70 | { |
| 71 | cl::CommandQueue q = CLScheduler::get().queue(); |
| 72 | |
| 73 | double min_exec_time = std::numeric_limits<double>::max(); |
| 74 | |
| 75 | cl::NDRange opt_lws = cl::NDRange(1, 1); |
| 76 | |
| 77 | for(int y = 1; y <= 16; ++y) |
| 78 | { |
| 79 | for(int x = 1; x <= 16; ++x) |
| 80 | { |
| 81 | cl::NDRange lws_test = cl::NDRange(x, y); |
| 82 | |
| 83 | //Set the Local-Workgroup-Size |
| 84 | kernel.set_lws_hint(lws_test); |
| 85 | |
| 86 | auto t_start = std::chrono::high_resolution_clock::now(); |
| 87 | |
| 88 | // Run |
| 89 | kernel.run(kernel.window(), q); |
| 90 | |
| 91 | CLScheduler::get().sync(); |
| 92 | |
| 93 | auto t_stop = std::chrono::high_resolution_clock::now(); |
| 94 | |
| 95 | std::chrono::duration<double, std::nano> fp_nano = t_stop - t_start; |
| 96 | |
| 97 | // Check the execution time |
| 98 | if(fp_nano.count() < min_exec_time) |
| 99 | { |
| 100 | min_exec_time = fp_nano.count(); |
| 101 | opt_lws = cl::NDRange(x, y); |
| 102 | } |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | return opt_lws; |
| 107 | } |
| 108 | |
| 109 | void CLTuner::import_lws_table(const std::unordered_map<std::string, cl::NDRange> &lws_table) |
| 110 | { |
| 111 | _lws_table.clear(); |
| 112 | _lws_table = lws_table; |
| 113 | } |
| 114 | |
| 115 | const std::unordered_map<std::string, cl::NDRange> &CLTuner::export_lws_table() |
| 116 | { |
| 117 | return _lws_table; |
| 118 | } |