morgolock | 5111264 | 2020-08-20 14:51:39 +0100 | [diff] [blame] | 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 | */ |
Sang-Hoon Park | 68dd25f | 2020-10-19 16:00:11 +0100 | [diff] [blame] | 24 | #include "src/runtime/SchedulerUtils.h" |
morgolock | 5111264 | 2020-08-20 14:51:39 +0100 | [diff] [blame] | 25 | |
| 26 | #include "arm_compute/core/Error.h" |
| 27 | |
| 28 | #include <cmath> |
| 29 | |
| 30 | namespace arm_compute |
| 31 | { |
Sang-Hoon Park | 68dd25f | 2020-10-19 16:00:11 +0100 | [diff] [blame] | 32 | namespace scheduler_utils |
| 33 | { |
morgolock | 5111264 | 2020-08-20 14:51:39 +0100 | [diff] [blame] | 34 | #ifndef BARE_METAL |
| 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) ) |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 50 | const unsigned adjusted = std::round(std::sqrt(max_threads * ratio)); |
morgolock | 5111264 | 2020-08-20 14:51:39 +0100 | [diff] [blame] | 51 | |
| 52 | //find the nearest factor of max_threads |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 53 | for (unsigned i = 0; i != adjusted; ++i) |
morgolock | 5111264 | 2020-08-20 14:51:39 +0100 | [diff] [blame] | 54 | { |
| 55 | //try down |
| 56 | const unsigned adj_down = adjusted - i; |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 57 | if (max_threads % adj_down == 0) |
morgolock | 5111264 | 2020-08-20 14:51:39 +0100 | [diff] [blame] | 58 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 59 | return {adj_down, max_threads / adj_down}; |
morgolock | 5111264 | 2020-08-20 14:51:39 +0100 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | //try up |
| 63 | const unsigned adj_up = adjusted + i; |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 64 | if (max_threads % adj_up == 0) |
morgolock | 5111264 | 2020-08-20 14:51:39 +0100 | [diff] [blame] | 65 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 66 | return {adj_up, max_threads / adj_up}; |
morgolock | 5111264 | 2020-08-20 14:51:39 +0100 | [diff] [blame] | 67 | } |
| 68 | } |
| 69 | |
| 70 | //we didn't find anything so lets bail out with maxes biased to the largest dimension |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 71 | if (m > n) |
morgolock | 5111264 | 2020-08-20 14:51:39 +0100 | [diff] [blame] | 72 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 73 | return {std::min<unsigned>(m, max_threads), 1}; |
morgolock | 5111264 | 2020-08-20 14:51:39 +0100 | [diff] [blame] | 74 | } |
| 75 | else |
| 76 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 77 | return {1, std::min<unsigned>(n, max_threads)}; |
morgolock | 5111264 | 2020-08-20 14:51:39 +0100 | [diff] [blame] | 78 | } |
| 79 | } |
| 80 | #endif /* #ifndef BARE_METAL */ |
Sang-Hoon Park | 68dd25f | 2020-10-19 16:00:11 +0100 | [diff] [blame] | 81 | } // namespace scheduler_utils |
morgolock | 5111264 | 2020-08-20 14:51:39 +0100 | [diff] [blame] | 82 | } // namespace arm_compute |