blob: baa1316745821a8f110ccfc9b559a5db9b91581b [file] [log] [blame]
Pablo Telloeb82fd22018-02-23 13:43:50 +00001/*
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 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
Anthony Barbier5f707732018-07-03 16:22:02 +010077 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 Telloeb82fd22018-02-23 13:43:50 +000079 /* For now don't do any blocking. TODO: figure out if we should. */
80 k_block = K;
81 n_block = N;
82 }
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
108 for (unsigned int pos=start; pos<end; pos++) {
109 const unsigned int ymax = std::min(y0 + strategy::out_height(), _Msize);
Michalis Spyroue7e96e02018-04-13 13:44:10 +0100110#ifdef CYCLE_PROFILING
David Manselle39334c2018-07-06 17:53:35 +0100111 auto p = prof.ScopedProfiler(PROFILE_KERNEL, (ymax-y0) * _Nsize * _Ksize);
Michalis Spyroue7e96e02018-04-13 13:44:10 +0100112#endif
113
David Manselle39334c2018-07-06 17:53:35 +0100114 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 Spyroue7e96e02018-04-13 13:44:10 +0100129 }
130 }
Pablo Telloeb82fd22018-02-23 13:43:50 +0000131 }
132 }
133};
134
135} // namespace arm_gemm