Repository navigation
[feature] add find* implement #17 #18
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
ThomasRetornaz
wants to merge
5
commits into
xtensor-stack:main
Choose a base branch
from
ThomasRetornaz:tre/feat/find_if
base: main
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
Changes from all commits
Commits
Show all changes
5 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
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,63 @@ | ||
| #include <algorithm> | ||
| #include <numeric> | ||
| #include <vector> | ||
|
|
||
| #include <benchmark/benchmark.h> | ||
|
|
||
| #include "xsimd_algorithm/stl/find.hpp" | ||
|
|
||
| #define BENCH_ARGS ->Arg(kSmall)->Arg(kMedium)->Arg(kLarge) | ||
|
|
||
| namespace | ||
| { | ||
| template <typename T> | ||
| std::vector<T> make_input(size_t size, size_t position, T constant) | ||
| { | ||
| std::vector<T> result(size); | ||
| std::iota(result.begin(), result.end(), T(1)); | ||
| if (position < size) | ||
| { | ||
| result[position] = constant; | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| template <typename T> | ||
| void BM_std_find(benchmark::State& state) | ||
| { | ||
| const auto constant = T { -42 }; | ||
| auto src = make_input<T>(static_cast<std::size_t>(state.range(0)), static_cast<std::size_t>(state.range(0)) - 1, constant); | ||
| for (auto _ : state) | ||
| { | ||
| auto result = std::find(src.begin(), src.end(), constant); | ||
| benchmark::DoNotOptimize(result); | ||
| benchmark::ClobberMemory(); | ||
| } | ||
| state.SetItemsProcessed(state.iterations() * state.range(0)); | ||
| } | ||
|
|
||
| template <typename T> | ||
| void BM_xsimd_find(benchmark::State& state) | ||
| { | ||
| const auto constant = T { -42 }; | ||
| auto src = make_input<T>(static_cast<std::size_t>(state.range(0)), static_cast<std::size_t>(state.range(0)) - 1, constant); | ||
| for (auto _ : state) | ||
| { | ||
| auto result = xsimd::find(src.begin(), src.end(), constant); | ||
| benchmark::DoNotOptimize(result); | ||
| benchmark::ClobberMemory(); | ||
| } | ||
| state.SetItemsProcessed(state.iterations() * state.range(0)); | ||
| } | ||
|
|
||
| static constexpr long long kSmall = 1LL << 12; | ||
| static constexpr long long kMedium = 1LL << 18; | ||
| static constexpr long long kLarge = 1LL << 22; | ||
|
|
||
| // find | ||
| BENCHMARK_TEMPLATE(BM_std_find, float) | ||
| BENCH_ARGS; | ||
| BENCHMARK_TEMPLATE(BM_xsimd_find, float) | ||
| BENCH_ARGS; | ||
|
|
||
| } |
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,29 @@ | ||
| /*************************************************************************** | ||
| * Copyright (c) Johan Mabille, Sylvain Corlay, Wolf Vollprecht and * | ||
| * Martin Renou * | ||
| * Copyright (c) QuantStack * | ||
| * Copyright (c) Serge Guelton * | ||
| * * | ||
| * Distributed under the terms of the BSD 3-Clause License. * | ||
| * * | ||
| * The full license is in the file LICENSE, distributed with this software. * | ||
| ****************************************************************************/ | ||
|
|
||
| #ifndef XSIMD_ALGORITHMS_FIND_HPP | ||
| #define XSIMD_ALGORITHMS_FIND_HPP | ||
|
|
||
| #include "xsimd/xsimd.hpp" | ||
| #include "xsimd_algorithm/stl/find_if.hpp" | ||
|
|
||
| #include <iostream> | ||
| #include <iterator> | ||
|
|
||
| namespace xsimd | ||
| { | ||
| template <class Arch = default_arch, class ContiguousIterator, class T = typename std::iterator_traits<ContiguousIterator>::value_type> | ||
| ContiguousIterator find(ContiguousIterator first, ContiguousIterator last, const T& value) noexcept | ||
| { | ||
| return find_if<Arch>(first, last, [value](const auto& x) { return x == value; }); | ||
| } | ||
| } | ||
| #endif // XSIMD_ALGORITHMS_FIND_HPP | ||
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,90 @@ | ||
| /*************************************************************************** | ||
| * Copyright (c) Johan Mabille, Sylvain Corlay, Wolf Vollprecht and * | ||
| * Martin Renou * | ||
| * Copyright (c) QuantStack * | ||
| * Copyright (c) Serge Guelton * | ||
| * * | ||
| * Distributed under the terms of the BSD 3-Clause License. * | ||
| * * | ||
| * The full license is in the file LICENSE, distributed with this software. * | ||
| ****************************************************************************/ | ||
|
|
||
| #ifndef XSIMD_ALGORITHMS_FIND_IF_HPP | ||
| #define XSIMD_ALGORITHMS_FIND_IF_HPP | ||
|
|
||
| #include "xsimd/xsimd.hpp" | ||
|
|
||
| #include <iostream> | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. idem |
||
| #include <iterator> | ||
|
|
||
| namespace xsimd | ||
| { | ||
| namespace detail | ||
| { | ||
| template <class Arch = default_arch, class ForwardIterator, class Predicate> | ||
| ForwardIterator sequential_find_if(ForwardIterator first, ForwardIterator last, Predicate&& p) noexcept | ||
| { | ||
| for (; first != last; ++first) | ||
| { | ||
| if (p(*first)) | ||
| { | ||
| return first; | ||
| } | ||
| } | ||
| return last; | ||
| } | ||
| } | ||
|
|
||
| template <class Arch = default_arch, class ContiguousIterator, class Predicate> | ||
| ContiguousIterator find_if(ContiguousIterator first, ContiguousIterator last, Predicate&& p) noexcept | ||
| { | ||
| using value_type = typename std::decay<decltype(*first)>::type; | ||
| using batch_type = batch<value_type, Arch>; | ||
|
|
||
| const std::size_t size = static_cast<std::size_t>(std::distance(first, last)); | ||
| constexpr std::size_t simd_size = batch_type::size; | ||
|
|
||
| // quick exit for small sizes | ||
| if (size < simd_size) | ||
| { | ||
| return detail::sequential_find_if<Arch>(first, last, std::forward<Predicate>(p)); | ||
| } | ||
|
|
||
| // prologue: process elements until the first aligned one | ||
| // compute alignment offsets | ||
| const auto* const ptr_begin = &(*first); | ||
| const std::size_t align_begin = xsimd::get_alignment_offset(ptr_begin, size, simd_size); | ||
| const std::size_t align_end = align_begin + ((size - align_begin) & ~(simd_size - 1)); | ||
|
|
||
| const auto align_begin_it = std::next(first, align_begin); | ||
| const auto align_end_it = std::next(first, align_end); | ||
|
|
||
| const auto prologue_it_result = detail::sequential_find_if<Arch>(first, align_begin_it, std::forward<Predicate>(p)); | ||
|
|
||
| if (prologue_it_result!= align_begin_it) | ||
| { | ||
| return prologue_it_result; | ||
| } | ||
|
|
||
| // main loop: process aligned elements | ||
| for (auto current = align_begin_it; current != align_end_it; std::advance(current, simd_size)) | ||
| { | ||
| batch_type batch_data = batch_type::load_aligned(&(*current)); | ||
| auto offset = xsimd::countr_zero(p(batch_data)); | ||
| if (offset != simd_size) | ||
| { | ||
| return std::next(current, offset); | ||
| } | ||
| } | ||
|
|
||
| // epilogue: process remaining elements | ||
| const auto epilogue_it_result = detail::sequential_find_if<Arch>(align_end_it, last, std::forward<Predicate>(p)); | ||
| if (epilogue_it_result != last) | ||
| { | ||
| return epilogue_it_result; | ||
| } | ||
|
|
||
| return last; | ||
| } | ||
| } | ||
| #endif // XSIMD_ALGORITHMS_FIND_IF_HPP | ||
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,30 @@ | ||
| /*************************************************************************** | ||
| * Copyright (c) Johan Mabille, Sylvain Corlay, Wolf Vollprecht and * | ||
| * Martin Renou * | ||
| * Copyright (c) QuantStack * | ||
| * Copyright (c) Serge Guelton * | ||
| * * | ||
| * Distributed under the terms of the BSD 3-Clause License. * | ||
| * * | ||
| * The full license is in the file LICENSE, distributed with this software. * | ||
| ****************************************************************************/ | ||
|
|
||
| #ifndef XSIMD_ALGORITHMS_FIND_IF_NOT_HPP | ||
| #define XSIMD_ALGORITHMS_FIND_IF_NOT_HPP | ||
|
|
||
| #include "xsimd/xsimd.hpp" | ||
| #include "xsimd_algorithm/stl/find_if.hpp" | ||
|
|
||
| #include <iostream> | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. idem |
||
| #include <iterator> | ||
|
|
||
| namespace xsimd | ||
| { | ||
| template <class Arch = default_arch, class ContiguousIterator, class Predicate> | ||
| ContiguousIterator find_if_not(ContiguousIterator first, ContiguousIterator last, Predicate&& p) noexcept | ||
| { | ||
| return find_if<Arch>(first, last, [p](const auto& x) { return !p(x); }); | ||
| } | ||
| } | ||
|
|
||
| #endif // XSIMD_ALGORITHMS_FIND_IF_NOT_HPP | ||
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,101 @@ | ||
| /*************************************************************************** | ||
| * Copyright (c) Johan Mabille, Sylvain Corlay, Wolf Vollprecht and * | ||
| * Martin Renou * | ||
| * Copyright (c) QuantStack * | ||
| * Copyright (c) Serge Guelton * | ||
| * * | ||
| * Distributed under the terms of the BSD 3-Clause License. * | ||
| * * | ||
| * The full license is in the file LICENSE, distributed with this software. * | ||
| ****************************************************************************/ | ||
|
|
||
| #include "xsimd_algorithm/stl/find.hpp" | ||
|
|
||
|
|
||
| #ifndef XSIMD_NO_SUPPORTED_ARCHITECTURE | ||
|
|
||
| #include "doctest/doctest.h" | ||
|
|
||
| #include <cstddef> | ||
| #include <numeric> | ||
| #include <vector> | ||
|
|
||
| #if XSIMD_WITH_NEON && !XSIMD_WITH_NEON64 | ||
| #define FIND_TYPES int, float | ||
| #else | ||
| #define FIND_TYPES int, long, float, double | ||
| #endif | ||
|
|
||
|
|
||
| template <typename Type> | ||
| struct find_test | ||
| { | ||
| using vector = std::vector<Type>; | ||
| static constexpr std::size_t simd_size = xsimd::batch<Type>::size; | ||
|
|
||
| static vector fill_data(size_t size, size_t position, Type constant) | ||
| { | ||
| vector result(size); | ||
| std::iota(begin(result), end(result), Type(1)); | ||
| if (position < size) | ||
| { | ||
| result[position] = constant; | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| void test_find_aligned( size_t size, Type constant= {-42}) const | ||
| { | ||
| const auto position = size / 2; | ||
| const vector data = fill_data(size, position, constant); | ||
|
|
||
| const auto result = xsimd::find(data.begin(), data.end(), constant); | ||
| const auto expected = std::find(data.begin(), data.end(), constant); | ||
| CHECK(result == expected); | ||
| } | ||
|
|
||
| void test_find_misaligned(size_t size, Type constant= {-42}) const | ||
| { | ||
| const auto position = size / 2; | ||
| const size_t missalignment = 1; | ||
| const vector data = fill_data(size + missalignment * 2, position, constant); | ||
| auto first = data.begin() + missalignment; | ||
| auto last = first + size; | ||
|
|
||
| const auto result = xsimd::find(first, last, constant); | ||
| const auto expected = std::find(first, last, constant); | ||
|
|
||
| CHECK(result == expected); | ||
| } | ||
| }; | ||
|
|
||
| TEST_CASE_TEMPLATE("find test", T, FIND_TYPES) | ||
| { | ||
| using Test = find_test<T>; | ||
|
|
||
| const std::array test_sizes { | ||
| size_t { 0 }, | ||
| size_t { 1 }, | ||
| Test::simd_size - 1, | ||
| Test::simd_size, | ||
| Test ::simd_size + 1, | ||
| }; | ||
|
|
||
| Test test; | ||
|
|
||
| size_t sub_case_id = 0; | ||
| for (const auto size : test_sizes) | ||
| { | ||
| const std::string aligned_subcase_name = "Aligned" + std::to_string(sub_case_id++) + ", size: " + std::to_string(size); | ||
| SUBCASE(aligned_subcase_name.c_str()) | ||
| { | ||
| test.test_find_aligned(size); | ||
| } | ||
| const std::string misaligned_subcase_name = "Misaligned" + std::to_string(sub_case_id++) + ", size: " + std::to_string(size); | ||
| SUBCASE(misaligned_subcase_name.c_str()) | ||
| { | ||
| test.test_find_misaligned(size); | ||
| } | ||
| } | ||
| } | ||
| #endif |
Oops, something went wrong.
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.
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.
not needed, debugging header crap