-
Notifications
You must be signed in to change notification settings - Fork 5
ConstantIterator #62
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
AntonReinhard
wants to merge
19
commits into
alpaka-group:master
Choose a base branch
from
AntonReinhard:constant-iterator
base: master
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
ConstantIterator #62
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
b8483e5
Add first draft ConstantIterator
dda5d7d
Remove previous example code
90866ef
Fix up the constant Iterator and spaceship usage
a3a8569
Add nodiscards
d9e48c9
Add some testing
7849e48
Add missing doxy comment
0f39d2a
Add template parameters in test for older compilers
1501f8f
Fix inclusion problem with compare
4a0b2f5
Review Fixes
05bded9
Apply suggestions from code review
9d7f2ea
More Review Fixes and Rebase
550e48b
Fix clangcxx20 build, add constexpr qualifiers
ea43993
Add a simple constant iterator usage example and a constant iterator …
victorjunaidy 688c1c5
WIP integration test
8553603
fixed constant iterator integration test
SimeonEhrig 17a9bff
Add constantiter benchmark
dd9aa2e
make ci build benchmark
2ccb01c
Add ALPAKA_FN_HOST_ACC to functions
b6bcef7
Cleanup, namespaces
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 |
|---|---|---|
|
|
@@ -26,4 +26,4 @@ | |
| /build* | ||
| /install* | ||
|
|
||
| .vscode | ||
| .vscode/ | ||
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 |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| cmake_minimum_required(VERSION 3.18) | ||
| add_subdirectory("constantIterator/") | ||
| add_subdirectory("reduce/") | ||
| add_subdirectory("transform/") |
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,4 @@ | ||
| cmake_minimum_required(VERSION 3.18) | ||
| set(_TARGET_NAME "example_constant_iterator") | ||
| alpaka_add_executable(${_TARGET_NAME} src/constantIterator-main.cpp) | ||
| target_link_libraries(${_TARGET_NAME} PUBLIC vikunja::internalvikunja) |
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,81 @@ | ||
| /* Copyright 2021 Hauke Mewes, Simeon Ehrig, Victor | ||
| * | ||
| * This file is part of vikunja. | ||
| * | ||
| * This Source Code Form is subject to the terms of the Mozilla Public | ||
| * License, v. 2.0. If a copy of the MPL was not distributed with this | ||
| * file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
| */ | ||
|
|
||
| #include <vikunja/mem/iterator/ConstantIterator.hpp> | ||
| #include <vikunja/reduce/reduce.hpp> | ||
|
|
||
| #include <alpaka/alpaka.hpp> | ||
|
|
||
| #include <iostream> | ||
|
|
||
|
|
||
| int main() | ||
| { | ||
| // Define the accelerator here. Must be one of the enabled accelerators. | ||
| #ifdef ALPAKA_ACC_GPU_CUDA_ENABLED | ||
| using TAcc = alpaka::AccGpuCudaRt<alpaka::DimInt<3u>, std::uint64_t>; | ||
| #else | ||
| using TAcc = alpaka::AccCpuSerial<alpaka::DimInt<3u>, std::uint64_t>; | ||
| #endif | ||
| // Type of the data that will be reduced | ||
| using TRed = uint64_t; | ||
|
|
||
| // Alpaka index type | ||
| using Idx = alpaka::Idx<TAcc>; | ||
| // Alpaka dimension type | ||
| using Dim = alpaka::Dim<TAcc>; | ||
| // number of elements to reduce | ||
| const Idx n = static_cast<Idx>(6400); | ||
|
|
||
| // define device, platform, and queue types. | ||
| using DevAcc = alpaka::Dev<TAcc>; | ||
| using PltfAcc = alpaka::Pltf<DevAcc>; | ||
| // using QueueAcc = alpaka::test::queue::DefaultQueue<alpaka::Dev<TAcc>>; | ||
| using PltfHost = alpaka::PltfCpu; | ||
| using DevHost = alpaka::Dev<PltfHost>; | ||
| using QueueAcc = alpaka::Queue<TAcc, alpaka::Blocking>; | ||
|
|
||
| // Get the host device. | ||
| DevHost devHost(alpaka::getDevByIdx<PltfHost>(0u)); | ||
| // Select a device to execute on. | ||
| DevAcc devAcc(alpaka::getDevByIdx<PltfAcc>(0u)); | ||
| // Get a queue on the accelerator device. | ||
| QueueAcc queueAcc(devAcc); | ||
|
|
||
| // Use Lambda function for reduction | ||
| auto sum = [] ALPAKA_FN_HOST_ACC(TRed const i, TRed const j) { return i + j; }; | ||
| auto doubleNum = [] ALPAKA_FN_HOST_ACC(TRed const i) { return 2 * i; }; | ||
| std::cout << "Testing accelerator: " << alpaka::getAccName<TAcc>() << " with size: " << n << "\n" | ||
| << "Testing constant iterator with value: 10\n"; | ||
|
|
||
| // Create the constant iterator | ||
| vikunja::iterator::ConstantIterator<TRed> constantIter(10); | ||
|
|
||
| // REDUCE CALL: | ||
| // Takes the arguments: accelerator device, host device, accelerator queue, size of data, pointer-like to memory, | ||
| // reduce lambda. | ||
| Idx reduceResult = vikunja::reduce::deviceReduce<TAcc>(devAcc, devHost, queueAcc, n, constantIter, sum); | ||
|
|
||
| // check reduce result | ||
| auto expectedResult = n * 10; | ||
| std::cout << "Expected reduce result: " << expectedResult << ", real result: " << reduceResult << "\n"; | ||
|
|
||
| // TRANSFORM_REDUCE CALL: | ||
| // Takes the arguments: accelerator device, host device, accelerator queue, size of data, pointer-like to memory, | ||
| // transform lambda, reduce lambda. | ||
| Idx transformReduceResult | ||
| = vikunja::reduce::deviceTransformReduce<TAcc>(devAcc, devHost, queueAcc, n, constantIter, doubleNum, sum); | ||
|
|
||
| // check transform result | ||
| auto expectedTransformReduce = expectedResult * 2; | ||
| std::cout << "Expected transform_reduce result: " << expectedTransformReduce | ||
| << ", real result: " << transformReduceResult << "\n"; | ||
|
|
||
| return 0; | ||
| } |
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,231 @@ | ||
| /* Copyright 2021 Anton Reinhard | ||
| * | ||
| * This file is part of vikunja. | ||
| * | ||
| * This Source Code Form is subject to the terms of the Mozilla Public | ||
| * License, v. 2.0. If a copy of the MPL was not distributed with this | ||
| * file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
| */ | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <alpaka/alpaka.hpp> | ||
|
|
||
| #include <iterator> | ||
|
|
||
| #if __has_include(<compare>) | ||
| # include <compare> | ||
| # if defined(__cpp_impl_three_way_comparison) && defined(__cpp_lib_three_way_comparison) | ||
| # define USESPACESHIP | ||
| # endif | ||
| #endif | ||
|
|
||
| #if __has_cpp_attribute(nodiscard) | ||
| # define NODISCARD [[nodiscard]] | ||
| #else | ||
| # define NODISCARD | ||
| #endif | ||
|
|
||
| namespace vikunja::iterator | ||
| { | ||
| /** | ||
| * @brief A constant iterator, returning a value given initially at any index. As such it has no bounds, | ||
| * other than the bounds of the index type used. | ||
| * @tparam DataType The type of the data | ||
| * @tparam IdxType The type of the index | ||
| */ | ||
| template<typename DataType, typename IdxType = int64_t> | ||
| class ConstantIterator | ||
| { | ||
| public: | ||
| // Need all 5 of these types for iterator_traits | ||
| using difference_type = IdxType; | ||
| using value_type = DataType; | ||
| using pointer = DataType*; | ||
| using reference = DataType&; | ||
| using iterator_category = std::random_access_iterator_tag; | ||
|
|
||
| /** | ||
| * @brief Constructor for the ConstantIterator | ||
| * @param value The value to initialize the iterator with | ||
| * @param idx The index for the iterator, default 0 | ||
| */ | ||
| ALPAKA_FN_HOST_ACC constexpr ConstantIterator(const DataType& value, const IdxType& idx = {}) | ||
| : v(value) | ||
| , index(idx) | ||
| { | ||
| } | ||
|
|
||
| /** | ||
| * @brief Dereference operator to receive the stored value | ||
| */ | ||
| ALPAKA_FN_HOST_ACC NODISCARD constexpr ALPAKA_FN_INLINE const DataType& operator*() const | ||
| { | ||
| return v; | ||
| } | ||
|
|
||
| /** | ||
| * @brief Index operator to get stored value at some given offset from this iterator | ||
| */ | ||
| ALPAKA_FN_HOST_ACC NODISCARD constexpr ALPAKA_FN_INLINE const DataType& operator[](int) const | ||
| { | ||
| return v; | ||
| } | ||
|
|
||
| #pragma region arithmeticoperators | ||
| /** | ||
| * @brief Postfix increment operator | ||
| * @note Use prefix increment operator instead if possible to avoid copies | ||
| */ | ||
| ALPAKA_FN_HOST_ACC constexpr ALPAKA_FN_INLINE ConstantIterator operator++() | ||
| { | ||
| ConstantIterator cpy = *this; | ||
| ++index; | ||
| return cpy; | ||
| } | ||
|
|
||
| /** | ||
| * @brief Prefix increment operator | ||
| */ | ||
| ALPAKA_FN_HOST_ACC constexpr ALPAKA_FN_INLINE ConstantIterator& operator++(int) | ||
| { | ||
| ++index; | ||
| return *this; | ||
| } | ||
|
|
||
| /** | ||
| * @brief Postfix decrement operator | ||
| * @note Use prefix decrement operator instead if possible to avoid copies | ||
| */ | ||
| ALPAKA_FN_HOST_ACC constexpr ALPAKA_FN_INLINE ConstantIterator operator--() | ||
| { | ||
| ConstantIterator cpy = *this; | ||
| --index; | ||
| return cpy; | ||
| } | ||
|
|
||
| /** | ||
| * @brief Prefix decrement operator | ||
| */ | ||
| ALPAKA_FN_HOST_ACC constexpr ALPAKA_FN_INLINE ConstantIterator& operator--(int) | ||
| { | ||
| --index; | ||
| return *this; | ||
| } | ||
|
|
||
| /** | ||
| * @brief Add an index to this iterator | ||
| */ | ||
| ALPAKA_FN_HOST_ACC NODISCARD constexpr friend ALPAKA_FN_INLINE ConstantIterator | ||
| operator+(ConstantIterator it, IdxType idx) | ||
| { | ||
| return it += idx; | ||
| } | ||
|
|
||
| /** | ||
| * @brief Subtract an index from this iterator | ||
| */ | ||
| ALPAKA_FN_HOST_ACC NODISCARD constexpr friend ALPAKA_FN_INLINE ConstantIterator | ||
| operator-(ConstantIterator it, const IdxType idx) | ||
| { | ||
| return it -= idx; | ||
| } | ||
|
|
||
| /** | ||
| * @brief Subtract a second constant iterator of the same value from this one | ||
| */ | ||
| ALPAKA_FN_HOST_ACC NODISCARD constexpr friend ALPAKA_FN_INLINE IdxType | ||
| operator-(ConstantIterator it, const ConstantIterator& other) | ||
| { | ||
| assert(it.v == other.v && "Can't subtract constant iterators of different values!"); | ||
| return it.index - other.index; | ||
| } | ||
|
|
||
| /** | ||
| * @brief Add an index to this iterator | ||
| */ | ||
| ALPAKA_FN_HOST_ACC constexpr friend ALPAKA_FN_INLINE ConstantIterator& operator+=( | ||
| ConstantIterator& it, | ||
| const IdxType idx) | ||
| { | ||
| it.index += idx; | ||
| return it; | ||
| } | ||
|
|
||
| /** | ||
| * @brief Subtract an index from this iterator | ||
| */ | ||
| ALPAKA_FN_HOST_ACC constexpr friend ALPAKA_FN_INLINE ConstantIterator& operator-=( | ||
| ConstantIterator& it, | ||
| const IdxType idx) | ||
| { | ||
| it.index -= idx; | ||
| return it; | ||
| } | ||
|
|
||
| #pragma endregion arithmeticoperators | ||
|
|
||
| #pragma region comparisonoperators | ||
|
|
||
| #ifdef USESPACESHIP | ||
|
|
||
| ALPAKA_FN_HOST_ACC NODISCARD constexpr ALPAKA_FN_INLINE auto operator<=>( | ||
| const ConstantIterator& other) const noexcept = default; | ||
|
|
||
| #else | ||
|
|
||
| ALPAKA_FN_HOST_ACC NODISCARD constexpr friend ALPAKA_FN_INLINE bool operator==( | ||
| const ConstantIterator& it, | ||
| const ConstantIterator& other) noexcept | ||
| { | ||
| return it.v == other.v && it.index == other.index; | ||
| } | ||
|
|
||
| ALPAKA_FN_HOST_ACC NODISCARD constexpr friend ALPAKA_FN_INLINE bool operator!=( | ||
| const ConstantIterator& it, | ||
| const ConstantIterator& other) noexcept | ||
| { | ||
| return !operator==(it, other); | ||
| } | ||
|
|
||
| ALPAKA_FN_HOST_ACC NODISCARD constexpr friend ALPAKA_FN_INLINE bool operator<( | ||
| const ConstantIterator& it, | ||
| const ConstantIterator& other) noexcept | ||
| { | ||
| if(it.v < other.v) | ||
| return true; | ||
| if(it.v > other.v) | ||
| return false; | ||
| return it.index < other.index; | ||
| } | ||
|
|
||
| ALPAKA_FN_HOST_ACC NODISCARD constexpr friend ALPAKA_FN_INLINE bool operator>( | ||
| const ConstantIterator& it, | ||
| const ConstantIterator& other) noexcept | ||
| { | ||
| return operator<(other, it); | ||
| } | ||
|
|
||
| ALPAKA_FN_HOST_ACC NODISCARD constexpr friend ALPAKA_FN_INLINE bool operator<=( | ||
| const ConstantIterator& it, | ||
| const ConstantIterator& other) noexcept | ||
| { | ||
| return operator<(it, other) || operator==(it, other); | ||
| } | ||
|
|
||
| ALPAKA_FN_HOST_ACC NODISCARD constexpr friend ALPAKA_FN_INLINE bool operator>=( | ||
| const ConstantIterator& it, | ||
| const ConstantIterator& other) noexcept | ||
| { | ||
| return operator<=(other, it); | ||
| } | ||
| #endif | ||
|
|
||
| #pragma endregion comparisonoperators | ||
|
|
||
| private: | ||
| DataType v; | ||
| IdxType index; | ||
| }; | ||
|
|
||
| } // namespace vikunja::iterator | ||
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.