Compute Library
 21.05
OpenCLMemoryUsage.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018 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_TEST_OPENCL_MEMORY_USAGE
25 #define ARM_COMPUTE_TEST_OPENCL_MEMORY_USAGE
26 
27 #include "Instrument.h"
28 
29 #ifdef ARM_COMPUTE_CL
31 #endif /* ARM_COMPUTE_CL */
32 
33 #include <list>
34 
35 namespace arm_compute
36 {
37 namespace test
38 {
39 namespace framework
40 {
41 /** Instrument collecting memory usage information for OpenCL*/
43 {
44 public:
45  /** Construct an OpenCL timer.
46  *
47  * @param[in] scale_factor Measurement scale factor.
48  */
49  OpenCLMemoryUsage(ScaleFactor scale_factor);
50  std::string id() const override;
51  void test_start() override;
52  void start() override;
53  void stop() override;
54  void test_stop() override;
55  MeasurementsMap test_measurements() const override;
56  MeasurementsMap measurements() const override;
57 #ifdef ARM_COMPUTE_CL
58  std::function<decltype(clCreateBuffer)> real_clCreateBuffer;
59  std::function<decltype(clRetainMemObject)> real_clRetainMemObject;
60  std::function<decltype(clReleaseMemObject)> real_clReleaseMemObject;
61  std::function<decltype(clSVMAlloc)> real_clSVMAlloc;
62  std::function<decltype(clSVMFree)> real_clSVMFree;
63 
64 private:
65  float _scale_factor{};
66  struct Allocation
67  {
68  Allocation() = default;
69  Allocation(size_t alloc_size)
70  : size(alloc_size)
71  {
72  }
73  size_t size{ 0 };
74  int refcount{ 1 };
75  };
76  std::map<cl_mem, Allocation> _allocations;
77  std::map<void *, size_t> _svm_allocations;
78  struct Stats
79  {
80  size_t total_allocated{ 0 };
81  size_t max_in_use{ 0 };
82  size_t in_use{ 0 };
83  size_t num_allocations{ 0 };
84  } _start, _end, _now;
85 #endif /* ARM_COMPUTE_CL */
86 };
87 } // namespace framework
88 } // namespace test
89 } // namespace arm_compute
90 #endif /* ARM_COMPUTE_TEST_OPENCL_MEMORY_USAGE */
cl_int clRetainMemObject(cl_mem memobj)
Definition: OpenCL.cpp:413
void test_start() override
Start of the test.
std::string id() const override
Identifier for the instrument.
Copyright (c) 2017-2021 Arm Limited.
void clSVMFree(cl_context context, void *svm_pointer)
Definition: OpenCL.cpp:252
Instrument collecting memory usage information for OpenCL.
MeasurementsMap measurements() const override
Return the latest measurements.
cl_mem clCreateBuffer(cl_context context, cl_mem_flags flags, size_t size, void *host_ptr, cl_int *errcode_ret)
Definition: OpenCL.cpp:759
MeasurementsMap test_measurements() const override
Return the latest test measurements.
OpenCLMemoryUsage(ScaleFactor scale_factor)
Construct an OpenCL timer.
Interface for classes that can be used to measure performance.
Definition: Instrument.h:50
void * clSVMAlloc(cl_context context, cl_svm_mem_flags_arm flags, size_t size, cl_uint alignment)
Definition: OpenCL.cpp:238
Wrapper to configure the Khronos OpenCL C++ header.
std::map< std::string, Measurement > MeasurementsMap
Map of measurements.
Definition: Instrument.h:109
cl_int clReleaseMemObject(cl_mem memobj)
Definition: OpenCL.cpp:427