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