blob: 86638298abeacaf7ed99c8bed0cb95380f447e6b [file] [log] [blame]
Georgios Pinitas1d480652019-01-23 11:24:50 +00001/*
Michele Di Giorgio6ad60af2020-06-09 14:52:15 +01002 * Copyright (c) 2019-2020 ARM Limited.
Georgios Pinitas1d480652019-01-23 11:24:50 +00003 *
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 <algorithm>
Georgios Pinitas5aa1a0b2020-07-02 20:02:20 +010027#include <array>
28#include <cassert>
Georgios Pinitas1d480652019-01-23 11:24:50 +000029#include <initializer_list>
30
Georgios Pinitas5aa1a0b2020-07-02 20:02:20 +010031namespace arm_gemm
32{
33template <unsigned int D>
34class NDRange
35{
Georgios Pinitas1d480652019-01-23 11:24:50 +000036private:
Georgios Pinitas5aa1a0b2020-07-02 20:02:20 +010037 std::array<unsigned int, D> m_sizes{};
38 std::array<unsigned int, D> m_totalsizes{};
Georgios Pinitas1d480652019-01-23 11:24:50 +000039
Georgios Pinitas5aa1a0b2020-07-02 20:02:20 +010040 class NDRangeIterator
41 {
Georgios Pinitas1d480652019-01-23 11:24:50 +000042 private:
43 const NDRange &m_parent;
Georgios Pinitas5aa1a0b2020-07-02 20:02:20 +010044 unsigned int m_pos = 0;
45 unsigned int m_end = 0;
Georgios Pinitas1d480652019-01-23 11:24:50 +000046
47 public:
Georgios Pinitas5aa1a0b2020-07-02 20:02:20 +010048 NDRangeIterator(const NDRange &p, unsigned int s, unsigned int e)
49 : m_parent(p), m_pos(s), m_end(e)
50 {
51 }
Georgios Pinitas1d480652019-01-23 11:24:50 +000052
Georgios Pinitas5aa1a0b2020-07-02 20:02:20 +010053 bool done() const
54 {
Georgios Pinitas1d480652019-01-23 11:24:50 +000055 return (m_pos >= m_end);
56 }
57
Georgios Pinitas5aa1a0b2020-07-02 20:02:20 +010058 unsigned int dim(unsigned int d) const
59 {
Georgios Pinitas1d480652019-01-23 11:24:50 +000060 unsigned int r = m_pos;
61
Georgios Pinitas5aa1a0b2020-07-02 20:02:20 +010062 if(d < (D - 1))
63 {
Georgios Pinitas1d480652019-01-23 11:24:50 +000064 r %= m_parent.m_totalsizes[d];
65 }
66
Georgios Pinitas5aa1a0b2020-07-02 20:02:20 +010067 if(d > 0)
68 {
69 r /= m_parent.m_totalsizes[d - 1];
Georgios Pinitas1d480652019-01-23 11:24:50 +000070 }
71
72 return r;
73 }
74
Georgios Pinitas5aa1a0b2020-07-02 20:02:20 +010075 bool next_dim0()
76 {
Georgios Pinitas1d480652019-01-23 11:24:50 +000077 m_pos++;
78
79 return !done();
80 }
81
Georgios Pinitas5aa1a0b2020-07-02 20:02:20 +010082 bool next_dim1()
83 {
Georgios Pinitas1d480652019-01-23 11:24:50 +000084 m_pos += m_parent.m_sizes[0] - dim(0);
85
86 return !done();
87 }
88
Georgios Pinitas5aa1a0b2020-07-02 20:02:20 +010089 unsigned int dim0_max() const
90 {
Georgios Pinitas1d480652019-01-23 11:24:50 +000091 unsigned int offset = std::min(m_end - m_pos, m_parent.m_sizes[0] - dim(0));
92
93 return dim(0) + offset;
94 }
95 };
96
Georgios Pinitas5aa1a0b2020-07-02 20:02:20 +010097 void set_totalsizes()
98 {
99 unsigned int t = 1;
100
101 for(unsigned int i = 0; i < D; i++)
102 {
103 if(m_sizes[i] == 0)
104 {
105 m_sizes[i] = 1;
106 }
107
108 t *= m_sizes[i];
109
110 m_totalsizes[i] = t;
111 }
112 }
113
Georgios Pinitas1d480652019-01-23 11:24:50 +0000114public:
Georgios Pinitas5aa1a0b2020-07-02 20:02:20 +0100115 NDRange &operator=(const NDRange &rhs) = default;
116 NDRange(const NDRange &rhs) = default;
Joseph Dobson6f8b17d2020-02-11 19:32:11 +0000117
Georgios Pinitas1d480652019-01-23 11:24:50 +0000118 template <typename... T>
Joseph Dobson6f8b17d2020-02-11 19:32:11 +0000119 NDRange(T... ts)
Georgios Pinitas5aa1a0b2020-07-02 20:02:20 +0100120 : m_sizes{ ts... }
Joseph Dobson6f8b17d2020-02-11 19:32:11 +0000121 {
Georgios Pinitas5aa1a0b2020-07-02 20:02:20 +0100122 set_totalsizes();
Joseph Dobson6f8b17d2020-02-11 19:32:11 +0000123 }
124
Georgios Pinitas5aa1a0b2020-07-02 20:02:20 +0100125 NDRange(const std::array<unsigned int, D> &n)
126 : m_sizes(n)
Joseph Dobson6f8b17d2020-02-11 19:32:11 +0000127 {
Georgios Pinitas5aa1a0b2020-07-02 20:02:20 +0100128 set_totalsizes();
Georgios Pinitas1d480652019-01-23 11:24:50 +0000129 }
130
Georgios Pinitas5aa1a0b2020-07-02 20:02:20 +0100131 NDRangeIterator iterator(unsigned int start, unsigned int end) const
132 {
Georgios Pinitas1d480652019-01-23 11:24:50 +0000133 return NDRangeIterator(*this, start, end);
134 }
135
Georgios Pinitas5aa1a0b2020-07-02 20:02:20 +0100136 unsigned int total_size() const
137 {
Georgios Pinitas1d480652019-01-23 11:24:50 +0000138 return m_totalsizes[D - 1];
139 }
140
Georgios Pinitas5aa1a0b2020-07-02 20:02:20 +0100141 unsigned int get_size(unsigned int v) const
142 {
Georgios Pinitas1d480652019-01-23 11:24:50 +0000143 return m_sizes[v];
144 }
145};
146
Joseph Dobson6f8b17d2020-02-11 19:32:11 +0000147/** NDCoordinate builds upon a range, but specifies a starting position
148 * in addition to a size which it inherits from NDRange
149 */
Georgios Pinitas5aa1a0b2020-07-02 20:02:20 +0100150template <unsigned int N>
151class NDCoordinate : public NDRange<N>
152{
153 using int_t = unsigned int;
Joseph Dobson6f8b17d2020-02-11 19:32:11 +0000154 using ndrange_t = NDRange<N>;
155
Georgios Pinitas5aa1a0b2020-07-02 20:02:20 +0100156 std::array<int_t, N> m_positions{};
157
Joseph Dobson6f8b17d2020-02-11 19:32:11 +0000158public:
Georgios Pinitas5aa1a0b2020-07-02 20:02:20 +0100159 NDCoordinate &operator=(const NDCoordinate &rhs) = default;
160 NDCoordinate(const NDCoordinate &rhs) = default;
161 NDCoordinate(const std::initializer_list<std::pair<int_t, int_t>> &list)
Joseph Dobson6f8b17d2020-02-11 19:32:11 +0000162 {
Georgios Pinitas0e240152020-05-11 12:29:03 +0100163 std::array<int_t, N> sizes{};
Joseph Dobson6f8b17d2020-02-11 19:32:11 +0000164
165 std::size_t i = 0;
Georgios Pinitas5aa1a0b2020-07-02 20:02:20 +0100166 for(auto &p : list)
167 {
168 m_positions[i] = p.first;
169 sizes[i++] = p.second;
Joseph Dobson6f8b17d2020-02-11 19:32:11 +0000170 }
171
172 //update the parents sizes
Georgios Pinitas5aa1a0b2020-07-02 20:02:20 +0100173 static_cast<ndrange_t &>(*this) = ndrange_t(sizes);
Joseph Dobson6f8b17d2020-02-11 19:32:11 +0000174 }
175
Georgios Pinitas5aa1a0b2020-07-02 20:02:20 +0100176 int_t get_position(int_t d) const
177 {
178 assert(d < N);
179
Joseph Dobson6f8b17d2020-02-11 19:32:11 +0000180 return m_positions[d];
181 }
182
Georgios Pinitas5aa1a0b2020-07-02 20:02:20 +0100183 void set_position(int_t d, int_t v)
184 {
185 assert(d < N);
Joseph Dobson6f8b17d2020-02-11 19:32:11 +0000186
187 m_positions[d] = v;
188 }
189
Georgios Pinitas5aa1a0b2020-07-02 20:02:20 +0100190 int_t get_position_end(int_t d) const
191 {
192 return get_position(d) + ndrange_t::get_size(d);
Joseph Dobson6f8b17d2020-02-11 19:32:11 +0000193 }
194}; //class NDCoordinate
195
Georgios Pinitas5aa1a0b2020-07-02 20:02:20 +0100196using ndrange_t = NDRange<6>;
197using ndcoord_t = NDCoordinate<6>;
Joseph Dobson6f8b17d2020-02-11 19:32:11 +0000198
Georgios Pinitas1d480652019-01-23 11:24:50 +0000199} // namespace arm_gemm