-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheuclidean_distance.c
141 lines (89 loc) · 2.62 KB
/
euclidean_distance.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// Prevajanje programa:
// gcc -O2 -lm -l pthread -lgomp -w -o euclidean_distance euclidean_distance.c
// Zagon programa:
// srun -n1 --reservation=fri --cpus-per-task=8 ./euclidean_distance
#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
#include <math.h>
#include <omp.h>
#include <stdlib.h>
#define THREADS 8
#define D 100000000
pthread_t thread[THREADS];
pthread_barrier_t barrier;
int level = THREADS;
int modul = 2;
double *a;
double *b;
double result[THREADS];
void *kvadrati(void *arg) {
int rank = (int)arg;
int start = (int)(D / THREADS * rank);
int stop = (int)(D / THREADS * (rank + 1));
double d = 0;
for(int i = start; i < stop; i++) {
d += (double)pow((a[i] - b[i]), 2);
}
result[rank] = d;
// navadno seštejemo vse d po drevesni strukturi (barrier!!!)
pthread_barrier_wait(&barrier);
while(level > 2) {
if (rank % modul == 0 && (rank + modul / 2) < THREADS){
result[rank] = result[rank] + result[rank + (modul/2)];
if((rank + modul / 2) == THREADS - 1) {
result[(rank + modul / 2)] = 0;
}
}
pthread_barrier_wait(&barrier);
if(rank == 0) {
level /= 2;
modul *= 2;
}
pthread_barrier_wait(&barrier);
}
if (rank == 0) {
if(result[THREADS - 1] == 0) {
if(ceil(log2(THREADS)) == floor(log2(THREADS))){
result[rank] = result[rank] + result[(THREADS / 2)];
} else {
result[rank] = result[rank] + result[(THREADS / 2) + 1];
}
} else {
result[rank] = result[rank] + result[(THREADS / 2)] + result[THREADS - 1];
}
}
return NULL;
}
int main() {
// generiranje vetorjev
a = malloc(D * sizeof(double));
b = malloc(D * sizeof(double));
for (int i = 0; i < D; i++) {
a[i] = rand() / ((double)RAND_MAX);
b[i] = rand() / ((double)RAND_MAX);
}
double start = omp_get_wtime();
pthread_barrier_init(&barrier, NULL, THREADS);
for(int i = 0; i < THREADS; i++) {
pthread_create(&thread[i], NULL, kvadrati, (void *)i);
}
for(int i = 0; i < THREADS; i++) {
pthread_join(thread[i], NULL);
}
double end = omp_get_wtime();
printf("Rezultat = %f\n", result[0]);
printf("Čas = %f seconds | Velikost = %d | Niti = %d\n", end - start, D, THREADS);
free(a);
free(b);
return 0;
}
/*
Rezultat naloge:
ŠTEVILO Čas
NITI
1 0.228282 seconds
2 0.238046 seconds
4 0.134414 seconds
8 0.108033 seconds
*/