SeqAn3 3.4.0-rc.1
The Modern C++ library for sequence analysis.
Loading...
Searching...
No Matches
policy_optimum_tracker_simd.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2006-2024 Knut Reinert & Freie Universität Berlin
2// SPDX-FileCopyrightText: 2016-2024 Knut Reinert & MPI für molekulare Genetik
3// SPDX-License-Identifier: BSD-3-Clause
4
10#pragma once
11
12#include <limits>
13#include <ranges>
14
16#include <seqan3/utility/simd/algorithm.hpp>
17#include <seqan3/utility/simd/simd.hpp>
18#include <seqan3/utility/simd/simd_traits.hpp>
21
22namespace seqan3::detail
23{
24
41struct max_score_updater_simd_global
42{
43
60 template <typename score_t, typename coordinate_t>
61 requires (std::assignable_from<score_t &, score_t const &> &&
62 requires (coordinate_t coordinate)
63 {
64 requires simd_concept<decltype(coordinate.col)>;
65 requires simd_concept<decltype(coordinate.row)>;
66 })
67 void operator()(score_t & optimal_score,
68 coordinate_t const & optimal_coordinate,
69 score_t current_score,
70 coordinate_t const & current_coordinate) const noexcept
71 {
72 auto mask =
73 (optimal_coordinate.col == current_coordinate.col) && (optimal_coordinate.row == current_coordinate.row);
74 optimal_score = (mask) ? std::move(current_score) : optimal_score;
75 }
76};
77
82template <typename alignment_configuration_t, std::semiregular optimum_updater_t>
83 requires is_type_specialisation_of_v<alignment_configuration_t, configuration>
84 && std::invocable<
85 optimum_updater_t,
86 typename alignment_configuration_traits<alignment_configuration_t>::score_type &,
87 typename alignment_configuration_traits<alignment_configuration_t>::matrix_coordinate_type &,
88 typename alignment_configuration_traits<alignment_configuration_t>::score_type,
89 typename alignment_configuration_traits<alignment_configuration_t>::matrix_coordinate_type>
90class policy_optimum_tracker_simd : protected policy_optimum_tracker<alignment_configuration_t, optimum_updater_t>
91{
92protected:
94 using base_policy_t = policy_optimum_tracker<alignment_configuration_t, optimum_updater_t>;
95
96 // Import the configured score type.
97 using typename base_policy_t::score_type;
98 using typename base_policy_t::traits_type;
99
101 using scalar_type = typename simd::simd_traits<score_type>::scalar_type;
103 using original_score_type = typename traits_type::original_score_type;
104
105 static_assert(simd_concept<score_type>, "Must be a simd type!");
106
107 // Import base variables into class scope.
108 using base_policy_t::compare_and_set_optimum;
109 using base_policy_t::optimal_coordinate;
110 using base_policy_t::optimal_score;
113
117 policy_optimum_tracker_simd() = default;
118 policy_optimum_tracker_simd(policy_optimum_tracker_simd const &) = default;
119 policy_optimum_tracker_simd(policy_optimum_tracker_simd &&) = default;
120 policy_optimum_tracker_simd & operator=(policy_optimum_tracker_simd const &) = default;
121 policy_optimum_tracker_simd & operator=(policy_optimum_tracker_simd &&) = default;
122 ~policy_optimum_tracker_simd() = default;
123
132 policy_optimum_tracker_simd(alignment_configuration_t const & config) : base_policy_t{config}
133 {
134 base_policy_t::test_last_row_cell = true;
135 base_policy_t::test_last_column_cell = true;
136 }
138
140 void reset_optimum()
141 {
142 optimal_score = simd::fill<score_type>(std::numeric_limits<scalar_type>::lowest());
143 }
144
191 template <std::ranges::input_range sequence1_collection_t, std::ranges::input_range sequence2_collection_t>
192 void initialise_tracker(sequence1_collection_t & sequence1_collection,
193 sequence2_collection_t & sequence2_collection)
194 {
195 using index_t = typename traits_type::matrix_index_type;
196 using scalar_index_t = typename simd_traits<index_t>::scalar_type;
197
198 scalar_index_t largest_sequence1_size{};
199 scalar_index_t largest_sequence2_size{};
200 alignas(alignof(index_t)) std::array<scalar_index_t, traits_type::alignments_per_vector> sequence1_sizes{};
201 alignas(alignof(index_t)) std::array<scalar_index_t, traits_type::alignments_per_vector> sequence2_sizes{};
202
203 // First, get all dimensions from the sequences and keep track of the maximal size in either dimension.
204 size_t sequence_count{};
205 for (auto && [sequence1, sequence2] : views::zip(sequence1_collection, sequence2_collection))
206 {
207 sequence1_sizes[sequence_count] = std::ranges::distance(sequence1);
208 sequence2_sizes[sequence_count] = std::ranges::distance(sequence2);
209 largest_sequence1_size = std::max(largest_sequence1_size, sequence1_sizes[sequence_count]);
210 largest_sequence2_size = std::max(largest_sequence2_size, sequence2_sizes[sequence_count]);
211 ++sequence_count;
212 }
213
214 // Second, determine the offset for each individual end-coordinate which is used to project the cell to the
215 // last row or column of the global alignment matrix. Choose the smallest distance as the correct offset
216 // to the projected cell.
217 for (size_t index = 0; index != sequence_count; ++index)
218 {
219 assert(sequence1_sizes[index] <= largest_sequence1_size);
220 assert(sequence2_sizes[index] <= largest_sequence2_size);
221
222 padding_offsets[index] = std::min(largest_sequence1_size - sequence1_sizes[index],
223 largest_sequence2_size - sequence2_sizes[index]);
224 sequence1_sizes[index] += padding_offsets[index];
225 sequence2_sizes[index] += padding_offsets[index];
226 }
227
228 // Load the target coordinate indices from the respective arrays.
229 optimal_coordinate.col = simd::load<index_t>(sequence1_sizes.data());
230 optimal_coordinate.row = simd::load<index_t>(sequence2_sizes.data());
231 }
232};
233} // namespace seqan3::detail
seqan::stl::views::zip zip
A view adaptor that takes several views and returns tuple-like values from every i-th element of each...
Definition zip.hpp:24
Provides lazy template instantiation traits.
T max(T... args)
T min(T... args)
Provides seqan3::detail::policy_optimum_tracker.
Provides seqan3::views::zip.
Hide me