blob: ed371e0642ef873af8261559d961bf7c415b7174 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
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_CLTENSORALLOCATOR_H__
25#define __ARM_COMPUTE_CLTENSORALLOCATOR_H__
26
27#include "arm_compute/core/CL/OpenCL.h"
28#include "arm_compute/runtime/ITensorAllocator.h"
29
30#include <cstdint>
31
32namespace arm_compute
33{
34/** Basic implementation of a CL memory tensor allocator. */
35class CLTensorAllocator : public ITensorAllocator
36{
37public:
38 /** Default constructor. */
39 CLTensorAllocator();
40 /** Prevent instances of this class from being copied (As this class contains pointers). */
41 CLTensorAllocator(const CLTensorAllocator &) = delete;
42 /** Prevent instances of this class from being copy assigned (As this class contains pointers). */
43 CLTensorAllocator &operator=(const CLTensorAllocator &) = delete;
44 /** Allow instances of this class to be moved */
45 CLTensorAllocator(CLTensorAllocator &&) = default;
46 /** Allow instances of this class to be moved */
47 CLTensorAllocator &operator=(CLTensorAllocator &&) = default;
48 /** Default destructor */
49 ~CLTensorAllocator() = default;
50
51 /** Interface to be implemented by the child class to return the pointer to the mapped data. */
52 uint8_t *data();
53 /** Interface to be implemented by the child class to return the pointer to the CL data. */
54 const cl::Buffer &cl_data() const;
55 /** Enqueue a map operation of the allocated buffer on the given queue.
56 *
57 * @param[in,out] q The CL command queue to use for the mapping operation.
58 * @param[in] blocking If true, then the mapping will be ready to use by the time
59 * this method returns, else it is the caller's responsibility
60 * to flush the queue and wait for the mapping operation to have completed before using the returned mapping pointer.
61 *
62 * @return The mapping address.
63 */
64 uint8_t *map(cl::CommandQueue &q, bool blocking);
65 /** Enqueue an unmap operation of the allocated buffer on the given queue.
66 *
67 * @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
68 * the memory is accessed by the device.
69 *
70 * @param[in,out] q The CL command queue to use for the mapping operation.
71 * @param[in] mapping The cpu mapping to unmap.
72 */
73 void unmap(cl::CommandQueue &q, uint8_t *mapping);
74
75 /** Allocate size specified by TensorInfo of OpenCL memory.
76 *
77 * @note: The tensor must not already be allocated when calling this function.
78 *
79 */
80 void allocate() override;
81
82 /** Free allocated OpenCL memory.
83 *
84 * @note The tensor must have been allocated when calling this function.
85 *
86 */
87 void free() override;
88
89protected:
90 /** Call map() on the OpenCL buffer.
91 *
92 * @return A pointer to the beginning of the tensor's allocation.
93 */
94 uint8_t *lock() override;
95 /** Call unmap() on the OpenCL buffer. */
96 void unlock() override;
97
98private:
99 cl::Buffer _buffer; /**< OpenCL buffer containing the tensor data. */
100 uint8_t *_mapping; /**< Pointer to the CPU mapping of the OpenCL buffer. */
101};
102}
103#endif /* __ARM_COMPUTE_CLTENSORALLOCATOR_H__ */