Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2016, 2017 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 | #ifndef __ARM_COMPUTE_CLSCHEDULER_H__ |
| 25 | #define __ARM_COMPUTE_CLSCHEDULER_H__ |
| 26 | |
| 27 | #include "arm_compute/core/CL/CLHelpers.h" |
| 28 | #include "arm_compute/core/CL/CLKernelLibrary.h" |
| 29 | #include "arm_compute/core/CL/CLTypes.h" |
| 30 | #include "arm_compute/core/CL/OpenCL.h" |
| 31 | #include "arm_compute/core/Error.h" |
| 32 | #include "arm_compute/core/Types.h" |
| 33 | |
| 34 | namespace arm_compute |
| 35 | { |
| 36 | class ICLKernel; |
| 37 | |
| 38 | /** Provides global access to a CL context and command queue. */ |
| 39 | class CLScheduler |
| 40 | { |
| 41 | private: |
| 42 | /** Constructor */ |
| 43 | CLScheduler(); |
| 44 | |
| 45 | public: |
| 46 | /** Access the scheduler singleton. |
| 47 | * |
| 48 | * @return The scheduler |
| 49 | */ |
| 50 | static CLScheduler &get(); |
| 51 | /** Initialises the context and command queue used by the scheduler to default values |
| 52 | * and sets a default device and kernel path for the @ref CLKernelLibrary. |
| 53 | */ |
| 54 | void default_init() |
| 55 | { |
| 56 | CLKernelLibrary::get().init("./cl_kernels/", cl::Context::getDefault(), cl::Device::getDefault()); |
| 57 | init(cl::Context::getDefault(), cl::CommandQueue::getDefault(), cl::Device::getDefault()); |
| 58 | } |
| 59 | /** Schedule the execution of the passed kernel if possible. |
| 60 | * |
| 61 | * @param[in] kernel Kernel to execute. |
| 62 | * @param[in] flush (Optional) Specifies if the command queue will be flushed after running the kernel. |
| 63 | */ |
| 64 | void enqueue(ICLKernel &kernel, bool flush = true); |
| 65 | |
| 66 | /** Initialises the context and command queue to be used by the scheduler. |
| 67 | * |
| 68 | * @param[in] context A CL context. |
| 69 | * @param[in] queue A CL command queue. |
| 70 | * @param[in] device A CL device. |
| 71 | */ |
| 72 | void init(cl::Context context = cl::Context::getDefault(), cl::CommandQueue queue = cl::CommandQueue::getDefault(), |
| 73 | cl::Device device = cl::Device::getDefault()) |
| 74 | { |
steniu01 | 3470247 | 2017-07-11 09:22:58 +0100 | [diff] [blame] | 75 | _context = std::move(context); |
| 76 | _queue = std::move(queue); |
| 77 | _target = get_target_from_device(device); |
| 78 | _is_initialised = true; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | /** Accessor for the associated CL context. |
| 82 | * |
| 83 | * @return A CL context. |
| 84 | */ |
| 85 | cl::Context &context() |
| 86 | { |
steniu01 | 3470247 | 2017-07-11 09:22:58 +0100 | [diff] [blame] | 87 | ARM_COMPUTE_ERROR_ON(!_is_initialised); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 88 | return _context; |
| 89 | } |
| 90 | |
| 91 | /** Accessor to set the CL context to be used by the scheduler. |
| 92 | * |
| 93 | * @param[in] context A CL context. |
| 94 | */ |
| 95 | void set_context(cl::Context context) |
| 96 | { |
| 97 | _context = std::move(context); |
| 98 | } |
| 99 | |
| 100 | /** Accessor for the associated CL command queue. |
| 101 | * |
| 102 | * @return A CL command queue. |
| 103 | */ |
| 104 | cl::CommandQueue &queue() |
| 105 | { |
steniu01 | 3470247 | 2017-07-11 09:22:58 +0100 | [diff] [blame] | 106 | ARM_COMPUTE_ERROR_ON(!_is_initialised); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 107 | return _queue; |
| 108 | } |
| 109 | |
| 110 | /** Get the target GPU. |
| 111 | * |
| 112 | * @return The target GPU. |
| 113 | */ |
| 114 | GPUTarget target() const |
| 115 | { |
| 116 | return _target; |
| 117 | } |
| 118 | |
| 119 | /** Accessor to set the CL command queue to be used by the scheduler. |
| 120 | * |
| 121 | * @param[in] queue A CL command queue. |
| 122 | */ |
| 123 | void set_queue(cl::CommandQueue queue) |
| 124 | { |
| 125 | _queue = std::move(queue); |
| 126 | } |
| 127 | |
| 128 | /** Accessor to set target GPU to be used by the scheduler. |
| 129 | * |
| 130 | * @param[in] target The target GPU. |
| 131 | */ |
| 132 | void set_target(GPUTarget target) |
| 133 | { |
| 134 | _target = target; |
| 135 | } |
| 136 | |
| 137 | /** Blocks until all commands in the associated command queue have finished. */ |
| 138 | void sync() |
| 139 | { |
| 140 | _queue.finish(); |
| 141 | } |
| 142 | |
| 143 | /** Enqueues a marker into the associated command queue and return the event. |
| 144 | * |
| 145 | * @return An event that can be waited on to block the executing thread. |
| 146 | */ |
| 147 | cl::Event enqueue_sync_event() |
| 148 | { |
| 149 | cl::Event event; |
| 150 | _queue.enqueueMarker(&event); |
| 151 | |
| 152 | return event; |
| 153 | } |
| 154 | |
| 155 | private: |
| 156 | cl::Context _context; |
| 157 | cl::CommandQueue _queue; |
| 158 | GPUTarget _target; |
steniu01 | 3470247 | 2017-07-11 09:22:58 +0100 | [diff] [blame] | 159 | bool _is_initialised; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 160 | }; |
| 161 | } |
| 162 | #endif /* __ARM_COMPUTE_CLSCHEDULER_H__ */ |