blob: 3919c339bfbcb52aae5df33aa975c08bbe40d8c6 [file] [log] [blame]
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +01001/*
Pablo Telloeb82fd22018-02-23 13:43:50 +00002 * Copyright (c) 2017-2018 ARM Limited.
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +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
Pablo Telloeb82fd22018-02-23 13:43:50 +000026#include <cstddef>
27
28namespace arm_gemm {
29
30// Abstract class for the GEMM/GEMV functions.
31//
32// GEMM implementations may be "native" (never require any input
33// permutation), "pretransposed" (require permutation up-front) or require
34// working space (permute as they go along). This interface should support
35// all of them.
36
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +010037template<typename To, typename Tr>
38class GemmCommon {
Pablo Telloeb82fd22018-02-23 13:43:50 +000039protected:
40 const To *_Aptr=nullptr;
41 int _lda=0;
Michalis Spyroue7e96e02018-04-13 13:44:10 +010042 int _A_batch_stride=0;
43 int _A_multi_stride=0;
Pablo Telloeb82fd22018-02-23 13:43:50 +000044 const To *_Bptr=nullptr;
45 int _ldb=0;
Michalis Spyroue7e96e02018-04-13 13:44:10 +010046 int _B_multi_stride=0;
Pablo Telloeb82fd22018-02-23 13:43:50 +000047 Tr *_Cptr=nullptr;
48 int _ldc=0;
Michalis Spyroue7e96e02018-04-13 13:44:10 +010049 int _C_batch_stride=0;
50 int _C_multi_stride=0;
Pablo Telloeb82fd22018-02-23 13:43:50 +000051
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +010052public:
Pablo Telloeb82fd22018-02-23 13:43:50 +000053 /* Pass in the pointers to the arrays to be operated on and their
54 * strides. This has a default implementation that just captures them
55 * all in protected members. If B is pretransposed (see below) then the
56 * settings for B here are ignored. */
Michalis Spyroue7e96e02018-04-13 13:44:10 +010057 virtual void set_arrays(const To *A, const int lda, const int A_batch_stride, const int A_multi_stride,
58 const To *B, const int ldb, /* batches share B */ const int B_multi_stride,
59 Tr *C, const int ldc, const int C_batch_stride, const int C_multi_stride) {
Pablo Telloeb82fd22018-02-23 13:43:50 +000060 _Aptr = A;
61 _lda = lda;
Michalis Spyroue7e96e02018-04-13 13:44:10 +010062 _A_batch_stride = A_batch_stride;
63 _A_multi_stride = A_multi_stride;
Pablo Telloeb82fd22018-02-23 13:43:50 +000064 _Bptr = B;
65 _ldb = ldb;
Michalis Spyroue7e96e02018-04-13 13:44:10 +010066 _B_multi_stride = B_multi_stride;
Pablo Telloeb82fd22018-02-23 13:43:50 +000067 _Cptr = C;
68 _ldc = ldc;
Michalis Spyroue7e96e02018-04-13 13:44:10 +010069 _C_batch_stride = C_batch_stride;
70 _C_multi_stride = C_multi_stride;
Pablo Telloeb82fd22018-02-23 13:43:50 +000071 }
72
73 /* For threading, we divide the work into some number of units and work
74 * out internally what unit corresponds to what work. This returns the
75 * total number of units. */
76 virtual unsigned int get_window_size() const = 0;
77
78 /* The maximum thread count is specified when the GEMM is created. Some
79 * implementations need to know how many threads will actually run in
80 * order to work properly.
81 *
82 * In some cases, after creating the GEMM the number of threads needs to
83 * be reduced (e.g. not enough work to split across threads). This
84 * method allows the number of actual threads to be run to be set (must
85 * be equal or lower).
86 *
87 * This has an empty default implementation, as GEMMs which don't care
88 * about thread count can safely ignore this.
89 */
90 virtual void set_nthreads(int nthreads) { };
91
92 /* Actually do the work. Provide a threadid to index any per-thread
93 * buffers, and a start/end range to indicate which work to do. */
94 virtual void execute(unsigned int start, unsigned int end, int threadid) = 0;
95
96 /*** Working space interface (optional) ***/
97 /* Total number of bytes of temporary working space needed. If zero, it's not necessary to call set_working_space(). */
98 virtual size_t get_working_size() const { return 0; }
99 /* Provide working space buffer - the void * passed in must remain allocated for the duration of any execute calls. */
100 virtual void set_working_space(void *) { };
101
102 /*** "Pretransposed" interface (optional) ***/
103 /* Is this object set up for pretranspose? If so, pretranspose_array() needs to be called before execute(); */
104 virtual bool B_is_pretransposed() const { return false; }
105 /* Does pretranspose still need to be done? */
106 virtual bool B_pretranspose_required() const { return false; }
107 /* Total number of bytes of space needed for pretransposed arrays. */
108 virtual size_t get_B_pretransposed_array_size() const { return 0; }
109 /* Perform pretranspose - the void * passed in must remain allocated for the duration of any execute calls. */
Michalis Spyroue7e96e02018-04-13 13:44:10 +0100110 virtual void pretranspose_B_array(void *buffer, const To *B, const int ldb, const int B_multi_stride) { };
111 /* Set pretransposed data - the void * passed in must previously have been passed to pretranspose_B_array() for the same or a similar GEMM. */
112 virtual void set_pretransposed_B_data(void *buffer) { }
Pablo Telloeb82fd22018-02-23 13:43:50 +0000113
114 // Destructor
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100115 virtual ~GemmCommon() { }
116};
Pablo Telloeb82fd22018-02-23 13:43:50 +0000117
118} // namespace arm_gemm