-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathConnectivityMap.cc
More file actions
1155 lines (1045 loc) · 49.9 KB
/
ConnectivityMap.cc
File metadata and controls
1155 lines (1045 loc) · 49.9 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
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
//---------------------------------Spheral++----------------------------------//
// ConnectivityMap
//
// Stores the full set of significant neighbors for a set of NodeLists.
//
// Created by J. Michael Owen, Sun Oct 30 15:36:33 PST 2005
//----------------------------------------------------------------------------//
#include "ConnectivityMap.hh"
#include "NodeList/NodeList.hh"
#include "Neighbor/Neighbor.hh"
#include "DataBase/DataBase.hh"
#include "Field/FieldList.hh"
#include "Boundary/Boundary.hh"
#include "Utilities/globalNodeIDs.hh"
#include "Utilities/timingUtilities.hh"
#include "Utilities/mortonOrderIndices.hh"
#include "Utilities/PairComparisons.hh"
#include "Utilities/pointDistances.hh"
#include "Utilities/Timer.hh"
#include <algorithm>
#include <ctime>
using std::vector;
using std::map;
using std::string;
using std::pair;
namespace Spheral {
namespace {
//------------------------------------------------------------------------------
// Append v2 to the end of v1
//------------------------------------------------------------------------------
template<typename T>
inline
void
appendSTLvectors(std::vector<T>& v1, std::vector<T>& v2) {
if (not v2.empty()) {
v1.reserve(v1.size() + v2.size());
v1.insert(v1.end(), v2.begin(), v2.end());
}
}
//------------------------------------------------------------------------------
// How should we compare pairs for sorting?
//------------------------------------------------------------------------------
inline
KeyTraits::Key
hashKeys(const KeyTraits::Key& a, const KeyTraits::Key& b) {
// Szudzik's function
// return (a >= b ?
// a * a + a + b :
// a + b * b); // where a, b >= 0
// return a + b;
return ((KeyTraits::Key(a) << 16) | KeyTraits::Key(b));
}
//------------------------------------------------------------------------------
// Helper to insert into a sorted list of IDs.
//------------------------------------------------------------------------------
template<typename KeyContainer>
inline
bool
insertUnique(const std::vector<int>& offsets,
std::vector<std::vector<std::vector<int>>>& indices,
const KeyContainer& keys,
const bool useKeys,
const int jN1, const int j1,
const int jN2, const int j2) {
if (jN1 != jN2 or j1 != j2) {
auto& overlap = indices[offsets[jN1] + j1][jN2];
std::vector<int>::iterator itr;
if (useKeys) {
itr = std::lower_bound(overlap.begin(), overlap.end(), j2,
[&](const int a, const int& b) { return keys(jN2, a) < keys(jN2, b); });
} else {
itr = std::lower_bound(overlap.begin(), overlap.end(), j2);
}
if (itr == overlap.end() or *itr != j2) {
overlap.insert(itr, j2);
return true;
} else {
return false;
}
}
return false;
}
//------------------------------------------------------------------------------
// Sort node pairs for domain independent ordering.
//------------------------------------------------------------------------------
template<typename KeyContainer>
inline
void
sortPairs(NodePairList& pairs,
const KeyContainer& keys) {
// Start by making sure the pairs themselves are deterministically arranged.
for (auto& p: pairs) {
if (keys(p.i_list, p.i_node) > keys(p.j_list, p.j_node)) {
std::swap(p.i_list, p.j_list);
std::swap(p.i_node, p.j_node);
}
}
// Now sort the list as a whole.
std::sort(pairs.begin(), pairs.end(),
[&](const NodePairIdxType& a, const NodePairIdxType& b) {
return hashKeys(keys(a.i_list, a.i_node), keys(a.j_list, a.j_node)) <
hashKeys(keys(b.i_list, b.i_node), keys(b.j_list, b.j_node));
});
}
}
//------------------------------------------------------------------------------
// Constructor.
//------------------------------------------------------------------------------
template<typename Dimension>
inline
ConnectivityMap<Dimension>::
ConnectivityMap():
mNodeLists(),
mBuildGhostConnectivity(false),
mBuildOverlapConnectivity(false),
mBuildIntersectionConnectivity(false),
mConnectivity(),
mNodeTraversalIndices(),
mKeys(FieldStorageType::CopyFields),
mCouplingPtr(std::make_shared<NodeCoupling>()),
mIntersectionConnectivity() {
}
//------------------------------------------------------------------------------
// Destructor.
//------------------------------------------------------------------------------
template<typename Dimension>
ConnectivityMap<Dimension>::
~ConnectivityMap() {
}
//------------------------------------------------------------------------------
// Internal method to build the connectivity for the requested set of NodeLists.
//------------------------------------------------------------------------------
template<typename Dimension>
void
ConnectivityMap<Dimension>::
patchConnectivity(const FieldList<Dimension, size_t>& flags,
const FieldList<Dimension, size_t>& old2new) {
TIME_BEGIN("ConnectivityMap_patch");
const auto domainDecompIndependent = NodeListRegistrar<Dimension>::instance().domainDecompositionIndependent();
// We have to recompute the keys to sort nodes by excluding the
// nodes that are being removed.
const auto numNodeLists = mNodeLists.size();
if (domainDecompIndependent) {
// #pragma omp parallel for collapse(2)
for (auto iNodeList = 0u; iNodeList < numNodeLists; ++iNodeList) {
for (auto i = 0u; i < mNodeLists[iNodeList]->numNodes(); ++i) {
if (flags(iNodeList, i) == 0) mKeys(iNodeList, i) = KeyTraits::maxKey;
}
}
}
// Iterate over the Connectivity (NodeList).
for (auto iNodeList = 0u; iNodeList != numNodeLists; ++iNodeList) {
const auto ioff = mOffsets[iNodeList];
const auto numNodes = ((domainDecompIndependent or mBuildGhostConnectivity or mBuildOverlapConnectivity) ?
mNodeLists[iNodeList]->numNodes() :
mNodeLists[iNodeList]->numInternalNodes());
vector<size_t> iNodesToKill;
vector<pair<int, Key>> keys;
#pragma omp parallel
{
vector<size_t> iNodesToKill_thread;
vector<pair<int, Key>> keys_thread;
// Patch the traversal ordering and connectivity for this NodeList.
#pragma omp for schedule(dynamic)
for (auto i = 0u; i < numNodes; ++i) {
// Should we patch this set of neighbors?
if (flags(iNodeList, i) == 0) {
iNodesToKill_thread.push_back(i);
} else {
if (domainDecompIndependent) keys_thread.push_back(std::make_pair(old2new(iNodeList, i), mKeys(iNodeList, i)));
mNodeTraversalIndices[iNodeList][i] = old2new(iNodeList, i);
auto& neighbors = mConnectivity[ioff + i];
CHECK(neighbors.size() == numNodeLists);
for (auto jNodeList = 0u; jNodeList < numNodeLists; ++jNodeList) {
vector<pair<int, Key>> nkeys;
vector<size_t> jNodesToKill;
for (auto k = 0u; k < neighbors[jNodeList].size(); ++k) {
const auto j = neighbors[jNodeList][k];
if (flags(jNodeList, j) == 0) {
jNodesToKill.push_back(k);
} else {
if (domainDecompIndependent) nkeys.push_back(std::make_pair(old2new(jNodeList, j), mKeys(jNodeList, j)));
neighbors[jNodeList][k] = old2new(jNodeList, j);
}
}
removeElements(neighbors[jNodeList], jNodesToKill);
// Recompute the ordering of the neighbors.
if (domainDecompIndependent) {
sort(nkeys.begin(), nkeys.end(), ComparePairsBySecondElement<pair<int, Key> >());
for (size_t k = 0; k != neighbors[jNodeList].size(); ++k) {
CHECK2(k == 0 or nkeys[k].second > nkeys[k-1].second,
"Incorrect neighbor ordering: "
<< i << " "
<< k << " "
<< nkeys[k-1].second << " "
<< nkeys[k].second);
neighbors[jNodeList][k] = nkeys[k].first;
}
} else {
sort(neighbors[jNodeList].begin(), neighbors[jNodeList].end());
}
}
}
}
#pragma omp critical
appendSTLvectors(iNodesToKill, iNodesToKill_thread);
appendSTLvectors(keys, keys_thread);
}
removeElements(mNodeTraversalIndices[iNodeList], iNodesToKill);
// Recompute the ordering for traversing the nodes.
{
const auto numNodes = mNodeTraversalIndices[iNodeList].size();
if (domainDecompIndependent) {
// keys = vector<pair<int, Key> >();
// for (size_t k = 0; k != numNodes; ++k) {
// const int i = mNodeTraversalIndices[iNodeList][k];
// keys.push_back(std::make_pair(i, mKeys(iNodeList, i)));
// }
sort(keys.begin(), keys.end(), ComparePairsBySecondElement<pair<int, Key> >());
#pragma omp parallel for
for (auto k = 0u; k < numNodes; ++k) {
mNodeTraversalIndices[iNodeList][k] = keys[k].first;
}
} else {
#pragma omp parallel for
for (auto i = 0u; i < numNodes; ++i) {
mNodeTraversalIndices[iNodeList][i] = i;
}
}
}
}
// We also need to patch the node pair structure
// Note here we deliberately reallocate the NodePairList, which will invalidate any
// PairFields pointing at the original pairs.
REQUIRE(mNodePairListPtr);
NodePairList& currentPairs = *mNodePairListPtr;
std::vector<NodePairIdxType> culledPairs;
culledPairs.reserve(currentPairs.size());
#pragma omp parallel
{
std::vector<NodePairIdxType> culledPairs_thread;
const auto npairs = currentPairs.size();
culledPairs_thread.reserve(npairs);
#pragma omp for
for (auto k = 0u; k < npairs; ++k) {
const auto iNodeList = currentPairs[k].i_list;
const auto jNodeList = currentPairs[k].j_list;
const auto i = currentPairs[k].i_node;
const auto j = currentPairs[k].j_node;
if (flags(iNodeList, i) != 0 and flags(jNodeList, j) != 0) {
culledPairs_thread.push_back(NodePairIdxType(old2new(iNodeList, i), iNodeList,
old2new(jNodeList, j), jNodeList));
}
}
#pragma omp critical
{
culledPairs.insert(culledPairs.end(), culledPairs_thread.begin(), culledPairs_thread.end());
}
}
mNodePairListPtr = std::make_shared<NodePairList>(std::move(culledPairs));
// Sort the NodePairList in order to enforce domain decomposition independence.
{
auto& pairs = *mNodePairListPtr;
if (domainDecompIndependent) {
// sort(pairs.begin(), pairs.end(), [this](const NodePairIdxType& a, const NodePairIdxType& b) { return (mKeys(a.i_list, a.i_node) + mKeys(a.j_list, a.j_node)) < (mKeys(b.i_list, b.i_node) + mKeys(b.j_list, b.j_node)); });
// sort(pairs.begin(), pairs.end(), [this](const NodePairIdxType& a, const NodePairIdxType& b) { return hashKeys(mKeys(a.i_list, a.i_node), mKeys(a.j_list, a.j_node)) < hashKeys(mKeys(b.i_list, b.i_node), mKeys(b.j_list, b.j_node)); });
sortPairs(pairs, mKeys);
} else {
std::sort(pairs.begin(), pairs.end());
}
}
// mNodePairListPtr->computeLookup();
// Patch the intersection lists if we're maintaining them
if (mBuildIntersectionConnectivity) {
IntersectionConnectivityContainer intersection;
for (const auto& element: mIntersectionConnectivity) {
auto pair = element.first;
const auto& oldintersect = element.second;
if (flags(pair.i_list, pair.i_node) != 0 and flags(pair.j_list, pair.j_node) != 0) {
pair.i_node = old2new(pair.i_list, pair.i_node);
pair.j_node = old2new(pair.j_list, pair.j_node);
vector<vector<int>> newintersect(numNodeLists);
for (auto klist = 0u; klist < numNodeLists; ++klist) {
for (const auto k: oldintersect[klist]) {
if (flags(klist, k) != 0) newintersect[klist].push_back(old2new(klist, k));
}
}
intersection[pair] = newintersect;
}
}
}
// You can't check valid yet 'cause the NodeLists have not been resized
// when we call patch! The valid method should be checked by whoever called
// this method after that point.
//ENSURE(valid());
TIME_END("ConnectivityMap_patch");
}
//------------------------------------------------------------------------------
// Compute the common neighbors for a pair of nodes. Note this method
// returns by value since this information is not stored by ConnectivityMap.
//------------------------------------------------------------------------------
template<typename Dimension>
vector<vector<int> >
ConnectivityMap<Dimension>::
connectivityIntersectionForNodes(const int nodeListi, const int i,
const int nodeListj, const int j,
const FieldList<Dimension, typename Dimension::Vector>& position) const {
// Pre-conditions.
TIME_BEGIN("ConnectivityMap_computeIntersectionConnectivity");
const auto numNodeLists = mNodeLists.size();
const auto domainDecompIndependent = NodeListRegistrar<Dimension>::instance().domainDecompositionIndependent();
const auto ghostConnectivity = (mBuildGhostConnectivity or
mBuildOverlapConnectivity or
domainDecompIndependent);
const auto firstGhostNodei = mNodeLists[nodeListi]->firstGhostNode();
const auto firstGhostNodej = mNodeLists[nodeListj]->firstGhostNode();
const bool usePosition = (position.numFields() == numNodeLists);
REQUIRE(nodeListi < (int)numNodeLists and
nodeListj < (int)numNodeLists);
REQUIRE(ghostConnectivity or i < (int)firstGhostNodei or j < (int)firstGhostNodej);
REQUIRE(position.numFields() == numNodeLists or position.numFields() == 0);
// Prepare the result.
vector<vector<int>> result(numNodeLists);
// If both nodes are internal, we simply intersect their neighbor lists.
if (ghostConnectivity or (i < (int)firstGhostNodei and j < (int)firstGhostNodej)) {
const auto& neighborsi = this->connectivityForNode(nodeListi, i);
const auto& neighborsj = this->connectivityForNode(nodeListj, j);
CHECK(neighborsi.size() == numNodeLists);
CHECK(neighborsj.size() == numNodeLists);
vector<int> neighborsijk;
Vector posi, posj, b;
if (usePosition) {
posi = position(nodeListi, i);
posj = position(nodeListj, j);
}
for (auto klist = 0u; klist < numNodeLists; ++klist) {
neighborsijk.clear();
if (domainDecompIndependent) {
std::set_intersection(neighborsi[klist].begin(), neighborsi[klist].end(),
neighborsj[klist].begin(), neighborsj[klist].end(),
std::back_inserter(neighborsijk),
[&](const int a, const int b) { return mKeys(klist, a) < mKeys(klist, b); });
} else {
std::set_intersection(neighborsi[klist].begin(), neighborsi[klist].end(),
neighborsj[klist].begin(), neighborsj[klist].end(),
std::back_inserter(neighborsijk));
}
if (usePosition) {
std::copy_if(neighborsijk.begin(), neighborsijk.end(), std::back_inserter(result[klist]),
[&](int k) { return (closestPointOnSegment(position(klist, k), posi, posj, b)); });
} else {
result[klist] = neighborsijk;
}
}
} else if (i < (int)firstGhostNodei) {
result = this->connectivityForNode(nodeListi, i);
} else {
result = this->connectivityForNode(nodeListj, j);
}
result[nodeListi].push_back(i);
result[nodeListj].push_back(j);
// That's it.
TIME_END("ConnectivityMap_computeIntersectionConnectivity");
return result;
}
//------------------------------------------------------------------------------
// Remove connectivity between neighbors.
// NOTE: this method assumes you are passing the indices of the neighbors to
// remove!
//------------------------------------------------------------------------------
template<typename Dimension>
void
ConnectivityMap<Dimension>::
removeConnectivity(const FieldList<Dimension, vector<vector<int>>>& neighborsToCut) {
TIME_BEGIN("ConnectivityMap_cutConnectivity");
const auto numNodeLists = mNodeLists.size();
REQUIRE(neighborsToCut.numFields() == numNodeLists);
for (auto nodeListi = 0u; nodeListi < numNodeLists; ++nodeListi) {
const auto n = mNodeLists[nodeListi]->numNodes();
for (auto i = 0u; i < n; ++i) {
const auto& allneighbors = neighborsToCut(nodeListi, i);
CHECK(allneighbors.size() == 0 or allneighbors.size() == numNodeLists);
for (auto nodeListj = 0u; nodeListj < allneighbors.size(); ++nodeListj) {
auto& neighborsi = mConnectivity[mOffsets[nodeListi] + i][nodeListj];
removeElements(neighborsi, allneighbors[nodeListj]);
}
}
}
TIME_END("ConnectivityMap_cutConnectivity");
}
//------------------------------------------------------------------------------
// Compute the union of neighbors for a pair of nodes. Note this method
// returns by value since this information is not stored by ConnectivityMap.
//------------------------------------------------------------------------------
template<typename Dimension>
vector<vector<int> >
ConnectivityMap<Dimension>::
connectivityUnionForNodes(const int nodeListi, const int i,
const int nodeListj, const int j) const {
// Pre-conditions.
const unsigned numNodeLists = mNodeLists.size();
const auto domainDecompIndependent = NodeListRegistrar<Dimension>::instance().domainDecompositionIndependent();
const auto ghostConnectivity = (mBuildGhostConnectivity or
mBuildOverlapConnectivity or
domainDecompIndependent);
const unsigned firstGhostNodei = mNodeLists[nodeListi]->firstGhostNode();
const unsigned firstGhostNodej = mNodeLists[nodeListj]->firstGhostNode();
CONTRACT_VAR(ghostConnectivity);
CONTRACT_VAR(firstGhostNodei);
CONTRACT_VAR(firstGhostNodej);
REQUIRE(nodeListi < (int)numNodeLists and
nodeListj < (int)numNodeLists);
REQUIRE(ghostConnectivity or i < (int)firstGhostNodei or j < (int)firstGhostNodej);
// Do the deed.
vector<vector<int> > result(numNodeLists);
vector<vector<int> > neighborsi = this->connectivityForNode(nodeListi, i);
vector<vector<int> > neighborsj = this->connectivityForNode(nodeListj, j);
CHECK(neighborsi.size() == numNodeLists);
CHECK(neighborsj.size() == numNodeLists);
for (unsigned k = 0; k != numNodeLists; ++k) {
sort(neighborsi[k].begin(), neighborsi[k].end());
sort(neighborsj[k].begin(), neighborsj[k].end());
set_union(neighborsi[k].begin(), neighborsi[k].end(),
neighborsj[k].begin(), neighborsj[k].end(),
back_inserter(result[k]));
}
// That's it.
return result;
}
//------------------------------------------------------------------------------
// Return the connectivity in terms of global node IDs.
//------------------------------------------------------------------------------
template<typename Dimension>
map<int, vector<int> >
ConnectivityMap<Dimension>::
globalConnectivity(vector<Boundary<Dimension>*>& boundaries) const {
// Get the set of global node IDs.
auto globalIDs = globalNodeIDs<Dimension, typename vector<const NodeList<Dimension>*>::const_iterator>
(mNodeLists.begin(), mNodeLists.end());
// Make sure all ghost nodes have the appropriate global IDs.
for (typename vector<Boundary<Dimension>*>::iterator boundItr = boundaries.begin();
boundItr != boundaries.end();
++boundItr) (*boundItr)->applyFieldListGhostBoundary(globalIDs);
for (typename vector<Boundary<Dimension>*>::iterator boundItr = boundaries.begin();
boundItr != boundaries.end();
++boundItr) (*boundItr)->finalizeGhostBoundary();
// Now convert our connectivity to global IDs.
map<int, vector<int> > result;
const size_t numNodeLists = mNodeLists.size();
for (size_t nodeListi = 0; nodeListi != numNodeLists; ++nodeListi) {
const NodeList<Dimension>* nodeListPtr = mNodeLists[nodeListi];
for (auto i = 0u; i != nodeListPtr->numInternalNodes(); ++i) {
const int gid = globalIDs(nodeListi, i);
result[gid] = vector<int>();
const vector< vector<int> >& fullConnectivity = connectivityForNode(nodeListPtr, i);
CHECK(fullConnectivity.size() == numNodeLists);
for (size_t nodeListj = 0; nodeListj != numNodeLists; ++nodeListj) {
const vector<int>& connectivity = fullConnectivity[nodeListj];
for (typename vector<int>::const_iterator jItr = connectivity.begin();
jItr != connectivity.end();
++jItr) result[gid].push_back(globalIDs(nodeListj, *jItr));
}
ENSURE(result[gid].size() == numNeighborsForNode(nodeListPtr, i));
}
}
// That's it.
return result;
}
//------------------------------------------------------------------------------
// Compute the index for the given NodeList in our known set.
//------------------------------------------------------------------------------
template<typename Dimension>
unsigned
ConnectivityMap<Dimension>::
nodeListIndex(const NodeList<Dimension>* nodeListPtr) const {
return distance(mNodeLists.begin(),
find(mNodeLists.begin(), mNodeLists.end(), nodeListPtr));
}
//------------------------------------------------------------------------------
// Valid test.
//------------------------------------------------------------------------------
template<typename Dimension>
bool
ConnectivityMap<Dimension>::
valid() const {
TIME_BEGIN("ConnectivityMap_valid");
const auto domainDecompIndependent = NodeListRegistrar<Dimension>::instance().domainDecompositionIndependent();
const auto ghostConnectivity = (mBuildGhostConnectivity or
mBuildOverlapConnectivity or
domainDecompIndependent);
// Check the offsets.
const auto numNodeLists = mNodeLists.size();
if (mOffsets.size() != numNodeLists) {
cerr << "ConnectivityMap::valid: Failed mOffsets.size() == numNodeLists" << endl;
return false;
}
{
const auto numNodes = (ghostConnectivity ?
mNodeLists.back()->numNodes() :
mNodeLists.back()->numInternalNodes());
if (mConnectivity.size() != mOffsets.back() + numNodes) {
cerr << "ConnectivityMap::valid: Failed offset bounding: " << mConnectivity.size() << " != " << mOffsets.back() << " + " << numNodes << endl;
}
}
// Make sure that the NodeLists are listed in the correct sequence, and are
// FluidNodeLists.
{
const NodeListRegistrar<Dimension>& registrar = NodeListRegistrar<Dimension>::instance();
const vector<string> names = registrar.registeredNames();
int lastPosition = -1;
for (typename vector<const NodeList<Dimension>*>::const_iterator itr = mNodeLists.begin();
itr != mNodeLists.end();
++itr) {
const int newPosition = distance(names.begin(),
find(names.begin(), names.end(), (*itr)->name()));
if (newPosition <= lastPosition) {
cerr << "ConnectivityMap::valid: Failed ordering of NodeLists" << endl;
return false;
}
lastPosition = newPosition;
}
}
// Iterate over each NodeList entered.
for (auto nodeListIDi = 0u; nodeListIDi < numNodeLists; ++nodeListIDi) {
// Are all internal nodes represented?
const NodeList<Dimension>* nodeListPtri = mNodeLists[nodeListIDi];
const auto numNodes = (ghostConnectivity ?
nodeListPtri->numNodes() :
nodeListPtri->numInternalNodes());
//const int firstGhostNodei = nodeListPtri->firstGhostNode();
if (((nodeListIDi < numNodeLists - 1u) and ((mOffsets[nodeListIDi + 1] - mOffsets[nodeListIDi]) != (int)numNodes)) or
((nodeListIDi == numNodeLists - 1u) and ((mConnectivity.size() - (size_t)mOffsets[nodeListIDi]) != numNodes))) {
cerr << "ConnectivityMap::valid: Failed test that all nodes set for NodeList "
<< mNodeLists[nodeListIDi]->name()
<< endl;
return false;
}
// Iterate over the nodes for this NodeList.
const int ioff = mOffsets[nodeListIDi];
for (auto i = 0u; i < numNodes; ++i) {
// The set of neighbors for this node. This has to be sized as the number of
// NodeLists.
const vector< vector<int> >& allNeighborsForNode = mConnectivity[ioff + i];
if (allNeighborsForNode.size() != numNodeLists) {
cerr << "ConnectivityMap::valid: Failed allNeighborsForNode.size() == numNodeLists" << endl;
return false;
}
// Iterate over the sets of NodeList neighbors for this node.
for (auto nodeListIDj = 0u; nodeListIDj < numNodeLists; ++nodeListIDj) {
const NodeList<Dimension>* nodeListPtrj = mNodeLists[nodeListIDj];
//const int firstGhostNodej = nodeListPtrj->firstGhostNode();
const vector<int>& neighbors = allNeighborsForNode[nodeListIDj];
// We require that the node IDs be sorted, unique, and of course in a valid range.
if (neighbors.size() > 0) {
const auto minNeighbor = *min_element(neighbors.begin(), neighbors.end());
const auto maxNeighbor = *max_element(neighbors.begin(), neighbors.end());
if (minNeighbor < 0 or (size_t)maxNeighbor >= nodeListPtrj->numNodes()) {
cerr << "ConnectivityMap::valid: Failed test that neighbors must be valid IDs: " << minNeighbor << " " << maxNeighbor << " " << nodeListPtrj->numNodes() << endl;
return false;
}
// // When enforcing domain independence the ith node may be a ghost, but all of it's neighbors should
// // be internal.
// if (domainDecompIndependent and (i >= firstGhostNodei) and (maxNeighbor > firstGhostNodej)) {
// cerr << "ConnectivityMap::valid: Failed test that all neighbors of a ghost node should be internal." << endl;
// return false;
// }
for (auto k = 1u; k < neighbors.size(); ++k) {
if (domainDecompIndependent) {
// In the case of domain decomposition reproducibility, neighbors are sorted
// by hashed IDs.
if (mKeys(nodeListIDj, neighbors[k]) < mKeys(nodeListIDj, neighbors[k - 1])) {
cerr << "ConnectivityMap::valid: Failed test that neighbors must be sorted for node "
<< i << endl;
for (vector<int>::const_iterator itr = neighbors.begin();
itr != neighbors.end();
++itr) cerr << "(" << *itr << " " << mKeys(nodeListIDj, *itr) << ") ";
cerr << endl;
return false;
}
} else {
// Otherwise they should be sorted by local ID.
if (neighbors[k] <= neighbors[k - 1]) {
cerr << "ConnectivityMap::valid: Failed test that neighbors must be sorted" << endl;
for (vector<int>::const_iterator itr = neighbors.begin();
itr != neighbors.end();
++itr) cerr << " " << *itr;
cerr << endl;
return false;
}
}
}
}
// Check that the connectivity is symmetric.
for (auto j: neighbors) {
if (ghostConnectivity or ((size_t)j < nodeListPtrj->numInternalNodes())) {
const vector< vector<int> >& otherNeighbors = connectivityForNode(nodeListPtrj, j);
if (find(otherNeighbors[nodeListIDi].begin(),
otherNeighbors[nodeListIDi].end(),
i) == otherNeighbors[nodeListIDi].end()) {
cerr << "ConnectivityMap::valid: Failed test that neighbors must be symmetric: "
<< i << " <> " << j
<< " numneigbors(i)=" << neighbors.size()
<< " numneigbors(j)=" << otherNeighbors[nodeListIDi].size()
<< endl;
cerr << " " << i << " : ";
std::copy(neighbors.begin(), neighbors.end(), std::ostream_iterator<int>(std::cerr, " "));
cerr << endl
<< " " << j << " : ";
std::copy(otherNeighbors[nodeListIDi].begin(), otherNeighbors[nodeListIDi].end(), std::ostream_iterator<int>(std::cerr, " "));
cerr << endl;
return false;
}
}
}
}
}
}
// Check that if we are using domain decompostion independence then the keys
// have been calculated.
if (domainDecompIndependent) {
for (typename vector<const NodeList<Dimension>*>::const_iterator itr = mNodeLists.begin();
itr != mNodeLists.end();
++itr) {
if (not mKeys.haveNodeList(**itr)) {
cerr << "ConnectivityMap::valid: missing information from Keys." << endl;
return false;
}
}
}
// Make sure all nodes are listed in the node index traversal stuff.
if (mNodeTraversalIndices.size() != mNodeLists.size()) {
cerr << "ConnectivityMap::valid: mNodeTraversalIndices wrong size!" << endl;
return false;
}
for (auto nodeList = 0u; nodeList < numNodeLists; ++nodeList) {
const auto numExpected = domainDecompIndependent ? mNodeLists[nodeList]->numNodes() : mNodeLists[nodeList]->numInternalNodes();
bool ok = mNodeTraversalIndices[nodeList].size() == numExpected;
for (auto i = 0u; i < numExpected; ++i) {
ok = ok and (count(mNodeTraversalIndices[nodeList].begin(),
mNodeTraversalIndices[nodeList].end(),
i) == 1);
}
if (not ok) {
cerr << "ConnectivityMap::valid: mNodeTraversalIndices elements messed up!" << endl;
return false;
}
}
// // Check that the node traversal is ordered correctly.
// for (int nodeList = 0; nodeList != numNodeLists; ++nodeList) {
// if ((domainDecompIndependent and mNodeLists[nodeList]->numNodes() > 0) or
// (not domainDecompIndependent and mNodeLists[nodeList]->numInternalNodes() > 0)) {
// const int firstGhostNode = mNodeLists[nodeList]->firstGhostNode();
// for (const_iterator itr = begin(nodeList); itr < end(nodeList) - 1; ++itr) {
// if (not calculatePairInteraction(nodeList, *itr,
// nodeList, *(itr + 1),
// firstGhostNode)) {
// cerr << "ConnectivityMap::valid: mNodeTraversalIndices ordered incorrectly." << endl;
// cerr << *itr << " "
// << *(itr + 1) << " "
// << mKeys(nodeList, *itr) << " "
// << mKeys(nodeList, *(itr + 1)) << " "
// << mNodeLists[nodeList]->positions()(*itr) << " "
// << mNodeLists[nodeList]->positions()(*(itr + 1)) << " "
// << endl;
// for (int i = 0; i != 100; ++i) cerr << mKeys(nodeList, i) << " " << mNodeLists[nodeList]->positions()(i) << " ";
// cerr << endl;
// return false;
// }
// }
// }
// }
// Everything must be OK.
TIME_END("ConnectivityMap_valid");
return true;
}
//------------------------------------------------------------------------------
// Internal method to build the connectivity for the requested set of NodeLists.
//------------------------------------------------------------------------------
template<typename Dimension>
void
ConnectivityMap<Dimension>::
computeConnectivity() {
TIME_BEGIN("ConnectivityMap_computeConnectivity");
typedef typename Dimension::Vector Vector;
typedef typename Dimension::SymTensor SymTensor;
// Pre-conditions.
BEGIN_CONTRACT_SCOPE
{
for (typename vector<const NodeList<Dimension>*>::const_iterator itr = mNodeLists.begin();
itr != mNodeLists.end();
++itr) {
REQUIRE((**itr).neighbor().valid());
}
REQUIRE(mOffsets.size() == mNodeLists.size());
}
END_CONTRACT_SCOPE
const bool domainDecompIndependent = NodeListRegistrar<Dimension>::instance().domainDecompositionIndependent();
// std::clock_t tpre = std::clock();
// Do we need to build the ghost connectivity as well?
const auto ghostConnectivity = (mBuildGhostConnectivity or
mBuildOverlapConnectivity or
domainDecompIndependent);
// Build ourselves a temporary DataBase with the set of NodeLists.
// Simultaneously find the maximum kernel extent.
DataBase<Dimension> dataBase;
double kernelExtent = 0.0;
for (typename vector<const NodeList<Dimension>*>::const_iterator itr = mNodeLists.begin();
itr != mNodeLists.end();
++itr) {
dataBase.appendNodeList(const_cast<NodeList<Dimension>&>(**itr));
kernelExtent = max(kernelExtent, (**itr).neighbor().kernelExtent());
}
const double kernelExtent2 = kernelExtent*kernelExtent;
// Erase any prior information.
const unsigned numNodeLists = dataBase.numNodeLists(),
connectivitySize = mOffsets.back() + (ghostConnectivity ?
mNodeLists.back()->numNodes() :
mNodeLists.back()->numInternalNodes());
const bool ok = (connectivitySize > 0 and mConnectivity.size() == connectivitySize);
if (ok) {
CHECK(mNodeTraversalIndices.size() == numNodeLists);
for (typename ConnectivityStorageType::iterator itr = mConnectivity.begin();
itr != mConnectivity.end();
++itr) {
CHECK(itr->size() == numNodeLists);
for (unsigned k = 0; k != numNodeLists; ++k) {
(*itr)[k].clear();
}
}
} else {
mConnectivity = ConnectivityStorageType(connectivitySize, vector<vector<int> >(numNodeLists));
mNodeTraversalIndices = vector<vector<int> >(numNodeLists);
}
mIntersectionConnectivity.clear();
// If we're trying to be domain decomposition independent, we need a key to sort
// by that will give us a unique ordering regardless of position. The Morton ordered
// hash fills the bill.
using Key = typename KeyTraits::Key;
if (domainDecompIndependent) mKeys = mortonOrderIndices(dataBase);
// Fill in the ordering for walking the nodes.
CHECK(mNodeTraversalIndices.size() == numNodeLists);
if (domainDecompIndependent) {
for (auto iNodeList = 0u; iNodeList != numNodeLists; ++iNodeList) {
const NodeList<Dimension>& nodeList = *mNodeLists[iNodeList];
mNodeTraversalIndices[iNodeList].resize(nodeList.numNodes());
vector<pair<int, Key> > keys;
keys.reserve(nodeList.numNodes());
for (auto i = 0u; i != nodeList.numNodes(); ++i) keys.push_back(pair<int, Key>(i, mKeys(iNodeList, i)));
sort(keys.begin(), keys.end(), ComparePairsBySecondElement<pair<int, Key> >());
for (auto i = 0u; i != nodeList.numNodes(); ++i) mNodeTraversalIndices[iNodeList][i] = keys[i].first;
CHECK(mNodeTraversalIndices[iNodeList].size() == nodeList.numNodes());
// std::cerr << "Traversal: ";
// std::copy(mNodeTraversalIndices[iNodeList].begin(), mNodeTraversalIndices[iNodeList].end(), std::ostream_iterator<int>(std::cerr, " "));
// std::cerr << std::endl;
}
} else {
for (auto iNodeList = 0u; iNodeList != numNodeLists; ++iNodeList) {
const NodeList<Dimension>& nodeList = *mNodeLists[iNodeList];
mNodeTraversalIndices[iNodeList].resize(nodeList.numInternalNodes());
for (auto i = 0u; i != nodeList.numInternalNodes(); ++i) mNodeTraversalIndices[iNodeList][i] = i;
}
}
// Create a list of flags to keep track of which nodes have been completed thus far.
FieldList<Dimension, int> flagNodeDone = dataBase.newGlobalFieldList(int());
flagNodeDone = 0;
// Get the position and H fields.
const FieldList<Dimension, Vector> position = dataBase.globalPosition();
const FieldList<Dimension, SymTensor> H = dataBase.globalHfield();
// Iterate over the NodeLists.
// std::clock_t t0,
// tmaster = std::clock_t(0),
// trefine = std::clock_t(0),
// twalk = std::clock_t(0);
std::vector<NodePairIdxType> nodePairs;
if (mNodePairListPtr) {
nodePairs.reserve(mNodePairListPtr->size());
}
CHECK(mConnectivity.size() == connectivitySize);
for (auto iiNodeList = 0u; iiNodeList < numNodeLists; ++iiNodeList) {
const auto etaMax = mNodeLists[iiNodeList]->neighbor().kernelExtent();
// Iterate over the nodes in this NodeList, and look for any that are not done yet.
const auto nii = (ghostConnectivity ?
mNodeLists[iiNodeList]->numNodes() :
mNodeLists[iiNodeList]->numInternalNodes());
for (auto ii = 0u; ii < nii; ++ii) {
if (flagNodeDone(iiNodeList, ii) == 0) {
// Set the master nodes.
// t0 = std::clock();
vector<vector<int>> masterLists, coarseNeighbors;
Neighbor<Dimension>::setMasterNeighborGroup(position(iiNodeList, ii),
H(iiNodeList, ii),
mNodeLists.begin(),
mNodeLists.end(),
etaMax,
masterLists,
coarseNeighbors,
ghostConnectivity);
// Iterate over the full of NodeLists again to work on the master nodes.
for (auto iNodeList = 0u; iNodeList != numNodeLists; ++iNodeList) {
const auto nmaster = masterLists[iNodeList].size();
#pragma omp parallel
{
std::vector<NodePairIdxType> nodePairs_private;
#pragma omp for schedule(dynamic)
for (auto k = 0u; k < nmaster; ++k) {
const auto i = masterLists[iNodeList][k];
CHECK2(flagNodeDone(iNodeList, i) == 0, "(" << iNodeList << " " << i << ")");
// Get the state for this node.
const auto& ri = position(iNodeList, i);
const auto& Hi = H(iNodeList, i);
auto& worki = mNodeLists[iNodeList]->work();
CHECK2(mOffsets[iNodeList] + i < (int)mConnectivity.size(),
iNodeList << " " << i << " " << mOffsets[iNodeList] << " " << mConnectivity.size());
const auto start = Timing::currentTime();
// Get the neighbor set we're building for this node.
auto& neighbors = mConnectivity[mOffsets[iNodeList] + i];
CHECK2(neighbors.size() == numNodeLists, neighbors.size() << " " << numNodeLists << " " << i);
// We keep track of the Morton indices.
vector<vector<pair<int, Key>>> keys(numNodeLists);
// Iterate over the neighbor NodeLists.
for (auto jNodeList = 0u; jNodeList != numNodeLists; ++jNodeList) {
const auto firstGhostNodej = mNodeLists[jNodeList]->firstGhostNode();
// Iterate over the coarse neighbors in this NodeList.
// t0 = std::clock();
for (const auto j: coarseNeighbors[jNodeList]) {
const auto& rj = position(jNodeList, j);
const auto& Hj = H(jNodeList, j);
// Compute the normalized distance between this pair.
const auto rij = ri - rj;
const auto eta2i = (Hi*rij).magnitude2();
const auto eta2j = (Hj*rij).magnitude2();
// If this pair is significant, add it to the list.
if (eta2i <= kernelExtent2 or eta2j <= kernelExtent2) {
// We don't include self-interactions.
if ((iNodeList != jNodeList) or (i != j)) {
neighbors[jNodeList].push_back(j);
CHECK2(neighbors[jNodeList].size() < 10000u, "Too many neighbors: check H");
if (calculatePairInteraction(iNodeList, i, jNodeList, j, firstGhostNodej)) nodePairs_private.push_back(NodePairIdxType(i, iNodeList, j, jNodeList));
if (domainDecompIndependent) keys[jNodeList].push_back(pair<int, Key>(j, mKeys(jNodeList, j)));
}
}
}
// twalk += std::clock() - t0;
}
CHECK(neighbors.size() == numNodeLists);
CHECK(keys.size() == numNodeLists);
// We have a few options for how to order the neighbors for this node.
for (auto jNodeList = 0u; jNodeList != numNodeLists; ++jNodeList) {
if (domainDecompIndependent) {
// Sort in a domain independent manner.
CHECK(keys[jNodeList].size() == neighbors[jNodeList].size());
sort(keys[jNodeList].begin(), keys[jNodeList].end(), ComparePairsBySecondElement<pair<int, Key>>());
for (auto j = 0u; j != neighbors[jNodeList].size(); ++j) neighbors[jNodeList][j] = keys[jNodeList][j].first;
} else {
// Sort in an attempt to be cache friendly.
sort(neighbors[jNodeList].begin(), neighbors[jNodeList].end());
}
}
// Flag this master node as done.
flagNodeDone(iNodeList, i) = 1;
worki(i) += Timing::difference(start, Timing::currentTime());
}
// Merge the NodePairList
#pragma omp critical
nodePairs.insert(nodePairs.end(), nodePairs_private.begin(), nodePairs_private.end());
} // end OMP parallel
}
}
}
}
mNodePairListPtr = std::make_shared<NodePairList>(std::move(nodePairs));
// // If necessary add ghost->internal connectivity.
// if (ghostConnectivity) {
// for (auto iNodeList = 0; iNodeList < numNodeLists; ++iNodeList) {
// for (auto i = 0; i < mNodeLists[iNodeList]->numInternalNodes(); ++i) {
// const auto& neighborsi = mConnectivity[mOffsets[iNodeList] + i];
// CHECK(neighborsi.size() == numNodeLists);
// for (auto jNodeList = 0; jNodeList < numNodeLists; ++jNodeList) {
// const auto firstGhostNodej = mNodeLists[jNodeList]->firstGhostNode();
// for (auto jItr = neighborsi[jNodeList].begin(); jItr < neighborsi[jNodeList].end(); ++jItr) {
// const auto j = *jItr;
// if (j >= firstGhostNodej) {
// auto& neighborsj = mConnectivity[mOffsets[jNodeList] + j];
// CHECK(neighborsj.size() == numNodeLists);
// neighborsj[iNodeList].push_back(i);
// // mNodePairListPtr->push_back(NodePairIdxType(i, iNodeList, j, jNodeList));
// }
// }
// }
// }
// }
// // Flag ghost nodes as done if at least one neighbor was found
// for (auto iNodeList = 0; iNodeList < numNodeLists; ++iNodeList) {
// for (auto i = mNodeLists[iNodeList]->numInternalNodes(); i < mNodeLists[iNodeList]->numNodes(); ++i) {
// const auto& neighborsi = mConnectivity[mOffsets[iNodeList] + i];
// if (neighborsi.size() > 0) {
// flagNodeDone(iNodeList, i) = 1;
// }
// }
// }
// }
// In the domain decompostion independent case, we need to sort the neighbors for ghost
// nodes as well.