blob: 241c5fea2717a72c5278b27f3a36de3348c6e9db [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 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
Anthony Barbier5f707732018-07-03 16:22:02 +010067 GemvNativeTransposed(const CPUInfo *ci, const unsigned int N, const unsigned int K, const unsigned int nmultis, const Tr beta) : _Nsize(N), _Ksize(K), _nmultis(nmultis), _beta(beta), _ci(ci) {
Pablo Telloeb82fd22018-02-23 13:43:50 +000068 /* For now don't do any blocking. TODO: figure out if we should. */
69 m_block = K;
70 n_block = N;
71 }
72
Michalis Spyroue7e96e02018-04-13 13:44:10 +010073 // Window is number of out_width blocks times number of multis.
Anthony Barbier5f707732018-07-03 16:22:02 +010074 unsigned int get_window_size() const override {
Michalis Spyroue7e96e02018-04-13 13:44:10 +010075 return iceildiv(_Nsize, strategy::out_width) * _nmultis;
Pablo Telloeb82fd22018-02-23 13:43:50 +000076 }
77
78 // Actually execute the GEMV.
Anthony Barbier5f707732018-07-03 16:22:02 +010079 void execute(unsigned int start, unsigned int end, int) override {
Michalis Spyroue7e96e02018-04-13 13:44:10 +010080#ifdef CYCLE_PROFILING
Pablo Telloeb82fd22018-02-23 13:43:50 +000081 profiler prof;
Michalis Spyroue7e96e02018-04-13 13:44:10 +010082#endif
Pablo Telloeb82fd22018-02-23 13:43:50 +000083 strategy strat(_ci);
84
Michalis Spyroue7e96e02018-04-13 13:44:10 +010085 const unsigned int window_per_multi = iceildiv(_Nsize, strategy::out_width);
Anthony Barbier5f707732018-07-03 16:22:02 +010086 const unsigned int multi_0 = start / window_per_multi;
87 const unsigned int multi_end = end / window_per_multi;
Michalis Spyroue7e96e02018-04-13 13:44:10 +010088
89 const unsigned int n_0 = (start - (multi_0 * window_per_multi)) * strategy::out_width;
90 const unsigned int n_max = (end - (multi_end * window_per_multi)) * strategy::out_width;
Pablo Telloeb82fd22018-02-23 13:43:50 +000091
92 static_assert(std::is_same<To, Toi>::value, "gemv_transposed: Operand types must be the same.");
93 static_assert(std::is_same<Tr, Tri>::value, "gemv_transposed: Result types must be the same.");
94
Anthony Barbier5f707732018-07-03 16:22:02 +010095 for (unsigned int multi=multi_0; multi<=multi_end; multi++) {
96 const unsigned int n_start = (multi==multi_0) ? n_0 : 0;
97 const unsigned int n_end = (multi==multi_end) ? n_max : _Nsize;
Pablo Telloeb82fd22018-02-23 13:43:50 +000098
Anthony Barbier5f707732018-07-03 16:22:02 +010099 if (n_end <= n_start)
Michalis Spyroue7e96e02018-04-13 13:44:10 +0100100 continue;
101
Anthony Barbier5f707732018-07-03 16:22:02 +0100102 for (unsigned int m0=0; m0<_Ksize; m0+=m_block) {
Michalis Spyroue7e96e02018-04-13 13:44:10 +0100103 unsigned int mmax = std::min(m0 + m_block, _Ksize);
Anthony Barbier5f707732018-07-03 16:22:02 +0100104
105 for (unsigned int n0=n_start; n0<n_end; n0+=n_block) {
Michalis Spyroue7e96e02018-04-13 13:44:10 +0100106 unsigned int nmax = std::min(n0 + n_block, n_end);
107#ifdef CYCLE_PROFILING
Anthony Barbier5f707732018-07-03 16:22:02 +0100108 auto p = prof.ScopedProfiler(PROFILE_KERNEL, (mmax-m0) * (nmax-n0));
Michalis Spyroue7e96e02018-04-13 13:44:10 +0100109#endif
110 strat.kernel(this->_Bptr + (multi * this->_B_multi_stride) + (m0 * this->_ldb) + n0,
111 this->_Aptr + (multi * this->_A_multi_stride) + m0,
112 this->_Cptr + (multi * this->_C_multi_stride) + n0,
Anthony Barbier5f707732018-07-03 16:22:02 +0100113 _beta, this->_ldb, (mmax-m0), (nmax-n0));
Michalis Spyroue7e96e02018-04-13 13:44:10 +0100114 }
Pablo Telloeb82fd22018-02-23 13:43:50 +0000115 }
116 }
117 }
118};
119
120} // namespace arm_gemm