1*c217d954SCole Faust /* 2*c217d954SCole Faust * Copyright (c) 2016-2022 Arm Limited. 3*c217d954SCole Faust * 4*c217d954SCole Faust * SPDX-License-Identifier: MIT 5*c217d954SCole Faust * 6*c217d954SCole Faust * Permission is hereby granted, free of charge, to any person obtaining a copy 7*c217d954SCole Faust * of this software and associated documentation files (the "Software"), to 8*c217d954SCole Faust * deal in the Software without restriction, including without limitation the 9*c217d954SCole Faust * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 10*c217d954SCole Faust * sell copies of the Software, and to permit persons to whom the Software is 11*c217d954SCole Faust * furnished to do so, subject to the following conditions: 12*c217d954SCole Faust * 13*c217d954SCole Faust * The above copyright notice and this permission notice shall be included in all 14*c217d954SCole Faust * copies or substantial portions of the Software. 15*c217d954SCole Faust * 16*c217d954SCole Faust * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17*c217d954SCole Faust * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18*c217d954SCole Faust * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19*c217d954SCole Faust * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20*c217d954SCole Faust * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21*c217d954SCole Faust * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22*c217d954SCole Faust * SOFTWARE. 23*c217d954SCole Faust */ 24*c217d954SCole Faust #ifndef ARM_COMPUTE_CLSCHEDULER_H 25*c217d954SCole Faust #define ARM_COMPUTE_CLSCHEDULER_H 26*c217d954SCole Faust 27*c217d954SCole Faust #include "arm_compute/core/CL/CLHelpers.h" 28*c217d954SCole Faust #include "arm_compute/core/CL/CLTypes.h" 29*c217d954SCole Faust #include "arm_compute/core/CL/OpenCL.h" 30*c217d954SCole Faust #include "arm_compute/core/Error.h" 31*c217d954SCole Faust #include "arm_compute/core/Types.h" 32*c217d954SCole Faust #include "arm_compute/core/experimental/Types.h" 33*c217d954SCole Faust #include "arm_compute/runtime/CL/CLGEMMHeuristicsHandle.h" 34*c217d954SCole Faust #include "arm_compute/runtime/CL/CLHelpers.h" 35*c217d954SCole Faust #include "arm_compute/runtime/CL/CLTypes.h" 36*c217d954SCole Faust #include "arm_compute/runtime/CL/ICLTuner.h" 37*c217d954SCole Faust 38*c217d954SCole Faust namespace arm_compute 39*c217d954SCole Faust { 40*c217d954SCole Faust class ICLKernel; 41*c217d954SCole Faust class ICLTuner; 42*c217d954SCole Faust /** Provides global access to a CL context and command queue. */ 43*c217d954SCole Faust class CLScheduler final 44*c217d954SCole Faust { 45*c217d954SCole Faust public: 46*c217d954SCole Faust /** Constructor */ 47*c217d954SCole Faust CLScheduler(); 48*c217d954SCole Faust /** Prevent instances of this class from being copied (As this class contains pointers) */ 49*c217d954SCole Faust CLScheduler(const CLScheduler &) = delete; 50*c217d954SCole Faust /** Prevent instances of this class from being copied (As this class contains pointers) */ 51*c217d954SCole Faust CLScheduler &operator=(const CLScheduler &) = delete; 52*c217d954SCole Faust /** Default destructor */ 53*c217d954SCole Faust ~CLScheduler() = default; 54*c217d954SCole Faust /** Access the scheduler singleton. 55*c217d954SCole Faust * This method has been deprecated and will be removed in future releases 56*c217d954SCole Faust * @return The scheduler 57*c217d954SCole Faust */ 58*c217d954SCole Faust static CLScheduler &get(); 59*c217d954SCole Faust /** Initialises the context and command queue used by the scheduler to default values 60*c217d954SCole Faust * and sets a default device and kernel path for the @ref CLKernelLibrary. 61*c217d954SCole Faust * 62*c217d954SCole Faust * @param[in] cl_tuner (Optional) Pointer to ICLTuner (default=nullptr) 63*c217d954SCole Faust * @param[in] gemm_h (Optional) Pointer to CLGEMMHeuristicsHandle (default = nullptr) 64*c217d954SCole Faust * @param[in] cl_backend_type (Optional) Type of backend to use (default = CLBackendType::Native) 65*c217d954SCole Faust */ 66*c217d954SCole Faust void default_init(ICLTuner *cl_tuner = nullptr, CLGEMMHeuristicsHandle *gemm_h = nullptr, CLBackendType cl_backend_type = CLBackendType::Native); 67*c217d954SCole Faust /** Initialises the scheduler with context and device provided by the user 68*c217d954SCole Faust * 69*c217d954SCole Faust * @param[in] device OpenCL device to be used 70*c217d954SCole Faust * @param[in] ctx OpenCL ctx to be used 71*c217d954SCole Faust * @param[in] cl_tuner (Optional) Pointer to ICLTuner (default=nullptr) 72*c217d954SCole Faust * @param[in] gemm_h (Optional) Pointer to CLGEMMHeuristicsHandle (default = nullptr) 73*c217d954SCole Faust */ 74*c217d954SCole Faust void default_init_with_context(cl::Device &device, cl::Context &ctx, ICLTuner *cl_tuner = nullptr, CLGEMMHeuristicsHandle *gemm_h = nullptr); 75*c217d954SCole Faust 76*c217d954SCole Faust /** Re-initializes the context and command queue used by the scheduler to default values 77*c217d954SCole Faust * and sets a default device and kernel path for the @ref CLKernelLibrary. 78*c217d954SCole Faust * 79*c217d954SCole Faust * @param[in] cl_tuner (Optional) Pointer to ICLTuner (default=nullptr) 80*c217d954SCole Faust * @param[in] gemm_h (Optional) Pointer to CLGEMMHeuristicsHandle (default = nullptr) 81*c217d954SCole Faust * @param[in] cl_backend_type (Optional) Type of backend to use (default = CLBackendType::Native) 82*c217d954SCole Faust */ 83*c217d954SCole Faust void default_reinit(ICLTuner *cl_tuner = nullptr, CLGEMMHeuristicsHandle *gemm_h = nullptr, CLBackendType cl_backend_type = CLBackendType::Native); 84*c217d954SCole Faust 85*c217d954SCole Faust /** Schedule the execution of the passed kernel if possible. 86*c217d954SCole Faust * 87*c217d954SCole Faust * @param[in] kernel Kernel to execute. 88*c217d954SCole Faust * @param[in] flush (Optional) Specifies if the command queue will be flushed after running the kernel. This will be ignored if job chaining is enabled. 89*c217d954SCole Faust */ 90*c217d954SCole Faust void enqueue(ICLKernel &kernel, bool flush = true); 91*c217d954SCole Faust /** Schedule the execution of the passed kernel if possible. 92*c217d954SCole Faust * 93*c217d954SCole Faust * @param[in] kernel Kernel to execute. 94*c217d954SCole Faust * @param[in] tensors Vector containing the tensors to operate on. 95*c217d954SCole Faust * @param[in] flush (Optional) Specifies if the command queue will be flushed after running the kernel. This will be ignored if job chaining is enabled. 96*c217d954SCole Faust */ 97*c217d954SCole Faust void enqueue_op(ICLKernel &kernel, ITensorPack &tensors, bool flush = true); 98*c217d954SCole Faust /** Initialises the context and command queue to be used by the scheduler. 99*c217d954SCole Faust * 100*c217d954SCole Faust * @param[in] context A CL context. 101*c217d954SCole Faust * @param[in] queue A CL command queue. 102*c217d954SCole Faust * @param[in] device A CL device. 103*c217d954SCole Faust * @param[in] cl_tuner (Optional) Pointer to OpenCL tuner (default=nullptr) 104*c217d954SCole Faust * Note: It is caller's responsibility to release the allocated memory for CLTuner 105*c217d954SCole Faust * @param[in] gemm_h (Optional) Pointer to CLGEMMHeuristicsHandle (default = nullptr) 106*c217d954SCole Faust * @param[in] cl_backend_type (Optional) Type of backend to use (default = CLBackendType::Native) 107*c217d954SCole Faust */ 108*c217d954SCole Faust void init(cl::Context context, cl::CommandQueue queue, const cl::Device &device, ICLTuner *cl_tuner = nullptr, CLGEMMHeuristicsHandle *gemm_h = nullptr, 109*c217d954SCole Faust CLBackendType cl_backend_type = CLBackendType::Native); 110*c217d954SCole Faust 111*c217d954SCole Faust /** Accessor for the associated CL context. 112*c217d954SCole Faust * 113*c217d954SCole Faust * @return A CL context. 114*c217d954SCole Faust */ 115*c217d954SCole Faust cl::Context &context(); 116*c217d954SCole Faust 117*c217d954SCole Faust /** Accessor for the associated CL command queue. 118*c217d954SCole Faust * 119*c217d954SCole Faust * @return A CL command queue. 120*c217d954SCole Faust */ 121*c217d954SCole Faust cl::CommandQueue &queue(); 122*c217d954SCole Faust 123*c217d954SCole Faust /** Get the target GPU. 124*c217d954SCole Faust * 125*c217d954SCole Faust * @return The target GPU. 126*c217d954SCole Faust */ 127*c217d954SCole Faust GPUTarget target() const; 128*c217d954SCole Faust 129*c217d954SCole Faust /** Accessor for the associated CLGEMMHeuristicsHandle 130*c217d954SCole Faust * 131*c217d954SCole Faust * @return Pointer to CLGEMMHeuristicsHandle 132*c217d954SCole Faust */ 133*c217d954SCole Faust CLGEMMHeuristicsHandle *gemm_heuristics() const; 134*c217d954SCole Faust 135*c217d954SCole Faust /** Accessor to set the CL context to be used by the scheduler. 136*c217d954SCole Faust * 137*c217d954SCole Faust * @param[in] context A CL context. 138*c217d954SCole Faust */ 139*c217d954SCole Faust void set_context(cl::Context context); 140*c217d954SCole Faust 141*c217d954SCole Faust /** Accessor to set the CL command queue to be used by the scheduler. 142*c217d954SCole Faust * 143*c217d954SCole Faust * @param[in] queue A CL command queue. 144*c217d954SCole Faust */ 145*c217d954SCole Faust void set_queue(cl::CommandQueue queue); 146*c217d954SCole Faust 147*c217d954SCole Faust /** Accessor to set target GPU to be used by the scheduler. 148*c217d954SCole Faust * 149*c217d954SCole Faust * @param[in] target The target GPU. 150*c217d954SCole Faust */ 151*c217d954SCole Faust void set_target(GPUTarget target); 152*c217d954SCole Faust 153*c217d954SCole Faust /** Accessor to set the CL tuner to be used by the scheduler. 154*c217d954SCole Faust * 155*c217d954SCole Faust * @param[in] tuner A CL tuner 156*c217d954SCole Faust */ 157*c217d954SCole Faust void set_tuner(ICLTuner *tuner); 158*c217d954SCole Faust 159*c217d954SCole Faust /** Blocks until all commands in the associated command queue have finished. */ 160*c217d954SCole Faust void sync(); 161*c217d954SCole Faust 162*c217d954SCole Faust /** Enqueues a marker into the associated command queue and return the event. 163*c217d954SCole Faust * 164*c217d954SCole Faust * @return An event that can be waited on to block the executing thread. 165*c217d954SCole Faust */ 166*c217d954SCole Faust cl::Event enqueue_sync_event(); 167*c217d954SCole Faust 168*c217d954SCole Faust /** Tunes OpenCL kernel 169*c217d954SCole Faust * 170*c217d954SCole Faust * @param[in] kernel Kernel to tune 171*c217d954SCole Faust */ 172*c217d954SCole Faust void tune_kernel_static(ICLKernel &kernel); 173*c217d954SCole Faust 174*c217d954SCole Faust /** Enable job chaining. The command queue will only be flushed when @p job_chaining_size kernels have been enqueued. 175*c217d954SCole Faust * 176*c217d954SCole Faust * @param[in] job_chaining_size Kernels to enqueue before flushing 177*c217d954SCole Faust */ 178*c217d954SCole Faust void enable_job_chaining(int job_chaining_size); 179*c217d954SCole Faust 180*c217d954SCole Faust bool is_initialised() const; 181*c217d954SCole Faust 182*c217d954SCole Faust private: 183*c217d954SCole Faust void enqueue_common(ICLKernel &kernel, ITensorPack &tensors, bool flush); 184*c217d954SCole Faust /** If job chain is disabled, then flush the command queue according to @p flush. Otherwise @p flush is ignored and the queue is only flushed when job chain count exceeds allocated job chain size 185*c217d954SCole Faust * 186*c217d954SCole Faust * @param[in] flush Flush the command queue. Ignored when job chain is enabled. 187*c217d954SCole Faust */ 188*c217d954SCole Faust void flush_queue(bool flush); 189*c217d954SCole Faust 190*c217d954SCole Faust /** Flag to ensure symbols initialisation is happening before Scheduler creation */ 191*c217d954SCole Faust static std::once_flag _initialize_symbols; 192*c217d954SCole Faust 193*c217d954SCole Faust cl::Context _context; 194*c217d954SCole Faust cl::CommandQueue _queue; 195*c217d954SCole Faust GPUTarget _target; 196*c217d954SCole Faust bool _is_initialised; 197*c217d954SCole Faust ICLTuner *_cl_tuner; 198*c217d954SCole Faust CLGEMMHeuristicsHandle *_gemm_heuristics; 199*c217d954SCole Faust CLBackendType _backend_type; 200*c217d954SCole Faust bool _job_chaining_enabled; 201*c217d954SCole Faust int _job_chaining_size; 202*c217d954SCole Faust int _job_chaining_count; 203*c217d954SCole Faust }; 204*c217d954SCole Faust } // namespace arm_compute 205*c217d954SCole Faust #endif /* ARM_COMPUTE_CLSCHEDULER_H */ 206