SeqAn3 3.4.0-rc.1
The Modern C++ library for sequence analysis.
Loading...
Searching...
No Matches
algorithm_executor_blocking.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 <functional>
13#include <optional>
14#include <ranges>
15#include <type_traits>
16
19
20namespace seqan3::detail
21{
22
51template <std::ranges::viewable_range resource_t,
52 std::semiregular algorithm_t,
53 std::semiregular algorithm_result_t,
54 typename execution_handler_t = execution_handler_sequential>
55 requires std::ranges::forward_range<resource_t>
56 && std::invocable<algorithm_t,
57 std::ranges::range_reference_t<resource_t>,
58 std::function<void(algorithm_result_t)>>
59class algorithm_executor_blocking
60{
61private:
66 using resource_type = std::views::all_t<resource_t>;
68 using resource_iterator_type = std::ranges::iterator_t<resource_type>;
70 using resource_difference_type = std::iter_difference_t<resource_iterator_type>;
72
77 using bucket_type = std::vector<algorithm_result_t>;
79 using bucket_iterator_type = std::ranges::iterator_t<bucket_type>;
81 using buffer_type = std::vector<bucket_type>;
83 using buffer_iterator_type = std::ranges::iterator_t<buffer_type>;
85
87 enum fill_status
88 {
89 non_empty_buffer,
90 empty_buffer,
91 end_of_resource
92 };
93
94public:
100 algorithm_executor_blocking() = delete;
102 algorithm_executor_blocking(algorithm_executor_blocking const &) = delete;
103
121 algorithm_executor_blocking(algorithm_executor_blocking && other) noexcept :
122 algorithm_executor_blocking{std::move(other), other.resource_position()}
123 {}
124
126 algorithm_executor_blocking & operator=(algorithm_executor_blocking const &) = delete;
127
130 algorithm_executor_blocking & operator=(algorithm_executor_blocking && other)
131 {
132 auto old_resource_position = other.resource_position();
133
134 resource = std::move(other.resource);
135 move_initialise(std::move(other), old_resource_position);
136 return *this;
137 }
138
140 ~algorithm_executor_blocking() = default;
141
156 algorithm_executor_blocking(resource_t resource,
157 algorithm_t algorithm,
158 algorithm_result_t const SEQAN3_DOXYGEN_ONLY(result) = algorithm_result_t{},
159 execution_handler_t && exec_handler = execution_handler_t{}) :
160 exec_handler{std::move(exec_handler)},
161 resource{std::forward<resource_t>(resource)},
162 resource_it{std::ranges::begin(this->resource)},
163 algorithm{std::move(algorithm)}
164 {
165 if constexpr (std::same_as<execution_handler_t, execution_handler_parallel>)
166 buffer_size = static_cast<size_t>(std::ranges::distance(this->resource));
167
168 buffer.resize(buffer_size);
169 buffer_it = buffer.end();
170 buffer_end_it = buffer_it;
171 }
173
190 {
191 fill_status status;
192 // Each invocation of the algorithm might produce zero results (e.g. a search might not find a query)
193 // this repeats the algorithm until it produces the first result or the input resource was consumed.
194 do
195 {
196 status = fill_buffer();
197 }
198 while (status == fill_status::empty_buffer);
199
200 if (status == fill_status::end_of_resource)
201 return {std::nullopt};
202
203 assert(status == fill_status::non_empty_buffer);
204 assert(bucket_it != buffer_it->end());
205
206 std::optional<algorithm_result_t> result = std::ranges::iter_move(bucket_it);
207 go_to_next_result(); // Go to next buffered result.
208 return result;
209 }
210
212 bool is_eof() noexcept
213 {
214 return resource_it == std::ranges::end(resource);
215 }
216
217private:
223 algorithm_executor_blocking(algorithm_executor_blocking && other,
224 resource_difference_type old_resource_position) noexcept :
225 resource{std::move(other.resource)}
226 {
227 move_initialise(std::move(other), old_resource_position);
228 }
229
236 resource_difference_type resource_position()
237 {
238 // Get the old resource position.
239 auto position = std::ranges::distance(std::ranges::begin(resource), resource_it);
240 assert(position >= 0);
241 return position;
242 }
243
245 fill_status fill_buffer()
246 {
247 if (!is_buffer_empty()) // Not everything consumed yet.
248 return fill_status::non_empty_buffer;
249
250 if (is_eof()) // Case: reached end of resource.
251 return fill_status::end_of_resource;
252
253 // Reset the buckets and the buffer iterator.
254 reset_buffer();
255
256 // Execute the algorithm (possibly asynchronous) and fill the buckets in this pre-assigned order.
257 for (buffer_end_it = buffer_it; buffer_end_it != buffer.end() && !is_eof(); ++buffer_end_it, ++resource_it)
258 {
259 exec_handler.execute(algorithm,
260 *resource_it,
261 [target_buffer_it = buffer_end_it](auto && algorithm_result)
262 {
263 target_buffer_it->push_back(std::move(algorithm_result));
264 });
265 }
266
267 exec_handler.wait();
268
269 // Move the results iterator to the next available result. (This skips empty results of the algorithm)
270 find_next_non_empty_bucket();
271
272 if (is_buffer_empty())
273 return fill_status::empty_buffer;
274
275 return fill_status::non_empty_buffer;
276 }
277
281 bool is_buffer_empty() const
282 {
283 return buffer_it == buffer_end_it;
284 }
285
294 void reset_buffer()
295 {
296 // Clear all buckets
297 for (auto & bucket : buffer)
298 bucket.clear();
299
300 // Reset the iterator over the buckets.
301 buffer_it = buffer.begin();
302 }
303
312 void find_next_non_empty_bucket()
313 {
314 assert(buffer_it <= buffer_end_it);
315 // find first buffered bucket that contains at least one element
316 buffer_it = std::find_if(buffer_it,
317 buffer_end_it,
318 [](auto const & buffer)
319 {
320 return !buffer.empty();
321 });
322
323 if (buffer_it != buffer_end_it)
324 bucket_it = buffer_it->begin();
325 }
326
334 void go_to_next_result()
335 {
336 if (++bucket_it == buffer_it->end())
337 {
338 ++buffer_it;
339 find_next_non_empty_bucket();
340 }
341 }
342
344 void move_initialise(algorithm_executor_blocking && other, resource_difference_type old_resource_position) noexcept
345 {
346 algorithm = std::move(other.algorithm);
347 buffer_size = std::move(other.buffer_size);
348 exec_handler = std::move(other.exec_handler);
349 // Move the resource and set the iterator state accordingly.
350 resource_it = std::ranges::next(std::ranges::begin(resource), old_resource_position);
351
352 // Get the old buffer and bucket iterator positions.
353 auto buffer_it_position = other.buffer_it - other.buffer.begin();
354 auto buffer_end_it_position = other.buffer_end_it - other.buffer.begin();
355
356 std::ptrdiff_t bucket_it_position = 0;
357 if (buffer_it_position != buffer_end_it_position)
358 bucket_it_position = other.bucket_it - other.buffer_it->begin();
359
360 // Move the buffer and set the buffer and bucket iterator accordingly.
361 buffer = std::move(other.buffer);
362 buffer_it = buffer.begin() + buffer_it_position;
363 buffer_end_it = buffer.begin() + buffer_end_it_position;
364
365 if (buffer_it_position != buffer_end_it_position)
366 bucket_it = buffer_it->begin() + bucket_it_position;
367 }
368
370 execution_handler_t exec_handler{};
371
373 resource_type resource; // a std::ranges::view
375 resource_iterator_type resource_it{};
377 algorithm_t algorithm{};
378
380 buffer_type buffer{};
382 buffer_iterator_type buffer_it{};
384 buffer_iterator_type buffer_end_it{};
386 bucket_iterator_type bucket_it{};
388 size_t buffer_size{1};
389};
390
397template <typename resource_rng_t, std::semiregular algorithm_t, std::semiregular algorithm_result_t>
398algorithm_executor_blocking(resource_rng_t &&, algorithm_t, algorithm_result_t const &)
399 -> algorithm_executor_blocking<resource_rng_t, algorithm_t, algorithm_result_t, execution_handler_sequential>;
401} // namespace seqan3::detail
T begin(T... args)
Provides seqan3::detail::execution_handler_parallel.
Provides seqan3::detail::execution_handler_sequential.
T find_if(T... args)
T forward(T... args)
constexpr auto is_eof
Checks whether a given letter is equal to the EOF constant defined in <cstdio>.
Definition predicate.hpp:72
T move(T... args)
SeqAn specific customisations in the standard namespace.
Hide me