Michalis Spyrou | e7e96e0 | 2018-04-13 13:44:10 +0100 | [diff] [blame] | 1 | /* |
Georgios Pinitas | 7cd26d4 | 2019-01-09 18:35:17 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2019 ARM Limited. |
Michalis Spyrou | e7e96e0 | 2018-04-13 13:44:10 +0100 | [diff] [blame] | 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 "arm_gemm.hpp" |
| 27 | |
Anthony Barbier | 5f70773 | 2018-07-03 16:22:02 +0100 | [diff] [blame] | 28 | namespace arm_gemm { |
David Mansell | ce8f605 | 2018-05-17 18:51:26 +0100 | [diff] [blame] | 29 | |
| 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 Barbier | 5f70773 | 2018-07-03 16:22:02 +0100 | [diff] [blame] | 33 | template<typename To, typename Tr> |
| 34 | class GemvBatched : public GemmCommon<To, Tr> { |
Michalis Spyrou | e7e96e0 | 2018-04-13 13:44:10 +0100 | [diff] [blame] | 35 | private: |
| 36 | UniqueGemmCommon<To, Tr> _subgemm = nullptr; |
| 37 | |
| 38 | public: |
David Mansell | e39334c | 2018-07-06 17:53:35 +0100 | [diff] [blame] | 39 | GemvBatched(const GemmArgs<Tr> &args) { |
Michalis Spyrou | e7e96e0 | 2018-04-13 13:44:10 +0100 | [diff] [blame] | 40 | /* Just create a subgemm with batches->M */ |
David Mansell | e39334c | 2018-07-06 17:53:35 +0100 | [diff] [blame] | 41 | GemmArgs<Tr> newargs = args; |
| 42 | newargs._Msize = args._nbatches; |
| 43 | newargs._nbatches = 1; |
Georgios Pinitas | 7cd26d4 | 2019-01-09 18:35:17 +0000 | [diff] [blame] | 44 | _subgemm = gemm<To,Tr>(newargs); |
Michalis Spyrou | e7e96e0 | 2018-04-13 13:44:10 +0100 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | void set_arrays(const To *A, const int lda, const int A_batch_stride, const int A_multi_stride, |
| 48 | const To *B, const int ldb, const int B_multi_stride, |
Georgios Pinitas | 1461383 | 2019-03-01 19:07:11 +0000 | [diff] [blame] | 49 | Tr *C, const int ldc, const int C_batch_stride, const int C_multi_stride) override { |
Michalis Spyrou | e7e96e0 | 2018-04-13 13:44:10 +0100 | [diff] [blame] | 50 | /* A and C's batch stride becomes their new row stride. New batch stride is 0 as nbatches for subgemm is always 1. */ |
| 51 | _subgemm->set_arrays(A, A_batch_stride, 0, A_multi_stride, |
| 52 | B, ldb, B_multi_stride, |
| 53 | C, C_batch_stride, 0, C_multi_stride); |
| 54 | } |
| 55 | |
Anthony Barbier | 5f70773 | 2018-07-03 16:22:02 +0100 | [diff] [blame] | 56 | unsigned int get_window_size() const override { |
Michalis Spyrou | e7e96e0 | 2018-04-13 13:44:10 +0100 | [diff] [blame] | 57 | return _subgemm->get_window_size(); |
| 58 | } |
| 59 | |
Anthony Barbier | 5f70773 | 2018-07-03 16:22:02 +0100 | [diff] [blame] | 60 | void set_nthreads(int nthreads) override { |
Michalis Spyrou | e7e96e0 | 2018-04-13 13:44:10 +0100 | [diff] [blame] | 61 | _subgemm->set_nthreads(nthreads); |
| 62 | } |
| 63 | |
Anthony Barbier | 5f70773 | 2018-07-03 16:22:02 +0100 | [diff] [blame] | 64 | void execute(unsigned int start, unsigned int end, int threadid) override { |
Michalis Spyrou | e7e96e0 | 2018-04-13 13:44:10 +0100 | [diff] [blame] | 65 | _subgemm->execute(start, end, threadid); |
| 66 | } |
| 67 | |
Anthony Barbier | 5f70773 | 2018-07-03 16:22:02 +0100 | [diff] [blame] | 68 | size_t get_working_size() const override { |
Michalis Spyrou | e7e96e0 | 2018-04-13 13:44:10 +0100 | [diff] [blame] | 69 | return _subgemm->get_working_size(); |
| 70 | } |
| 71 | |
Anthony Barbier | 5f70773 | 2018-07-03 16:22:02 +0100 | [diff] [blame] | 72 | void set_working_space(void *space) override { |
Michalis Spyrou | e7e96e0 | 2018-04-13 13:44:10 +0100 | [diff] [blame] | 73 | _subgemm->set_working_space(space); |
| 74 | } |
| 75 | |
Anthony Barbier | 5f70773 | 2018-07-03 16:22:02 +0100 | [diff] [blame] | 76 | bool B_is_pretransposed() const override { |
Michalis Spyrou | e7e96e0 | 2018-04-13 13:44:10 +0100 | [diff] [blame] | 77 | return _subgemm->B_is_pretransposed(); |
| 78 | } |
| 79 | |
Anthony Barbier | 5f70773 | 2018-07-03 16:22:02 +0100 | [diff] [blame] | 80 | bool B_pretranspose_required() const override { |
Michalis Spyrou | e7e96e0 | 2018-04-13 13:44:10 +0100 | [diff] [blame] | 81 | return _subgemm->B_pretranspose_required(); |
| 82 | } |
| 83 | |
Anthony Barbier | 5f70773 | 2018-07-03 16:22:02 +0100 | [diff] [blame] | 84 | size_t get_B_pretransposed_array_size() const override { |
Michalis Spyrou | e7e96e0 | 2018-04-13 13:44:10 +0100 | [diff] [blame] | 85 | return _subgemm->get_B_pretransposed_array_size(); |
| 86 | } |
| 87 | |
Anthony Barbier | 5f70773 | 2018-07-03 16:22:02 +0100 | [diff] [blame] | 88 | void pretranspose_B_array(void *buffer, const To *B, const int ldb, const int B_multi_stride) override { |
Michalis Spyrou | e7e96e0 | 2018-04-13 13:44:10 +0100 | [diff] [blame] | 89 | _subgemm->pretranspose_B_array(buffer, B, ldb, B_multi_stride); |
| 90 | } |
| 91 | |
Anthony Barbier | 5f70773 | 2018-07-03 16:22:02 +0100 | [diff] [blame] | 92 | void set_pretransposed_B_data(void *buffer) override { |
Michalis Spyrou | e7e96e0 | 2018-04-13 13:44:10 +0100 | [diff] [blame] | 93 | _subgemm->set_pretransposed_B_data(buffer); |
| 94 | } |
| 95 | }; |
| 96 | |
| 97 | } // namespace arm_gemm |