blob: 5ebc6342d747a8511242224b8b16d8e3e9505032 [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 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.
Anthony Barbier5f707732018-07-03 16:22:02 +010046template<typename strategy, typename To, typename Tr>
47class GemvNativeTransposed : 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 _Nsize;
52 const unsigned int _Ksize;
Anthony Barbier5f707732018-07-03 16:22:02 +010053
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
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 unsigned int m_block=0;
61 unsigned int n_block=0;
Pablo Telloeb82fd22018-02-23 13:43:50 +000062
63public:
64 GemvNativeTransposed(GemvNativeTransposed &) = delete;
Anthony Barbier5f707732018-07-03 16:22:02 +010065 GemvNativeTransposed & operator= (GemvNativeTransposed &) = delete;
Pablo Telloeb82fd22018-02-23 13:43:50 +000066
Georgios Pinitas7cd26d42019-01-09 18:35:17 +000067 GemvNativeTransposed(const GemmArgs<Tr> &args)
68 : _Nsize(args._Nsize), _Ksize(args._Ksize), _nmultis(args._nmulti), _beta(args._beta), _ci(args._ci) {
Pablo Telloeb82fd22018-02-23 13:43:50 +000069 /* For now don't do any blocking. TODO: figure out if we should. */
Georgios Pinitas7cd26d42019-01-09 18:35:17 +000070 m_block = _Ksize;
71 n_block = _Nsize;
Pablo Telloeb82fd22018-02-23 13:43:50 +000072 }
73
Michalis Spyroue7e96e02018-04-13 13:44:10 +010074 // Window is number of out_width blocks times number of multis.
Anthony Barbier5f707732018-07-03 16:22:02 +010075 unsigned int get_window_size() const override {
Georgios Pinitas1d480652019-01-23 11:24:50 +000076 return iceildiv(_Nsize, strategy::out_width()) * _nmultis;
Pablo Telloeb82fd22018-02-23 13:43:50 +000077 }
78
79 // Actually execute the GEMV.
Anthony Barbier5f707732018-07-03 16:22:02 +010080 void execute(unsigned int start, unsigned int end, int) override {
Michalis Spyroue7e96e02018-04-13 13:44:10 +010081#ifdef CYCLE_PROFILING
Pablo Telloeb82fd22018-02-23 13:43:50 +000082 profiler prof;
Michalis Spyroue7e96e02018-04-13 13:44:10 +010083#endif
Pablo Telloeb82fd22018-02-23 13:43:50 +000084 strategy strat(_ci);
85
Georgios Pinitas1d480652019-01-23 11:24:50 +000086 const unsigned int window_per_multi = iceildiv(_Nsize, strategy::out_width());
Anthony Barbier5f707732018-07-03 16:22:02 +010087 const unsigned int multi_0 = start / window_per_multi;
88 const unsigned int multi_end = end / window_per_multi;
Michalis Spyroue7e96e02018-04-13 13:44:10 +010089
Georgios Pinitas1d480652019-01-23 11:24:50 +000090 const unsigned int n_0 = (start - (multi_0 * window_per_multi)) * strategy::out_width();
91 const unsigned int n_max = (end - (multi_end * window_per_multi)) * strategy::out_width();
Pablo Telloeb82fd22018-02-23 13:43:50 +000092
93 static_assert(std::is_same<To, Toi>::value, "gemv_transposed: Operand types must be the same.");
94 static_assert(std::is_same<Tr, Tri>::value, "gemv_transposed: Result types must be the same.");
95
Anthony Barbier5f707732018-07-03 16:22:02 +010096 for (unsigned int multi=multi_0; multi<=multi_end; multi++) {
97 const unsigned int n_start = (multi==multi_0) ? n_0 : 0;
98 const unsigned int n_end = (multi==multi_end) ? n_max : _Nsize;
Pablo Telloeb82fd22018-02-23 13:43:50 +000099
Anthony Barbier5f707732018-07-03 16:22:02 +0100100 if (n_end <= n_start)
Michalis Spyroue7e96e02018-04-13 13:44:10 +0100101 continue;
102
Anthony Barbier5f707732018-07-03 16:22:02 +0100103 for (unsigned int m0=0; m0<_Ksize; m0+=m_block) {
Michalis Spyroue7e96e02018-04-13 13:44:10 +0100104 unsigned int mmax = std::min(m0 + m_block, _Ksize);
Anthony Barbier5f707732018-07-03 16:22:02 +0100105
106 for (unsigned int n0=n_start; n0<n_end; n0+=n_block) {
Michalis Spyroue7e96e02018-04-13 13:44:10 +0100107 unsigned int nmax = std::min(n0 + n_block, n_end);
108#ifdef CYCLE_PROFILING
Anthony Barbier5f707732018-07-03 16:22:02 +0100109 auto p = prof.ScopedProfiler(PROFILE_KERNEL, (mmax-m0) * (nmax-n0));
Michalis Spyroue7e96e02018-04-13 13:44:10 +0100110#endif
111 strat.kernel(this->_Bptr + (multi * this->_B_multi_stride) + (m0 * this->_ldb) + n0,
112 this->_Aptr + (multi * this->_A_multi_stride) + m0,
113 this->_Cptr + (multi * this->_C_multi_stride) + n0,
Anthony Barbier5f707732018-07-03 16:22:02 +0100114 _beta, this->_ldb, (mmax-m0), (nmax-n0));
Michalis Spyroue7e96e02018-04-13 13:44:10 +0100115 }
Pablo Telloeb82fd22018-02-23 13:43:50 +0000116 }
117 }
118 }
119};
120
121} // namespace arm_gemm