Skip to content

Commit 600a81a

Browse files
GordonGordon
authored andcommitted
Add async handler to verify_sycl example.
1 parent 204db56 commit 600a81a

File tree

1 file changed

+42
-35
lines changed

1 file changed

+42
-35
lines changed

examples/verify_sycl.cpp

Lines changed: 42 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -36,46 +36,53 @@ class hello_world;
3636
* (as determined by the SYCL implementation) whose only function is to
3737
* output the canonical "hello world" string. */
3838
int main() {
39-
/* Selectors determine which device kernels will be dispatched to.
40-
* Try using a host_selector, too! */
41-
cl::sycl::default_selector selector;
4239

43-
/* Queues are used to enqueue work.
44-
* In this case we construct the queue using the selector. Users can create
45-
* their own selectors to choose whatever kind of device they might need. */
46-
cl::sycl::queue myQueue(selector);
47-
std::cout << "Running on "
48-
<< myQueue.get_device().get_info<cl::sycl::info::device::name>()
49-
<< "\n";
40+
try {
5041

51-
/* C++ 11 lambda functions can be used to submit work to the queue.
52-
* They set up data transfers, kernel compilation and the actual
53-
* kernel execution. This submission has no data, only a "stream" object.
54-
* Useful in debugging, it is a lot like an std::ostream. The handler
55-
* object is used to control the scope certain operations can be done. */
56-
myQueue.submit([&](cl::sycl::handler& cgh) {
57-
/* The stream object allows output to be generated from the kernel. It
58-
* takes three parameters in its constructor. The first is the maximum
59-
* output size in bytes, the second is how large [in bytes] the total
60-
* output of any single << chain can be, and the third is the cgh,
61-
* ensuring it can only be constructed inside a submit() call. */
62-
cl::sycl::stream os(1024, 80, cgh);
42+
/* Selectors determine which device kernels will be dispatched to.
43+
* Try using a host_selector, too! */
44+
cl::sycl::default_selector selector;
6345

64-
/* single_task is the simplest way of executing a kernel on a
65-
* SYCL device. A single thread executes the code inside the kernel
66-
* lambda. The template parameter needs to be a unique name that
67-
* the runtime can use to identify the kernel (since lambdas have
68-
* no accessible name). */
69-
cgh.single_task<hello_world>([=]() {
70-
/* We use the stream operator on the stream object we created above
71-
* to print to stdout from the device. */
72-
os << "Hello, World!\n";
46+
/* Queues are used to enqueue work.
47+
* In this case we construct the queue using the selector. Users can create
48+
* their own selectors to choose whatever kind of device they might need. */
49+
cl::sycl::queue myQueue(selector);
50+
std::cout << "Running on "
51+
<< myQueue.get_device().get_info<cl::sycl::info::device::name>()
52+
<< "\n";
53+
54+
/* C++ 11 lambda functions can be used to submit work to the queue.
55+
* They set up data transfers, kernel compilation and the actual
56+
* kernel execution. This submission has no data, only a "stream" object.
57+
* Useful in debugging, it is a lot like an std::ostream. The handler
58+
* object is used to control the scope certain operations can be done. */
59+
myQueue.submit([&](cl::sycl::handler& cgh) {
60+
/* The stream object allows output to be generated from the kernel. It
61+
* takes three parameters in its constructor. The first is the maximum
62+
* output size in bytes, the second is how large [in bytes] the total
63+
* output of any single << chain can be, and the third is the cgh,
64+
* ensuring it can only be constructed inside a submit() call. */
65+
cl::sycl::stream os(1024, 80, cgh);
66+
67+
/* single_task is the simplest way of executing a kernel on a
68+
* SYCL device. A single thread executes the code inside the kernel
69+
* lambda. The template parameter needs to be a unique name that
70+
* the runtime can use to identify the kernel (since lambdas have
71+
* no accessible name). */
72+
cgh.single_task<hello_world>([=]() {
73+
/* We use the stream operator on the stream object we created above
74+
* to print to stdout from the device. */
75+
os << "Hello, World!\n";
76+
});
7377
});
74-
});
7578

76-
/* the queue does not wait on destruction so as there are no datavdependencies
77-
* to wait for this task to complete you must explicitly wait on the queue. */
78-
myQueue.wait();
79+
/* the queue does not wait on destruction so as there are no datavdependencies
80+
* to wait for this task to complete you must explicitly wait on the queue. */
81+
myQueue.wait();
82+
83+
} catch (cl::sycl::exception e) {
84+
std::cout << "SYCL exception caught: " << e.what() << std::endl;
85+
}
7986

8087
return 0;
8188
}

0 commit comments

Comments
 (0)