blob: 8dae7c309825a36b5d312e8c0afee76be47e357d [file] [log] [blame]
Michalis Spyroue7e96e02018-04-13 13:44:10 +01001/*
Georgios Pinitas7cd26d42019-01-09 18:35:17 +00002 * Copyright (c) 2017-2019 ARM Limited.
Michalis Spyroue7e96e02018-04-13 13:44:10 +01003 *
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 "arm_gemm.hpp"
27
Anthony Barbier5f707732018-07-03 16:22:02 +010028namespace arm_gemm {
David Mansellce8f6052018-05-17 18:51:26 +010029
30/* "Batched GEMV" (where M=1 and nbatches>1) can be executed much more
31 * efficiently as a GEMM (with M'=nbatches and nbatches'=1). This wrapper
32 * implements this. */
Anthony Barbier5f707732018-07-03 16:22:02 +010033template<typename To, typename Tr>
34class GemvBatched : public GemmCommon<To, Tr> {
Michalis Spyroue7e96e02018-04-13 13:44:10 +010035private:
36 UniqueGemmCommon<To, Tr> _subgemm = nullptr;
37
38public:
David Manselle39334c2018-07-06 17:53:35 +010039 GemvBatched(const GemmArgs<Tr> &args) {
Michalis Spyroue7e96e02018-04-13 13:44:10 +010040 /* Just create a subgemm with batches->M */
David Manselle39334c2018-07-06 17:53:35 +010041 GemmArgs<Tr> newargs = args;
42 newargs._Msize = args._nbatches;
43 newargs._nbatches = 1;
Georgios Pinitascfa2bba2019-06-27 17:00:52 +010044 newargs._cfg = nullptr;
Georgios Pinitas7cd26d42019-01-09 18:35:17 +000045 _subgemm = gemm<To,Tr>(newargs);
Michalis Spyroue7e96e02018-04-13 13:44:10 +010046 }
47
48 void set_arrays(const To *A, const int lda, const int A_batch_stride, const int A_multi_stride,
49 const To *B, const int ldb, const int B_multi_stride,
Georgios Pinitascfa2bba2019-06-27 17:00:52 +010050 Tr *C, const int ldc, const int C_batch_stride, const int C_multi_stride) override {
Michalis Spyroue7e96e02018-04-13 13:44:10 +010051 /* A and C's batch stride becomes their new row stride. New batch stride is 0 as nbatches for subgemm is always 1. */
52 _subgemm->set_arrays(A, A_batch_stride, 0, A_multi_stride,
53 B, ldb, B_multi_stride,
54 C, C_batch_stride, 0, C_multi_stride);
55 }
56
Anthony Barbier5f707732018-07-03 16:22:02 +010057 unsigned int get_window_size() const override {
Michalis Spyroue7e96e02018-04-13 13:44:10 +010058 return _subgemm->get_window_size();
59 }
60
Anthony Barbier5f707732018-07-03 16:22:02 +010061 void set_nthreads(int nthreads) override {
Michalis Spyroue7e96e02018-04-13 13:44:10 +010062 _subgemm->set_nthreads(nthreads);
63 }
64
Anthony Barbier5f707732018-07-03 16:22:02 +010065 void execute(unsigned int start, unsigned int end, int threadid) override {
Michalis Spyroue7e96e02018-04-13 13:44:10 +010066 _subgemm->execute(start, end, threadid);
67 }
68
Anthony Barbier5f707732018-07-03 16:22:02 +010069 size_t get_working_size() const override {
Michalis Spyroue7e96e02018-04-13 13:44:10 +010070 return _subgemm->get_working_size();
71 }
72
Anthony Barbier5f707732018-07-03 16:22:02 +010073 void set_working_space(void *space) override {
Michalis Spyroue7e96e02018-04-13 13:44:10 +010074 _subgemm->set_working_space(space);
75 }
76
Anthony Barbier5f707732018-07-03 16:22:02 +010077 bool B_is_pretransposed() const override {
Michalis Spyroue7e96e02018-04-13 13:44:10 +010078 return _subgemm->B_is_pretransposed();
79 }
80
Anthony Barbier5f707732018-07-03 16:22:02 +010081 bool B_pretranspose_required() const override {
Michalis Spyroue7e96e02018-04-13 13:44:10 +010082 return _subgemm->B_pretranspose_required();
83 }
84
Anthony Barbier5f707732018-07-03 16:22:02 +010085 size_t get_B_pretransposed_array_size() const override {
Michalis Spyroue7e96e02018-04-13 13:44:10 +010086 return _subgemm->get_B_pretransposed_array_size();
87 }
88
Anthony Barbier5f707732018-07-03 16:22:02 +010089 void pretranspose_B_array(void *buffer, const To *B, const int ldb, const int B_multi_stride) override {
Michalis Spyroue7e96e02018-04-13 13:44:10 +010090 _subgemm->pretranspose_B_array(buffer, B, ldb, B_multi_stride);
91 }
92
Anthony Barbier5f707732018-07-03 16:22:02 +010093 void set_pretransposed_B_data(void *buffer) override {
Michalis Spyroue7e96e02018-04-13 13:44:10 +010094 _subgemm->set_pretransposed_B_data(buffer);
95 }
96};
97
98} // namespace arm_gemm