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 | |
Anthony Barbier | 5f70773 | 2018-07-03 16:22:02 +0100 | [diff] [blame] | 37 | namespace arm_gemm { |
| 38 | |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 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 | |
Anthony Barbier | 5f70773 | 2018-07-03 16:22:02 +0100 | [diff] [blame] | 46 | template<typename strategy, typename To, typename Tr> |
| 47 | class GemmNative : public GemmCommon<To, Tr> { |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 48 | typedef typename strategy::operand_type Toi; |
Anthony Barbier | 5f70773 | 2018-07-03 16:22:02 +0100 | [diff] [blame] | 49 | typedef typename strategy::result_type Tri; |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 50 | |
| 51 | const unsigned int _Msize; |
| 52 | const unsigned int _Nsize; |
| 53 | const unsigned int _Ksize; |
| 54 | |
Michalis Spyrou | e7e96e0 | 2018-04-13 13:44:10 +0100 | [diff] [blame] | 55 | const unsigned int _nbatches; |
| 56 | const unsigned int _nmultis; |
| 57 | |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 58 | Tr _beta; |
| 59 | |
Anthony Barbier | 5f70773 | 2018-07-03 16:22:02 +0100 | [diff] [blame] | 60 | const CPUInfo * const _ci; |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 61 | |
Anthony Barbier | 5f70773 | 2018-07-03 16:22:02 +0100 | [diff] [blame] | 62 | unsigned int k_block=0; |
| 63 | unsigned int n_block=0; |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 64 | |
David Mansell | e39334c | 2018-07-06 17:53:35 +0100 | [diff] [blame] | 65 | unsigned int window_per_batch() const { |
| 66 | return iceildiv(_Msize, strategy::out_height()); |
| 67 | } |
| 68 | |
| 69 | unsigned int window_per_multi() const { |
| 70 | return window_per_batch() * _nbatches; |
| 71 | } |
| 72 | |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 73 | public: |
| 74 | GemmNative(GemmNative &) = delete; |
Anthony Barbier | 5f70773 | 2018-07-03 16:22:02 +0100 | [diff] [blame] | 75 | GemmNative & operator= (GemmNative &) = delete; |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 76 | |
Anthony Barbier | 5f70773 | 2018-07-03 16:22:02 +0100 | [diff] [blame] | 77 | 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) : |
| 78 | _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] | 79 | /* For now don't do any blocking. TODO: figure out if we should. */ |
| 80 | k_block = K; |
| 81 | n_block = N; |
| 82 | } |
| 83 | |
David Mansell | e39334c | 2018-07-06 17:53:35 +0100 | [diff] [blame] | 84 | // Window is amount per multi multiplied by total number of multis. |
Anthony Barbier | 5f70773 | 2018-07-03 16:22:02 +0100 | [diff] [blame] | 85 | unsigned int get_window_size() const override { |
David Mansell | e39334c | 2018-07-06 17:53:35 +0100 | [diff] [blame] | 86 | return window_per_multi() * _nmultis; |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | // Actually execute the GEMM. |
Anthony Barbier | 5f70773 | 2018-07-03 16:22:02 +0100 | [diff] [blame] | 90 | void execute(unsigned int start, unsigned int end, int) override { |
Michalis Spyrou | e7e96e0 | 2018-04-13 13:44:10 +0100 | [diff] [blame] | 91 | #ifdef CYCLE_PROFILING |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 92 | profiler prof; |
Michalis Spyrou | e7e96e0 | 2018-04-13 13:44:10 +0100 | [diff] [blame] | 93 | #endif |
Anthony Barbier | 5f70773 | 2018-07-03 16:22:02 +0100 | [diff] [blame] | 94 | strategy strat(_ci); |
| 95 | |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 96 | static_assert(std::is_same<To, Toi>::value, "gemm_native: Operand types must be the same."); |
| 97 | static_assert(std::is_same<Tr, Tri>::value, "gemm_native: Result types must be the same."); |
| 98 | |
David Mansell | e39334c | 2018-07-06 17:53:35 +0100 | [diff] [blame] | 99 | /* Compute starting point based on 'start' */ |
| 100 | unsigned int multi = start / window_per_multi(); |
| 101 | unsigned int multi_pos = start % window_per_multi(); |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 102 | |
David Mansell | e39334c | 2018-07-06 17:53:35 +0100 | [diff] [blame] | 103 | unsigned int batch = multi_pos / window_per_batch(); |
| 104 | unsigned int batch_pos = multi_pos % window_per_batch(); |
Michalis Spyrou | e7e96e0 | 2018-04-13 13:44:10 +0100 | [diff] [blame] | 105 | |
David Mansell | e39334c | 2018-07-06 17:53:35 +0100 | [diff] [blame] | 106 | unsigned int y0 = batch_pos * strategy::out_height(); |
| 107 | |
| 108 | for (unsigned int pos=start; pos<end; pos++) { |
| 109 | const unsigned int ymax = std::min(y0 + strategy::out_height(), _Msize); |
Michalis Spyrou | e7e96e0 | 2018-04-13 13:44:10 +0100 | [diff] [blame] | 110 | #ifdef CYCLE_PROFILING |
David Mansell | e39334c | 2018-07-06 17:53:35 +0100 | [diff] [blame] | 111 | auto p = prof.ScopedProfiler(PROFILE_KERNEL, (ymax-y0) * _Nsize * _Ksize); |
Michalis Spyrou | e7e96e0 | 2018-04-13 13:44:10 +0100 | [diff] [blame] | 112 | #endif |
| 113 | |
David Mansell | e39334c | 2018-07-06 17:53:35 +0100 | [diff] [blame] | 114 | strat.kernel(this->_Aptr + (multi * this->_A_multi_stride) + (batch * this->_A_batch_stride) + (y0 * this->_lda), this->_lda, |
| 115 | this->_Bptr + (multi * this->_B_multi_stride), this->_ldb, |
| 116 | this->_Cptr + (multi * this->_C_multi_stride) + (batch * this->_C_batch_stride) + (y0 * this->_ldc), this->_ldc, |
| 117 | _beta, (ymax-y0), _Nsize, _Ksize); |
| 118 | |
| 119 | /* Advance to next item */ |
| 120 | y0 += strategy::out_height(); |
| 121 | |
| 122 | /* Check for batch/multi overflow */ |
| 123 | if (y0 >= _Msize) { |
| 124 | y0=0; |
| 125 | batch++; |
| 126 | if (batch == _nbatches) { |
| 127 | batch=0; |
| 128 | multi++; |
Michalis Spyrou | e7e96e0 | 2018-04-13 13:44:10 +0100 | [diff] [blame] | 129 | } |
| 130 | } |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 131 | } |
| 132 | } |
| 133 | }; |
| 134 | |
| 135 | } // namespace arm_gemm |