blob: 0672e899b6f7672463b1cad0fef1dbe2dd990acd [file] [log] [blame]
Joseph Dobson6f8b17d2020-02-11 19:32:11 +00001/*
Sang-Hoon Park4f7693d2021-05-12 13:59:10 +01002 * Copyright (c) 2020-2021 Arm Limited.
Joseph Dobson6f8b17d2020-02-11 19:32:11 +00003 *
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 Dobson6f8b17d2020-02-11 19:32:11 +000026#include "arm_compute/core/Dimensions.h"
Sang-Hoon Park4f7693d2021-05-12 13:59:10 +010027#include "arm_compute/core/Window.h"
Michele Di Giorgio6ad60af2020-06-09 14:52:15 +010028
29#include "ndrange.hpp"
Joseph Dobson6f8b17d2020-02-11 19:32:11 +000030#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
Sang-Hoon Park4f7693d2021-05-12 13:59:10 +010037namespace arm_gemm
38{
Joseph Dobson6f8b17d2020-02-11 19:32:11 +000039//we want to unify the maximum number of dimensions used beween arm_gemm and arm compute library
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010040constexpr std::size_t ndrange_max = arm_compute::Dimensions<unsigned int>::num_max_dimensions;
Joseph Dobson6f8b17d2020-02-11 19:32:11 +000041
Sang-Hoon Park4f7693d2021-05-12 13:59:10 +010042using ndrange_t = NDRange<ndrange_max>;
43using ndcoord_t = NDCoordinate<ndrange_max>;
Joseph Dobson6f8b17d2020-02-11 19:32:11 +000044
45/* Converts an `arm_gemm::ndrange_t` to a `arm_compute::Window`
46 *
47 * As `NDRange<T>` does not not encode start positions, we specify
48 * the start to be zero in the produced `arm_compute::Window`
49 *
50 * @param [ndr] the `arm_gemm::ndrange_t` we wish to convert into a `arm_compute::Window`
51 * @returns an `arm_compute::Window` representing the same dimensional ranges as `ndr`
52 */
Sang-Hoon Park4f7693d2021-05-12 13:59:10 +010053inline arm_compute::Window to_window(const ndrange_t &ndr)
54{
Joseph Dobson6f8b17d2020-02-11 19:32:11 +000055 arm_compute::Window win;
56
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010057 for (unsigned int i = 0; i != ndrange_max; ++i)
Sang-Hoon Park4f7693d2021-05-12 13:59:10 +010058 {
Joseph Dobson6f8b17d2020-02-11 19:32:11 +000059 //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 */
Sang-Hoon Park4f7693d2021-05-12 13:59:10 +010072inline arm_compute::Window to_window(const ndcoord_t &ndc)
73{
Joseph Dobson6f8b17d2020-02-11 19:32:11 +000074 arm_compute::Window win;
75
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010076 for (unsigned int i = 0; i != ndrange_max; ++i)
Sang-Hoon Park4f7693d2021-05-12 13:59:10 +010077 {
Joseph Dobson6f8b17d2020-02-11 19:32:11 +000078 const auto start = ndc.get_position(i);
79 const auto size = ndc.get_size(i);
80 const auto stop = start + size;
81
82 //populate the window with the dimensions of the NDRange
83 win.set(i, arm_compute::Window::Dimension(start, stop));
84 }
85
86 return win;
87}
88
89/** Convert an `arm_compute::Window` to an `arm_gemm::NDRange` of the same max dimensions
90 *
91 * It should be noted that `arm_compute::Window` specifies a `start()` and an `end()`
92 * where as `arm_gemm::ndrange_t` only has a size, as a result we store the delta between the range
93 *
94 * @param [win] the `arm_compute::Window` we want to convert to `arm_gemm::ndrange_t`
95 * @return the resultant ndrange_t
96 */
Sang-Hoon Park4f7693d2021-05-12 13:59:10 +010097inline ndrange_t to_ndrange(const arm_compute::Window &win)
98{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010099 return {static_cast<unsigned int>(win[0].end() - win[0].start()),
100 static_cast<unsigned int>(win[1].end() - win[1].start()),
101 static_cast<unsigned int>(win[2].end() - win[2].start()),
102 static_cast<unsigned int>(win[3].end() - win[3].start()),
103 static_cast<unsigned int>(win[4].end() - win[4].start()),
104 static_cast<unsigned int>(win[5].end() - win[5].start())};
Joseph Dobson6f8b17d2020-02-11 19:32:11 +0000105}
106
107/** Convert an `arm_compute::Window` to an `arm_gemm::NDCoord` of the same max dimensions
108 *
109 * @param [win] the `arm_compute::Window` we want to convert to `arm_gemm::ndcoord_t`
110 * @return the resultant ndcoord_t
111 */
Sang-Hoon Park4f7693d2021-05-12 13:59:10 +0100112inline ndcoord_t to_ndcoord(const arm_compute::Window &win)
113{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100114 return {{static_cast<unsigned int>(win[0].start()), static_cast<unsigned int>(win[0].end() - win[0].start())},
115 {static_cast<unsigned int>(win[1].start()), static_cast<unsigned int>(win[1].end() - win[1].start())},
116 {static_cast<unsigned int>(win[2].start()), static_cast<unsigned int>(win[2].end() - win[2].start())},
117 {static_cast<unsigned int>(win[3].start()), static_cast<unsigned int>(win[3].end() - win[3].start())},
118 {static_cast<unsigned int>(win[4].start()), static_cast<unsigned int>(win[4].end() - win[4].start())},
119 {static_cast<unsigned int>(win[5].start()), static_cast<unsigned int>(win[5].end() - win[5].start())}};
Joseph Dobson6f8b17d2020-02-11 19:32:11 +0000120}
121
122} //namespace arm_gemm