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
Example of synchronous error handling #163
Merged
DuncanMcBain
merged 1 commit into
codeplaysoftware:master
from
RossBrunton:ross/sync-err
Dec 11, 2018
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| set(SOURCE_NAME "sync-handler") | ||
| add_executable( | ||
| ${SOURCE_NAME} | ||
| ${CMAKE_CURRENT_SOURCE_DIR}/${SOURCE_NAME}.cpp | ||
| ) | ||
| add_sycl_to_target( | ||
| TARGET ${SOURCE_NAME} | ||
| SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/${SOURCE_NAME}.cpp | ||
| ) | ||
| add_test( | ||
| NAME ${SOURCE_NAME} | ||
| COMMAND ${SOURCE_NAME} | ||
| ) | ||
| install( | ||
| TARGETS ${SOURCE_NAME} | ||
| RUNTIME DESTINATION bin | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| /*************************************************************************** | ||
| * | ||
| * 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 <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; | ||
| } | ||
DuncanMcBain marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| return 0; | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a declaration. IIRC we need this to be a definition (cc @Ruyk).