Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
cpp_parallelism
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Erik Strand
cpp_parallelism
Commits
d84e8f2e
Commit
d84e8f2e
authored
6 years ago
by
Erik Strand
Browse files
Options
Downloads
Patches
Plain Diff
Add pi calculation using manually spawned threads
parent
af6fa97b
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
Makefile
+1
-1
1 addition, 1 deletion
Makefile
pi_threads.cpp
+50
-0
50 additions, 0 deletions
pi_threads.cpp
with
51 additions
and
1 deletion
Makefile
+
1
−
1
View file @
d84e8f2e
# This Makefile compiles and flashes four different programs:
CPPFLAGS
=
-Wall
-O3
-pthread
-std
=
c++11
CPPFLAGS
=
-Wall
-O3
-pthread
-std
=
c++11
-ffast-math
%
:
%.cpp
g++
$(
CPPFLAGS
)
-o
build/
$@
$<
...
...
This diff is collapsed.
Click to expand it.
pi_threads.cpp
0 → 100644
+
50
−
0
View file @
d84e8f2e
#include
<array>
#include
<chrono>
#include
<iostream>
#include
<numeric>
#include
<thread>
int
main
()
{
constexpr
double
a
=
0.5
;
constexpr
double
b
=
0.75
;
constexpr
double
c
=
0.25
;
// n_threads * n_iterations_per_thread + 1 must fit in a uint32_t
constexpr
uint32_t
n_threads
=
4u
;
constexpr
uint32_t
n_iterations_per_thread
=
100000000u
;
std
::
array
<
std
::
thread
,
n_threads
>
threads
;
std
::
array
<
double
,
n_threads
>
thread_results
;
const
auto
pi_sum
=
[
&
](
uint32_t
index
)
{
double
&
value
=
thread_results
[
index
];
value
=
0
;
const
uint32_t
i_begin
=
n_iterations_per_thread
*
index
+
1
;
const
uint32_t
i_end
=
n_iterations_per_thread
*
(
index
+
1
);
for
(
uint32_t
i
=
i_begin
;
i
<=
i_end
;
++
i
)
{
value
+=
a
/
((
i
-
b
)
*
(
i
-
c
));
}
};
const
auto
start_time
=
std
::
chrono
::
steady_clock
::
now
();
for
(
auto
i
=
0u
;
i
<
n_threads
;
++
i
)
{
threads
[
i
]
=
std
::
thread
(
pi_sum
,
i
);
}
for
(
auto
i
=
0u
;
i
<
n_threads
;
++
i
)
{
threads
[
i
].
join
();
}
const
auto
end_time
=
std
::
chrono
::
steady_clock
::
now
();
double
pi
=
std
::
accumulate
(
thread_results
.
begin
(),
thread_results
.
end
(),
double
(
0
));
std
::
cout
<<
"Final value: "
<<
pi
<<
'\n'
;
const
auto
microseconds
=
std
::
chrono
::
duration_cast
<
std
::
chrono
::
microseconds
>
(
end_time
-
start_time
).
count
();
std
::
cout
<<
"Time (microseconds): "
<<
microseconds
<<
'\n'
;
const
auto
mega_flops
=
n_threads
*
n_iterations_per_thread
*
5.0
/
microseconds
;
std
::
cout
<<
"MFLOPS: "
<<
mega_flops
<<
'\n'
;
return
0
;
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment