Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions benchmark/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ set(XSIMD_ALGORITHM_BENCHMARKS
bench_math.cpp
bench_map.cpp
bench_of.cpp
bench_min_max.cpp
)

add_executable(benchmark_xsimd_algorithm ${XSIMD_ALGORITHM_BENCHMARKS})
Expand Down
114 changes: 114 additions & 0 deletions benchmark/bench_min_max.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
#include <algorithm>
#include <execution>
#include <vector>

#include <benchmark/benchmark.h>

#include "xsimd_algorithm/stl/max_element.hpp"
#include "xsimd_algorithm/stl/min_element.hpp"

#define BENCH_ARGS ->Arg(kSmall)->Arg(kMedium)->Arg(kLarge)

namespace
{
template <class ForwardIterator>
typename std::iterator_traits<ForwardIterator>::value_type dummy_std_max_value(ForwardIterator first, ForwardIterator last) noexcept
{
auto candidate = std::numeric_limits<typename std::iterator_traits<ForwardIterator>::value_type>::lowest();
for (; first != last; ++first)
{
if (candidate < *first)
{
candidate = *first;
}
}
return candidate;
}

template <typename T>
std::vector<T> make_input(std::size_t n, T constant)
{
std::vector<T> v(n);
std::fill(v.begin(), v.end(), constant);
return v;
}

template <typename T>
void BM_std_max_element(benchmark::State& state)
{
const auto constant = T { 5 };
auto src = make_input<T>(static_cast<std::size_t>(state.range(0)), constant);
src[state.range(0)-1] = T { 10 }; // Ensure there is a maximum value at the end

for (auto _ : state)
{
auto result = std::max_element(src.begin(), src.end());
benchmark::DoNotOptimize(result);
benchmark::ClobberMemory();
}
state.SetItemsProcessed(state.iterations() * state.range(0));
}

template <typename T>
void BM_xsimd_max_element(benchmark::State& state)
{
const auto constant = T { 5 };
auto src = make_input<T>(static_cast<std::size_t>(state.range(0)), constant);
src[state.range(0)-1] = T { 10 }; // Ensure there is a maximum value at the end
for (auto _ : state)
{
auto result = xsimd::max_element(src.begin(), src.end());
benchmark::DoNotOptimize(result);
benchmark::ClobberMemory();
}
state.SetItemsProcessed(state.iterations() * state.range(0));
}

template <typename T>
void BM_std_min_element(benchmark::State& state)
{
const auto constant = T { 5 };
auto src = make_input<T>(static_cast<std::size_t>(state.range(0)), constant);
src[state.range(0)-1] = T { 10 }; // Ensure there is a maximum value at the end

for (auto _ : state)
{
auto result = std::min_element(src.begin(), src.end());
benchmark::DoNotOptimize(result);
benchmark::ClobberMemory();
}
state.SetItemsProcessed(state.iterations() * state.range(0));
}

template <typename T>
void BM_xsimd_min_element(benchmark::State& state)
{
const auto constant = T { 5 };
auto src = make_input<T>(static_cast<std::size_t>(state.range(0)), constant);
src[state.range(0)-1] = T { 10 }; // Ensure there is a maximum value at the end
for (auto _ : state)
{
auto result = xsimd::min_element(src.begin(), src.end());
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;

// max_element
BENCHMARK_TEMPLATE(BM_std_max_element, float)
BENCH_ARGS;
BENCHMARK_TEMPLATE(BM_xsimd_max_element, float)
BENCH_ARGS;

// min_element
BENCHMARK_TEMPLATE(BM_std_min_element, float)
BENCH_ARGS;
BENCHMARK_TEMPLATE(BM_xsimd_min_element, float)
BENCH_ARGS;

}
2 changes: 2 additions & 0 deletions include/xsimd_algorithm/algorithms.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
#include "xsimd_algorithm/stl/all_of.hpp"
#include "xsimd_algorithm/stl/any_of.hpp"
#include "xsimd_algorithm/stl/arange.hpp"
#include "xsimd_algorithm/stl/max_element.hpp"
#include "xsimd_algorithm/stl/min_element.hpp"
//#include "xsimd_algorithm/stl/none_of.hpp"
#include "xsimd_algorithm/stl/reduce.hpp"
#include "xsimd_algorithm/stl/transform.hpp"
Expand Down
121 changes: 121 additions & 0 deletions include/xsimd_algorithm/stl/max_element.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
/***************************************************************************
* 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_MAX_ELEMENT_HPP
#define XSIMD_ALGORITHMS_MAX_ELEMENT_HPP

#include "xsimd/xsimd.hpp"

#include <iterator>

namespace xsimd
{
namespace detail
{
template <class Arch = default_arch, class ForwardIterator, class Compare>
ForwardIterator sequential_max_element(ForwardIterator first, ForwardIterator last, Compare&& comp) noexcept
{
if (first == last)
{
return last;
}

ForwardIterator max_it = first;

for (++first; first != last; ++first)
{
if (comp(*max_it, *first))
{
max_it = first;
}
}

return max_it;
}
}

template <class Arch = default_arch, class ContiguousIterator, class Compare>
ContiguousIterator max_element(ContiguousIterator first, ContiguousIterator last, Compare&& comp) 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 return for small ranges
if (size < simd_size)
{
return detail::sequential_max_element(first, last, std::forward<Compare>(comp));
}

// 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);

auto max_it = detail::sequential_max_element(first, align_begin_it, std::forward<Compare>(comp));
auto current_max = *max_it;
auto current_max_index = std::distance(first, max_it);

// Generate a batch of the current max value
batch_type current_max_batch = batch_type(current_max);

// main loop: process aligned elements
for (auto current = align_begin_it; current != align_end_it; std::advance(current, simd_size))
{
batch_type batch_val = batch_type::load_aligned(&(*current));

// Compare the current batch with the current max batch
auto mask = comp(current_max_batch, batch_val);

// Update ?
if (xsimd::any(mask))
{
// Update the current max batch
current_max_batch = xsimd::select(mask, batch_val, current_max_batch);
// extract the max value
current_max = xsimd::reduce_max(current_max_batch);
//retrieve position of the max value in the current batch
auto max_pos = std::find(current, current + simd_size, current_max);
if (max_pos != current + simd_size)
{
current_max_index = std::distance(first, max_pos);
}
}
}

max_it = std::next(first, current_max_index);

for (auto current = align_end_it; current != last; ++current)
{
if (comp(current_max, *current))
{
current_max = *current;
max_it = current;
}
}
return max_it;
}

template <class Arch = default_arch, class ContiguousIterator>
ContiguousIterator max_element(ContiguousIterator first, ContiguousIterator last) noexcept
{
return xsimd::max_element<Arch>(first, last, [](const auto& lhs, const auto& rhs) { return lhs < rhs; });
}

} // namespace xsimd

#endif // XSIMD_ALGORITHMS_MAX_ELEMENT_HPP
121 changes: 121 additions & 0 deletions include/xsimd_algorithm/stl/min_element.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
/***************************************************************************
* 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_MIN_ELEMENT_HPP
#define XSIMD_ALGORITHMS_MIN_ELEMENT_HPP

#include "xsimd/xsimd.hpp"

#include <iterator>

namespace xsimd
{
namespace detail
{
template <class Arch = default_arch, class ForwardIterator, class Compare>
ForwardIterator sequential_min_element(ForwardIterator first, ForwardIterator last, Compare&& comp) noexcept
{
if (first == last)
{
return last;
}

ForwardIterator min_it = first;

for (++first; first != last; ++first)
{
if (comp(*min_it, *first))
{
min_it = first;
}
}

return min_it;
}
}

template <class Arch = default_arch, class ContiguousIterator, class Compare>
ContiguousIterator min_element(ContiguousIterator first, ContiguousIterator last, Compare&& comp) 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 return for small ranges
if (size < simd_size)
{
return detail::sequential_min_element(first, last, std::forward<Compare>(comp));
}

// 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);

auto min_it = detail::sequential_min_element(first, align_begin_it, std::forward<Compare>(comp));
auto current_min = *min_it;
auto current_min_index = std::distance(first, min_it);

// Generate a batch of the current min value
batch_type current_min_batch = batch_type(current_min);

// main loop: process aligned elements
for (auto current = align_begin_it; current != align_end_it; std::advance(current, simd_size))
{
batch_type batch_val = batch_type::load_aligned(&(*current));

// Compare the current batch with the current min batch
auto mask = comp(current_min_batch, batch_val);

// Update ?
if (xsimd::any(mask))
{
// Update the current min batch
current_min_batch = xsimd::select(mask, batch_val, current_min_batch);
// extract the min value
current_min = xsimd::reduce_min(current_min_batch);
//retrieve position of the min value in the current batch
auto min_pos = std::find(current, current + simd_size, current_min);
if (min_pos != current + simd_size)
{
current_min_index = std::distance(first, min_pos);
}
}
}

min_it = std::next(first, current_min_index);

for (auto current = align_end_it; current != last; ++current)
{
if (comp(current_min, *current))
{
current_min = *current;
min_it = current;
}
}
return min_it;
}

template <class Arch = default_arch, class ContiguousIterator>
ContiguousIterator min_element(ContiguousIterator first, ContiguousIterator last) noexcept
{
return xsimd::min_element<Arch>(first, last, [](const auto& lhs, const auto& rhs) { return lhs > rhs; });
}

} // namespace xsimd

#endif // XSIMD_ALGORITHMS_MIN_ELEMENT_HPP
2 changes: 2 additions & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ set(XSIMD_ALGORITHM_TESTS
test_math.cpp
test_reduce.cpp
test_transform.cpp
test_max_element.cpp
test_min_element.cpp
)

add_executable(test_xsimd_algorithm ${XSIMD_ALGORITHM_TESTS})
Expand Down
Loading
Loading