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
Added use-onchip-memory sample #165
Merged
Merged
Changes from 1 commit
Commits
Show all changes
4 commits Select commit Hold shift + click to select a range
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,21 @@ | ||
| set(SOURCE_NAME "use-onchip-memory") | ||
| | ||
| add_executable( | ||
| ${SOURCE_NAME} | ||
| ${CMAKE_CURRENT_SOURCE_DIR}/${SOURCE_NAME}.cpp | ||
| ) | ||
| include_directories( | ||
| ${PROJECT_SOURCE_DIR}/include | ||
| ) | ||
| 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,142 @@ | ||
| /*************************************************************************** | ||
| * | ||
| * Copyright 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 | ||
| * | ||
| * example_vptr.cpp | ||
cjdb marked this conversation as resolved. Outdated Show resolved Hide resolved cjdb marked this conversation as resolved. Outdated Show resolved Hide resolved | ||
| * | ||
| * Description: | ||
| * Sample code that demonstrates the use of the use_onchip_memory extension | ||
| * to SYCL provided by ComputeCpp. | ||
| * | ||
| **************************************************************************/ | ||
| | ||
| #include <CL/sycl.hpp> | ||
| #include <SYCL/codeplay.hpp> | ||
| #include <iostream> | ||
| | ||
| namespace sycl = cl::sycl; | ||
| namespace access = sycl::access; | ||
| | ||
| // Sample kernel | ||
| // ------------- | ||
| // Since kernels are no longer allowed to be declared inline, I dislike | ||
| // declaring a random name in the global namespace that can potentially be | ||
| // repurposed. | ||
cjdb marked this conversation as resolved. Outdated Show resolved Hide resolved | ||
| // | ||
| // This kernel performs the same operation as std::iota, but also scales | ||
| // the result by two. | ||
| // | ||
| class scaled_iota { | ||
| public: | ||
| using accessor = sycl::accessor<int, 1, access::mode::discard_write, | ||
| access::target::global_buffer>; | ||
| | ||
| explicit scaled_iota(accessor deviceAccess) noexcept | ||
| : m_deviceAccess{std::move(deviceAccess)} {} | ||
cjdb marked this conversation as resolved. Outdated Show resolved Hide resolved | ||
| | ||
| void operator()(sycl::nd_item<2> itemId) const noexcept { | ||
| const auto linearId = itemId.get_global_linear_id(); | ||
| m_deviceAccess[linearId] = linearId * 2; | ||
| } | ||
| | ||
| private: | ||
| accessor m_deviceAccess; | ||
| }; | ||
cjdb marked this conversation as resolved. Outdated Show resolved Hide resolved | ||
| | ||
| namespace codeplay = sycl::codeplay; | ||
| | ||
| template <class Policy> | ||
| void use_with_policy(Policy policy, sycl::queue& queue) { | ||
| auto hostData = sycl::vector_class<int>(1024); | ||
| { | ||
| auto taskContext = queue.get_context(); | ||
DuncanMcBain marked this conversation as resolved. Show resolved Hide resolved | ||
| | ||
| // Notice that the Codeplay property takes a policy argument: this is used | ||
cjdb marked this conversation as resolved. Outdated Show resolved Hide resolved | ||
| // to indicate whether the property is advantageous or genuinely necessary. | ||
| // | ||
| auto deviceData = sycl::buffer<int, 1>{ | ||
| hostData.data(), // | ||
| sycl::range<1>(hostData.size()), // | ||
| sycl::property_list{ | ||
| sycl::property::buffer::context_bound(taskContext), | ||
| codeplay::property::buffer::use_onchip_memory(policy) // <-------- | ||
| } // ^--------------------- | ||
cjdb marked this conversation as resolved. Outdated Show resolved Hide resolved | ||
| }; | ||
| | ||
| deviceData.set_final_data(hostData.data()); | ||
cjdb marked this conversation as resolved. Outdated Show resolved Hide resolved | ||
| | ||
| queue.submit([&](sycl::handler & cgh) { | ||
| constexpr auto dimension_size = 2; | ||
| auto r = sycl::nd_range<dimension_size>{ | ||
| sycl::range<dimension_size>{ | ||
| hostData.size() / dimension_size, // | ||
| dimension_size // | ||
| }, | ||
| sycl::range<dimension_size>{dimension_size, 1} // | ||
cjdb marked this conversation as resolved. Outdated Show resolved Hide resolved | ||
| }; | ||
| cgh.parallel_for( | ||
| r, | ||
| scaled_iota(deviceData.get_access<access::mode::discard_write>(cgh))); | ||
| }); | ||
| | ||
| queue.wait_and_throw(); | ||
| } | ||
| } | ||
| | ||
| // Codeplay policy extensions have two different enabling mechanisms: the first | ||
cjdb marked this conversation as resolved. Outdated Show resolved Hide resolved | ||
| // is to indicate that a policy is preferred. Using this policy means that if | ||
| // the system supports the feature, then the feature will be enabled. If the | ||
| // feature is not present on the system, then it will not be enabled. | ||
| // | ||
| // Puns aside, this is the preferred default. | ||
| // | ||
| void how_to_use_with_prefer() { | ||
| auto queue = sycl::queue{}; | ||
| ::use_with_policy(codeplay::property::prefer, queue); | ||
| } | ||
| | ||
| // Alternatively, if you can guarantee that your system will support this | ||
DuncanMcBain marked this conversation as resolved. Show resolved Hide resolved | ||
| // policy, or if it is expected any system using your software must support the | ||
| // policy, then you can use the require tag to indicate that the feature is | ||
| // required by your software. | ||
| // | ||
| // In the event that the property isn't supported, an exception will be thrown. | ||
cjdb marked this conversation as resolved. Outdated Show resolved Hide resolved | ||
| // | ||
| void how_to_use_with_require() { | ||
| try { | ||
| auto queue = sycl::queue{}; | ||
cjdb marked this conversation as resolved. Outdated Show resolved Hide resolved | ||
| ::use_with_policy(codeplay::property::require, queue); | ||
| } catch (const sycl::exception& e) { | ||
| std::cerr << "An error occurred: " << e.what() | ||
| << "\n" | ||
| "\n" | ||
| "This particular error has occurred because you are requiring " | ||
| "the policy use_onchip_memory be available, and your hardware " | ||
| "doesn't support the use_onchip_memory, so the SYCL ecosystem " | ||
| ||
| "will raise an error.\n"; | ||
| } | ||
| } | ||
| | ||
| int main() { | ||
| // Using the on-chip memory policy with the require tag. | ||
| how_to_use_with_require(); | ||
| | ||
| // Using the on-chip memory policy with the prefer tag. | ||
| how_to_use_with_prefer(); | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.