FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
cpp-coding-exercise/parallel-transform/main.cpp at main · DoctorLai/cpp-coding-exercise · GitHub
DoctorLai
/
cpp-coding-exercise
Public
Uh oh!
There was an error while loading.
Please reload this page
.
Notifications
You must be signed in to change notification settings
Fork
3
Star
3
Code
Issues
0
Pull requests
1
Discussions
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Discussions
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
cpp-coding-exercise
/
parallel-transform
/
main.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
30 lines (27 loc) · 1021 Bytes
Breadcrumbs
cpp-coding-exercise
/
parallel-transform
/
main.cpp
Copy path
File metadata and controls
30 lines (27 loc) · 1021 Bytes
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
/*
*
This shows using parallel transform_reduce from C++17
to perform a fold (reduce) operation on a range of values. e.g. sum up 100 values in parallel
*/
#
include
<
vector
>
#
include
<
numeric
>
//
std::transform_reduce
#
include
<
functional
>
//
std::plus
#
include
<
iostream
>
#
include
<
execution
>
//
std::execution::par
int
main
()
{
std::vector<
int
>
numbers
(
100
);
//
Initialize the vector with values 1 to 100
std::iota
(numbers.
begin
(), numbers.
end
(),
1
);
//
Use parallel transform_reduce to sum the values in parallel
int
sum =
std::transform_reduce
(
std::execution::par,
//
parallel execution policy parallel or std::execution::seq for sequential
numbers.
begin
(),
numbers.
end
(),
0
,
//
initial value
std::plus<
int
>{},
//
binary operation to combine results
[](
int
v) {
return
v; }
//
unary operation to transform each element
);
std::cout <<
"
Sum of numbers from 1 to 100:
"
<< sum <<
'
\n
'
;
return
0
;
}
Back
|
FazBrowse Home
|
New Git URL