-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkdMapKnlogn.h
1004 lines (900 loc) · 44.3 KB
/
kdMapKnlogn.h
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
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2015, 2021, 2023, 2025 Russell A. Brown
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors
* may be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
* The following compilation defines are relevant.
*
* -D TREE - If defined, a k-d tree is created to implement a set instead of creating
* a key-to-multiple-value map.
*
* -D PREALLOCATE - If defined, all instances of KdNodes are allocated within a vector
* instead of being allocated individually. This decreases the time
* required to allocate and deallocate the KdNode instances.
* If DIMENSIONS is undefined, the (x, y, z, w...) coordinates are
* stored in an array whose first element is the last member field of
* the KdNode instance and whose remaining elements immediately follow
* the KdNode instance in the vector of Kdnodes. This decreases the
* time required to access the coordinates relative to storing the
* coordinates in a separate array because one degree of indirection
* is removed and because proximity of the coordinates to the KdNode
* instance may place both in the same cache line.
*
* -D DIMENSIONS=k - If defined, k is the number of dimensions and the -d command-line
* option is ignored. The (x, y, z, w...) coordinates are stored in
* each KdNode instance instead of in a separate array. This decreases
* the time required to access the coordinates relative to storing the
* coordinates in a separate array because one degree of indirection
* is removed and because proximity of the coordinates to the KdNode
* instance may place both in the same cache line. This define confers
* no performance improvement relative to the PREALLOCATE define and
* in addition, it does not permit dynamically sized coordinate arrays.
* It is useful only if the PREALLOCATE define fails to compile correctly.
* This define is ignored if PREALLOCATE is undefined.
*
* -D NO_SUPER_KEY - Do not compare super-keys in the KdNode::regionSearch function.
*
* -D INSERTION_SORT_CUTOFF=n - A cutoff for switching from merge sort to insertion sort
* in the KdNode::mergeSort* functions (default 15)
*
* -D REVERSE_NEAREST_NEIGHBORS - Enable the construction of a reverse nearest neighbors list.
*/
#ifndef KD_MAP_KNLOGN_H
#define KD_MAP_KNLOGN_H
#include "kdMapNode.h"
/* The KdTree class defines the k-d tree API. */
template <typename K, typename V=int> // V is a dummy template parameter if TREE is defined.
class KdTree {
private:
KdNode<K,V>* root = nullptr;
#ifdef PREALLOCATE
#ifdef DIMENSIONS
vector<KdNode<K,V>>* kdNodes = nullptr;
#else
vector<uint8_t>* kdNodes = nullptr;
#endif
#endif
public:
~KdTree() {
// If the KdNode instances and tuples are contained by a preallocated
// vector, delete it; otherwise, delete the root KdNode so that the
// ~KdNode destructor will recursively delete all KdNode instances.
#ifdef PREALLOCATE
delete kdNodes;
#else
delete root;
#endif
}
/*
* The buildKdTree function builds a k-d tree by recursively partitioning
* the reference arrays and adding KdNodes to the tree. These arrays
* are permuted cyclically for successive levels of the tree in
* order that sorting occur in the order x, y, z, w...
*
* Calling parameters:
*
* reference - a KdNode*** array to recursively sort via its (x, y, z, w...) tuples array
* temporary - a KdNode*** temporary array from which to copy sorted results;
* start - start element of the reference array
* end - end element of the reference array
* maximumSubmitDepth - the maximum tree depth at which a child task may be launched
* depth - the depth in the tree
*
* returns: a KdNode pointer to the root of the k-d tree
*/
private:
static KdNode<K,V>* buildKdTree(KdNode<K,V>*** const references,
vector< vector<signed_size_t> > const& permutation,
signed_size_t const start,
signed_size_t const end,
signed_size_t const maximumSubmitDepth,
signed_size_t const depth) {
KdNode<K,V>* node = nullptr;
// The partition permutes as x, y, z, w... and specifies the most significant key.
signed_size_t p = permutation.at(depth).at(permutation.at(0).size() - 1);
// Get the number of dimensions.
signed_size_t dim = permutation.at(0).size() - 2;
// Obtain the reference array that corresponds to the most significant key.
KdNode<K,V>** reference = references[permutation.at(depth).at(dim)];
if (end == start) {
// Only one reference was passed to this function, so add it to the tree.
node = reference[end];
}
else if (end == start + 1) {
// Two references were passed to this function in sorted order, so store the start
// element at this level of the tree and store the end element as the > child.
node = reference[start];
node->gtChild = reference[end];
}
else if (end == start + 2) {
// Three references were passed to this function in sorted order, so
// store the median element at this level of the tree, store the start
// element as the < child and store the end element as the > child.
node = reference[start + 1];
node->ltChild = reference[start];
node->gtChild = reference[end];
}
else if (end > start + 2) {
// Four or more references were passed to this function, so the
// median element of the reference array is chosen as the tuple
// about which the other reference arrays will be partitioned
// Avoid overflow when computing the median.
signed_size_t const median = start + ((end - start) / 2);
// Store the median element of the reference array in a new KdNode.
node = reference[median];
// Build both branches with child threads at as many levels of the tree
// as possible. Create the child threads as high in the tree as possible.
// Are child threads available to build both branches of the tree?
if (maximumSubmitDepth < 0 || depth > maximumSubmitDepth) {
// No, child threads are not available, so one thread will be used.
// Initialize startIndex=1 so that the 'for' loop that partitions the
// reference arrays will partition a number of arrays equal to dim.
signed_size_t startIndex = 1;
// If depth < dim-1, copy references[permut[dim]] to references[permut[0]]
// where permut is the permutation vector for this level of the tree.
// Sort the two halves of references[permut[0]] with p+1 as the most
// significant key of the super key. Use as the temporary array
// references[permut[1]] because that array is not used for partitioning.
// Partition a number of reference arrays equal to the tree depth because
// those reference arrays are already sorted.
if (depth < dim - 1) {
startIndex = dim - depth;
auto const dst = references[permutation.at(depth).at(0)];
auto const tmp = references[permutation.at(depth).at(1)];
for (int i = start; i <= end; ++i) {
dst[i] = reference[i];
}
// Sort the lower half of references[permut[0]] with the current thread.
MergeSort<K,V>::mergeSortReferenceAscending(dst, tmp, start, median - 1, p + 1, dim,
maximumSubmitDepth, depth + 1);
// Sort the upper half of references[permut[0]] with the current thread.
MergeSort<K,V>::mergeSortReferenceAscending(dst, tmp, median + 1, end, p + 1, dim,
maximumSubmitDepth, depth + 1);
}
// Partition the reference arrays specified by 'startIndex' in
// a priori sorted order by comparing super keys. Store the
// result from references[permut[i]]] in references[permut[i-1]]
// where permut is the permutation vector for this level of the
// tree, thus permuting the reference arrays. Skip the element
// of references[permut[i]] that equals the tuple that is stored
// in the new KdNode.
auto const tuple = node->tuple;
for (signed_size_t i = startIndex; i < dim; ++i) {
// Specify the source and destination reference arrays.
auto const src = references[permutation.at(depth).at(i)];
auto const dst = references[permutation.at(depth).at(i - 1)];
// Fill the lower and upper halves of one reference array
// in ascending order with the current thread.
for (signed_size_t j = start, lower = start - 1, upper = median; j <= end; ++j) {
auto const src_j = src[j];
auto const compare = MergeSort<K,V>::superKeyCompare(src_j->tuple, tuple, p, dim);
if (compare < 0) {
dst[++lower] = src_j;
}
else if (compare > 0) {
dst[++upper] = src_j;
}
}
}
// Recursively build the < branch of the tree with the current thread.
node->ltChild = buildKdTree(references, permutation, start, median - 1,
maximumSubmitDepth, depth + 1);
// Then recursively build the > branch of the tree with the current thread.
node->gtChild = buildKdTree(references, permutation, median + 1, end,
maximumSubmitDepth, depth + 1);
}
else {
// Yes, child threads are available, so two threads will be used.
// Initialize endIndex=0 so that the 'for' loop that partitions the
// reference arrays will partition a number of arrays equal to dim.
signed_size_t startIndex = 1;
// If depth < dim-1, copy references[permut[dim]] to references[permut[0]]
// where permut is the permutation vector for this level of the tree.
// Sort the two halves of references[permut[0]] with p+1 as the most
// significant key of the super key. Use as the temporary array
// references[permut[1]] because that array is not used for partitioning.
// Partition a number of reference arrays equal to the tree depth because
// those reference arrays are already sorted.
if (depth < dim - 1) {
startIndex = dim - depth;
auto const dst = references[permutation.at(depth).at(0)];
auto const tmp = references[permutation.at(depth).at(1)];
// Copy and sort the lower half of references[permut[0]] with a child thread.
auto copyFuture =
async(launch::async, [&] {
for (int i = start; i <= median - 1; ++i) {
dst[i] = reference[i];
}
MergeSort<K,V>::mergeSortReferenceAscending(dst, tmp, start, median - 1, p + 1,
dim, maximumSubmitDepth, depth);
});
// Copy and sort the upper half of references[permut[0]] with the current thread.
for (int i = median + 1; i <= end; ++i) {
dst[i] = reference[i];
}
MergeSort<K,V>::mergeSortReferenceAscending(dst, tmp, median + 1, end, p + 1, dim, maximumSubmitDepth, depth);
// Wait for the child thread to finish execution.
try {
copyFuture.get();
}
catch (exception const& e) {
throw runtime_error("\n\ncaught exception for copy future in buildKdTree\n");
}
}
// Create a copy of the node->tuple array so that the current thread
// and the child thread do not contend for read access to this array.
auto const tuple = node->tuple;
auto const point = new K[dim];
for (signed_size_t i = 0; i < dim; ++i) {
point[i] = tuple[i];
}
// Partition the reference arrays specified by 'startIndex' in
// a priori sorted order by comparing super keys. Store the
// result from references[permut[i]]] in references[permut[i-1]]
// where permut is the permutation vector for this level of the
// tree, thus permuting the reference arrays. Skip the element
// of references[permut[i]] that equals the tuple that is stored
// in the new KdNode.
for (signed_size_t i = startIndex; i < dim; ++i) {
// Specify the source and destination reference arrays.
auto const src = references[permutation.at(depth).at(i)];
auto const dst = references[permutation.at(depth).at(i - 1)];
// Two threads may be used to partition the reference arrays, analogous to
// the use of two threads to merge the results for the merge sort algorithm.
// Fill one reference array in ascending order with a child thread.
auto partitionFuture =
async(launch::async, [&] {
for (signed_size_t lower = start - 1, upper = median, j = start; j <= median; ++j) {
auto const src_j = src[j];
auto const compare = MergeSort<K,V>::superKeyCompare(src_j->tuple, point, p, dim);
if (compare < 0) {
dst[++lower] = src_j;
}
else if (compare > 0) {
dst[++upper] = src_j;
}
}
});
// Simultaneously fill the same reference array in descending order with the current thread.
for (signed_size_t lower = median, upper = end + 1, k = end; k > median; --k) {
auto const src_k = src[k];
auto const compare = MergeSort<K,V>::superKeyCompare(src_k->tuple, tuple, p, dim);
if (compare < 0) {
dst[--lower] = src_k;
}
else if (compare > 0) {
dst[--upper] = src_k;
}
}
// Wait for the child thread to finish execution.
try {
partitionFuture.get();
}
catch (exception const& e) {
throw runtime_error("\n\ncaught exception for partition future in buildKdTree\n");
}
}
// Delete the point array.
delete[] point;
// Recursively build the < branch of the tree with a child thread.
auto buildFuture = async(launch::async,
buildKdTree,
references,
ref(permutation),
start,
median - 1,
maximumSubmitDepth,
depth + 1);
// And simultaneously build the > branch of the tree with the current thread.
node->gtChild = buildKdTree(references, permutation, median + 1, end,
maximumSubmitDepth, depth + 1);
// Wait for the child thread to finish execution.
try {
node->ltChild = buildFuture.get();
}
catch (exception const& e) {
throw runtime_error("\n\ncaught exception for build future in buildKdTree\n");
}
}
}
else if (end < start) {
// This is an illegal condition that should never occur, so test for it last.
ostringstream buffer;
buffer << "\n\nerror has occurred at depth = " << depth << " : end = " << end
<< " < start = " << start << " in buildKdTree\n";
throw runtime_error(buffer.str());
}
// Return the pointer to the root of the k-d tree.
return node;
}
/*
* The swap function swaps two elements in a vector<signed_size_t>.
*
* Calling parameters:
*
* a - the vector
* i - the index of the first element
* j - the index of the second element
*/
private:
inline
static void swap(vector<signed_size_t>& a,
signed_size_t const i,
signed_size_t const j) {
signed_size_t const t = a[i];
a[i] = a[j];
a[j] = t;
}
/*
* The createKdTree function performs the necessary initialization then calls the buildKdTree function.
*
* Calling parameters:
*
* coordinates - if TREE is defined, a vector of (x, y, z, w...) coordinates and their associated values
* if TREE is undefined, a vector of pairs that store the coordinates and their associated values
* maximumSubmitDepth - the maximum tree depth at which a child task may be launched
* numberOfNodes - the number of nodes counted by KdNode::verifyKdTree - returned by reference
* allocateTime, sortTime, removeTime, kdTime, verifyTime, deallocateTime - execution times returned by reference
*
* returns: a KdNode pointer to the root of the k-d tree
*/
public:
static KdTree<K,V>* createKdTree(
#ifdef TREE
vector<vector<K>> const& coordinates,
#else
vector<pair<vector<K>,V>> const& coordinates,
#endif
signed_size_t const maximumSubmitDepth,
signed_size_t& numberOfNodes,
double& allocateTime,
double& sortTime,
double& removeTime,
double& kdTime,
double& verifyTime,
double& deallocateTime
) {
// Create a KdTree instance.
auto tree = new KdTree();
// Allocate the references arrays including one additional array.
#ifdef TREE
size_t numDimensions = coordinates[0].size();
#else
size_t numDimensions = coordinates[0].first.size();
#endif
auto beginTime = steady_clock::now();
KdNode<K,V>*** references = new KdNode<K,V>**[numDimensions + 1];
for (size_t i = 0; i < numDimensions + 1; ++i) {
references[i] = new KdNode<K,V>*[coordinates.size()];
}
#ifdef PREALLOCATE
// Allocate all KdNodes instances as a single vector so that they
// may be subsequently deleted as a single vector by the ~KdTree
// destructor, which is faster than deleting them individually.
//
// Point each element of the first references array to a KdNode instance
// that is an element of the kdNodes vector and initalize that instance.
#ifdef DIMENSIONS
// KdNode::tuple is an array of dimensions elements of type K
// that is embedded in each KdNode instance.
tree->kdNodes = new vector<KdNode<K,V>>(coordinates.size());
for (size_t i = 0; i < tree->kdNodes->size(); ++i) {
new(&(*(tree->kdNodes))[i]) KdNode<K,V>(coordinates, i);
references[0][i] = &(*(tree->kdNodes))[i];
}
#else // DIMENSIONS
// KdNode::tuple is an array of 1 element that is extended
// to dimensions elements by appending dimensions-1 elements
// to the KdNode instance.
//
// Because KdNode::tuple contains one element of type K,
// the alignment of KdNode at least as large as the
// alignment of K. Round up all alignments to the next
// multiple of kdNodeAlign.
size_t const kdNodeAlign = alignof(KdNode<K,V>);
size_t const kdNodeSize = ((sizeof(KdNode<K,V>) + kdNodeAlign - 1) / kdNodeAlign) * kdNodeAlign;
size_t const tupleSize = ((sizeof(K) * (numDimensions - 1)) / kdNodeAlign) * kdNodeAlign;
size_t const entrySize = kdNodeSize + tupleSize;
// The following kdNodeAlign argument to new is likely redundant and requires c++17. See
// https://stackoverflow.com/questions/15511909/does-the-alignas-specifier-work-with-new
tree->kdNodes = new vector<uint8_t>(entrySize * coordinates.size(), kdNodeAlign); // requires c++17
for (size_t i = 0; i < coordinates.size(); ++i) {
new(&(*(tree->kdNodes))[entrySize * i]) KdNode<K,V>(coordinates, i);
references[0][i] = reinterpret_cast<KdNode<K,V>*>(&(*(tree->kdNodes))[entrySize * i]);
}
#endif // DIMENSIONS
#else // PREALLOCATE
// Allocate KdNode instances for the first references array. These
// KdNode instances will be deallocated by the ~KdTree destructor.
for (size_t i = 0; i < coordinates.size(); ++i) {
references[0][i] = new KdNode<K,V>(coordinates, i);
}
#endif // PREALLOCATE
auto endTime = steady_clock::now();
auto duration = duration_cast<std::chrono::microseconds>(endTime - beginTime);
allocateTime = static_cast<double>(duration.count()) / MICROSECONDS_TO_SECONDS;
// Sort the first references array using multiple threads. Importantly,
// for compatibility with the 'permutation' vector initialized below,
// use the first dimension (0) as the leading key of the super key.
// Also, only the first references array is populated with T arrays.
beginTime = steady_clock::now();
MergeSort<K,V>::mergeSortReferenceAscending(references[0], references[numDimensions],
0, coordinates.size() - 1,
0, numDimensions, maximumSubmitDepth, 0);
endTime = steady_clock::now();
duration = duration_cast<std::chrono::microseconds>(endTime - beginTime);
sortTime = static_cast<double>(duration.count()) / MICROSECONDS_TO_SECONDS;
// Remove references to duplicate coordinates via one pass through the first reference array.
beginTime = steady_clock::now();
signed_size_t const end = KdNode<K,V>::removeDuplicates(references[0], 0, numDimensions, coordinates.size());
endTime = steady_clock::now();
duration = duration_cast<std::chrono::microseconds>(endTime - beginTime);
removeTime = static_cast<double>(duration.count()) / MICROSECONDS_TO_SECONDS;
// Start the timer to time building the k-d tree.
beginTime = steady_clock::now();
// Determine the maximum depth of the k-d tree, which is log2( coordinates.size() ).
signed_size_t maxDepth = 1;
signed_size_t size = coordinates.size();
while (size > 0) {
++maxDepth;
size >>= 1;
}
// It is unnecessary to compute either the permutation of the reference array or
// the partition coordinate upon each recursive call of the buildKdTree function
// because both depend only on the depth of recursion, so they may be pre-computed.
//
// Because this vector is initialized with 0, 1, 2, 3, 0, 1, 2, 3, etc. (for
// e.g. 4-dimensional data), the leading key of the super key will be 0 at the
// first level of the nascent tree, consistent with having sorted the reference
// array above using 0 as the leading key of the super key.
//
// Begin by creating an 'indices' vector.
vector<signed_size_t> indices(numDimensions + 2);
for (size_t i = 0; i < indices.size() - 1; ++i) {
indices[i] = i;
}
// Create a 2D 'permutation' vector from the 'indices' vector to specify permutation
// of the reference arrays and of the partition coordinate.
vector< vector<signed_size_t> > permutation(maxDepth, vector<signed_size_t>(numDimensions + 2));
// Fill the permutation vector by calculating the permutation of the indices vector
// and the the partition coordinate of the tuple at each depth in the tree.
for (size_t i = 0; i < permutation.size(); ++i) {
// The last entry of the indices vector contains the partition coordinate.
indices[numDimensions + 1] = i % numDimensions;
// Swap the first and second to the last elements of the indices vector.
swap(indices, 0, numDimensions);
// Copy the indices vector to one row of the permutation vector.
permutation[i] = indices;
// Swap the third and second to the last elements of the indices vector.
swap(indices, numDimensions - 1, numDimensions);
}
// Build the k-d tree with multiple threads if possible.
tree->root = buildKdTree(references, permutation, 0, end, maximumSubmitDepth, 0);
endTime = steady_clock::now();
duration = duration_cast<std::chrono::microseconds>(endTime - beginTime);
kdTime = static_cast<double>(duration.count()) / MICROSECONDS_TO_SECONDS;
// Verify the k-d tree and report the number of kdNodes. Begin by
// creating a 1D permutation vector for use by the verifyKdTree function.
//
// Because the partition coordinate permutes in the order 0, 1, 2, 3, 0, 1, 2, 3, etc.
// (for e.g. 4-dimensional data), the leading key of the super key will be 0 at the
// first level of the nascent tree, consistent with having sorted the reference array
// using 0 as the leading key of the super key.
beginTime = steady_clock::now();
vector<signed_size_t> permutationVerify;
KdNode<K,V>::createPermutation(permutationVerify, numDimensions, coordinates.size());
numberOfNodes = tree->root->verifyKdTree(permutationVerify, numDimensions, maximumSubmitDepth, 0);
endTime = steady_clock::now();
duration = duration_cast<std::chrono::microseconds>(endTime - beginTime);
verifyTime = static_cast<double>(duration.count()) / MICROSECONDS_TO_SECONDS;
// Delete the references arrays but not the KdNodes instances that they point to
// because those KdNodes instances will be deleted by the ~KdTree destructor.
beginTime = steady_clock::now();
for (size_t i = 0; i < numDimensions + 1; ++i) {
delete[] references[i];
}
delete[] references;
endTime = steady_clock::now();
duration = duration_cast<std::chrono::microseconds>(endTime - beginTime);
deallocateTime = static_cast<double>(duration.count()) / MICROSECONDS_TO_SECONDS;
// Return the pointer to the KdTree instance.
return tree;
}
/*
* The searchRegion function searches the k-d tree to find the KdNodes that
* lie within a hyper-rectangle defined by the query lower and upper bounds.
*
* Calling parameters:
*
* result - a list<KdNode<K,V>*> that is passed by reference and modified
* queryLower - the query lower bound vector that is passed by reference and modified
* queryUpper - the query upper bound vector that is passed by reference and modified
* maximumSubmitDepth - the maximum tree depth at which a child task may be launched
* size - the number of points in the coordinates vector
*
* return a list of KdNodes that lie within the query hyper-rectangle
*/
public:
void searchRegion(list<KdNode<K,V>*>& result,
vector<K>& queryLower,
vector<K>& queryUpper,
signed_size_t const maximumSubmitDepth,
signed_size_t const size) {
root->searchRegion(result, queryLower, queryUpper, maximumSubmitDepth, size);
}
/*
* The searchRegion function searches the k-d tree to find the KdNodes that
* lie within a hyper-rectangle defined by the query lower and upper bounds.
*
* Calling parameters:
*
* result - a list of KdNode pointers that is passed by reference and modified
* queryLower - the query lower bound vector that is passed by reference and modified
* queryUpper - the query upper bound vector that is passed by reference and modified
* maximumSubmitDepth - the maximum tree depth at which a child task may be launched
* size - the number of points in the coordinates vector
* enable - a vector that specifies the dimensions on which to test for insidedness
* and prune the region search
*
* return a list of KdNodes that lie within the query hyper-rectangle
*/
public:
void searchRegion(list<KdNode<K,V>*>& result,
vector<K>& queryLower,
vector<K>& queryUpper,
signed_size_t const maximumSubmitDepth,
signed_size_t const size,
vector<bool> const& enable) {
root->searchRegion(result, queryLower, queryUpper, maximumSubmitDepth, size, enable);
}
/*
* Walk the k-d tree and append to a list each KdNode that lies inside
* the hyper-rectangle defined by the query lower and upper bounds.
*
* Calling parameters:
*
* result - a list of KdNode pointers that is passed by reference and modified
* queryLower - the query lower bound vector
* queryUpper - the query upper bound vector
*
* return a list of pointers to KdNodes that lie within the query hyper-rectangle.
*/
public:
void bruteRegion(list<KdNode<K,V>*>& result,
vector<K>& queryLower,
vector<K>& queryUpper) {
root->bruteRegion(result, queryLower, queryUpper);
}
/*
* Find up to M nearest neighbors to the query vector and return them as a list ordered by increasing distance.
*
* Calling parameters:
*
* neighbors - the nearest neighbors list that is passed by reference and modified.
* query - the query vector
* numNeighbors - the number M of nearest neighbors to attempt to find
* size - the number of points in the coordinates vector
*/
public:
void findNearestNeighbors(forward_list< pair<double, KdNode<K,V>*> >& neighbors,
vector<K> const& query,
signed_size_t const numNeighbors,
signed_size_t const size) {
root->findNearestNeighbors(neighbors, query, numNeighbors, size);
}
/*
* Find up to M nearest neighbors to the query vector and return them as a list ordered by increasing distance.
*
* Calling parameters:
*
* neighbors - the nearest neighbors list that is passed by reference and modified.
* query - the query vector
* numNeighbors - the number M of nearest neighbors to attempt to find
* size - the number of points in the coordinates vector
* enable - a vector that specifies the dimensions for which to test distance
*/
public:
void findNearestNeighbors(forward_list< pair<double, KdNode<K,V>*> >& neighbors,
vector<K> const& query,
signed_size_t const numNeighbors,
signed_size_t const size,
vector<bool> const& enable) {
root->findNearestNeighbors(neighbors, query, numNeighbors, size, enable);
}
/*
* Find up to M nearest neighbors to the query vector and return them as a list ordered by increasing distance.
*
* Calling parameters:
*
* neighbors - the nearest neighbors list that is passed by reference and modified
* query - the query vector
* permutation - vector that specifies permutation of the partition coordinate
* numNeighbors - the number M of nearest neighbors to attempt to find
*/
public:
void findNearestNeighbors(forward_list< pair<double, KdNode<K,V>*> >& neighbors,
vector<K> const& query,
vector<signed_size_t> const& permutation,
signed_size_t const numNeighbors) {
root->findNearestNeighbors(neighbors, query, permutation, numNeighbors);
}
/*
* Find up to M nearest neighbors to the query vector and return them as a list ordered by increasing distance.
*
* Calling parameters:
*
* neighbors - the nearest neighbors list that is passed by reference and modified
* query - the query vector
* permutation - vector that specifies permutation of the partition coordinate
* numNeighbors - the number M of nearest neighbors to attempt to find
* enable - a vector that specifies the dimensions for which to test distance
*/
public:
void findNearestNeighbors(forward_list< pair<double, KdNode<K,V>*> >& neighbors,
vector<K> const& query,
vector<signed_size_t> const& permutation,
signed_size_t const numNeighbors,
vector<bool> const& enable) {
root->findNearestNeighbors(neighbors, query, permutation, numNeighbors, enable);
}
/*
* Verify the consistency between the nearest neighbors lists found
* by k-d tree search and by brute force.
*
* Calling parameters:
*
* neighborsFast - a list of nearest neighbors found by k-d tree search
* neighborsSlow - a list of nearest neighbors found by brute force.
*
* Although this function does not directly access the k-d tree, it requires the persistence
* of the k-d tree for access to the KdNodes via the lists. Hence, this function is not static.
*/
public:
void verifyNearestNeighbors(forward_list< pair<double, KdNode<K,V>*> >& neighborsFast,
forward_list< pair<double, KdNode<K,V>*> >& neighborsSlow) const {
root->verifyNearestNeighbors(neighborsFast, neighborsSlow);
}
/*
* Sort a list of KdNode instances by increasing distance from the query point.
*
* Calling parameters:
*
* kdList - the list of KdNode instances
* query - a vector that contains the query point coordinates
* maxNodes - the maximum number of nodes to maintain on the heap
*
* returns: a sorted forward list of KdNode instances
*
* Because this function does not access the k-d tree, it could be static.
* However, calling it as a static function requires speicification of a
* type, so calling it as a non-static function is less cumbersome.
*/
public:
forward_list<pair<double, KdNode<K,V>*>> sortByDistance(list<KdNode<K,V>*> const& kdList,
vector<K> const& query,
signed_size_t const& maxNodes) {
return root->sortByDistance(kdList, query, maxNodes);
}
#ifdef REVERSE_NEAREST_NEIGHBORS
/*
* Walk the k-d tree, find up to M nearest neighbors to each point in the k-d tree,
* and add those nearest neighbors to a nearest neighbors vector and to a reverse
* nearest neighbors vector.
*
* Each element of the reverse nearest neighbors vector contains a list
* of KdNodes to which the reference KdNode is a nearest neighbor.
*
* The concept of reverse nearest neighbors was first described by F. Korn and
* S. Muthukrishnan in "Influence Sets Based on Reverse Nearest Neigbor Queries",
* Proceedings of the 2000 ACM SIGMOD International Conference on Management of
* Data, pages 201-212.
*
* Calling parameters:
*
* nn - the nearest neighbors vector that is passed by reference and modified
* rnn - the reverse nearest neighbors vector that is passed by reference and modified
* mutexes - a vector of mutexes to make individual rnn list update thread safe
* numDimensions - the dimensionality k of the k-d tree
* numNeighbors - the number M of nearest neighbors to attempt to find
* maximumSubmitDepth - the maximum tree depth at which a child task may be launched
*/
public:
void findReverseNearestNeighbors(vector< forward_list< pair<double, KdNode<K,V>*> > >& nn,
vector< forward_list< pair<double, KdNode<K,V>*> > >& rnn,
vector<mutex>& mutexes,
signed_size_t const numDimensions,
signed_size_t const numNeighbors,
signed_size_t maximumSubmitDepth) {
root->findReverseNearestNeighbors(nn, rnn, mutexes, numDimensions, numNeighbors, maximumSubmitDepth);
}
/*
* Walk the k-d tree, find up to M nearest neighbors to each point in the k-d tree,
* and add those nearest neighbors to a nearest neighbors vector and to a reverse
* nearest neighbors vector.
*
* Each element of the reverse nearest neighbors vector contains a list
* of KdNodes to which the reference KdNode is a nearest neighbor.
*
* The concept of reverse nearest neighbors was first described by F. Korn and
* S. Muthukrishnan in "Influence Sets Based on Reverse Nearest Neigbor Queries",
* Proceedings of the 2000 ACM SIGMOD International Conference on Management of
* Data, pages 201-212.
*
* Calling parameters:
*
* nn - the nearest neighbors vector that is passed by reference and modified
* rnn - the reverse nearest neighbors vector that is passed by reference and modified
* mutexes - a vector of mutexes to make individual rnn list update thread safe
* numDimensions - the dimensionality k of the k-d tree
* numNeighbors - the number M of nearest neighbors to attempt to find
* maximumSubmitDepth - the maximum tree depth at which a child task may be launched
* enable - a vector that specifies the dimensions for which to test distance
*/
public:
void findReverseNearestNeighbors(vector< forward_list< pair<double, KdNode<K,V>*> >* >& nn,
vector< forward_list< pair<double, KdNode<K,V>*> >* >& rnn,
vector<mutex>& mutexes,
signed_size_t const numDimensions,
signed_size_t const numNeighbors,
signed_size_t const maximumSubmitDepth,
vector<bool> const& enable) {
root->findReverseNearestNeighbors(nn, rnn, mutexes, numDimensions, numNeighbors, maximumSubmitDepth, enable);
}
/*
* Verify the correctness of the reverse nearest neighbors vector.
*
* Calling parameter:
*
* nn - the nearest neighbors vector
* rnn - the reverse nearest neighbors vector
* size - the number of KdNode instances prior to calling KdMapNode::removeDuplicates
*
* Although this function does not directly access the k-d tree, it requires the persistence
* of the k-d tree for access to the KdNodes via the vectors. Hence, this function is not static.
*/
public:
void verifyReverseNeighbors(vector< forward_list< pair<double, KdNode<K,V>*> > >& nn,
vector< forward_list< pair<double, KdNode<K,V>*> > >& rnn,
size_t const size) {
root->verifyReverseNeighbors(nn, rnn, size);
}
/*
* Calculate the mean and standard deviation of the distances and list sizes in a vector.
*
* Calling parameter:
*
* vec - a vector of lists
*
* Although this function does not directly access the k-d tree, it requires the persistence
* of the k-d tree for access to the KdNodes via the vector. Hence, this function is not static.
*/
public:
void calculateMeanStd(vector< forward_list< pair<double, KdNode<K,V>*> > >& vec,
double& meanSize,
double& stdSize,
double& meanDist,
double& stdDist) const {
root->calculateMeanStd(vec, meanSize, stdSize, meanDist, stdDist);
}
/*
* Count the number of non-empty lists in a vector.
*
* Calling parameter:
*
* vec - a vector of lists
*
* Although this function does not directly access the k-d tree, it requires the persistence
* of the k-d tree for access to the KdNodes via the vector. Hence, this function is not static.
*/
public:
size_t nonEmptyLists(vector< forward_list< pair<double, KdNode<K,V>*> > >& vec) const {
return root->nonEmptyLists(vec);
}
#endif // REVERSE_NEAREST_NEIGHBORS
/*
* Find M nearest neighbors to the query vector via brute force
* and return them as a list ordered by increasing distance.
*
* Calling parameters:
*
* neighbors - the nearest neighbors list that is passed by reference and modified.
* query - the query vector
* numNeighbors - the number M of nearest neighbors to find
*/
public:
void bruteNearestNeighbors(forward_list< pair<double, KdNode<K,V>*> >& neighbors,
vector<K> const& query,
signed_size_t const numNeighbors) {
root->bruteNearestNeighbors(neighbors, query, numNeighbors);
}
/*
* The printTuple function prints one tuple.
*
* Calling parameters:
*
* tuple - the tuple as an array
* dim - the number of dimensions
*
* Because this function does not access the k-d tree, it could be static.
* However, calling it as a static function requires speicification of a
* type, so calling it as a non-static function is less cumbersome.
*/
public:
void printTuple(K const* tuple,
signed_size_t const dim) const {
root->printTuple(tuple, dim);
}
/*
* The printTuple function prints one tuple.
*
* Calling parameter:
*
* tuple - the tuple as a vector
*
* Because this function does not access the k-d tree, it could be static.
* However, calling it as a static function requires speicification of a
* type, so calling it as a non-static function is less cumbersome.
*/
public:
void printTuple(vector<K> const& tuple) const {
root->printTuple(tuple);
}
/*
* The printTuples function prints all tuples in a forward list of pairs.
*
* Calling parameters:
*
* regionList - a forward list of (double, KdNode*) pairs
* maximumNumberOfNodesToPrint - the maximum number of KdNodes to print
* numDimensions - the number of dimensions
*
* Because this function does not access the k-d tree, it could be static.
* However, calling it as a static function requires speicification of a
* type, so calling it as a non-static function is less cumbersome.
*/
public:
void printTuples(forward_list<pair<double, KdNode<K,V>*>> const& regionList,
signed_size_t const maximumNumberOfNodesToPrint,
signed_size_t const numDimensions) const {
root->printTuples(regionList, maximumNumberOfNodesToPrint, numDimensions);
}
/*
* The printKdTree function prints the k-d tree "sideways" with the root at the ltChild.
*
* Calling parameters:
*
* dim - the number of dimensions
* depth - the depth in the k-d tree
*/
public:
void printKdTree(signed_size_t const dim,
signed_size_t const depth) const {
root->printKdTree(dim, depth);