26 #include "../Framework.h" 29 #ifndef ARM_COMPUTE_CL 30 #error "You can't use OpenCLMemoryUsage without OpenCL" 43 return "OpenCLMemoryUsage";
47 : real_clCreateBuffer(
CLSymbols::get().clCreateBuffer_ptr), real_clRetainMemObject(
CLSymbols::get().clRetainMemObject_ptr), real_clReleaseMemObject(
CLSymbols::get().clReleaseMemObject_ptr),
48 real_clSVMAlloc(
CLSymbols::get().clSVMAlloc_ptr), real_clSVMFree(
CLSymbols::get().clSVMFree_ptr), _allocations(), _svm_allocations(), _start(), _end(), _now()
61 _scale_factor = 1000000;
81 cl_mem retval = this->real_clCreateBuffer(context, flags, size, host_ptr, errcode_ret);
82 if(host_ptr !=
nullptr)
89 _now.num_allocations++;
91 _now.total_allocated += size;
92 if(_now.in_use > _now.max_in_use)
94 _now.max_in_use = _now.in_use;
97 this->_allocations[retval] = Allocation(size);
103 cl_int retval = this->real_clRetainMemObject(memobj);
104 this->_allocations[memobj].refcount++;
110 cl_int retval = this->real_clRetainMemObject(memobj);
111 Allocation &alloc = this->_allocations[memobj];
112 if(--alloc.refcount == 0)
114 _now.in_use -= alloc.size;
124 void *retval = this->real_clSVMAlloc(context, flags, size, alignment);
125 if(retval !=
nullptr)
127 _svm_allocations[retval] = size;
128 _now.num_allocations++;
130 _now.total_allocated += size;
131 if(_now.in_use > _now.max_in_use)
133 _now.max_in_use = _now.in_use;
145 this->real_clSVMFree(context, svm_pointer);
146 auto iterator = _svm_allocations.find(svm_pointer);
147 if(iterator != _svm_allocations.end())
149 size_t size = iterator->second;
150 _svm_allocations.erase(iterator);
179 measurements.emplace(
"Num buffers allocated per run",
Measurement(_end.num_allocations - _start.num_allocations,
""));
180 measurements.emplace(
"Total memory allocated per run",
Measurement((_end.total_allocated - _start.total_allocated) / _scale_factor, _unit));
181 measurements.emplace(
"Memory in use at start of run",
Measurement(_start.in_use / _scale_factor, _unit));
188 measurements.emplace(
"Num buffers",
Measurement(_now.num_allocations,
""));
189 measurements.emplace(
"Total memory allocated",
Measurement(_now.total_allocated / _scale_factor, _unit));
190 measurements.emplace(
"Max memory allocated",
Measurement(_now.max_in_use / _scale_factor, _unit));
191 measurements.emplace(
"Memory leaked",
Measurement(_now.in_use / _scale_factor, _unit));
194 size_t total_size = 0;
197 std::vector<size_t> binary_sizes = it.second.getInfo<CL_PROGRAM_BINARY_SIZES>();
198 total_size =
std::accumulate(binary_sizes.begin(), binary_sizes.end(), total_size);
201 measurements.emplace(
"Num programs in cache",
Measurement(num_programs,
""));
202 measurements.emplace(
"Total programs memory in cache",
Measurement(total_size / _scale_factor, _unit));
Class for loading OpenCL symbols.
std::function< decltype(clSVMFree)> clSVMFree_ptr
void start() override
Start measuring.
void test_start() override
Start of the test.
std::function< decltype(clCreateBuffer)> clCreateBuffer_ptr
#define ARM_COMPUTE_ERROR(msg)
Print the given message then throw an std::runtime_error.
std::function< decltype(clReleaseMemObject)> clReleaseMemObject_ptr
Generic measurement that stores values as either double or long long int.
#define ARM_COMPUTE_ERROR_ON(cond)
If the condition is true then an error message is printed and an exception thrown.
static CLKernelLibrary & get()
Access the KernelLibrary singleton.
std::string id() const override
Identifier for the instrument.
std::function< decltype(clSVMAlloc)> clSVMAlloc_ptr
Copyright (c) 2017-2021 Arm Limited.
MeasurementsMap measurements() const override
Return the latest measurements.
MeasurementsMap test_measurements() const override
Return the latest test measurements.
OpenCLMemoryUsage(ScaleFactor scale_factor)
Construct an OpenCL timer.
Manages all the OpenCL kernels compilation and caching, provides accessors for the OpenCL Context...
void stop() override
Stop measuring.
__kernel void accumulate(__global uchar *input_ptr, uint input_stride_x, uint input_step_x, uint input_stride_y, uint input_step_y, uint input_offset_first_element_in_bytes, __global uchar *accu_ptr, uint accu_stride_x, uint accu_step_x, uint accu_stride_y, uint accu_step_y, uint accu_offset_first_element_in_bytes)
This function accumulates an input image into output image.
std::map< std::string, Measurement > MeasurementsMap
Map of measurements.
std::function< decltype(clRetainMemObject)> clRetainMemObject_ptr
static CLSymbols & get()
Get the static instance of CLSymbols.
const std::map< std::string, cl::Program > & get_built_programs() const
Access the cache of built OpenCL programs.
void test_stop() override
End of the test.