PatchworkOS
28a9544
A non-POSIX operating system.
Theme:
Default
Round
Robot
Loading...
Searching...
No Matches
main.c
Go to the documentation of this file.
1
#include <
stdatomic.h
>
2
#include <
stdbool.h
>
3
#include <
stdint.h
>
4
#include <
stdio.h
>
5
#include <
threads.h
>
6
#include <
time.h
>
7
8
#define PRIME_MAX (10000000)
9
10
static
atomic_long
count
;
11
static
atomic_long
next
;
12
13
bool
is_prime
(
uint64_t
n)
14
{
15
if
(n <= 1)
16
{
17
return
false
;
18
}
19
if
(n <= 3)
20
{
21
return
true
;
22
}
23
if
(n % 2 == 0 || n % 3 == 0)
24
{
25
return
false
;
26
}
27
28
for
(
uint64_t
i = 5; i * i <= n; i += 6)
29
{
30
if
(n % i == 0 || n % (i + 2) == 0)
31
{
32
return
false
;
33
}
34
}
35
36
return
true
;
37
}
38
39
static
void
count_primes
(
uint64_t
start
,
uint64_t
end)
40
{
41
for
(
uint64_t
i =
start
; i < end; i++)
42
{
43
if
(
is_prime
(i))
44
{
45
atomic_fetch_add
(&
count
, 1);
46
}
47
}
48
}
49
50
static
int
thread_entry
(
void
* arg)
51
{
52
(void)arg;
53
54
uint64_t
start
;
55
while
((
start
=
atomic_fetch_add
(&
next
, 1000)) <
PRIME_MAX
)
56
{
57
uint64_t
end =
start
+ 1000;
58
if
(end >
PRIME_MAX
)
59
{
60
end =
PRIME_MAX
;
61
}
62
63
count_primes
(
start
, end);
64
}
65
return
thrd_success
;
66
}
67
68
static
void
benchmark
(
uint64_t
threadAmount)
69
{
70
printf
(
"%d threads: starting..."
, threadAmount);
71
fflush
(
stdout
);
72
clock_t
start
=
clock
();
73
74
atomic_init
(&
count
, 0);
75
atomic_init
(&
next
, 0);
76
77
thrd_t
threads
[threadAmount];
78
for
(
uint64_t
i = 0; i < threadAmount; i++)
79
{
80
if
(
thrd_create
(&
threads
[i],
thread_entry
,
NULL
) !=
thrd_success
)
81
{
82
printf
(
" (thrd_create error %d) "
, i);
83
fflush
(
stdout
);
84
}
85
}
86
87
for
(
uint64_t
i = 0; i < threadAmount; i++)
88
{
89
if
(
thrd_join
(
threads
[i],
NULL
) !=
thrd_success
)
90
{
91
printf
(
" (thrd_join error %d) "
, i);
92
fflush
(
stdout
);
93
}
94
}
95
96
clock_t
end =
clock
();
97
printf
(
"\ttook %d ms to find %d primes\n"
, (end -
start
) / (
CLOCKS_PER_SEC
/ 1000),
atomic_load
(&
count
));
98
}
99
100
int
main
(
void
)
101
{
102
for
(
uint64_t
threads
= 1;
threads
<= 1024;
threads
*= 2)
103
{
104
benchmark
(
threads
);
105
}
106
107
printf
(
"Testing complete.\n"
);
108
return
0;
109
}
CLOCKS_PER_SEC
#define CLOCKS_PER_SEC
Definition
clock_t.h:15
NULL
#define NULL
Pointer error value.
Definition
NULL.h:23
clock_t
__UINT64_TYPE__ clock_t
A nanosecond time.
Definition
clock_t.h:13
threads
static list_t threads
Definition
thread.c:8
main
int main(void)
Definition
main.c:5
start
static void start()
Definition
main.c:542
next
static atomic_long next
Definition
main.c:11
is_prime
bool is_prime(uint64_t n)
Definition
main.c:13
benchmark
static void benchmark(uint64_t threadAmount)
Definition
main.c:68
PRIME_MAX
#define PRIME_MAX
Definition
main.c:8
count_primes
static void count_primes(uint64_t start, uint64_t end)
Definition
main.c:39
thread_entry
static int thread_entry(void *arg)
Definition
main.c:50
count
static atomic_long count
Definition
main.c:10
stdatomic.h
atomic_load
#define atomic_load(object)
Definition
stdatomic.h:288
atomic_fetch_add
#define atomic_fetch_add(object, operand)
Definition
stdatomic.h:283
atomic_init
#define atomic_init(obj, value)
Definition
stdatomic.h:75
stdbool.h
stdint.h
uint64_t
__UINT64_TYPE__ uint64_t
Definition
stdint.h:17
stdio.h
fflush
_PUBLIC int fflush(FILE *stream)
Definition
fflush.c:6
printf
_PUBLIC int printf(const char *_RESTRICT format,...)
Definition
printf.c:5
stdout
FILE * stdout
Definition
std_streams.c:17
thrd_t
Definition
threads.h:37
threads.h
thrd_create
_PUBLIC int thrd_create(thrd_t *thr, thrd_start_t func, void *arg)
Definition
thrd_create.c:30
thrd_join
_PUBLIC int thrd_join(thrd_t thr, int *res)
Definition
thrd_join.c:10
thrd_success
@ thrd_success
Definition
threads.h:74
time.h
clock
_PUBLIC clock_t clock(void)
Definition
clock.c:5
src
programs
utils
threadtest
main.c
Generated by
1.9.8