blob: 7dff01003d89ec1ed5e37fd949d4a4c666103201 [file] [log] [blame]
Joseph Dobson6f8b17d2020-02-11 19:32:11 +00001/*
2 * Copyright (c) 2020 ARM Limited.
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 "arm_compute/core/Window.h"
27#include "arm_compute/core/Dimensions.h"
28#include "src/core/NEON/kernels/arm_gemm/ndrange.hpp"
29
30#include <cassert>
31
32/* This file contains mapping between integral types used in arm_compute and arm_gemm
33 * These two codebases both require a degree of separation for the sake of modularity
34 * so maintain their own types which represent similar information.
35 */
36
37namespace arm_gemm {
38
39//we want to unify the maximum number of dimensions used beween arm_gemm and arm compute library
40constexpr std::size_t ndrange_max =
41 arm_compute::Dimensions<unsigned int>::num_max_dimensions;
42
43using ndrange_t=NDRange<ndrange_max>;
44using ndcoord_t=NDCoordinate<ndrange_max>;
45
46/* Converts an `arm_gemm::ndrange_t` to a `arm_compute::Window`
47 *
48 * As `NDRange<T>` does not not encode start positions, we specify
49 * the start to be zero in the produced `arm_compute::Window`
50 *
51 * @param [ndr] the `arm_gemm::ndrange_t` we wish to convert into a `arm_compute::Window`
52 * @returns an `arm_compute::Window` representing the same dimensional ranges as `ndr`
53 */
54inline arm_compute::Window to_window(const ndrange_t& ndr) {
55 arm_compute::Window win;
56
57 for(unsigned int i = 0; i!=ndrange_max; ++i) {
58 //populate the window with the dimensions of the NDRange
59 win.set(i, arm_compute::Window::Dimension(0, ndr.get_size(i)));
60 }
61
62 return win;
63}
64
65/*
66 * Converts an `arm_gemm::ndcoord_t` to a `arm_compute::Window`
67 *
68 * @param [ndc] the `arm_gemm::ndcoord_t` we wish to convert into a `arm_compute::Window`
69 * @returns an `arm_compute::Window` representing the same dimensional ranges as `ndc`
70 */
71inline arm_compute::Window to_window(const ndcoord_t& ndc) {
72 arm_compute::Window win;
73
74 for(unsigned int i = 0; i!=ndrange_max; ++i) {
75 const auto start = ndc.get_position(i);
76 const auto size = ndc.get_size(i);
77 const auto stop = start + size;
78
79 //populate the window with the dimensions of the NDRange
80 win.set(i, arm_compute::Window::Dimension(start, stop));
81 }
82
83 return win;
84}
85
86/** Convert an `arm_compute::Window` to an `arm_gemm::NDRange` of the same max dimensions
87 *
88 * It should be noted that `arm_compute::Window` specifies a `start()` and an `end()`
89 * where as `arm_gemm::ndrange_t` only has a size, as a result we store the delta between the range
90 *
91 * @param [win] the `arm_compute::Window` we want to convert to `arm_gemm::ndrange_t`
92 * @return the resultant ndrange_t
93 */
94inline ndrange_t to_ndrange(const arm_compute::Window& win) {
95 return {
96 static_cast<unsigned int>(win[0].end() - win[0].start()),
97 static_cast<unsigned int>(win[1].end() - win[1].start()),
98 static_cast<unsigned int>(win[2].end() - win[2].start()),
99 static_cast<unsigned int>(win[3].end() - win[3].start()),
100 static_cast<unsigned int>(win[4].end() - win[4].start()),
101 static_cast<unsigned int>(win[5].end() - win[5].start())
102 };
103}
104
105/** Convert an `arm_compute::Window` to an `arm_gemm::NDCoord` of the same max dimensions
106 *
107 * @param [win] the `arm_compute::Window` we want to convert to `arm_gemm::ndcoord_t`
108 * @return the resultant ndcoord_t
109 */
110inline ndcoord_t to_ndcoord(const arm_compute::Window& win) {
111 return {
112 { static_cast<unsigned int>(win[0].start()), static_cast<unsigned int>(win[0].end() - win[0].start()) },
113 { static_cast<unsigned int>(win[1].start()), static_cast<unsigned int>(win[1].end() - win[1].start()) },
114 { static_cast<unsigned int>(win[2].start()), static_cast<unsigned int>(win[2].end() - win[2].start()) },
115 { static_cast<unsigned int>(win[3].start()), static_cast<unsigned int>(win[3].end() - win[3].start()) },
116 { static_cast<unsigned int>(win[4].start()), static_cast<unsigned int>(win[4].end() - win[4].start()) },
117 { static_cast<unsigned int>(win[5].start()), static_cast<unsigned int>(win[5].end() - win[5].start()) }
118 };
119}
120
121} //namespace arm_gemm