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" |
| 31 | #include "profiler.hpp" |
| 32 | #include "transform.hpp" |
| 33 | |
| 34 | namespace arm_gemm |
| 35 | { |
| 36 | // Implementation of the GemmCommon abstract class. |
| 37 | // |
| 38 | // This is implementation is for native GEMM with no transposition. |
| 39 | // |
| 40 | // By default the source data is used in-place, but if type conversion is |
| 41 | // needed we need to allocate working space (CURRENTLY NOT IMPLEMENTED). |
| 42 | |
| 43 | template <typename strategy, typename To, typename Tr> |
| 44 | class GemmNative : public GemmCommon<To, Tr> |
| 45 | { |
| 46 | typedef typename strategy::operand_type Toi; |
| 47 | typedef typename strategy::result_type Tri; |
| 48 | |
| 49 | const unsigned int _Msize; |
| 50 | const unsigned int _Nsize; |
| 51 | const unsigned int _Ksize; |
| 52 | |
| 53 | Tr _beta; |
| 54 | |
| 55 | const CPUInfo *const _ci; |
| 56 | |
| 57 | unsigned int k_block = 0; |
| 58 | unsigned int n_block = 0; |
| 59 | |
| 60 | public: |
| 61 | GemmNative(GemmNative &) = delete; |
| 62 | GemmNative &operator=(GemmNative &) = delete; |
| 63 | |
| 64 | GemmNative(const CPUInfo *ci, const unsigned int M, const unsigned int N, const unsigned int K, const Tr beta) |
| 65 | : _Msize(M), _Nsize(N), _Ksize(K), _beta(beta), _ci(ci) |
| 66 | { |
| 67 | /* For now don't do any blocking. TODO: figure out if we should. */ |
| 68 | k_block = K; |
| 69 | n_block = N; |
| 70 | } |
| 71 | |
| 72 | // Window is number of out_height blocks |
| 73 | unsigned int get_window_size() const override |
| 74 | { |
| 75 | return iceildiv(_Msize, strategy::out_height); |
| 76 | } |
| 77 | |
| 78 | // Actually execute the GEMM. |
| 79 | void execute(unsigned int start, unsigned int end, int) override |
| 80 | { |
| 81 | profiler prof; |
| 82 | strategy strat(_ci); |
| 83 | |
| 84 | unsigned int M_start = start * strategy::out_height; |
| 85 | unsigned int M_end = std::min(end * strategy::out_height, _Msize); |
| 86 | |
| 87 | static_assert(std::is_same<To, Toi>::value, "gemm_native: Operand types must be the same."); |
| 88 | static_assert(std::is_same<Tr, Tri>::value, "gemm_native: Result types must be the same."); |
| 89 | |
| 90 | for(unsigned int y0 = M_start; y0 < M_end; y0 += strategy::out_height) |
| 91 | { |
| 92 | unsigned int ymax = std::min(y0 + strategy::out_height, M_end); |
| 93 | |
| 94 | prof(PROFILE_KERNEL, (ymax - y0) * _Nsize * _Ksize, [&](void) |
| 95 | { |
| 96 | strat.kernel(this->_Aptr + (y0 * this->_lda), this->_lda, this->_Bptr, this->_ldb, this->_Cptr + (y0 * this->_ldc), this->_ldc, _beta, (ymax - y0), _Nsize, _Ksize); |
| 97 | }); |
| 98 | } |
| 99 | } |
| 100 | }; |
| 101 | |
| 102 | } // namespace arm_gemm |