-
Notifications
You must be signed in to change notification settings - Fork 1
/
V0.c
158 lines (124 loc) · 3.83 KB
/
V0.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include "cblas.h"
#include <math.h>
typedef struct knnresult
{
int *nidx; /* Indices (0-based) of nearest neighbors [m-by-k] */
double *ndist; /* Distance of nearest neighbors [m-by-k] */
int m; /* Number of query points [scalar] */
int k; /* Number of nearest neighbors [scalar] */
} knnresult;
/* Returns the location to insert element in list */
int binary_Search(double *a, double item, int low, int high)
{
if (high <= low)
return (item > a[low]) ? (low + 1) : low;
int mid = (low + high) / 2;
if(item == a[mid])
return mid+1;
if(item > a[mid])
return binary_Search(a, item, mid+1, high);
else
return binary_Search(a, item, low, mid-1);
}
/* Binary insertion sort */
void insertion_Sort(double *a, int *b, int size)
{
int i, loc, j, k;
double selected;
for (i = 0; i < size; i++)
{
j = i - 1;
selected = a[i];
loc = binary_Search(a, selected, 0, j);
while (j >= loc)
{
a[j+1] = a[j];
b[j+1] = b[j];
j--;
}
a[j+1] = selected;
b[j+1] = i;
}
}
knnresult kNN(double *X, double *Y, int n, int m, int d, int k){
/* int *TMP;
int tmp;
TMP = X;
X = Y; <---- USE THIS IF YOU WANT TO FIND K-NN OF Y ARRAY, WORKS FOR X BY DEFAULT
Y = TMP;
tmp = n;
n = m;
m = tmp; */
knnresult knn;
knn.ndist = malloc(k * m * sizeof(double));
knn.nidx = malloc(k * m * sizeof(int));
knn.m = m;
knn.k = k;
double *ndist = knn.ndist;
int *nidx = knn.nidx;
double pos, elemD;
int loc, cnt;
double *D = (double *)malloc(m * n * sizeof(double));
double sumx[ n ],
sumy[ m ];
for(int i = 0; i < n; i++)
sumx[i] = cblas_ddot(d, X+i*d, 1, X+i*d, 1);
for(int i = 0; i < m; i++)
sumy[i] = cblas_ddot(d, Y+i*d, 1, Y+i*d, 1);
cblas_dgemm(CblasRowMajor, CblasNoTrans, CblasTrans, n, m, d, -2, X, d, Y, d, 0, D, m);
for(int i = 0; i < n; i++)
{
cnt = 0;
for(int j = 0; j < m; j++)
{
D[j+i*m] += sumx[i] + sumy[j];
elemD = sqrt(fabs( D[j+i*m] ));
pos = j;
if(cnt < k)
{
ndist[i*k+cnt] = elemD;
nidx [i*k+cnt] = pos;
if(cnt == k - 1) insertion_Sort(ndist+i*k, nidx+i*k, k);
cnt++;
}
else
{
loc = binary_Search(ndist+i*k, elemD, 0, k);
if(loc == k - 1)
{
ndist[i*k+loc] = elemD;
nidx [i*k+loc] = pos;
}
else if(loc < k - 1)
{
for(int z = i*k+k-1; z > i*k+loc; z--)
{
ndist[z] = ndist[z-1];
nidx [z] = nidx [z-1];
}
ndist[i*k+loc] = elemD;
nidx [i*k+loc] = pos;
}
}
}
}
return knn;
}
void main(int argc, char **argv){
int n = atoi(argv[1]),
m = atoi(argv[2]),
d = atoi(argv[3]),
k = atoi(argv[4]);
knnresult knn;
knn.nidx = malloc(n * k * sizeof(int ));
knn.ndist = malloc(n * k * sizeof(double));
double *X = malloc(n * d * sizeof(double)),
*Y = malloc(m * d * sizeof(double));
for(int i = 0; i < n*d; i++) X[i] = ((double)(rand())) / ((double)(RAND_MAX));
for(int i = 0; i < m*d; i++) Y[i] = ((double)(rand())) / ((double)(RAND_MAX));
kNN(X, Y, n, m, d, k);
}