SeqAn3  3.0.3
The Modern C++ library for sequence analysis.
execution_handler_parallel.hpp
Go to the documentation of this file.
1 // -----------------------------------------------------------------------------------------------------
2 // Copyright (c) 2006-2021, Knut Reinert & Freie Universität Berlin
3 // Copyright (c) 2016-2021, Knut Reinert & MPI für molekulare Genetik
4 // This file may be used, modified and/or redistributed under the terms of the 3-clause BSD-License
5 // shipped with this file and also available at: https://github.com/seqan/seqan3/blob/master/LICENSE.md
6 // -----------------------------------------------------------------------------------------------------
7 
13 #pragma once
14 
15 #include <seqan3/std/concepts>
16 #include <functional>
17 #include <seqan3/std/ranges>
18 #include <thread>
19 #include <type_traits>
20 #include <vector>
21 
25 
26 namespace seqan3::detail
27 {
28 
54 class execution_handler_parallel
55 {
56 private:
58  using task_type = std::function<void()>;
59 
60 public:
73  execution_handler_parallel(size_t const thread_count) : state{std::make_unique<internal_state>()}
74  {
75  auto * q = &(state->queue);
76  for (size_t i = 0; i < thread_count; ++i)
77  {
78  state->thread_pool.emplace_back([q] ()
79  {
80  for (;;)
81  {
82  task_type task;
83  if (q->wait_pop(task) == contrib::queue_op_status::closed)
84  return;
85 
86  task();
87  }
88  });
89  }
90  }
91 
106  execution_handler_parallel() : execution_handler_parallel{1u}
107  {}
108 
109  execution_handler_parallel(execution_handler_parallel const &) = delete;
110  execution_handler_parallel(execution_handler_parallel &&) = default;
111  execution_handler_parallel & operator=(execution_handler_parallel const &) = delete;
112  execution_handler_parallel & operator=(execution_handler_parallel &&) = default;
113  ~execution_handler_parallel() = default;
114 
116 
135  template <std::copy_constructible algorithm_t,
136  typename algorithm_input_t,
137  std::copy_constructible callback_t>
139  requires std::invocable<algorithm_t, algorithm_input_t, callback_t> &&
140  (std::is_lvalue_reference_v<algorithm_input_t> || std::move_constructible<algorithm_input_t>)
142  void execute(algorithm_t && algorithm, algorithm_input_t && input, callback_t && callback)
143  {
144  assert(state != nullptr);
145 
146  // Note: Unfortunately, we can't use std::forward_as_tuple here because a std::function object (`task_type`)
147  // cannot be constructed if the tuple element type is a rvalue-reference.
148  // So we capture the input as a `tuple<algorithm_input_t>` which either is a lvalue reference or has no
149  // reference type according to the reference collapsing rules of forwarding references.
150  // Then we forward the input into the tuple which either just stores the reference or the input is moved into
151  // the tuple. When the task is executed by some thread the stored input will either be forwarded as a
152  // lvalue-reference to the algorithm or the input is moved into the algorithm from the tuple. This is valid
153  // since the task is executed only once by the parallel execution handler.
154  // Here is a discussion about the problem on stackoverflow:
155  // https://stackoverflow.com/questions/26831382/capturing-perfectly-forwarded-variable-in-lambda/
156 
157  // Asynchronously pushes the algorithm job as a task to the queue.
158  task_type task = [=, input_tpl = std::tuple<algorithm_input_t>{std::forward<algorithm_input_t>(input)}] ()
159  {
160  using forward_input_t = std::tuple_element_t<0, decltype(input_tpl)>;
161  algorithm(std::forward<forward_input_t>(std::get<0>(input_tpl)), std::move(callback));
162  };
163 
164  [[maybe_unused]] contrib::queue_op_status status = state->queue.wait_push(std::move(task));
165  assert(status == contrib::queue_op_status::success);
166  }
167 
184  template <std::copy_constructible algorithm_t,
185  std::ranges::input_range algorithm_input_range_t,
186  std::copy_constructible callback_t>
188  requires std::invocable<algorithm_t, std::ranges::range_reference_t<algorithm_input_range_t>, callback_t>
190  void bulk_execute(algorithm_t && algorithm, algorithm_input_range_t && input_range, callback_t && callback)
191  {
192  for (auto && input : input_range)
193  execute(algorithm, std::forward<decltype(input)>(input), callback);
194 
195  wait();
196  }
197 
199  void wait()
200  {
201  assert(state != nullptr);
202 
203  state->stop_and_wait();
204  }
205 
206 private:
215  class internal_state
216  {
217  public:
222  internal_state() = default;
223  internal_state(internal_state const &) = delete;
224  internal_state(internal_state &&) = default;
225  internal_state & operator=(internal_state const &) = delete;
226  internal_state & operator=(internal_state &&) = default;
227 
229  ~internal_state()
230  {
231  stop_and_wait();
232  }
234 
243  void stop_and_wait()
244  {
245  queue.close();
246 
247  for (auto & t : thread_pool)
248  {
249  if (t.joinable())
250  t.join();
251  }
252  }
253 
255  std::vector<std::thread> thread_pool{};
257  contrib::fixed_buffer_queue<task_type> queue{10000};
258  };
259 
261  std::unique_ptr<internal_state> state{nullptr};
262 };
263 
264 } // namespace seqan3
Provides seqan3::buffer_queue.
The Concepts library.
T forward(T... args)
auto const move
A view that turns lvalue-references into rvalue-references.
Definition: move.hpp:74
T make_unique(T... args)
SeqAn specific customisations in the standard namespace.
Adaptations of concepts from the Ranges TS.
Provides seqan3::detail::reader_writer_manager.
Provides various type traits on generic types.