This repository was archived by the owner on Aug 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 89
Expand file tree
/
Copy pathsync-handler.cpp
More file actions
92 lines (80 loc) · 3.07 KB
/
Copy pathsync-handler.cpp
File metadata and controls
92 lines (80 loc) · 3.07 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
/***************************************************************************
*
* Copyright (C) 2018 Codeplay Software Limited
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* For your convenience, a copy of the License has been included in this
* repository.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Codeplay's ComputeCpp SDK
*
* sync-handler.cpp
*
* Description:
* Sample code that demonstrates the use of a synchronous error handler to
* demonstrate error handling.
*
**************************************************************************/
#include <CL/sycl.hpp>
#include <algorithm>
#include <iostream>
using namespace cl::sycl;
namespace {
class Worker;
/* A custom device selector */
class PickySelector : public device_selector {
int operator()(const device&) const final {
/* Here logic would exist to find a specific device to do our computation
* with. As this is just an example, it's hardcoded to find nothing. In a
* real program, it would inspect the device information and return priority
* numbers depending on that device's suitablity. */
return -1;
}
};
} // namespace
int main() {
/* Our output array */
constexpr size_t number_of_sevens = 7;
std::array<size_t, number_of_sevens> sevens;
try {
/* Use our custom device selector to find a specific device. In this example
* we require a very specific piece of hardware. If that isn't present, the
* code should just run on the host. */
PickySelector selector;
/* Attempt to create a queue for this using this selector. If the selector
* fails to find a device, this will throw an exception. */
queue myQueue(selector);
/* If the selector couldn't find a suitable device, the following code
* will not run. */
buffer<size_t, 1> buf(sevens.data(), range<1>(number_of_sevens));
myQueue.submit([&](handler& cgh) {
auto ptr = buf.get_access<access::mode::discard_write>(cgh);
cgh.parallel_for<Worker>(range<1>(number_of_sevens),
[=](item<1> item) { ptr[item] = 7; });
});
} catch (const exception& e) {
/* Report the exception to the user. */
std::cerr << "SYCL exception caught: " << e.what() << "\n";
std::cerr << "Running on host...\n";
/* Can't find any suitable devices, so just run the code on the host
* system normally. */
sevens.fill(7);
}
/* Check the array has been populated with sevens correctly. */
if (std::any_of(sevens.begin(), sevens.end(),
[](const auto x) { return x != 7; })) {
std::cerr << "A seven was not set!\n";
return 1;
}
return 0;
}