Georgios Pinitas | 1d48065 | 2019-01-23 11:24:50 +0000 | [diff] [blame] | 1 | /* |
Sang-Hoon Park | 4f7693d | 2021-05-12 13:59:10 +0100 | [diff] [blame] | 2 | * Copyright (c) 2019-2021 Arm Limited. |
Georgios Pinitas | 1d48065 | 2019-01-23 11:24:50 +0000 | [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 <algorithm> |
Georgios Pinitas | 5aa1a0b | 2020-07-02 20:02:20 +0100 | [diff] [blame] | 27 | #include <array> |
| 28 | #include <cassert> |
Georgios Pinitas | 1d48065 | 2019-01-23 11:24:50 +0000 | [diff] [blame] | 29 | #include <initializer_list> |
| 30 | |
Georgios Pinitas | 5aa1a0b | 2020-07-02 20:02:20 +0100 | [diff] [blame] | 31 | namespace arm_gemm |
| 32 | { |
| 33 | template <unsigned int D> |
| 34 | class NDRange |
| 35 | { |
Georgios Pinitas | 1d48065 | 2019-01-23 11:24:50 +0000 | [diff] [blame] | 36 | private: |
Georgios Pinitas | 5aa1a0b | 2020-07-02 20:02:20 +0100 | [diff] [blame] | 37 | std::array<unsigned int, D> m_sizes{}; |
| 38 | std::array<unsigned int, D> m_totalsizes{}; |
Georgios Pinitas | 1d48065 | 2019-01-23 11:24:50 +0000 | [diff] [blame] | 39 | |
Georgios Pinitas | 5aa1a0b | 2020-07-02 20:02:20 +0100 | [diff] [blame] | 40 | class NDRangeIterator |
| 41 | { |
Georgios Pinitas | 1d48065 | 2019-01-23 11:24:50 +0000 | [diff] [blame] | 42 | private: |
| 43 | const NDRange &m_parent; |
Georgios Pinitas | 5aa1a0b | 2020-07-02 20:02:20 +0100 | [diff] [blame] | 44 | unsigned int m_pos = 0; |
| 45 | unsigned int m_end = 0; |
Georgios Pinitas | 1d48065 | 2019-01-23 11:24:50 +0000 | [diff] [blame] | 46 | |
| 47 | public: |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 48 | NDRangeIterator(const NDRange &p, unsigned int s, unsigned int e) : m_parent(p), m_pos(s), m_end(e) |
Georgios Pinitas | 5aa1a0b | 2020-07-02 20:02:20 +0100 | [diff] [blame] | 49 | { |
| 50 | } |
Georgios Pinitas | 1d48065 | 2019-01-23 11:24:50 +0000 | [diff] [blame] | 51 | |
Georgios Pinitas | 5aa1a0b | 2020-07-02 20:02:20 +0100 | [diff] [blame] | 52 | bool done() const |
| 53 | { |
Georgios Pinitas | 1d48065 | 2019-01-23 11:24:50 +0000 | [diff] [blame] | 54 | return (m_pos >= m_end); |
| 55 | } |
| 56 | |
Georgios Pinitas | 5aa1a0b | 2020-07-02 20:02:20 +0100 | [diff] [blame] | 57 | unsigned int dim(unsigned int d) const |
| 58 | { |
Georgios Pinitas | 1d48065 | 2019-01-23 11:24:50 +0000 | [diff] [blame] | 59 | unsigned int r = m_pos; |
| 60 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 61 | if (d < (D - 1)) |
Georgios Pinitas | 5aa1a0b | 2020-07-02 20:02:20 +0100 | [diff] [blame] | 62 | { |
Georgios Pinitas | 1d48065 | 2019-01-23 11:24:50 +0000 | [diff] [blame] | 63 | r %= m_parent.m_totalsizes[d]; |
| 64 | } |
| 65 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 66 | if (d > 0) |
Georgios Pinitas | 5aa1a0b | 2020-07-02 20:02:20 +0100 | [diff] [blame] | 67 | { |
| 68 | r /= m_parent.m_totalsizes[d - 1]; |
Georgios Pinitas | 1d48065 | 2019-01-23 11:24:50 +0000 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | return r; |
| 72 | } |
| 73 | |
Georgios Pinitas | 5aa1a0b | 2020-07-02 20:02:20 +0100 | [diff] [blame] | 74 | bool next_dim0() |
| 75 | { |
Georgios Pinitas | 1d48065 | 2019-01-23 11:24:50 +0000 | [diff] [blame] | 76 | m_pos++; |
| 77 | |
| 78 | return !done(); |
| 79 | } |
| 80 | |
Georgios Pinitas | 5aa1a0b | 2020-07-02 20:02:20 +0100 | [diff] [blame] | 81 | bool next_dim1() |
| 82 | { |
Georgios Pinitas | 1d48065 | 2019-01-23 11:24:50 +0000 | [diff] [blame] | 83 | m_pos += m_parent.m_sizes[0] - dim(0); |
| 84 | |
| 85 | return !done(); |
| 86 | } |
| 87 | |
Georgios Pinitas | 5aa1a0b | 2020-07-02 20:02:20 +0100 | [diff] [blame] | 88 | unsigned int dim0_max() const |
| 89 | { |
Georgios Pinitas | 1d48065 | 2019-01-23 11:24:50 +0000 | [diff] [blame] | 90 | unsigned int offset = std::min(m_end - m_pos, m_parent.m_sizes[0] - dim(0)); |
| 91 | |
| 92 | return dim(0) + offset; |
| 93 | } |
| 94 | }; |
| 95 | |
Georgios Pinitas | 5aa1a0b | 2020-07-02 20:02:20 +0100 | [diff] [blame] | 96 | void set_totalsizes() |
| 97 | { |
| 98 | unsigned int t = 1; |
| 99 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 100 | for (unsigned int i = 0; i < D; i++) |
Georgios Pinitas | 5aa1a0b | 2020-07-02 20:02:20 +0100 | [diff] [blame] | 101 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 102 | if (m_sizes[i] == 0) |
Georgios Pinitas | 5aa1a0b | 2020-07-02 20:02:20 +0100 | [diff] [blame] | 103 | { |
| 104 | m_sizes[i] = 1; |
| 105 | } |
| 106 | |
| 107 | t *= m_sizes[i]; |
| 108 | |
| 109 | m_totalsizes[i] = t; |
| 110 | } |
| 111 | } |
| 112 | |
Georgios Pinitas | 1d48065 | 2019-01-23 11:24:50 +0000 | [diff] [blame] | 113 | public: |
Georgios Pinitas | 5aa1a0b | 2020-07-02 20:02:20 +0100 | [diff] [blame] | 114 | NDRange &operator=(const NDRange &rhs) = default; |
| 115 | NDRange(const NDRange &rhs) = default; |
Joseph Dobson | 6f8b17d | 2020-02-11 19:32:11 +0000 | [diff] [blame] | 116 | |
Georgios Pinitas | 1d48065 | 2019-01-23 11:24:50 +0000 | [diff] [blame] | 117 | template <typename... T> |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 118 | NDRange(T... ts) : m_sizes{ts...} |
Joseph Dobson | 6f8b17d | 2020-02-11 19:32:11 +0000 | [diff] [blame] | 119 | { |
Georgios Pinitas | 5aa1a0b | 2020-07-02 20:02:20 +0100 | [diff] [blame] | 120 | set_totalsizes(); |
Joseph Dobson | 6f8b17d | 2020-02-11 19:32:11 +0000 | [diff] [blame] | 121 | } |
| 122 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 123 | NDRange(const std::array<unsigned int, D> &n) : m_sizes(n) |
Joseph Dobson | 6f8b17d | 2020-02-11 19:32:11 +0000 | [diff] [blame] | 124 | { |
Georgios Pinitas | 5aa1a0b | 2020-07-02 20:02:20 +0100 | [diff] [blame] | 125 | set_totalsizes(); |
Georgios Pinitas | 1d48065 | 2019-01-23 11:24:50 +0000 | [diff] [blame] | 126 | } |
| 127 | |
Georgios Pinitas | 5aa1a0b | 2020-07-02 20:02:20 +0100 | [diff] [blame] | 128 | NDRangeIterator iterator(unsigned int start, unsigned int end) const |
| 129 | { |
Georgios Pinitas | 1d48065 | 2019-01-23 11:24:50 +0000 | [diff] [blame] | 130 | return NDRangeIterator(*this, start, end); |
| 131 | } |
| 132 | |
Georgios Pinitas | 5aa1a0b | 2020-07-02 20:02:20 +0100 | [diff] [blame] | 133 | unsigned int total_size() const |
| 134 | { |
Georgios Pinitas | 1d48065 | 2019-01-23 11:24:50 +0000 | [diff] [blame] | 135 | return m_totalsizes[D - 1]; |
| 136 | } |
| 137 | |
Georgios Pinitas | 5aa1a0b | 2020-07-02 20:02:20 +0100 | [diff] [blame] | 138 | unsigned int get_size(unsigned int v) const |
| 139 | { |
Georgios Pinitas | 1d48065 | 2019-01-23 11:24:50 +0000 | [diff] [blame] | 140 | return m_sizes[v]; |
| 141 | } |
| 142 | }; |
| 143 | |
Joseph Dobson | 6f8b17d | 2020-02-11 19:32:11 +0000 | [diff] [blame] | 144 | /** NDCoordinate builds upon a range, but specifies a starting position |
| 145 | * in addition to a size which it inherits from NDRange |
| 146 | */ |
Georgios Pinitas | 5aa1a0b | 2020-07-02 20:02:20 +0100 | [diff] [blame] | 147 | template <unsigned int N> |
| 148 | class NDCoordinate : public NDRange<N> |
| 149 | { |
| 150 | using int_t = unsigned int; |
Joseph Dobson | 6f8b17d | 2020-02-11 19:32:11 +0000 | [diff] [blame] | 151 | using ndrange_t = NDRange<N>; |
| 152 | |
Georgios Pinitas | 5aa1a0b | 2020-07-02 20:02:20 +0100 | [diff] [blame] | 153 | std::array<int_t, N> m_positions{}; |
| 154 | |
Joseph Dobson | 6f8b17d | 2020-02-11 19:32:11 +0000 | [diff] [blame] | 155 | public: |
Georgios Pinitas | 5aa1a0b | 2020-07-02 20:02:20 +0100 | [diff] [blame] | 156 | NDCoordinate &operator=(const NDCoordinate &rhs) = default; |
| 157 | NDCoordinate(const NDCoordinate &rhs) = default; |
| 158 | NDCoordinate(const std::initializer_list<std::pair<int_t, int_t>> &list) |
Joseph Dobson | 6f8b17d | 2020-02-11 19:32:11 +0000 | [diff] [blame] | 159 | { |
Georgios Pinitas | 0e24015 | 2020-05-11 12:29:03 +0100 | [diff] [blame] | 160 | std::array<int_t, N> sizes{}; |
Joseph Dobson | 6f8b17d | 2020-02-11 19:32:11 +0000 | [diff] [blame] | 161 | |
| 162 | std::size_t i = 0; |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 163 | for (auto &p : list) |
Georgios Pinitas | 5aa1a0b | 2020-07-02 20:02:20 +0100 | [diff] [blame] | 164 | { |
| 165 | m_positions[i] = p.first; |
| 166 | sizes[i++] = p.second; |
Joseph Dobson | 6f8b17d | 2020-02-11 19:32:11 +0000 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | //update the parents sizes |
Georgios Pinitas | 5aa1a0b | 2020-07-02 20:02:20 +0100 | [diff] [blame] | 170 | static_cast<ndrange_t &>(*this) = ndrange_t(sizes); |
Joseph Dobson | 6f8b17d | 2020-02-11 19:32:11 +0000 | [diff] [blame] | 171 | } |
| 172 | |
Georgios Pinitas | 5aa1a0b | 2020-07-02 20:02:20 +0100 | [diff] [blame] | 173 | int_t get_position(int_t d) const |
| 174 | { |
| 175 | assert(d < N); |
| 176 | |
Joseph Dobson | 6f8b17d | 2020-02-11 19:32:11 +0000 | [diff] [blame] | 177 | return m_positions[d]; |
| 178 | } |
| 179 | |
Georgios Pinitas | 5aa1a0b | 2020-07-02 20:02:20 +0100 | [diff] [blame] | 180 | void set_position(int_t d, int_t v) |
| 181 | { |
| 182 | assert(d < N); |
Joseph Dobson | 6f8b17d | 2020-02-11 19:32:11 +0000 | [diff] [blame] | 183 | |
| 184 | m_positions[d] = v; |
| 185 | } |
| 186 | |
Georgios Pinitas | 5aa1a0b | 2020-07-02 20:02:20 +0100 | [diff] [blame] | 187 | int_t get_position_end(int_t d) const |
| 188 | { |
| 189 | return get_position(d) + ndrange_t::get_size(d); |
Joseph Dobson | 6f8b17d | 2020-02-11 19:32:11 +0000 | [diff] [blame] | 190 | } |
| 191 | }; //class NDCoordinate |
| 192 | |
Georgios Pinitas | 5aa1a0b | 2020-07-02 20:02:20 +0100 | [diff] [blame] | 193 | using ndrange_t = NDRange<6>; |
| 194 | using ndcoord_t = NDCoordinate<6>; |
Joseph Dobson | 6f8b17d | 2020-02-11 19:32:11 +0000 | [diff] [blame] | 195 | |
Georgios Pinitas | 1d48065 | 2019-01-23 11:24:50 +0000 | [diff] [blame] | 196 | } // namespace arm_gemm |