blob: 6f9a32c879783fd0da645bdf142fdd8bedbe4356 [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) )
50 const unsigned adjusted = std::round(
51 std::sqrt(max_threads * ratio));
52
53 //find the nearest factor of max_threads
54 for(unsigned i = 0; i != adjusted; ++i)
55 {
56 //try down
57 const unsigned adj_down = adjusted - i;
58 if(max_threads % adj_down == 0)
59 {
60 return { adj_down, max_threads / adj_down };
61 }
62
63 //try up
64 const unsigned adj_up = adjusted + i;
65 if(max_threads % adj_up == 0)
66 {
67 return { adj_up, max_threads / adj_up };
68 }
69 }
70
71 //we didn't find anything so lets bail out with maxes biased to the largest dimension
72 if(m > n)
73 {
74 return { std::min<unsigned>(m, max_threads), 1 };
75 }
76 else
77 {
78 return { 1, std::min<unsigned>(n, max_threads) };
79 }
80}
81#endif /* #ifndef BARE_METAL */
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010082} // namespace scheduler_utils
morgolock51112642020-08-20 14:51:39 +010083} // namespace arm_compute