-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathc_sim.c
More file actions
499 lines (388 loc) · 11.2 KB
/
c_sim.c
File metadata and controls
499 lines (388 loc) · 11.2 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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
#define UINT8
#define SSE
#include "c_sim.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <math.h>
#include <omp.h>
#include <xmmintrin.h>
#include <pmmintrin.h>
#include <smmintrin.h>
// Each array == "3072 entry long - 3 * 32 * 32"
#define ARRAY_LENGTH 3072
// Similarity div == 255.0 * 1024.0 * 3.0 - Max value * element in array * rgb (3)
#define SIMILARITY_DIV 783360.0
#define SIMILARITY_THRESHOLD 0.98
// Global vars
//append_to_list()
struct node* head = NULL;
struct node* last = NULL;
int list_length = 0;
//c_get_similarity_next()
struct node* cur = NULL;
int done = 0;
// Rest of the app
const double** array;
const char** path;
int length;
// Floats part of the app
const float** findex;
float* farray;
// uint8 part of the app
const uint8_t** uindex;
uint8_t* uarray;
//##############################################################################
// Simple Append only linked list
//##############################################################################
int append_to_list(double fp, int patha, int pathb) {
// allocate the new struct
struct node* newnode = malloc(sizeof(struct node));
if(newnode == NULL) {
printf("Out of memory\n");
}
// set the data
newnode->fp = fp;
newnode->patha = patha;
newnode->pathb = pathb;
newnode->next = NULL;
// Check to see if list is empty
if(head == NULL) {
head = newnode;
last = newnode;
list_length += 1;
return 0;
}
// List is not empty
last->next = newnode;
last = last->next;
list_length += 1;
return 0;
}
int free_list() {
if(head == NULL) {
return 0;
}
struct node* prev = head;
struct node* next = head->next;
int count = 0;
while(prev != NULL) {
free(prev);
count += 1;
prev = next;
if(prev != NULL) {
next = prev->next;
}
}
printf("freed: %d\n", count);
head = NULL;
last = NULL;
list_length = 0;
return 0;
}
//##############################################################################
// Return the values out of the linked list
//##############################################################################
int c_get_similarity_next(struct similar* a) {
if(head == NULL) {
return 0;
} else if(done) {
return 0;
} else if(cur == NULL) {
cur = head;
}
// Fill the struct
a->fp = cur->fp;
a->patha = path[cur->patha];
a->pathb = path[cur->pathb];
struct node* next = cur->next;
if(next != NULL) {
cur = next;
} else {
done = 1;
}
return 1;
}
//##############################################################################
// Setup and cleanup code
//##############################################################################
void c_setup( int N ) {
printf( "c_setup: %d\n", N);
length = N;
array = calloc(N, sizeof(*array));
path = calloc(N, sizeof(*path));
if((array == NULL) || (path == NULL)) {
printf("Out of memory\n");
}
}
void c_add( int idx, const double a[], const char* b ) {
if((idx >= 0) && (idx < length)) {
array[idx] = &a[0];
path[idx] = b;
}
}
void c_teardown() {
printf( "c_teardown\n" );
free(array);
free(path);
array = NULL;
path = NULL;
free_list();
}
//##############################################################################
// Double -> Floats here
//##############################################################################
void c_double_to_float() {
printf( "c_double_to_float:\n");
// The address of a block returned by malloc or realloc in the GNU system
// is always a multiple of eight (or sixteen on 64-bit systems).
findex = calloc(length, sizeof(*findex));
farray = calloc((length * ARRAY_LENGTH), sizeof(*farray));
if((findex == NULL) || (farray == NULL)) {
printf("Out of memory\n");
}
// Start the conversion here
int i, k;
for(i = 0; i < length; i++) {
const double* sim = array[i];
// File away this pointer as a index into the index array
findex[i] = &farray[(i * ARRAY_LENGTH)];
// Populate the farray with values
for(k = 0; k < ARRAY_LENGTH; k++) {
farray[((i * ARRAY_LENGTH) + k)] = (float)sim[k];
}
}
}
void c_teardown_floats() {
printf( "c_teardown_floats\n" );
free(findex);
free(farray);
findex = NULL;
farray = NULL;
}
//##############################################################################
// Double -> uint8 integers
//##############################################################################
void c_double_to_uint8() {
printf( "c_double_to_uint8:\n");
// The address of a block returned by malloc or realloc in the GNU system
// is always a multiple of eight (or sixteen on 64-bit systems).
uindex = calloc(length, sizeof(*uindex));
uarray = calloc((length * ARRAY_LENGTH), sizeof(*uarray));
if((uindex == NULL) || (uarray == NULL)) {
printf("Out of memory\n");
}
// Start the conversion here
int i, k;
for(i = 0; i < length; i++) {
const double* sim = array[i];
// File away this pointer as a index into the index array
uindex[i] = &uarray[(i * ARRAY_LENGTH)];
// Populate the farray with values
for(k = 0; k < ARRAY_LENGTH; k++) {
uarray[((i * ARRAY_LENGTH) + k)] = (uint8_t)sim[k];
}
}
}
void c_teardown_uint8() {
printf( "c_teardown_uint8\n" );
free(uindex);
free(uarray);
uindex = NULL;
uarray = NULL;
}
//##############################################################################
// Inlined abs
//##############################################################################
inline __m128 abs_ps(__m128 x) {
const __m128 sign_mask = _mm_set1_ps(-0.0f); // -0.0f = 1 << 31
return _mm_andnot_ps(sign_mask, x);
}
inline __m128d abs_pd(__m128d x) {
const __m128d sign_mask = _mm_set1_pd(-0.0); // -0.0 = 1 << 63
return _mm_andnot_pd(sign_mask, x); // !sign_mask & x
}
//##############################################################################
// Setup and cleanup code
//##############################################################################
void c_process() {
printf( "c_process\n");
int i, j, k;
int dup = 0;
#ifdef DOUBLE
#ifndef SSE
#pragma omp parallel for shared(dup) private(j, i, k) schedule (dynamic)
for(i = 0; i < length; i++) {
for(j = (i + 1); j < length; j++) {
const double* sima = array[i];
const double* simb = array[j];
double sum = 0.0;
for(k = 0; k < ARRAY_LENGTH; k++) {
sum += fabs(sima[k] - simb[k]);
}
double fp = (1.0 - (sum / SIMILARITY_DIV));
if(fp >= SIMILARITY_THRESHOLD) {
#pragma omp critical
{
dup += 1;
append_to_list(fp, i, j);
}
}
}
}
#else
#pragma omp parallel for shared(dup) private(j, i, k) schedule (dynamic)
for(i = 0; i < length; i++) {
for(j = (i + 1); j < length; j++) {
const __m128d* sima = (__m128d*) array[i];
const __m128d* simb = (__m128d*) array[j];
// Init sums
__m128d sum1 = _mm_setzero_pd();
__m128d sum2 = _mm_setzero_pd();
for(k = 0; k < ARRAY_LENGTH/2; k += 2) {
sum1 += abs_pd(_mm_sub_pd(sima[k], simb[k]));
sum2 += abs_pd(_mm_sub_pd(sima[k+1], simb[k+1]));
}
__m128d sum = _mm_add_pd(sum1, sum2);
// Accumulate the partial sums into one
sum = _mm_hadd_pd(sum, _mm_setzero_pd());
// calc vsum = vsum / vdiv
sum = _mm_div_sd(sum, _mm_set1_pd(SIMILARITY_DIV));
// calc vsum = 1.0 - vsum
sum = _mm_sub_pd(_mm_set1_pd(1.0), sum);
// Unload vsum -> fp
double fp[2];
_mm_store_sd(&fp[0], sum);
if(fp[0] >= SIMILARITY_THRESHOLD) {
#pragma omp critical
{
dup += 1;
append_to_list(fp[0], i, j);
}
}
}
}
#endif
#endif
#ifdef FLOAT
// Convert the doubles to float
c_double_to_float();
#ifndef SSE
#pragma omp parallel for shared(dup) private(j, i, k) schedule (dynamic)
for(i = 0; i < length; i++) {
for(j = (i + 1); j < length; j++) {
const float* sima = findex[i];
const float* simb = findex[j];
float sum = 0.0;
for(k = 0; k < ARRAY_LENGTH; k++) {
sum += fabs(sima[k] - simb[k]);
}
float fp = (1.0 - (sum / SIMILARITY_DIV));
if(fp >= SIMILARITY_THRESHOLD) {
#pragma omp critical
{
dup += 1;
append_to_list((double)fp, i, j);
}
}
}
}
#else
#pragma omp parallel for shared(dup) private(j, i, k) schedule (dynamic)
for(i = 0; i < length; i++) {
for(j = (i + 1); j < length; j++) {
const __m128* sima = (__m128*) findex[i];
const __m128* simb = (__m128*) findex[j];
// Init sums
__m128 sum1 = _mm_setzero_ps();
__m128 sum2 = _mm_setzero_ps();
for(k = 0; k < ARRAY_LENGTH/4; k += 2) {
sum1 += abs_ps(_mm_sub_ps(sima[k], simb[k]));
sum2 += abs_ps(_mm_sub_ps(sima[k+1], simb[k+1]));
}
__m128 sum = _mm_add_ps(sum1, sum2);
// Accumulate the partial sums into one
sum = _mm_hadd_ps(sum, _mm_setzero_ps());
sum = _mm_hadd_ps(sum, _mm_setzero_ps());
// calc vsum = vsum / vdiv
sum = _mm_div_ps(sum, _mm_set1_ps(SIMILARITY_DIV));
// calc vsum = 1.0 - vsum
sum = _mm_sub_ps(_mm_set1_ps(1.0), sum);
// unload vsum -> fp
float fp[4];
_mm_store_ps(&fp[0], sum);
if(fp[0] >= SIMILARITY_THRESHOLD) {
#pragma omp critical
{
dup += 1;
append_to_list((double)fp[0], i, j);
}
}
}
}
#endif
// Clean up
c_teardown_floats();
#endif
#ifdef UINT8
// Convert the doubles to uint8
c_double_to_uint8();
#ifndef SSE
#pragma omp parallel for shared(dup) private(j, i, k) schedule (dynamic)
for(i = 0; i < length; i++) {
for(j = (i + 1); j < length; j++) {
const uint8_t* sima = uindex[i];
const uint8_t* simb = uindex[j];
int sum = 0;
for(k = 0; k < ARRAY_LENGTH; k++) {
sum += abs(sima[k] - simb[k]);
}
float fp = (1.0 - ((float)sum / SIMILARITY_DIV));
if(fp >= SIMILARITY_THRESHOLD) {
#pragma omp critical
{
dup += 1;
append_to_list((double)fp, i, j);
}
}
}
}
#else
#pragma omp parallel for shared(dup) private(j, i, k) schedule (dynamic)
for(i = 0; i < length; i++) {
for(j = (i + 1); j < length; j++) {
const __m128i* sima = (__m128i*) uindex[i];
const __m128i* simb = (__m128i*) uindex[j];
// Init sum
__m128i sum = _mm_setzero_si128();
for(k = 0; k < ARRAY_LENGTH/16; k += 1) {
sum = _mm_add_epi32(sum, _mm_sad_epu8(sima[k], simb[k]));
}
// Accumulate the partial sums into one
// 0, int32 | 0, int32
sum = _mm_hadd_epi32(sum, _mm_setzero_si128());
sum = _mm_hadd_epi32(sum, _mm_setzero_si128());
// Convert the signed int32 to float
__m128 fsum = _mm_cvtepi32_ps(sum);
// calc fvsum = fvsum / vdiv
fsum = _mm_div_ps(fsum, _mm_set1_ps(SIMILARITY_DIV));
// calc fvsum = 1.0 - fvsum
fsum = _mm_sub_ps(_mm_set1_ps(1.0), fsum);
// unload fsum -> fp
float fp[4];
_mm_store_ps(&fp[0], fsum);
if(fp[0] >= SIMILARITY_THRESHOLD) {
#pragma omp critical
{
dup += 1;
append_to_list((double)fp[0], i, j);
}
}
}
}
#endif
// Clean up
c_teardown_uint8();
#endif
printf("dup: %d - list-length: %d\n", dup, list_length );
}