xref: /aosp_15_r20/external/ComputeLibrary/src/runtime/SchedulerUtils.cpp (revision c217d954acce2dbc11938adb493fc0abd69584f3)
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 #include "src/runtime/SchedulerUtils.h"
25 
26 #include "arm_compute/core/Error.h"
27 
28 #include <cmath>
29 
30 namespace arm_compute
31 {
32 namespace scheduler_utils
33 {
34 #ifndef BARE_METAL
split_2d(unsigned max_threads,std::size_t m,std::size_t n)35 std::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 */
82 } // namespace scheduler_utils
83 } // namespace arm_compute
84