Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F97421861
pi_slow_critical.cc
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Subscribers
None
File Metadata
Details
File Info
Storage
Attached
Created
Sat, Jan 4, 09:40
Size
1 KB
Mime Type
text/x-c
Expires
Mon, Jan 6, 09:40 (1 d, 23 h)
Engine
blob
Format
Raw Data
Handle
23312672
Attached To
R7871 phys-743-exercises
pi_slow_critical.cc
View Options
/*
This exercise is taken from the class Parallel Programming Workshop (MPI,
OpenMP and Advanced Topics) at HLRS given by Rolf Rabenseifner
*/
#include <chrono>
#include <cstdio>
#include <cmath>
#include <omp.h>
using
clk
=
std
::
chrono
::
high_resolution_clock
;
using
second
=
std
::
chrono
::
duration
<
double
>
;
using
time_point
=
std
::
chrono
::
time_point
<
clk
>
;
inline
int
digit
(
double
x
,
int
n
)
{
return
std
::
trunc
(
x
*
std
::
pow
(
10.
,
n
))
-
std
::
trunc
(
x
*
std
::
pow
(
10.
,
n
-
1
))
*
10.
;
}
inline
double
f
(
double
a
)
{
return
(
4.
/
(
1.
+
a
*
a
));
}
const
int
n
=
10000000
;
int
main
(
int
/* argc */
,
char
**
/* argv */
)
{
int
i
;
double
dx
,
x
,
sum
,
pi
;
auto
nthreads
=
omp_get_max_threads
();
auto
t1
=
clk
::
now
();
/* calculate pi = integral [0..1] 4 / (1 + x**2) dx */
dx
=
1.
/
n
;
sum
=
0.0
;
#pragma omp parallel for shared(sum) private(x)
for
(
i
=
0
;
i
<
n
;
i
++
)
{
x
=
1.
*
i
*
dx
;
#pragma omp critical
sum
=
sum
+
f
(
x
);
}
pi
=
dx
*
sum
;
second
elapsed
=
clk
::
now
()
-
t1
;
std
::
printf
(
"computed pi = %.16g
\n
"
,
pi
);
std
::
printf
(
"wall clock time (chrono) = %.4gs
\n
"
,
elapsed
.
count
());
std
::
printf
(
"using n threads = %.d
\n
"
,
nthreads
);
for
(
int
d
=
1
;
d
<=
15
;
++
d
)
{
std
::
printf
(
"%d"
,
digit
(
pi
,
d
));
}
return
0
;
}
Event Timeline
Log In to Comment