Raptor 3.0.1
A fast and space-efficient pre-filter for querying very large collections of nucleotide sequences
do_parallel.hpp
Go to the documentation of this file.
1// --------------------------------------------------------------------------------------------------
2// Copyright (c) 2006-2023, Knut Reinert & Freie Universität Berlin
3// Copyright (c) 2016-2023, 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/raptor/blob/main/LICENSE.md
6// --------------------------------------------------------------------------------------------------
7
13#pragma once
14
15#include <chrono>
16#include <future>
17#include <vector>
18
19namespace raptor
20{
21
22template <typename algorithm_t>
23void do_parallel(algorithm_t && worker, size_t const num_records, size_t const threads)
24{
25 std::vector<decltype(std::async(std::launch::async, worker, size_t{}, size_t{}))> tasks;
26 size_t const records_per_thread = num_records / threads;
27
28 for (size_t i = 0; i < threads; ++i)
29 {
30 size_t const start = records_per_thread * i;
31 size_t const end = i == (threads - 1) ? num_records : records_per_thread * (i + 1);
32 tasks.emplace_back(std::async(std::launch::async, worker, start, end));
33 }
34
35 for (auto && task : tasks)
36 task.wait();
37}
38
39} // namespace raptor
T async(T... args)
T end(T... args)