Raptor
A fast and space-efficient pre-filter
All Classes Namespaces Files Functions Variables Macros Pages Concepts
do_parallel.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 <algorithm>
13#include <functional>
14#include <future>
15#include <omp.h>
16#include <vector>
17
18#include <hibf/misc/divide_and_ceil.hpp>
19
20namespace raptor
21{
22
23template <typename algorithm_t>
24void do_parallel(algorithm_t && worker, size_t const num_records, size_t const threads)
25{
26 size_t const chunk_size = seqan::hibf::divide_and_ceil(num_records, threads * threads);
27 size_t const number_of_chunks = seqan::hibf::divide_and_ceil(num_records, chunk_size);
28
29#pragma omp parallel for schedule(dynamic) num_threads(threads)
30 for (size_t i = 0; i < number_of_chunks; ++i)
31 {
32 size_t const start = chunk_size * i;
33 size_t const extent = i == (number_of_chunks - 1) ? num_records - i * chunk_size : chunk_size;
34 std::invoke(worker, start, extent);
35 }
36}
37
38template <typename algorithm_t>
39void do_parallel(algorithm_t && worker, size_t const num_records, size_t const threads, bool const output_results)
40{
41 std::vector<decltype(std::async(std::launch::async, worker, size_t{}, size_t{}, bool{}))> tasks;
42 size_t const records_per_thread = num_records / threads;
43
44 for (size_t i = 0; i < threads; ++i)
45 {
46 size_t const start = records_per_thread * i;
47 size_t const extent = i == (threads - 1) ? num_records - i * records_per_thread : records_per_thread;
48 tasks.emplace_back(std::async(std::launch::async, worker, start, extent, output_results));
49 }
50
51 for (auto && task : tasks)
52 task.get();
53}
54
55} // namespace raptor
T async(T... args)
T invoke(T... args)
Hide me