blob: 695236bdc4dbbba758ce2369d91cc07349ec17b5 [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
Pablo Telloeb82fd22018-02-23 13:43:50 +000037namespace arm_gemm
38{
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
46template <typename strategy, typename To, typename Tr>
47class GemmNative : public GemmCommon<To, Tr>
48{
49 typedef typename strategy::operand_type Toi;
50 typedef typename strategy::result_type Tri;
51
52 const unsigned int _Msize;
53 const unsigned int _Nsize;
54 const unsigned int _Ksize;
55
Michalis Spyroue7e96e02018-04-13 13:44:10 +010056 const unsigned int _nbatches;
57 const unsigned int _nmultis;
58
Pablo Telloeb82fd22018-02-23 13:43:50 +000059 Tr _beta;
60
61 const CPUInfo *const _ci;
62
63 unsigned int k_block = 0;
64 unsigned int n_block = 0;
65
66public:
67 GemmNative(GemmNative &) = delete;
68 GemmNative &operator=(GemmNative &) = delete;
69
Michalis Spyroue7e96e02018-04-13 13:44:10 +010070 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)
71 : _Msize(M), _Nsize(N), _Ksize(K), _nbatches(nbatches), _nmultis(nmultis), _beta(beta), _ci(ci)
Pablo Telloeb82fd22018-02-23 13:43:50 +000072 {
73 /* For now don't do any blocking. TODO: figure out if we should. */
74 k_block = K;
75 n_block = N;
76 }
77
78 // Window is number of out_height blocks
79 unsigned int get_window_size() const override
80 {
Michalis Spyroue7e96e02018-04-13 13:44:10 +010081 return iceildiv(_Msize, strategy::out_height) * _nbatches * _nmultis;
Pablo Telloeb82fd22018-02-23 13:43:50 +000082 }
83
84 // Actually execute the GEMM.
85 void execute(unsigned int start, unsigned int end, int) override
86 {
Michalis Spyroue7e96e02018-04-13 13:44:10 +010087#ifdef CYCLE_PROFILING
Pablo Telloeb82fd22018-02-23 13:43:50 +000088 profiler prof;
Michalis Spyroue7e96e02018-04-13 13:44:10 +010089#endif
Pablo Tello6c421272018-05-03 10:42:35 +010090 strategy strat(_ci);
Michalis Spyroue7e96e02018-04-13 13:44:10 +010091 const unsigned int window_per_batch = iceildiv(_Msize, strategy::out_height);
92 const unsigned int window_per_multi = window_per_batch * _nbatches;
93
94 const unsigned int first_multi = start / window_per_multi;
95 const unsigned int last_multi = end / window_per_multi;
96
97 const unsigned int first_batch = (start - (first_multi * window_per_multi)) / window_per_batch;
98 const unsigned int last_batch = (end - (last_multi * window_per_multi)) / window_per_batch;
99
100 const unsigned int first_row = ((start - (first_multi * window_per_multi)) % window_per_batch) * strategy::out_height;
101 const unsigned int last_row = ((end - (last_multi * window_per_multi)) % window_per_batch) * strategy::out_height;
Pablo Telloeb82fd22018-02-23 13:43:50 +0000102
103 static_assert(std::is_same<To, Toi>::value, "gemm_native: Operand types must be the same.");
104 static_assert(std::is_same<Tr, Tri>::value, "gemm_native: Result types must be the same.");
105
Michalis Spyroue7e96e02018-04-13 13:44:10 +0100106 for(unsigned int multi = first_multi; multi <= last_multi; multi++)
Pablo Telloeb82fd22018-02-23 13:43:50 +0000107 {
Michalis Spyroue7e96e02018-04-13 13:44:10 +0100108 const unsigned int batch_0 = (multi == first_multi) ? first_batch : 0;
Anthony Barbier71ac9c02018-05-09 18:23:57 +0100109 const unsigned int batch_max = (multi == last_multi) ? last_batch : _nbatches - 1;
Pablo Telloeb82fd22018-02-23 13:43:50 +0000110
Pablo Tello6c421272018-05-03 10:42:35 +0100111 for(unsigned int batch = batch_0; batch <= batch_max; batch++)
Pablo Telloeb82fd22018-02-23 13:43:50 +0000112 {
Michalis Spyroue7e96e02018-04-13 13:44:10 +0100113 const unsigned int m_start = ((multi == first_multi) && (batch == first_batch)) ? first_row : 0;
114 const unsigned int m_end = ((multi == last_multi) && (batch == last_batch)) ? last_row : _Msize;
115
116 for(unsigned int y0 = m_start; y0 < m_end; y0 += strategy::out_height)
117 {
118 const unsigned int ymax = std::min(y0 + strategy::out_height, m_end);
119#ifdef CYCLE_PROFILING
120 auto p = prof.ScopedProfiler(PROFILE_KERNEL, (ymax - y0) * _Nsize * _Ksize);
121#endif
122
123 strat.kernel(this->_Aptr + (multi * this->_A_multi_stride) + (batch * this->_A_batch_stride) + (y0 * this->_lda), this->_lda,
124 this->_Bptr + (multi * this->_B_multi_stride), this->_ldb,
125 this->_Cptr + (multi * this->_C_multi_stride) + (batch * this->_C_batch_stride) + (y0 * this->_ldc), this->_ldc,
126 _beta, (ymax - y0), _Nsize, _Ksize);
127 }
128 }
Pablo Telloeb82fd22018-02-23 13:43:50 +0000129 }
130 }
131};
132
133} // namespace arm_gemm