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