blob: 7f47abcbb977d79459d0657fa216e59c5b0cce7f [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;
42 const To *_Bptr=nullptr;
43 int _ldb=0;
44 Tr *_Cptr=nullptr;
45 int _ldc=0;
46
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +010047public:
Pablo Telloeb82fd22018-02-23 13:43:50 +000048 /* Pass in the pointers to the arrays to be operated on and their
49 * strides. This has a default implementation that just captures them
50 * all in protected members. If B is pretransposed (see below) then the
51 * settings for B here are ignored. */
52 virtual void set_arrays(const To *A, const int lda, const To *B, const int ldb, Tr *C, const int ldc) {
53 _Aptr = A;
54 _lda = lda;
55 _Bptr = B;
56 _ldb = ldb;
57 _Cptr = C;
58 _ldc = ldc;
59 }
60
61 /* For threading, we divide the work into some number of units and work
62 * out internally what unit corresponds to what work. This returns the
63 * total number of units. */
64 virtual unsigned int get_window_size() const = 0;
65
66 /* The maximum thread count is specified when the GEMM is created. Some
67 * implementations need to know how many threads will actually run in
68 * order to work properly.
69 *
70 * In some cases, after creating the GEMM the number of threads needs to
71 * be reduced (e.g. not enough work to split across threads). This
72 * method allows the number of actual threads to be run to be set (must
73 * be equal or lower).
74 *
75 * This has an empty default implementation, as GEMMs which don't care
76 * about thread count can safely ignore this.
77 */
78 virtual void set_nthreads(int nthreads) { };
79
80 /* Actually do the work. Provide a threadid to index any per-thread
81 * buffers, and a start/end range to indicate which work to do. */
82 virtual void execute(unsigned int start, unsigned int end, int threadid) = 0;
83
84 /*** Working space interface (optional) ***/
85 /* Total number of bytes of temporary working space needed. If zero, it's not necessary to call set_working_space(). */
86 virtual size_t get_working_size() const { return 0; }
87 /* Provide working space buffer - the void * passed in must remain allocated for the duration of any execute calls. */
88 virtual void set_working_space(void *) { };
89
90 /*** "Pretransposed" interface (optional) ***/
91 /* Is this object set up for pretranspose? If so, pretranspose_array() needs to be called before execute(); */
92 virtual bool B_is_pretransposed() const { return false; }
93 /* Does pretranspose still need to be done? */
94 virtual bool B_pretranspose_required() const { return false; }
95 /* Total number of bytes of space needed for pretransposed arrays. */
96 virtual size_t get_B_pretransposed_array_size() const { return 0; }
97 /* Perform pretranspose - the void * passed in must remain allocated for the duration of any execute calls. */
98 virtual void pretranspose_B_array(void *buffer, const To *, const int) { };
99
100 // Destructor
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100101 virtual ~GemmCommon() { }
102};
Pablo Telloeb82fd22018-02-23 13:43:50 +0000103
104} // namespace arm_gemm