-
Notifications
You must be signed in to change notification settings - Fork 109
Add Sycl Scan Algorithm #1776
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
long58
wants to merge
15
commits into
develop
Choose a base branch
from
feature/long58/sycl_scan_sort
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Add Sycl Scan Algorithm #1776
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
932a090
Rough implementation of scan algorithm.
long58 d855faa
Merge branch 'develop' into feature/long58/sycl_scan_sort
long58 691a9df
Merge branch 'develop' of github.com:LLNL/RAJA into feature/long58/sy…
long58 44c9c7e
Merge branch 'feature/long58/sycl_scan_sort' of github.com:LLNL/RAJA …
long58 878f6e3
Merge branch 'develop' into feature/long58/sycl_scan_sort
rhornung67 e4801c9
Merge branch 'feature/long58/sycl_scan_sort' of github.com:LLNL/RAJA …
long58 5fe4a54
Got inclusive scan working
long58 3cb82e6
All scan types working!
long58 ce4bc40
Merge branch 'develop' of github.com:LLNL/RAJA into feature/long58/sy…
long58 998559e
Changes based on PR feedback
long58 50d16a6
Changed std::copy to sycl memcpy.
long58 c1ec6f4
use iterator traits instead of decltype
long58 9a43cb5
Changed waits to be conditional on policy async
long58 7af2015
Merge branch 'develop' into feature/long58/sycl_scan_sort
long58 f914505
Merge branch 'develop' into feature/long58/sycl_scan_sort
rhornung67 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
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,242 @@ | ||
| /*! | ||
| ****************************************************************************** | ||
| * | ||
| * \file | ||
| * | ||
| * \brief Header file providing RAJA scan declarations. | ||
| * | ||
| ****************************************************************************** | ||
| */ | ||
|
|
||
| //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~// | ||
| // Copyright (c) 2016-24, Lawrence Livermore National Security, LLC | ||
| // and RAJA project contributors. See the RAJA/LICENSE file for details. | ||
| // | ||
| // SPDX-License-Identifier: (BSD-3-Clause) | ||
| //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~// | ||
|
|
||
| #ifndef RAJA_scan_sycl_HPP | ||
| #define RAJA_scan_sycl_HPP | ||
|
|
||
| #include <cstddef> | ||
| #include <iterator> | ||
| #include "RAJA/config.hpp" | ||
| #include "camp/resource/sycl.hpp" | ||
|
|
||
| #if defined(RAJA_ENABLE_SYCL) | ||
|
|
||
| #include "RAJA/policy/sycl/policy.hpp" | ||
|
|
||
| namespace RAJA | ||
| { | ||
| namespace impl | ||
| { | ||
| namespace scan | ||
| { | ||
|
|
||
| template <size_t BLOCK_SIZE, bool Async, typename InputIter, typename Function> | ||
| RAJA_INLINE | ||
| camp::resources::EventProxy<camp::resources::Sycl> | ||
| inclusive_inplace( | ||
| camp::resources::Sycl sycl_res, | ||
| ::RAJA::policy::sycl::sycl_exec<BLOCK_SIZE, Async>, | ||
| InputIter begin, | ||
| InputIter end, | ||
| Function binary_op) | ||
| { | ||
| ::sycl::queue* sycl_queue = sycl_res.get_queue(); | ||
|
|
||
| // using valueT = typename std::remove_reference<decltype(*begin)>::type; | ||
| using valueT = typename std::iterator_traits<InputIter>::value_type; | ||
|
|
||
| // Calculate the size of the input range | ||
| size_t size = std::distance(begin, end); | ||
|
|
||
| ::sycl::buffer<valueT, 1> buffer(begin, end); | ||
| ::sycl::buffer<valueT, 1> tempAccBuff(begin, ::sycl::range<1>(size)); | ||
|
|
||
| int iterations = 0; | ||
| for (size_t ii = size >> 1; ii > 0; ii >>= 1) { | ||
| iterations++; | ||
| } | ||
| if ((size & (size - 1)) != 0) { | ||
| iterations++; | ||
| } | ||
|
|
||
| auto buffPtr = &buffer; | ||
| auto tempPtr = &tempAccBuff; | ||
|
|
||
| if (iterations % 2 == 0) { | ||
| tempPtr = &buffer; | ||
| buffPtr = &tempAccBuff; | ||
| } | ||
|
|
||
| int ii = 1; | ||
| do { | ||
| // Submit the kernel to the SYCL queue | ||
| sycl_queue->submit([&](::sycl::handler& cgh) { | ||
| auto buffAccessor = buffPtr->get_access(cgh); | ||
| auto tempAccessor = tempPtr->get_access(cgh); | ||
| // outBuffAccessor = outBuff->get_access<::sycl::access::mode::read(cgh); | ||
|
|
||
| cgh.parallel_for(::sycl::range<1>(size), [=](::sycl::item<1> idx) { | ||
| size_t td = 1 << (ii - 1); | ||
| size_t thisID = idx.get_id(0); | ||
| if (thisID < size and thisID >= td) { | ||
| tempAccessor[thisID] = binary_op(buffAccessor[thisID - td], buffAccessor[thisID]); | ||
| } else { | ||
| tempAccessor[thisID] = buffAccessor[thisID]; | ||
| } | ||
| }); | ||
| }); | ||
|
|
||
| std::swap(buffPtr, tempPtr); | ||
| ii++; | ||
| } while ( ii <= iterations); | ||
|
|
||
| if (!Async) { sycl_res.wait(); } | ||
| return camp::resources::EventProxy<camp::resources::Sycl>(sycl_res); | ||
| } | ||
|
|
||
| template <size_t BLOCK_SIZE, | ||
| bool Async, | ||
| typename InputIter, | ||
| typename Function, | ||
| typename TT> | ||
| RAJA_INLINE | ||
| resources::EventProxy<resources::Sycl> | ||
| exclusive_inplace( | ||
| resources::Sycl sycl_res, | ||
| ::RAJA::policy::sycl::sycl_exec<BLOCK_SIZE, Async> exec, | ||
| InputIter begin, | ||
| InputIter end, | ||
| Function binary_op, | ||
| TT initVal) | ||
| { | ||
| ::sycl::queue* sycl_queue = sycl_res.get_queue(); | ||
|
|
||
| // using valueT = typename std::remove_reference<decltype(*begin)>::type; | ||
| using valueT = typename std::iterator_traits<InputIter>::value_type; | ||
|
|
||
|
|
||
| // Calculate the size of the input range | ||
| size_t size = std::distance(begin, end); | ||
|
|
||
| ::sycl::buffer<valueT, 1> buffer(begin, end); | ||
| ::sycl::buffer<valueT, 1> tempAccBuff(begin, ::sycl::range<1>(size)); | ||
|
|
||
| int iterations = 0; | ||
| for (size_t ii = size >> 1; ii > 0; ii >>= 1) { | ||
| iterations++; | ||
| } | ||
| if ((size & (size - 1)) != 0) { | ||
| iterations++; | ||
| } | ||
|
|
||
| auto buffPtr = &buffer; | ||
| auto tempPtr = &tempAccBuff; | ||
|
|
||
| if (iterations % 2 != 0) { | ||
| tempPtr = &buffer; | ||
| buffPtr = &tempAccBuff; | ||
| } | ||
|
|
||
| // Submit the kernel to the SYCL queue | ||
| sycl_queue->submit([&](::sycl::handler& cgh) { | ||
| auto inAccessor = buffPtr->get_access(cgh); | ||
| auto outAccessor = tempPtr->get_access(cgh); | ||
| // outBuffAccessor = outBuff->get_access<::sycl::access::mode::read(cgh); | ||
|
|
||
| cgh.parallel_for(::sycl::range<1>(size), [=](::sycl::item<1> idx) { | ||
| size_t thisID = idx.get_id(0); | ||
| // size_t td = 1 << (ii - 1); | ||
| if (thisID > 0) { | ||
| outAccessor[thisID] = inAccessor[thisID - 1]; | ||
| } else { | ||
| outAccessor[thisID] = initVal; | ||
| } | ||
| }); | ||
| }); | ||
|
|
||
| std::swap(buffPtr, tempPtr); | ||
|
|
||
| int ii = 1; | ||
| do { | ||
| // Submit the kernel to the SYCL queue | ||
| sycl_queue->submit([&](::sycl::handler& cgh) { | ||
| auto buffAccessor = buffPtr->get_access(cgh); | ||
| auto tempAccessor = tempPtr->get_access(cgh); | ||
| // outBuffAccessor = outBuff->get_access<::sycl::access::mode::read(cgh); | ||
|
|
||
| cgh.parallel_for(::sycl::range<1>(size), [=](::sycl::item<1> idx) { | ||
| size_t td = 1 << (ii - 1); | ||
| size_t thisID = idx.get_id(0); | ||
| if (thisID < size and thisID >= td) { | ||
| tempAccessor[thisID] = binary_op(buffAccessor[thisID - td], buffAccessor[thisID]); | ||
| } else { | ||
| tempAccessor[thisID] = buffAccessor[thisID]; | ||
| } | ||
| }); | ||
| }); | ||
|
|
||
| std::swap(buffPtr, tempPtr); | ||
| ii++; | ||
| } while ( ii <= iterations); | ||
|
|
||
| if(!Async) { sycl_res.wait(); } | ||
| return camp::resources::EventProxy<camp::resources::Sycl>(sycl_res); | ||
| return inclusive_inplace(sycl_res, exec, begin, end, binary_op); | ||
| } | ||
|
|
||
| template <size_t BLOCK_SIZE, | ||
| bool Async, | ||
| typename InputIter, | ||
| typename OutputIter, | ||
| typename Function> | ||
| RAJA_INLINE | ||
| resources::EventProxy<resources::Sycl> | ||
| inclusive( | ||
| resources::Sycl sycl_res, | ||
| ::RAJA::policy::sycl::sycl_exec<BLOCK_SIZE, Async> exec, | ||
| InputIter begin, | ||
| InputIter end, | ||
| OutputIter out, | ||
| Function binary_op) | ||
| { | ||
| // using valueT = typename std::remove_reference<decltype(*out)>::type; | ||
| using valueT = typename std::iterator_traits<OutputIter>::value_type; | ||
| sycl_res.memcpy(out, begin, std::distance(begin, end) * sizeof(valueT)); | ||
| return inclusive_inplace(sycl_res, exec, out, out + std::distance(begin, end), binary_op); | ||
| } | ||
|
|
||
| template <size_t BLOCK_SIZE, | ||
| bool Async, | ||
| typename InputIter, | ||
| typename OutputIter, | ||
| typename Function, | ||
| typename TT> | ||
| RAJA_INLINE | ||
| resources::EventProxy<resources::Sycl> | ||
| exclusive( | ||
| resources::Sycl sycl_res, | ||
| ::RAJA::policy::sycl::sycl_exec<BLOCK_SIZE, Async> exec, | ||
| InputIter begin, | ||
| InputIter end, | ||
| OutputIter out, | ||
| Function binary_op, | ||
| TT initVal) | ||
| { | ||
| // using valueT = typename std::remove_reference<decltype(*out)>::type; | ||
| using valueT = typename std::iterator_traits<OutputIter>::value_type; | ||
|
|
||
| sycl_res.memcpy(out, begin, std::distance(begin, end) * sizeof(valueT)); | ||
| return exclusive_inplace(sycl_res, exec, out, out + std::distance(begin, end), binary_op, initVal); | ||
| } | ||
|
|
||
| } // namespace scan | ||
| } // namespace impl | ||
| } // namespace RAJA | ||
|
|
||
| #endif // closing endif for RAJA enable Sycl guard | ||
|
|
||
| #endif // closing endif for header include guard | ||
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
Oops, something went wrong.
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.
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.
I'm not familiar with how sycl::buffer objects work, do they make a copy of the data?
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.
If the data is already on the device, then no, otherwise yes, it will copy from the host to the device and synchronize them automatically when changes are made to one or the other.