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_count.cpp
)

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

#include <benchmark/benchmark.h>

#include "xsimd_algorithm/stl/count.hpp"

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

namespace
{
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_count(benchmark::State& state)
{
const auto constant = T { 5 };

auto src = make_input<T>(static_cast<std::size_t>(state.range(0)), constant);

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

template <typename T>
void BM_xsimd_count(benchmark::State& state)
{
const auto constant = T { 5 };
auto src = make_input<T>(static_cast<std::size_t>(state.range(0)), constant);
for (auto _ : state)
{
auto result = xsimd::count(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;

// count
BENCHMARK_TEMPLATE(BM_std_count, float)
BENCH_ARGS;
BENCHMARK_TEMPLATE(BM_xsimd_count, float)
BENCH_ARGS;

}
4 changes: 3 additions & 1 deletion include/xsimd_algorithm/algorithms.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
#include "xsimd_algorithm/stl/all_of.hpp"
#include "xsimd_algorithm/stl/any_of.hpp"
#include "xsimd_algorithm/stl/arange.hpp"
//#include "xsimd_algorithm/stl/none_of.hpp"
#include "xsimd_algorithm/stl/count.hpp"
#include "xsimd_algorithm/stl/count_if.hpp"
#include "xsimd_algorithm/stl/none_of.hpp"
#include "xsimd_algorithm/stl/reduce.hpp"
#include "xsimd_algorithm/stl/transform.hpp"

Expand Down
29 changes: 29 additions & 0 deletions include/xsimd_algorithm/stl/count.hpp
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_COUNT_HPP
#define XSIMD_ALGORITHMS_COUNT_HPP

#include "xsimd/xsimd.hpp"
#include "xsimd_algorithm/stl/count_if.hpp"

#include <iterator>

namespace xsimd
{
template <class Arch = default_arch, class ContiguousIterator, class T = typename std::iterator_traits<ContiguousIterator>::value_type>
std::iter_difference_t<ContiguousIterator> count(ContiguousIterator first, ContiguousIterator last, const T& value) noexcept
{
return count_if<Arch>(first, last, [value](const auto& x) { return x == value; });
}
}

#endif // XSIMD_ALGORITHMS_COUNT_HPP
82 changes: 82 additions & 0 deletions include/xsimd_algorithm/stl/count_if.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/***************************************************************************
* 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_COUNT_IF_HPP
#define XSIMD_ALGORITHMS_COUNT_IF_HPP

#include "xsimd/xsimd.hpp"

#include <iterator>

namespace xsimd
{
namespace detail
{
template <class Arch = default_arch, class ForwardIterator, class Predicate>
std::iter_difference_t<ForwardIterator> sequential_count_if(ForwardIterator first, ForwardIterator last, Predicate&& p) noexcept
{
std::iter_difference_t<ForwardIterator> count = {0};
for (; first != last; ++first)
{
if (p(*first))
{
++count;
}
}
return count;
}
}

template <class Arch = default_arch, class ContiguousIterator, class Predicate>
std::iter_difference_t<ContiguousIterator> count_if(ContiguousIterator first, ContiguousIterator last, Predicate&& p) noexcept
{
std::iter_difference_t<ContiguousIterator> count = {0};

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_count_if(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);

count += detail::sequential_count_if(first, align_begin_it, std::forward<Predicate>(p));

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

// epilogue: process remaining elements
count += detail::sequential_count_if(align_end_it, last, std::forward<Predicate>(p));

return count;
}

}

#endif // XSIMD_ALGORITHMS_COUNT_IF_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_count.cpp
test_count_if.cpp
)

add_executable(test_xsimd_algorithm ${XSIMD_ALGORITHM_TESTS})
Expand Down
96 changes: 96 additions & 0 deletions test/test_count.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/***************************************************************************
* 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/count.hpp"

#ifndef XSIMD_NO_SUPPORTED_ARCHITECTURE

#include "doctest/doctest.h"

#include <cstddef>
#include <vector>

#if XSIMD_WITH_NEON && !XSIMD_WITH_NEON64
#define COUNT_TYPES int, float
#else
#define COUNT_TYPES int, long, float, double
#endif

template <typename Type>
struct count_test
{
using vector = std::vector<Type>;
static constexpr std::size_t simd_size = xsimd::batch<Type>::size;

static vector fill_data(size_t size, Type constant)
{
vector result(size);

for (size_t i = 0; i < size; ++i)
{
result[i] = constant;
}

return result;
}

void test_count_aligned( size_t size, Type constant= {5}) const
{
const vector data = fill_data(size, constant);

const auto result = xsimd::count(data.begin(), data.end(), constant);

CHECK(result == size);
}

void test_count_misaligned(size_t size, Type constant= {5}) const
{
const size_t missalignment = 1;
const vector data = fill_data(size + missalignment * 2, constant);
auto first = data.begin() + missalignment;
auto last = first + size;

const auto result = xsimd::count(first, last, constant);

CHECK(result == std::distance(first, last));
}
};

TEST_CASE_TEMPLATE("count test", T, COUNT_TYPES)
{
using Test = count_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_count_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_count_misaligned(size);
}
}
}
#endif
Loading
Loading