aboutsummaryrefslogtreecommitdiff
path: root/arm_compute/runtime/CL
diff options
context:
space:
mode:
authorGeorgios Pinitas <georgios.pinitas@arm.com>2018-04-23 16:26:46 +0100
committerAnthony Barbier <anthony.barbier@arm.com>2018-11-02 16:50:15 +0000
commit99d40951df87790fb884ce1c42d5e2a7a0009ee0 (patch)
tree774e2c6d0849a7fb3f13c7cc4773f7cbfea448fb /arm_compute/runtime/CL
parentcda0c38373b2f114509392ba16ef04e8c1e0f819 (diff)
downloadComputeLibrary-99d40951df87790fb884ce1c42d5e2a7a0009ee0.tar.gz
COMPMID-1023: Import memory for OpenCL
Change-Id: I201bc00a1261814737e6b6878ecfe9904bae0cc1 Reviewed-on: https://eu-gerrit-1.euhpc.arm.com/128212 Tested-by: Jenkins <bsgcomp@arm.com> Reviewed-by: Anthony Barbier <anthony.barbier@arm.com>
Diffstat (limited to 'arm_compute/runtime/CL')
-rw-r--r--arm_compute/runtime/CL/CLBufferAllocator.h5
-rw-r--r--arm_compute/runtime/CL/CLMemory.h82
-rw-r--r--arm_compute/runtime/CL/CLMemoryRegion.h178
-rw-r--r--arm_compute/runtime/CL/CLTensor.h2
-rw-r--r--arm_compute/runtime/CL/CLTensorAllocator.h26
-rw-r--r--arm_compute/runtime/CL/SVMMemory.h57
6 files changed, 281 insertions, 69 deletions
diff --git a/arm_compute/runtime/CL/CLBufferAllocator.h b/arm_compute/runtime/CL/CLBufferAllocator.h
index 05b0363dc3..19a3e627ca 100644
--- a/arm_compute/runtime/CL/CLBufferAllocator.h
+++ b/arm_compute/runtime/CL/CLBufferAllocator.h
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2017 ARM Limited.
+ * Copyright (c) 2017-2018 ARM Limited.
*
* SPDX-License-Identifier: MIT
*
@@ -34,7 +34,7 @@
namespace arm_compute
{
/** Default OpenCL cl buffer allocator implementation */
-class CLBufferAllocator : public IAllocator
+class CLBufferAllocator final : public IAllocator
{
public:
/** Default constructor */
@@ -43,6 +43,7 @@ public:
// Inherited methods overridden:
void *allocate(size_t size, size_t alignment) override;
void free(void *ptr) override;
+ std::unique_ptr<IMemoryRegion> make_region(size_t size, size_t alignment) override;
private:
cl::Context _context;
diff --git a/arm_compute/runtime/CL/CLMemory.h b/arm_compute/runtime/CL/CLMemory.h
new file mode 100644
index 0000000000..edd9de8097
--- /dev/null
+++ b/arm_compute/runtime/CL/CLMemory.h
@@ -0,0 +1,82 @@
+/*
+ * Copyright (c) 2018 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_RUNTIME_CL_CLMEMORY_H__
+#define __ARM_COMPUTE_RUNTIME_CL_CLMEMORY_H__
+
+#include "arm_compute/core/CL/OpenCL.h"
+#include "arm_compute/runtime/CL/CLMemoryRegion.h"
+
+#include <cstddef>
+#include <memory>
+
+namespace arm_compute
+{
+/** OpenCL implementation of memory object */
+class CLMemory
+{
+public:
+ /** Default Constructor */
+ CLMemory();
+ /** Default Constructor
+ *
+ * @param[in] memory Memory to be imported
+ */
+ CLMemory(std::shared_ptr<ICLMemoryRegion> memory);
+ /** Default Constructor
+ *
+ * @note Ownership of the memory is not transferred to this object.
+ * Thus management (allocate/free) should be done by the client.
+ *
+ * @param[in] memory Memory to be imported
+ */
+ CLMemory(ICLMemoryRegion *memory);
+ /** Allow instances of this class to be copied */
+ CLMemory(const CLMemory &) = default;
+ /** Allow instances of this class to be copy assigned */
+ CLMemory &operator=(const CLMemory &) = default;
+ /** Allow instances of this class to be moved */
+ CLMemory(CLMemory &&) noexcept = default;
+ /** Allow instances of this class to be move assigned */
+ CLMemory &operator=(CLMemory &&) noexcept = default;
+ /** Region accessor
+ *
+ * @return Memory region
+ */
+ ICLMemoryRegion *region();
+ /** Region accessor
+ *
+ * @return Memory region
+ */
+ ICLMemoryRegion *region() const;
+
+private:
+ /** Creates empty region */
+ void create_empty_region();
+
+private:
+ ICLMemoryRegion *_region;
+ std::shared_ptr<ICLMemoryRegion> _region_owned;
+};
+} // namespace arm_compute
+#endif /* __ARM_COMPUTE_RUNTIME_CL_CLMEMORY_H__ */
diff --git a/arm_compute/runtime/CL/CLMemoryRegion.h b/arm_compute/runtime/CL/CLMemoryRegion.h
new file mode 100644
index 0000000000..01dd54e391
--- /dev/null
+++ b/arm_compute/runtime/CL/CLMemoryRegion.h
@@ -0,0 +1,178 @@
+/*
+ * Copyright (c) 2018 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_RUNTIME_CL_CL_MEMORY_REGION_H__
+#define __ARM_COMPUTE_RUNTIME_CL_CL_MEMORY_REGION_H__
+
+#include "arm_compute/core/CL/OpenCL.h"
+#include "arm_compute/runtime/IMemoryRegion.h"
+
+#include <cstddef>
+
+namespace arm_compute
+{
+/** OpenCL memory region interface */
+class ICLMemoryRegion : public IMemoryRegion
+{
+public:
+ /** Constructor
+ *
+ * @param[in] ctx OpenCL context
+ * @param[in] size Region size
+ */
+ ICLMemoryRegion(cl::Context ctx, size_t size);
+ /** Default Destructor */
+ virtual ~ICLMemoryRegion() = default;
+ /** Prevent instances of this class from being copied (As this class contains pointers) */
+ ICLMemoryRegion(const ICLMemoryRegion &) = delete;
+ /** Default move constructor */
+ ICLMemoryRegion(ICLMemoryRegion &&) = default;
+ /** Prevent instances of this class from being copied (As this class contains pointers) */
+ ICLMemoryRegion &operator=(const ICLMemoryRegion &) = delete;
+ /** Default move assignment operator */
+ ICLMemoryRegion &operator=(ICLMemoryRegion &&) = default;
+ /** Returns the underlying CL buffer
+ *
+ * @return CL memory buffer object
+ */
+ const cl::Buffer &cl_data() const;
+ /** Host/SVM pointer accessor
+ *
+ * @return Host/SVM pointer base
+ */
+ virtual void *ptr() = 0;
+ /** Enqueue a map operation of the allocated buffer on the given queue.
+ *
+ * @param[in,out] q The CL command queue to use for the mapping operation.
+ * @param[in] blocking If true, then the mapping will be ready to use by the time
+ * this method returns, else it is the caller's responsibility
+ * to flush the queue and wait for the mapping operation to have completed before using the returned mapping pointer.
+ *
+ * @return The mapping address.
+ */
+ virtual void *map(cl::CommandQueue &q, bool blocking) = 0;
+ /** Enqueue an unmap operation of the allocated buffer on the given queue.
+ *
+ * @note This method simply enqueue the unmap operation, it is the caller's responsibility to flush the queue and make sure the unmap is finished before
+ * the memory is accessed by the device.
+ *
+ * @param[in,out] q The CL command queue to use for the mapping operation.
+ */
+ virtual void unmap(cl::CommandQueue &q) = 0;
+
+ // Inherited methods overridden :
+ void *buffer() override;
+ void *buffer() const override;
+ void **handle() override;
+
+protected:
+ cl::Context _ctx;
+ void *_mapping;
+ cl::Buffer _mem;
+};
+
+/** OpenCL buffer memory region implementation */
+class CLBufferMemoryRegion final : public ICLMemoryRegion
+{
+public:
+ /** Constructor
+ *
+ * @param[in] ctx OpenCL context
+ * @param[in] flags Memory flags
+ * @param[in] size Region size
+ */
+ CLBufferMemoryRegion(cl::Context ctx, cl_mem_flags flags, size_t size);
+
+ // Inherited methods overridden :
+ void *ptr() override;
+ void *map(cl::CommandQueue &q, bool blocking) override;
+ void unmap(cl::CommandQueue &q) override;
+};
+
+/** OpenCL SVM memory region interface */
+class ICLSVMMemoryRegion : public ICLMemoryRegion
+{
+protected:
+ /** Constructor
+ *
+ * @param[in] ctx OpenCL 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);
+ /** Destructor */
+ virtual ~ICLSVMMemoryRegion();
+ /** Prevent instances of this class from being copied (As this class contains pointers) */
+ ICLSVMMemoryRegion(const ICLSVMMemoryRegion &) = delete;
+ /** Default move constructor */
+ ICLSVMMemoryRegion(ICLSVMMemoryRegion &&) = default;
+ /** Prevent instances of this class from being copied (As this class contains pointers) */
+ ICLSVMMemoryRegion &operator=(const ICLSVMMemoryRegion &) = delete;
+ /** Default move assignment operator */
+ ICLSVMMemoryRegion &operator=(ICLSVMMemoryRegion &&) = default;
+
+ // Inherited methods overridden :
+ void *ptr() override;
+
+protected:
+ void *_ptr;
+};
+
+/** OpenCL coarse-grain SVM memory region implementation */
+class CLCoarseSVMMemoryRegion final : public ICLSVMMemoryRegion
+{
+public:
+ /** Constructor
+ *
+ * @param[in] ctx OpenCL 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);
+
+ // Inherited methods overridden :
+ void *map(cl::CommandQueue &q, bool blocking) override;
+ void unmap(cl::CommandQueue &q) override;
+};
+
+/** OpenCL fine-grain SVM memory region implementation */
+class CLFineSVMMemoryRegion final : public ICLSVMMemoryRegion
+{
+public:
+ /** Constructor
+ *
+ * @param[in] ctx OpenCL 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);
+
+ // Inherited methods overridden :
+ void *map(cl::CommandQueue &q, bool blocking) override;
+ void unmap(cl::CommandQueue &q) override;
+};
+} // namespace arm_compute
+#endif /* __ARM_COMPUTE_RUNTIME_CL_CL_MEMORY_REGION_H__ */
diff --git a/arm_compute/runtime/CL/CLTensor.h b/arm_compute/runtime/CL/CLTensor.h
index e05f307621..c47d2be1b0 100644
--- a/arm_compute/runtime/CL/CLTensor.h
+++ b/arm_compute/runtime/CL/CLTensor.h
@@ -45,7 +45,7 @@ public:
*
* @return A pointer to the tensor's allocator
*/
- ITensorAllocator *allocator();
+ CLTensorAllocator *allocator();
/** Enqueue a map operation of the allocated buffer.
*
* @param[in] blocking If true, then the mapping will be ready to use by the time
diff --git a/arm_compute/runtime/CL/CLTensorAllocator.h b/arm_compute/runtime/CL/CLTensorAllocator.h
index 6929d551fb..a372195555 100644
--- a/arm_compute/runtime/CL/CLTensorAllocator.h
+++ b/arm_compute/runtime/CL/CLTensorAllocator.h
@@ -24,10 +24,11 @@
#ifndef __ARM_COMPUTE_CLTENSORALLOCATOR_H__
#define __ARM_COMPUTE_CLTENSORALLOCATOR_H__
+#include "arm_compute/runtime/CL/CLMemory.h"
#include "arm_compute/runtime/ITensorAllocator.h"
#include "arm_compute/core/CL/OpenCL.h"
-#include "arm_compute/runtime/CL/SVMMemory.h"
+
#include <cstdint>
namespace arm_compute
@@ -47,8 +48,6 @@ public:
* @param[in] owner (Optional) Owner of the allocator.
*/
CLTensorAllocator(CLTensor *owner = nullptr);
- /** Default destructor */
- ~CLTensorAllocator();
/** 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) */
@@ -68,8 +67,6 @@ public:
* @return pointer to the CL data.
*/
const cl::Buffer &cl_data() const;
- /** SVM memory */
- void *svm_ptr();
/** Enqueue a map operation of the allocated buffer on the given queue.
*
@@ -104,6 +101,19 @@ public:
*
*/
void free() override;
+ /** Import an existing memory as a tensor's backing memory
+ *
+ * @warning If the tensor is flagged to be managed by a memory manager,
+ * this call will lead to an error.
+ * @warning Ownership of memory depends on the way the @ref CLMemory object was constructed
+ * @note Calling free on a tensor with imported memory will just clear
+ * the internal pointer value.
+ *
+ * @param[in] memory Memory to import
+ *
+ * @return error status
+ */
+ arm_compute::Status import_memory(CLMemory memory);
/** Associates the tensor with a memory group
*
* @param[in] associated_memory_group Memory group to associate the tensor with
@@ -121,10 +131,8 @@ protected:
private:
CLMemoryGroup *_associated_memory_group; /**< Registered memory manager */
- cl::Buffer _buffer; /**< OpenCL buffer containing the tensor data. */
- uint8_t *_mapping; /**< Pointer to the CPU mapping of the OpenCL buffer. */
+ CLMemory _memory; /**< OpenCL memory */
CLTensor *_owner; /**< Owner of the allocator */
- SVMMemory _svm_memory; /**< Svm memory */
};
-}
+} // namespace arm_compute
#endif /* __ARM_COMPUTE_CLTENSORALLOCATOR_H__ */
diff --git a/arm_compute/runtime/CL/SVMMemory.h b/arm_compute/runtime/CL/SVMMemory.h
deleted file mode 100644
index 9029388c70..0000000000
--- a/arm_compute/runtime/CL/SVMMemory.h
+++ /dev/null
@@ -1,57 +0,0 @@
-/*
- * Copyright (c) 2018 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_SVMMEMORY_H__
-#define __ARM_COMPUTE_SVMMEMORY_H__
-
-namespace arm_compute
-{
-class SVMMemory final
-{
-public:
- SVMMemory() = default;
- SVMMemory(void *ptr, bool fine_grain)
- : _ptr(ptr), _fine_grain(fine_grain), _size(0)
- {
- }
- void *ptr() const
- {
- return _ptr;
- }
- bool fine_grain() const
- {
- return _fine_grain;
- }
- size_t size() const
- {
- return _size;
- }
- void *allocate(cl_context context, size_t size, cl_svm_mem_flags flags, cl_uint alignment);
-
-private:
- void *_ptr{ nullptr };
- bool _fine_grain{ false };
- size_t _size{ 0 };
-};
-}
-#endif /* __ARM_COMPUTE_SVMMEMORY_H__ */