blob: 74ee539fec178e9f055bb7f14c3f255019d4199d [file] [log] [blame]
morgolock51112642020-08-20 14:51:39 +01001/*
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 */
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010024#include "src/runtime/SchedulerUtils.h"
morgolock51112642020-08-20 14:51:39 +010025
26#include "arm_compute/core/Error.h"
27
28#include <cmath>
29
30namespace arm_compute
31{
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010032namespace scheduler_utils
33{
morgolock51112642020-08-20 14:51:39 +010034#ifndef BARE_METAL
35std::pair<unsigned, unsigned> split_2d(unsigned max_threads, std::size_t m, std::size_t n)
36{
37 /*
38 * We want the same ratio of threads in M & N to the ratio of m and n problem size
39 *
40 * Therefore: mt/nt == m/n where mt*nt == max_threads
41 *
42 * max_threads/nt = mt & (max_threads/nt) * (m/n) = nt
43 * nt^2 = max_threads * (m/n)
44 * nt = sqrt( max_threads * (m/n) )
45 */
46 //ratio of m to n in problem dimensions
47 double ratio = m / static_cast<double>(n);
48
49 // nt = sqrt(max_threads * (m / n) )
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010050 const unsigned adjusted = std::round(std::sqrt(max_threads * ratio));
morgolock51112642020-08-20 14:51:39 +010051
52 //find the nearest factor of max_threads
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010053 for (unsigned i = 0; i != adjusted; ++i)
morgolock51112642020-08-20 14:51:39 +010054 {
55 //try down
56 const unsigned adj_down = adjusted - i;
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010057 if (max_threads % adj_down == 0)
morgolock51112642020-08-20 14:51:39 +010058 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010059 return {adj_down, max_threads / adj_down};
morgolock51112642020-08-20 14:51:39 +010060 }
61
62 //try up
63 const unsigned adj_up = adjusted + i;
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010064 if (max_threads % adj_up == 0)
morgolock51112642020-08-20 14:51:39 +010065 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010066 return {adj_up, max_threads / adj_up};
morgolock51112642020-08-20 14:51:39 +010067 }
68 }
69
70 //we didn't find anything so lets bail out with maxes biased to the largest dimension
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010071 if (m > n)
morgolock51112642020-08-20 14:51:39 +010072 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010073 return {std::min<unsigned>(m, max_threads), 1};
morgolock51112642020-08-20 14:51:39 +010074 }
75 else
76 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010077 return {1, std::min<unsigned>(n, max_threads)};
morgolock51112642020-08-20 14:51:39 +010078 }
79}
80#endif /* #ifndef BARE_METAL */
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010081} // namespace scheduler_utils
morgolock51112642020-08-20 14:51:39 +010082} // namespace arm_compute