blob: e53ddb26c1e66281b0e6c73fa36065028ac169ae [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//
Michalis Spyroue7e96e02018-04-13 13:44:10 +010041// This is implementation is for GEMV with pretransposition.
Anthony Barbier5f707732018-07-03 16:22:02 +010042//
Michalis Spyroue7e96e02018-04-13 13:44:10 +010043// batches are not supported as a batched GEMV makes no sense (can be converted to a GEMM).
Anthony Barbier5f707732018-07-03 16:22:02 +010044template<typename strategy, typename To, typename Tr>
45class GemvPretransposed : public GemmCommon<To, Tr> {
Pablo Telloeb82fd22018-02-23 13:43:50 +000046 typedef typename strategy::operand_type Toi;
Anthony Barbier5f707732018-07-03 16:22:02 +010047 typedef typename strategy::result_type Tri;
Pablo Telloeb82fd22018-02-23 13:43:50 +000048
49 const unsigned int _Nsize;
50 const unsigned int _Ksize;
Anthony Barbier5f707732018-07-03 16:22:02 +010051
Michalis Spyroue7e96e02018-04-13 13:44:10 +010052 const unsigned int _nmultis;
Pablo Telloeb82fd22018-02-23 13:43:50 +000053
54 const bool _trB;
55
56 const Tr _beta;
57
Anthony Barbier5f707732018-07-03 16:22:02 +010058 const CPUInfo * const _ci;
Pablo Telloeb82fd22018-02-23 13:43:50 +000059
Anthony Barbier5f707732018-07-03 16:22:02 +010060 const unsigned int _buffer_per_multi;
61
62 unsigned int m_block=0;
63 unsigned int n_block=0;
Pablo Telloeb82fd22018-02-23 13:43:50 +000064
65 const Toi *_A_pretransposed = nullptr;
66
67public:
68 GemvPretransposed(GemvPretransposed &) = delete;
Anthony Barbier5f707732018-07-03 16:22:02 +010069 GemvPretransposed & operator= (GemvPretransposed &) = delete;
Pablo Telloeb82fd22018-02-23 13:43:50 +000070
Anthony Barbier5f707732018-07-03 16:22:02 +010071 GemvPretransposed(const CPUInfo *ci, const unsigned int N, const unsigned int K, const unsigned int nmultis, const bool trB, const Tr beta) :
72 _Nsize(N), _Ksize(K), _nmultis(nmultis), _trB(trB), _beta(beta), _ci(ci),
73 _buffer_per_multi(_Ksize * iceildiv(_Nsize, strategy::A_interleave) * strategy::A_interleave) {
Pablo Telloeb82fd22018-02-23 13:43:50 +000074 /* For now don't do any blocking. TODO: figure out if we should. */
75 m_block = K;
76 n_block = N;
77 }
78
Michalis Spyroue7e96e02018-04-13 13:44:10 +010079 // Window is number of out_width blocks, times number of multis.
Anthony Barbier5f707732018-07-03 16:22:02 +010080 unsigned int get_window_size() const override {
Michalis Spyroue7e96e02018-04-13 13:44:10 +010081 return iceildiv(_Nsize, strategy::out_width) * _nmultis;
Pablo Telloeb82fd22018-02-23 13:43:50 +000082 }
83
84 // Actually execute the GEMV.
Anthony Barbier5f707732018-07-03 16:22:02 +010085 void execute(unsigned int start, unsigned int end, int) override {
Michalis Spyroue7e96e02018-04-13 13:44:10 +010086#ifdef CYCLE_PROFILING
Pablo Telloeb82fd22018-02-23 13:43:50 +000087 profiler prof;
Michalis Spyroue7e96e02018-04-13 13:44:10 +010088#endif
Pablo Telloeb82fd22018-02-23 13:43:50 +000089 strategy strat(_ci);
90
Michalis Spyroue7e96e02018-04-13 13:44:10 +010091 /* Break the window values down into multis of interest... */
92 const unsigned int window_per_multi = iceildiv(_Nsize, strategy::out_width);
Anthony Barbier5f707732018-07-03 16:22:02 +010093 const unsigned int multi_0 = start / window_per_multi;
94 const unsigned int multi_end = end / window_per_multi;
Michalis Spyroue7e96e02018-04-13 13:44:10 +010095
96 /* ... and figure out where we start and end in the first and last multi. */
97 const unsigned int n_0 = (start - (multi_0 * window_per_multi)) * strategy::out_width;
98 const unsigned int n_max = (end - (multi_end * window_per_multi)) * strategy::out_width;
Pablo Telloeb82fd22018-02-23 13:43:50 +000099
100 static_assert(std::is_same<Tr, Tri>::value, "GemvPretransposed: Result types must be the same.");
101
Anthony Barbier5f707732018-07-03 16:22:02 +0100102 for (unsigned int multi=multi_0; multi<=multi_end; multi++) {
103 const unsigned int n_start = (multi==multi_0) ? n_0 : 0;
104 const unsigned int n_end = (multi==multi_end) ? n_max : _Nsize;
Pablo Telloeb82fd22018-02-23 13:43:50 +0000105
Anthony Barbier5f707732018-07-03 16:22:02 +0100106 if (n_end <= n_start)
Michalis Spyroue7e96e02018-04-13 13:44:10 +0100107 continue;
108
Anthony Barbier5f707732018-07-03 16:22:02 +0100109 for (unsigned int m0=0; m0<_Ksize; m0+=m_block) {
Michalis Spyroue7e96e02018-04-13 13:44:10 +0100110 unsigned int mmax = std::min(m0 + m_block, _Ksize);
Anthony Barbier5f707732018-07-03 16:22:02 +0100111
112 for (unsigned int n=n_start; n<n_end; n+=n_block) {
Michalis Spyroue7e96e02018-04-13 13:44:10 +0100113 unsigned int nmax = std::min(n + n_block, n_end);
114#ifdef CYCLE_PROFILING
Anthony Barbier5f707732018-07-03 16:22:02 +0100115 auto p = prof.ScopedProfiler(PROFILE_KERNEL, (mmax-m0) * (nmax-n));
Michalis Spyroue7e96e02018-04-13 13:44:10 +0100116#endif
Pablo Telloeb82fd22018-02-23 13:43:50 +0000117 /* This assumes that the underlying call was a GEMM with M=1; for the N=1 case we would have to pick up this->_Bptr below instead */
Michalis Spyroue7e96e02018-04-13 13:44:10 +0100118 strat.kernel(_A_pretransposed + (multi * _buffer_per_multi) + (n * _Ksize) + (m0 * strategy::A_interleave),
119 (_Ksize * strategy::A_interleave),
120 this->_Aptr + (multi * this->_A_multi_stride) + m0,
121 this->_Cptr + (multi * this->_C_multi_stride) + n,
Anthony Barbier5f707732018-07-03 16:22:02 +0100122 _beta, (mmax-m0), (nmax-n));
Michalis Spyroue7e96e02018-04-13 13:44:10 +0100123 }
Pablo Telloeb82fd22018-02-23 13:43:50 +0000124 }
125 }
126 }
127
128 /* Pretransposed interface implementation */
Anthony Barbier5f707732018-07-03 16:22:02 +0100129 bool B_is_pretransposed() const override {
Pablo Telloeb82fd22018-02-23 13:43:50 +0000130 return true;
131 }
132
Anthony Barbier5f707732018-07-03 16:22:02 +0100133 bool B_pretranspose_required() const override {
Pablo Telloeb82fd22018-02-23 13:43:50 +0000134 /* Transpose is required if _A_pretransposed is still nullptr */
135 return (_A_pretransposed == nullptr);
136 }
137
Anthony Barbier5f707732018-07-03 16:22:02 +0100138 size_t get_B_pretransposed_array_size() const override {
Michalis Spyroue7e96e02018-04-13 13:44:10 +0100139 return _buffer_per_multi * _nmultis * sizeof(To);
Pablo Telloeb82fd22018-02-23 13:43:50 +0000140 }
141
Anthony Barbier5f707732018-07-03 16:22:02 +0100142 void pretranspose_B_array(void *buffer, const To *B, const int ldb, const int B_multi_stride) override {
Pablo Telloeb82fd22018-02-23 13:43:50 +0000143 Toi *A_buffer = reinterpret_cast<Toi *>(buffer);
144
Anthony Barbier5f707732018-07-03 16:22:02 +0100145 for (unsigned int multi=0; multi<_nmultis; multi++) {
Michalis Spyroue7e96e02018-04-13 13:44:10 +0100146 /* Reverse sense here as we are dealing with B rather than A. So if
147 * strategy::A_transpose is false and _trB is false, we still
148 * transpose. */
Anthony Barbier5f707732018-07-03 16:22:02 +0100149 if (_trB ^ strategy::A_transpose) {
Michalis Spyroue7e96e02018-04-13 13:44:10 +0100150 Transform<strategy::A_interleave, strategy::A_block, false>(A_buffer + (multi * _buffer_per_multi), B + (multi * B_multi_stride), ldb, 0, _Nsize, 0, _Ksize);
Anthony Barbier5f707732018-07-03 16:22:02 +0100151 } else {
Michalis Spyroue7e96e02018-04-13 13:44:10 +0100152 Transform<strategy::A_interleave, strategy::A_block, true>(A_buffer + (multi * _buffer_per_multi), B + (multi * B_multi_stride), ldb, 0, _Nsize, 0, _Ksize);
153 }
Pablo Telloeb82fd22018-02-23 13:43:50 +0000154 }
155
156 _A_pretransposed = A_buffer;
157 }
Michalis Spyroue7e96e02018-04-13 13:44:10 +0100158
Anthony Barbier5f707732018-07-03 16:22:02 +0100159 void set_pretransposed_B_data(void *buffer) override {
Michalis Spyroue7e96e02018-04-13 13:44:10 +0100160 _A_pretransposed = reinterpret_cast<Toi *>(buffer);
161 }
Pablo Telloeb82fd22018-02-23 13:43:50 +0000162};
163
164} // namespace arm_gemm