| 1 | // |
| 2 | // SPDX-FileCopyrightText: Copyright 2025-2026 Arm Limited and/or its affiliates <open-source-office@arm.com> |
| 3 | // |
| 4 | // SPDX-License-Identifier: Apache-2.0 |
| 5 | // |
| 6 | |
| 7 | #include <cassert> |
| 8 | #include <cmath> |
| 9 | |
| 10 | #include "scheduler.hpp" |
| 11 | #include "gemm_lib.hpp" |
| 12 | |
| 13 | /* |
| 14 | * Helper function to handle 2D work splitting. This takes two parameters - a thread count and an ndrange_t. |
| 15 | * |
| 16 | * The return value is the number of pieces to divide the first dimension into. |
| 17 | */ |
| 18 | unsigned int granulator::split_2d(const unsigned int max_threads, const kai::ops::ndrange_t &range, const unsigned int dim0_override) { |
| 19 | auto m = range.get_size(0); |
| 20 | auto n = range.get_size(1); |
| 21 | |
| 22 | // Save some time if it's actually a 1D range. This avoids the need to |
| 23 | // prove that the code below will (eventually) do this. |
| 24 | if (n==1) { |
| 25 | return max_threads; |
| 26 | } |
| 27 | |
| 28 | // If we have a preconfigured shape, check it is legal before returning it. |
| 29 | if (dim0_override > 0) { |
| 30 | if ((dim0_override <= max_threads) && |
| 31 | (dim0_override <= m) && |
| 32 | (max_threads % dim0_override == 0)) { |
| 33 | return dim0_override; |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | // We assume that we want to divide the work into approximately square blocks. |
| 38 | // |
| 39 | // Therefore we want to find m, n such that m/n ~= aspect ratio of work area and m*n == total threads. |
| 40 | // |
| 41 | // So n = m / aspect ratio, substituting into the other equation we get m^2/ratio == total threads |
| 42 | // Therefore m = sqrt(total threads * ratio) |
| 43 | float ratio = m / static_cast<float>(n); |
| 44 | |
| 45 | unsigned int ideal_height = ::round(std::sqrt(max_threads * ratio)); |
| 46 | |
| 47 | // If it's so incredibly wide that it rounds down to zero, return 1. |
| 48 | if (ideal_height == 0) { |
| 49 | return 1; |
| 50 | } |
| 51 | |
| 52 | // Search up and down from the "ideal" number until we find a ratio that works. |
| 53 | for (unsigned int adj=0; adj<ideal_height; adj++) { |
| 54 | const unsigned int round_down = ideal_height - adj; |
| 55 | |
| 56 | if (max_threads % round_down == 0) { |
| 57 | return round_down; |
| 58 | } |
| 59 | |
| 60 | const unsigned int round_up = ideal_height + adj; |
| 61 | |
| 62 | if (max_threads % round_up == 0) { |
| 63 | return round_up; |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | // We can't get here - 'round_down' will become 1 before the loop terminates. |
| 68 | assert(false); |
| 69 | __builtin_unreachable(); |
| 70 | } |
| 71 | |
| 72 | void granulator::run_granule(unsigned int granule_id, unsigned int threadid) { |
| 73 | // Ignore out of range granules. |
| 74 | if (granule_id >= this->m_nthreads) { |
| 75 | return; |
| 76 | } |
| 77 | |
| 78 | auto winsize = this->m_gemm->get_window_size(); |
| 79 | |
| 80 | // Dimension 0 is M, and M is rows. So "X" is columns which is dimension 1. |
| 81 | auto thread_y = granule_id % m_dim0_parts; |
| 82 | auto thread_x = granule_id / m_dim0_parts; |
| 83 | |
| 84 | auto size_y = winsize.get_size(0); |
| 85 | auto size_x = winsize.get_size(1); |
| 86 | |
| 87 | auto y_start = (thread_y * size_y) / m_dim0_parts; |
| 88 | auto y_end = ((thread_y+1) * size_y) / m_dim0_parts; |
| 89 | |
| 90 | auto x_start = (thread_x * size_x) / m_dim1_parts; |
| 91 | auto x_end = ((thread_x+1) * size_x) / m_dim1_parts; |
| 92 | |
| 93 | kai::ops::ndcoord_t pos = { { y_start, y_end - y_start }, { x_start, x_end - x_start} }; |
| 94 | kai::ops::ndcoord_t thr = { { thread_y, m_dim0_parts }, { thread_x, m_dim1_parts } }; |
| 95 | |
| 96 | if (!(x_end > x_start && y_end > y_start)) { |
| 97 | printf("Warning: no work for thread %u\n", threadid); |
| 98 | return; |
| 99 | } |
| 100 | |
| 101 | // printf("Thread %u, running granule %u x=%u-%u (of %u) y=%u-%u (of %u)\n",threadid, granule_id,x_start,x_end,size_x,y_start,y_end,size_y); |
| 102 | |
| 103 | this->m_gemm->execute(pos, thr, threadid); |
| 104 | } |
| 105 | |
| 106 | void static_scheduler::execute(unsigned int threadid) { |
| 107 | m_granulator.run_granule(threadid, threadid); |
| 108 | } |
| 109 | |
| 110 | #ifndef NO_MULTI_THREADING |
| 111 | |
| 112 | unsigned int dynamic_scheduler::compute_granule_count(kai::ops::IGemmCommon *gemm, unsigned int granule_count) { |
| 113 | // The actual granule count can never exceed the window size of the GEMM. |
| 114 | // We also set these equal if the supplied granule count is 'zero'. Fix that up here. |
| 115 | unsigned int winsize = gemm->get_window_size().total_size(); |
| 116 | |
| 117 | if (granule_count==0 || granule_count > winsize) { |
| 118 | granule_count = winsize; |
| 119 | } |
| 120 | |
| 121 | return granule_count; |
| 122 | } |
| 123 | |
| 124 | dynamic_scheduler::dynamic_scheduler(kai::ops::IGemmCommon *gemm, const unsigned int nthreads, const unsigned int granule_count) |
| 125 | : scheduler(gemm, nthreads), m_pos(0), |
| 126 | m_granule_count(compute_granule_count(gemm, granule_count)), |
| 127 | m_granulator(m_gemm, m_granule_count) |
| 128 | { |
| 129 | } |
| 130 | |
| 131 | void dynamic_scheduler::execute(unsigned int threadid) { |
| 132 | for (;;) { |
| 133 | unsigned int granule = m_pos.fetch_add(1); |
| 134 | |
| 135 | if (granule >= m_granule_count) { |
| 136 | // Every thread will obtain one last granule which is out of |
| 137 | // bounds (indicating that it should exit). This is true even |
| 138 | // if there are races where the other threads complete all the |
| 139 | // work before the last threads start up (in that case the last |
| 140 | // threads will still request one granule, which will be out of |
| 141 | // bounds). |
| 142 | // |
| 143 | // As IDs start from zero, this means the last thread to exit |
| 144 | // will get a granule ID equal to (granule count + thread count) |
| 145 | // - 1. |
| 146 | // |
| 147 | // If that's us, reset the position to zero for the next |
| 148 | // iteration. This is safe, because we get that granule ID iff |
| 149 | // no other threads are going to request one until after we have |
| 150 | // exited (we assume that the GEMM will not be executed again |
| 151 | // until all threads have finished it this time). |
| 152 | if (granule == m_granule_count + this->m_nthreads - 1) { |
| 153 | m_pos=0; |
| 154 | } |
| 155 | |
| 156 | break; |
| 157 | } |
| 158 | |
| 159 | m_granulator.run_granule(granule, threadid); |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | #define FIFO_SIZE 16 |
| 164 | |
| 165 | class taskmaster_scheduler::thread_queue { |
| 166 | private: |
| 167 | // Class to maintain a head/tail pointer that should occupy its own |
| 168 | // cache line (assumes 64 byte cache lines) |
| 169 | class alignas(64) ht_ptr { |
| 170 | private: |
| 171 | volatile uint64_t m_value; |
| 172 | |
| 173 | public: |
| 174 | ht_ptr() : m_value(0) { } |
| 175 | |
| 176 | uint64_t get() const { |
| 177 | return m_value; |
| 178 | } |
| 179 | |
| 180 | uint64_t get_and_increment() { |
| 181 | return m_value++; |
| 182 | } |
| 183 | }; |
| 184 | |
| 185 | static_assert(alignof(ht_ptr) == 64, "ht_ptr must be cache-line aligned"); |
| 186 | static_assert(sizeof(ht_ptr) == 64, "ht_ptr must occupy one cache line"); |
| 187 | |
| 188 | ht_ptr m_head; |
| 189 | ht_ptr m_tail; |
| 190 | unsigned int m_fifo[FIFO_SIZE] = {}; |
| 191 | |
| 192 | public: |
| 193 | thread_queue() : m_head(), m_tail() { } |
| 194 | |
| 195 | unsigned int size() { |
| 196 | return m_head.get() - m_tail.get(); |
| 197 | } |
| 198 | |
| 199 | void additem(unsigned int v) { |
| 200 | m_fifo[m_head.get_and_increment() % FIFO_SIZE] = v; |
| 201 | } |
| 202 | |
| 203 | unsigned int popitem() { |
| 204 | assert(size() > 0); |
| 205 | return m_fifo[m_tail.get_and_increment() % FIFO_SIZE]; |
| 206 | } |
| 207 | }; |
| 208 | |
| 209 | bool taskmaster_scheduler::populate_queues() { |
| 210 | if (m_pos >= m_granule_count) { |
| 211 | // Reset logic: Every thread will call into this function with the |
| 212 | // lock held, as a "true" return from here is the only way out of |
| 213 | // the main loop. |
| 214 | // |
| 215 | // So just count the threads in, when the last one completes reset |
| 216 | // the completion count and the position. |
| 217 | m_completed_count++; |
| 218 | |
| 219 | if (m_completed_count == this->m_nthreads) { |
| 220 | m_completed_count = 0; |
| 221 | m_pos = 0; |
| 222 | } |
| 223 | |
| 224 | return true; |
| 225 | } |
| 226 | |
| 227 | // Cache the sizes here. |
| 228 | std::vector<unsigned int> sizes(this->m_nthreads); |
| 229 | |
| 230 | for (unsigned int t=0; t<this->m_nthreads; t++) { |
| 231 | sizes[t] = m_queues[t]->size(); |
| 232 | } |
| 233 | |
| 234 | // "Deal" out work to the shortest queues first. |
| 235 | for (unsigned int s=0; s<(FIFO_SIZE-1); s++) { |
| 236 | for (unsigned int t=0; t<this->m_nthreads; t++) { |
| 237 | if (sizes[t] <= s) { |
| 238 | m_queues[t]->additem(m_pos++); |
| 239 | |
| 240 | if (m_pos >= m_granule_count) { |
| 241 | return false; |
| 242 | } |
| 243 | } |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | return false; |
| 248 | } |
| 249 | |
| 250 | unsigned int taskmaster_scheduler::compute_granule_count(kai::ops::IGemmCommon *gemm, unsigned int granule_count) { |
| 251 | unsigned int winsize = gemm->get_window_size().total_size(); |
| 252 | |
| 253 | if (granule_count == 0 || granule_count > winsize) { |
| 254 | granule_count = winsize; |
| 255 | } |
| 256 | |
| 257 | return granule_count; |
| 258 | } |
| 259 | |
| 260 | taskmaster_scheduler::taskmaster_scheduler(kai::ops::IGemmCommon *gemm, const unsigned int nthreads, const unsigned int granule_count) |
| 261 | : scheduler(gemm, nthreads), m_pos(0), m_granule_count(compute_granule_count(gemm, granule_count)), |
| 262 | m_granulator(m_gemm, m_granule_count) { |
| 263 | printf("Taskmaster scheduler initialized; %u granules.\n", m_granule_count); |
| 264 | |
| 265 | m_queues.resize(nthreads); |
| 266 | |
| 267 | m_threadmem = allocate_aligned_memory( 64, sizeof(thread_queue) * nthreads ); |
| 268 | for (unsigned int i=0; i<nthreads; i++) { |
| 269 | m_queues[i] = new (static_cast<thread_queue *>(m_threadmem) + i) thread_queue(); |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | taskmaster_scheduler::~taskmaster_scheduler() { |
| 274 | for(unsigned int i=0; i<this->m_nthreads; i++) { |
| 275 | m_queues[i]->~thread_queue(); |
| 276 | } |
| 277 | |
| 278 | free_aligned_memory(this->m_threadmem); |
| 279 | } |
| 280 | |
| 281 | void taskmaster_scheduler::execute(unsigned int threadid) { |
| 282 | for(;;) { |
| 283 | if (m_queues[threadid]->size() > 0) { |
| 284 | unsigned int v = m_queues[threadid]->popitem(); |
| 285 | |
| 286 | m_granulator.run_granule(v, threadid); |
| 287 | |
| 288 | continue; |
| 289 | } |
| 290 | |
| 291 | if (m_lock.try_lock()) { |
| 292 | bool abort = false; |
| 293 | |
| 294 | if (m_queues[threadid]->size() == 0) { |
| 295 | abort = populate_queues(); |
| 296 | } |
| 297 | |
| 298 | m_lock.unlock(); |
| 299 | |
| 300 | if (abort) |
| 301 | break; |
| 302 | } |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | #endif |