Compute Library
 20.11
CLArray.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016-2019 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 
29 #include "arm_compute/core/Error.h"
30 #include "arm_compute/core/Types.h"
32 
33 namespace arm_compute
34 {
35 /** CLArray implementation */
36 template <class T>
37 class CLArray : public ICLArray<T>
38 {
39 public:
40  /** Default constructor: empty array */
42  : ICLArray<T>(0), _buffer()
43  {
44  }
45  /** Prevent instances of this class from being copied (As this class contains pointers) */
46  CLArray(const CLArray &) = delete;
47  /** Prevent instances of this class from being copied (As this class contains pointers) */
48  CLArray &operator=(const CLArray &) = delete;
49  /** Allow instances of this class to be move constructed */
50  CLArray(CLArray &&) = default;
51  /** Allow instances of this class to be moved */
52  CLArray &operator=(CLArray &&) = default;
53  /** Constructor: initializes an array which can contain up to max_num_points values
54  *
55  * @param[in] max_num_values Maximum number of values the array will be able to stored
56  */
58  : ICLArray<T>(max_num_values), _buffer(CLScheduler::get().context(), CL_MEM_ALLOC_HOST_PTR | CL_MEM_READ_WRITE, max_num_values * sizeof(T))
59  {
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  {
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 
89 protected:
90  // Inherited methods overridden:
91  uint8_t *do_map(cl::CommandQueue &q, bool blocking) override
92  {
93  ARM_COMPUTE_ERROR_ON(nullptr == _buffer.get());
94  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)));
95  }
96  void do_unmap(cl::CommandQueue &q, uint8_t *mapping) override
97  {
98  ARM_COMPUTE_ERROR_ON(nullptr == _buffer.get());
99  q.enqueueUnmapMemObject(_buffer, mapping);
100  }
101 
102 private:
103  cl::Buffer _buffer;
104 };
105 
106 /** OpenCL Array of Key Points. */
108 /** OpenCL Array of 2D Coordinates. */
110 /** OpenCL Array of Detection Windows. */
112 /** OpenCL Array of 2D Sizes. */
114 /** OpenCL Array of uint8s. */
116 /** OpenCL Array of uint16s. */
118 /** OpenCL Array of uint32s. */
120 /** OpenCL Array of int16s. */
122 /** OpenCL Array of int32s. */
124 /** OpenCL Array of floats. */
126 }
127 #endif /* ARM_COMPUTE_CLARRAY_H */
static CLScheduler & get()
Access the scheduler singleton.
Definition: CLScheduler.cpp:99
void map(cl::CommandQueue &q, bool blocking=true)
Enqueue a map operation of the allocated buffer on the given queue.
Definition: ICLArray.h:72
const cl::Buffer & cl_buffer() const override
Interface to be implemented by the child class to return a reference to the OpenCL buffer containing ...
Definition: CLArray.h:84
#define ARM_COMPUTE_ERROR_ON(cond)
If the condition is true then an error message is printed and an exception thrown.
Definition: Error.h:466
CLArray()
Default constructor: empty array.
Definition: CLArray.h:41
Copyright (c) 2017-2020 Arm Limited.
Interface to enqueue OpenCL kernels and get/set the OpenCL CommandQueue and ICLTuner.
Interface for OpenCL Array.
Definition: ICLArray.h:35
void map(bool blocking=true)
Enqueue a map operation of the allocated buffer.
Definition: CLArray.h:67
CLArray(size_t max_num_values)
Constructor: initializes an array which can contain up to max_num_points values.
Definition: CLArray.h:57
void unmap(cl::CommandQueue &q)
Enqueue an unmap operation of the allocated and mapped buffer on the given queue.
Definition: ICLArray.h:83
Provides global access to a CL context and command queue.
Definition: CLScheduler.h:40
Wrapper to configure the Khronos OpenCL C++ header.
size_t max_num_values() const
Maximum number of values which can be stored in this array.
Definition: IArray.h:58
CLArray implementation.
Definition: CLArray.h:37
CLArray & operator=(const CLArray &)=delete
Prevent instances of this class from being copied (As this class contains pointers)
void unmap()
Enqueue an unmap operation of the allocated and mapped buffer.
Definition: CLArray.h:77