blob: 579533418dfc8dfbdb4e08e3d8c6369311032d18 [file] [log] [blame]
Pablo Telloeb82fd22018-02-23 13:43:50 +00001/*
Georgios Pinitas7cd26d42019-01-09 18:35:17 +00002 * Copyright (c) 2017-2019 ARM Limited.
Pablo Telloeb82fd22018-02-23 13:43:50 +00003 *
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 Telloeb82fd22018-02-23 13:43:50 +000031#include "transform.hpp"
32
Michalis Spyroue7e96e02018-04-13 13:44:10 +010033#ifdef CYCLE_PROFILING
34#include "profiler.hpp"
35#endif
36
Anthony Barbier5f707732018-07-03 16:22:02 +010037namespace arm_gemm {
38
Pablo Telloeb82fd22018-02-23 13:43:50 +000039// 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 Barbier5f707732018-07-03 16:22:02 +010046template<typename strategy, typename To, typename Tr>
47class GemmNative : public GemmCommon<To, Tr> {
Pablo Telloeb82fd22018-02-23 13:43:50 +000048 typedef typename strategy::operand_type Toi;
Anthony Barbier5f707732018-07-03 16:22:02 +010049 typedef typename strategy::result_type Tri;
Pablo Telloeb82fd22018-02-23 13:43:50 +000050
51 const unsigned int _Msize;
52 const unsigned int _Nsize;
53 const unsigned int _Ksize;
54
Michalis Spyroue7e96e02018-04-13 13:44:10 +010055 const unsigned int _nbatches;
56 const unsigned int _nmultis;
57
Pablo Telloeb82fd22018-02-23 13:43:50 +000058 Tr _beta;
59
Anthony Barbier5f707732018-07-03 16:22:02 +010060 const CPUInfo * const _ci;
Pablo Telloeb82fd22018-02-23 13:43:50 +000061
Anthony Barbier5f707732018-07-03 16:22:02 +010062 unsigned int k_block=0;
63 unsigned int n_block=0;
Pablo Telloeb82fd22018-02-23 13:43:50 +000064
David Manselle39334c2018-07-06 17:53:35 +010065 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 Telloeb82fd22018-02-23 13:43:50 +000073public:
74 GemmNative(GemmNative &) = delete;
Anthony Barbier5f707732018-07-03 16:22:02 +010075 GemmNative & operator= (GemmNative &) = delete;
Pablo Telloeb82fd22018-02-23 13:43:50 +000076
Georgios Pinitas7cd26d42019-01-09 18:35:17 +000077 GemmNative(const GemmArgs<Tr> &args)
78 : _Msize(args._Msize), _Nsize(args._Nsize), _Ksize(args._Ksize), _nbatches(args._nbatches), _nmultis(args._nmulti), _beta(args._beta), _ci(args._ci) {
Pablo Telloeb82fd22018-02-23 13:43:50 +000079 /* For now don't do any blocking. TODO: figure out if we should. */
Georgios Pinitas7cd26d42019-01-09 18:35:17 +000080 k_block = _Ksize;
81 n_block = _Nsize;
Pablo Telloeb82fd22018-02-23 13:43:50 +000082 }
83
David Manselle39334c2018-07-06 17:53:35 +010084 // Window is amount per multi multiplied by total number of multis.
Anthony Barbier5f707732018-07-03 16:22:02 +010085 unsigned int get_window_size() const override {
David Manselle39334c2018-07-06 17:53:35 +010086 return window_per_multi() * _nmultis;
Pablo Telloeb82fd22018-02-23 13:43:50 +000087 }
88
89 // Actually execute the GEMM.
Anthony Barbier5f707732018-07-03 16:22:02 +010090 void execute(unsigned int start, unsigned int end, int) override {
Michalis Spyroue7e96e02018-04-13 13:44:10 +010091#ifdef CYCLE_PROFILING
Pablo Telloeb82fd22018-02-23 13:43:50 +000092 profiler prof;
Michalis Spyroue7e96e02018-04-13 13:44:10 +010093#endif
Anthony Barbier5f707732018-07-03 16:22:02 +010094 strategy strat(_ci);
95
Pablo Telloeb82fd22018-02-23 13:43:50 +000096 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 Manselle39334c2018-07-06 17:53:35 +010099 /* Compute starting point based on 'start' */
100 unsigned int multi = start / window_per_multi();
101 unsigned int multi_pos = start % window_per_multi();
Pablo Telloeb82fd22018-02-23 13:43:50 +0000102
David Manselle39334c2018-07-06 17:53:35 +0100103 unsigned int batch = multi_pos / window_per_batch();
104 unsigned int batch_pos = multi_pos % window_per_batch();
Michalis Spyroue7e96e02018-04-13 13:44:10 +0100105
David Manselle39334c2018-07-06 17:53:35 +0100106 unsigned int y0 = batch_pos * strategy::out_height();
107
Georgios Pinitas7cd26d42019-01-09 18:35:17 +0000108 for (unsigned int l=end-start; l>0; ) {
109 // Do work from here to the end of the current batch/multi
110 const unsigned int ymax = std::min(y0 + (l * strategy::out_height()), _Msize);
111
112 // Work out how many units this is and subtract from loop counter.
113 l -= ((ymax - y0) + (strategy::out_height() - 1)) / strategy::out_height();
114
Michalis Spyroue7e96e02018-04-13 13:44:10 +0100115#ifdef CYCLE_PROFILING
David Manselle39334c2018-07-06 17:53:35 +0100116 auto p = prof.ScopedProfiler(PROFILE_KERNEL, (ymax-y0) * _Nsize * _Ksize);
Michalis Spyroue7e96e02018-04-13 13:44:10 +0100117#endif
118
David Manselle39334c2018-07-06 17:53:35 +0100119 strat.kernel(this->_Aptr + (multi * this->_A_multi_stride) + (batch * this->_A_batch_stride) + (y0 * this->_lda), this->_lda,
120 this->_Bptr + (multi * this->_B_multi_stride), this->_ldb,
121 this->_Cptr + (multi * this->_C_multi_stride) + (batch * this->_C_batch_stride) + (y0 * this->_ldc), this->_ldc,
122 _beta, (ymax-y0), _Nsize, _Ksize);
123
124 /* Advance to next item */
Georgios Pinitas7cd26d42019-01-09 18:35:17 +0000125 y0 = ymax;
David Manselle39334c2018-07-06 17:53:35 +0100126
127 /* Check for batch/multi overflow */
128 if (y0 >= _Msize) {
129 y0=0;
130 batch++;
131 if (batch == _nbatches) {
132 batch=0;
133 multi++;
Michalis Spyroue7e96e02018-04-13 13:44:10 +0100134 }
135 }
Pablo Telloeb82fd22018-02-23 13:43:50 +0000136 }
137 }
138};
139
140} // namespace arm_gemm