Gian Marco | de691f0 | 2017-09-08 16:13:11 +0100 | [diff] [blame] | 1 | /* |
Gian Marco | 85e6f51 | 2018-02-01 16:57:48 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2018 ARM Limited. |
Gian Marco | de691f0 | 2017-09-08 16:13:11 +0100 | [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 | #ifndef __ARM_COMPUTE_CLTUNER_H__ |
| 25 | #define __ARM_COMPUTE_CLTUNER_H__ |
| 26 | |
| 27 | #include "arm_compute/core/CL/OpenCL.h" |
| 28 | #include "arm_compute/runtime/CL/ICLTuner.h" |
| 29 | |
| 30 | #include <unordered_map> |
| 31 | |
| 32 | namespace arm_compute |
| 33 | { |
| 34 | class ICLKernel; |
| 35 | |
| 36 | /** Basic implementation of the OpenCL tuner interface */ |
| 37 | class CLTuner : public ICLTuner |
| 38 | { |
| 39 | public: |
Anthony Barbier | 8db8318 | 2018-02-27 13:08:00 +0000 | [diff] [blame] | 40 | /** Constructor |
| 41 | * |
| 42 | * @param[in] tune_new_kernels Find the optimal local workgroup size for kernels which are not present in the table ? |
| 43 | * |
| 44 | */ |
| 45 | CLTuner(bool tune_new_kernels = true); |
Gian Marco | de691f0 | 2017-09-08 16:13:11 +0100 | [diff] [blame] | 46 | |
| 47 | /** Destructor */ |
| 48 | ~CLTuner() = default; |
| 49 | |
Anthony Barbier | 8b81195 | 2018-02-28 13:47:58 +0000 | [diff] [blame] | 50 | /** Setter for tune_new_kernels option |
Anthony Barbier | 8db8318 | 2018-02-27 13:08:00 +0000 | [diff] [blame] | 51 | * |
| 52 | * @param[in] tune_new_kernels Find the optimal local workgroup size for kernels which are not present in the table ? |
| 53 | */ |
| 54 | void set_tune_new_kernels(bool tune_new_kernels); |
Anthony Barbier | 8b81195 | 2018-02-28 13:47:58 +0000 | [diff] [blame] | 55 | /** Tune kernels that are not in the LWS table |
| 56 | * |
| 57 | * @return True if tuning of new kernels is enabled. |
| 58 | */ |
| 59 | bool tune_new_kernels() const; |
Anthony Barbier | 8db8318 | 2018-02-27 13:08:00 +0000 | [diff] [blame] | 60 | /** Manually add a LWS for a kernel |
| 61 | * |
| 62 | * @param[in] kernel_id Unique identifiant of the kernel |
| 63 | * @param[in] optimal_lws Optimal local workgroup size to use for the given kernel |
| 64 | */ |
| 65 | void add_lws_to_table(const std::string &kernel_id, cl::NDRange optimal_lws); |
Gian Marco | de691f0 | 2017-09-08 16:13:11 +0100 | [diff] [blame] | 66 | /** Import LWS table |
| 67 | * |
| 68 | * @param[in] lws_table The unordered_map container to import |
| 69 | */ |
| 70 | void import_lws_table(const std::unordered_map<std::string, cl::NDRange> &lws_table); |
| 71 | |
Anthony Barbier | 8b81195 | 2018-02-28 13:47:58 +0000 | [diff] [blame] | 72 | /** Give read access to the LWS table |
Gian Marco | de691f0 | 2017-09-08 16:13:11 +0100 | [diff] [blame] | 73 | * |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 74 | * @return The lws table as unordered_map container |
Gian Marco | de691f0 | 2017-09-08 16:13:11 +0100 | [diff] [blame] | 75 | */ |
Anthony Barbier | 8b81195 | 2018-02-28 13:47:58 +0000 | [diff] [blame] | 76 | const std::unordered_map<std::string, cl::NDRange> &lws_table() const; |
Gian Marco | de691f0 | 2017-09-08 16:13:11 +0100 | [diff] [blame] | 77 | |
Gian Marco | 85e6f51 | 2018-02-01 16:57:48 +0000 | [diff] [blame] | 78 | /** Set the OpenCL kernel event |
| 79 | * |
| 80 | * @note The interceptor can use this function to store the event associated to the OpenCL kernel |
| 81 | * |
| 82 | * @param[in] kernel_event The OpenCL kernel event |
| 83 | */ |
| 84 | void set_cl_kernel_event(cl_event kernel_event); |
| 85 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 86 | /** clEnqueueNDRangeKernel symbol */ |
Anthony Barbier | 8db8318 | 2018-02-27 13:08:00 +0000 | [diff] [blame] | 87 | std::function<decltype(clEnqueueNDRangeKernel)> real_clEnqueueNDRangeKernel; |
| 88 | |
Anthony Barbier | 8b81195 | 2018-02-28 13:47:58 +0000 | [diff] [blame] | 89 | /** Load the LWS table from file |
| 90 | * |
| 91 | * @param[in] filename Load the LWS table from this file.(Must exist) |
| 92 | */ |
| 93 | void load_from_file(const std::string &filename); |
| 94 | |
| 95 | /** Save the content of the LWS table to file |
| 96 | * |
| 97 | * @param[in] filename Save the LWS table to this file. (Content will be overwritten) |
| 98 | */ |
| 99 | void save_to_file(const std::string &filename) const; |
| 100 | |
Anthony Barbier | 8db8318 | 2018-02-27 13:08:00 +0000 | [diff] [blame] | 101 | // Inherited methods overridden: |
Georgios Pinitas | c0d1c86 | 2018-03-23 15:13:15 +0000 | [diff] [blame] | 102 | void tune_kernel_static(ICLKernel &kernel) override; |
| 103 | void tune_kernel_dynamic(ICLKernel &kernel) override; |
Gian Marco | 85e6f51 | 2018-02-01 16:57:48 +0000 | [diff] [blame] | 104 | |
Anthony Barbier | f5dcf79 | 2018-02-28 18:04:45 +0000 | [diff] [blame] | 105 | /** Is the kernel_event set ? |
| 106 | * |
| 107 | * @return true if the kernel_event is set. |
| 108 | */ |
| 109 | bool kernel_event_is_set() const; |
| 110 | |
Gian Marco | de691f0 | 2017-09-08 16:13:11 +0100 | [diff] [blame] | 111 | private: |
| 112 | /** Find optimal LWS using brute-force approach |
| 113 | * |
| 114 | * @param[in] kernel OpenCL kernel to be tuned with LWS |
| 115 | * |
| 116 | * @return The optimal LWS to use |
| 117 | */ |
| 118 | cl::NDRange find_optimal_lws(ICLKernel &kernel); |
| 119 | |
| 120 | std::unordered_map<std::string, cl::NDRange> _lws_table; |
Gian Marco | 85e6f51 | 2018-02-01 16:57:48 +0000 | [diff] [blame] | 121 | cl::CommandQueue _queue; |
| 122 | cl::CommandQueue _queue_profiler; |
| 123 | cl::Event _kernel_event; |
Anthony Barbier | 8db8318 | 2018-02-27 13:08:00 +0000 | [diff] [blame] | 124 | bool _tune_new_kernels; |
Gian Marco | 85e6f51 | 2018-02-01 16:57:48 +0000 | [diff] [blame] | 125 | }; |
Gian Marco | de691f0 | 2017-09-08 16:13:11 +0100 | [diff] [blame] | 126 | } |
| 127 | #endif /*__ARM_COMPUTE_CLTUNER_H__ */ |