David Mansell | e39334c | 2018-07-06 17:53:35 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 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 | |
| 25 | #include "gemv_batched.hpp" |
| 26 | |
| 27 | namespace arm_gemm { |
| 28 | |
| 29 | template<typename Top, typename Tret> |
| 30 | class GemmImplementation { |
| 31 | public: |
| 32 | /* Is this implementation compatible with the args as provided? */ |
| 33 | virtual bool is_supported(const GemmArgs<Tret> &args) { return true; } |
| 34 | /* Is this implementation "recommended" for these args (heuristic)? */ |
| 35 | virtual bool is_recommended(const GemmArgs<Tret> &args) { return true; } |
| 36 | /* Instantiate this method please. */ |
| 37 | virtual UniqueGemmCommon<Top, Tret> instantiate(const GemmArgs<Tret> &args) = 0; |
| 38 | |
| 39 | /* Indicate the "GemmMethod" for use as a selector */ |
| 40 | const GemmMethod method; |
| 41 | |
| 42 | virtual ~GemmImplementation() { } |
| 43 | |
| 44 | GemmImplementation(GemmMethod method) : method(method) { } |
| 45 | }; |
| 46 | |
| 47 | /* "gemv_batched" implementation is type-agnostic, so template it here. */ |
| 48 | template<typename Top, typename Tret> |
| 49 | class GemmImpl_gemv_batched : public GemmImplementation<Top, Tret> { |
| 50 | public: |
| 51 | bool is_supported(const GemmArgs<Tret> &args) override { |
| 52 | return (args._Msize==1 && args._nbatches > 1); |
| 53 | } |
| 54 | |
| 55 | UniqueGemmCommon<Top, Tret> instantiate(const GemmArgs<Tret> &args) override { |
| 56 | return UniqueGemmCommon<Top, Tret> (new GemvBatched<Top, Tret>(args)); |
| 57 | } |
| 58 | |
| 59 | GemmImpl_gemv_batched() : GemmImplementation<Top, Tret>(GemmMethod::GEMV_BATCHED) { } |
| 60 | }; |
| 61 | |
| 62 | /* "Master" function implemented for each valid combination of types. |
| 63 | * Returns a list of GEMM implementation descriptors for processing by the |
| 64 | * other functions. */ |
| 65 | template<typename Top, typename Tret> |
| 66 | std::vector<GemmImplementation<Top, Tret> *> &gemm_implementation_list(); |
| 67 | |
| 68 | template<typename Top, typename Tret> |
| 69 | GemmImplementation<Top, Tret> *find_implementation(GemmArgs<Tret> &args, GemmConfig *cfg) { |
| 70 | auto gemms = gemm_implementation_list<Top, Tret>(); |
| 71 | |
| 72 | for(auto &&i : gemms) { |
| 73 | /* Skip if this implementation doesn't support these args. */ |
| 74 | if (!i->is_supported(args)) { |
| 75 | continue; |
| 76 | } |
| 77 | |
| 78 | /* Skip if a specific method is requested and this is a different one. */ |
| 79 | if (cfg && cfg->method != GemmMethod::DEFAULT && i->method != cfg->method) { |
| 80 | continue; |
| 81 | } |
| 82 | |
| 83 | /* If no specific method is requested, check that this method recommends itself. */ |
| 84 | if ((!cfg || cfg->method == GemmMethod::DEFAULT) && !i->is_recommended(args)) { |
| 85 | continue; |
| 86 | } |
| 87 | |
| 88 | return i; |
| 89 | } |
| 90 | |
| 91 | return nullptr; |
| 92 | } |
| 93 | |
| 94 | template<typename Top, typename Tret> |
| 95 | UniqueGemmCommon<Top, Tret> gemm(GemmArgs<Tret> &args, GemmConfig *cfg) { |
| 96 | auto impl = find_implementation<Top, Tret>(args, cfg); |
| 97 | |
| 98 | if (impl) { |
| 99 | return impl->instantiate(args); |
| 100 | } |
| 101 | |
| 102 | return UniqueGemmCommon<Top, Tret>(nullptr); |
| 103 | } |
| 104 | |
| 105 | template<typename Top, typename Tret> |
| 106 | GemmMethod get_gemm_method(GemmArgs<Tret> &args) { |
| 107 | auto impl = find_implementation<Top, Tret>(args, nullptr); |
| 108 | |
| 109 | if (impl) { |
| 110 | return impl->method; |
| 111 | } |
| 112 | |
| 113 | /* This shouldn't happen - there should always be at least one valid implementation. */ |
| 114 | return GemmMethod::DEFAULT; |
| 115 | } |
| 116 | |
| 117 | template<typename Top, typename Tret> |
| 118 | bool method_is_compatible(GemmMethod method, GemmArgs<Tret> &args) { |
| 119 | /* Determine if the method is valid by attempting to obtain an implementation specifying this method. */ |
| 120 | GemmConfig cfg(method); |
| 121 | |
| 122 | auto impl = find_implementation<Top, Tret>(args, &cfg); |
| 123 | |
| 124 | if (impl) { |
| 125 | return true; |
| 126 | } |
| 127 | |
| 128 | return false; |
| 129 | } |
| 130 | |
| 131 | } // namespace arm_gemm |