forked from intel/pti-gpu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tool.cc
155 lines (129 loc) · 4.03 KB
/
tool.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
//==============================================================
// Copyright (C) Intel Corporation
//
// SPDX-License-Identifier: MIT
// =============================================================
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <map>
#include <memory>
#include <mutex>
#include <set>
#include "cl_kernel_collector.h"
static ClKernelCollector* cpu_collector = nullptr;
static ClKernelCollector* gpu_collector = nullptr;
static std::chrono::steady_clock::time_point start;
// External Tool Interface ////////////////////////////////////////////////////
extern "C"
#if defined(_WIN32)
__declspec(dllexport)
#endif
void Usage() {
std::cout <<
"Usage: ./cl_hot_functions[.exe] <application> <args>" <<
std::endl;
}
extern "C"
#if defined(_WIN32)
__declspec(dllexport)
#endif
int ParseArgs(int argc, char* argv[]) {
return 1;
}
extern "C"
#if defined(_WIN32)
__declspec(dllexport)
#endif
void SetToolEnv() {}
// Internal Tool Functionality ////////////////////////////////////////////////
static uint64_t CalculateTotalTime(ClKernelCollector* collector) {
PTI_ASSERT(collector != nullptr);
uint64_t total_duration = 0;
const ClKernelInfoMap& kernel_info_map = collector->GetKernelInfoMap();
if (kernel_info_map.size() != 0) {
for (auto& value : kernel_info_map) {
total_duration += value.second.total_time;
}
}
return total_duration;
}
static void PrintDeviceTable(
ClKernelCollector* collector, const char* device_type) {
PTI_ASSERT(collector != nullptr);
PTI_ASSERT(device_type != nullptr);
uint64_t total_duration = CalculateTotalTime(collector);
if (total_duration > 0) {
std::cerr << std::endl;
std::cerr << "== " << device_type << " Backend: ==" << std::endl;
std::cerr << std::endl;
const ClKernelInfoMap& kernel_info_map = collector->GetKernelInfoMap();
PTI_ASSERT(kernel_info_map.size() > 0);
ClKernelCollector::PrintKernelsTable(kernel_info_map);
}
}
static void PrintResults() {
if (cpu_collector == nullptr && gpu_collector == nullptr) {
return;
}
std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now();
std::chrono::duration<uint64_t, std::nano> time = end - start;
std::cerr << std::endl;
std::cerr << "=== Device Timing Results: ===" << std::endl;
std::cerr << std::endl;
std::cerr << "Total Execution Time (ns): " << time.count() << std::endl;
if (cpu_collector != nullptr) {
std::cerr << "Total Device Time for CPU (ns): " <<
CalculateTotalTime(cpu_collector) << std::endl;
}
if (gpu_collector != nullptr) {
std::cerr << "Total Device Time for GPU (ns): " <<
CalculateTotalTime(gpu_collector) << std::endl;
}
if (cpu_collector != nullptr) {
PrintDeviceTable(cpu_collector, "CPU");
}
if (gpu_collector != nullptr) {
PrintDeviceTable(gpu_collector, "GPU");
}
std::cerr << std::endl;
}
// Internal Tool Interface ////////////////////////////////////////////////////
void EnableProfiling() {
cl_device_id cpu_device = utils::cl::GetIntelDevice(CL_DEVICE_TYPE_CPU);
cl_device_id gpu_device = utils::cl::GetIntelDevice(CL_DEVICE_TYPE_GPU);
if (cpu_device == nullptr && gpu_device == nullptr) {
std::cerr << "[WARNING] Unable to find device for tracing" << std::endl;
return;
}
if (gpu_device == nullptr) {
std::cerr << "[WARNING] Unable to find GPU device for tracing" <<
std::endl;
}
if (cpu_device == nullptr) {
std::cerr << "[WARNING] Unable to find CPU device for tracing" <<
std::endl;
}
if (cpu_device != nullptr) {
cpu_collector = ClKernelCollector::Create(cpu_device);
}
if (gpu_device != nullptr) {
gpu_collector = ClKernelCollector::Create(gpu_device);
}
start = std::chrono::steady_clock::now();
}
void DisableProfiling() {
if (cpu_collector != nullptr) {
cpu_collector->DisableTracing();
}
if (gpu_collector != nullptr) {
gpu_collector->DisableTracing();
}
PrintResults();
if (cpu_collector != nullptr) {
delete cpu_collector;
}
if (gpu_collector != nullptr) {
delete gpu_collector;
}
}