Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 1 | /* |
Georgios Pinitas | 1d48065 | 2019-01-23 11:24:50 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2019 Arm Limited. |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [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 | #pragma once |
| 25 | |
| 26 | #include <stdio.h> |
| 27 | |
| 28 | #include "arm_gemm.hpp" |
| 29 | |
Georgios Pinitas | 1d48065 | 2019-01-23 11:24:50 +0000 | [diff] [blame] | 30 | #include "ndrange.hpp" |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 31 | |
Michalis Spyrou | e7e96e0 | 2018-04-13 13:44:10 +0100 | [diff] [blame] | 32 | #ifdef CYCLE_PROFILING |
| 33 | #include "profiler.hpp" |
| 34 | #endif |
| 35 | |
Anthony Barbier | 5f70773 | 2018-07-03 16:22:02 +0100 | [diff] [blame] | 36 | namespace arm_gemm { |
| 37 | |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 38 | // Implementation of the GemmCommon abstract class. |
| 39 | // |
| 40 | // This is implementation is for native GEMM with no transposition. |
| 41 | // |
| 42 | // By default the source data is used in-place, but if type conversion is |
| 43 | // needed we need to allocate working space (CURRENTLY NOT IMPLEMENTED). |
| 44 | |
Anthony Barbier | 5f70773 | 2018-07-03 16:22:02 +0100 | [diff] [blame] | 45 | template<typename strategy, typename To, typename Tr> |
| 46 | class GemmNative : public GemmCommon<To, Tr> { |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 47 | typedef typename strategy::operand_type Toi; |
Anthony Barbier | 5f70773 | 2018-07-03 16:22:02 +0100 | [diff] [blame] | 48 | typedef typename strategy::result_type Tri; |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 49 | |
| 50 | const unsigned int _Msize; |
| 51 | const unsigned int _Nsize; |
| 52 | const unsigned int _Ksize; |
| 53 | |
Michalis Spyrou | e7e96e0 | 2018-04-13 13:44:10 +0100 | [diff] [blame] | 54 | const unsigned int _nbatches; |
| 55 | const unsigned int _nmultis; |
| 56 | |
Georgios Pinitas | 1d48065 | 2019-01-23 11:24:50 +0000 | [diff] [blame] | 57 | const Tr _beta; |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 58 | |
Anthony Barbier | 5f70773 | 2018-07-03 16:22:02 +0100 | [diff] [blame] | 59 | const CPUInfo * const _ci; |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 60 | |
Georgios Pinitas | 1d48065 | 2019-01-23 11:24:50 +0000 | [diff] [blame] | 61 | const unsigned int _k_block; |
| 62 | const unsigned int _n_block; |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 63 | |
Georgios Pinitas | 1d48065 | 2019-01-23 11:24:50 +0000 | [diff] [blame] | 64 | const NDRange<4> _window_range; |
| 65 | |
| 66 | static unsigned int compute_k_block(const GemmArgs<Tr> &args) { |
| 67 | return args._Ksize; |
David Mansell | e39334c | 2018-07-06 17:53:35 +0100 | [diff] [blame] | 68 | } |
| 69 | |
Georgios Pinitas | 1d48065 | 2019-01-23 11:24:50 +0000 | [diff] [blame] | 70 | static unsigned int compute_n_block(const GemmArgs<Tr> &args) { |
| 71 | if ((args._cfg != nullptr) && args._cfg->outer_block_size > 0) { |
| 72 | return args._cfg->outer_block_size; |
| 73 | } else { |
| 74 | return args._Nsize; |
| 75 | } |
David Mansell | e39334c | 2018-07-06 17:53:35 +0100 | [diff] [blame] | 76 | } |
| 77 | |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 78 | public: |
| 79 | GemmNative(GemmNative &) = delete; |
Anthony Barbier | 5f70773 | 2018-07-03 16:22:02 +0100 | [diff] [blame] | 80 | GemmNative & operator= (GemmNative &) = delete; |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 81 | |
Georgios Pinitas | 7cd26d4 | 2019-01-09 18:35:17 +0000 | [diff] [blame] | 82 | GemmNative(const GemmArgs<Tr> &args) |
Georgios Pinitas | cfa2bba | 2019-06-27 17:00:52 +0100 | [diff] [blame] | 83 | : _Msize(args._Msize), _Nsize(args._Nsize), _Ksize(args._Ksize), |
| 84 | _nbatches(args._nbatches), _nmultis(args._nmulti), |
| 85 | _beta(args._beta), _ci(args._ci), |
| 86 | _k_block(compute_k_block(args)), _n_block(compute_n_block(args)), |
| 87 | _window_range(iceildiv(_Msize, strategy::out_height()), _nbatches, iceildiv(_Nsize, _n_block), _nmultis) { } |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 88 | |
David Mansell | e39334c | 2018-07-06 17:53:35 +0100 | [diff] [blame] | 89 | // Window is amount per multi multiplied by total number of multis. |
Anthony Barbier | 5f70773 | 2018-07-03 16:22:02 +0100 | [diff] [blame] | 90 | unsigned int get_window_size() const override { |
Georgios Pinitas | 1d48065 | 2019-01-23 11:24:50 +0000 | [diff] [blame] | 91 | return _window_range.total_size(); |
| 92 | } |
| 93 | |
| 94 | // Native GEMMs can always be dynamically scheduled (whether requested or not) |
| 95 | bool supports_dynamic_scheduling() const override { |
| 96 | return true; |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | // Actually execute the GEMM. |
Anthony Barbier | 5f70773 | 2018-07-03 16:22:02 +0100 | [diff] [blame] | 100 | void execute(unsigned int start, unsigned int end, int) override { |
Michalis Spyrou | e7e96e0 | 2018-04-13 13:44:10 +0100 | [diff] [blame] | 101 | #ifdef CYCLE_PROFILING |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 102 | profiler prof; |
Michalis Spyrou | e7e96e0 | 2018-04-13 13:44:10 +0100 | [diff] [blame] | 103 | #endif |
Anthony Barbier | 5f70773 | 2018-07-03 16:22:02 +0100 | [diff] [blame] | 104 | strategy strat(_ci); |
| 105 | |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 106 | static_assert(std::is_same<To, Toi>::value, "gemm_native: Operand types must be the same."); |
| 107 | static_assert(std::is_same<Tr, Tri>::value, "gemm_native: Result types must be the same."); |
| 108 | |
Georgios Pinitas | 1d48065 | 2019-01-23 11:24:50 +0000 | [diff] [blame] | 109 | auto p = _window_range.iterator(start, end); |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 110 | |
Georgios Pinitas | 1d48065 | 2019-01-23 11:24:50 +0000 | [diff] [blame] | 111 | if (p.done()) { |
| 112 | return; |
| 113 | } |
Michalis Spyrou | e7e96e0 | 2018-04-13 13:44:10 +0100 | [diff] [blame] | 114 | |
Georgios Pinitas | 1d48065 | 2019-01-23 11:24:50 +0000 | [diff] [blame] | 115 | do { |
| 116 | unsigned int y0 = p.dim(0) * strategy::out_height(); |
| 117 | unsigned int ymax = std::min(p.dim0_max() * strategy::out_height(), _Msize); |
| 118 | unsigned int batch = p.dim(1); |
| 119 | unsigned int n0 = p.dim(2) * _n_block; |
| 120 | unsigned int nmax = std::min(n0 + _n_block, _Nsize); |
| 121 | unsigned int multi = p.dim(3); |
Georgios Pinitas | 7cd26d4 | 2019-01-09 18:35:17 +0000 | [diff] [blame] | 122 | |
Michalis Spyrou | e7e96e0 | 2018-04-13 13:44:10 +0100 | [diff] [blame] | 123 | #ifdef CYCLE_PROFILING |
Georgios Pinitas | 1d48065 | 2019-01-23 11:24:50 +0000 | [diff] [blame] | 124 | auto p = prof.ScopedProfiler(PROFILE_KERNEL, (ymax-y0) * (nmax - n0) * _Ksize); |
Michalis Spyrou | e7e96e0 | 2018-04-13 13:44:10 +0100 | [diff] [blame] | 125 | #endif |
| 126 | |
David Mansell | e39334c | 2018-07-06 17:53:35 +0100 | [diff] [blame] | 127 | strat.kernel(this->_Aptr + (multi * this->_A_multi_stride) + (batch * this->_A_batch_stride) + (y0 * this->_lda), this->_lda, |
Georgios Pinitas | 1d48065 | 2019-01-23 11:24:50 +0000 | [diff] [blame] | 128 | this->_Bptr + (multi * this->_B_multi_stride) + n0, this->_ldb, |
| 129 | this->_Cptr + (multi * this->_C_multi_stride) + (batch * this->_C_batch_stride) + (y0 * this->_ldc) + n0, this->_ldc, |
| 130 | _beta, (ymax-y0), (nmax - n0), _Ksize); |
| 131 | } while (p.next_dim1()); |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 132 | } |
| 133 | }; |
| 134 | |
Georgios Pinitas | cfa2bba | 2019-06-27 17:00:52 +0100 | [diff] [blame] | 135 | } // namespace arm_gemm |