You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// Initialize with minimum similarity and invalid index
18
+
for (int i = 0; i < k; ++i) {
19
+
topKSimilarities[i] = -1.0;
20
+
topKIndices[i] = -1;
21
+
}
22
+
23
+
for (int baseIdx = 0; baseIdx < n; ++baseIdx) {
24
+
float ip = 0;
25
+
float sumu2 = 0;
26
+
float sumv2 = 0;
27
+
28
+
for (int dim = 0; dim < d; ++dim) {
29
+
float u = base[baseIdx * d + dim];
30
+
float v = query[queryIdx * d + dim];
31
+
ip += u * v;
32
+
sumu2 += u * u;
33
+
sumv2 += v * v;
34
+
}
35
+
36
+
float sim = ip / (sqrt(sumu2) * sqrt(sumv2));
37
+
38
+
// Check if this similarity is in the top-k
39
+
for (int i = 0; i < k; ++i) {
40
+
if (sim > topKSimilarities[i]) {
41
+
// Shift lower similarities down
42
+
for (int j = k - 1; j > i; --j) {
43
+
topKSimilarities[j] = topKSimilarities[j - 1];
44
+
topKIndices[j] = topKIndices[j - 1];
45
+
}
46
+
47
+
// Insert new similarity
48
+
topKSimilarities[i] = sim;
49
+
topKIndices[i] = baseIdx;
50
+
break;
51
+
}
52
+
}
53
+
}
54
+
55
+
// Write the top-k indices to the output
56
+
for (int i = 0; i < k; ++i) {
57
+
output[queryIdx * k + i] = topKIndices[i];
58
+
}
59
+
}
60
+
}
61
+
62
+
9
63
__global__voidfindNearestNeighborCosine(float *points, float *queries, float *max_cosine, int *max_index, int n, int num_queries, int dimensions, float target_similarity) {
10
64
extern__shared__char shared[];
11
65
float *s_cosine = (float*)shared;
@@ -93,14 +147,17 @@ int main(int argc, char* argv[]) {
__global__voidfindNearestNeighborCosine(float *points, float *queries, float *max_cosine, int *max_index, int n, int num_queries, int dimensions, float target_similarity) {
10
+
extern__shared__char shared[];
11
+
float *s_cosine = (float*)shared;
12
+
int *s_index = (int*)(shared + blockDim.x * sizeof(float));
0 commit comments