-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathParallelFor01.h
More file actions
48 lines (39 loc) · 1.28 KB
/
ParallelFor01.h
File metadata and controls
48 lines (39 loc) · 1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// ===========================================================================
// ParallelFor01.h
// ===========================================================================
#pragma once
#include <algorithm>
#include <concepts>
#include <execution>
#include <ranges>
#include <thread>
#include <vector>
namespace Concurrency_ParallelFor
{
template <typename TIndex, typename TFunc>
requires std::integral<TIndex>
void parallel_for_stl(TIndex first, TIndex last, TFunc func) {
std::vector<std::size_t> indices(last - first);
std::iota(indices.begin(), indices.end(), first);
std::for_each(
std::execution::par,
indices.begin(),
indices.end(),
std::move(func)
);
}
template <typename TIndex, typename TFunc>
requires std::integral<TIndex>
void parallel_for_ranges(TIndex first, TIndex last, TFunc func) {
auto range{ std::views::iota(first, last) };
std::for_each(
std::execution::par,
range.begin(),
range.end(),
std::move(func)
);
}
}
// ===========================================================================
// End-of-File
// ===========================================================================