blob: 3dc7f19bc710469b01cc8d4cc2965216bf0dbb9a [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_CLARRAY_H__
25#define __ARM_COMPUTE_CLARRAY_H__
26
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:
40 /** Prevent instances of this class from being copied (As this class contains pointers) */
41 CLArray(const CLArray &) = delete;
42 /** Prevent instances of this class from being copied (As this class contains pointers) */
43 const CLArray &operator=(const CLArray &) = delete;
44 /** Constructor: initializes an array which can contain up to max_num_points values
45 *
46 * @param[in] max_num_values Maximum number of values the array will be able to stored
47 */
48 CLArray(size_t max_num_values)
49 : ICLArray<T>(max_num_values), _buffer(cl::Buffer(CLScheduler::get().context(), CL_MEM_ALLOC_HOST_PTR | CL_MEM_READ_WRITE, max_num_values * sizeof(T)))
50 {
51 }
52 /** Enqueue a map operation of the allocated buffer.
53 *
54 * @param[in] blocking If true, then the mapping will be ready to use by the time
55 * this method returns, else it is the caller's responsibility
56 * to flush the queue and wait for the mapping operation to have completed.
57 */
58 void map(bool blocking = true)
59 {
60 ICLArray<T>::map(CLScheduler::get().queue(), blocking);
61 }
62 using ICLArray<T>::map;
63 /** Enqueue an unmap operation of the allocated and mapped buffer.
64 *
65 * @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
66 * the memory is accessed by the device.
67 */
68 void unmap()
69 {
70 ICLArray<T>::unmap(CLScheduler::get().queue());
71 }
72 using ICLArray<T>::unmap;
73
74 // Inherited methods overridden:
75 const cl::Buffer &cl_buffer() const override
76 {
77 return _buffer;
78 }
79
80protected:
81 // Inherited methods overridden:
82 uint8_t *do_map(cl::CommandQueue &q, bool blocking) override
83 {
84 ARM_COMPUTE_ERROR_ON(nullptr == _buffer.get());
85 return static_cast<uint8_t *>(q.enqueueMapBuffer(_buffer, blocking ? CL_TRUE : CL_FALSE, CL_MAP_READ | CL_MAP_WRITE, 0, this->max_num_values() * sizeof(T)));
86 }
87 void do_unmap(cl::CommandQueue &q, uint8_t *mapping) override
88 {
89 ARM_COMPUTE_ERROR_ON(nullptr == _buffer.get());
90 q.enqueueUnmapMemObject(_buffer, mapping);
91 }
92
93private:
94 cl::Buffer _buffer;
95};
96
97using CLKeyPointArray = CLArray<KeyPoint>;
98using CLCoordinates2DArray = CLArray<Coordinates2D>;
99using CLDetectionWindowArray = CLArray<DetectionWindow>;
SiCong Li3e363692017-07-04 15:02:10 +0100100using CLROIArray = CLArray<ROI>;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100101using CLSize2DArray = CLArray<Size2D>;
102using CLUInt8Array = CLArray<cl_uchar>;
103using CLUInt16Array = CLArray<cl_ushort>;
104using CLUInt32Array = CLArray<cl_uint>;
105using CLInt16Array = CLArray<cl_short>;
106using CLInt32Array = CLArray<cl_int>;
107using CLFloatArray = CLArray<cl_float>;
108}
109#endif /* __ARM_COMPUTE_CLARRAY_H__ */