FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
cpp-coding-exercise/pi-monte-carlo/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
/
pi-monte-carlo
/
main.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
61 lines (49 loc) · 1.75 KB
Breadcrumbs
cpp-coding-exercise
/
pi-monte-carlo
/
main.cpp
Copy path
File metadata and controls
61 lines (49 loc) · 1.75 KB
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#
include
<
algorithm
>
#
include
<
cstdint
>
#
include
<
iostream
>
#
include
<
numeric
>
#
include
<
random
>
#
include
<
thread
>
#
include
<
vector
>
static
std::
uint64_t
count_hits
(std::
uint64_t
n, std::
uint64_t
seed)
{
std::mt19937_64
rng
(seed);
std::uniform_real_distribution<
double
>
dist
(
0.0
,
1.0
);
std::
uint64_t
hits =
0
;
for
(std::
uint64_t
i =
0
; i < n; ++i) {
const
double
x =
dist
(rng);
const
double
y =
dist
(rng);
if
(x * x + y * y <=
1.0
)
++hits;
}
return
hits;
}
int
main
(
int
argc,
char
* argv[])
{
int
threads =
2
;
std::
uint64_t
n =
1'000'000
;
if
(argc >
1
)
threads =
std::max
(
1
,
std::atoi
(argv[
1
]));
if
(argc >
2
)
n = std::max<std::
uint64_t
>(
1
,
std::strtoull
(argv[
2
],
nullptr
,
10
));
const
std::
uint64_t
base = n / threads;
const
std::
uint64_t
rem = n % threads;
std::vector<std::thread> pool;
std::vector<std::
uint64_t
>
hits
(threads,
0
);
//
a single nondeterministic seed source; each thread gets a different seed
std::random_device rd;
const
std::
uint64_t
master_seed = (
static_cast
<std::
uint64_t
>(
rd
()) <<
32
) ^
static_cast
<std::
uint64_t
>(
rd
());
for
(
int
i =
0
; i < threads; ++i) {
const
std::
uint64_t
points = base + (
static_cast
<std::
uint64_t
>(i) < rem ?
1
:
0
);
const
std::
uint64_t
seed = master_seed +
0x9e3779b97f4a7c15ULL
*
static_cast
<std::
uint64_t
>(i +
1
);
pool.
emplace_back
([&, i, points, seed] { hits[i] =
count_hits
(points, seed); });
}
for
(
auto
& t : pool)
t.
join
();
const
std::
uint64_t
total_hits =
std::accumulate
(hits.
begin
(), hits.
end
(), std::
uint64_t
{
0
});
const
long
double
pi =
4.0
*
static_cast
<
long
double
>(total_hits) /
static_cast
<
long
double
>(n);
std::cout << pi <<
"
\n
"
;
return
0
;
}
Back
|
FazBrowse Home
|
New Git URL