|
| 1 | +/*************************************************************************** |
| 2 | + * |
| 3 | + * Copyright (C) 2018 Codeplay Software Limited |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * For your convenience, a copy of the License has been included in this |
| 11 | + * repository. |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, software |
| 14 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | + * See the License for the specific language governing permissions and |
| 17 | + * limitations under the License. |
| 18 | + * |
| 19 | + * Codeplay's ComputeCpp SDK |
| 20 | + * |
| 21 | + * sync-handler.cpp |
| 22 | + * |
| 23 | + * Description: |
| 24 | + * Sample code that demonstrates the use of a synchronous error handler to |
| 25 | + * demonstrate error handling. |
| 26 | + * |
| 27 | + **************************************************************************/ |
| 28 | + |
| 29 | +#include <CL/sycl.hpp> |
| 30 | + |
| 31 | +#include <iostream> |
| 32 | + |
| 33 | +using namespace cl::sycl; |
| 34 | + |
| 35 | +namespace { |
| 36 | +class Worker; |
| 37 | + |
| 38 | +/* A custom device selector */ |
| 39 | +class PickySelector : public device_selector { |
| 40 | + virtual int operator()(const device&) const final { |
| 41 | + /* Here logic would exist to find a specific device to do our computation |
| 42 | + * with. As this is just an example, it's hardcoded to find nothing. In a |
| 43 | + * real program, it would inspect the device information and return priority |
| 44 | + * numbers depending on that device's suitablity. */ |
| 45 | + return -1; |
| 46 | + } |
| 47 | +}; |
| 48 | +} // namespace |
| 49 | + |
| 50 | +int main() { |
| 51 | + /* Our output array */ |
| 52 | + constexpr size_t number_of_sevens = 7; |
| 53 | + std::array<size_t, number_of_sevens> sevens; |
| 54 | + |
| 55 | + try { |
| 56 | + /* Use our custom device selector to find a specific device. In this example |
| 57 | + * we require a very specific piece of hardware. If that isn't present, the |
| 58 | + * code should just run on the host. */ |
| 59 | + PickySelector selector; |
| 60 | + |
| 61 | + /* Attempt to create a queue for this using this selector. If the selector |
| 62 | + * fails to find a device, this will throw an exception. */ |
| 63 | + queue myQueue(selector); |
| 64 | + |
| 65 | + /* If the selector couldn't find a suitable device, the following code |
| 66 | + * will not run. */ |
| 67 | + buffer<size_t, 1> buf(sevens.data(), range<1>(number_of_sevens)); |
| 68 | + myQueue.submit([&](handler& cgh) { |
| 69 | + auto ptr = buf.get_access<access::mode::discard_write>(cgh); |
| 70 | + cgh.parallel_for<Worker>(range<1>(number_of_sevens), |
| 71 | + [=](item<1> item) { ptr[item] = 7; }); |
| 72 | + }); |
| 73 | + } catch (exception const& e) { |
| 74 | + /* Report the exception to the user. */ |
| 75 | + std::cout << "SYCL exception caught: " << e.what() << "\n"; |
| 76 | + std::cout << "Running on host...\n"; |
| 77 | + |
| 78 | + /* Can't find any suitable devices, so just run the code on the host |
| 79 | + * system normally. */ |
| 80 | + for (auto& item : sevens) { |
| 81 | + item = 7; |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + /* Check the array has been populated with sevens correctly. */ |
| 86 | + for (auto& item : sevens) { |
| 87 | + if (item != 7) { |
| 88 | + std::cerr << "A seven was not set!\n"; |
| 89 | + return 1; |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + return 0; |
| 94 | +} |
0 commit comments