-
Notifications
You must be signed in to change notification settings - Fork 69
Expand file tree
/
Copy pathtransfer_bw.cpp
More file actions
395 lines (351 loc) · 13.6 KB
/
transfer_bw.cpp
File metadata and controls
395 lines (351 loc) · 13.6 KB
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
394
395
/*
*
* Copyright (C) 2019 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
*/
#include "../include/ze_peak.h"
#include "../../common/include/common.hpp"
long double ZePeak::_transfer_bw_gpu_copy(L0Context &context,
void *destination_buffer,
void *source_buffer,
size_t buffer_size) {
Timer<std::chrono::nanoseconds::period> timer;
long double gbps = 0;
ze_result_t result = ZE_RESULT_SUCCESS;
auto cmd_l = context.command_list;
auto cmd_q = context.command_queue;
if (context.copy_command_queue) {
cmd_l = context.copy_command_list;
cmd_q = context.copy_command_queue;
} else if (context.sub_device_count) {
cmd_l = context.cmd_list[current_sub_device_id];
cmd_q = context.cmd_queue[current_sub_device_id];
}
SUCCESS_OR_TERMINATE(zeCommandListReset(cmd_l));
SUCCESS_OR_TERMINATE(zeCommandListAppendMemoryCopy(cmd_l, destination_buffer,
source_buffer, buffer_size,
nullptr, 0, nullptr));
SUCCESS_OR_TERMINATE(zeCommandListClose(cmd_l));
for (uint32_t i = 0; i < warmup_iterations; i++) {
SUCCESS_OR_TERMINATE(
zeCommandQueueExecuteCommandLists(cmd_q, 1, &cmd_l, nullptr));
SUCCESS_OR_TERMINATE(zeCommandQueueSynchronize(cmd_q, UINT64_MAX));
}
timer.start();
for (uint32_t i = 0; i < iters; i++) {
SUCCESS_OR_TERMINATE(
zeCommandQueueExecuteCommandLists(cmd_q, 1, &cmd_l, nullptr));
SUCCESS_OR_TERMINATE(zeCommandQueueSynchronize(cmd_q, UINT64_MAX));
}
timer.end();
double timed = timer.period_minus_overhead();
timed /= static_cast<long double>(iters);
return calculate_gbps(timed, static_cast<long double>(buffer_size));
}
long double ZePeak::_transfer_bw_host_copy(L0Context &context,
void *destination_buffer,
void *source_buffer,
size_t buffer_size,
bool shared_is_dest) {
Timer<std::chrono::nanoseconds::period> timer;
long double gbps = 0, timed = 0;
ze_command_list_handle_t temp_cmd_list = nullptr;
ze_command_queue_desc_t cmd_q_desc = {};
cmd_q_desc.stype = ZE_STRUCTURE_TYPE_COMMAND_QUEUE_DESC;
cmd_q_desc.mode = ZE_COMMAND_QUEUE_MODE_SYNCHRONOUS;
if (context.sub_device_count) {
zeCommandListCreateImmediate(context.context,
context.sub_devices[current_sub_device_id],
&cmd_q_desc, &temp_cmd_list);
} else {
if (enable_fixed_ordinal_index) {
if (command_queue_group_ordinal >= context.queueProperties.size()) {
std::cout << "Specified command queue group "
<< command_queue_group_ordinal
<< " is not valid, defaulting to first group" << std::endl;
} else {
cmd_q_desc.ordinal = command_queue_group_ordinal;
if (command_queue_index <
context.queueProperties[command_queue_group_ordinal].numQueues) {
cmd_q_desc.index = command_queue_index;
} else {
cmd_q_desc.index = context.command_queue_id;
}
}
}
zeCommandListCreateImmediate(context.context, context.device, &cmd_q_desc,
&temp_cmd_list);
}
/*
Apply memory advise with preferred location set to system memory
to ensure best placement for kmd-migrated shared allocations (only).
*/
if (shared_is_dest) {
zeCommandListAppendMemAdvise(
temp_cmd_list, context.device, destination_buffer, buffer_size,
ZE_MEMORY_ADVICE_SET_SYSTEM_MEMORY_PREFERRED_LOCATION);
} else {
zeCommandListAppendMemAdvise(
temp_cmd_list, context.device, source_buffer, buffer_size,
ZE_MEMORY_ADVICE_SET_SYSTEM_MEMORY_PREFERRED_LOCATION);
}
for (uint32_t i = 0; i < warmup_iterations; i++) {
/*
This test uses a shared memory buffer to measure transfer bandwidth
between a host and device. The following helps to insure that the
buffer is located on the device.
*/
uint8_t pattern = 0x0;
size_t pattern_size = 1;
zeCommandListAppendMemoryFill(
temp_cmd_list, (shared_is_dest ? destination_buffer : source_buffer),
&pattern, pattern_size, buffer_size, nullptr, 0, nullptr);
memcpy(destination_buffer, source_buffer, buffer_size);
}
for (uint32_t i = 0; i < iters; i++) {
uint8_t pattern = 0x0;
size_t pattern_size = 1;
zeCommandListAppendMemoryFill(
temp_cmd_list, (shared_is_dest ? destination_buffer : source_buffer),
&pattern, pattern_size, buffer_size, nullptr, 0, nullptr);
timer.start();
memcpy(destination_buffer, source_buffer, buffer_size);
timed += timer.stopAndTime();
}
timed /= static_cast<long double>(iters);
gbps = calculate_gbps(timed, static_cast<long double>(buffer_size));
zeCommandListDestroy(temp_cmd_list);
return gbps;
}
void ZePeak::_transfer_bw_shared_memory(L0Context &context,
size_t local_memory_size,
void *local_memory) {
ze_result_t result = ZE_RESULT_SUCCESS;
long double gflops;
void *shared_memory_buffer = nullptr;
std::vector<void *> shared_buf;
ze_device_mem_alloc_desc_t device_desc = {};
device_desc.stype = ZE_STRUCTURE_TYPE_DEVICE_MEM_ALLOC_DESC;
device_desc.pNext = nullptr;
device_desc.ordinal = 0;
device_desc.flags = 0;
ze_host_mem_alloc_desc_t host_desc = {};
host_desc.stype = ZE_STRUCTURE_TYPE_HOST_MEM_ALLOC_DESC;
host_desc.pNext = nullptr;
host_desc.flags = 0;
if (context.sub_device_count) {
shared_buf.resize(context.sub_device_count);
uint32_t i = 0;
for (auto device : context.sub_devices) {
result = zeMemAllocShared(context.context, &device_desc, &host_desc,
local_memory_size / context.sub_device_count, 1,
device, &shared_buf[i]);
if (result) {
throw std::runtime_error("zeMemAllocShared failed: " +
std::to_string(result));
}
i++;
}
} else {
result = zeMemAllocShared(context.context, &device_desc, &host_desc,
local_memory_size, 1, context.device,
&shared_memory_buffer);
if (result) {
throw std::runtime_error("zeMemAllocShared failed: " +
std::to_string(result));
}
}
gflops = 0;
if (context.sub_device_count) {
current_sub_device_id = 0;
for (auto i = 0; i < context.sub_device_count; i++) {
gflops +=
_transfer_bw_gpu_copy(context, shared_buf[i], local_memory,
local_memory_size / context.sub_device_count);
current_sub_device_id++;
}
gflops = gflops / context.sub_device_count;
} else {
gflops = _transfer_bw_gpu_copy(context, shared_memory_buffer, local_memory,
local_memory_size);
}
std::cout << "GPU Copy Host to Shared Memory : ";
std::cout << gflops << " GBPS\n";
gflops = 0;
if (context.sub_device_count) {
current_sub_device_id = 0;
for (auto i = 0; i < context.sub_device_count; i++) {
gflops +=
_transfer_bw_gpu_copy(context, local_memory, shared_buf[i],
local_memory_size / context.sub_device_count);
current_sub_device_id++;
}
gflops = gflops / context.sub_device_count;
} else {
gflops = _transfer_bw_gpu_copy(context, local_memory, shared_memory_buffer,
local_memory_size);
}
std::cout << "GPU Copy Shared Memory to Host : ";
std::cout << gflops << " GBPS\n";
gflops = 0;
if (context.sub_device_count) {
current_sub_device_id = 0;
for (auto i = 0; i < context.sub_device_count; i++) {
gflops += _transfer_bw_host_copy(
context, shared_buf[i], local_memory,
local_memory_size / context.sub_device_count, true);
current_sub_device_id++;
}
gflops = gflops / context.sub_device_count;
} else {
gflops = _transfer_bw_host_copy(context, shared_memory_buffer, local_memory,
local_memory_size, true);
}
std::cout << "System Memory Copy to Shared Memory : ";
std::cout << gflops << " GBPS\n";
gflops = 0;
if (context.sub_device_count) {
current_sub_device_id = 0;
for (auto i = 0; i < context.sub_device_count; i++) {
gflops += _transfer_bw_host_copy(
context, local_memory, shared_buf[i],
local_memory_size / context.sub_device_count, false);
current_sub_device_id++;
}
gflops = gflops / context.sub_device_count;
} else {
gflops = _transfer_bw_host_copy(context, local_memory, shared_memory_buffer,
local_memory_size, false);
}
std::cout << "System Memory Copy from Shared Memory : ";
std::cout << gflops << " GBPS\n";
current_sub_device_id = 0;
if (context.sub_device_count) {
for (auto output_buf : shared_buf) {
result = zeMemFree(context.context, output_buf);
if (result) {
throw std::runtime_error("zeMemFree failed: " + std::to_string(result));
}
}
} else {
result = zeMemFree(context.context, shared_memory_buffer);
if (result) {
throw std::runtime_error("zeMemFree failed: " + std::to_string(result));
}
}
}
void ZePeak::ze_peak_transfer_bw(L0Context &context) {
ze_result_t result = ZE_RESULT_SUCCESS;
long double gflops;
uint64_t max_number_of_allocated_items =
context.device_property.maxMemAllocSize / sizeof(float) / 2;
uint64_t number_of_items = roundToMultipleOf(
max_number_of_allocated_items,
context.device_compute_property.maxGroupSizeX, transfer_bw_max_size);
size_t local_memory_size =
static_cast<size_t>((number_of_items * sizeof(float)));
void *host_memory = nullptr;
ze_host_mem_alloc_desc_t host_desc = {};
result = zeMemAllocHost(context.context, &host_desc, local_memory_size, 1,
&host_memory);
if (result) {
throw std::runtime_error("zeMemAllocHost failed: " +
std::to_string(result));
}
if (!host_memory) {
throw std::runtime_error("Failed to allocate host memory");
}
float *local_memory = reinterpret_cast<float *>(host_memory);
for (uint32_t i = 0; i < static_cast<uint32_t>(number_of_items); i++) {
local_memory[i] = static_cast<float>(i);
}
void *device_buffer;
std::vector<void *> dev_out_buf;
ze_device_mem_alloc_desc_t device_desc = {};
device_desc.stype = ZE_STRUCTURE_TYPE_DEVICE_MEM_ALLOC_DESC;
device_desc.pNext = nullptr;
device_desc.ordinal = 0;
device_desc.flags = 0;
if (context.sub_device_count) {
dev_out_buf.resize(context.sub_device_count);
uint32_t i = 0;
for (auto device : context.sub_devices) {
result = zeMemAllocDevice(context.context, &device_desc,
local_memory_size / context.sub_device_count, 1,
device, &dev_out_buf[i]);
if (result) {
throw std::runtime_error("zeMemAllocDevice failed: " +
std::to_string(result));
}
i++;
}
} else {
result = zeMemAllocDevice(context.context, &device_desc, local_memory_size,
1, context.device, &device_buffer);
if (result) {
throw std::runtime_error("zeMemAllocDevice failed: " +
std::to_string(result));
}
}
if (verbose)
std::cout << "device buffer allocated\n";
std::cout << "Transfer Bandwidth (GBPS)\n";
gflops = 0;
if (context.sub_device_count) {
current_sub_device_id = 0;
for (auto i = 0; i < context.sub_device_count; i++) {
gflops +=
_transfer_bw_gpu_copy(context, dev_out_buf[i], host_memory,
local_memory_size / context.sub_device_count);
current_sub_device_id++;
}
gflops = gflops / context.sub_device_count;
} else {
gflops = _transfer_bw_gpu_copy(context, device_buffer, host_memory,
local_memory_size);
}
std::cout << "enqueueWriteBuffer : ";
std::cout << gflops << " GBPS\n";
gflops = 0;
if (context.sub_device_count) {
current_sub_device_id = 0;
for (auto i = 0; i < context.sub_device_count; i++) {
gflops +=
_transfer_bw_gpu_copy(context, host_memory, dev_out_buf[i],
local_memory_size / context.sub_device_count);
current_sub_device_id++;
}
gflops = gflops / context.sub_device_count;
} else {
gflops = _transfer_bw_gpu_copy(context, host_memory, device_buffer,
local_memory_size);
}
std::cout << "enqueueReadBuffer : ";
std::cout << gflops << " GBPS\n";
current_sub_device_id = 0;
_transfer_bw_shared_memory(context, local_memory_size, local_memory);
if (context.sub_device_count) {
for (auto output_buf : dev_out_buf) {
result = zeMemFree(context.context, output_buf);
if (result) {
throw std::runtime_error("zeMemFree failed: " + std::to_string(result));
}
}
} else {
result = zeMemFree(context.context, device_buffer);
if (result) {
throw std::runtime_error("zeMemFree failed: " + std::to_string(result));
}
}
if (verbose)
std::cout << "Device Buffer freed\n";
result = zeMemFree(context.context, local_memory);
if (result) {
throw std::runtime_error("zeMemFree failed: " + std::to_string(result));
}
if (verbose)
std::cout << "Host Buffer freed\n";
print_test_complete();
}