-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathtest_sycl_queue_submit.cpp
More file actions
442 lines (382 loc) · 14.2 KB
/
Copy pathtest_sycl_queue_submit.cpp
File metadata and controls
442 lines (382 loc) · 14.2 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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
//===-- test_sycl_queue_submit.cpp - Test cases for kernel submission fns. ===//
//
// Data Parallel Control (dpctl)
//
// Copyright 2021 Intel Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
//===----------------------------------------------------------------------===//
///
/// \file
/// This file has unit test cases for the various submit functions defined
/// inside dpctl_sycl_queue_interface.cpp.
//===----------------------------------------------------------------------===//
#include "dpctl_sycl_context_interface.h"
#include "dpctl_sycl_device_interface.h"
#include "dpctl_sycl_device_selector_interface.h"
#include "dpctl_sycl_event_interface.h"
#include "dpctl_sycl_kernel_bundle_interface.h"
#include "dpctl_sycl_kernel_interface.h"
#include "dpctl_sycl_queue_interface.h"
#include "dpctl_sycl_type_casters.hpp"
#include "dpctl_sycl_usm_interface.h"
#include <stddef.h>
#include <cstdint>
#include <filesystem>
#include <fstream>
#include <gtest/gtest.h>
#include <iostream>
#include <sycl/sycl.hpp>
#include <utility>
namespace
{
static constexpr std::size_t SIZE = 1024;
static_assert(SIZE % 8 == 0);
using namespace dpctl::syclinterface;
template <typename T>
void submit_kernel(DPCTLSyclQueueRef QRef,
DPCTLSyclKernelBundleRef KBRef,
std::vector<char> spirvBuffer,
std::size_t spirvFileSize,
DPCTLKernelArgType kernelArgTy,
std::string kernelName)
{
T scalarVal = 3;
static constexpr std::size_t NARGS = 4;
static constexpr std::size_t RANGE_NDIMS_1 = 1;
static constexpr std::size_t RANGE_NDIMS_2 = 2;
static constexpr std::size_t RANGE_NDIMS_3 = 3;
ASSERT_TRUE(DPCTLKernelBundle_HasKernel(KBRef, kernelName.c_str()));
auto kernel = DPCTLKernelBundle_GetKernel(KBRef, kernelName.c_str());
// Create the input args
auto a = DPCTLmalloc_shared(SIZE * sizeof(T), QRef);
ASSERT_TRUE(a != nullptr);
auto b = DPCTLmalloc_shared(SIZE * sizeof(T), QRef);
ASSERT_TRUE(b != nullptr);
auto c = DPCTLmalloc_shared(SIZE * sizeof(T), QRef);
ASSERT_TRUE(c != nullptr);
// Create kernel args for vector_add
std::size_t Range[] = {SIZE};
void *args[NARGS] = {unwrap<void>(a), unwrap<void>(b), unwrap<void>(c),
(void *)&scalarVal};
DPCTLKernelArgType addKernelArgTypes[] = {DPCTL_VOID_PTR, DPCTL_VOID_PTR,
DPCTL_VOID_PTR, kernelArgTy};
auto E1Ref =
DPCTLQueue_SubmitRange(kernel, QRef, args, addKernelArgTypes, NARGS,
Range, RANGE_NDIMS_1, nullptr, 0);
ASSERT_TRUE(E1Ref != nullptr);
// Create kernel args for vector_add
std::size_t Range2D[] = {SIZE, 1};
DPCTLSyclEventRef DepEvs[] = {E1Ref};
auto E2Ref =
DPCTLQueue_SubmitRange(kernel, QRef, args, addKernelArgTypes, NARGS,
Range2D, RANGE_NDIMS_2, DepEvs, 1);
ASSERT_TRUE(E2Ref != nullptr);
// Create kernel args for vector_add
std::size_t Range3D[] = {SIZE, 1, 1};
DPCTLSyclEventRef DepEvs2[] = {E1Ref, E2Ref};
auto E3Ref =
DPCTLQueue_SubmitRange(kernel, QRef, args, addKernelArgTypes, NARGS,
Range3D, RANGE_NDIMS_3, DepEvs2, 2);
ASSERT_TRUE(E3Ref != nullptr);
DPCTLEvent_Wait(E3Ref);
// clean ups
DPCTLEvent_Delete(E1Ref);
DPCTLEvent_Delete(E2Ref);
DPCTLEvent_Delete(E3Ref);
DPCTLKernel_Delete(kernel);
DPCTLfree_with_queue((DPCTLSyclUSMRef)a, QRef);
DPCTLfree_with_queue((DPCTLSyclUSMRef)b, QRef);
DPCTLfree_with_queue((DPCTLSyclUSMRef)c, QRef);
}
} /* end of anonymous namespace */
/*
// The oneD_range_kernel spv files were generated from the SYCL program included
// in this comment. The program can be compiled using
// `icpx -fsycl oneD_range_kernel.cpp`. After that if the generated executable
// is run with the environment variable `SYCL_DUMP_IMAGES=1`, icpx runtime
// will dump all offload sections of fat binary to the current working
// directory. When tested with DPC++ 2024.0 the kernels are split across two
// separate SPV files. One contains all kernels for integers and FP32
// data type, and another contains the kernel for FP64.
//
// Note that, `SYCL_DUMP_IMAGES=1` will also generate extra SPV files that
// contain the code for built in functions such as indexing and barriers. To
// figure which SPV file contains the kernels, use `spirv-dis` from the
// spirv-tools package to translate the SPV binary format to a human-readable
// textual format.
#include <CL/sycl.hpp>
#include <iostream>
#include <sstream>
template <typename T>
class Range1DKernel
{
private:
T *a_ = nullptr;
T *b_ = nullptr;
T *c_ = nullptr;
T scalarVal_;
public:
RangeKernel(T *a, T *b, T *c, T scalarVal)
: a_(a), b_(b), c_(c), scalarVal_(scalarVal)
{
}
void operator()(sycl::item<1> it) const
{
auto i = it.get_id();
a_[i] = i + 1;
b_[i] = i + 2;
c_[i] = scalarVal_ * (a_[i] + b_[i]);
}
};
template <typename T>
void submit_kernel(
sycl::queue q,
const unsigned long N,
T *a,
T *b,
T *c,
T scalarVal)
{
// clang-format off
q.submit([&](auto &h) {
h.parallel_for(sycl::range(N), RangeKernel<T>(a, b, c, scalarVal));
});
// clang-format on
}
template <typename T>
void driver(std::size_t N)
{
sycl::queue q;
auto *a = sycl::malloc_shared<T>(N, q);
auto *b = sycl::malloc_shared<T>(N, q);
auto *c = sycl::malloc_shared<T>(N, q);
T scalarVal = 3;
submit_kernel(q, N, a, b, c, scalarVal);
q.wait();
std::cout << "C[0] : " << (size_t)c[0] << " " << std::endl;
sycl::free(a, q);
}
int main(int argc, const char **argv)
{
std::size_t N = 0;
std::cout << "Enter problem size in N:\n";
std::cin >> N;
std::cout << "Executing with N = " << N << std::endl;
driver<std::int8_t>(N);
driver<std::uint8_t>(N);
driver<std::int16_t>(N);
driver<std::uint16_t>(N);
driver<std::int32_t>(N);
driver<std::uint32_t>(N);
driver<std::int64_t>(N);
driver<std::uint64_t>(N);
driver<float>(N);
driver<double>(N);
return 0;
}
*/
struct TestQueueSubmit : public ::testing::Test
{
std::ifstream spirvFile;
std::size_t spirvFileSize_;
std::vector<char> spirvBuffer_;
DPCTLSyclQueueRef QRef = nullptr;
DPCTLSyclKernelBundleRef KBRef = nullptr;
TestQueueSubmit()
{
DPCTLSyclDeviceSelectorRef DSRef = nullptr;
DPCTLSyclDeviceRef DRef = nullptr;
spirvFile.open("./oneD_range_kernel_inttys_fp32.spv",
std::ios::binary | std::ios::ate);
spirvFileSize_ =
std::filesystem::file_size("./oneD_range_kernel_inttys_fp32.spv");
spirvBuffer_.reserve(spirvFileSize_);
spirvFile.seekg(0, std::ios::beg);
spirvFile.read(spirvBuffer_.data(), spirvFileSize_);
DSRef = DPCTLDefaultSelector_Create();
DRef = DPCTLDevice_CreateFromSelector(DSRef);
QRef =
DPCTLQueue_CreateForDevice(DRef, nullptr, DPCTL_DEFAULT_PROPERTY);
auto CRef = DPCTLQueue_GetContext(QRef);
KBRef = DPCTLKernelBundle_CreateFromSpirv(
CRef, DRef, spirvBuffer_.data(), spirvFileSize_, nullptr, 0,
nullptr);
DPCTLDevice_Delete(DRef);
DPCTLDeviceSelector_Delete(DSRef);
}
~TestQueueSubmit()
{
spirvFile.close();
DPCTLQueue_Delete(QRef);
DPCTLKernelBundle_Delete(KBRef);
}
};
struct TestQueueSubmitFP64 : public ::testing::Test
{
std::ifstream spirvFile;
std::size_t spirvFileSize_;
std::vector<char> spirvBuffer_;
DPCTLSyclDeviceRef DRef = nullptr;
DPCTLSyclQueueRef QRef = nullptr;
DPCTLSyclKernelBundleRef KBRef = nullptr;
TestQueueSubmitFP64()
{
DPCTLSyclDeviceSelectorRef DSRef = nullptr;
spirvFile.open("./oneD_range_kernel_fp64.spv",
std::ios::binary | std::ios::ate);
spirvFileSize_ =
std::filesystem::file_size("./oneD_range_kernel_fp64.spv");
spirvBuffer_.reserve(spirvFileSize_);
spirvFile.seekg(0, std::ios::beg);
spirvFile.read(spirvBuffer_.data(), spirvFileSize_);
DSRef = DPCTLDefaultSelector_Create();
DRef = DPCTLDevice_CreateFromSelector(DSRef);
QRef =
DPCTLQueue_CreateForDevice(DRef, nullptr, DPCTL_DEFAULT_PROPERTY);
auto CRef = DPCTLQueue_GetContext(QRef);
KBRef = DPCTLKernelBundle_CreateFromSpirv(
CRef, DRef, spirvBuffer_.data(), spirvFileSize_, nullptr, 0,
nullptr);
DPCTLDeviceSelector_Delete(DSRef);
}
~TestQueueSubmitFP64()
{
spirvFile.close();
DPCTLDevice_Delete(DRef);
DPCTLQueue_Delete(QRef);
DPCTLKernelBundle_Delete(KBRef);
}
};
TEST_F(TestQueueSubmit, CheckForInt8)
{
submit_kernel<std::int8_t>(QRef, KBRef, spirvBuffer_, spirvFileSize_,
DPCTLKernelArgType::DPCTL_INT8_T,
"_ZTS11RangeKernelIaE");
}
TEST_F(TestQueueSubmit, CheckForUInt8)
{
submit_kernel<std::uint8_t>(QRef, KBRef, spirvBuffer_, spirvFileSize_,
DPCTLKernelArgType::DPCTL_UINT8_T,
"_ZTS11RangeKernelIhE");
}
TEST_F(TestQueueSubmit, CheckForInt16)
{
submit_kernel<std::int16_t>(QRef, KBRef, spirvBuffer_, spirvFileSize_,
DPCTLKernelArgType::DPCTL_INT16_T,
"_ZTS11RangeKernelIsE");
}
TEST_F(TestQueueSubmit, CheckForUInt16)
{
submit_kernel<std::uint16_t>(QRef, KBRef, spirvBuffer_, spirvFileSize_,
DPCTLKernelArgType::DPCTL_UINT16_T,
"_ZTS11RangeKernelItE");
}
TEST_F(TestQueueSubmit, CheckForInt32)
{
submit_kernel<std::int32_t>(QRef, KBRef, spirvBuffer_, spirvFileSize_,
DPCTLKernelArgType::DPCTL_INT32_T,
"_ZTS11RangeKernelIiE");
}
TEST_F(TestQueueSubmit, CheckForUInt32)
{
submit_kernel<std::uint32_t>(QRef, KBRef, spirvBuffer_, spirvFileSize_,
DPCTLKernelArgType::DPCTL_UINT32_T,
"_ZTS11RangeKernelIjE");
}
TEST_F(TestQueueSubmit, CheckForInt64)
{
submit_kernel<std::int64_t>(QRef, KBRef, spirvBuffer_, spirvFileSize_,
DPCTLKernelArgType::DPCTL_INT64_T,
"_ZTS11RangeKernelIlE");
}
TEST_F(TestQueueSubmit, CheckForUInt64)
{
submit_kernel<std::uint64_t>(QRef, KBRef, spirvBuffer_, spirvFileSize_,
DPCTLKernelArgType::DPCTL_UINT64_T,
"_ZTS11RangeKernelImE");
}
TEST_F(TestQueueSubmit, CheckForFloat)
{
submit_kernel<float>(QRef, KBRef, spirvBuffer_, spirvFileSize_,
DPCTLKernelArgType::DPCTL_FLOAT32_T,
"_ZTS11RangeKernelIfE");
}
TEST_F(TestQueueSubmitFP64, CheckForDouble)
{
if (DPCTLDevice_HasAspect(DRef, DPCTLSyclAspectType::fp64)) {
submit_kernel<double>(QRef, KBRef, spirvBuffer_, spirvFileSize_,
DPCTLKernelArgType::DPCTL_FLOAT64_T,
"_ZTS11RangeKernelIdE");
}
}
TEST_F(TestQueueSubmit, CheckForUnsupportedArgTy)
{
int scalarVal = 3;
std::size_t Range[] = {SIZE};
std::size_t RANGE_NDIMS = 1;
static constexpr std::size_t NARGS = 4;
auto kernel = DPCTLKernelBundle_GetKernel(KBRef, "_ZTS11RangeKernelIdE");
void *args[NARGS] = {unwrap<void>(nullptr), unwrap<void>(nullptr),
unwrap<void>(nullptr), (void *)&scalarVal};
DPCTLKernelArgType addKernelArgTypes[] = {DPCTL_VOID_PTR, DPCTL_VOID_PTR,
DPCTL_VOID_PTR,
DPCTL_UNSUPPORTED_KERNEL_ARG};
auto ERef = DPCTLQueue_SubmitRange(kernel, QRef, args, addKernelArgTypes,
NARGS, Range, RANGE_NDIMS, nullptr, 0);
ASSERT_TRUE(ERef == nullptr);
}
struct TestQueueSubmitBarrier : public ::testing::Test
{
DPCTLSyclQueueRef QRef = nullptr;
TestQueueSubmitBarrier()
{
DPCTLSyclDeviceSelectorRef DSRef = nullptr;
DPCTLSyclDeviceRef DRef = nullptr;
EXPECT_NO_FATAL_FAILURE(DSRef = DPCTLDefaultSelector_Create());
EXPECT_NO_FATAL_FAILURE(DRef = DPCTLDevice_CreateFromSelector(DSRef));
EXPECT_NO_FATAL_FAILURE(QRef = DPCTLQueue_CreateForDevice(
DRef, nullptr, DPCTL_DEFAULT_PROPERTY));
EXPECT_NO_FATAL_FAILURE(DPCTLDevice_Delete(DRef));
EXPECT_NO_FATAL_FAILURE(DPCTLDeviceSelector_Delete(DSRef));
}
~TestQueueSubmitBarrier()
{
EXPECT_NO_FATAL_FAILURE(DPCTLQueue_Delete(QRef));
}
};
TEST_F(TestQueueSubmitBarrier, ChkSubmitBarrier)
{
DPCTLSyclEventRef ERef = nullptr;
ASSERT_TRUE(QRef != nullptr);
EXPECT_NO_FATAL_FAILURE(ERef = DPCTLQueue_SubmitBarrier(QRef));
ASSERT_TRUE(ERef != nullptr);
EXPECT_NO_FATAL_FAILURE(DPCTLEvent_Wait(ERef));
EXPECT_NO_FATAL_FAILURE(DPCTLEvent_Delete(ERef));
}
TEST_F(TestQueueSubmitBarrier, ChkSubmitBarrierWithEvents)
{
DPCTLSyclEventRef ERef = nullptr;
DPCTLSyclEventRef DepsERefs[2] = {nullptr, nullptr};
EXPECT_NO_FATAL_FAILURE(DepsERefs[0] = DPCTLEvent_Create());
EXPECT_NO_FATAL_FAILURE(DepsERefs[1] = DPCTLEvent_Create());
ASSERT_TRUE(QRef != nullptr);
EXPECT_NO_FATAL_FAILURE(
ERef = DPCTLQueue_SubmitBarrierForEvents(QRef, DepsERefs, 2));
ASSERT_TRUE(ERef != nullptr);
EXPECT_NO_FATAL_FAILURE(DPCTLEvent_Wait(ERef));
EXPECT_NO_FATAL_FAILURE(DPCTLEvent_Delete(ERef));
EXPECT_NO_FATAL_FAILURE(DPCTLEvent_Delete(DepsERefs[0]));
EXPECT_NO_FATAL_FAILURE(DPCTLEvent_Delete(DepsERefs[1]));
}