forked from codeplaysoftware/syclacademy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
source.cpp
88 lines (75 loc) · 2.43 KB
/
source.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
/*
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/>.
*
* SYCL Quick Reference
* ~~~~~~~~~~~~~~~~~~~~
*
* // Make a child class of sycl::device_selector
* class my_functor_selector : public sycl::device_selector {
* // Overload operator() for sycl::device.
* int operator()(const sycl::device& dev) const override {
* ...
* }
* }
* ...
* auto q = sycl::queue{my_functor_selector{}};
*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*
* // Or use a function selector
* int my_function_selector(const sycl::device &d) {
* ...
* }
* ...
* auto q = sycl::queue{my_function_selector};
*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*
* // Or use a lambda selector
* auto my_lambda_selector = [](const sycl::device &d) {
* ...
* };
* ...
* auto q = sycl::queue{my_lambda_selector};
*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*
* // Query a device for some things:
* std::string vendor = dev.get_info<sycl::info::device::vendor>();
* std::string dev_name = dev.get_info<sycl::info::device::name>();
* std::string dev_driver_ver = dev.get_info<sycl::info::device::driver_version>();
*
*
*/
#define CATCH_CONFIG_MAIN
#include <catch2/catch.hpp>
#include <sycl/sycl.hpp>
class scalar_add;
TEST_CASE("intel_gpu_device_selector", "device_selectors_solution") {
int a = 18, b = 24, r = 0;
try {
// Task: add a device selector to create this queue with an Intel GPU
auto defaultQueue = sycl::queue{};
{
auto bufA = sycl::buffer{&a, sycl::range{1}};
auto bufB = sycl::buffer{&b, sycl::range{1}};
auto bufR = sycl::buffer{&r, sycl::range{1}};
defaultQueue
.submit([&](sycl::handler& cgh) {
auto accA = sycl::accessor{bufA, cgh, sycl::read_only};
auto accB = sycl::accessor{bufB, cgh, sycl::read_only};
auto accR = sycl::accessor{bufR, cgh, sycl::write_only};
cgh.single_task<scalar_add>([=]() { accR[0] = accA[0] + accB[0]; });
})
.wait();
}
defaultQueue.throw_asynchronous();
} catch (const sycl::exception& e) {
std::cout << "Exception caught: " << e.what() << std::endl;
}
REQUIRE(r == 42);
}