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