Vidhya Sudhan Loganathan | 050471e | 2019-04-25 09:27:24 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2019 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 | #ifndef __ARM_COMPUTE_CL_LWS_LIST_H__ |
| 25 | #define __ARM_COMPUTE_CL_LWS_LIST_H__ |
| 26 | |
| 27 | #include "arm_compute/core/CL/OpenCL.h" |
| 28 | #include "arm_compute/core/Error.h" |
| 29 | #include "arm_compute/core/Helpers.h" |
| 30 | #include "arm_compute/runtime/CL/CLTunerTypes.h" |
| 31 | #include "support/ToolchainSupport.h" |
| 32 | #include <memory> |
| 33 | |
| 34 | namespace arm_compute |
| 35 | { |
| 36 | namespace cl_tuner |
| 37 | { |
| 38 | constexpr unsigned int max_lws_supported_x{ 64u }; |
| 39 | constexpr unsigned int max_lws_supported_y{ 32u }; |
| 40 | constexpr unsigned int max_lws_supported_z{ 32u }; |
| 41 | |
| 42 | /** Interface for LWS lists */ |
| 43 | class ICLLWSList |
| 44 | { |
| 45 | public: |
| 46 | /** Constructor */ |
| 47 | ICLLWSList() = default; |
| 48 | /** Copy Constructor */ |
| 49 | ICLLWSList(const ICLLWSList &) = default; |
| 50 | /** Move Constructor */ |
| 51 | ICLLWSList(ICLLWSList &&) noexcept(true) = default; |
| 52 | /** Assignment */ |
| 53 | ICLLWSList &operator=(const ICLLWSList &) = default; |
| 54 | /** Move Assignment */ |
| 55 | ICLLWSList &operator=(ICLLWSList &&) noexcept(true) = default; |
| 56 | /** Destructor */ |
| 57 | virtual ~ICLLWSList() = default; |
| 58 | |
| 59 | /** Return the LWS value at the given index. |
| 60 | * |
| 61 | * @return LWS value at the given index |
| 62 | */ |
| 63 | virtual cl::NDRange operator[](size_t) = 0; |
| 64 | |
| 65 | /** LWS list size. |
| 66 | * |
| 67 | * @return LWS list size |
| 68 | */ |
| 69 | virtual size_t size() = 0; |
| 70 | }; |
| 71 | |
| 72 | /** Non instantiable base class for LWS combinations that use Index2Cooard mapping */ |
| 73 | class CLLWSList : public ICLLWSList |
| 74 | { |
| 75 | protected: |
| 76 | /* Shape of 3-D search space */ |
| 77 | TensorShape search_space_shape{ 0, 0, 0 }; |
| 78 | |
| 79 | /** Constructor */ |
| 80 | CLLWSList() = default; |
| 81 | /** Copy Constructor */ |
| 82 | CLLWSList(const CLLWSList &) = default; |
| 83 | /** Move Constructor */ |
| 84 | CLLWSList(CLLWSList &&) noexcept(true) = default; |
| 85 | /** Assignment */ |
| 86 | CLLWSList &operator=(const CLLWSList &) = default; |
| 87 | /** Move Assignment */ |
| 88 | CLLWSList &operator=(CLLWSList &&) noexcept(true) = default; |
| 89 | /** Destructor */ |
| 90 | virtual ~CLLWSList() = default; |
| 91 | |
| 92 | // Inherited methods overridden: |
| 93 | virtual size_t size() override; |
| 94 | }; |
| 95 | |
| 96 | /** Exhaustive list of all possible LWS values */ |
| 97 | class CLLWSListExhaustive : public CLLWSList |
| 98 | { |
| 99 | public: |
| 100 | /** Prevent default constructor calls */ |
| 101 | CLLWSListExhaustive() = delete; |
| 102 | /** Constructor */ |
| 103 | CLLWSListExhaustive(const cl::NDRange &gws); |
| 104 | /** Copy Constructor */ |
| 105 | CLLWSListExhaustive(const CLLWSListExhaustive &) = default; |
| 106 | /** Move Constructor */ |
| 107 | CLLWSListExhaustive(CLLWSListExhaustive &&) noexcept(true) = default; |
| 108 | /** Assignment */ |
| 109 | CLLWSListExhaustive &operator=(const CLLWSListExhaustive &) = default; |
| 110 | /** Move Assignment */ |
| 111 | CLLWSListExhaustive &operator=(CLLWSListExhaustive &&) noexcept(true) = default; |
| 112 | /** Destructor */ |
| 113 | ~CLLWSListExhaustive() = default; |
| 114 | |
| 115 | // Inherited methods overridden: |
| 116 | cl::NDRange operator[](size_t) override; |
| 117 | }; |
| 118 | |
| 119 | /** A subset of LWS values that are either factors of gws when gws[2] < 16 or power of 2 */ |
| 120 | class CLLWSListNormal : public CLLWSList |
| 121 | { |
| 122 | public: |
| 123 | /** Constructor */ |
| 124 | CLLWSListNormal(const cl::NDRange &gws); |
| 125 | /** Copy Constructor */ |
| 126 | CLLWSListNormal(const CLLWSListNormal &) = default; |
| 127 | /** Move Constructor */ |
| 128 | CLLWSListNormal(CLLWSListNormal &&) noexcept(true) = default; |
| 129 | /** Assignment */ |
| 130 | CLLWSListNormal &operator=(const CLLWSListNormal &) = default; |
| 131 | /** Move Assignment */ |
| 132 | CLLWSListNormal &operator=(CLLWSListNormal &&) noexcept(true) = default; |
| 133 | /** Destructor */ |
| 134 | ~CLLWSListNormal() = default; |
| 135 | |
| 136 | // Inherited methods overridden: |
| 137 | cl::NDRange operator[](size_t) override; |
| 138 | |
| 139 | protected: |
| 140 | std::vector<unsigned int> _lws_x{}; |
| 141 | std::vector<unsigned int> _lws_y{}; |
| 142 | std::vector<unsigned int> _lws_z{}; |
| 143 | |
| 144 | /** Prevent default constructor calls */ |
| 145 | CLLWSListNormal() = default; |
| 146 | |
| 147 | private: |
| 148 | /** Utility function used to initialize the LWS values to test. |
| 149 | * Only the LWS values which are power of 2 or satisfy the modulo conditions with GWS are taken into account by the CLTuner |
| 150 | * |
| 151 | * @param[in, out] lws Vector of LWS to test |
| 152 | * @param[in] gws Size of the specific GWS |
| 153 | * @param[in] lws_max Max LWS value allowed to be tested |
| 154 | * @param[in] mod_let_one True if the results of the modulo operation between gws and the lws can be less than one. |
| 155 | */ |
| 156 | void initialize_lws_values(std::vector<unsigned int> &lws, unsigned int gws, unsigned int lws_max, bool mod_let_one); |
| 157 | }; |
| 158 | |
| 159 | /** A minimal subset of LWS values that only have 1,2 and 4/8 */ |
| 160 | class CLLWSListRapid : public CLLWSListNormal |
| 161 | { |
| 162 | public: |
| 163 | /** Prevent default constructor calls */ |
| 164 | CLLWSListRapid() = delete; |
| 165 | /** Constructor */ |
| 166 | CLLWSListRapid(const cl::NDRange &gws); |
| 167 | /** Copy Constructor */ |
| 168 | CLLWSListRapid(const CLLWSListRapid &) = default; |
| 169 | /** Move Constructor */ |
| 170 | CLLWSListRapid(CLLWSListRapid &&) noexcept(true) = default; |
| 171 | /** Assignment */ |
| 172 | CLLWSListRapid &operator=(const CLLWSListRapid &) = default; |
| 173 | /** Move Assignment */ |
| 174 | CLLWSListRapid &operator=(CLLWSListRapid &&) noexcept(true) = default; |
| 175 | /** Destructor */ |
| 176 | virtual ~CLLWSListRapid() = default; |
| 177 | |
| 178 | private: |
| 179 | /** Utility function used to initialize the LWS values to test. |
| 180 | * Only the LWS values that have 1,2 and 4/8 for each dimension are taken into account by the CLTuner |
| 181 | * |
| 182 | * @param[in, out] lws Vector of LWS to test |
| 183 | * @param[in] lws_max Max LWS value allowed to be tested |
| 184 | */ |
| 185 | void initialize_lws_values(std::vector<unsigned int> &lws, unsigned int lws_max); |
| 186 | }; |
| 187 | |
| 188 | /** Factory to construct an ICLLWSList object based on the CL tuner mode */ |
| 189 | class CLLWSListFactory final |
| 190 | { |
| 191 | public: |
| 192 | /** Construct an ICLLWSList object for the given tuner mode and gws configuration. |
| 193 | * |
| 194 | * @return unique_ptr to the requested ICLLWSList implementation. |
| 195 | */ |
| 196 | static std::unique_ptr<ICLLWSList> get_lws_list(CLTunerMode mode, const cl::NDRange &gws) |
| 197 | { |
| 198 | switch(mode) |
| 199 | { |
| 200 | case CLTunerMode::EXHAUSTIVE: |
| 201 | return arm_compute::support::cpp14::make_unique<CLLWSListExhaustive>(gws); |
| 202 | case CLTunerMode::NORMAL: |
| 203 | return arm_compute::support::cpp14::make_unique<CLLWSListNormal>(gws); |
| 204 | case CLTunerMode::RAPID: |
| 205 | return arm_compute::support::cpp14::make_unique<CLLWSListRapid>(gws); |
| 206 | default: |
| 207 | return nullptr; |
| 208 | } |
| 209 | } |
| 210 | }; |
| 211 | } // namespace cl_tuner |
| 212 | } // namespace arm_compute |
| 213 | #endif /*__ARM_COMPUTE_CL_LWS_LIST_H__ */ |