-
Notifications
You must be signed in to change notification settings - Fork 0
/
pthread.c
90 lines (76 loc) · 2.2 KB
/
pthread.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
78
79
80
81
82
83
84
85
86
87
88
89
90
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <sys/time.h>
#define SIZE 1000000000
int *a, num_threads;
unsigned long long *private_sum;
void *solver (void *param)
{
int i, id = *(int*)(param), j;
private_sum[id] = 0;
if (num_threads == 2) {
for (i=SIZE/2; i<SIZE; i++) {
private_sum[id] += a[i];
}
}
else {
j = (SIZE/num_threads)*(id+1);
for (i=(SIZE/num_threads)*id; i<j; i++) {
private_sum[id] += a[i];
}
}
}
int main (int argc, char *argv[])
{
int i, j, *tid;
pthread_t *threads;
pthread_attr_t attr;
struct timeval tp_start, tp_end;
unsigned long long sum=0;
if (argc != 2) {
printf ("Need number of threads.\n");
exit(1);
}
num_threads = atoi(argv[1]);
threads = (pthread_t*)malloc(num_threads*sizeof(pthread_t));
private_sum = (unsigned long long*)malloc(num_threads*sizeof(unsigned long long));
tid = (int*)malloc(num_threads*sizeof(int));
for (i=0; i<num_threads; i++) tid[i] = i;
a = (int*)malloc(SIZE*sizeof(int));
for (i=0; i<SIZE; i++) a[i] = i;
pthread_attr_init(&attr);
gettimeofday(&tp_start, NULL);
for (i=1; i<num_threads; i++) {
/* pthread_create arguments: thread pointer,
attribute pointer,
function pointer,
argument pointer to the function
*/
pthread_create(&threads[i], &attr, solver, &tid[i]);
}
if (num_threads == 1) {
for (i=0; i<SIZE; i++) {
sum += a[i];
}
}
else if (num_threads == 2) {
for (i=0; i<SIZE/2; i++) {
sum += a[i];
}
}
else {
j=SIZE/num_threads;
for (i=0; i<j; i++) {
sum += a[i];
}
}
for (i=1; i<num_threads; i++) {
pthread_join(threads[i], NULL);
sum += private_sum[i];
}
printf("SUM: %llu\n", sum);
gettimeofday(&tp_end, NULL);
printf("Total time: %lld microseconds\n", tp_end.tv_sec*1000000+tp_end.tv_usec - (tp_start.tv_sec*1000000+tp_start.tv_usec));
return 0;
}