Compute Library
 21.05
OpenCLMemoryUsage Class Reference

Instrument collecting memory usage information for OpenCL. More...

#include <OpenCLMemoryUsage.h>

Collaboration diagram for OpenCLMemoryUsage:
[legend]

Public Member Functions

 OpenCLMemoryUsage (ScaleFactor scale_factor)
 Construct an OpenCL timer. More...
 
std::string id () const override
 Identifier for the instrument. More...
 
void test_start () override
 Start of the test. More...
 
void start () override
 Start measuring. More...
 
void stop () override
 Stop measuring. More...
 
void test_stop () override
 End of the test. More...
 
MeasurementsMap test_measurements () const override
 Return the latest test measurements. More...
 
MeasurementsMap measurements () const override
 Return the latest measurements. More...
 
- Public Member Functions inherited from Instrument
 Instrument ()=default
 Default constructor. More...
 
 Instrument (const Instrument &)=default
 Allow instances of this class to be copy constructed. More...
 
 Instrument (Instrument &&)=default
 Allow instances of this class to be move constructed. More...
 
Instrumentoperator= (const Instrument &)=default
 Allow instances of this class to be copied. More...
 
Instrumentoperator= (Instrument &&)=default
 Allow instances of this class to be moved. More...
 
virtual ~Instrument ()=default
 Default destructor. More...
 

Additional Inherited Members

- Public Types inherited from Instrument
using MeasurementsMap = std::map< std::string, Measurement >
 Map of measurements. More...
 
- Static Public Member Functions inherited from Instrument
template<typename T , ScaleFactor scale>
static std::unique_ptr< Instrumentmake_instrument ()
 Helper function to create an instrument of the given type. More...
 

Detailed Description

Instrument collecting memory usage information for OpenCL.

Definition at line 42 of file OpenCLMemoryUsage.h.

Constructor & Destructor Documentation

◆ OpenCLMemoryUsage()

OpenCLMemoryUsage ( ScaleFactor  scale_factor)

Construct an OpenCL timer.

Parameters
[in]scale_factorMeasurement scale factor.

Definition at line 46 of file OpenCLMemoryUsage.cpp.

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()
49 {
50  switch(scale_factor)
51  {
52  case ScaleFactor::NONE:
53  _scale_factor = 1;
54  _unit = "";
55  break;
57  _scale_factor = 1000;
58  _unit = "K ";
59  break;
61  _scale_factor = 1000000;
62  _unit = "M ";
63  break;
64  default:
65  ARM_COMPUTE_ERROR("Invalid scale");
66  }
67 }
#define ARM_COMPUTE_ERROR(msg)
Print the given message then throw an std::runtime_error.
Definition: Error.h:352
static CLSymbols & get()
Get the static instance of CLSymbols.
Definition: OpenCL.cpp:45

Member Function Documentation

◆ id()

std::string id ( ) const
overridevirtual

Identifier for the instrument.

Implements Instrument.

Definition at line 41 of file OpenCLMemoryUsage.cpp.

42 {
43  return "OpenCLMemoryUsage";
44 }

◆ measurements()

Instrument::MeasurementsMap measurements ( ) const
overridevirtual

Return the latest measurements.

Returns
the latest measurements.

Reimplemented from Instrument.

Definition at line 176 of file OpenCLMemoryUsage.cpp.

177 {
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));
182 
183  return measurements;
184 }
MeasurementsMap measurements() const override
Return the latest measurements.
std::map< std::string, Measurement > MeasurementsMap
Map of measurements.
Definition: Instrument.h:109

Referenced by OpenCLMemoryUsage::test_measurements().

◆ start()

void start ( )
overridevirtual

Start measuring.

Called just before the run of the test starts

Reimplemented from Instrument.

Definition at line 157 of file OpenCLMemoryUsage.cpp.

158 {
159  _start = _now;
160 }

◆ stop()

void stop ( )
overridevirtual

Stop measuring.

Called just after the run of the test ends

Reimplemented from Instrument.

Definition at line 161 of file OpenCLMemoryUsage.cpp.

162 {
163  _end = _now;
164 }

◆ test_measurements()

Instrument::MeasurementsMap test_measurements ( ) const
overridevirtual

Return the latest test measurements.

Returns
the latest test measurements.

Reimplemented from Instrument.

Definition at line 185 of file OpenCLMemoryUsage.cpp.

186 {
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));
192 
193  size_t num_programs = CLKernelLibrary::get().get_built_programs().size();
194  size_t total_size = 0;
195  for(auto const &it : CLKernelLibrary::get().get_built_programs())
196  {
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);
199  }
200 
201  measurements.emplace("Num programs in cache", Measurement(num_programs, ""));
202  measurements.emplace("Total programs memory in cache", Measurement(total_size / _scale_factor, _unit));
203 
204  return measurements;
205 }
static CLKernelLibrary & get()
Access the KernelLibrary singleton.
SimpleTensor< T2 > accumulate(const SimpleTensor< T1 > &src, DataType output_data_type)
Definition: Accumulate.cpp:38
MeasurementsMap measurements() const override
Return the latest measurements.
std::map< std::string, Measurement > MeasurementsMap
Map of measurements.
Definition: Instrument.h:109
const std::map< std::string, cl::Program > & get_built_programs() const
Access the cache of built OpenCL programs.

References arm_compute::test::validation::reference::accumulate(), CLKernelLibrary::get(), CLKernelLibrary::get_built_programs(), and OpenCLMemoryUsage::measurements().

◆ test_start()

void test_start ( )
overridevirtual

Start of the test.

Called before the test set up starts

Reimplemented from Instrument.

Definition at line 69 of file OpenCLMemoryUsage.cpp.

