From db8485ac24135f17e9882c76196924435abc064f Mon Sep 17 00:00:00 2001 From: Pablo Tello Date: Tue, 24 Sep 2019 11:03:47 +0100 Subject: COMPMID-2205: CL runtime context. CL Interfaces implemented. Concrete classes implemented. One test (ActivationLayer) ported to the new interface. Change-Id: I283808bec36ccfc2f13fe048c45cbbee698ce525 Signed-off-by: Pablo Tello Reviewed-on: https://review.mlplatform.org/c/1998 Tested-by: Arm Jenkins Reviewed-by: Georgios Pinitas Comments-Addressed: Arm Jenkins --- arm_compute/core/CL/CLCoreRuntimeContext.h | 63 +++++++++++++++++ arm_compute/core/CL/CLHelpers.h | 13 ++++ arm_compute/core/CL/CLKernelLibrary.h | 78 +++++----------------- arm_compute/core/CL/OpenCL.h | 3 +- .../core/CL/kernels/CLActivationLayerKernel.h | 11 +-- arm_compute/graph/backends/CL/CLDeviceBackend.h | 10 +-- arm_compute/runtime/CL/CLBufferAllocator.h | 20 ++++-- arm_compute/runtime/CL/CLHelpers.h | 10 +++ arm_compute/runtime/CL/CLMemoryRegion.h | 33 ++++----- arm_compute/runtime/CL/CLRuntimeContext.h | 70 +++++++++++++++++++ arm_compute/runtime/CL/CLScheduler.h | 74 +++++--------------- arm_compute/runtime/CL/CLTensor.h | 27 +++++++- arm_compute/runtime/CL/CLTensorAllocator.h | 6 +- arm_compute/runtime/CL/ICLSimpleFunction.h | 20 +++++- .../runtime/CL/functions/CLActivationLayer.h | 2 +- arm_compute/runtime/GLES_COMPUTE/GCTensor.h | 9 ++- arm_compute/runtime/Tensor.h | 10 ++- 17 files changed, 292 insertions(+), 167 deletions(-) create mode 100644 arm_compute/core/CL/CLCoreRuntimeContext.h create mode 100644 arm_compute/runtime/CL/CLRuntimeContext.h (limited to 'arm_compute') diff --git a/arm_compute/core/CL/CLCoreRuntimeContext.h b/arm_compute/core/CL/CLCoreRuntimeContext.h new file mode 100644 index 0000000000..6e2bd43d53 --- /dev/null +++ b/arm_compute/core/CL/CLCoreRuntimeContext.h @@ -0,0 +1,63 @@ +/* + * Copyright (c) 2019 ARM Limited. + * + * SPDX-License-Identifier: MIT + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#ifndef __ARM_COMPUTE_CLCORERUNTIME_CONTEXT_H__ +#define __ARM_COMPUTE_CLCORERUNTIME_CONTEXT_H__ + +#include "arm_compute/core/CL/OpenCL.h" + +namespace arm_compute +{ +class CLKernelLibrary; +/** Core runtime context */ +class CLCoreRuntimeContext final +{ +public: + /** Legacy constructor */ + CLCoreRuntimeContext(); + + /** Constructor */ + CLCoreRuntimeContext(CLKernelLibrary *kernel_lib, cl::Context ctx, cl::CommandQueue queue); + /** Destructor */ + ~CLCoreRuntimeContext() = default; + /** Default copy constructor */ + CLCoreRuntimeContext(const CLCoreRuntimeContext &) = default; + /** Default move constructor */ + CLCoreRuntimeContext(CLCoreRuntimeContext &&) = default; + /** Default copy assignment */ + CLCoreRuntimeContext &operator=(const CLCoreRuntimeContext &) = default; + /** Default move assignment operator */ + CLCoreRuntimeContext &operator=(CLCoreRuntimeContext &&) = default; + /** CPU Scheduler setter */ + + CLKernelLibrary *kernel_library() const; + cl::Context context(); + cl::CommandQueue queue(); + +private: + CLKernelLibrary *_kernel_lib{ nullptr }; + cl::Context _ctx{}; + cl::CommandQueue _queue{}; +}; +} // namespace arm_compute +#endif /*__ARM_COMPUTE_CLCORERUNTIME_CONTEXT_H__ */ diff --git a/arm_compute/core/CL/CLHelpers.h b/arm_compute/core/CL/CLHelpers.h index 16fe09fb96..1d647a86b0 100644 --- a/arm_compute/core/CL/CLHelpers.h +++ b/arm_compute/core/CL/CLHelpers.h @@ -34,6 +34,9 @@ namespace arm_compute { +class CLCoreRuntimeContext; +class CLBuildOptions; + enum class DataType; /** Max vector width of an OpenCL vector */ @@ -153,5 +156,15 @@ size_t preferred_vector_width(const cl::Device &device, DataType dt); * @return True if dummy work-items should be preferred to dispatch the NDRange */ bool preferred_dummy_work_items_support(const cl::Device &device); + +/** Creates an opencl kernel + * + * @param[in] ctx A context to be used to create the opencl kernel. + * @param[in] kernel_name The kernel name. + * @param[in] build_opts The build options to be used for the opencl kernel compilation. + * + * @return An opencl kernel + */ +cl::Kernel create_opencl_kernel(CLCoreRuntimeContext *ctx, const std::string &kernel_name, const CLBuildOptions &build_opts); } #endif /* __ARM_COMPUTE_CLHELPERS_H__ */ diff --git a/arm_compute/core/CL/CLKernelLibrary.h b/arm_compute/core/CL/CLKernelLibrary.h index 9f183f1232..f2282692f9 100644 --- a/arm_compute/core/CL/CLKernelLibrary.h +++ b/arm_compute/core/CL/CLKernelLibrary.h @@ -34,7 +34,7 @@ namespace arm_compute { /** Build options */ -class CLBuildOptions +class CLBuildOptions final { using StringSet = std::set; @@ -80,7 +80,7 @@ private: StringSet _build_opts; /**< Build options set */ }; /** Program class */ -class Program +class Program final { public: /** Default constructor. */ @@ -147,7 +147,7 @@ private: }; /** Kernel class */ -class Kernel +class Kernel final { public: /** Default Constructor. */ @@ -189,20 +189,19 @@ private: }; /** CLKernelLibrary class */ -class CLKernelLibrary +class CLKernelLibrary final { using StringSet = std::set; -private: +public: /** Default Constructor. */ CLKernelLibrary(); - -public: /** Prevent instances of this class from being copied */ CLKernelLibrary(const CLKernelLibrary &) = delete; /** Prevent instances of this class from being copied */ const CLKernelLibrary &operator=(const CLKernelLibrary &) = delete; /** Access the KernelLibrary singleton. + * This method has been deprecated and will be removed in the next release. * @return The KernelLibrary instance. */ static CLKernelLibrary &get(); @@ -212,26 +211,15 @@ public: * @param[in] context CL context used to create programs. * @param[in] device CL device for which the programs are created. */ - void init(std::string kernel_path, cl::Context context, cl::Device device) - { - _kernel_path = std::move(kernel_path); - _context = std::move(context); - _device = std::move(device); - } + void init(std::string kernel_path, cl::Context context, cl::Device device); /** Sets the path that the kernels reside in. * * @param[in] kernel_path Path of the kernel. */ - void set_kernel_path(const std::string &kernel_path) - { - _kernel_path = kernel_path; - }; + void set_kernel_path(const std::string &kernel_path); /** Gets the path that the kernels reside in. */ - std::string get_kernel_path() - { - return _kernel_path; - }; + std::string get_kernel_path(); /** Gets the source of the selected program. * * @param[in] program_name Program name. @@ -246,51 +234,22 @@ public: * * @param[in] context A CL context. */ - void set_context(cl::Context context) - { - _context = std::move(context); - if(_context.get() == nullptr) - { - _device = cl::Device(); - } - else - { - const auto cl_devices = _context.getInfo(); - - if(cl_devices.empty()) - { - _device = cl::Device(); - } - else - { - _device = cl_devices[0]; - } - } - } + void set_context(cl::Context context); /** Accessor for the associated CL context. * * @return A CL context. */ - cl::Context &context() - { - return _context; - } + cl::Context &context(); /** Gets the CL device for which the programs are created. */ - cl::Device &get_device() - { - return _device; - } + cl::Device &get_device(); /** Sets the CL device for which the programs are created. * * @param[in] device A CL device. */ - void set_device(cl::Device device) - { - _device = std::move(device); - } + void set_device(cl::Device device); /** Return the device version * @@ -321,17 +280,10 @@ public: /** Clear the library's cache of binary programs */ - void clear_programs_cache() - { - _programs_map.clear(); - _built_programs_map.clear(); - } + void clear_programs_cache(); /** Access the cache of built OpenCL programs */ - const std::map &get_built_programs() const - { - return _built_programs_map; - } + const std::map &get_built_programs() const; /** Add a new built program to the cache * diff --git a/arm_compute/core/CL/OpenCL.h b/arm_compute/core/CL/OpenCL.h index 912a53103a..b1d50e73b1 100644 --- a/arm_compute/core/CL/OpenCL.h +++ b/arm_compute/core/CL/OpenCL.h @@ -60,11 +60,10 @@ bool opencl_is_available(); /** Class for loading OpenCL symbols. */ class CLSymbols final { -private: +public: CLSymbols() = default; void load_symbols(void *handle); -public: /** Get the static instance of CLSymbols. * * @return The static instance of CLSymbols. diff --git a/arm_compute/core/CL/kernels/CLActivationLayerKernel.h b/arm_compute/core/CL/kernels/CLActivationLayerKernel.h index f20d6c3362..c64f7c42ac 100644 --- a/arm_compute/core/CL/kernels/CLActivationLayerKernel.h +++ b/arm_compute/core/CL/kernels/CLActivationLayerKernel.h @@ -29,13 +29,13 @@ namespace arm_compute { class ICLTensor; - +class CLCoreRuntimeContext; /** Interface for the activation layer kernel. */ class CLActivationLayerKernel : public ICLKernel { public: /** Default constructor */ - CLActivationLayerKernel(); + CLActivationLayerKernel(CLCoreRuntimeContext *ctx = nullptr); /** Prevent instances of this class from being copied (As this class contains pointers) */ CLActivationLayerKernel(const CLActivationLayerKernel &) = delete; /** Prevent instances of this class from being copied (As this class contains pointers) */ @@ -71,9 +71,10 @@ public: void run(const Window &window, cl::CommandQueue &queue) override; private: - ICLTensor *_input; - ICLTensor *_output; - bool _run_in_place; + ICLTensor *_input; + ICLTensor *_output; + bool _run_in_place; + CLCoreRuntimeContext *_ctx; }; } // namespace arm_compute #endif /*__ARM_COMPUTE_CLACTIVATIONLAYERKERNEL_H__ */ diff --git a/arm_compute/graph/backends/CL/CLDeviceBackend.h b/arm_compute/graph/backends/CL/CLDeviceBackend.h index 8569cf1f34..1239d5d3ad 100644 --- a/arm_compute/graph/backends/CL/CLDeviceBackend.h +++ b/arm_compute/graph/backends/CL/CLDeviceBackend.h @@ -31,6 +31,7 @@ namespace arm_compute { +class CLCoreRuntimeContext; namespace graph { namespace backends @@ -70,10 +71,11 @@ public: std::shared_ptr create_weights_manager() override; private: - int _context_count; /**< Counts how many contexts are currently using the backend */ - CLTuner _tuner; /**< CL kernel tuner */ - std::unique_ptr _allocator; /**< CL buffer affinity allocator */ - std::string _tuner_file; /**< Filename to load/store the tuner's values from */ + int _context_count; /**< Counts how many contexts are currently using the backend */ + CLTuner _tuner; /**< CL kernel tuner */ + std::unique_ptr _allocator; /**< CL buffer affinity allocator */ + std::string _tuner_file; /**< Filename to load/store the tuner's values from */ + std::unique_ptr _legacy_ctx; }; } // namespace backends } // namespace graph diff --git a/arm_compute/runtime/CL/CLBufferAllocator.h b/arm_compute/runtime/CL/CLBufferAllocator.h index 19a3e627ca..772402270b 100644 --- a/arm_compute/runtime/CL/CLBufferAllocator.h +++ b/arm_compute/runtime/CL/CLBufferAllocator.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017-2018 ARM Limited. + * Copyright (c) 2017-2019 ARM Limited. * * SPDX-License-Identifier: MIT * @@ -26,19 +26,25 @@ #include "arm_compute/runtime/IAllocator.h" -#include "arm_compute/core/CL/OpenCL.h" -#include "arm_compute/runtime/CL/CLScheduler.h" - #include namespace arm_compute { +class CLCoreRuntimeContext; /** Default OpenCL cl buffer allocator implementation */ class CLBufferAllocator final : public IAllocator { public: - /** Default constructor */ - explicit CLBufferAllocator(cl::Context context = CLScheduler::get().context()); + /** Default constructor + * + * @param[in] ctx A runtime context. + */ + CLBufferAllocator(CLCoreRuntimeContext *ctx = nullptr); + + /** Default copy constructor */ + CLBufferAllocator(const CLBufferAllocator &) = default; + /** Default copy assignment operator */ + CLBufferAllocator &operator=(const CLBufferAllocator &) = default; // Inherited methods overridden: void *allocate(size_t size, size_t alignment) override; @@ -46,7 +52,7 @@ public: std::unique_ptr make_region(size_t size, size_t alignment) override; private: - cl::Context _context; + CLCoreRuntimeContext *_ctx; }; } // arm_compute #endif /*__ARM_COMPUTE_CLBUFFERALLOCATOR_H__ */ diff --git a/arm_compute/runtime/CL/CLHelpers.h b/arm_compute/runtime/CL/CLHelpers.h index f3b11f8b75..84f155afd2 100644 --- a/arm_compute/runtime/CL/CLHelpers.h +++ b/arm_compute/runtime/CL/CLHelpers.h @@ -25,9 +25,12 @@ #define __ARM_COMPUTE_CL_HELPERS_H__ #include "arm_compute/core/CL/OpenCL.h" +#include "arm_compute/runtime/IScheduler.h" namespace arm_compute { +class CLRuntimeContext; +class ICLKernel; /** This function creates an OpenCL context and a device. * * @note In debug builds, the function will automatically enable cl_arm_printf if the driver/device supports it. @@ -37,5 +40,12 @@ namespace arm_compute * a value telling why the function failed. */ std::tuple create_opencl_context_and_device(); +/** Schedules a kernel using the context if not nullptr else uses the legacy scheduling flow. + * + * @param[in] ctx Context to use. + * @param[in] kernel Kernel to schedule. + * @param[in] flush (Optional) Specifies if the command queue will be flushed after running the kernel. + */ +void schedule_kernel_on_ctx(CLRuntimeContext *ctx, ICLKernel *kernel, bool flush = true); } // namespace arm_compute #endif /* __ARM_COMPUTE_CL_HELPERS_H__ */ diff --git a/arm_compute/runtime/CL/CLMemoryRegion.h b/arm_compute/runtime/CL/CLMemoryRegion.h index dbfd8225ca..6f7c3cd9a8 100644 --- a/arm_compute/runtime/CL/CLMemoryRegion.h +++ b/arm_compute/runtime/CL/CLMemoryRegion.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018 ARM Limited. + * Copyright (c) 2018-2019 ARM Limited. * * SPDX-License-Identifier: MIT * @@ -31,16 +31,17 @@ namespace arm_compute { +class CLCoreRuntimeContext; /** OpenCL memory region interface */ class ICLMemoryRegion : public IMemoryRegion { public: /** Constructor * - * @param[in] ctx OpenCL context + * @param[in] ctx Runtime context * @param[in] size Region size */ - ICLMemoryRegion(cl::Context ctx, size_t size); + ICLMemoryRegion(CLCoreRuntimeContext *ctx, size_t size); /** Default Destructor */ virtual ~ICLMemoryRegion() = default; /** Prevent instances of this class from being copied (As this class contains pointers) */ @@ -86,9 +87,10 @@ public: std::unique_ptr extract_subregion(size_t offset, size_t size) override; protected: - cl::Context _ctx; - void *_mapping; - cl::Buffer _mem; + cl::CommandQueue _queue; + cl::Context _ctx; + void *_mapping; + cl::Buffer _mem; }; /** OpenCL buffer memory region implementation */ @@ -97,16 +99,17 @@ class CLBufferMemoryRegion final : public ICLMemoryRegion public: /** Constructor * - * @param[in] ctx OpenCL context + * @param[in] ctx Runtime context * @param[in] flags Memory flags * @param[in] size Region size */ - CLBufferMemoryRegion(cl::Context ctx, cl_mem_flags flags, size_t size); + CLBufferMemoryRegion(CLCoreRuntimeContext *ctx, cl_mem_flags flags, size_t size); /** Constructor * * @param[in] buffer Buffer to be used as a memory region + * @param[in] ctx Runtime context */ - CLBufferMemoryRegion(const cl::Buffer &buffer); + CLBufferMemoryRegion(const cl::Buffer &buffer, CLCoreRuntimeContext *ctx); // Inherited methods overridden : void *ptr() final; @@ -120,12 +123,12 @@ class ICLSVMMemoryRegion : public ICLMemoryRegion protected: /** Constructor * - * @param[in] ctx OpenCL context + * @param[in] ctx Runtime context * @param[in] flags Memory flags * @param[in] size Region size * @param[in] alignment Alignment */ - ICLSVMMemoryRegion(cl::Context ctx, cl_mem_flags flags, size_t size, size_t alignment); + ICLSVMMemoryRegion(CLCoreRuntimeContext *ctx, cl_mem_flags flags, size_t size, size_t alignment); /** Destructor */ virtual ~ICLSVMMemoryRegion(); /** Prevent instances of this class from being copied (As this class contains pointers) */ @@ -150,12 +153,12 @@ class CLCoarseSVMMemoryRegion final : public ICLSVMMemoryRegion public: /** Constructor * - * @param[in] ctx OpenCL context + * @param[in] ctx Runtime context * @param[in] flags Memory flags * @param[in] size Region size * @param[in] alignment Alignment */ - CLCoarseSVMMemoryRegion(cl::Context ctx, cl_mem_flags flags, size_t size, size_t alignment); + CLCoarseSVMMemoryRegion(CLCoreRuntimeContext *ctx, cl_mem_flags flags, size_t size, size_t alignment); // Inherited methods overridden : void *map(cl::CommandQueue &q, bool blocking) final; @@ -168,12 +171,12 @@ class CLFineSVMMemoryRegion final : public ICLSVMMemoryRegion public: /** Constructor * - * @param[in] ctx OpenCL context + * @param[in] ctx Runtime context * @param[in] flags Memory flags * @param[in] size Region size * @param[in] alignment Alignment */ - CLFineSVMMemoryRegion(cl::Context ctx, cl_mem_flags flags, size_t size, size_t alignment); + CLFineSVMMemoryRegion(CLCoreRuntimeContext *ctx, cl_mem_flags flags, size_t size, size_t alignment); // Inherited methods overridden : void *map(cl::CommandQueue &q, bool blocking) final; diff --git a/arm_compute/runtime/CL/CLRuntimeContext.h b/arm_compute/runtime/CL/CLRuntimeContext.h new file mode 100644 index 0000000000..971dfd2224 --- /dev/null +++ b/arm_compute/runtime/CL/CLRuntimeContext.h @@ -0,0 +1,70 @@ +/* + * Copyright (c) 2019 ARM Limited. + * + * SPDX-License-Identifier: MIT + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#ifndef __ARM_COMPUTE_CLRUNTIME_CONTEXT_H__ +#define __ARM_COMPUTE_CLRUNTIME_CONTEXT_H__ + +#include "arm_compute/core/CL/CLCoreRuntimeContext.h" +#include "arm_compute/core/CL/CLKernelLibrary.h" +#include "arm_compute/core/CL/OpenCL.h" +#include "arm_compute/runtime/CL/CLScheduler.h" +#include "arm_compute/runtime/CL/CLTuner.h" +#include "arm_compute/runtime/IScheduler.h" +#include "arm_compute/runtime/RuntimeContext.h" + +namespace arm_compute +{ +/** Runtime context */ +class CLRuntimeContext : public RuntimeContext +{ +public: + /** Default Constructor */ + CLRuntimeContext(); + /** Destructor */ + ~CLRuntimeContext() = default; + /** Prevent instances of this class from being copied (As this class contains pointers) */ + CLRuntimeContext(const CLRuntimeContext &) = delete; + /** Default move constructor */ + CLRuntimeContext(CLRuntimeContext &&) = default; + /** Prevent instances of this class from being copied (As this class contains pointers) */ + CLRuntimeContext &operator=(const CLRuntimeContext &) = delete; + /** Default move assignment operator */ + CLRuntimeContext &operator=(CLRuntimeContext &&) = default; + /** CPU Scheduler setter */ + void set_gpu_scheduler(CLScheduler *scheduler); + + // Inherited overridden methods + CLScheduler *gpu_scheduler(); + CLKernelLibrary &kernel_library(); + CLCoreRuntimeContext *core_runtime_context(); + +private: + std::unique_ptr _gpu_owned_scheduler{ nullptr }; + CLScheduler *_gpu_scheduler{ nullptr }; + CLTuner _tuner{ false }; + CLKernelLibrary _kernel_lib{}; + CLSymbols _symbols{}; + CLCoreRuntimeContext _core_context{}; +}; +} // namespace arm_compute +#endif /*__ARM_COMPUTE_CLRUNTIME_CONTEXT_H__ */ diff --git a/arm_compute/runtime/CL/CLScheduler.h b/arm_compute/runtime/CL/CLScheduler.h index 53cb88ad5b..720c8b37f5 100644 --- a/arm_compute/runtime/CL/CLScheduler.h +++ b/arm_compute/runtime/CL/CLScheduler.h @@ -25,7 +25,6 @@ #define __ARM_COMPUTE_CLSCHEDULER_H__ #include "arm_compute/core/CL/CLHelpers.h" -#include "arm_compute/core/CL/CLKernelLibrary.h" #include "arm_compute/core/CL/CLTypes.h" #include "arm_compute/core/CL/OpenCL.h" #include "arm_compute/core/Error.h" @@ -35,21 +34,21 @@ namespace arm_compute { class ICLKernel; - +class ICLTuner; /** Provides global access to a CL context and command queue. */ -class CLScheduler +class CLScheduler final { -private: +public: /** Constructor */ CLScheduler(); /** Prevent instances of this class from being copied (As this class contains pointers) */ CLScheduler(const CLScheduler &) = delete; /** Prevent instances of this class from being copied (As this class contains pointers) */ CLScheduler &operator=(const CLScheduler &) = delete; - -public: + /** Default destructor */ + ~CLScheduler() = default; /** Access the scheduler singleton. - * + * This method has been deprecated and will be removed in future releases * @return The scheduler */ static CLScheduler &get(); @@ -88,31 +87,19 @@ public: * * @return A CL context. */ - cl::Context &context() - { - ARM_COMPUTE_ERROR_ON(!_is_initialised); - _context = CLKernelLibrary::get().context(); - return _context; - } + cl::Context &context(); /** Accessor for the associated CL command queue. * * @return A CL command queue. */ - cl::CommandQueue &queue() - { - ARM_COMPUTE_ERROR_ON(!_is_initialised); - return _queue; - } + cl::CommandQueue &queue(); /** Get the target GPU. * * @return The target GPU. */ - GPUTarget target() const - { - return _target; - } + GPUTarget target() const; /** Accessor to set the CL context to be used by the scheduler. * @@ -124,63 +111,36 @@ public: * * @param[in] queue A CL command queue. */ - void set_queue(cl::CommandQueue queue) - { - _queue = std::move(queue); - } + void set_queue(cl::CommandQueue queue); /** Accessor to set target GPU to be used by the scheduler. * * @param[in] target The target GPU. */ - void set_target(GPUTarget target) - { - _target = target; - } + void set_target(GPUTarget target); /** Accessor to set the CL tuner to be used by the scheduler. * * @param[in] tuner A CL tuner */ - void set_tuner(ICLTuner *tuner) - { - _cl_tuner = tuner; - } + void set_tuner(ICLTuner *tuner); /** Blocks until all commands in the associated command queue have finished. */ - void sync() - { - _queue.finish(); - } + void sync(); /** Enqueues a marker into the associated command queue and return the event. * * @return An event that can be waited on to block the executing thread. */ - cl::Event enqueue_sync_event() - { - cl::Event event; - _queue.enqueueMarker(&event); - - return event; - } + cl::Event enqueue_sync_event(); /** Tunes OpenCL kernel * * @param[in] kernel Kernel to tune */ - void tune_kernel_static(ICLKernel &kernel) - { - if(_cl_tuner != nullptr) - { - _cl_tuner->tune_kernel_static(kernel); - } - } - - bool is_initialised() const - { - return _is_initialised; - } + void tune_kernel_static(ICLKernel &kernel); + + bool is_initialised() const; private: /** Flag to ensure symbols initialisation is happening before Scheduler creation */ diff --git a/arm_compute/runtime/CL/CLTensor.h b/arm_compute/runtime/CL/CLTensor.h index bc72839492..c108d1afad 100644 --- a/arm_compute/runtime/CL/CLTensor.h +++ b/arm_compute/runtime/CL/CLTensor.h @@ -35,13 +35,32 @@ namespace arm_compute // Forward declarations class ITensorAllocator; class ITensorInfo; - +class IRuntimeContext; +class CLRuntimeContext; /** Basic implementation of the OpenCL tensor interface */ class CLTensor : public ICLTensor, public IMemoryManageable { public: - /** Constructor */ - CLTensor(); + /** Constructor. + * + * @param[in] ctx (Optional) Pointer to a @ref CLRuntimeContext. + * If nullptr is passed in, the legacy api using the singletons will be used. Otherwise the memory for the + * tensor will allocate on the context passed in. + * The singletons legacy api has been deprecated and will be removed. + */ + CLTensor(IRuntimeContext *ctx = nullptr); + + /** Destructor */ + ~CLTensor() = default; + /** Default copy constructor */ + CLTensor(const CLTensor &) = default; + /** Default move constructor */ + CLTensor(CLTensor &&) = default; + /** Default copy assignment */ + CLTensor &operator=(const CLTensor &) = default; + /** Default move assignment operator */ + CLTensor &operator=(CLTensor &&) = default; + /** Return a pointer to the tensor's allocator * * @return A pointer to the tensor's allocator @@ -69,6 +88,7 @@ public: const cl::Buffer &cl_buffer() const override; CLQuantization quantization() const override; void associate_memory_group(IMemoryGroup *memory_group) override; + CLRuntimeContext *context(); protected: // Inherited methods overridden: @@ -77,6 +97,7 @@ protected: private: mutable CLTensorAllocator _allocator; /**< Instance of the OpenCL tensor allocator */ + CLRuntimeContext *_ctx{ nullptr }; }; /** OpenCL Image */ diff --git a/arm_compute/runtime/CL/CLTensorAllocator.h b/arm_compute/runtime/CL/CLTensorAllocator.h index 3450c72d61..b3ffd8b949 100644 --- a/arm_compute/runtime/CL/CLTensorAllocator.h +++ b/arm_compute/runtime/CL/CLTensorAllocator.h @@ -37,7 +37,7 @@ namespace arm_compute { class CLTensor; - +class CLRuntimeContext; /** Basic implementation of a CL memory tensor allocator. */ class CLTensorAllocator : public ITensorAllocator { @@ -45,8 +45,9 @@ public: /** Default constructor. * * @param[in] owner (Optional) Owner of the allocator. + * @param[in] ctx (Optional) Runtime context. */ - CLTensorAllocator(IMemoryManageable *owner = nullptr); + CLTensorAllocator(IMemoryManageable *owner = nullptr, CLRuntimeContext *ctx = nullptr); /** Prevent instances of this class from being copied (As this class contains pointers) */ CLTensorAllocator(const CLTensorAllocator &) = delete; /** Prevent instances of this class from being copy assigned (As this class contains pointers) */ @@ -139,6 +140,7 @@ private: static const cl::Buffer _empty_buffer; private: + CLRuntimeContext *_ctx; IMemoryManageable *_owner; /**< Memory manageable object that owns the allocator */ IMemoryGroup *_associated_memory_group; /**< Registered memory manager */ CLMemory _memory; /**< OpenCL memory */ diff --git a/arm_compute/runtime/CL/ICLSimpleFunction.h b/arm_compute/runtime/CL/ICLSimpleFunction.h index 130c58a98c..8399a3d58e 100644 --- a/arm_compute/runtime/CL/ICLSimpleFunction.h +++ b/arm_compute/runtime/CL/ICLSimpleFunction.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2016, 2017 ARM Limited. + * Copyright (c) 2016-2019 ARM Limited. * * SPDX-License-Identifier: MIT * @@ -32,12 +32,25 @@ namespace arm_compute { +class CLRuntimeContext; /** Basic interface for functions which have a single OpenCL kernel */ class ICLSimpleFunction : public IFunction { public: - /** Default constructor */ - ICLSimpleFunction(); + /** Constructor + * + * @param[in] ctx Runtime context to be used by the function + */ + ICLSimpleFunction(CLRuntimeContext *ctx = nullptr); + + /** Prevent instances of this class from being copied (As this class contains pointers) */ + ICLSimpleFunction(const ICLSimpleFunction &) = delete; + /** Default move constructor */ + ICLSimpleFunction(ICLSimpleFunction &&) = default; + /** Prevent instances of this class from being copied (As this class contains pointers) */ + ICLSimpleFunction &operator=(const ICLSimpleFunction &) = delete; + /** Default move assignment operator */ + ICLSimpleFunction &operator=(ICLSimpleFunction &&) = default; // Inherited methods overridden: void run() override final; @@ -45,6 +58,7 @@ public: protected: std::unique_ptr _kernel; /**< Kernel to run */ CLFillBorderKernel _border_handler; /**< Kernel to handle borders */ + CLRuntimeContext *_ctx; /**< Context to use */ }; } #endif /*__ARM_COMPUTE_ICLSIMPLEFUNCTION_H__ */ diff --git a/arm_compute/runtime/CL/functions/CLActivationLayer.h b/arm_compute/runtime/CL/functions/CLActivationLayer.h index 1201d7d355..e1e3e877d5 100644 --- a/arm_compute/runtime/CL/functions/CLActivationLayer.h +++ b/arm_compute/runtime/CL/functions/CLActivationLayer.h @@ -43,7 +43,7 @@ public: * * @param[in] ctx Runtime context to be used by the function */ - CLActivationLayer(void *ctx = nullptr); + CLActivationLayer(CLRuntimeContext *ctx = nullptr); /** Prevent instances of this class from being copied (As this class contains pointers) */ CLActivationLayer(const CLActivationLayer &) = delete; /** Default move constructor */ diff --git a/arm_compute/runtime/GLES_COMPUTE/GCTensor.h b/arm_compute/runtime/GLES_COMPUTE/GCTensor.h index 344c78852b..a308ba0237 100644 --- a/arm_compute/runtime/GLES_COMPUTE/GCTensor.h +++ b/arm_compute/runtime/GLES_COMPUTE/GCTensor.h @@ -32,13 +32,18 @@ namespace arm_compute { class ITensorAllocator; class ITensorInfo; +class IRuntimeContext; /** Interface for OpenGL ES tensor */ class GCTensor : public IGCTensor, public IMemoryManageable { public: - /** Default constructor */ - GCTensor(); + /** Default constructor + * + * @param[in] ctx (Optional) Pointer to the runtime context. + * + */ + GCTensor(IRuntimeContext *ctx = nullptr); /** Prevent instances of this class from being copied (As this class contains pointers) */ GCTensor(const GCTensor &) = delete; diff --git a/arm_compute/runtime/Tensor.h b/arm_compute/runtime/Tensor.h index 6fa7c8ca0e..e469f70817 100644 --- a/arm_compute/runtime/Tensor.h +++ b/arm_compute/runtime/Tensor.h @@ -32,13 +32,17 @@ namespace arm_compute { class ITensorInfo; - +class IRuntimeContext; /** Basic implementation of the tensor interface */ class Tensor : public ITensor, public IMemoryManageable { public: - /** Constructor */ - Tensor(); + /** Constructor + * + * @param[in] ctx (Optional) Pointer to the runtime context. + * + */ + Tensor(IRuntimeContext *ctx = nullptr); /** Destructor: free the tensor's memory */ ~Tensor() = default; /** Allow instances of this class to be move constructed */ -- cgit v1.2.1