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