70 {
71  _now = Stats();
72 
73  ARM_COMPUTE_ERROR_ON(CLSymbols::get().clCreateBuffer_ptr == nullptr);
75  cl_context context,
76  cl_mem_flags flags,
77  size_t size,
78  void *host_ptr,
79  cl_int * errcode_ret)
80  {
81  cl_mem retval = this->real_clCreateBuffer(context, flags, size, host_ptr, errcode_ret);
82  if(host_ptr != nullptr)
83  {
84  // If it's an SVM / external allocation;
85  size = 0;
86  }
87  else
88  {
89  _now.num_allocations++;
90  _now.in_use += size;
91  _now.total_allocated += size;
92  if(_now.in_use > _now.max_in_use)
93  {
94  _now.max_in_use = _now.in_use;
95  }
96  }
97  this->_allocations[retval] = Allocation(size);
98  return retval;
99  };
100  ARM_COMPUTE_ERROR_ON(CLSymbols::get().clRetainMemObject_ptr == nullptr);
101  CLSymbols::get().clRetainMemObject_ptr = [this](cl_mem memobj)
102  {
103  cl_int retval = this->real_clRetainMemObject(memobj);
104  this->_allocations[memobj].refcount++;
105  return retval;
106  };
107  ARM_COMPUTE_ERROR_ON(CLSymbols::get().clReleaseMemObject_ptr == nullptr);
108  CLSymbols::get().clReleaseMemObject_ptr = [this](cl_mem memobj)
109  {
110  cl_int retval = this->real_clRetainMemObject(memobj);
111  Allocation &alloc = this->_allocations[memobj];
112  if(--alloc.refcount == 0)
113  {
114  _now.in_use -= alloc.size;
115  }
116  return retval;
117  };
118 
119  //Only intercept the function if it exists:
120  if(CLSymbols::get().clSVMAlloc_ptr != nullptr)
121  {
122  CLSymbols::get().clSVMAlloc_ptr = [this](cl_context context, cl_svm_mem_flags flags, size_t size, cl_uint alignment)
123  {
124  void *retval = this->real_clSVMAlloc(context, flags, size, alignment);
125  if(retval != nullptr)
126  {
127  _svm_allocations[retval] = size;
128  _now.num_allocations++;
129  _now.in_use += size;
130  _now.total_allocated += size;
131  if(_now.in_use > _now.max_in_use)
132  {
133  _now.max_in_use = _now.in_use;
134  }
135  }
136  return retval;
137  };
138  }
139 
140  //Only intercept the function if it exists:
141  if(CLSymbols::get().clSVMFree_ptr != nullptr)
142  {
143  CLSymbols::get().clSVMFree_ptr = [this](cl_context context, void *svm_pointer)
144  {
145  this->real_clSVMFree(context, svm_pointer);
146  auto iterator = _svm_allocations.find(svm_pointer);
147  if(iterator != _svm_allocations.end())
148  {
149  size_t size = iterator->second;
150  _svm_allocations.erase(iterator);
151  _now.in_use -= size;
152  }
153  };
154  }
155 }
std::function< decltype(clSVMFree)> clSVMFree_ptr
Definition: OpenCL.h:134
std::function< decltype(clCreateBuffer)> clCreateBuffer_ptr
Definition: OpenCL.h:102
std::function< decltype(clReleaseMemObject)> clReleaseMemObject_ptr
Definition: OpenCL.h:122
#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
std::function< decltype(clSVMAlloc)> clSVMAlloc_ptr
Definition: OpenCL.h:133
std::function< decltype(clRetainMemObject)> clRetainMemObject_ptr
Definition: OpenCL.h:121
static CLSymbols & get()
Get the static instance of CLSymbols.
Definition: OpenCL.cpp:45

References ARM_COMPUTE_ERROR_ON, CLSymbols::clCreateBuffer_ptr, CLSymbols::clReleaseMemObject_ptr, CLSymbols::clRetainMemObject_ptr, CLSymbols::clSVMAlloc_ptr, CLSymbols::clSVMFree_ptr, and CLSymbols::get().

◆ test_stop()

void test_stop ( )
overridevirtual

End of the test.

Called after the test teardown ended

Reimplemented from Instrument.

Definition at line 166 of file OpenCLMemoryUsage.cpp.

167 {
168  // Restore real function
169  CLSymbols::get().clCreateBuffer_ptr = real_clCreateBuffer;
170  CLSymbols::get().clRetainMemObject_ptr = real_clRetainMemObject;
171  CLSymbols::get().clReleaseMemObject_ptr = real_clReleaseMemObject;
172  CLSymbols::get().clSVMAlloc_ptr = real_clSVMAlloc;
173  CLSymbols::get().clSVMFree_ptr = real_clSVMFree;
174 }
std::function< decltype(clSVMFree)> clSVMFree_ptr
Definition: OpenCL.h:134
std::function< decltype(clCreateBuffer)> clCreateBuffer_ptr
Definition: OpenCL.h:102
std::function< decltype(clReleaseMemObject)> clReleaseMemObject_ptr
Definition: OpenCL.h:122
std::function< decltype(clSVMAlloc)> clSVMAlloc_ptr
Definition: OpenCL.h:133
std::function< decltype(clRetainMemObject)> clRetainMemObject_ptr
Definition: OpenCL.h:121
static CLSymbols & get()
Get the static instance of CLSymbols.
Definition: OpenCL.cpp:45

References CLSymbols::clCreateBuffer_ptr, CLSymbols::clReleaseMemObject_ptr, CLSymbols::clRetainMemObject_ptr, CLSymbols::clSVMAlloc_ptr, CLSymbols::clSVMFree_ptr, and CLSymbols::get().


The documentation for this class was generated from the following files: