Compute Library
 23.08
ICLArray.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016-2021, 2023 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_ICLARRAY_H
25 #define ARM_COMPUTE_ICLARRAY_H
26 
30 
31 namespace arm_compute
32 {
33 /** Interface for OpenCL Array */
34 template <class T>
35 class ICLArray : public IArray<T>
36 {
37 public:
38  /** Constructor
39  *
40  * @param[in] max_num_values Maximum size of the array.
41  *
42  */
43  explicit ICLArray(size_t max_num_values)
44  : IArray<T>(max_num_values), _mapping(nullptr)
45  {
46  }
47 
48  /** Prevent instances of this class from being copy constructed */
49  ICLArray(const ICLArray &) = delete;
50  /** Prevent instances of this class from being copied */
51  ICLArray &operator=(const ICLArray &) = delete;
52  /** Allow instances of this class to be move constructed */
53  ICLArray(ICLArray &&) = default;
54  /** Allow instances of this class to be moved */
55  ICLArray &operator=(ICLArray &&) = default;
56  /** Default virtual destructor. */
57  virtual ~ICLArray() = default;
58  /** Interface to be implemented by the child class to return a reference to the OpenCL buffer containing the array's data.
59  *
60  * @return A reference to an OpenCL buffer containing the array's data.
61  */
62  virtual const cl::Buffer &cl_buffer() const = 0;
63  /** Enqueue a map operation of the allocated buffer on the given queue.
64  *
65  * @param[in,out] q The CL command queue to use for the mapping operation.
66  * @param[in] blocking If true, then the mapping will be ready to use by the time
67  * this method returns, else it is the caller's responsibility
68  * to flush the queue and wait for the mapping operation to have completed before using the returned mapping pointer.
69  */
70  void map(cl::CommandQueue &q, bool blocking = true)
71  {
72  _mapping = do_map(q, blocking);
73  }
74  /** Enqueue an unmap operation of the allocated and mapped buffer on the given queue.
75  *
76  * @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
77  * the memory is accessed by the device.
78  *
79  * @param[in,out] q The CL command queue to use for the mapping operation.
80  */
81  void unmap(cl::CommandQueue &q)
82  {
83  do_unmap(q, _mapping);
84  _mapping = nullptr;
85  }
86 
87  // Inherited methods overridden:
88  T *buffer() const override
89  {
90  return reinterpret_cast<T *>(_mapping);
91  }
92 
93 protected:
94  /** Method to be implemented by the child class to map the OpenCL buffer
95  *
96  * @param[in,out] q The CL command queue to use for the mapping operation.
97  * @param[in] blocking If true, then the mapping will be ready to use by the time
98  * this method returns, else it is the caller's responsibility
99  * to flush the queue and wait for the mapping operation to have completed before using the returned mapping pointer.
100  */
101  virtual uint8_t *do_map(cl::CommandQueue &q, bool blocking) = 0;
102  /** Method to be implemented by the child class to unmap the OpenCL buffer
103  *
104  * @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
105  * the memory is accessed by the device.
106  *
107  * @param[in,out] q The CL command queue to use for the mapping operation.
108  * @param[in] mapping Pointer to the buffer to be unmapped.
109  */
110  virtual void do_unmap(cl::CommandQueue &q, uint8_t *mapping) = 0;
111 
112 private:
113  uint8_t *_mapping;
114 };
115 
116 /** Interface for OpenCL Array of uint8s. */
118 /** Interface for OpenCL Array of uint16s. */
120 /** Interface for OpenCL Array of uint32s. */
122 /** Interface for OpenCL Array of int16s. */
124 /** Interface for OpenCL Array of int32s. */
126 /** Interface for OpenCL Array of floats. */
128 }
129 #endif /*ARM_COMPUTE_ICLARRAY_H*/
IArray.h
arm_compute::ICLArray::ICLArray
ICLArray(size_t max_num_values)
Constructor.
Definition: ICLArray.h:43
arm_compute::ICLArray::map
void map(cl::CommandQueue &q, bool blocking=true)
Enqueue a map operation of the allocated buffer on the given queue.
Definition: ICLArray.h:70
arm_compute::IArray
Array of type T.
Definition: IArray.h:35
arm_compute::IArray::max_num_values
size_t max_num_values() const
Maximum number of values which can be stored in this array.
Definition: IArray.h:53
arm_compute::ICLArray
Interface for OpenCL Array.
Definition: ICLArray.h:35
arm_compute::ICLArray::~ICLArray
virtual ~ICLArray()=default
Default virtual destructor.
OpenCL.h
Wrapper to configure the Khronos OpenCL C++ header.
arm_compute::ICLArray::operator=
ICLArray & operator=(const ICLArray &)=delete
Prevent instances of this class from being copied.
arm_compute::ICLArray::unmap
void unmap(cl::CommandQueue &q)
Enqueue an unmap operation of the allocated and mapped buffer on the given queue.
Definition: ICLArray.h:81
arm_compute
Copyright (c) 2017-2023 Arm Limited.
Definition: introduction.dox:24
arm_compute::ICLArray::buffer
T * buffer() const override
Pointer to the first element of the array.
Definition: ICLArray.h:88
ITensor.h
arm_compute::ICLArray::cl_buffer
virtual const cl::Buffer & cl_buffer() const =0
Interface to be implemented by the child class to return a reference to the OpenCL buffer containing ...