Compute Library
 19.08
CLTensorAllocator.cpp
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  */
25 
26 #include "arm_compute/core/Error.h"
30 
31 namespace arm_compute
32 {
33 const cl::Buffer CLTensorAllocator::_empty_buffer = cl::Buffer();
34 
35 namespace
36 {
45 std::unique_ptr<ICLMemoryRegion> allocate_region(const cl::Context &context, size_t size, cl_uint alignment)
46 {
47  // Try fine-grain SVM
48  std::unique_ptr<ICLMemoryRegion> region = support::cpp14::make_unique<CLFineSVMMemoryRegion>(context,
49  CL_MEM_READ_WRITE | CL_MEM_SVM_FINE_GRAIN_BUFFER,
50  size,
51  alignment);
52 
53  // Try coarse-grain SVM in case of failure
54  if(region != nullptr && region->ptr() == nullptr)
55  {
56  region = support::cpp14::make_unique<CLCoarseSVMMemoryRegion>(context, CL_MEM_READ_WRITE, size, alignment);
57  }
58  // Try legacy buffer memory in case of failure
59  if(region != nullptr && region->ptr() == nullptr)
60  {
61  region = support::cpp14::make_unique<CLBufferMemoryRegion>(context, CL_MEM_ALLOC_HOST_PTR | CL_MEM_READ_WRITE, size);
62  }
63  return region;
64 }
70 void clear_quantization_arrays(CLFloatArray &scale, CLInt32Array &offset)
71 {
72  // Clear arrays
73  scale = CLFloatArray();
74  offset = CLInt32Array();
75 }
85 void populate_quantization_info(CLFloatArray &scale, CLInt32Array &offset, const QuantizationInfo &qinfo, size_t pad_size)
86 {
87  clear_quantization_arrays(scale, offset);
88 
89  // Create scale array
90  const std::vector<float> &qscale = qinfo.scale();
91  const size_t num_elements = qscale.size();
92  const size_t element_size = sizeof(std::remove_reference<decltype(qscale)>::type::value_type);
93  scale = CLFloatArray(num_elements + pad_size);
94  scale.resize(num_elements);
95  CLScheduler::get().queue().enqueueWriteBuffer(scale.cl_buffer(), CL_TRUE, 0, num_elements * element_size, qinfo.scale().data());
96 }
97 } // namespace
98 
100  : _associated_memory_group(nullptr), _memory(), _mapping(nullptr), _owner(owner), _scale(), _offset()
101 {
102 }
103 
105 {
106  return { &_scale, &_offset };
107 }
108 
110 {
111  return _mapping;
112 }
113 
114 const cl::Buffer &CLTensorAllocator::cl_data() const
115 {
116  return _memory.region() == nullptr ? _empty_buffer : _memory.cl_region()->cl_data();
117 }
118 
120 {
121  // Allocate tensor backing memory
122  if(_associated_memory_group == nullptr)
123  {
124  if(_memory.region() != nullptr && _memory.cl_region()->cl_data().get() != nullptr)
125  {
126  // Memory is already allocated. Reuse it if big enough, otherwise fire an assertion
127  ARM_COMPUTE_ERROR_ON_MSG(info().total_size() > _memory.region()->size(),
128  "Reallocation of a bigger memory region is not allowed!");
129  }
130  else
131  {
132  // Perform memory allocation
133  _memory.set_owned_region(allocate_region(CLScheduler::get().context(), info().total_size(), 0));
134  }
135  }
136  else
137  {
138  _associated_memory_group->finalize_memory(_owner, _memory, info().total_size());
139  }
140 
141  // Allocate and fill the quantization parameter arrays
143  {
144  const size_t pad_size = 0;
145  populate_quantization_info(_scale, _offset, info().quantization_info(), pad_size);
146  }
147 
148  // Lock allocator
149  info().set_is_resizable(false);
150 }
151 
153 {
154  _mapping = nullptr;
155  _memory.set_region(nullptr);
156  clear_quantization_arrays(_scale, _offset);
157  info().set_is_resizable(true);
158 }
159 
161 {
162  ARM_COMPUTE_RETURN_ERROR_ON(buffer.get() == nullptr);
163  ARM_COMPUTE_RETURN_ERROR_ON(buffer.getInfo<CL_MEM_SIZE>() < info().total_size());
164  ARM_COMPUTE_RETURN_ERROR_ON(buffer.getInfo<CL_MEM_CONTEXT>().get() != CLScheduler::get().context().get());
165  ARM_COMPUTE_RETURN_ERROR_ON(_associated_memory_group != nullptr);
166 
167  _memory.set_owned_region(support::cpp14::make_unique<CLBufferMemoryRegion>(buffer));
168  info().set_is_resizable(false);
169 
170  return Status{};
171 }
172 
174 {
175  ARM_COMPUTE_ERROR_ON(associated_memory_group == nullptr);
176  ARM_COMPUTE_ERROR_ON(_associated_memory_group != nullptr);
177  ARM_COMPUTE_ERROR_ON(_memory.region() != nullptr && _memory.cl_region()->cl_data().get() != nullptr);
178 
179  _associated_memory_group = associated_memory_group;
180 }
181 
182 uint8_t *CLTensorAllocator::lock()
183 {
184  return map(CLScheduler::get().queue(), true);
185 }
186 
187 void CLTensorAllocator::unlock()
188 {
189  ARM_COMPUTE_ERROR_ON(_memory.region() == nullptr);
190  unmap(CLScheduler::get().queue(), reinterpret_cast<uint8_t *>(_memory.region()->buffer()));
191 }
192 
193 uint8_t *CLTensorAllocator::map(cl::CommandQueue &q, bool blocking)
194 {
195  ARM_COMPUTE_ERROR_ON(_mapping != nullptr);
196  ARM_COMPUTE_ERROR_ON(_memory.region() == nullptr);
197  ARM_COMPUTE_ERROR_ON(_memory.region()->buffer() != nullptr);
198 
199  _mapping = reinterpret_cast<uint8_t *>(_memory.cl_region()->map(q, blocking));
200  return _mapping;
201 }
202 
203 void CLTensorAllocator::unmap(cl::CommandQueue &q, uint8_t *mapping)
204 {
205  ARM_COMPUTE_ERROR_ON(_mapping == nullptr);
206  ARM_COMPUTE_ERROR_ON(_mapping != mapping);
207  ARM_COMPUTE_ERROR_ON(_memory.region() == nullptr);
208  ARM_COMPUTE_ERROR_ON(_memory.region()->buffer() == nullptr);
209  ARM_COMPUTE_UNUSED(mapping);
210 
211  _memory.cl_region()->unmap(q);
212  _mapping = nullptr;
213 }
214 } // namespace arm_compute
__global uchar * offset(const Image *img, int x, int y)
Get the pointer position of a Image.
Definition: helpers.h:328
CLTensorAllocator(CLTensor *owner=nullptr)
Default constructor.
static CLScheduler & get()
Access the scheduler singleton.
Definition: CLScheduler.cpp:41
CLQuantization quantization() const
Wrapped quantization info data accessor.
#define ARM_COMPUTE_ERROR_ON(cond)
If the condition is true then an error message is printed and an exception thrown.
Definition: Error.h:337
virtual void * buffer()=0
Returns the pointer to the allocated data.
Status class.
Definition: Error.h:52
#define ARM_COMPUTE_RETURN_ERROR_ON(cond)
If the condition is true, an error is returned.
Definition: Error.h:244
OpenCL quantization data.
Definition: CLTypes.h:59
Copyright (c) 2017-2018 ARM Limited.
Status import_memory(cl::Buffer buffer)
Import an existing memory as a tensor's backing memory.
const cl::Buffer & cl_data() const
Returns the underlying CL buffer.
CLArray< cl_float > CLFloatArray
OpenCL Array of floats.
Definition: CLArray.h:125
uint8_t * data()
Interface to be implemented by the child class to return the pointer to the mapped data.
#define ARM_COMPUTE_UNUSED(...)
To avoid unused variables warnings.
Definition: Error.h:160
uint8_t * map(cl::CommandQueue &q, bool blocking)
Enqueue a map operation of the allocated buffer on the given queue.
void set_owned_region(std::unique_ptr< IMemoryRegion > region) final
Sets a memory region.
Definition: CLMemory.cpp:76
void finalize_memory(TensorType *obj, IMemory &obj_memory, size_t size, size_t alignment=0)
Finalizes memory for a given object.
IMemoryRegion * region() final
Region accessor.
Definition: CLMemory.cpp:59
const std::vector< float > & scale() const
Scale vector accessor.
ICLMemoryRegion * cl_region()
OpenCL Region accessor.
Definition: CLMemory.cpp:49
void unmap(cl::CommandQueue &q, uint8_t *mapping)
Enqueue an unmap operation of the allocated buffer on the given queue.
ITensorInfo & set_is_resizable(bool is_resizable) override
Set the flag whether the tensor size can be changed.
Definition: TensorInfo.h:280
cl::CommandQueue & queue()
Accessor for the associated CL command queue.
Definition: CLScheduler.h:102
virtual void * map(cl::CommandQueue &q, bool blocking)=0
Enqueue a map operation of the allocated buffer on the given queue.
TensorInfo & info()
Return a reference to the tensor's metadata.
void set_region(IMemoryRegion *region) final
Sets a memory region.
Definition: CLMemory.cpp:69
quantized, symmetric per channel fixed-point 8-bit number
void allocate() override
Allocate size specified by TensorInfo of OpenCL memory.
CLArray< cl_int > CLInt32Array
OpenCL Array of int32s.
Definition: CLArray.h:123
void set_associated_memory_group(CLMemoryGroup *associated_memory_group)
Associates the tensor with a memory group.
const QuantizationInfo qinfo
Definition: Im2Col.cpp:150
const cl::Buffer & cl_data() const
Interface to be implemented by the child class to return the pointer to the CL data.
void free() override
Free allocated OpenCL memory.
virtual void unmap(cl::CommandQueue &q)=0
Enqueue an unmap operation of the allocated buffer on the given queue.
size_t size() const
Memory region size accessor.
Definition: IMemoryRegion.h:73
cl::Context & context()
Accessor for the associated CL context.
Definition: CLScheduler.h:91
#define ARM_COMPUTE_ERROR_ON_MSG(cond,...)
Definition: Error.h:328
Basic implementation of the OpenCL tensor interface.
Definition: CLTensor.h:40