forked from codeplaysoftware/syclacademy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution.cpp
163 lines (129 loc) · 4.94 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
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
/*
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>
#include <sycl/sycl.hpp>
class kernel_a_1;
class kernel_b_1;
class kernel_c_1;
class kernel_d_1;
class kernel_a_2;
class kernel_b_2;
class kernel_c_2;
class kernel_d_2;
int usm_selector(const sycl::device& dev) {
if (dev.has(sycl::aspect::usm_device_allocations)) {
if (dev.has(sycl::aspect::gpu)) return 2;
return 1;
}
return -1;
}
TEST_CASE("buffer_accessor_diamond", "managing_dependencies_solution") {
constexpr size_t dataSize = 1024;
int inA[dataSize], inB[dataSize], inC[dataSize], out[dataSize];
for (int i = 0; i < dataSize; ++i) {
inA[i] = static_cast<float>(i);
inB[i] = static_cast<float>(i);
inC[i] = static_cast<float>(i);
out[i] = 0.0f;
}
try {
auto defaultQueue = sycl::queue{};
auto bufInA = sycl::buffer{inA, sycl::range{dataSize}};
auto bufInB = sycl::buffer{inB, sycl::range{dataSize}};
auto bufInC = sycl::buffer{inC, sycl::range{dataSize}};
auto bufOut = sycl::buffer{out, sycl::range{dataSize}};
defaultQueue.submit([&](sycl::handler& cgh) {
sycl::accessor acc{bufInA, cgh, sycl::read_write};
cgh.parallel_for<kernel_a_1>(sycl::range{dataSize}, [=](sycl::id<1> idx) {
acc[idx] = acc[idx] * 2.0f;
});
});
defaultQueue.submit([&](sycl::handler& cgh) {
sycl::accessor accIn{bufInA, cgh, sycl::read_only};
sycl::accessor accOut{bufInB, cgh, sycl::read_write};
cgh.parallel_for<kernel_b_1>(sycl::range{dataSize}, [=](sycl::id<1> idx) {
accOut[idx] += accIn[idx];
});
});
defaultQueue.submit([&](sycl::handler& cgh) {
sycl::accessor accIn{bufInA, cgh, sycl::read_only};
sycl::accessor accOut{bufInC, cgh, sycl::read_write};
cgh.parallel_for<kernel_c_1>(sycl::range{dataSize}, [=](sycl::id<1> idx) {
accOut[idx] -= accIn[idx];
});
});
defaultQueue.submit([&](sycl::handler& cgh) {
sycl::accessor accInA{bufInB, cgh, sycl::read_only};
sycl::accessor accInB{bufInC, cgh, sycl::read_only};
sycl::accessor accOut{bufOut, cgh, sycl::write_only};
cgh.parallel_for<kernel_d_1>(sycl::range{dataSize}, [=](sycl::id<1> idx) {
accOut[idx] = accInA[idx] + accInB[idx];
});
});
defaultQueue.wait_and_throw();
} catch (const sycl::exception& e) {
std::cout << "Exception caught: " << e.what() << std::endl;
}
for (int i = 0; i < dataSize; ++i) {
REQUIRE(out[i] == i * 2.0f);
}
}
TEST_CASE("usm_diamond", "usm_vector_add_solution") {
constexpr size_t dataSize = 1024;
int inA[dataSize], inB[dataSize], inC[dataSize], out[dataSize];
for (int i = 0; i < dataSize; ++i) {
inA[i] = static_cast<float>(i);
inB[i] = static_cast<float>(i);
inC[i] = static_cast<float>(i);
out[i] = 0.0f;
}
try {
auto usmQueue = sycl::queue{usm_selector};
auto devicePtrInA = sycl::malloc_device<float>(dataSize, usmQueue);
auto devicePtrInB = sycl::malloc_device<float>(dataSize, usmQueue);
auto devicePtrInC = sycl::malloc_device<float>(dataSize, usmQueue);
auto devicePtrOut = sycl::malloc_device<float>(dataSize, usmQueue);
auto e1 = usmQueue.memcpy(devicePtrInA, inA, sizeof(float) * dataSize);
auto e2 = usmQueue.memcpy(devicePtrInB, inB, sizeof(float) * dataSize);
auto e3 = usmQueue.memcpy(devicePtrInC, inC, sizeof(float) * dataSize);
auto e4 = usmQueue.parallel_for<kernel_a_2>(
sycl::range{dataSize}, e1, [=](sycl::id<1> idx) {
auto globalId = idx[0];
devicePtrInA[globalId] = devicePtrInA[globalId] * 2.0f;
});
auto e5 = usmQueue.parallel_for<kernel_b_2>(
sycl::range{dataSize}, {e2, e4}, [=](sycl::id<1> idx) {
auto globalId = idx[0];
devicePtrInB[globalId] += devicePtrInA[globalId];
});
auto e6 = usmQueue.parallel_for<kernel_c_2>(
sycl::range{dataSize}, {e3, e4}, [=](sycl::id<1> idx) {
auto globalId = idx[0];
devicePtrInC[globalId] -= devicePtrInA[globalId];
});
auto e7 = usmQueue.parallel_for<kernel_d_2>(
sycl::range{dataSize}, {e5, e6}, [=](sycl::id<1> idx) {
auto globalId = idx[0];
devicePtrOut[globalId] =
devicePtrInB[globalId] + devicePtrInC[globalId];
});
auto e8 = usmQueue.memcpy(out, devicePtrOut, sizeof(float) * dataSize, e7);
e8.wait();
sycl::free(devicePtrInA, usmQueue);
sycl::free(devicePtrInB, usmQueue);
sycl::free(devicePtrInC, usmQueue);
sycl::free(devicePtrOut, 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(out[i] == i * 2.0f);
}
}