Compute Library
 21.02
GCTensorAllocator.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017-2020 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 
26 
27 #include "arm_compute/core/Error.h"
31 
32 using namespace arm_compute;
33 
35  : _owner(owner), _associated_memory_group(nullptr), _memory(), _mapping(nullptr)
36 {
37 }
38 
40 {
41  return _mapping;
42 }
43 
45 {
46  if(_associated_memory_group == nullptr)
47  {
48  _memory.set_owned_region(std::make_unique<GCBufferMemoryRegion>(info().total_size()));
49  }
50  else
51  {
52  _associated_memory_group->finalize_memory(_owner, _memory, info().total_size(), alignment());
53  }
54  info().set_is_resizable(false);
55 }
56 
58 {
59  _mapping = nullptr;
60  _memory.set_region(nullptr);
61  info().set_is_resizable(true);
62 }
63 
65 {
66  ARM_COMPUTE_ERROR_ON(associated_memory_group == nullptr);
67  ARM_COMPUTE_ERROR_ON(_associated_memory_group != nullptr && _associated_memory_group != associated_memory_group);
68  ARM_COMPUTE_ERROR_ON(_memory.region() != nullptr && _memory.gc_region()->gc_ssbo_name() != 0);
69 
70  _associated_memory_group = associated_memory_group;
71 }
72 
73 uint8_t *GCTensorAllocator::lock()
74 {
75  return map(true);
76 }
77 
78 void GCTensorAllocator::unlock()
79 {
80  unmap();
81 }
82 
84 {
85  return (_memory.region() == nullptr) ? static_cast<GLuint>(0) : _memory.gc_region()->gc_ssbo_name();
86 }
87 
88 uint8_t *GCTensorAllocator::map(bool blocking)
89 {
90  ARM_COMPUTE_ERROR_ON(_mapping != nullptr);
91  ARM_COMPUTE_ERROR_ON(_memory.region() == nullptr);
92 
93  _mapping = reinterpret_cast<uint8_t *>(_memory.gc_region()->map(blocking));
94  return _mapping;
95 }
96 
98 {
99  ARM_COMPUTE_ERROR_ON(_mapping == nullptr);
100  ARM_COMPUTE_ERROR_ON(_memory.region() == nullptr);
101 
102  _memory.gc_region()->unmap();
103  _mapping = nullptr;
104 }
Memory group interface.
Definition: IMemoryGroup.h:37
const GLuint & gc_ssbo_name() const
Returns the underlying CL buffer.
uint8_t * map(bool blocking)
Enqueue a map operation of the allocated buffer on the given queue.
void free() override
Free allocated GLES memory.
void set_associated_memory_group(IMemoryGroup *associated_memory_group)
Associates the tensor with a memory group.
#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
void unmap()
Enqueue an unmap operation of the allocated buffer on the given queue.
IMemoryRegion * region() final
Region accessor.
Definition: GCMemory.cpp:59
Copyright (c) 2017-2021 Arm Limited.
virtual void unmap()=0
Enqueue an unmap operation of the allocated buffer on the given queue.
void allocate() override
Allocate size specified by TensorInfo of GLES memory.
virtual void finalize_memory(IMemoryManageable *obj, IMemory &obj_memory, size_t size, size_t alignment)=0
Finalizes memory for a given object.
uint8_t * data()
Interface to be implemented by the child class to return the pointer to the mapped data...
GLuint get_gl_ssbo_name() const
Get the OpenGL ES buffer object name.
size_t alignment() const
Return underlying&#39;s tensor buffer alignment.
ITensorInfo & set_is_resizable(bool is_resizable) override
Set the flag whether the tensor size can be changed.
Definition: TensorInfo.h:298
IGCMemoryRegion * gc_region()
GLES Region accessor.
Definition: GCMemory.cpp:49
TensorInfo & info()
Return a reference to the tensor&#39;s metadata.
Interface of an object than can be memory managed.
Definition: IMemoryGroup.h:69
GCTensorAllocator(IMemoryManageable *owner=nullptr)
Default constructor.
Interface to enqueue GLES kernels and get/set the GLES CommandQueue.
void set_region(IMemoryRegion *region) final
Sets a memory region.
Definition: GCMemory.cpp:69
virtual void * map(bool blocking)=0
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: GCMemory.cpp:76