-
Notifications
You must be signed in to change notification settings - Fork 56
/
cl_kernel_collector.h
393 lines (323 loc) · 12.1 KB
/
cl_kernel_collector.h
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
//==============================================================
// Copyright (C) Intel Corporation
//
// SPDX-License-Identifier: MIT
// =============================================================
#ifndef PTI_SAMPLES_CL_HOT_KERNELS_CL_KERNEL_COLLECTOR_H_
#define PTI_SAMPLES_CL_HOT_KERNELS_CL_KERNEL_COLLECTOR_H_
#include <list>
#include <map>
#include <mutex>
#include <string>
#include <vector>
#include <algorithm>
#include "cl_api_tracer.h"
#include "cl_utils.h"
class ClKernelCollector;
struct ClEventData {
ClKernelCollector* collector;
std::string kernel_name;
cl_kernel kernel;
};
struct ClKernelInfo {
uint64_t total_time;
uint64_t min_time;
uint64_t max_time;
uint64_t call_count;
size_t simd_width;
bool operator>(const ClKernelInfo& r) const {
if (total_time != r.total_time) {
return total_time > r.total_time;
}
return call_count > r.call_count;
}
bool operator!=(const ClKernelInfo& r) const {
if (total_time == r.total_time) {
return call_count != r.call_count;
}
return true;
}
};
struct ClKernelInterval {
std::string name;
uint64_t start;
uint64_t end;
};
using ClKernelInfoMap = std::map<std::string, ClKernelInfo>;
#ifdef PTI_KERNEL_INTERVALS
using ClKernelIntervalList = std::vector<ClKernelInterval>;
#endif // PTI_KERNEL_INTERVALS
class ClKernelCollector {
public: // Interface
static ClKernelCollector* Create(cl_device_id device) {
PTI_ASSERT(device != nullptr);
ClKernelCollector* collector = new ClKernelCollector(device);
PTI_ASSERT(collector != nullptr);
ClApiTracer* tracer = new ClApiTracer(device, Callback, collector);
if (tracer == nullptr || !tracer->IsValid()) {
std::cerr << "[WARNING] Unable to create OpenCL tracer " <<
"for target device" << std::endl;
if (tracer != nullptr) {
delete tracer;
}
delete collector;
return nullptr;
}
collector->EnableTracing(tracer);
return collector;
}
~ClKernelCollector() {
if (tracer_ != nullptr) {
delete tracer_;
}
}
void DisableTracing() {
PTI_ASSERT(tracer_ != nullptr);
bool disabled = tracer_->Disable();
PTI_ASSERT(disabled);
}
const ClKernelInfoMap& GetKernelInfoMap() const {
return kernel_info_map_;
}
#ifdef PTI_KERNEL_INTERVALS
const ClKernelIntervalList& GetKernelIntervalList() const {
return kernel_interval_list_;
}
#endif // PTI_KERNEL_INTERVALS
ClKernelCollector(const ClKernelCollector& copy) = delete;
ClKernelCollector& operator=(const ClKernelCollector& copy) = delete;
static void PrintKernelsTable(const ClKernelInfoMap& kernel_info_map) {
std::set< std::pair<std::string, ClKernelInfo>,
utils::Comparator > sorted_list(
kernel_info_map.begin(), kernel_info_map.end());
uint64_t total_duration = 0;
size_t max_name_length = kKernelLength;
for (auto& value : sorted_list) {
total_duration += value.second.total_time;
if (value.first.size() > max_name_length) {
max_name_length = value.first.size();
}
}
if (total_duration == 0) {
return;
}
std::cerr << std::setw(max_name_length) << "Kernel" << "," <<
std::setw(kCallsLength) << "Calls" << "," <<
std::setw(kSimdLength) << "SIMD" << "," <<
std::setw(kTimeLength) << "Time (ns)" << "," <<
std::setw(kPercentLength) << "Time (%)" << "," <<
std::setw(kTimeLength) << "Average (ns)" << "," <<
std::setw(kTimeLength) << "Min (ns)" << "," <<
std::setw(kTimeLength) << "Max (ns)" << std::endl;
for (auto& value : sorted_list) {
const std::string& function = value.first;
uint64_t call_count = value.second.call_count;
size_t simd_width = value.second.simd_width;
uint64_t duration = value.second.total_time;
uint64_t avg_duration = duration / call_count;
uint64_t min_duration = value.second.min_time;
uint64_t max_duration = value.second.max_time;
float percent_duration = 100.0f * duration / total_duration;
std::cerr << std::setw(max_name_length) << function << "," <<
std::setw(kCallsLength) << call_count << "," <<
std::setw(kSimdLength) << simd_width << "," <<
std::setw(kTimeLength) << duration << "," <<
std::setw(kPercentLength) << std::setprecision(2) <<
std::fixed << percent_duration << "," <<
std::setw(kTimeLength) << avg_duration << "," <<
std::setw(kTimeLength) << min_duration << "," <<
std::setw(kTimeLength) << max_duration << std::endl;
}
}
private: // Implementation Details
ClKernelCollector(cl_device_id device) {
PTI_ASSERT(device != nullptr);
#ifdef PTI_KERNEL_INTERVALS
utils::cl::GetTimestamps(device, &host_sync_, &dev_sync_);
#endif // PTI_KERNEL_INTERVALS
}
void EnableTracing(ClApiTracer* tracer) {
PTI_ASSERT(tracer != nullptr);
tracer_ = tracer;
bool set = true;
set = set && tracer->SetTracingFunction(
CL_FUNCTION_clCreateCommandQueueWithProperties);
set = set && tracer->SetTracingFunction(CL_FUNCTION_clCreateCommandQueue);
set = set && tracer->SetTracingFunction(CL_FUNCTION_clEnqueueNDRangeKernel);
set = set && tracer->SetTracingFunction(CL_FUNCTION_clEnqueueReadBuffer);
set = set && tracer->SetTracingFunction(CL_FUNCTION_clEnqueueWriteBuffer);
PTI_ASSERT(set);
bool enabled = tracer_->Enable();
PTI_ASSERT(enabled);
}
void AddKernelInfo(std::string name, uint64_t time, size_t simd_width) {
PTI_ASSERT(!name.empty());
PTI_ASSERT(time > 0);
const std::lock_guard<std::mutex> lock(lock_);
if (kernel_info_map_.count(name) == 0) {
kernel_info_map_[name] = {time, time, time, 1, simd_width};
} else {
ClKernelInfo& kernel = kernel_info_map_[name];
kernel.total_time += time;
if (time > kernel.max_time) {
kernel.max_time = time;
}
if (time < kernel.min_time) {
kernel.min_time = time;
}
kernel.call_count += 1;
kernel.simd_width = (std::max)(kernel.simd_width, simd_width);
}
}
#ifdef PTI_KERNEL_INTERVALS
void AddKernelInterval(std::string name, uint64_t start, uint64_t end) {
PTI_ASSERT(!name.empty());
PTI_ASSERT(start < end);
const std::lock_guard<std::mutex> lock(lock_);
PTI_ASSERT(start >= dev_sync_);
uint64_t host_start = host_sync_ + (start - dev_sync_);
PTI_ASSERT(end >= start);
uint64_t host_end = host_start + (end - start);
kernel_interval_list_.push_back({name, host_start, host_end});
}
#endif // PTI_KERNEL_INTERVALS
private: // Callbacks
static void CL_CALLBACK EventNotify(
cl_event event, cl_int event_status, void* user_data) {
PTI_ASSERT(event_status == CL_COMPLETE);
PTI_ASSERT(user_data != nullptr);
ClEventData* event_data = reinterpret_cast<ClEventData*>(user_data);
ClKernelCollector* collector = event_data->collector;
PTI_ASSERT(collector != nullptr);
cl_command_queue queue = utils::cl::GetCommandQueue(event);
PTI_ASSERT(queue != nullptr);
std::string name = event_data->kernel_name;
PTI_ASSERT(!name.empty());
cl_ulong started =
utils::cl::GetEventTimestamp(event, CL_PROFILING_COMMAND_START);
cl_ulong ended =
utils::cl::GetEventTimestamp(event, CL_PROFILING_COMMAND_END);
cl_ulong time = ended - started;
PTI_ASSERT(time > 0);
cl_kernel kernel = event_data->kernel;
cl_device_id device = utils::cl::GetDevice(queue);
PTI_ASSERT(device != nullptr);
size_t simd_width = utils::cl::GetKernelSimdWidth(device, kernel);
PTI_ASSERT(simd_width > 0);
cl_int status = clReleaseKernel(kernel);
PTI_ASSERT(status == CL_SUCCESS);
collector->AddKernelInfo(name, time, simd_width);
#ifdef PTI_KERNEL_INTERVALS
collector->AddKernelInterval(name, started, ended);
#endif // PTI_KERNEL_INTERVALS
status = clReleaseEvent(event);
PTI_ASSERT(status == CL_SUCCESS);
delete event_data;
}
static void OnEnterCreateCommandQueueWithProperties(cl_callback_data* data) {
PTI_ASSERT(data != nullptr);
const cl_params_clCreateCommandQueueWithProperties* params =
reinterpret_cast<const cl_params_clCreateCommandQueueWithProperties*>(
data->functionParams);
PTI_ASSERT(params != nullptr);
cl_queue_properties* props =
utils::cl::EnableQueueProfiling(*(params->properties));
*(params->properties) = props;
data->correlationData[0] = reinterpret_cast<cl_ulong>(props);
}
static void OnExitCreateCommandQueueWithProperties(cl_callback_data* data) {
PTI_ASSERT(data != nullptr);
cl_queue_properties* props =
reinterpret_cast<cl_queue_properties*>(data->correlationData[0]);
PTI_ASSERT(props != nullptr);
delete[] props;
}
static void OnEnterCreateCommandQueue(cl_callback_data* data) {
PTI_ASSERT(data != nullptr);
const cl_params_clCreateCommandQueue* params =
reinterpret_cast<const cl_params_clCreateCommandQueue*>(
data->functionParams);
PTI_ASSERT(params != nullptr);
*(params->properties) |=
static_cast<unsigned long>(CL_QUEUE_PROFILING_ENABLE);
}
static void OnEnterEnqueueNDRangeKernel(cl_callback_data* data) {
PTI_ASSERT(data != nullptr);
const cl_params_clEnqueueNDRangeKernel* params =
reinterpret_cast<const cl_params_clEnqueueNDRangeKernel*>(
data->functionParams);
PTI_ASSERT(params != nullptr);
if (*(params->event) == nullptr) {
*(params->event) = reinterpret_cast<cl_event*>(data->correlationData);
}
}
static void OnExitEnqueueNDRangeKernel(
cl_callback_data* data, ClKernelCollector* collector) {
PTI_ASSERT(data != nullptr);
const cl_params_clEnqueueNDRangeKernel* params =
reinterpret_cast<const cl_params_clEnqueueNDRangeKernel*>(
data->functionParams);
PTI_ASSERT(params != nullptr);
cl_int* return_value =
reinterpret_cast<cl_int*>(data->functionReturnValue);
if (*return_value == CL_SUCCESS) {
PTI_ASSERT(*(params->event) != nullptr);
cl_int status = CL_SUCCESS;
if (*(params->event) !=
reinterpret_cast<cl_event*>(data->correlationData)) {
status = clRetainEvent(**(params->event));
PTI_ASSERT(status == CL_SUCCESS);
}
ClEventData* event_data = new ClEventData;
PTI_ASSERT(event_data != nullptr);
cl_kernel kernel = *(params->kernel);
event_data->collector = collector;
event_data->kernel_name = utils::cl::GetKernelName(kernel);
event_data->kernel = kernel;
status = clRetainKernel(kernel);
PTI_ASSERT(status == CL_SUCCESS);
status = clSetEventCallback(
**(params->event), CL_COMPLETE, EventNotify, event_data);
PTI_ASSERT(status == CL_SUCCESS);
}
}
static void Callback(cl_function_id function,
cl_callback_data* callback_data,
void* user_data) {
ClKernelCollector* collector =
reinterpret_cast<ClKernelCollector*>(user_data);
PTI_ASSERT(collector != nullptr);
if (function == CL_FUNCTION_clCreateCommandQueueWithProperties) {
if (callback_data->site == CL_CALLBACK_SITE_ENTER) {
OnEnterCreateCommandQueueWithProperties(callback_data);
} else {
OnExitCreateCommandQueueWithProperties(callback_data);
}
} else if (function == CL_FUNCTION_clCreateCommandQueue) {
if (callback_data->site == CL_CALLBACK_SITE_ENTER) {
OnEnterCreateCommandQueue(callback_data);
}
} else if (function == CL_FUNCTION_clEnqueueNDRangeKernel) {
if (callback_data->site == CL_CALLBACK_SITE_ENTER) {
OnEnterEnqueueNDRangeKernel(callback_data);
} else {
OnExitEnqueueNDRangeKernel(callback_data, collector);
}
}
}
private: // Data
ClApiTracer* tracer_ = nullptr;
std::mutex lock_;
ClKernelInfoMap kernel_info_map_;
static const uint32_t kKernelLength = 10;
static const uint32_t kCallsLength = 12;
static const uint32_t kSimdLength = 5;
static const uint32_t kTimeLength = 20;
static const uint32_t kPercentLength = 10;
#ifdef PTI_KERNEL_INTERVALS
ClKernelIntervalList kernel_interval_list_;
cl_ulong host_sync_ = 0;
cl_ulong dev_sync_ = 0;
#endif // PTI_KERNEL_INTERVALS
};
#endif // PTI_SAMPLES_CL_HOT_KERNELS_CL_KERNEL_COLLECTOR_H_