blob: 6e81a46a29a4f6bacf12bcad2d3d7cf5f99112aa [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Georgios Pinitasc6f95102021-03-30 10:03:01 +01002 * Copyright (c) 2016-2021 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 */
Michalis Spyrouf4643372019-11-29 16:17:13 +000024#ifndef ARM_COMPUTE_CLARRAY_H
25#define ARM_COMPUTE_CLARRAY_H
Anthony Barbier6ff3b192017-09-04 18:44:23 +010026
27#include "arm_compute/core/CL/ICLArray.h"
28#include "arm_compute/core/CL/OpenCL.h"
29#include "arm_compute/core/Error.h"
30#include "arm_compute/core/Types.h"
31#include "arm_compute/runtime/CL/CLScheduler.h"
32
33namespace arm_compute
34{
35/** CLArray implementation */
36template <class T>
37class CLArray : public ICLArray<T>
38{
39public:
Moritz Pflanzer6c6597c2017-09-24 12:09:41 +010040 /** Default constructor: empty array */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010041 CLArray() : ICLArray<T>(0), _buffer()
Moritz Pflanzer6c6597c2017-09-24 12:09:41 +010042 {
43 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +010044 /** Prevent instances of this class from being copied (As this class contains pointers) */
45 CLArray(const CLArray &) = delete;
46 /** Prevent instances of this class from being copied (As this class contains pointers) */
Moritz Pflanzer6c6597c2017-09-24 12:09:41 +010047 CLArray &operator=(const CLArray &) = delete;
Alex Gildayc357c472018-03-21 13:54:09 +000048 /** Allow instances of this class to be move constructed */
49 CLArray(CLArray &&) = default;
50 /** Allow instances of this class to be moved */
Moritz Pflanzer6c6597c2017-09-24 12:09:41 +010051 CLArray &operator=(CLArray &&) = default;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010052 /** Constructor: initializes an array which can contain up to max_num_points values
53 *
54 * @param[in] max_num_values Maximum number of values the array will be able to stored
55 */
56 CLArray(size_t max_num_values)
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010057 : ICLArray<T>(max_num_values),
58 _buffer(CLScheduler::get().context(), CL_MEM_ALLOC_HOST_PTR | CL_MEM_READ_WRITE, max_num_values * sizeof(T))
Anthony Barbier6ff3b192017-09-04 18:44:23 +010059 {
60 }
61 /** Enqueue a map operation of the allocated buffer.
62 *
63 * @param[in] blocking If true, then the mapping will be ready to use by the time
64 * this method returns, else it is the caller's responsibility
65 * to flush the queue and wait for the mapping operation to have completed.
66 */
67 void map(bool blocking = true)
68 {
69 ICLArray<T>::map(CLScheduler::get().queue(), blocking);
70 }
71 using ICLArray<T>::map;
72 /** Enqueue an unmap operation of the allocated and mapped buffer.
73 *
74 * @note This method simply enqueues the unmap operation, it is the caller's responsibility to flush the queue and make sure the unmap is finished before
75 * the memory is accessed by the device.
76 */
77 void unmap()
78 {
79 ICLArray<T>::unmap(CLScheduler::get().queue());
80 }
81 using ICLArray<T>::unmap;
82
83 // Inherited methods overridden:
84 const cl::Buffer &cl_buffer() const override
85 {
86 return _buffer;
87 }
88
89protected:
90 // Inherited methods overridden:
91 uint8_t *do_map(cl::CommandQueue &q, bool blocking) override
92 {
93 ARM_COMPUTE_ERROR_ON(nullptr == _buffer.get());
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010094 return static_cast<uint8_t *>(q.enqueueMapBuffer(
95 _buffer, blocking ? CL_TRUE : CL_FALSE, CL_MAP_READ | CL_MAP_WRITE, 0, this->max_num_values() * sizeof(T)));
Anthony Barbier6ff3b192017-09-04 18:44:23 +010096 }
97 void do_unmap(cl::CommandQueue &q, uint8_t *mapping) override
98 {
99 ARM_COMPUTE_ERROR_ON(nullptr == _buffer.get());
100 q.enqueueUnmapMemObject(_buffer, mapping);
101 }
102
103private:
104 cl::Buffer _buffer;
105};
Alex Gildayc357c472018-03-21 13:54:09 +0000106/** OpenCL Array of uint8s. */
107using CLUInt8Array = CLArray<cl_uchar>;
108/** OpenCL Array of uint16s. */
109using CLUInt16Array = CLArray<cl_ushort>;
110/** OpenCL Array of uint32s. */
111using CLUInt32Array = CLArray<cl_uint>;
112/** OpenCL Array of int16s. */
113using CLInt16Array = CLArray<cl_short>;
114/** OpenCL Array of int32s. */
115using CLInt32Array = CLArray<cl_int>;
116/** OpenCL Array of floats. */
117using CLFloatArray = CLArray<cl_float>;
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100118} // namespace arm_compute
Michalis Spyrouf4643372019-11-29 16:17:13 +0000119#endif /* ARM_COMPUTE_CLARRAY_H */