Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017-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 | #pragma once |
| 25 | |
| 26 | #include <stdio.h> |
| 27 | |
| 28 | #include "arm_gemm.hpp" |
| 29 | |
| 30 | #include "mergeresults.hpp" |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 31 | #include "transform.hpp" |
| 32 | |
Michalis Spyrou | e7e96e0 | 2018-04-13 13:44:10 +0100 | [diff] [blame] | 33 | #ifdef CYCLE_PROFILING |
| 34 | #include "profiler.hpp" |
| 35 | #endif |
| 36 | |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 37 | namespace arm_gemm |
| 38 | { |
| 39 | // Implementation of the GemmCommon abstract class. |
| 40 | // |
| 41 | // This is implementation is for native GEMM with no transposition. |
| 42 | // |
| 43 | // By default the source data is used in-place, but if type conversion is |
| 44 | // needed we need to allocate working space (CURRENTLY NOT IMPLEMENTED). |
| 45 | |
| 46 | template <typename strategy, typename To, typename Tr> |
| 47 | class GemmNative : public GemmCommon<To, Tr> |
| 48 | { |
| 49 | typedef typename strategy::operand_type Toi; |
| 50 | typedef typename strategy::result_type Tri; |
| 51 | |
| 52 | const unsigned int _Msize; |
| 53 | const unsigned int _Nsize; |
| 54 | const unsigned int _Ksize; |
| 55 | |
Michalis Spyrou | e7e96e0 | 2018-04-13 13:44:10 +0100 | [diff] [blame] | 56 | const unsigned int _nbatches; |
| 57 | const unsigned int _nmultis; |
| 58 | |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 59 | Tr _beta; |
| 60 | |
| 61 | const CPUInfo *const _ci; |
| 62 | |
| 63 | unsigned int k_block = 0; |
| 64 | unsigned int n_block = 0; |
| 65 | |
| 66 | public: |
| 67 | GemmNative(GemmNative &) = delete; |
| 68 | GemmNative &operator=(GemmNative &) = delete; |
| 69 | |
Michalis Spyrou | e7e96e0 | 2018-04-13 13:44:10 +0100 | [diff] [blame] | 70 | GemmNative(const CPUInfo *ci, const unsigned int M, const unsigned int N, const unsigned int K, const unsigned int nbatches, const unsigned int nmultis, const Tr beta) |
| 71 | : _Msize(M), _Nsize(N), _Ksize(K), _nbatches(nbatches), _nmultis(nmultis), _beta(beta), _ci(ci) |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 72 | { |
| 73 | /* For now don't do any blocking. TODO: figure out if we should. */ |
| 74 | k_block = K; |
| 75 | n_block = N; |
| 76 | } |
| 77 | |
| 78 | // Window is number of out_height blocks |
| 79 | unsigned int get_window_size() const override |
| 80 | { |
Michalis Spyrou | e7e96e0 | 2018-04-13 13:44:10 +0100 | [diff] [blame] | 81 | return iceildiv(_Msize, strategy::out_height) * _nbatches * _nmultis; |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | // Actually execute the GEMM. |
| 85 | void execute(unsigned int start, unsigned int end, int) override |
| 86 | { |
Michalis Spyrou | e7e96e0 | 2018-04-13 13:44:10 +0100 | [diff] [blame] | 87 | #ifdef CYCLE_PROFILING |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 88 | profiler prof; |
Michalis Spyrou | e7e96e0 | 2018-04-13 13:44:10 +0100 | [diff] [blame] | 89 | #endif |
Pablo Tello | 6c42127 | 2018-05-03 10:42:35 +0100 | [diff] [blame] | 90 | strategy strat(_ci); |
Michalis Spyrou | e7e96e0 | 2018-04-13 13:44:10 +0100 | [diff] [blame] | 91 | const unsigned int window_per_batch = iceildiv(_Msize, strategy::out_height); |
| 92 | const unsigned int window_per_multi = window_per_batch * _nbatches; |
| 93 | |
| 94 | const unsigned int first_multi = start / window_per_multi; |
| 95 | const unsigned int last_multi = end / window_per_multi; |
| 96 | |
| 97 | const unsigned int first_batch = (start - (first_multi * window_per_multi)) / window_per_batch; |
| 98 | const unsigned int last_batch = (end - (last_multi * window_per_multi)) / window_per_batch; |
| 99 | |
| 100 | const unsigned int first_row = ((start - (first_multi * window_per_multi)) % window_per_batch) * strategy::out_height; |
| 101 | const unsigned int last_row = ((end - (last_multi * window_per_multi)) % window_per_batch) * strategy::out_height; |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 102 | |
| 103 | static_assert(std::is_same<To, Toi>::value, "gemm_native: Operand types must be the same."); |
| 104 | static_assert(std::is_same<Tr, Tri>::value, "gemm_native: Result types must be the same."); |
| 105 | |
Michalis Spyrou | e7e96e0 | 2018-04-13 13:44:10 +0100 | [diff] [blame] | 106 | for(unsigned int multi = first_multi; multi <= last_multi; multi++) |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 107 | { |
Michalis Spyrou | e7e96e0 | 2018-04-13 13:44:10 +0100 | [diff] [blame] | 108 | const unsigned int batch_0 = (multi == first_multi) ? first_batch : 0; |
Anthony Barbier | 71ac9c0 | 2018-05-09 18:23:57 +0100 | [diff] [blame^] | 109 | const unsigned int batch_max = (multi == last_multi) ? last_batch : _nbatches - 1; |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 110 | |
Pablo Tello | 6c42127 | 2018-05-03 10:42:35 +0100 | [diff] [blame] | 111 | for(unsigned int batch = batch_0; batch <= batch_max; batch++) |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 112 | { |
Michalis Spyrou | e7e96e0 | 2018-04-13 13:44:10 +0100 | [diff] [blame] | 113 | const unsigned int m_start = ((multi == first_multi) && (batch == first_batch)) ? first_row : 0; |
| 114 | const unsigned int m_end = ((multi == last_multi) && (batch == last_batch)) ? last_row : _Msize; |
| 115 | |
| 116 | for(unsigned int y0 = m_start; y0 < m_end; y0 += strategy::out_height) |
| 117 | { |
| 118 | const unsigned int ymax = std::min(y0 + strategy::out_height, m_end); |
| 119 | #ifdef CYCLE_PROFILING |
| 120 | auto p = prof.ScopedProfiler(PROFILE_KERNEL, (ymax - y0) * _Nsize * _Ksize); |
| 121 | #endif |
| 122 | |
| 123 | strat.kernel(this->_Aptr + (multi * this->_A_multi_stride) + (batch * this->_A_batch_stride) + (y0 * this->_lda), this->_lda, |
| 124 | this->_Bptr + (multi * this->_B_multi_stride), this->_ldb, |
| 125 | this->_Cptr + (multi * this->_C_multi_stride) + (batch * this->_C_batch_stride) + (y0 * this->_ldc), this->_ldc, |
| 126 | _beta, (ymax - y0), _Nsize, _Ksize); |
| 127 | } |
| 128 | } |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 129 | } |
| 130 | } |
| 131 | }; |
| 132 | |
| 133 | } // namespace arm_gemm |