blob: e5cc79eaedd7b8a3002aebd727f10d68e541cc83 [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 a "native" (no-transform) GEMV with a
42// transposed matrix.
43//
44// As a native operation the source data is used in-place, so the internal
45// and external operand/result types must match.
46template <typename strategy, typename To, typename Tr>
47class GemvNativeTransposed : public GemmCommon<To, Tr>
48{
49 typedef typename strategy::operand_type Toi;
50 typedef typename strategy::result_type Tri;
51
52 const unsigned int _Nsize;
53 const unsigned int _Ksize;
Michalis Spyroue7e96e02018-04-13 13:44:10 +010054 const unsigned int _nmultis;
Pablo Telloeb82fd22018-02-23 13:43:50 +000055
Pablo Tello99ef8402018-03-20 16:46:55 +000056 const Tr _beta;
Pablo Telloeb82fd22018-02-23 13:43:50 +000057
58 const CPUInfo *const _ci;
59
60 unsigned int m_block = 0;
61 unsigned int n_block = 0;
62
63public:
64 GemvNativeTransposed(GemvNativeTransposed &) = delete;
65 GemvNativeTransposed &operator=(GemvNativeTransposed &) = delete;
66
Michalis Spyroue7e96e02018-04-13 13:44:10 +010067 GemvNativeTransposed(const CPUInfo *ci, const unsigned int N, const unsigned int K, const unsigned int nmultis, const Tr beta)
68 : _Nsize(N), _Ksize(K), _nmultis(nmultis), _beta(beta), _ci(ci)
Pablo Telloeb82fd22018-02-23 13:43:50 +000069 {
70 /* For now don't do any blocking. TODO: figure out if we should. */
71 m_block = K;
72 n_block = N;
73 }
74
Michalis Spyroue7e96e02018-04-13 13:44:10 +010075 // Window is number of out_width blocks times number of multis.
Pablo Telloeb82fd22018-02-23 13:43:50 +000076 unsigned int get_window_size() const override
77 {
Michalis Spyroue7e96e02018-04-13 13:44:10 +010078 return iceildiv(_Nsize, strategy::out_width) * _nmultis;
Pablo Telloeb82fd22018-02-23 13:43:50 +000079 }
80
81 // Actually execute the GEMV.
82 void execute(unsigned int start, unsigned int end, int) override
83 {
Michalis Spyroue7e96e02018-04-13 13:44:10 +010084#ifdef CYCLE_PROFILING
Pablo Telloeb82fd22018-02-23 13:43:50 +000085 profiler prof;
Michalis Spyroue7e96e02018-04-13 13:44:10 +010086#endif
87
Pablo Telloeb82fd22018-02-23 13:43:50 +000088 strategy strat(_ci);
89
Michalis Spyroue7e96e02018-04-13 13:44:10 +010090 const unsigned int window_per_multi = iceildiv(_Nsize, strategy::out_width);
91 const unsigned int multi_0 = start / window_per_multi;
92 const unsigned int multi_end = end / window_per_multi;
93
94 const unsigned int n_0 = (start - (multi_0 * window_per_multi)) * strategy::out_width;
95 const unsigned int n_max = (end - (multi_end * window_per_multi)) * strategy::out_width;
Pablo Telloeb82fd22018-02-23 13:43:50 +000096
97 static_assert(std::is_same<To, Toi>::value, "gemv_transposed: Operand types must be the same.");
98 static_assert(std::is_same<Tr, Tri>::value, "gemv_transposed: Result types must be the same.");
99
Michalis Spyroue7e96e02018-04-13 13:44:10 +0100100 for(unsigned int multi = multi_0; multi <= multi_end; multi++)
Pablo Telloeb82fd22018-02-23 13:43:50 +0000101 {
Michalis Spyroue7e96e02018-04-13 13:44:10 +0100102 const unsigned int n_start = (multi == multi_0) ? n_0 : 0;
103 const unsigned int n_end = (multi == multi_end) ? n_max : _Nsize;
Pablo Telloeb82fd22018-02-23 13:43:50 +0000104
Michalis Spyroue7e96e02018-04-13 13:44:10 +0100105 if(n_end <= n_start)
106 continue;
107
108 for(unsigned int m0 = 0; m0 < _Ksize; m0 += m_block)
Pablo Telloeb82fd22018-02-23 13:43:50 +0000109 {
Michalis Spyroue7e96e02018-04-13 13:44:10 +0100110 unsigned int mmax = std::min(m0 + m_block, _Ksize);
111 for(unsigned int n0 = n_start; n0 < n_end; n0 += n_block)
Pablo Telloeb82fd22018-02-23 13:43:50 +0000112 {
Michalis Spyroue7e96e02018-04-13 13:44:10 +0100113 unsigned int nmax = std::min(n0 + n_block, n_end);
114#ifdef CYCLE_PROFILING
115 auto p = prof.ScopedProfiler(PROFILE_KERNEL, (mmax - m0) * (nmax - n0));
116#endif
117 strat.kernel(this->_Bptr + (multi * this->_B_multi_stride) + (m0 * this->_ldb) + n0,
118 this->_Aptr + (multi * this->_A_multi_stride) + m0,
119 this->_Cptr + (multi * this->_C_multi_stride) + n0,
Pablo Tello99ef8402018-03-20 16:46:55 +0000120 _beta, this->_ldb, (mmax - m0), (nmax - n0));
Michalis Spyroue7e96e02018-04-13 13:44:10 +0100121 }
Pablo Telloeb82fd22018-02-23 13:43:50 +0000122 }
123 }
124 }
125};
126
127} // namespace arm_gemm