-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathk_nearest_thread.c
More file actions
325 lines (281 loc) · 9.88 KB
/
k_nearest_thread.c
File metadata and controls
325 lines (281 loc) · 9.88 KB
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include <math.h>
#include <sys/time.h>
#include "simpletimer.h"
#include "parse.h"
#include "vec.h"
#include "mythread.h"
data_t features[ROWS][FEATURE_LENGTH] __attribute__((aligned(32)));
data_t timer_ref_MD,timer_ref_ED,timer_ref_CS;
data_t timer_opt_MD,timer_opt_ED,timer_opt_CS;
data_t abs_diff(data_t x, data_t y){
data_t diff = x-y;
return fabs(diff);
}
data_t mult(data_t x,data_t y){
data_t m = x*y;
return m;
}
data_t manhattan_distance(data_t *x, data_t *y, int length){
data_t distance=0;
int i =0;
for(i=0;i<length;i++){
distance+=abs_diff(x[i],y[i]);
}
return distance;
}
data_t squared_eucledean_distance(data_t *x,data_t *y, int length){
data_t distance=0;
int i = 0;
for(i=0;i<length;i++){
distance+= mult(abs_diff(x[i],y[i]),abs_diff(x[i],y[i]));
}
return distance;
}
data_t norm(data_t *x, int length){
data_t n = 0;
int i=0;
for (i=0;i<length;i++){
n += mult(x[i],x[i]);
}
n = sqrt(n);
return n;
}
data_t cosine_similarity(data_t *x, data_t *y, int length){
data_t sim=0;
int i=0;
for(i=0;i<length;i++){
sim += mult(x[i],y[i]);
}
sim = sim / mult(norm(x,FEATURE_LENGTH),norm(y,FEATURE_LENGTH));
return sim;
}
data_t *ref_classify_MD(unsigned int lookFor, unsigned int *found) {
data_t *result =(data_t*)malloc(sizeof(data_t)*(ROWS-1));
struct timeval stv, etv;
int i,closest_point=0;
data_t min_distance,current_distance;
timer_start(&stv);
min_distance = manhattan_distance(features[lookFor],features[0],FEATURE_LENGTH);
result[0] = min_distance;
for(i=1;i<ROWS-1;i++){
current_distance = manhattan_distance(features[lookFor],features[i],FEATURE_LENGTH);
result[i]=current_distance;
if(current_distance<min_distance){
min_distance=current_distance;
closest_point=i;
}
}
timer_ref_MD = timer_end(stv);
printf("Calculation using reference MD took: %10.6f \n", timer_ref_MD);
*found=closest_point;
return result;
}
void *thread_manhattan_distance(void *arg)
{
int i;
parm *p=(parm *)arg;
unsigned int start_index, end_index;
data_t current_distance;
start_index=p->id * p->chunk;
end_index=start_index+p->chunk;
for(i=start_index;i<end_index;i++){
current_distance = manhattan_distance(features[p->lookFor],features[i],FEATURE_LENGTH);
p->tempResult[i]=current_distance;
if(current_distance<p->min_distance){
p->min_distance=current_distance;
p->located=i;
}
}
return (NULL);
}
//NO NEED to modify this function!
data_t *opt_classify_MD(unsigned int lookFor, unsigned int *found) {
data_t *result =(data_t*)malloc(sizeof(data_t)*(ROWS-1));
struct timeval stv, etv;
int i, closest_point=0, chunk=(ROWS-1)/NUM_THREADS;
data_t min_distance,current_distance;
pthread_t mythreads[NUM_THREADS];
parm threadParameters[NUM_THREADS];
timer_start(&stv);
min_distance = 1000000.0;
for (i=0; i<NUM_THREADS; i++)
{
threadParameters[i].id=i;
threadParameters[i].chunk=chunk;
threadParameters[i].min_distance=min_distance;
threadParameters[i].lookFor=lookFor;
threadParameters[i].located=0;
threadParameters[i].tempResult=result;
}
for (i=0; i<NUM_THREADS; i++) {
pthread_create(&mythreads[i], NULL, thread_manhattan_distance, (void *)(threadParameters+i));
}
for(i=NUM_THREADS*chunk;i<ROWS-1;i++){
current_distance = manhattan_distance(features[lookFor],features[i],FEATURE_LENGTH);
result[i]=current_distance;
if(current_distance<min_distance){
min_distance=current_distance;
closest_point=i;
}
}
for (i=0; i<NUM_THREADS; i++) {
pthread_join(mythreads[i],NULL);
}
for (i=0; i<NUM_THREADS; i++) {
if (threadParameters[i].min_distance<min_distance) {
min_distance=threadParameters[i].min_distance;
closest_point=threadParameters[i].located;
}
}
timer_opt_MD = timer_end(stv);
printf("Calculation using optimized MD took: %10.6f \n", timer_opt_MD);
*found = closest_point;
return result;
}
//Don't touch this function
data_t *ref_classify_ED(unsigned int lookFor, unsigned int *found) {
data_t *result =(data_t*)malloc(sizeof(data_t)*(ROWS-1));
struct timeval stv, etv;
int i,closest_point=0;
data_t min_distance,current_distance;
timer_start(&stv);
min_distance = squared_eucledean_distance(features[lookFor],features[0],FEATURE_LENGTH);
result[0] = min_distance;
for(i=1;i<ROWS-1;i++){
current_distance = squared_eucledean_distance(features[lookFor],features[i],FEATURE_LENGTH);
result[i]=current_distance;
if(current_distance<min_distance){
min_distance=current_distance;
closest_point=i;
}
}
timer_ref_ED = timer_end(stv);
printf("Calculation using reference ED took: %10.6f \n", timer_ref_ED);
*found = closest_point;
return result;
}
//Modify this function
data_t *opt_classify_ED(unsigned int lookFor, unsigned int *found) {
data_t *result =(data_t*)malloc(sizeof(data_t)*(ROWS-1));
struct timeval stv, etv;
int i,closest_point=0;
data_t min_distance,current_distance;
timer_start(&stv);
//FROM HERE
min_distance = squared_eucledean_distance(features[lookFor],features[0],FEATURE_LENGTH);
result[0] = min_distance;
for(i=1;i<ROWS-1;i++){
current_distance = squared_eucledean_distance(features[lookFor],features[i],FEATURE_LENGTH);
result[i]=current_distance;
if(current_distance<min_distance){
min_distance=current_distance;
closest_point=i;
}
}
//TO HERE
timer_opt_ED = timer_end(stv);
printf("Calculation using optimized ED took: %10.6f \n", timer_opt_ED);
*found = closest_point;
return result;
}
//Don't touch this function
data_t *ref_classify_CS(unsigned int lookFor, unsigned int* found) {
data_t *result =(data_t*)malloc(sizeof(data_t)*(ROWS-1));
struct timeval stv, etv;
int i,closest_point=0;
data_t min_distance,current_distance;
timer_start(&stv);
min_distance = cosine_similarity(features[lookFor],features[0],FEATURE_LENGTH);
result[0] = min_distance;
for(i=1;i<ROWS-1;i++){
current_distance = cosine_similarity(features[lookFor],features[i],FEATURE_LENGTH);
result[i]=current_distance;
if(current_distance>min_distance){
min_distance=current_distance;
closest_point=i;
}
}
timer_ref_CS = timer_end(stv);
printf("Calculation using reference CS took: %10.6f \n", timer_ref_CS);
*found = closest_point;
return result;
}
//Modify this function
data_t *opt_classify_CS(unsigned int lookFor, unsigned int *found) {
data_t *result =(data_t*)malloc(sizeof(data_t)*(ROWS-1));
struct timeval stv, etv;
int i,closest_point=0;
data_t min_distance,current_distance;
timer_start(&stv);
//MODIFY FROM HERE
min_distance = cosine_similarity(features[lookFor],features[0],FEATURE_LENGTH);
result[0] = min_distance;
for(i=1;i<ROWS-1;i++) {
current_distance = cosine_similarity(features[lookFor],features[i],FEATURE_LENGTH);
result[i]=current_distance;
if(current_distance>min_distance){
min_distance=current_distance;
closest_point=i;
}
}
//TO HERE
timer_opt_CS = timer_end(stv);
printf("Calculation using optimized CS took: %10.6f \n", timer_opt_CS);
*found = closest_point;
return result;
}
typedef data_t (*(*classifying_funct)(unsigned int lookFor, unsigned int* found));
int check_correctness(classifying_funct a, classifying_funct b, unsigned int lookFor, unsigned int *found) {
unsigned int i, a_found, b_found;
data_t *a_res = a(lookFor, &a_found);
data_t *b_res = b(lookFor, &b_found);
for(i=0;i<ROWS-1;i++){
if(a_res[i]!=b_res[i])
return 0;
}
if (a_found != b_found) return 0;
else *found=a_found;
return 1;
}
int main(int argc, char **argv){
char* dataset_name=DATASET;
int i,j;
struct timeval stv, etv;
unsigned int lookFor=ROWS-1, located;
//PARSE CSV
//holds the information regarding author and title
char metadata[ROWS][2][20];
timer_start(&stv);
parse_csv(dataset_name, features, metadata);
printf("Parsing took %9.6f s \n\n", timer_end(stv));
printf("Classifying using MD:");
printf("<Record %d, author =\"%s\", title=\"%s\">\n",lookFor,metadata[lookFor][0],metadata[lookFor][1]);
if(check_correctness(ref_classify_MD,opt_classify_MD, lookFor, &located)){
printf("opt_classify_MD is correct, speedup: %10.6f\n\n",timer_ref_MD/timer_opt_MD);
}
else
printf("opt_classify_MD is incorrect! \n"); // , speedup: %10.6f\n\n",timer_ref_MD/timer_opt_MD);
printf("Best match: ");
printf("<Record %d, author =\"%s\", title=\"%s\">\n\n",located,metadata[located][0],metadata[located][1]);
printf("Classifying using ED:");
printf("<Record %d, author =\"%s\", title=\"%s\">\n",lookFor,metadata[lookFor][0],metadata[lookFor][1]);
if(check_correctness(ref_classify_ED,opt_classify_ED, lookFor, &located)) {
printf("opt_classify_ED is correct, speedup: %10.6f\n\n",timer_ref_ED/timer_opt_ED);
}
else
printf("opt_classify_ED id incorrect!\n\n");
printf("Best match: ");
printf("<Record %d, author =\"%s\", title=\"%s\">\n\n",located,metadata[located][0],metadata[located][1]);
printf("Classifying using CS (cosine similarity):");
printf("<Record %d, author =\"%s\", title=\"%s\">\n",lookFor,metadata[lookFor][0],metadata[lookFor][1]);
if(check_correctness(ref_classify_CS,opt_classify_CS, lookFor, &located)) {
printf("opt_classify_CS is correct, speedup: %10.6f\n\n",timer_ref_CS/timer_opt_CS);
}
else
printf("opt_classify_CS id incorrect!\n\n");
printf("Best match: ");
printf("<Record %d, author =\"%s\", title=\"%s\">\n\n",located,metadata[located][0],metadata[located][1]);
}