Visual Computing Library
Loading...
Searching...
No Matches
parallel.h
1/*****************************************************************************
2 * VCLib *
3 * Visual Computing Library *
4 * *
5 * Copyright(C) 2021-2025 *
6 * Visual Computing Lab *
7 * ISTI - Italian National Research Council *
8 * *
9 * All rights reserved. *
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the Mozilla Public License Version 2.0 as published *
13 * by the Mozilla Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 * This program is distributed in the hope that it will be useful, *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
19 * Mozilla Public License Version 2.0 *
20 * (https://www.mozilla.org/en-US/MPL/2.0/) for more details. *
21 ****************************************************************************/
22
23#ifndef VCL_MISC_PARALLEL_H
24#define VCL_MISC_PARALLEL_H
25
26#include <vclib/concepts/range.h>
27#include <vclib/types.h>
28
29// tbb and qt conflicts: if both are linked, we need to first undef Qt's
30// emit - see: https://github.com/oneapi-src/oneTBB/issues/547
31#if defined(emit)
32#undef emit
33#define VCLIB_EMIT_REDEFINED
34#endif // emit
35
36// Hack to compensate lack of support for c++17 parallel algorithms by
37// several compilers. We use poolSTL.
38#define POOLSTL_STD_SUPPLEMENT
39#if __has_include(<poolstl/poolstl.hpp>)
40#include <poolstl/poolstl.hpp>
41#else
42#include "../../../external/poolSTL-0.3.5/include/poolstl/poolstl.hpp"
43#endif
44
45// Restore the definition of "emit" if it was defined before
46#ifdef VCLIB_EMIT_REDEFINED
47#undef VCLIB_EMIT_REDEFINED
48#define emit // restore the macro definition of "emit", as it was
49 // defined in gtmetamacros.h
50#endif // VCLIB_EMIT_REDEFINED
51
52#include <algorithm>
53
54namespace vcl {
55
74template<typename Iterator, typename Lambda>
75void parallelFor(Iterator&& begin, Iterator&& end, Lambda&& F)
76{
77 std::for_each(std::execution::par, begin, end, F);
78}
79
98template<typename Iterator, typename Lambda>
99void parallelFor(const Iterator& begin, const Iterator& end, Lambda&& F)
100{
101 std::for_each(std::execution::par, begin, end, F);
102}
103
119template<Range Rng, typename Lambda>
120void parallelFor(Rng&& r, Lambda&& F)
121{
122 parallelFor(std::ranges::begin(r), std::ranges::end(r), F);
123}
124
140template<Range Rng, typename Lambda>
141void parallelFor(const Rng& r, Lambda&& F)
142{
143 parallelFor(std::ranges::begin(r), std::ranges::end(r), F);
144}
145
146} // namespace vcl
147
148#endif // VCL_MISC_PARALLEL_H