-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsearchInTheBox.cl
More file actions
163 lines (132 loc) · 4.07 KB
/
searchInTheBox.cl
File metadata and controls
163 lines (132 loc) · 4.07 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
#define MAX_SIZE 128
#define NUM_DIMENSIONS 3
#define MAX_RESULT_SIZE 256
#define RANGE 0.2f;
typedef struct
{
unsigned int data[MAX_SIZE];
unsigned int front;
unsigned int tail;
unsigned int size;
} Queue;
bool push_back(Queue* queue, unsigned int index)
{
if (queue->size < MAX_SIZE)
{
queue->data[queue->tail] = index;
queue->tail = (queue->tail + 1) % MAX_SIZE;
queue->size++;
return true;
}
return false;
}
unsigned int pop_front(Queue* queue)
{
if (queue->size > 0)
{
unsigned int element = queue->data[queue->front];
queue->front = (queue->front + 1) % MAX_SIZE;
queue->size--;
return element;
}
}
void erase_first_n_elements(Queue* queue, unsigned int n)
{
unsigned int elementsToErase = queue->size - n > 0 ? n : queue->size;
queue->size -= elementsToErase;
queue->front = (queue->front + elementsToErase) % MAX_SIZE;
}
unsigned int leftSonIndex(unsigned int index)
{
return 2 * index + 1;
}
unsigned int rightSonIndex(unsigned int index)
{
return 2 * index + 2;
}
bool intersects(unsigned int index, __global float* theDimensions, unsigned int nPoints,
float* minPoint, float* maxPoint, int dimension)
{
return (theDimensions[nPoints * dimension + index] <= maxPoint[dimension]
&& theDimensions[nPoints * dimension + index] >= minPoint[dimension]);
}
bool isInTheBox(unsigned int index, __global float* theDimensions, unsigned int nPoints,
float* minPoint, float* maxPoint)
{
bool inTheBox = true;
for (int i = 0; i < NUM_DIMENSIONS; ++i)
{
inTheBox &= (theDimensions[nPoints * i + index] <= maxPoint[i]
&& theDimensions[nPoints * i + index] >= minPoint[i]);
}
return inTheBox;
}
__kernel void SearchInTheKDBox(unsigned int nPoints, __global float* dimensions, __global unsigned int* ids, __global unsigned int* results)
{
unsigned int point_index = get_global_id(0);
// float range = 0.1f;
if(point_index < nPoints)
{
int theDepth = floor(log2((float)nPoints));
float minPoint[NUM_DIMENSIONS];
float maxPoint[NUM_DIMENSIONS];
for(int i = 0; i<NUM_DIMENSIONS; ++i)
{
minPoint[i] = dimensions[nPoints*i+point_index] - RANGE;
maxPoint[i] = dimensions[nPoints*i+point_index] + RANGE;
}
Queue indecesToVisit;
indecesToVisit.front = indecesToVisit.tail =indecesToVisit.size =0;
unsigned int pointsFound=0;
unsigned int resultIndex = nPoints + MAX_RESULT_SIZE*point_index;
push_back(&indecesToVisit, 0);
for (int depth = 0; depth < theDepth + 1; ++depth)
{
int dimension = depth % NUM_DIMENSIONS;
unsigned int numberOfIndecesToVisitThisDepth =
indecesToVisit.size;
for (unsigned int visitedIndecesThisDepth = 0;
visitedIndecesThisDepth < numberOfIndecesToVisitThisDepth;
visitedIndecesThisDepth++)
{
// unsigned int index = indecesToVisit.data[(indecesToVisit.front+visitedIndecesThisDepth)% MAX_SIZE];
unsigned int index = pop_front(&indecesToVisit);
// if(point_index == 0)
// {
// printf("index: %d, dimensions: %f %f %f\n", index, dimensions[index], dimensions[nPoints+index], dimensions[2*nPoints+index]);
// }
bool intersection = intersects(index,dimensions, nPoints, minPoint, maxPoint,
dimension);
if(intersection && isInTheBox(index, dimensions, nPoints, minPoint, maxPoint))
{
if(pointsFound < MAX_RESULT_SIZE)
{
// if(point_index == 0)
// {
// printf("index: %d added to results", index);
// }
results[resultIndex] = index;
resultIndex++;
pointsFound++;
}
}
bool isLowerThanBoxMin = dimensions[nPoints*dimension + index]
< minPoint[dimension];
int startSon = isLowerThanBoxMin; //left son = 0, right son =1
int endSon = isLowerThanBoxMin || intersection;
for (int whichSon = startSon; whichSon < endSon + 1; ++whichSon)
{
unsigned int indexToAdd = leftSonIndex(index) + whichSon;
if (indexToAdd < nPoints)
{
push_back(&indecesToVisit,indexToAdd);
if(indecesToVisit.size == MAX_SIZE)
printf("queue limit hit");
}
}
}
// erase_first_n_elements(&indecesToVisit,numberOfIndecesToVisitThisDepth );
}
results[point_index] = pointsFound;
}
}