xref: /aosp_15_r20/external/ComputeLibrary/tests/framework/instruments/OpenCLTimer.h (revision c217d954acce2dbc11938adb493fc0abd69584f3)
1 /*
2  * Copyright (c) 2017-2018, 2021 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_TIMER
25 #define ARM_COMPUTE_TEST_OPENCL_TIMER
26 
27 #include "Instrument.h"
28 
29 #ifdef ARM_COMPUTE_CL
30 #include "arm_compute/core/CL/OpenCL.h"
31 #endif /* ARM_COMPUTE_CL */
32 
33 #include "arm_compute/graph/Workload.h"
34 
35 #include <list>
36 
37 namespace arm_compute
38 {
39 namespace test
40 {
41 namespace framework
42 {
43 /** Instrument creating measurements based on the information returned by clGetEventProfilingInfo for each OpenCL kernel executed*/
44 template <bool output_timestamps>
45 class OpenCLClock : public Instrument
46 {
47 public:
48     /** Construct an OpenCL timer.
49      *
50      * @param[in] scale_factor Measurement scale factor.
51      */
52     OpenCLClock(ScaleFactor scale_factor);
53     std::string     id() const override;
54     void            test_start() override;
55     void            start() override;
56     void            stop() override;
57     void            test_stop() override;
58     MeasurementsMap measurements() const override;
59     MeasurementsMap test_measurements() const override;
60 
61 private:
62 #ifdef ARM_COMPUTE_CL
63     struct kernel_info
64     {
65         ::cl::Event event{}; /**< OpenCL event associated to the kernel enqueue */
66         std::string name{};  /**< OpenCL Kernel name */
67     };
68     std::list<kernel_info>                          _kernels;
69     std::function<decltype(clEnqueueNDRangeKernel)> _real_function;
70 #ifdef ARM_COMPUTE_GRAPH_ENABLED
71     std::function<decltype(graph::execute_task)> _real_graph_function;
72 #endif /* ARM_COMPUTE_GRAPH_ENABLED */
73     std::string _prefix;
74     bool        _timer_enabled;
75 #endif /* ARM_COMPUTE_CL */
76 
77 private:
78     float _scale_factor{};
79 };
80 
81 using OpenCLTimer      = OpenCLClock<false>;
82 using OpenCLTimestamps = OpenCLClock<true>;
83 
84 } // namespace framework
85 } // namespace test
86 } // namespace arm_compute
87 #endif /* ARM_COMPUTE_TEST_OPENCL_TIMER */
88