forked from aboria/Aboria
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCellList.h
1232 lines (1104 loc) · 41.1 KB
/
CellList.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) 2005-2016, University of Oxford.
All rights reserved.
University of Oxford means the Chancellor, Masters and Scholars of the
University of Oxford, having an administrative office at Wellington
Square, Oxford OX1 2JD, UK.
This file is part of Aboria.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* 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.
* Neither the name of the University of Oxford 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.
*/
#ifndef BUCKETSEARCH_SERIAL_H_
#define BUCKETSEARCH_SERIAL_H_
#include "Get.h"
#include "Log.h"
#include "NeighbourSearchBase.h"
#include "SpatialUtil.h"
#include "Traits.h"
#include "Vector.h"
#include <boost/iterator/iterator_facade.hpp>
#include <iostream>
#include <set>
#include <vector>
namespace Aboria {
template <typename Traits> struct CellListQuery;
/// @brief A cell list spatial data structure that is paired with a
/// CellListQuery query type
///
/// This class implements neighbourhood searching using a bucket search, or
/// cell list algorithm. The domain is first divided up into a regular grid of
/// constant size "buckets".
///
/// After the buckets are created, a set of 3D points can be assigned to their
/// respective buckets. After this,
/// neighbourhood queries around a given point can be performed be looking
/// at all the points in the same bucket or surrounding buckets of the given
/// point.
///
template <typename Traits>
class CellList : public neighbour_search_base<CellList<Traits>, Traits,
CellListQuery<Traits>> {
typedef typename Traits::double_d double_d;
typedef typename Traits::position position;
typedef typename Traits::vector_int vector_int;
typedef typename Traits::iterator iterator;
typedef typename Traits::unsigned_int_d unsigned_int_d;
typedef neighbour_search_base<CellList<Traits>, Traits, CellListQuery<Traits>>
base_type;
friend base_type;
public:
///
/// @brief constructs the default base structure
///
CellList()
: base_type(),
m_size_calculated_with_n(std::numeric_limits<size_t>::max()),
m_serial(detail::concurrent_processes<Traits>() == 1) {}
///
/// @brief This structure is not ordered. That is, the order of the particles
/// is not important to the search algorithm. Instead, the indicies of
/// the particles are kept in a linked list
///
static constexpr bool ordered() { return false; }
struct delete_points_in_bucket_lambda;
struct insert_points_lambda_sequential_serial;
struct insert_points_lambda_non_sequential_serial;
struct insert_points_lambda_sequential;
struct insert_points_lambda_non_sequential;
struct copy_points_in_bucket_lambda;
///
/// @brief Print the data structure to stdout
///
void print_data_structure() const {
#ifndef __CUDA_ARCH__
LOG(1, "\tbuckets:");
for (size_t i = 0; i < m_buckets.size(); ++i) {
if (m_buckets[i] != detail::get_empty_id()) {
LOG(1, "\ti = " << i << " bucket contents = " << m_buckets[i]);
}
}
LOG(1, "\tend buckets");
LOG(1, "\tlinked list:");
for (size_t i = 0; i < m_linked_list.size(); ++i) {
if (m_serial) {
LOG(1, "\ti = " << i << " p = "
<< static_cast<const double_d &>(
get<position>(*(this->m_particles_begin + i)))
<< " contents = " << m_linked_list[i]
<< ". reverse = " << m_linked_list_reverse[i]);
} else {
LOG(1, "\ti = " << i << " p = "
<< static_cast<const double_d &>(
get<position>(*(this->m_particles_begin + i)))
<< " contents = " << m_linked_list[i]);
}
}
LOG(1, "\tend linked list:");
#endif
}
private:
///
/// @brief set the domain, called from base class
/// @return true if this function wishes to trigger a reconstruction of the
/// data structure (i.e. resize the buckets and re-insert all the particles to
/// the linked list) set
///
bool set_domain_impl() {
const size_t n = this->m_particles_end - this->m_particles_begin;
if (n < 0.5 * m_size_calculated_with_n ||
n > 2 * m_size_calculated_with_n) {
m_size_calculated_with_n = n;
LOG(2, "CellList: recalculating bucket size");
if (this->m_n_particles_in_leaf > n) {
m_size = unsigned_int_d::Constant(1);
} else {
const double total_volume =
(this->m_bounds.bmax - this->m_bounds.bmin).prod();
const double box_volume =
this->m_n_particles_in_leaf / double(n) * total_volume;
const double box_side_length =
std::pow(box_volume, 1.0 / Traits::dimension);
m_size =
floor((this->m_bounds.bmax - this->m_bounds.bmin) / box_side_length)
.template cast<unsigned int>();
for (size_t i = 0; i < Traits::dimension; ++i) {
if (m_size[i] == 0) {
m_size[i] = 1;
}
}
}
m_bucket_side_length =
(this->m_bounds.bmax - this->m_bounds.bmin) / m_size;
m_point_to_bucket_index =
detail::point_to_bucket_index<Traits::dimension>(
m_size, m_bucket_side_length, this->m_bounds);
LOG(2, "\tbucket side length = " << m_bucket_side_length);
LOG(2, "\tnumber of buckets = " << m_size << " (total=" << m_size.prod()
<< ")");
m_buckets.assign(m_size.prod(), detail::get_empty_id());
// TODO: should always be true?
m_use_dirty_cells = true;
this->m_query.m_buckets_begin =
iterator_to_raw_pointer(m_buckets.begin());
this->m_query.m_bucket_side_length = this->m_bucket_side_length;
this->m_query.m_bounds.bmin = this->m_bounds.bmin;
this->m_query.m_bounds.bmax = this->m_bounds.bmax;
this->m_query.m_periodic = this->m_periodic;
this->m_query.m_end_bucket = m_size - 1;
this->m_query.m_point_to_bucket_index = m_point_to_bucket_index;
return true;
} else {
return false;
}
}
///
/// @brief check that the data structure is internally consistent
///
void check_data_structure() {
int num_particles = 0;
for (size_t i = 0; i < m_buckets.size(); i++) {
int j = m_buckets[i];
int old_j = detail::get_empty_id();
while (j != detail::get_empty_id()) {
ASSERT(m_linked_list_reverse[j] == old_j,
"m_linked_list_reverse not right: m_linked_list_reverse[j] = "
<< m_linked_list_reverse[j] << ", j = " << j
<< ", old_j = " << old_j);
ASSERT(m_dirty_buckets[j] == i,
"m_dirty_buckets not right: m_dirty_buckets[j] = "
<< m_dirty_buckets[j] << ", i = " << i << ", j = " << j);
ASSERT(j < m_linked_list.size(), "j index too large");
ASSERT(j >= 0, "j index less than zero");
num_particles++;
old_j = j;
j = m_linked_list[old_j];
}
}
ASSERT(num_particles == m_linked_list.size(),
"m_linked_list size inconsistent");
ASSERT(num_particles == m_linked_list_reverse.size(),
"m_linked_list_reverse size inconsistent");
ASSERT(num_particles == m_dirty_buckets.size(),
"m_dirty_buckets size inconsistent");
for (size_t i = 0; i < m_linked_list.size(); ++i) {
ASSERT(i < m_linked_list.size(), "i index too large");
ASSERT(m_dirty_buckets[i] < m_buckets.size(),
"m_dirty_buckets not right");
ASSERT(m_dirty_buckets[i] >= 0, "m_dirty_buckets not right");
ASSERT(m_linked_list[i] < m_linked_list.size() ||
m_linked_list[i] == detail::get_empty_id(),
"m_linked_list not right: m_linked_list[i] = "
<< m_linked_list[i]
<< ", m_linked_list.size() = " << m_linked_list.size());
ASSERT(m_linked_list[i] >= 0 ||
m_linked_list[i] == detail::get_empty_id(),
"m_linked_list not right");
ASSERT(m_linked_list_reverse[i] < m_linked_list_reverse.size() ||
m_linked_list_reverse[i] == detail::get_empty_id(),
"m_linked_list_reverse not right");
ASSERT(m_linked_list_reverse[i] >= 0 ||
m_linked_list_reverse[i] == detail::get_empty_id(),
"m_linked_list_reverse not right");
}
}
///
/// @brief called by base class, does nothing for this data structure
///
void update_iterator_impl() {
// check_data_structure();
}
///
/// @brief called by base class, deals with an updated particle range.
/// Needs to handle:
/// dead particles (only if @p update_end == @p m_particles_end),
/// particles with different positions,
/// and new particles
///
/// @param update_begin iterator to the beginning of the update range
/// @param update_end iterator to the end of the update range
/// @param num_new_particles_added number of new particles to be added
/// @param call_set_domain set to true (the default) if set_domain_impl()
/// needs to be called
///
void update_positions_impl(iterator update_begin, iterator update_end,
const int num_new_particles_added,
const bool call_set_domain = true) {
// if call_set_domain == false then set_domain_impl() has already
// been called, and returned true
const bool reset_domain = call_set_domain ? set_domain_impl() : true;
const size_t n_update = update_end - update_begin;
const size_t n_alive = this->m_alive_indices.size();
const size_t n_dead_in_update = n_update - n_alive;
const size_t n_all = this->m_particles_end - this->m_particles_begin;
const size_t n = n_all - n_dead_in_update;
const int update_begin_index = update_begin - this->m_particles_begin;
LOG(2, "BucketSearchSerial: update_positions, n_update = "
<< n_update << " n_alive = " << n_alive << " n = " << n);
if (n_update == n_all || reset_domain) {
// updating everthing so clear out entire ds
if (!reset_domain) {
if (m_dirty_buckets.size() <
static_cast<float>(m_buckets.size()) /
detail::concurrent_processes<Traits>()) {
for (int i : m_dirty_buckets) {
m_buckets[i] = detail::get_empty_id();
}
} else {
m_buckets.assign(m_buckets.size(), detail::get_empty_id());
}
}
m_linked_list.assign(n, detail::get_empty_id());
if (m_serial) {
m_linked_list_reverse.assign(n, detail::get_empty_id());
}
m_dirty_buckets.resize(n);
} else {
// only updating some so only clear out indices in update range
const int start_index_deleted = update_begin - this->m_particles_begin;
const int end_index_deleted =
update_end - this->m_particles_begin - num_new_particles_added;
if (m_serial) {
for (int toi = start_index_deleted; toi < end_index_deleted; ++toi) {
const int toi_back = m_linked_list_reverse[toi];
const int toi_forward = m_linked_list[toi];
ASSERT(
toi_back == detail::get_empty_id() ||
(toi_back < static_cast<int>(m_linked_list_reverse.size()) &&
toi_back >= 0),
"invalid index of " << toi_back
<< ". Note linked list reverse size is "
<< m_linked_list_reverse.size());
ASSERT(toi_forward == detail::get_empty_id() ||
(toi_forward <
static_cast<int>(m_linked_list_reverse.size()) &&
toi_forward >= 0),
"invalid index of " << toi_back
<< ". Note linked list reverse size is "
<< m_linked_list_reverse.size());
if (toi_back != detail::get_empty_id()) {
m_linked_list[toi_back] = toi_forward;
} else {
m_buckets[m_dirty_buckets[toi]] = toi_forward;
}
if (toi_forward != detail::get_empty_id()) {
m_linked_list_reverse[toi_forward] = toi_back;
}
}
} else {
m_deleted_buckets.resize(end_index_deleted - start_index_deleted);
detail::copy(m_dirty_buckets.begin() + start_index_deleted,
m_dirty_buckets.begin() + end_index_deleted,
m_deleted_buckets.begin());
detail::sort(m_deleted_buckets.begin(), m_deleted_buckets.end());
detail::for_each(
m_deleted_buckets.begin(),
detail::unique(m_deleted_buckets.begin(), m_deleted_buckets.end()),
delete_points_in_bucket_lambda(
start_index_deleted, end_index_deleted,
iterator_to_raw_pointer(m_linked_list.begin()),
iterator_to_raw_pointer(m_buckets.begin())));
}
// increase capacity of linked list vectors
m_linked_list.resize(n, detail::get_empty_id());
if (m_serial) {
m_linked_list_reverse.resize(n, detail::get_empty_id());
}
m_dirty_buckets.resize(n);
}
if (reset_domain) {
// resetting domain so need to insert all particles
insert_points(0, update_begin_index);
}
// then insert points that are still alive within update range
if (n_dead_in_update == 0) {
insert_points(update_begin_index, n_update);
} else {
insert_points(this->m_alive_indices.begin(), this->m_alive_indices.end(),
update_begin_index);
}
#ifndef __CUDA_ARCH__
if (4 <= ABORIA_LOG_LEVEL) {
LOG(4, "\tbuckets:");
for (size_t i = 0; i < m_buckets.size(); ++i) {
if (m_buckets[i] != detail::get_empty_id()) {
LOG(4, "\ti = " << i << " bucket contents = " << m_buckets[i]);
}
}
LOG(4, "\tend buckets");
LOG(4, "\tlinked list:");
for (size_t i = 0; i < m_linked_list.size(); ++i) {
if (m_serial) {
LOG(4, "\ti = " << i << " p = "
<< static_cast<const double_d &>(
get<position>(*(this->m_particles_begin + i)))
<< " contents = " << m_linked_list[i]
<< ". reverse = " << m_linked_list_reverse[i]);
} else {
LOG(4, "\ti = " << i << " p = "
<< static_cast<const double_d &>(
get<position>(*(this->m_particles_begin + i)))
<< " contents = " << m_linked_list[i]);
}
}
LOG(4, "\tend linked list:");
}
#endif
// check_data_structure();
this->m_query.m_linked_list_begin =
iterator_to_raw_pointer(this->m_linked_list.begin());
}
///
/// @brief insert non-consecutive points into data structure
///
/// @param start_adding iterator to the start of the list of indices to be
/// inserted
/// @param stop_adding iterator to the end of the list of indices to be
/// inserted
/// @param start the index of the particle set to start adding (the start of
/// the update range)
///
void insert_points(typename vector_int::iterator start_adding,
typename vector_int::iterator stop_adding,
const int start) {
const int n = stop_adding - start_adding;
#if defined(__CUDACC__)
typedef typename thrust::detail::iterator_category_to_system<
typename vector_int::iterator::iterator_category>::type system;
thrust::counting_iterator<int, system> count(0);
#else
auto count = Traits::make_counting_iterator(0);
#endif
if (m_serial) { // running in serial
detail::for_each(
count, count + n,
insert_points_lambda_non_sequential_serial(
iterator_to_raw_pointer(get<position>(this->m_particles_begin)),
iterator_to_raw_pointer(start_adding), m_point_to_bucket_index,
iterator_to_raw_pointer(m_buckets.begin()),
iterator_to_raw_pointer(m_dirty_buckets.begin()),
iterator_to_raw_pointer(m_linked_list.begin()),
iterator_to_raw_pointer(m_linked_list_reverse.begin()), start));
} else { // running in parallel
detail::for_each(
count, count + n,
insert_points_lambda_non_sequential(
iterator_to_raw_pointer(get<position>(this->m_particles_begin)),
iterator_to_raw_pointer(start_adding), m_point_to_bucket_index,
iterator_to_raw_pointer(m_buckets.begin()),
iterator_to_raw_pointer(m_dirty_buckets.begin()),
iterator_to_raw_pointer(m_linked_list.begin()), start));
}
}
void insert_points(const int start, const int n) {
#if defined(__CUDACC__)
typedef typename thrust::detail::iterator_category_to_system<
typename vector_int::iterator::iterator_category>::type system;
thrust::counting_iterator<int, system> count(0);
#else
auto count = Traits::make_counting_iterator(0);
#endif
if (m_serial) { // running in serial
detail::for_each(
count, count + n,
insert_points_lambda_sequential_serial(
iterator_to_raw_pointer(get<position>(this->m_particles_begin)),
m_point_to_bucket_index,
iterator_to_raw_pointer(m_buckets.begin()),
iterator_to_raw_pointer(m_dirty_buckets.begin()),
iterator_to_raw_pointer(m_linked_list.begin()),
iterator_to_raw_pointer(m_linked_list_reverse.begin()), start));
} else { // running in parallel
detail::for_each(
count, count + n,
insert_points_lambda_sequential(
iterator_to_raw_pointer(get<position>(this->m_particles_begin)),
m_point_to_bucket_index,
iterator_to_raw_pointer(m_buckets.begin()),
iterator_to_raw_pointer(m_dirty_buckets.begin()),
iterator_to_raw_pointer(m_linked_list.begin()), start));
}
}
///
/// @brief returns the query object via the base class
///
const CellListQuery<Traits> &get_query_impl() const { return m_query; }
///
/// @brief returns the query object via the base class
///
CellListQuery<Traits> &get_query_impl() { return m_query; }
///
/// @brief vector of the beginning nodes for each bucket linked list
///
vector_int m_buckets;
///
/// @brief vector of the linked list nodes for each particle
///
vector_int m_linked_list;
///
/// @brief vector of the backwards linked list nodes for each particle
///
vector_int m_linked_list_reverse;
///
/// @brief vector of which buckets have particles in them
///
vector_int m_dirty_buckets;
///
/// @brief internal storage for detecting which buckets have particles to be
/// deleted
///
vector_int m_deleted_buckets;
///
/// @brief internal storage for detecting which buckets have particles to be
/// copied
///
vector_int m_copied_buckets;
///
/// @brief the query object
///
CellListQuery<Traits> m_query;
///
/// @brief use m_dirty_buckets for efficiency
///
bool m_use_dirty_cells;
///
/// @brief last resizing of the buckets occurred with n particles
///
size_t m_size_calculated_with_n;
///
/// @brief running with multiple processes, or on gpu
///
bool m_serial;
///
/// @brief how many buckets in each dimension
///
unsigned_int_d m_size;
///
/// @brief length of buckets in each dimension
///
double_d m_bucket_side_length;
///
/// @brief struct to convert a point to a bucket index
///
detail::point_to_bucket_index<Traits::dimension> m_point_to_bucket_index;
};
///
/// @brief non-threadsafe function object to insert a non-consecutive list of
/// particles into
/// the data structure
///
/// @tparam Traits The @ref TraitsCommon type
///
template <typename Traits>
struct CellList<Traits>::insert_points_lambda_non_sequential_serial {
typedef typename Traits::double_d double_d;
typedef typename detail::point_to_bucket_index<Traits::dimension> ptobl_type;
double_d *m_positions;
int *m_alive_indices;
ptobl_type m_point_to_bucket_index;
int *m_buckets;
int *m_dirty_buckets;
int *m_linked_list;
int *m_linked_list_reverse;
int start;
///
/// @brief copy all the neccessary info into the function object
///
insert_points_lambda_non_sequential_serial(
double_d *m_positions, int *m_alive_indices,
const ptobl_type &m_point_to_bucket_index, int *m_buckets,
int *m_dirty_buckets, int *m_linked_list, int *m_linked_list_reverse,
int start)
: m_positions(m_positions), m_alive_indices(m_alive_indices),
m_point_to_bucket_index(m_point_to_bucket_index), m_buckets(m_buckets),
m_dirty_buckets(m_dirty_buckets), m_linked_list(m_linked_list),
m_linked_list_reverse(m_linked_list_reverse), start(start) {}
///
/// @brief insert a particle at index @p i into the data structure
///
/// It is assumed that this is run in serial (not thread-safe)
CUDA_HOST_DEVICE
void operator()(const int i) {
// use actual index to insert into ds
const int new_index = i + start;
// use m_alive_index to get position
const double_d &r = m_positions[m_alive_indices[i]];
const unsigned int bucketi = m_point_to_bucket_index.find_bucket_index(r);
const int bucket_entry = m_buckets[bucketi];
// Insert into own cell
m_buckets[bucketi] = new_index;
m_dirty_buckets[new_index] = bucketi;
m_linked_list[new_index] = bucket_entry;
m_linked_list_reverse[new_index] = detail::get_empty_id();
if (bucket_entry != detail::get_empty_id())
m_linked_list_reverse[bucket_entry] = new_index;
}
};
///
/// @brief non-threadsafe insert a consecutive vector of particles into the data
/// structure
///
/// @tparam Traits the @ref TraitsCommon type
///
template <typename Traits>
struct CellList<Traits>::insert_points_lambda_sequential_serial {
typedef typename Traits::double_d double_d;
typedef typename detail::point_to_bucket_index<Traits::dimension> ptobl_type;
double_d *m_positions;
ptobl_type m_point_to_bucket_index;
int *m_buckets;
int *m_dirty_buckets;
int *m_linked_list;
int *m_linked_list_reverse;
int start;
///
/// @brief copy in all the required info
///
insert_points_lambda_sequential_serial(
double_d *m_positions, const ptobl_type &m_point_to_bucket_index,
int *m_buckets, int *m_dirty_buckets, int *m_linked_list,
int *m_linked_list_reverse, int start)
: m_positions(m_positions),
m_point_to_bucket_index(m_point_to_bucket_index), m_buckets(m_buckets),
m_dirty_buckets(m_dirty_buckets), m_linked_list(m_linked_list),
m_linked_list_reverse(m_linked_list_reverse), start(start) {}
///
/// @brief insert a particle at index @p i into the data structure
///
/// It is assumed that this is run in serial (not thread-safe)
CUDA_HOST_DEVICE
void operator()(const int i) {
// use actual index to insert into ds
const int new_index = i + start;
const double_d &r = m_positions[new_index];
const unsigned int bucketi = m_point_to_bucket_index.find_bucket_index(r);
ASSERT_CUDA(bucketi < m_point_to_bucket_index.m_bucket_index.m_size.prod());
// std::cout << "inserting particle in index "<<new_index<<" at "<<r << "
// into bucket "<<bucketi<<std::endl;
const int bucket_entry = m_buckets[bucketi];
// Insert into own cell
m_buckets[bucketi] = new_index;
m_dirty_buckets[new_index] = bucketi;
m_linked_list[new_index] = bucket_entry;
m_linked_list_reverse[new_index] = detail::get_empty_id();
if (bucket_entry != detail::get_empty_id())
m_linked_list_reverse[bucket_entry] = new_index;
}
};
///
/// @brief thread-safe function object to insert a list of non-consecutive
/// particles into the data structure
///
/// @tparam Traits the @ref TraitsCommon type
///
template <typename Traits>
struct CellList<Traits>::insert_points_lambda_non_sequential {
typedef typename Traits::double_d double_d;
typedef typename detail::point_to_bucket_index<Traits::dimension> ptobl_type;
double_d *m_positions;
int *m_alive_indices;
ptobl_type m_point_to_bucket_index;
int *m_buckets;
int *m_dirty_buckets;
int *m_linked_list;
int start;
///
/// @brief copy all the required info
///
insert_points_lambda_non_sequential(double_d *m_positions,
int *m_alive_indices,
const ptobl_type &m_point_to_bucket_index,
int *m_buckets, int *m_dirty_buckets,
int *m_linked_list, int start)
: m_positions(m_positions), m_alive_indices(m_alive_indices),
m_point_to_bucket_index(m_point_to_bucket_index), m_buckets(m_buckets),
m_dirty_buckets(m_dirty_buckets), m_linked_list(m_linked_list),
start(start) {}
///
/// @brief insert a particle with index @p i into the data structure
///
/// implements a lock-free linked list using atomic CAS
///
CUDA_HOST_DEVICE
void operator()(const int i) {
// if (!m_alive[i]) return;
const int new_index = i + start;
const unsigned int bucketi = m_point_to_bucket_index.find_bucket_index(
m_positions[m_alive_indices[i]]);
// printf("insert_points_lambda: i = %d, bucketi = %d",i,bucketi);
// try inserting at head of the list
#if defined(__CUDA_ARCH__)
int next;
do {
next = m_buckets[bucketi];
} while (atomicCAS(m_buckets + bucketi, next, new_index) != new_index);
#else
int next = m_buckets[bucketi];
while (!__atomic_compare_exchange_n(m_buckets + bucketi, &next, new_index,
true, __ATOMIC_RELAXED,
__ATOMIC_RELAXED))
;
#endif
// successful
m_dirty_buckets[new_index] = bucketi;
m_linked_list[new_index] = next;
// m_linked_list_reverse[i] = detail::get_empty_id();
// if (next != detail::get_empty_id()) m_linked_list_reverse[next] = i;
}
};
///
/// @brief thread-safe function object to insert a list of consecutive
/// particles into the data structure
///
/// @tparam Traits the @ref TraitsCommon type
///
template <typename Traits>
struct CellList<Traits>::insert_points_lambda_sequential {
typedef typename Traits::double_d double_d;
typedef typename detail::point_to_bucket_index<Traits::dimension> ptobl_type;
double_d *m_positions;
ptobl_type m_point_to_bucket_index;
int *m_buckets;
int *m_dirty_buckets;
int *m_linked_list;
int start;
///
/// @brief copy all the info
///
insert_points_lambda_sequential(double_d *m_positions,
const ptobl_type &m_point_to_bucket_index,
int *m_buckets, int *m_dirty_buckets,
int *m_linked_list, int start)
: m_positions(m_positions),
m_point_to_bucket_index(m_point_to_bucket_index), m_buckets(m_buckets),
m_dirty_buckets(m_dirty_buckets), m_linked_list(m_linked_list),
start(start) {}
///
/// @brief insert the particle at index @p i
///
/// implements a lock-free linked list using atomic CAS
///
CUDA_HOST_DEVICE
void operator()(const int i) {
// if (!m_alive[i]) return;
const int new_index = i + start;
const unsigned int bucketi =
m_point_to_bucket_index.find_bucket_index(m_positions[new_index]);
// printf("insert_points_lambda: i = %d, bucketi = %d",i,bucketi);
// try inserting at head of the list
#if defined(__CUDA_ARCH__)
int next;
do {
next = m_buckets[bucketi];
} while (atomicCAS(m_buckets + bucketi, next, new_index) != new_index);
#else
int next = m_buckets[bucketi];
while (!__atomic_compare_exchange_n(m_buckets + bucketi, &next, new_index,
true, __ATOMIC_RELAXED,
__ATOMIC_RELAXED))
;
#endif
// successful
m_dirty_buckets[new_index] = bucketi;
m_linked_list[new_index] = next;
// m_linked_list_reverse[i] = detail::get_empty_id();
// if (next != detail::get_empty_id()) m_linked_list_reverse[next] = i;
}
};
///
/// @brief function object to delete a range of particles within the buckets
///
/// @tparam Traits the @ref TraitsCommon type
///
template <typename Traits>
struct CellList<Traits>::delete_points_in_bucket_lambda {
int *m_linked_list;
int *m_buckets;
int start_index_deleted;
int end_index_deleted;
delete_points_in_bucket_lambda(int start_index_deleted, int end_index_deleted,
int *m_linked_list, int *m_buckets)
: m_linked_list(m_linked_list), m_buckets(m_buckets),
start_index_deleted(start_index_deleted),
end_index_deleted(end_index_deleted) {}
///
/// @brief goes through all the particles in bucket @p celli and deletes
/// particles within the range given by m_start_index_deleted < i <
/// m_end_index_deleted
///
CUDA_HOST_DEVICE
void operator()(const int celli) {
// go through linked list
int i_minus_1 = detail::get_empty_id();
int i = m_buckets[celli];
while (i != detail::get_empty_id()) {
// unlink each contiguous deleted range
if (i >= start_index_deleted && i < end_index_deleted) {
// get first forward index not in range
do {
i = m_linked_list[i];
} while (i != detail::get_empty_id() && i >= start_index_deleted &&
i < end_index_deleted);
// update links
if (i_minus_1 != detail::get_empty_id()) {
m_linked_list[i_minus_1] = i;
} else {
m_buckets[celli] = i;
}
if (i == detail::get_empty_id()) {
break;
}
}
i_minus_1 = i;
i = m_linked_list[i];
}
}
};
///
/// @brief function object to copy a range of particles to another index range
///
/// @tparam Traits the @ref TraitsCommon type
///
template <typename Traits>
struct CellList<Traits>::copy_points_in_bucket_lambda {
int *m_linked_list;
int *m_buckets;
size_t start_index_deleted;
size_t start_index_copied;
size_t end_index_copied;
copy_points_in_bucket_lambda(size_t start_index_deleted,
size_t start_index_copied,
size_t end_index_copied, int *m_linked_list,
int *m_buckets)
: start_index_deleted(start_index_deleted),
start_index_copied(start_index_copied),
end_index_copied(end_index_copied), m_linked_list(m_linked_list),
m_buckets(m_buckets) {}
///
/// @brief goes through bucket @p celli and updates the particle indices to
/// point to the new range
///
CUDA_HOST_DEVICE
void operator()(const int celli) {
// go through linked list
int i_minus_1 = detail::get_empty_id();
int i = m_buckets[celli];
while (i != detail::get_empty_id()) {
// update each copied index
if (i >= start_index_copied && i < end_index_copied) {
int i_plus_1 = m_linked_list[i];
i = i - start_index_copied + start_index_deleted;
if (i_minus_1 != detail::get_empty_id()) {
m_linked_list[i_minus_1] = i;
} else {
m_buckets[celli] = i;
}
m_linked_list[i] = i_plus_1;
}
i_minus_1 = i;
i = m_linked_list[i];
}
}
};
/// @copydetails NeighbourQueryBase
///
/// @brief This is a query object for the CellList spatial data structure
///
template <typename Traits>
struct CellListQuery : public NeighbourQueryBase<Traits> {
typedef Traits traits_type;
typedef typename Traits::raw_pointer raw_pointer;
typedef typename Traits::double_d double_d;
typedef typename Traits::bool_d bool_d;
typedef typename Traits::int_d int_d;
typedef typename Traits::unsigned_int_d unsigned_int_d;
typedef typename Traits::reference particle_reference;
typedef typename Traits::const_reference particle_const_reference;
const static unsigned int dimension = Traits::dimension;
template <int LNormNumber, typename Transform = IdentityTransform>
using query_iterator =
lattice_iterator_within_distance<CellListQuery, LNormNumber, Transform>;
typedef lattice_iterator<dimension> all_iterator;
typedef lattice_iterator<dimension> child_iterator;
typedef typename query_iterator<2>::reference reference;
typedef typename query_iterator<2>::pointer pointer;
typedef typename query_iterator<2>::value_type value_type;
typedef linked_list_iterator<Traits> particle_iterator;
typedef bbox<dimension> box_type;
///
/// @brief periodicity of domain
///
bool_d m_periodic;
///
/// @brief bucket length in each dimension
///
double_d m_bucket_side_length;
///
/// @brief index of last bucket
///
int_d m_end_bucket;
///
/// @brief domain min/max bounds
///
bbox<dimension> m_bounds;
///
/// @brief function object to calculate a bucket index from a position
///
detail::point_to_bucket_index<dimension> m_point_to_bucket_index;
///
/// @brief pointer to the beginning of the particle set
///
raw_pointer m_particles_begin;
///
/// @brief pointer to the end of the particle set
///
raw_pointer m_particles_end;
///
/// @brief pointer to the beginning of the buckets
///
int *m_buckets_begin;
///
/// @brief pointer to the beginning of the linked list
///
int *m_linked_list_begin;
///
/// @brief pointer to the find-by-id map key
///
size_t *m_id_map_key;
///
/// @brief pointer to the find-by-id map value
///
size_t *m_id_map_value;
///
/// @brief constructor checks that we are not using std::vector and cuda
/// at the same time
///
ABORIA_HOST_DEVICE_IGNORE_WARN
CUDA_HOST_DEVICE
CellListQuery() {
#if defined(__CUDA_ARCH__)
CHECK_CUDA((!std::is_same<typename Traits::template vector<double>,
std::vector<double>>::value),
"Cannot use std::vector in device code");
#endif