-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathvklTutorialGPU.cpp
More file actions
410 lines (312 loc) · 13 KB
/
vklTutorialGPU.cpp
File metadata and controls
410 lines (312 loc) · 13 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
// Copyright 2022 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
#include <openvkl/openvkl.h>
#include <openvkl/device/openvkl.h>
#include <iomanip>
#include <iostream>
#if defined(_MSC_VER)
#include <windows.h> // Sleep
#endif
// setup specialization constant for feature flags
static_assert(std::is_trivially_copyable<VKLFeatureFlags>::value);
constexpr static sycl::specialization_id<VKLFeatureFlags> samplerSpecId{
VKL_FEATURE_FLAGS_DEFAULT};
void demoGpuAPI(sycl::queue &syclQueue, VKLDevice device, VKLVolume volume)
{
std::cout << "demo of GPU API" << std::endl;
std::cout << std::fixed << std::setprecision(6);
VKLSampler sampler = vklNewSampler(volume);
vklCommit(sampler);
// feature flags improve performance on GPU, as well as JIT times
const VKLFeatureFlags requiredFeatures = vklGetFeatureFlags(sampler);
// bounding box
vkl_box3f bbox = vklGetBoundingBox(volume);
std::cout << "\tbounding box" << std::endl;
std::cout << "\t\tlower = " << bbox.lower.x << " " << bbox.lower.y << " "
<< bbox.lower.z << std::endl;
std::cout << "\t\tupper = " << bbox.upper.x << " " << bbox.upper.y << " "
<< bbox.upper.z << std::endl
<< std::endl;
// number of attributes
unsigned int numAttributes = vklGetNumAttributes(volume);
std::cout << "\tnum attributes = " << numAttributes << std::endl;
// value range for all attributes
for (unsigned int i = 0; i < numAttributes; i++) {
vkl_range1f valueRange = vklGetValueRange(volume, i);
std::cout << "\tvalue range (attribute " << i << ") = (" << valueRange.lower
<< " " << valueRange.upper << ")" << std::endl;
}
std::cout << std::endl << "\tsampling" << std::endl;
// coordinate for sampling / gradients
vkl_vec3f coord = {1.f, 2.f, 3.f};
std::cout << "\n\tcoord = " << coord.x << " " << coord.y << " " << coord.z
<< std::endl
<< std::endl;
// sample, gradient (first attribute)
const unsigned int attributeIndex = 0;
const float time = 0.f;
// USM shared allocations, required when we want to pass results back from GPU
float *sample = sycl::malloc_shared<float>(1, syclQueue);
vkl_vec3f *grad = sycl::malloc_shared<vkl_vec3f>(1, syclQueue);
syclQueue
.submit([=](sycl::handler &cgh) {
cgh.set_specialization_constant<samplerSpecId>(requiredFeatures);
cgh.single_task([=](sycl::kernel_handler kh) {
const VKLFeatureFlags featureFlags =
kh.get_specialization_constant<samplerSpecId>();
*sample = vklComputeSample(
&sampler, &coord, attributeIndex, time, featureFlags);
*grad = vklComputeGradient(
&sampler, &coord, attributeIndex, time, featureFlags);
});
})
.wait();
std::cout << "\tsampling and gradient computation (first attribute)"
<< std::endl;
std::cout << "\t\tsample = " << *sample << std::endl;
std::cout << "\t\tgrad = " << grad->x << " " << grad->y << " " << grad->z
<< std::endl
<< std::endl;
sycl::free(sample, syclQueue);
sycl::free(grad, syclQueue);
// sample (multiple attributes)
const unsigned int M = 3;
const unsigned int attributeIndices[] = {0, 1, 2};
float *samples = sycl::malloc_shared<float>(M, syclQueue);
syclQueue
.submit([=](sycl::handler &cgh) {
cgh.set_specialization_constant<samplerSpecId>(requiredFeatures);
cgh.single_task([=](sycl::kernel_handler kh) {
const VKLFeatureFlags featureFlags =
kh.get_specialization_constant<samplerSpecId>();
vklComputeSampleM(&sampler,
&coord,
samples,
M,
attributeIndices,
time,
featureFlags);
});
})
.wait();
std::cout << "\tsampling (multiple attributes)" << std::endl;
std::cout << "\t\tsamples = " << samples[0] << " " << samples[1] << " "
<< samples[2] << std::endl;
sycl::free(samples, syclQueue);
// interval iterator context setup
std::cout << std::endl << "\tinterval iteration" << std::endl << std::endl;
std::vector<vkl_range1f> ranges{{10, 20}, {50, 75}};
VKLData rangesData =
vklNewData(device, ranges.size(), VKL_BOX1F, ranges.data());
VKLIntervalIteratorContext intervalContext =
vklNewIntervalIteratorContext(sampler);
vklSetInt(intervalContext, "attributeIndex", 0);
vklSetData(intervalContext, "valueRanges", rangesData);
vklRelease(rangesData);
vklCommit(intervalContext);
// ray definition for iterators
vkl_vec3f rayOrigin{0.f, 1.f, 1.f};
vkl_vec3f rayDirection{1.f, 0.f, 0.f};
vkl_range1f rayTRange{0.f, 200.f};
std::cout << "\trayOrigin = " << rayOrigin.x << " " << rayOrigin.y << " "
<< rayOrigin.z << std::endl;
std::cout << "\trayDirection = " << rayDirection.x << " " << rayDirection.y
<< " " << rayDirection.z << std::endl;
std::cout << "\trayTRange = " << rayTRange.lower << " " << rayTRange.upper
<< std::endl
<< std::endl;
// interval iteration
char *iteratorBuffer = sycl::malloc_device<char>(
vklGetIntervalIteratorSize(&intervalContext), syclQueue);
int *numIntervals = sycl::malloc_shared<int>(1, syclQueue);
*numIntervals = 0;
const size_t maxNumIntervals = 999;
VKLInterval *intervalsBuffer =
sycl::malloc_shared<VKLInterval>(maxNumIntervals, syclQueue);
memset(intervalsBuffer, 0, maxNumIntervals * sizeof(VKLInterval));
std::cout << "\tinterval iterator for value ranges";
for (const auto &r : ranges) {
std::cout << " {" << r.lower << " " << r.upper << "}";
}
std::cout << std::endl << std::endl;
syclQueue
.submit([=](sycl::handler &cgh) {
cgh.set_specialization_constant<samplerSpecId>(requiredFeatures);
cgh.single_task([=](sycl::kernel_handler kh) {
const VKLFeatureFlags featureFlags =
kh.get_specialization_constant<samplerSpecId>();
VKLIntervalIterator intervalIterator =
vklInitIntervalIterator(&intervalContext,
&rayOrigin,
&rayDirection,
&rayTRange,
time,
(void *)iteratorBuffer,
featureFlags);
for (;;) {
VKLInterval interval;
int result =
vklIterateInterval(intervalIterator, &interval, featureFlags);
if (!result) {
break;
}
intervalsBuffer[*numIntervals] = interval;
*numIntervals = *numIntervals + 1;
if (*numIntervals >= maxNumIntervals)
break;
}
});
})
.wait();
for (int i = 0; i < *numIntervals; ++i) {
std::cout << "\t\ttRange (" << intervalsBuffer[i].tRange.lower << " "
<< intervalsBuffer[i].tRange.upper << ")" << std::endl;
std::cout << "\t\tvalueRange (" << intervalsBuffer[i].valueRange.lower
<< " " << intervalsBuffer[i].valueRange.upper << ")" << std::endl;
std::cout << "\t\tnominalDeltaT " << intervalsBuffer[i].nominalDeltaT
<< std::endl
<< std::endl;
}
sycl::free(iteratorBuffer, syclQueue);
sycl::free(numIntervals, syclQueue);
sycl::free(intervalsBuffer, syclQueue);
vklRelease(intervalContext);
// hit iteration
std::cout << std::endl << "\thit iteration" << std::endl << std::endl;
// hit iterator context setup
float values[2] = {32.f, 96.f};
int num_values = 2;
VKLData valuesData = vklNewData(device, num_values, VKL_FLOAT, values);
VKLHitIteratorContext hitContext = vklNewHitIteratorContext(sampler);
vklSetInt(hitContext, "attributeIndex", 0);
vklSetData(hitContext, "values", valuesData);
vklRelease(valuesData);
vklCommit(hitContext);
// ray definition for iterators
// see rayOrigin, Direction and TRange above
char *hitIteratorBuffer =
sycl::malloc_device<char>(vklGetHitIteratorSize(&hitContext), syclQueue);
int *numHits = sycl::malloc_shared<int>(1, syclQueue);
*numHits = 0;
const size_t maxNumHits = 999;
VKLHit *hitBuffer = sycl::malloc_shared<VKLHit>(maxNumHits, syclQueue);
memset(hitBuffer, 0, maxNumHits * sizeof(VKLHit));
std::cout << "\thit iterator for values";
for (const auto &r : values) {
std::cout << " " << r << " ";
}
std::cout << std::endl << std::endl;
syclQueue
.submit([=](sycl::handler &cgh) {
cgh.set_specialization_constant<samplerSpecId>(requiredFeatures);
cgh.single_task([=](sycl::kernel_handler kh) {
const VKLFeatureFlags featureFlags =
kh.get_specialization_constant<samplerSpecId>();
VKLHitIterator hitIterator =
vklInitHitIterator(&hitContext,
&rayOrigin,
&rayDirection,
&rayTRange,
time,
(void *)hitIteratorBuffer,
featureFlags);
for (;;) {
VKLHit hit;
int result = vklIterateHit(hitIterator, &hit, featureFlags);
if (!result) {
break;
}
hitBuffer[*numHits] = hit;
*numHits = *numHits + 1;
if (*numHits >= maxNumHits)
break;
}
});
})
.wait();
for (int i = 0; i < *numHits; ++i) {
std::cout << "\t\tt " << hitBuffer[i].t << std::endl;
std::cout << "\t\tsample " << hitBuffer[i].sample << std::endl;
std::cout << "\t\tepsilon " << hitBuffer[i].epsilon << std::endl
<< std::endl;
}
sycl::free(hitIteratorBuffer, syclQueue);
sycl::free(numHits, syclQueue);
sycl::free(hitBuffer, syclQueue);
vklRelease(hitContext);
vklRelease(sampler);
}
int main()
{
auto IntelGPUDeviceSelector = [](const sycl::device &device) {
using namespace sycl::info;
const std::string deviceName = device.get_info<device::name>();
bool match = device.is_gpu() &&
device.get_info<sycl::info::device::vendor_id>() == 0x8086 &&
device.get_backend() == sycl::backend::ext_oneapi_level_zero;
return match ? 1 : -1;
};
sycl::queue syclQueue(IntelGPUDeviceSelector);
sycl::context syclContext = syclQueue.get_context();
sycl::device syclDevice = syclQueue.get_device();
std::cout << "Target SYCL device: "
<< syclQueue.get_device().get_info<sycl::info::device::name>()
<< std::endl
<< std::endl;
vklInit();
VKLDevice device = vklNewDevice("gpu");
vklDeviceSetVoidPtr(device, "syclContext", static_cast<void *>(&syclContext));
// "syclDevice" is optional.
// By default first available SYCL device will be used.
vklDeviceSetVoidPtr(device, "syclDevice", static_cast<void *>(&syclDevice));
vklCommitDevice(device);
const int dimensions[] = {128, 128, 128};
const int numVoxels = dimensions[0] * dimensions[1] * dimensions[2];
const int numAttributes = 3;
VKLVolume volume = vklNewVolume(device, "structuredRegular");
vklSetVec3i(
volume, "dimensions", dimensions[0], dimensions[1], dimensions[2]);
vklSetVec3f(volume, "gridOrigin", 0, 0, 0);
vklSetVec3f(volume, "gridSpacing", 1, 1, 1);
std::vector<float> voxels(numVoxels);
// volume attribute 0: x-grad
for (int k = 0; k < dimensions[2]; k++)
for (int j = 0; j < dimensions[1]; j++)
for (int i = 0; i < dimensions[0]; i++)
voxels[k * dimensions[0] * dimensions[1] + j * dimensions[2] + i] =
(float)i;
VKLData data0 = vklNewData(device, numVoxels, VKL_FLOAT, voxels.data());
// volume attribute 1: y-grad
for (int k = 0; k < dimensions[2]; k++)
for (int j = 0; j < dimensions[1]; j++)
for (int i = 0; i < dimensions[0]; i++)
voxels[k * dimensions[0] * dimensions[1] + j * dimensions[2] + i] =
(float)j;
VKLData data1 = vklNewData(device, numVoxels, VKL_FLOAT, voxels.data());
// volume attribute 2: z-grad
for (int k = 0; k < dimensions[2]; k++)
for (int j = 0; j < dimensions[1]; j++)
for (int i = 0; i < dimensions[0]; i++)
voxels[k * dimensions[0] * dimensions[1] + j * dimensions[2] + i] =
(float)k;
VKLData data2 = vklNewData(device, numVoxels, VKL_FLOAT, voxels.data());
VKLData attributes[] = {data0, data1, data2};
VKLData attributesData =
vklNewData(device, numAttributes, VKL_DATA, attributes);
vklRelease(data0);
vklRelease(data1);
vklRelease(data2);
vklSetData(volume, "data", attributesData);
vklRelease(attributesData);
vklCommit(volume);
demoGpuAPI(syclQueue, device, volume);
vklRelease(volume);
vklReleaseDevice(device);
std::cout << "complete." << std::endl;
#if defined(_MSC_VER)
// On Windows, sleep for a few seconds so the terminal window doesn't close
// immediately.
Sleep(3000);
#endif
return 0;
}