forked from codeplaysoftware/syclacademy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution.cpp
86 lines (70 loc) · 2.54 KB
/
solution.cpp
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
/*
SYCL Academy (c)
SYCL Academy is licensed under a Creative Commons
Attribution-ShareAlike 4.0 International License.
You should have received a copy of the license along with this
work. If not, see <http://creativecommons.org/licenses/by-sa/4.0/>.
*/
#define CATCH_CONFIG_MAIN
#include <catch2/catch.hpp>
#if __has_include(<SYCL/sycl.hpp>)
#include <SYCL/sycl.hpp>
#else
#include <CL/sycl.hpp>
#endif
class vector_add;
class usm_selector : public sycl::device_selector {
public:
int operator()(const sycl::device& dev) const {
if (dev.has(sycl::aspect::usm_device_allocations)) {
if (dev.has(sycl::aspect::gpu)) return 2;
return 1;
}
return -1;
}
};
TEST_CASE("usm_vector_add", "usm_vector_add_solution") {
constexpr size_t dataSize = 1024;
float a[dataSize], b[dataSize], r[dataSize];
for (int i = 0; i < dataSize; ++i) {
a[i] = static_cast<float>(i);
b[i] = static_cast<float>(i);
r[i] = 0.0f;
}
try {
auto usmQueue = sycl::queue{usm_selector{}};
#ifdef SYCL_ACADEMY_USE_COMPUTECPP
auto devicePtrA = sycl::experimental::usm_wrapper<float>{
sycl::malloc_device<float>(dataSize, usmQueue)};
auto devicePtrB = sycl::experimental::usm_wrapper<float>{
sycl::malloc_device<float>(dataSize, usmQueue)};
auto devicePtrR = sycl::experimental::usm_wrapper<float>{
sycl::malloc_device<float>(dataSize, usmQueue)};
#else
auto devicePtrA = sycl::malloc_device<float>(dataSize, usmQueue);
auto devicePtrB = sycl::malloc_device<float>(dataSize, usmQueue);
auto devicePtrR = sycl::malloc_device<float>(dataSize, usmQueue);
#endif
usmQueue.memcpy(devicePtrA, a, sizeof(float) * dataSize).wait();
usmQueue.memcpy(devicePtrB, b, sizeof(float) * dataSize).wait();
usmQueue
.parallel_for<vector_add>(sycl::range{dataSize},
[=](sycl::id<1> idx) {
auto globalId = idx[0];
devicePtrR[globalId] =
devicePtrA[globalId] +
devicePtrB[globalId];
})
.wait();
usmQueue.memcpy(r, devicePtrR, sizeof(float) * dataSize).wait();
sycl::free(devicePtrA, usmQueue);
sycl::free(devicePtrB, usmQueue);
sycl::free(devicePtrR, usmQueue);
usmQueue.throw_asynchronous();
} catch (const sycl::exception& e) {
std::cout << "Exception caught: " << e.what() << std::endl;
}
for (int i = 0; i < dataSize; ++i) {
REQUIRE(r[i] == i * 2);
}
}