HIBF 1.0.0-rc.1
Loading...
Searching...
No Matches
counting_vector.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2006-2026, Knut Reinert & Freie Universität Berlin
2// SPDX-FileCopyrightText: 2016-2026, Knut Reinert & MPI für molekulare Genetik
3// SPDX-License-Identifier: BSD-3-Clause
4
10#pragma once
11
12#include <algorithm> // for transform
13#include <bit> // for countr_zero
14#include <climits> // for CHAR_BIT
15#include <concepts> // for integral
16#include <cstddef> // for size_t
17#include <cstdint> // for uint64_t, uint8_t
18#include <functional> // for minus, plus
19#include <type_traits> // for conditional_t
20#include <vector> // for vector
21
22#include <hibf/contrib/aligned_allocator.hpp> // for aligned_allocator
23#include <hibf/misc/bit_vector.hpp> // for bit_vector
24#include <hibf/misc/divide_and_ceil.hpp> // for divide_and_ceil
25#include <hibf/misc/unreachable.hpp> // for assert, unreachable
26#include <hibf/platform.hpp> // for HIBF_HAS_AVX512
27
28#if HIBF_HAS_AVX512
29# include <simde/x86/avx512/add.h> // for simde_mm512_add_epi16, simde_mm512_add_epi32, simde_mm512_add_...
30# include <simde/x86/avx512/load.h> // for simde_mm512_load_si512
31# include <simde/x86/avx512/mov.h> // for simde_mm512_maskz_mov_epi16, simde_mm512_maskz_mov_epi32, simd...
32# include <simde/x86/avx512/set1.h> // for simde_mm512_set1_epi16, simde_mm512_set1_epi32, simde_mm512_se...
33# include <simde/x86/avx512/store.h> // for simde_mm512_store_si512
34# include <simde/x86/avx512/sub.h> // for simde_mm512_sub_epi16, simde_mm512_sub_epi32, simde_mm512_sub_...
35# include <simde/x86/avx512/types.h> // for simde__m512i
36#endif
37
38namespace seqan::hibf
39{
40
41#if HIBF_HAS_AVX512
43// Since the counting_vector can have different value types, we need specific SIMD instructions for each value type.
44template <std::integral integral_t>
45struct simd_mapping
46{};
47
48// CRTP base class for the simd_mapping, containg common functionality.
49template <typename derived_t, std::integral integral_t>
51struct simd_mapping_crtp
52{
53 // Let `B = sizeof(integral_t) * CHAR_BIT`, e.g. 8 for (u)int8_t, and 16 for (u)int16_t.
54 // We can process `512 / B` bits of the bit_vector at once.
55 static inline constexpr size_t bits_per_iterations = 512u / (sizeof(integral_t) * CHAR_BIT);
56 // clang-format off
57 // The type that is need to represent `bits_per_iterations` bits.
58 using bits_type = std::conditional_t<bits_per_iterations == 64, uint64_t,
59 std::conditional_t<bits_per_iterations == 32, uint32_t,
60 std::conditional_t<bits_per_iterations == 16, uint16_t,
62 // clang-format on
63 static_assert(!std::same_as<bits_type, void>, "Unsupported bits_type.");
64
65 static inline constexpr size_t next_multiple(size_t const value) noexcept
66 {
67 return (value + bits_per_iterations - 1u) & ~(bits_per_iterations - 1u);
68 }
69
70 // Takes B bits from the bit_vector and expands them to a bits_type.
71 // E.g., B = 8 : [1,0,1,1,0,0,1,0] -> [0...01, 0...00, 0...01, 0...01, 0...00, 0...00, 0...01, 0...00], where
72 // each element is 64 bits wide.
73 static inline constexpr auto expand_bits(bits_type const * const bits)
74 {
75 return derived_t::mm512_maskz_mov_epi(*bits, derived_t::mm512_set1_epi(1));
76 }
77};
78
79// SIMD instructions for int8_t and uint8_t.
80template <std::integral integral_t>
81 requires (sizeof(integral_t) == 1)
82struct simd_mapping<integral_t> : simd_mapping_crtp<simd_mapping<integral_t>, integral_t>
83{
84 static inline constexpr auto mm512_maskz_mov_epi = simde_mm512_maskz_mov_epi8;
85 static inline constexpr auto mm512_set1_epi = simde_mm512_set1_epi8;
86 static inline constexpr auto mm512_add_epi = simde_mm512_add_epi8;
87 static inline constexpr auto mm512_sub_epi = simde_mm512_sub_epi8;
88};
89
90// SIMD instructions for int16_t and uint16_t.
91template <std::integral integral_t>
92 requires (sizeof(integral_t) == 2)
93struct simd_mapping<integral_t> : simd_mapping_crtp<simd_mapping<integral_t>, integral_t>
94{
95 static inline constexpr auto mm512_maskz_mov_epi = simde_mm512_maskz_mov_epi16;
96 static inline constexpr auto mm512_set1_epi = simde_mm512_set1_epi16;
97 static inline constexpr auto mm512_add_epi = simde_mm512_add_epi16;
98 static inline constexpr auto mm512_sub_epi = simde_mm512_sub_epi16;
99};
100
101// SIMD instructions for int32_t and uint32_t.
102template <std::integral integral_t>
103 requires (sizeof(integral_t) == 4)
104struct simd_mapping<integral_t> : simd_mapping_crtp<simd_mapping<integral_t>, integral_t>
105{
106 static inline constexpr auto mm512_maskz_mov_epi = simde_mm512_maskz_mov_epi32;
107 static inline constexpr auto mm512_set1_epi = simde_mm512_set1_epi32;
108 static inline constexpr auto mm512_add_epi = simde_mm512_add_epi32;
109 static inline constexpr auto mm512_sub_epi = simde_mm512_sub_epi32;
110};
111
112// SIMD instructions for int64_t and uint64_t.
113template <std::integral integral_t>
114 requires (sizeof(integral_t) == 8)
115struct simd_mapping<integral_t> : simd_mapping_crtp<simd_mapping<integral_t>, integral_t>
116{
117 static inline constexpr auto mm512_maskz_mov_epi = simde_mm512_maskz_mov_epi64;
118 static inline constexpr auto mm512_set1_epi = simde_mm512_set1_epi64;
119 static inline constexpr auto mm512_add_epi = simde_mm512_add_epi64;
120 static inline constexpr auto mm512_sub_epi = simde_mm512_sub_epi64;
121};
123#endif
124
148template <std::integral value_t>
149class counting_vector : public std::vector<value_t, seqan::hibf::contrib::aligned_allocator<value_t, 64u>>
150{
151private:
154
155public:
159 counting_vector() = default;
160 counting_vector(counting_vector const &) = default;
164 ~counting_vector() = default;
165
166 using base_t::base_t;
168
177 {
178 impl<operation::add>(bit_vector);
179 return *this;
180 }
181
193 {
194 impl<operation::sub>(bit_vector);
195 return *this;
196 }
197
206 {
207 assert(this->size() >= rhs.size()); // The counting vector may be bigger than what we need.
208
209 std::transform(this->begin(), this->end(), rhs.begin(), this->begin(), std::plus<value_t>());
210
211 return *this;
212 }
213
219 {
220 assert(this->size() >= rhs.size()); // The counting vector may be bigger than what we need.
221
222 std::transform(this->begin(), this->end(), rhs.begin(), this->begin(), std::minus<value_t>());
223
224 return *this;
225 }
226
227private:
229 enum class operation : uint8_t
230 {
231 add,
232 sub
233 };
234
236 template <operation op>
237 inline void impl(bit_vector const & bit_vector)
238 {
239 // The counting vector may be bigger than what we need, but not smaller.
240 if (this->size() < bit_vector.size())
241 seqan::hibf::unreachable();
242#if HIBF_HAS_AVX512
243 // AVX512BW: mm512_maskz_mov_epi, mm512_add_epi
244 // AVX512F: mm512_set1_epi, _mm512_load_si512, _mm512_store_si512
245 using simd = simd_mapping<value_t>;
246 using bits_type = typename simd::bits_type;
247
248 bits_type const * bit_vector_ptr = reinterpret_cast<bits_type const *>(bit_vector.data());
249 value_t * counting_vector_ptr = base_t::data();
250
251 size_t const bits = simd::next_multiple(bit_vector.size());
252 assert(bits <= this->capacity()); // Not enough memory reserved for AVX512 chunk access.
253 size_t const iterations = bits / simd::bits_per_iterations;
254
255 for (size_t iteration = 0; iteration < iterations;
256 ++iteration, ++bit_vector_ptr, counting_vector_ptr += simd::bits_per_iterations)
257 {
258 simde__m512i load = simde_mm512_load_si512(counting_vector_ptr);
259 if constexpr (op == operation::add)
260 {
261 load = simd::mm512_add_epi(load, simd::expand_bits(bit_vector_ptr));
262 }
263 else
264 {
265 load = simd::mm512_sub_epi(load, simd::expand_bits(bit_vector_ptr));
266 }
267 simde_mm512_store_si512(counting_vector_ptr, load);
268 }
269#else
270 size_t const words = divide_and_ceil(bit_vector.size(), 64u);
271 uint64_t const * const bit_vector_ptr = bit_vector.data();
272
273 // Jump to the next 1 and return the number of jumped bits in value.
274 auto jump_to_next_1bit = [](uint64_t & value)
275 {
276 auto const zeros = std::countr_zero(value);
277 value >>= zeros; // skip number of zeros
278 return zeros;
279 };
280
281 // Each iteration can handle 64 bits, i.e., one word.
282 for (size_t iteration = 0; iteration < words; ++iteration)
283 {
284 uint64_t current_word = bit_vector_ptr[iteration];
285
286 // For each set bit in the current word, add/subtract 1 to the corresponding bin.
287 for (size_t bin = iteration * 64u; current_word != 0u; ++bin, current_word >>= 1)
288 {
289 // Jump to the next 1
290 bin += jump_to_next_1bit(current_word);
291
292 if constexpr (op == operation::add)
293 {
294 ++(*this)[bin];
295 }
296 else
297 {
298 --(*this)[bin];
299 }
300 }
301 }
302#endif
303 }
304};
305
306} // namespace seqan::hibf
Provides seqan::hibf::bit_vector.
An bit vector.
Definition bit_vector.hpp:66
A data structure that behaves like a std::vector and can be used to consolidate the results of multip...
Definition counting_vector.hpp:150
counting_vector & operator+=(counting_vector const &rhs)
Bin-wise addition of two seqan::hibf::counting_vectors.
Definition counting_vector.hpp:205
counting_vector & operator+=(bit_vector const &bit_vector)
Bin-wise adds the bits of a seqan::hibf::bit_vector.
Definition counting_vector.hpp:176
counting_vector(counting_vector const &)=default
Defaulted.
counting_vector & operator-=(bit_vector const &bit_vector)
Bin-wise subtracts the bits of a seqan::hibf::bit_vector.
Definition counting_vector.hpp:192
counting_vector & operator-=(counting_vector const &rhs)
Bin-wise subtraction of two seqan::hibf::counting_vectors.
Definition counting_vector.hpp:218
~counting_vector()=default
Defaulted.
counting_vector & operator=(counting_vector &&)=default
Defaulted.
counting_vector(counting_vector &&)=default
Defaulted.
counting_vector & operator=(counting_vector const &)=default
Defaulted.
counting_vector()=default
Defaulted.
T countr_zero(T... args)
constexpr size_t divide_and_ceil(t1 const dividend, t2 const divisor) noexcept
Returns, for unsigned integral operands, dividend / divisor ceiled to the next integer value.
Definition divide_and_ceil.hpp:21
T is_base_of_v
Provides platform and dependency checks.
T transform(T... args)
Provides seqan::hibf::unreachable.