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 | |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 26 | #include <stdio.h> |
Anthony Barbier | 5f70773 | 2018-07-03 16:22:02 +0100 | [diff] [blame] | 27 | #include <assert.h> |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 28 | |
| 29 | #include <algorithm> |
| 30 | |
| 31 | #include "arm_gemm.hpp" |
| 32 | #include "utils.hpp" |
| 33 | |
| 34 | #include "buffer_manager.hpp" |
| 35 | #include "mergeresults.hpp" |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 36 | #include "transform.hpp" |
| 37 | |
Michalis Spyrou | e7e96e0 | 2018-04-13 13:44:10 +0100 | [diff] [blame] | 38 | #ifdef CYCLE_PROFILING |
| 39 | #include "profiler.hpp" |
| 40 | #endif |
| 41 | |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 42 | // Some macros used to decide how much working space to allocate. |
| 43 | // Round allocations up to the next cache line. |
Anthony Barbier | 5f70773 | 2018-07-03 16:22:02 +0100 | [diff] [blame] | 44 | #define ALLOC_ROUND 64 |
| 45 | #define ROUND_UP(x) ((((x) + ALLOC_ROUND-1) / ALLOC_ROUND) * ALLOC_ROUND) |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 46 | |
| 47 | // Implementation of the GemmCommon abstract class. |
| 48 | // |
| 49 | // This implementation interleaves the source matrices in blocks - good for |
| 50 | // larger matrices. |
Anthony Barbier | 5f70773 | 2018-07-03 16:22:02 +0100 | [diff] [blame] | 51 | namespace arm_gemm { |
| 52 | |
| 53 | template<typename strategy, typename To, typename Tr> |
| 54 | class GemmInterleaved : public GemmCommon<To, Tr> { |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 55 | typedef typename strategy::operand_type Toi; |
Anthony Barbier | 5f70773 | 2018-07-03 16:22:02 +0100 | [diff] [blame] | 56 | typedef typename strategy::result_type Tri; |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 57 | |
| 58 | /* const properties set by constructor */ |
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 | |
| 61 | const unsigned int _Msize; |
| 62 | const unsigned int _Nsize; |
| 63 | const unsigned int _Ksize; |
| 64 | |
Michalis Spyrou | e7e96e0 | 2018-04-13 13:44:10 +0100 | [diff] [blame] | 65 | const unsigned int _nbatches; |
| 66 | const unsigned int _nmulti; |
| 67 | |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 68 | const bool _trA; |
| 69 | const bool _trB; |
| 70 | |
| 71 | const Tr _alpha; |
| 72 | const Tr _beta; |
| 73 | |
Anthony Barbier | 5f70773 | 2018-07-03 16:22:02 +0100 | [diff] [blame] | 74 | const int _maxthreads; |
| 75 | int _nthreads; |
| 76 | const bool _pretransposed; |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 77 | |
| 78 | /* Blocking info */ |
Anthony Barbier | 5f70773 | 2018-07-03 16:22:02 +0100 | [diff] [blame] | 79 | unsigned int _k_block=0; |
| 80 | unsigned int _x_block=0; |
| 81 | unsigned int _Mround=0; |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 82 | |
| 83 | /* Working space, pretransposed buffer, buffer manager */ |
Anthony Barbier | 5f70773 | 2018-07-03 16:22:02 +0100 | [diff] [blame] | 84 | const Toi *_B_transposed=nullptr; |
| 85 | BufferManager *_bm=nullptr; |
| 86 | void *_working_space=nullptr; |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 87 | |
| 88 | /* We will need to walk through the blocks of B in a few contexts, so |
| 89 | * factor that out. */ |
Anthony Barbier | 5f70773 | 2018-07-03 16:22:02 +0100 | [diff] [blame] | 90 | class blockwalker { |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 91 | private: |
Michalis Spyrou | e7e96e0 | 2018-04-13 13:44:10 +0100 | [diff] [blame] | 92 | /* Size loops, etc. based on our parent's configuration */ |
| 93 | const GemmInterleaved<strategy, To, Tr> &_parent; |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 94 | |
Anthony Barbier | 5f70773 | 2018-07-03 16:22:02 +0100 | [diff] [blame] | 95 | /* K, X and multi parameters for current iteration. */ |
| 96 | unsigned int _k0=0, _x0=0, _multi=0; |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 97 | |
Anthony Barbier | 5f70773 | 2018-07-03 16:22:02 +0100 | [diff] [blame] | 98 | unsigned int _index=0; |
| 99 | bool _done=false; |
| 100 | bool _newkblock=true; |
| 101 | bool _newmulti=true; |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 102 | |
| 103 | public: |
Anthony Barbier | 5f70773 | 2018-07-03 16:22:02 +0100 | [diff] [blame] | 104 | blockwalker(const GemmInterleaved<strategy, To, Tr> &parent) : _parent(parent) { } |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 105 | |
Anthony Barbier | 5f70773 | 2018-07-03 16:22:02 +0100 | [diff] [blame] | 106 | unsigned int xmax() { |
Michalis Spyrou | e7e96e0 | 2018-04-13 13:44:10 +0100 | [diff] [blame] | 107 | return std::min(_x0 + _parent._x_block, _parent._Nsize); |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 108 | } |
| 109 | |
Anthony Barbier | 5f70773 | 2018-07-03 16:22:02 +0100 | [diff] [blame] | 110 | unsigned int kmax() { |
Michalis Spyrou | e7e96e0 | 2018-04-13 13:44:10 +0100 | [diff] [blame] | 111 | return std::min(_k0 + _parent._k_block, _parent._Ksize); |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | /* Advance to the next block, return false at the end. */ |
Anthony Barbier | 5f70773 | 2018-07-03 16:22:02 +0100 | [diff] [blame] | 115 | bool advance(void) { |
| 116 | if (_done) { |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 117 | return false; |
| 118 | } |
| 119 | |
Anthony Barbier | 5f70773 | 2018-07-03 16:22:02 +0100 | [diff] [blame] | 120 | _newkblock=false; |
Michalis Spyrou | e7e96e0 | 2018-04-13 13:44:10 +0100 | [diff] [blame] | 121 | _x0 += _parent._x_block; |
Anthony Barbier | 5f70773 | 2018-07-03 16:22:02 +0100 | [diff] [blame] | 122 | if (_x0 >= _parent._Nsize) { |
| 123 | _x0=0; |
Michalis Spyrou | e7e96e0 | 2018-04-13 13:44:10 +0100 | [diff] [blame] | 124 | _k0 += _parent._k_block; |
Anthony Barbier | 5f70773 | 2018-07-03 16:22:02 +0100 | [diff] [blame] | 125 | if (_k0 >= _parent._Ksize) { |
| 126 | _k0=0; |
Michalis Spyrou | e7e96e0 | 2018-04-13 13:44:10 +0100 | [diff] [blame] | 127 | _multi++; |
Anthony Barbier | 5f70773 | 2018-07-03 16:22:02 +0100 | [diff] [blame] | 128 | if (_multi >= _parent._nmulti) { |
| 129 | _done=true; |
Michalis Spyrou | e7e96e0 | 2018-04-13 13:44:10 +0100 | [diff] [blame] | 130 | return false; |
| 131 | } |
Anthony Barbier | 5f70773 | 2018-07-03 16:22:02 +0100 | [diff] [blame] | 132 | _newmulti=true; |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 133 | } |
Anthony Barbier | 5f70773 | 2018-07-03 16:22:02 +0100 | [diff] [blame] | 134 | _newkblock=true; |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 135 | } |
| 136 | _index++; |
| 137 | |
| 138 | return true; |
| 139 | } |
| 140 | |
Anthony Barbier | 5f70773 | 2018-07-03 16:22:02 +0100 | [diff] [blame] | 141 | unsigned int k0(void) { return _k0; } |
| 142 | unsigned int x0(void) { return _x0; } |
| 143 | unsigned int multi(void) { return _multi; } |
| 144 | unsigned int index(void) { return _index; } |
| 145 | bool done(void) { return _done; } |
| 146 | bool newkblock(void) { return _newkblock; } |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 147 | }; |
| 148 | |
| 149 | // A working size: One of these needed, regardless of thread count. Divided according to window. |
Anthony Barbier | 5f70773 | 2018-07-03 16:22:02 +0100 | [diff] [blame] | 150 | size_t get_a_working_size() const { |
Michalis Spyrou | e7e96e0 | 2018-04-13 13:44:10 +0100 | [diff] [blame] | 151 | return ROUND_UP(sizeof(Toi) * _k_block * _Mround * _nbatches); |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | // B working size: 0, 1 or 3 of these needed depending on pretransposed and threading settings. |
Anthony Barbier | 5f70773 | 2018-07-03 16:22:02 +0100 | [diff] [blame] | 155 | size_t get_b_working_size() const { |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 156 | return ROUND_UP(sizeof(Toi) * _x_block * _k_block); |
| 157 | } |
| 158 | |
| 159 | // C working size: One needed per thread. |
Anthony Barbier | 5f70773 | 2018-07-03 16:22:02 +0100 | [diff] [blame] | 160 | size_t get_c_working_size() const { |
David Mansell | d93991e | 2018-07-06 14:52:52 +0100 | [diff] [blame] | 161 | return ROUND_UP(sizeof(Tri) * _x_block * strategy::out_height()); |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | // Internal execute function. |
| 165 | // This supports both the "pretransposed" and "standard" interfaces via the template parameter. |
Anthony Barbier | 5f70773 | 2018-07-03 16:22:02 +0100 | [diff] [blame] | 166 | template<bool pretransposed> |
| 167 | void execute_internal(unsigned int start, unsigned int end, int threadid) { |
Michalis Spyrou | e7e96e0 | 2018-04-13 13:44:10 +0100 | [diff] [blame] | 168 | #ifdef CYCLE_PROFILING |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 169 | profiler prof; |
Michalis Spyrou | e7e96e0 | 2018-04-13 13:44:10 +0100 | [diff] [blame] | 170 | #endif |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 171 | strategy strat(_ci); |
| 172 | |
Michalis Spyrou | e7e96e0 | 2018-04-13 13:44:10 +0100 | [diff] [blame] | 173 | blockwalker current(*this); |
Anthony Barbier | 5f70773 | 2018-07-03 16:22:02 +0100 | [diff] [blame] | 174 | blockwalker next=current; |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 175 | |
Michalis Spyrou | e7e96e0 | 2018-04-13 13:44:10 +0100 | [diff] [blame] | 176 | /* Translate 'start' and 'end' into a position within the batches and rows. */ |
David Mansell | d93991e | 2018-07-06 14:52:52 +0100 | [diff] [blame] | 177 | const unsigned int window_per_batch = _Mround / strategy::out_height(); |
Anthony Barbier | 5f70773 | 2018-07-03 16:22:02 +0100 | [diff] [blame] | 178 | unsigned int batch_0 = start / window_per_batch; |
| 179 | unsigned int batch_end = end / window_per_batch; |
Michalis Spyrou | e7e96e0 | 2018-04-13 13:44:10 +0100 | [diff] [blame] | 180 | |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 181 | /* Compute the M values to operate on */ |
David Mansell | d93991e | 2018-07-06 14:52:52 +0100 | [diff] [blame] | 182 | unsigned int m_0 = (start - (batch_0 * window_per_batch)) * strategy::out_height(); |
| 183 | unsigned int m_max = (end - (batch_end * window_per_batch)) * strategy::out_height(); |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 184 | |
| 185 | /* Make sure we've been set up correctly. */ |
Anthony Barbier | 5f70773 | 2018-07-03 16:22:02 +0100 | [diff] [blame] | 186 | if (pretransposed) { |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 187 | assert(_B_transposed); |
Anthony Barbier | 5f70773 | 2018-07-03 16:22:02 +0100 | [diff] [blame] | 188 | } else { |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 189 | assert(_bm); |
| 190 | } |
| 191 | |
| 192 | assert(_working_space); |
| 193 | int8_t *working_space_bytes = reinterpret_cast<int8_t *>(_working_space); |
| 194 | |
Anthony Barbier | 5f70773 | 2018-07-03 16:22:02 +0100 | [diff] [blame] | 195 | // Private buffers. Treat working_space as an array of C buffers |
| 196 | // (one per thread) first, followed by the (window-divided) A |
| 197 | // buffer. |
Michalis Spyrou | e7e96e0 | 2018-04-13 13:44:10 +0100 | [diff] [blame] | 198 | // Set a_panel to the base of the A buffers - compute offsets into it based on M/batches later. |
Anthony Barbier | 5f70773 | 2018-07-03 16:22:02 +0100 | [diff] [blame] | 199 | Toi * const a_panel = reinterpret_cast<Toi *>(working_space_bytes + (_maxthreads * get_c_working_size())); |
| 200 | Tri * const c_panel = reinterpret_cast<Tri *>(working_space_bytes + (threadid * get_c_working_size())); |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 201 | |
| 202 | // Shared buffers - these come either from BufferManager or _B_transposed. |
| 203 | const Toi *b_panel; |
| 204 | |
Anthony Barbier | 5f70773 | 2018-07-03 16:22:02 +0100 | [diff] [blame] | 205 | if (pretransposed) { |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 206 | b_panel = _B_transposed; |
| 207 | } |
| 208 | |
| 209 | //printf("Starting GEMM loop, x_block=%d, k_block=%d\n", _x_block, _k_block); |
| 210 | |
| 211 | // newkblock() is always true on the first iteration, so this will be set properly on the first loop. |
| 212 | int kern_k = 0; |
| 213 | |
Anthony Barbier | 5f70773 | 2018-07-03 16:22:02 +0100 | [diff] [blame] | 214 | for (;!current.done();current.advance()) { |
| 215 | if (current.newkblock()) { |
Michalis Spyrou | e7e96e0 | 2018-04-13 13:44:10 +0100 | [diff] [blame] | 216 | #ifdef CYCLE_PROFILING |
David Mansell | d93991e | 2018-07-06 14:52:52 +0100 | [diff] [blame] | 217 | auto p=prof.ScopedProfiler(PROFILE_PREPA, (end - start) * strategy::out_height() * (current.kmax()-current.k0()) * sizeof(Toi)); |
Michalis Spyrou | e7e96e0 | 2018-04-13 13:44:10 +0100 | [diff] [blame] | 218 | #endif |
Anthony Barbier | 5f70773 | 2018-07-03 16:22:02 +0100 | [diff] [blame] | 219 | for (unsigned int batch = batch_0; batch <= batch_end; batch++) { |
| 220 | unsigned int first_m = (batch == batch_0) ? m_0 : 0; |
Michalis Spyrou | e7e96e0 | 2018-04-13 13:44:10 +0100 | [diff] [blame] | 221 | unsigned int last_m = (batch == batch_end) ? m_max : _Msize; |
| 222 | |
Anthony Barbier | 5f70773 | 2018-07-03 16:22:02 +0100 | [diff] [blame] | 223 | if (first_m >= last_m) |
Michalis Spyrou | e7e96e0 | 2018-04-13 13:44:10 +0100 | [diff] [blame] | 224 | continue; |
Anthony Barbier | 5f70773 | 2018-07-03 16:22:02 +0100 | [diff] [blame] | 225 | |
David Mansell | d93991e | 2018-07-06 14:52:52 +0100 | [diff] [blame] | 226 | strat.transforms.PrepareA(a_panel + ((batch * _Mround + first_m) * _k_block), |
| 227 | this->_Aptr + (batch * this->_A_batch_stride) + (current.multi() * this->_A_multi_stride), |
| 228 | this->_lda, first_m, last_m, current.k0(), current.kmax(), _trA); |
Michalis Spyrou | e7e96e0 | 2018-04-13 13:44:10 +0100 | [diff] [blame] | 229 | } |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 230 | |
| 231 | // Figure out how many "K" the kernel will actually process. |
David Mansell | d93991e | 2018-07-06 14:52:52 +0100 | [diff] [blame] | 232 | kern_k = iceildiv(current.kmax() - current.k0(), strategy::k_unroll()); |
| 233 | kern_k *= strat.k_unroll(); |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 234 | } |
| 235 | |
David Mansell | d93991e | 2018-07-06 14:52:52 +0100 | [diff] [blame] | 236 | int bblocks = iceildiv(current.xmax() - current.x0(), strategy::out_width()); |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 237 | |
Anthony Barbier | 5f70773 | 2018-07-03 16:22:02 +0100 | [diff] [blame] | 238 | if (!pretransposed) { |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 239 | /* Look ahead to the next block and populate it if necessary. |
| 240 | * This avoids the populate operation becoming a bottleneck, and |
| 241 | * helps keep the threads synchronized (the first thread to get |
| 242 | * here will populate while the rest will advance). |
| 243 | * |
| 244 | * If we are running single threaded, bm->try_populate() will do |
| 245 | * nothing. |
| 246 | */ |
Anthony Barbier | 5f70773 | 2018-07-03 16:22:02 +0100 | [diff] [blame] | 247 | if (next.advance()) { |
| 248 | _bm->try_populate(next.index(), [&](void *buffer) { |
Michalis Spyrou | e7e96e0 | 2018-04-13 13:44:10 +0100 | [diff] [blame] | 249 | #ifdef CYCLE_PROFILING |
Anthony Barbier | 5f70773 | 2018-07-03 16:22:02 +0100 | [diff] [blame] | 250 | auto p=prof.ScopedProfiler(PROFILE_PREPB, (next.xmax()-next.x0()) * (next.kmax()-next.k0()) * sizeof(Toi)); |
Michalis Spyrou | e7e96e0 | 2018-04-13 13:44:10 +0100 | [diff] [blame] | 251 | #endif |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 252 | |
Michalis Spyrou | e7e96e0 | 2018-04-13 13:44:10 +0100 | [diff] [blame] | 253 | Toi *b_panel = reinterpret_cast<Toi *>(buffer); |
David Mansell | d93991e | 2018-07-06 14:52:52 +0100 | [diff] [blame] | 254 | |
| 255 | strat.transforms.PrepareB(b_panel, this->_Bptr + (next.multi() * this->_B_multi_stride), this->_ldb, |
| 256 | next.x0(), next.xmax(), next.k0(), next.kmax(), _trB); |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 257 | }); |
Michalis Spyrou | e7e96e0 | 2018-04-13 13:44:10 +0100 | [diff] [blame] | 258 | } |
Anthony Barbier | 5f70773 | 2018-07-03 16:22:02 +0100 | [diff] [blame] | 259 | |
Michalis Spyrou | e7e96e0 | 2018-04-13 13:44:10 +0100 | [diff] [blame] | 260 | /* Get the buffer for this iteration from the BufferManager. */ |
Anthony Barbier | 5f70773 | 2018-07-03 16:22:02 +0100 | [diff] [blame] | 261 | b_panel = reinterpret_cast<Toi *>(_bm->get(current.index(), [&](void *bpv) { |
Michalis Spyrou | e7e96e0 | 2018-04-13 13:44:10 +0100 | [diff] [blame] | 262 | #ifdef CYCLE_PROFILING |
Anthony Barbier | 5f70773 | 2018-07-03 16:22:02 +0100 | [diff] [blame] | 263 | auto p=prof.ScopedProfiler(PROFILE_PREPB, (current.xmax()-current.x0()) * (current.kmax()-current.k0()) * sizeof(Toi)); |
Michalis Spyrou | e7e96e0 | 2018-04-13 13:44:10 +0100 | [diff] [blame] | 264 | #endif |
| 265 | |
| 266 | Toi *b_panel = reinterpret_cast<Toi *>(bpv); |
David Mansell | d93991e | 2018-07-06 14:52:52 +0100 | [diff] [blame] | 267 | |
| 268 | strat.transforms.PrepareB(b_panel, this->_Bptr + (current.multi() * this->_B_multi_stride), this->_ldb, |
| 269 | current.x0(), current.xmax(), current.k0(), current.kmax(), _trB); |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 270 | })); |
| 271 | } |
| 272 | |
| 273 | /* Do the actual work. */ |
Anthony Barbier | 5f70773 | 2018-07-03 16:22:02 +0100 | [diff] [blame] | 274 | for (unsigned int batch = batch_0; batch <= batch_end; batch++) { |
| 275 | unsigned int first_m = (batch == batch_0) ? m_0 : 0; |
Michalis Spyrou | e7e96e0 | 2018-04-13 13:44:10 +0100 | [diff] [blame] | 276 | unsigned int last_m = (batch == batch_end) ? m_max : _Msize; |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 277 | |
Michalis Spyrou | e7e96e0 | 2018-04-13 13:44:10 +0100 | [diff] [blame] | 278 | const Toi *a_ptr = a_panel + (batch * _Mround + first_m) * _k_block; |
| 279 | |
Anthony Barbier | 5f70773 | 2018-07-03 16:22:02 +0100 | [diff] [blame] | 280 | if (first_m >= last_m) |
Michalis Spyrou | e7e96e0 | 2018-04-13 13:44:10 +0100 | [diff] [blame] | 281 | continue; |
| 282 | |
David Mansell | d93991e | 2018-07-06 14:52:52 +0100 | [diff] [blame] | 283 | for (unsigned int y=first_m; y<last_m; y+=strategy::out_height()) { |
| 284 | unsigned int ymax = std::min(_Msize, y + strategy::out_height()); |
Michalis Spyrou | e7e96e0 | 2018-04-13 13:44:10 +0100 | [diff] [blame] | 285 | |
| 286 | { |
| 287 | #ifdef CYCLE_PROFILING |
David Mansell | d93991e | 2018-07-06 14:52:52 +0100 | [diff] [blame] | 288 | auto p=prof.ScopedProfiler(PROFILE_KERNEL, (strategy::out_height() * bblocks * strategy::out_width() * kern_k)); |
Michalis Spyrou | e7e96e0 | 2018-04-13 13:44:10 +0100 | [diff] [blame] | 289 | #endif |
| 290 | |
| 291 | strat.kernel(a_ptr, b_panel, c_panel, 1, bblocks, kern_k); |
| 292 | |
David Mansell | d93991e | 2018-07-06 14:52:52 +0100 | [diff] [blame] | 293 | a_ptr += (strategy::out_height() * kern_k); |
Michalis Spyrou | e7e96e0 | 2018-04-13 13:44:10 +0100 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | { |
| 297 | #ifdef CYCLE_PROFILING |
David Mansell | d93991e | 2018-07-06 14:52:52 +0100 | [diff] [blame] | 298 | auto p=prof.ScopedProfiler(PROFILE_MERGE, (strategy::out_height() * bblocks * strategy::out_width() * sizeof(Tr))); |
Michalis Spyrou | e7e96e0 | 2018-04-13 13:44:10 +0100 | [diff] [blame] | 299 | #endif |
David Mansell | d93991e | 2018-07-06 14:52:52 +0100 | [diff] [blame] | 300 | strat.transforms.Merge(this->_Cptr + (batch * this->_C_batch_stride) + (current.multi() * this->_C_multi_stride), |
| 301 | c_panel, this->_ldc, y, ymax, current.x0(), current.xmax(), |
| 302 | _alpha, (current.k0()==0 ? _beta : static_cast<Tr>(1))); |
Michalis Spyrou | e7e96e0 | 2018-04-13 13:44:10 +0100 | [diff] [blame] | 303 | } |
| 304 | } |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 305 | } |
| 306 | |
Anthony Barbier | 5f70773 | 2018-07-03 16:22:02 +0100 | [diff] [blame] | 307 | if (pretransposed) { |
David Mansell | d93991e | 2018-07-06 14:52:52 +0100 | [diff] [blame] | 308 | b_panel += (bblocks * strat.out_width() * kern_k); |
Anthony Barbier | 5f70773 | 2018-07-03 16:22:02 +0100 | [diff] [blame] | 309 | } else { |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 310 | _bm->release(current.index()); |
| 311 | } |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | public: |
| 316 | GemmInterleaved(GemmInterleaved &) = delete; |
Anthony Barbier | 5f70773 | 2018-07-03 16:22:02 +0100 | [diff] [blame] | 317 | GemmInterleaved & operator= (GemmInterleaved &) = delete; |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 318 | |
| 319 | /* Constructor */ |
David Mansell | e39334c | 2018-07-06 17:53:35 +0100 | [diff] [blame] | 320 | GemmInterleaved(const GemmArgs<Tr> &args) |
| 321 | : _ci(args._ci), _Msize(args._Msize), _Nsize(args._Nsize), _Ksize(args._Ksize), |
| 322 | _nbatches(args._nbatches), _nmulti(args._nmulti), _trA(args._trA), _trB(args._trB), |
| 323 | _alpha(args._alpha), _beta(args._beta), _maxthreads(args._maxthreads), _nthreads(args._maxthreads), |
| 324 | _pretransposed(args._pretransposed_hint) { |
| 325 | const unsigned int L1_size = _ci->get_L1_cache_size(); |
| 326 | const unsigned int L2_size = _ci->get_L2_cache_size(); |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 327 | |
David Mansell | e39334c | 2018-07-06 17:53:35 +0100 | [diff] [blame] | 328 | assert(_maxthreads > 0); |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 329 | |
| 330 | // Work out blocking parameters |
| 331 | |
| 332 | // k_block: Find out how much of the larger array can be loaded into half the cache. |
| 333 | // This should account for associative caches. |
David Mansell | d93991e | 2018-07-06 14:52:52 +0100 | [diff] [blame] | 334 | _k_block = (L1_size / 2) / (sizeof(Toi) * (std::max(strategy::out_width(), strategy::out_height()))); |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 335 | |
| 336 | // Needs to be (at least a single) multiple of the K unroll level. |
David Mansell | d93991e | 2018-07-06 14:52:52 +0100 | [diff] [blame] | 337 | _k_block /= strategy::k_unroll(); |
| 338 | _k_block = std::max(_k_block, 1U) * strategy::k_unroll(); |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 339 | |
| 340 | // Now tune to presented problem size; this is how many blocks we need. |
David Mansell | e39334c | 2018-07-06 17:53:35 +0100 | [diff] [blame] | 341 | int num_k_blocks = iceildiv(_Ksize, _k_block); |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 342 | |
| 343 | // So divide the space equally into that many blocks. |
David Mansell | e39334c | 2018-07-06 17:53:35 +0100 | [diff] [blame] | 344 | _k_block = iceildiv(_Ksize, num_k_blocks); |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 345 | |
| 346 | // And round UP to the K unroll level required. |
David Mansell | d93991e | 2018-07-06 14:52:52 +0100 | [diff] [blame] | 347 | _k_block = iceildiv(_k_block, strategy::k_unroll()); |
| 348 | _k_block *= strategy::k_unroll(); |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 349 | |
| 350 | // x_block: Work out how many rows (of length k_block) will fit in the L2 |
| 351 | // Don't allocate more than 90% of the L2 to allow for overheads, and subtract off the L1 contents. |
David Mansell | d93991e | 2018-07-06 14:52:52 +0100 | [diff] [blame] | 352 | _x_block = (((L2_size * 9) / 10) - (_k_block * sizeof(Toi) * (strategy::out_width() + strategy::out_height()))) / |
Anthony Barbier | 5f70773 | 2018-07-03 16:22:02 +0100 | [diff] [blame] | 353 | (sizeof(Toi) * _k_block); |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 354 | |
| 355 | // Needs to be (at least a single) multiple of the kernel output width. |
David Mansell | d93991e | 2018-07-06 14:52:52 +0100 | [diff] [blame] | 356 | _x_block /= strategy::out_width(); |
| 357 | _x_block = std::max(_x_block, 1U) * strategy::out_width(); |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 358 | |
| 359 | // And tune to the presented problem size. |
David Mansell | e39334c | 2018-07-06 17:53:35 +0100 | [diff] [blame] | 360 | int num_x_blocks = iceildiv(_Nsize, _x_block); |
| 361 | _x_block = iceildiv(_Nsize, num_x_blocks); |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 362 | |
David Mansell | d93991e | 2018-07-06 14:52:52 +0100 | [diff] [blame] | 363 | _x_block = iceildiv(_x_block, strategy::out_width()); |
| 364 | _x_block *= strategy::out_width(); |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 365 | |
| 366 | // Work out the rounded size of M - needed for some buffers. |
David Mansell | e39334c | 2018-07-06 17:53:35 +0100 | [diff] [blame] | 367 | _Mround = iceildiv(_Msize, strategy::out_height()); |
David Mansell | d93991e | 2018-07-06 14:52:52 +0100 | [diff] [blame] | 368 | _Mround *= strategy::out_height(); |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | // Interface implementation - Compulsory functions |
| 372 | |
Michalis Spyrou | e7e96e0 | 2018-04-13 13:44:10 +0100 | [diff] [blame] | 373 | // Window size: Only the last thread should do a ragged block, so dole |
| 374 | // out work in units of out_height. Factor batches into the window, but |
| 375 | // not multi for now (as this would cause problems with the buffer |
| 376 | // manager). |
Anthony Barbier | 5f70773 | 2018-07-03 16:22:02 +0100 | [diff] [blame] | 377 | unsigned int get_window_size() const override { |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 378 | // _Mround is a multiple of out_height by definition. |
David Mansell | d93991e | 2018-07-06 14:52:52 +0100 | [diff] [blame] | 379 | return (_Mround / strategy::out_height()) * _nbatches; |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | // set_nthreads: pass on to buffer manager to avoid it waiting for non-existant threads. |
Anthony Barbier | 5f70773 | 2018-07-03 16:22:02 +0100 | [diff] [blame] | 383 | void set_nthreads(int nthreads) override { |
| 384 | _nthreads = std::min(nthreads, _maxthreads); |
| 385 | if (_bm) { |
| 386 | _bm->set_nthreads(_nthreads); |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 387 | } |
| 388 | } |
| 389 | |
| 390 | // Execute |
Anthony Barbier | 5f70773 | 2018-07-03 16:22:02 +0100 | [diff] [blame] | 391 | void execute(unsigned int start, unsigned int end, int threadid) override { |
| 392 | if (_pretransposed) { |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 393 | execute_internal<true>(start, end, threadid); |
Anthony Barbier | 5f70773 | 2018-07-03 16:22:02 +0100 | [diff] [blame] | 394 | } else { |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 395 | execute_internal<false>(start, end, threadid); |
| 396 | } |
| 397 | } |
| 398 | |
| 399 | // Interface implementation - working space |
Anthony Barbier | 5f70773 | 2018-07-03 16:22:02 +0100 | [diff] [blame] | 400 | size_t get_working_size() const override { |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 401 | // In all cases, we need one A buffer plus a C buffer per thread. |
| 402 | size_t size = get_a_working_size() + (get_c_working_size() * _maxthreads); |
| 403 | |
| 404 | // For pretransposed case, there is no working space needed for B. |
| 405 | // Otherwise, we need a BufferManager. |
Anthony Barbier | 5f70773 | 2018-07-03 16:22:02 +0100 | [diff] [blame] | 406 | if (!_pretransposed) { |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 407 | size += BufferManager::get_storage_requirement(_maxthreads, get_b_working_size()); |
| 408 | } |
| 409 | |
| 410 | size += 64; // Add on a cache line extra for alignment. |
| 411 | |
| 412 | return size; |
| 413 | } |
| 414 | |
Anthony Barbier | 5f70773 | 2018-07-03 16:22:02 +0100 | [diff] [blame] | 415 | void set_working_space(void *working_space) override { |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 416 | // Make sure everything ends up cache line aligned |
| 417 | int8_t *working_space_bytes = reinterpret_cast<int8_t *>(working_space); |
Anthony Barbier | 5f70773 | 2018-07-03 16:22:02 +0100 | [diff] [blame] | 418 | intptr_t working_space_int = reinterpret_cast<intptr_t>(working_space); |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 419 | |
Anthony Barbier | 5f70773 | 2018-07-03 16:22:02 +0100 | [diff] [blame] | 420 | size_t diff=0; |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 421 | |
Anthony Barbier | 5f70773 | 2018-07-03 16:22:02 +0100 | [diff] [blame] | 422 | if (working_space_int & 0x3F) { |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 423 | diff = 0x40 - (working_space_int & 0x3F); |
| 424 | } |
| 425 | |
| 426 | working_space_bytes += diff; |
| 427 | |
Anthony Barbier | 5f70773 | 2018-07-03 16:22:02 +0100 | [diff] [blame] | 428 | if (_pretransposed) { |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 429 | // Pretransposed case: just set internal pointer to parameter value. |
| 430 | _working_space = reinterpret_cast<void *>(working_space_bytes); |
Anthony Barbier | 5f70773 | 2018-07-03 16:22:02 +0100 | [diff] [blame] | 431 | } else { |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 432 | // Otherwise, use the first part of the working space for the buffer manager. |
| 433 | // It's legal to call this again so don't leak a buffer manager if it already existed. |
| 434 | delete _bm; |
| 435 | |
Anthony Barbier | 5f70773 | 2018-07-03 16:22:02 +0100 | [diff] [blame] | 436 | _bm = new BufferManager(_nthreads, get_b_working_size(), reinterpret_cast<void *>(working_space_bytes)); |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 437 | |
| 438 | working_space_bytes += BufferManager::get_storage_requirement(_maxthreads, get_b_working_size()); |
| 439 | |
| 440 | _working_space = reinterpret_cast<void *>(working_space_bytes); |
| 441 | } |
| 442 | } |
| 443 | |
| 444 | // Interface implementation - pretransposed |
Anthony Barbier | 5f70773 | 2018-07-03 16:22:02 +0100 | [diff] [blame] | 445 | bool B_is_pretransposed() const override { |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 446 | return _pretransposed; |
| 447 | } |
| 448 | |
Anthony Barbier | 5f70773 | 2018-07-03 16:22:02 +0100 | [diff] [blame] | 449 | bool B_pretranspose_required() const override { |
| 450 | return _pretransposed && (_B_transposed==nullptr); |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 451 | } |
| 452 | |
| 453 | // TODO: this could almost certainly be considerably simpler. |
Anthony Barbier | 5f70773 | 2018-07-03 16:22:02 +0100 | [diff] [blame] | 454 | size_t get_B_pretransposed_array_size() const override { |
| 455 | size_t total=0; |
Michalis Spyrou | e7e96e0 | 2018-04-13 13:44:10 +0100 | [diff] [blame] | 456 | blockwalker current(*this); |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 457 | |
Anthony Barbier | 5f70773 | 2018-07-03 16:22:02 +0100 | [diff] [blame] | 458 | do { |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 459 | /* Figure out the size of each block. */ |
| 460 | size_t x_size = (current.xmax() - current.x0()); |
| 461 | size_t k_size = (current.kmax() - current.k0()); |
| 462 | |
| 463 | /* Round sizes up as needed. */ |
David Mansell | d93991e | 2018-07-06 14:52:52 +0100 | [diff] [blame] | 464 | x_size = iceildiv(x_size, strategy::out_width()); |
| 465 | x_size *= strategy::out_width(); |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 466 | |
David Mansell | d93991e | 2018-07-06 14:52:52 +0100 | [diff] [blame] | 467 | k_size = iceildiv(k_size, strategy::k_unroll()); |
| 468 | k_size *= strategy::k_unroll(); |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 469 | |
| 470 | total += x_size * k_size * sizeof(Toi); |
Anthony Barbier | 5f70773 | 2018-07-03 16:22:02 +0100 | [diff] [blame] | 471 | } while (current.advance()); |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 472 | |
| 473 | return total; |
| 474 | } |
| 475 | |
Anthony Barbier | 5f70773 | 2018-07-03 16:22:02 +0100 | [diff] [blame] | 476 | void pretranspose_B_array(void *in_buffer, const To *B, const int ldb, const int B_multi_stride) override { |
Michalis Spyrou | e7e96e0 | 2018-04-13 13:44:10 +0100 | [diff] [blame] | 477 | blockwalker current(*this); |
Anthony Barbier | 5f70773 | 2018-07-03 16:22:02 +0100 | [diff] [blame] | 478 | Toi *buffer = reinterpret_cast<Toi *>(in_buffer); |
| 479 | _B_transposed = buffer; |
David Mansell | d93991e | 2018-07-06 14:52:52 +0100 | [diff] [blame] | 480 | strategy strat(_ci); |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 481 | |
Anthony Barbier | 5f70773 | 2018-07-03 16:22:02 +0100 | [diff] [blame] | 482 | do { |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 483 | /* Figure out the size of each block. */ |
| 484 | size_t x_size = (current.xmax() - current.x0()); |
| 485 | size_t k_size = (current.kmax() - current.k0()); |
| 486 | |
| 487 | /* Round sizes up as needed. */ |
David Mansell | d93991e | 2018-07-06 14:52:52 +0100 | [diff] [blame] | 488 | x_size = iceildiv(x_size, strategy::out_width()); |
| 489 | x_size *= strategy::out_width(); |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 490 | |
David Mansell | d93991e | 2018-07-06 14:52:52 +0100 | [diff] [blame] | 491 | k_size = iceildiv(k_size, strategy::k_unroll()); |
| 492 | k_size *= strategy::k_unroll(); |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 493 | |
David Mansell | d93991e | 2018-07-06 14:52:52 +0100 | [diff] [blame] | 494 | strat.transforms.PrepareB(buffer, B + (current.multi() * B_multi_stride), ldb, |
| 495 | current.x0(), current.xmax(), current.k0(), current.kmax(), _trB); |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 496 | |
| 497 | buffer += (x_size * k_size); |
Anthony Barbier | 5f70773 | 2018-07-03 16:22:02 +0100 | [diff] [blame] | 498 | } while (current.advance()); |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 499 | } |
| 500 | |
Anthony Barbier | 5f70773 | 2018-07-03 16:22:02 +0100 | [diff] [blame] | 501 | void set_pretransposed_B_data(void *in_buffer) override { |
Michalis Spyrou | e7e96e0 | 2018-04-13 13:44:10 +0100 | [diff] [blame] | 502 | _B_transposed = reinterpret_cast<Toi *>(in_buffer); |
| 503 | } |
| 504 | |
Anthony Barbier | 5f70773 | 2018-07-03 16:22:02 +0100 | [diff] [blame] | 505 | ~GemmInterleaved() override { |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 506 | delete _bm; |
| 507 | } |
| 508 | }; |
| 509 | |
| 510 | } // namespace arm_gemm |