| 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 | #pragma once |
| 8 | |
| 9 | #include <cstdio> |
| 10 | #include <functional> |
| 11 | |
| 12 | #include "kai/ops/gemm/kai_ops.hpp" |
| 13 | |
| 14 | #include "kai/ops/gemm/ndrange.hpp" |
| 15 | |
| 16 | /* |
| 17 | * Class to handle (1D or) 2D splits for kai_ops kernels. |
| 18 | * |
| 19 | * This is used for static scheduling (to divide into threads) and dynamic |
| 20 | * (to divide into granules). |
| 21 | */ |
| 22 | class granulator { |
| 23 | private: |
| 24 | kai::ops::IGemmCommon * const m_gemm; |
| 25 | unsigned int m_nthreads; |
| 26 | unsigned int m_dim0_parts; |
| 27 | unsigned int m_dim1_parts; |
| 28 | |
| 29 | static unsigned int split_2d(unsigned int max_threads, const kai::ops::ndrange_t &window_size, const unsigned int dim0_override); |
| 30 | |
| 31 | public: |
| 32 | granulator(kai::ops::IGemmCommon *gemm, const unsigned int nthreads, const unsigned int dim0_override=0) : |
| 33 | m_gemm(gemm), |
| 34 | m_nthreads(nthreads), |
| 35 | m_dim0_parts(split_2d(nthreads, gemm->get_window_size(), dim0_override)), |
| 36 | m_dim1_parts(nthreads / m_dim0_parts) |
| 37 | { |
| 38 | auto range = m_gemm->get_window_size(); |
| 39 | unsigned int max_threads = std::min<unsigned int>(m_dim0_parts,range.get_size(0)) * std::min<unsigned int>(m_dim1_parts, range.get_size(1)); |
| 40 | #ifndef SILENT |
| 41 | unsigned int orig_threads = m_nthreads; |
| 42 | #endif |
| 43 | |
| 44 | if (max_threads < m_nthreads) { |
| 45 | m_dim0_parts = std::min(m_dim0_parts, range.get_size(0)); |
| 46 | m_dim1_parts = std::min(m_dim1_parts, range.get_size(1)); |
| 47 | |
| 48 | m_nthreads = max_threads; |
| 49 | gemm->set_nthreads(max_threads); |
| 50 | } |
| 51 | |
| 52 | #ifndef SILENT |
| 53 | if(orig_threads > m_nthreads) { |
| 54 | printf("Granulator: Clamping threads to %u (was %u).\n",m_nthreads,orig_threads); |
| 55 | } |
| 56 | printf("Set up scheduling: %ux%u granules (%ux%u total size).\n",m_dim1_parts,m_dim0_parts,range.get_size(1),range.get_size(0)); |
| 57 | #endif |
| 58 | } |
| 59 | |
| 60 | void run_granule(unsigned int granule_id, unsigned int threadid); |
| 61 | }; |
| 62 | |
| 63 | /* |
| 64 | * Semi-abstract 'scheduler' class. |
| 65 | * |
| 66 | * All implementations will want to capture the gemm pointer and the thread |
| 67 | * count, so store those in here. |
| 68 | */ |
| 69 | class scheduler { |
| 70 | protected: |
| 71 | kai::ops::IGemmCommon * const m_gemm; |
| 72 | const unsigned int m_nthreads; |
| 73 | |
| 74 | public: |
| 75 | scheduler(kai::ops::IGemmCommon *gemm, const unsigned int nthreads) : |
| 76 | m_gemm(gemm), m_nthreads(nthreads) { } |
| 77 | |
| 78 | virtual void execute(unsigned int threadid) = 0; |
| 79 | |
| 80 | virtual ~scheduler() { } |
| 81 | }; |
| 82 | |
| 83 | /* Static scheduler just divides work across threads. */ |
| 84 | class static_scheduler : public scheduler { |
| 85 | private: |
| 86 | granulator m_granulator; |
| 87 | |
| 88 | public: |
| 89 | static_scheduler(kai::ops::IGemmCommon *gemm, const unsigned int nthreads, const unsigned int dim0_override=0) : |
| 90 | scheduler(gemm, nthreads), m_granulator(m_gemm, m_nthreads, dim0_override) { } |
| 91 | |
| 92 | virtual void execute(unsigned int threadid) override; |
| 93 | }; |
| 94 | |
| 95 | #ifndef NO_MULTI_THREADING |
| 96 | |
| 97 | #include <atomic> |
| 98 | #include <mutex> |
| 99 | |
| 100 | /* |
| 101 | * Dynamic scheduler also needs to track scheduling granules: _pos indicates |
| 102 | * the next granule to be processed, and _granule_count the total number of |
| 103 | * granules in the workload. |
| 104 | */ |
| 105 | class dynamic_scheduler : public scheduler { |
| 106 | private: |
| 107 | std::atomic<unsigned int> m_pos; |
| 108 | unsigned int m_granule_count; |
| 109 | granulator m_granulator; |
| 110 | |
| 111 | static unsigned int compute_granule_count(kai::ops::IGemmCommon *, unsigned int); |
| 112 | |
| 113 | public: |
| 114 | /* Non-trivial constructor to sort of the granule count. */ |
| 115 | dynamic_scheduler(kai::ops::IGemmCommon *gemm, const unsigned int nthreads, const unsigned int granule_count=0); |
| 116 | |
| 117 | virtual void execute(unsigned int threadid) override; |
| 118 | }; |
| 119 | |
| 120 | class taskmaster_scheduler : public scheduler { |
| 121 | private: |
| 122 | class thread_queue; |
| 123 | |
| 124 | bool populate_queues(); |
| 125 | |
| 126 | void *m_threadmem; |
| 127 | std::vector<thread_queue *> m_queues; |
| 128 | std::mutex m_lock; |
| 129 | volatile unsigned int m_pos=0; |
| 130 | unsigned int m_granule_count; |
| 131 | unsigned int m_completed_count=0; |
| 132 | granulator m_granulator; |
| 133 | |
| 134 | static unsigned int compute_granule_count(kai::ops::IGemmCommon *, unsigned int); |
| 135 | |
| 136 | public: |
| 137 | taskmaster_scheduler(kai::ops::IGemmCommon *gemm, const unsigned int nthreads, const unsigned int granule_count=0); |
| 138 | ~taskmaster_scheduler() override; |
| 139 | |
| 140 | void execute(unsigned int threadid) override; |
| 141 | }; |
| 142 | |
| 143 | #endif |