blob: 6929d551fbc24cf7c4ce0f935cb058c0768f13db [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Alex Gildayc357c472018-03-21 13:54:09 +00002 * Copyright (c) 2016-2018 ARM Limited.
Anthony Barbier6ff3b192017-09-04 18:44:23 +01003 *
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_CLTENSORALLOCATOR_H__
25#define __ARM_COMPUTE_CLTENSORALLOCATOR_H__
26
Anthony Barbier6ff3b192017-09-04 18:44:23 +010027#include "arm_compute/runtime/ITensorAllocator.h"
28
Georgios Pinitasbaf174e2017-09-08 19:47:30 +010029#include "arm_compute/core/CL/OpenCL.h"
Pablo Telloe86a09f2018-01-11 15:44:48 +000030#include "arm_compute/runtime/CL/SVMMemory.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010031#include <cstdint>
32
33namespace arm_compute
34{
Georgios Pinitasbaf174e2017-09-08 19:47:30 +010035class CLTensor;
36template <typename>
37class MemoryGroupBase;
Alex Gildayc357c472018-03-21 13:54:09 +000038/** Memory Group in OpenCL */
Georgios Pinitasbaf174e2017-09-08 19:47:30 +010039using CLMemoryGroup = MemoryGroupBase<CLTensor>;
40
Anthony Barbier6ff3b192017-09-04 18:44:23 +010041/** Basic implementation of a CL memory tensor allocator. */
42class CLTensorAllocator : public ITensorAllocator
43{
44public:
Alex Gildayc357c472018-03-21 13:54:09 +000045 /** Default constructor.
46 *
47 * @param[in] owner (Optional) Owner of the allocator.
48 */
Georgios Pinitasbaf174e2017-09-08 19:47:30 +010049 CLTensorAllocator(CLTensor *owner = nullptr);
50 /** Default destructor */
51 ~CLTensorAllocator();
Alex Gildayc357c472018-03-21 13:54:09 +000052 /** Prevent instances of this class from being copied (As this class contains pointers) */
Anthony Barbier6ff3b192017-09-04 18:44:23 +010053 CLTensorAllocator(const CLTensorAllocator &) = delete;
Alex Gildayc357c472018-03-21 13:54:09 +000054 /** Prevent instances of this class from being copy assigned (As this class contains pointers) */
Anthony Barbier6ff3b192017-09-04 18:44:23 +010055 CLTensorAllocator &operator=(const CLTensorAllocator &) = delete;
56 /** Allow instances of this class to be moved */
57 CLTensorAllocator(CLTensorAllocator &&) = default;
58 /** Allow instances of this class to be moved */
59 CLTensorAllocator &operator=(CLTensorAllocator &&) = default;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010060
Alex Gildayc357c472018-03-21 13:54:09 +000061 /** Interface to be implemented by the child class to return the pointer to the mapped data.
62 *
63 * @return pointer to the mapped data.
64 */
Anthony Barbier6ff3b192017-09-04 18:44:23 +010065 uint8_t *data();
Alex Gildayc357c472018-03-21 13:54:09 +000066 /** Interface to be implemented by the child class to return the pointer to the CL data.
67 *
68 * @return pointer to the CL data.
69 */
Anthony Barbier6ff3b192017-09-04 18:44:23 +010070 const cl::Buffer &cl_data() const;
Pablo Telloe86a09f2018-01-11 15:44:48 +000071 /** SVM memory */
72 void *svm_ptr();
73
Anthony Barbier6ff3b192017-09-04 18:44:23 +010074 /** Enqueue a map operation of the allocated buffer on the given queue.
75 *
76 * @param[in,out] q The CL command queue to use for the mapping operation.
77 * @param[in] blocking If true, then the mapping will be ready to use by the time
78 * this method returns, else it is the caller's responsibility
79 * to flush the queue and wait for the mapping operation to have completed before using the returned mapping pointer.
80 *
81 * @return The mapping address.
82 */
83 uint8_t *map(cl::CommandQueue &q, bool blocking);
84 /** Enqueue an unmap operation of the allocated buffer on the given queue.
85 *
86 * @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
87 * the memory is accessed by the device.
88 *
89 * @param[in,out] q The CL command queue to use for the mapping operation.
90 * @param[in] mapping The cpu mapping to unmap.
91 */
92 void unmap(cl::CommandQueue &q, uint8_t *mapping);
93
94 /** Allocate size specified by TensorInfo of OpenCL memory.
95 *
96 * @note: The tensor must not already be allocated when calling this function.
97 *
98 */
99 void allocate() override;
100
101 /** Free allocated OpenCL memory.
102 *
103 * @note The tensor must have been allocated when calling this function.
104 *
105 */
106 void free() override;
Georgios Pinitasbaf174e2017-09-08 19:47:30 +0100107 /** Associates the tensor with a memory group
108 *
109 * @param[in] associated_memory_group Memory group to associate the tensor with
110 */
111 void set_associated_memory_group(CLMemoryGroup *associated_memory_group);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100112
113protected:
114 /** Call map() on the OpenCL buffer.
115 *
116 * @return A pointer to the beginning of the tensor's allocation.
117 */
118 uint8_t *lock() override;
119 /** Call unmap() on the OpenCL buffer. */
120 void unlock() override;
121
122private:
Georgios Pinitasbaf174e2017-09-08 19:47:30 +0100123 CLMemoryGroup *_associated_memory_group; /**< Registered memory manager */
124 cl::Buffer _buffer; /**< OpenCL buffer containing the tensor data. */
125 uint8_t *_mapping; /**< Pointer to the CPU mapping of the OpenCL buffer. */
126 CLTensor *_owner; /**< Owner of the allocator */
Pablo Telloe86a09f2018-01-11 15:44:48 +0000127 SVMMemory _svm_memory; /**< Svm memory */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100128};
129}
130#endif /* __ARM_COMPUTE_CLTENSORALLOCATOR_H__ */