forked from arceos-org/arceos-apps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
77 lines (62 loc) · 1.76 KB
/
main.c
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <pthread.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#define NUM_DATA 2000000
#define NUM_TASKS 16
uint64_t array[NUM_DATA] = {0};
uint64_t my_sqrt(uint64_t n)
{
uint64_t x = n;
while (1) {
if (x * x <= n && (x + 1) * (x + 1) > n)
return x;
x = (x + n / x) / 2;
}
}
void *ThreadFunc(void *arg)
{
int id = *(int *)arg;
int left = (NUM_DATA / NUM_TASKS) * id;
int right =
(left + (NUM_DATA / NUM_TASKS)) < NUM_DATA ? (left + (NUM_DATA / NUM_TASKS)) : NUM_DATA;
char buf[512];
sprintf(buf, "part %d: [%d, %d)", id, left, right);
puts(buf);
uint64_t *partial_sum = (uint64_t *)calloc(1, sizeof(uint64_t));
for (int i = left; i < right; i++) *partial_sum += my_sqrt(array[i]);
sprintf(buf, "part %d finished", id);
puts(buf);
return (void *)(partial_sum);
}
int main()
{
for (int i = 0; i < NUM_DATA; i++) array[i] = rand();
uint64_t expect = 0;
for (int i = 0; i < NUM_DATA; i++) expect += my_sqrt(array[i]);
int thread_part[NUM_TASKS];
for (int i = 0; i < NUM_TASKS; i++) thread_part[i] = i;
pthread_t tasks[NUM_TASKS];
for (int i = 0; i < NUM_TASKS; i++) {
pthread_t t;
pthread_create(&t, NULL, ThreadFunc, (void *)(&thread_part[i]));
tasks[i] = t;
}
uint64_t *partial_sum;
uint64_t actual = 0;
for (int i = 0; i < NUM_TASKS; i++) {
pthread_join(tasks[i], (void *)(&partial_sum));
actual += *partial_sum;
}
char buf[64];
sprintf(buf, "actual sum = %lld", actual);
puts(buf);
if (actual == expect)
puts("(C)Pthread parallel run OK!");
else {
puts("(C)Pthread parallel run FAIL!");
}
return 0;
}