aboutsummaryrefslogtreecommitdiff
path: root/arm_compute/core/NEON/kernels/arm_gemm/ndrange.hpp
blob: 4ff83fbc51959fe0ea95b6067c2f66b4505a100b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*
 * Copyright (c) 2019-2020 Arm Limited.
 *
 * SPDX-License-Identifier: MIT
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to
 * deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
#pragma once

#include <array>
#include <algorithm>
#include <initializer_list>

#include <cassert>

namespace arm_gemm {

template<unsigned int D>
class NDRange {
private:
    std::array<unsigned int, D> m_sizes {};
    std::array<unsigned int, D> m_totalsizes {};

    class NDRangeIterator {
    private:
        const NDRange &m_parent;
        unsigned int m_pos = 0;
        unsigned int m_end = 0;

    public:
        NDRangeIterator(const NDRange &p, unsigned int s, unsigned int e) : m_parent(p), m_pos(s), m_end(e) { }

        bool done() const {
            return (m_pos >= m_end);
        }

        unsigned int dim(unsigned int d) const {
            unsigned int r = m_pos;

            if (d < (D - 1)) {
                r %= m_parent.m_totalsizes[d];
            }

            if (d > 0) {
                r /= m_parent.m_totalsizes[d-1];
            }

            return r;
        }

        bool next_dim0() {
            m_pos++;

            return !done();
        }

        bool next_dim1() {
            m_pos += m_parent.m_sizes[0] - dim(0);

            return !done();
        }

        unsigned int dim0_max() const {
            unsigned int offset = std::min(m_end - m_pos, m_parent.m_sizes[0] - dim(0));

            return dim(0) + offset;
        }
    };

public:
    NDRange& operator=(const NDRange& rhs)=default;
    NDRange(const NDRange& rhs)           =default;

    template <typename... T>
    NDRange(T... ts)
    : m_sizes{ts...}
    {
        unsigned int t=1;

        for (unsigned int i=0; i<D; i++) {
            t *= m_sizes[i];

            m_totalsizes[i] = t;
        }
    }

    NDRange(const std::array<unsigned int, D>& n)
    : m_sizes(n)
    {
        unsigned int t=1;

        for (unsigned int i=0; i<D; i++) {
            t *= m_sizes[i];

            m_totalsizes[i] = t;
        }
    }

    NDRangeIterator iterator(unsigned int start, unsigned int end) const {
        return NDRangeIterator(*this, start, end);
    }

    unsigned int total_size() const {
        return m_totalsizes[D - 1];
    }

    unsigned int get_size(unsigned int v) const {
        return m_sizes[v];
    }
};

/** NDCoordinate builds upon a range, but specifies a starting position
 * in addition to a size which it inherits from NDRange
 */
template<unsigned int N>
class NDCoordinate : public NDRange<N> {
    using int_t     =unsigned int;
    using ndrange_t = NDRange<N>;

    std::array<int_t, N> m_positions {};
public:
    NDCoordinate& operator=(const NDCoordinate& rhs)=default;
    NDCoordinate(const NDCoordinate& rhs)           =default;
    NDCoordinate(const std::initializer_list<std::pair<int_t, int_t>>& list)
    {
        std::array<int_t, N> sizes{};

        std::size_t i = 0;
        for(auto& p : list) {
            m_positions[i]= p.first;
            sizes[i++]    = p.second;
        }

        //update the parents sizes
        static_cast<ndrange_t&>(*this) = ndrange_t(sizes);
    }

    int_t get_position(int_t d) const {
        assert(d < m_positions.size());
        return m_positions[d];
    }

    void set_position(int_t d, int_t v) {
        assert(d < size(m_positions));
        assert(v < ndrange_t::get_size(d));

        m_positions[d] = v;
    }

    int_t get_position_end(int_t d) const {
        return get_position(d) + NDRange<N>::get_size(d);
    }
}; //class NDCoordinate

/** @returns the number of dimensions in the NDRange which have none-1 values
 * IE there is actual work in these dimensions that can be broken up
 */
template<unsigned int N>
std::size_t ndrange_popcount(const NDRange<N>& ndr) {
    std::size_t count = 0;

    for(unsigned int d = 0; d != N; ++d) {
        if(ndr.get_size(d) != 1)
            ++count;
    }
    return count;
}

} // namespace arm_gemm