-
Notifications
You must be signed in to change notification settings - Fork 849
/
Copy pathCGeometry.cpp
4110 lines (3123 loc) · 152 KB
/
CGeometry.cpp
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
/*!
* \file CGeometry.cpp
* \brief Implementation of the base geometry class.
* \author F. Palacios, T. Economon
* \version 7.5.1 "Blackbird"
*
* SU2 Project Website: https://su2code.github.io
*
* The SU2 Project is maintained by the SU2 Foundation
* (http://su2foundation.org)
*
* Copyright 2012-2023, SU2 Contributors (cf. AUTHORS.md)
*
* SU2 is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* SU2 is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with SU2. If not, see <http://www.gnu.org/licenses/>.
*/
#include <unordered_set>
#include "../../include/geometry/CGeometry.hpp"
#include "../../include/geometry/elements/CElement.hpp"
#include "../../include/parallelization/omp_structure.hpp"
#include "../../include/toolboxes/geometry_toolbox.hpp"
#include "../../include/toolboxes/ndflattener.hpp"
CGeometry::CGeometry(void) :
size(SU2_MPI::GetSize()),
rank(SU2_MPI::GetRank()) {
}
CGeometry::~CGeometry(void) {
unsigned long iElem, iElem_Bound, iVertex;
unsigned short iMarker;
if (elem != nullptr) {
for (iElem = 0; iElem < nElem; iElem++)
delete elem[iElem];
delete[] elem;
}
if (bound != nullptr) {
for (iMarker = 0; iMarker < nMarker; iMarker++) {
for (iElem_Bound = 0; iElem_Bound < nElem_Bound[iMarker]; iElem_Bound++) {
delete bound[iMarker][iElem_Bound];
}
delete [] bound[iMarker];
}
delete [] bound;
}
delete nodes;
delete edges;
if (vertex != nullptr) {
for (iMarker = 0; iMarker < nMarker; iMarker++) {
for (iVertex = 0; iVertex < nVertex[iMarker]; iVertex++) {
delete vertex[iMarker][iVertex];
}
delete [] vertex[iMarker];
}
delete [] vertex;
}
delete [] nElem_Bound;
delete [] nVertex;
delete [] Marker_All_SendRecv;
delete [] Tag_to_Marker;
delete [] beg_node;
delete [] end_node;
delete [] nPointLinear;
delete [] nPointCumulative;
if(CustomBoundaryHeatFlux != nullptr){
for(iMarker=0; iMarker < nMarker; iMarker++){
delete [] CustomBoundaryHeatFlux[iMarker];
}
delete [] CustomBoundaryHeatFlux;
}
if(CustomBoundaryTemperature != nullptr){
for(iMarker=0; iMarker < nMarker; iMarker++){
delete [] CustomBoundaryTemperature[iMarker];
}
delete [] CustomBoundaryTemperature;
}
/*--- Delete structures for MPI point-to-point communication. ---*/
delete [] bufD_P2PRecv;
delete [] bufD_P2PSend;
delete [] bufS_P2PRecv;
delete [] bufS_P2PSend;
delete [] req_P2PSend;
delete [] req_P2PRecv;
delete [] nPoint_P2PRecv;
delete [] nPoint_P2PSend;
delete [] Neighbors_P2PSend;
delete [] Neighbors_P2PRecv;
delete [] Local_Point_P2PSend;
delete [] Local_Point_P2PRecv;
/*--- Delete structures for MPI periodic communication. ---*/
delete [] bufD_PeriodicRecv;
delete [] bufD_PeriodicSend;
delete [] bufS_PeriodicRecv;
delete [] bufS_PeriodicSend;
delete [] req_PeriodicSend;
delete [] req_PeriodicRecv;
delete [] nPoint_PeriodicRecv;
delete [] nPoint_PeriodicSend;
delete [] Neighbors_PeriodicSend;
delete [] Neighbors_PeriodicRecv;
delete [] Local_Point_PeriodicSend;
delete [] Local_Point_PeriodicRecv;
delete [] Local_Marker_PeriodicSend;
delete [] Local_Marker_PeriodicRecv;
}
void CGeometry::PreprocessP2PComms(CGeometry *geometry,
CConfig *config) {
/*--- We start with the send and receive lists already available in
the form of SEND_RECEIVE boundary markers. We will loop through
these markers and establish the neighboring ranks and number of
send/recv points per pair. We will store this information and set
up persistent data structures so that we can reuse them throughout
the calculation for any point-to-point communications. The goal
is to break the non-blocking comms into InitiateComms() and
CompleteComms() in separate routines so that we can overlap the
communication and computation to hide the communication latency. ---*/
/*--- Local variables. ---*/
unsigned short iMarker;
unsigned long nVertexS, nVertexR, iVertex, MarkerS, MarkerR;
int iRank, iSend, iRecv, count;
/*--- Create some temporary structures for tracking sends/recvs. ---*/
int *nPoint_Send_All = new int[size+1]; nPoint_Send_All[0] = 0;
int *nPoint_Recv_All = new int[size+1]; nPoint_Recv_All[0] = 0;
int *nPoint_Flag = new int[size];
for (iRank = 0; iRank < size; iRank++) {
nPoint_Send_All[iRank] = 0; nPoint_Recv_All[iRank] = 0; nPoint_Flag[iRank]= -1;
}
nPoint_Send_All[size] = 0; nPoint_Recv_All[size] = 0;
/*--- Loop through all of our SEND_RECEIVE markers and track
our sends with each rank. ---*/
for (iMarker = 0; iMarker < config->GetnMarker_All(); iMarker++) {
if ((config->GetMarker_All_KindBC(iMarker) == SEND_RECEIVE) &&
(config->GetMarker_All_SendRecv(iMarker) > 0)) {
/*--- Get the destination rank and number of points to send. ---*/
iRank = config->GetMarker_All_SendRecv(iMarker)-1;
nVertexS = geometry->nVertex[iMarker];
/*--- If we have not visited this element yet, increment our
number of elements that must be sent to a particular proc. ---*/
if ((nPoint_Flag[iRank] != (int)iMarker)) {
nPoint_Flag[iRank] = (int)iMarker;
nPoint_Send_All[iRank+1] += nVertexS;
}
}
}
delete [] nPoint_Flag;
/*--- Communicate the number of points to be sent/recv'd amongst
all processors. After this communication, each proc knows how
many cells it will receive from each other processor. ---*/
SU2_MPI::Alltoall(&(nPoint_Send_All[1]), 1, MPI_INT,
&(nPoint_Recv_All[1]), 1, MPI_INT, SU2_MPI::GetComm());
/*--- Prepare to send connectivities. First check how many
messages we will be sending and receiving. Here we also put
the counters into cumulative storage format to make the
communications simpler. ---*/
nP2PSend = 0; nP2PRecv = 0;
for (iRank = 0; iRank < size; iRank++) {
if ((iRank != rank) && (nPoint_Send_All[iRank+1] > 0)) nP2PSend++;
if ((iRank != rank) && (nPoint_Recv_All[iRank+1] > 0)) nP2PRecv++;
nPoint_Send_All[iRank+1] += nPoint_Send_All[iRank];
nPoint_Recv_All[iRank+1] += nPoint_Recv_All[iRank];
}
/*--- Allocate only as much memory as we need for the P2P neighbors. ---*/
nPoint_P2PSend = new int[nP2PSend+1]; nPoint_P2PSend[0] = 0;
nPoint_P2PRecv = new int[nP2PRecv+1]; nPoint_P2PRecv[0] = 0;
Neighbors_P2PSend = new int[nP2PSend];
Neighbors_P2PRecv = new int[nP2PRecv];
iSend = 0; iRecv = 0;
for (iRank = 0; iRank < size; iRank++) {
if ((nPoint_Send_All[iRank+1] > nPoint_Send_All[iRank]) && (iRank != rank)) {
Neighbors_P2PSend[iSend] = iRank;
nPoint_P2PSend[iSend+1] = nPoint_Send_All[iRank+1];
iSend++;
}
if ((nPoint_Recv_All[iRank+1] > nPoint_Recv_All[iRank]) && (iRank != rank)) {
Neighbors_P2PRecv[iRecv] = iRank;
nPoint_P2PRecv[iRecv+1] = nPoint_Recv_All[iRank+1];
iRecv++;
}
}
/*--- Create a reverse mapping of the message to the rank so that we
can quickly access the correct data in the buffers when receiving
messages dynamically. ---*/
P2PSend2Neighbor.clear();
for (iSend = 0; iSend < nP2PSend; iSend++)
P2PSend2Neighbor[Neighbors_P2PSend[iSend]] = iSend;
P2PRecv2Neighbor.clear();
for (iRecv = 0; iRecv < nP2PRecv; iRecv++)
P2PRecv2Neighbor[Neighbors_P2PRecv[iRecv]] = iRecv;
delete [] nPoint_Send_All;
delete [] nPoint_Recv_All;
/*--- Allocate the memory that we need for receiving the conn
values and then cue up the non-blocking receives. Note that
we do not include our own rank in the communications. We will
directly copy our own data later. ---*/
Local_Point_P2PSend = nullptr;
Local_Point_P2PSend = new unsigned long[nPoint_P2PSend[nP2PSend]];
for (iSend = 0; iSend < nPoint_P2PSend[nP2PSend]; iSend++)
Local_Point_P2PSend[iSend] = 0;
Local_Point_P2PRecv = nullptr;
Local_Point_P2PRecv = new unsigned long[nPoint_P2PRecv[nP2PRecv]];
for (iRecv = 0; iRecv < nPoint_P2PRecv[nP2PRecv]; iRecv++)
Local_Point_P2PRecv[iRecv] = 0;
/*--- We allocate the memory for communicating values in a later step
once we know the maximum packet size that we need to communicate. This
memory is deallocated and reallocated automatically in the case that
the previously allocated memory is not sufficient. ---*/
bufD_P2PSend = nullptr;
bufD_P2PRecv = nullptr;
bufS_P2PSend = nullptr;
bufS_P2PRecv = nullptr;
/*--- Allocate memory for the MPI requests if we need to communicate. ---*/
if (nP2PSend > 0) {
req_P2PSend = new SU2_MPI::Request[nP2PSend];
}
if (nP2PRecv > 0) {
req_P2PRecv = new SU2_MPI::Request[nP2PRecv];
}
/*--- Build lists of local index values for send. ---*/
count = 0;
for (iSend = 0; iSend < nP2PSend; iSend++) {
for (iMarker = 0; iMarker < config->GetnMarker_All(); iMarker++) {
if ((config->GetMarker_All_KindBC(iMarker) == SEND_RECEIVE) &&
(config->GetMarker_All_SendRecv(iMarker) > 0)) {
MarkerS = iMarker;
nVertexS = geometry->nVertex[MarkerS];
iRank = config->GetMarker_All_SendRecv(MarkerS)-1;
if (iRank == Neighbors_P2PSend[iSend]) {
for (iVertex = 0; iVertex < nVertexS; iVertex++) {
Local_Point_P2PSend[count] = geometry->vertex[MarkerS][iVertex]->GetNode();
count++;
}
}
}
}
}
/*--- Build lists of local index values for receive. ---*/
count = 0;
for (iRecv = 0; iRecv < nP2PRecv; iRecv++) {
for (iMarker = 0; iMarker < config->GetnMarker_All(); iMarker++) {
if ((config->GetMarker_All_KindBC(iMarker) == SEND_RECEIVE) &&
(config->GetMarker_All_SendRecv(iMarker) > 0)) {
MarkerR = iMarker+1;
nVertexR = geometry->nVertex[MarkerR];
iRank = abs(config->GetMarker_All_SendRecv(MarkerR))-1;
if (iRank == Neighbors_P2PRecv[iRecv]) {
for (iVertex = 0; iVertex < nVertexR; iVertex++) {
Local_Point_P2PRecv[count] = geometry->vertex[MarkerR][iVertex]->GetNode();
count++;
}
}
}
}
}
/*--- In the future, some additional data structures could be created
here to separate the interior and boundary nodes in order to help
further overlap computation and communication. ---*/
}
void CGeometry::AllocateP2PComms(unsigned short countPerPoint) {
/*--- This routine is activated whenever we attempt to perform
a point-to-point MPI communication with our neighbors but the
memory buffer allocated is not large enough for the packet size.
Therefore, we deallocate the previously allocated space and
reallocate a large enough array. Note that after the first set
communications, this routine will not need to be called again. ---*/
if (countPerPoint <= maxCountPerPoint) return;
BEGIN_SU2_OMP_SAFE_GLOBAL_ACCESS {
/*--- Store the larger packet size to the class data. ---*/
maxCountPerPoint = countPerPoint;
/*-- Deallocate and reallocate our su2double cummunication memory. ---*/
delete [] bufD_P2PSend;
bufD_P2PSend = new su2double[maxCountPerPoint*nPoint_P2PSend[nP2PSend]] ();
delete [] bufD_P2PRecv;
bufD_P2PRecv = new su2double[maxCountPerPoint*nPoint_P2PRecv[nP2PRecv]] ();
delete [] bufS_P2PSend;
bufS_P2PSend = new unsigned short[maxCountPerPoint*nPoint_P2PSend[nP2PSend]] ();
delete [] bufS_P2PRecv;
bufS_P2PRecv = new unsigned short[maxCountPerPoint*nPoint_P2PRecv[nP2PRecv]] ();
}
END_SU2_OMP_SAFE_GLOBAL_ACCESS
}
void CGeometry::PostP2PRecvs(CGeometry *geometry,
const CConfig *config,
unsigned short commType,
unsigned short countPerPoint,
bool val_reverse) const {
/*--- Launch the non-blocking recv's first. Note that we have stored
the counts and sources, so we can launch these before we even load
the data and send from the neighbor ranks. ---*/
SU2_OMP_MASTER
for (int iRecv = 0; iRecv < nP2PRecv; iRecv++) {
const auto iMessage = iRecv;
/*--- In some instances related to the adjoint solver, we need
to reverse the direction of communications such that the normal
send nodes become the recv nodes and vice-versa. ---*/
if (val_reverse) {
/*--- Compute our location in the buffer using the send data
structure since we are reversing the comms. ---*/
auto offset = countPerPoint*nPoint_P2PSend[iRecv];
/*--- Take advantage of cumulative storage format to get the number
of elems that we need to recv. Note again that we select the send
points here as the recv points. ---*/
auto nPointP2P = nPoint_P2PSend[iRecv+1] - nPoint_P2PSend[iRecv];
/*--- Total count can include multiple pieces of data per element. ---*/
auto count = countPerPoint*nPointP2P;
/*--- Get the rank from which we receive the message. Note again
that we use the send rank as the source instead of the recv rank. ---*/
auto source = Neighbors_P2PSend[iRecv];
auto tag = source + 1;
/*--- Post non-blocking recv for this proc. Note that we use the
send buffer here too. This is important to make sure the arrays
are the correct size. ---*/
switch (commType) {
case COMM_TYPE_DOUBLE:
SU2_MPI::Irecv(&(bufD_P2PSend[offset]), count, MPI_DOUBLE,
source, tag, SU2_MPI::GetComm(), &(req_P2PRecv[iRecv]));
break;
case COMM_TYPE_UNSIGNED_SHORT:
SU2_MPI::Irecv(&(bufS_P2PSend[offset]), count, MPI_UNSIGNED_SHORT,
source, tag, SU2_MPI::GetComm(), &(req_P2PRecv[iRecv]));
break;
default:
SU2_MPI::Error("Unrecognized data type for point-to-point MPI comms.",
CURRENT_FUNCTION);
break;
}
} else {
/*--- Compute our location in the recv buffer. ---*/
auto offset = countPerPoint*nPoint_P2PRecv[iRecv];
/*--- Take advantage of cumulative storage format to get the number
of elems that we need to recv. ---*/
auto nPointP2P = nPoint_P2PRecv[iRecv+1] - nPoint_P2PRecv[iRecv];
/*--- Total count can include multiple pieces of data per element. ---*/
auto count = countPerPoint*nPointP2P;
/*--- Get the rank from which we receive the message. ---*/
auto source = Neighbors_P2PRecv[iRecv];
auto tag = source + 1;
/*--- Post non-blocking recv for this proc. ---*/
switch (commType) {
case COMM_TYPE_DOUBLE:
SU2_MPI::Irecv(&(bufD_P2PRecv[offset]), count, MPI_DOUBLE,
source, tag, SU2_MPI::GetComm(), &(req_P2PRecv[iMessage]));
break;
case COMM_TYPE_UNSIGNED_SHORT:
SU2_MPI::Irecv(&(bufS_P2PRecv[offset]), count, MPI_UNSIGNED_SHORT,
source, tag, SU2_MPI::GetComm(), &(req_P2PRecv[iMessage]));
break;
default:
SU2_MPI::Error("Unrecognized data type for point-to-point MPI comms.",
CURRENT_FUNCTION);
break;
}
}
}
END_SU2_OMP_MASTER
}
void CGeometry::PostP2PSends(CGeometry *geometry,
const CConfig *config,
unsigned short commType,
unsigned short countPerPoint,
int val_iSend,
bool val_reverse) const {
/*--- Post the non-blocking send as soon as the buffer is loaded. ---*/
/*--- In some instances related to the adjoint solver, we need
to reverse the direction of communications such that the normal
send nodes become the recv nodes and vice-versa. ---*/
SU2_OMP_MASTER
if (val_reverse) {
/*--- Compute our location in the buffer using the recv data
structure since we are reversing the comms. ---*/
auto offset = countPerPoint*nPoint_P2PRecv[val_iSend];
/*--- Take advantage of cumulative storage format to get the number
of points that we need to send. Note again that we select the recv
points here as the send points. ---*/
auto nPointP2P = nPoint_P2PRecv[val_iSend+1] - nPoint_P2PRecv[val_iSend];
/*--- Total count can include multiple pieces of data per element. ---*/
auto count = countPerPoint*nPointP2P;
/*--- Get the rank to which we send the message. Note again
that we use the recv rank as the dest instead of the send rank. ---*/
auto dest = Neighbors_P2PRecv[val_iSend];
auto tag = rank + 1;
/*--- Post non-blocking send for this proc. Note that we use the
send buffer here too. This is important to make sure the arrays
are the correct size. ---*/
switch (commType) {
case COMM_TYPE_DOUBLE:
SU2_MPI::Isend(&(bufD_P2PRecv[offset]), count, MPI_DOUBLE,
dest, tag, SU2_MPI::GetComm(), &(req_P2PSend[val_iSend]));
break;
case COMM_TYPE_UNSIGNED_SHORT:
SU2_MPI::Isend(&(bufS_P2PRecv[offset]), count, MPI_UNSIGNED_SHORT,
dest, tag, SU2_MPI::GetComm(), &(req_P2PSend[val_iSend]));
break;
default:
SU2_MPI::Error("Unrecognized data type for point-to-point MPI comms.",
CURRENT_FUNCTION);
break;
}
} else {
/*--- Compute our location in the send buffer. ---*/
auto offset = countPerPoint*nPoint_P2PSend[val_iSend];
/*--- Take advantage of cumulative storage format to get the number
of points that we need to send. ---*/
auto nPointP2P = nPoint_P2PSend[val_iSend+1] - nPoint_P2PSend[val_iSend];
/*--- Total count can include multiple pieces of data per element. ---*/
auto count = countPerPoint*nPointP2P;
/*--- Get the rank to which we send the message. ---*/
auto dest = Neighbors_P2PSend[val_iSend];
auto tag = rank + 1;
/*--- Post non-blocking send for this proc. ---*/
switch (commType) {
case COMM_TYPE_DOUBLE:
SU2_MPI::Isend(&(bufD_P2PSend[offset]), count, MPI_DOUBLE,
dest, tag, SU2_MPI::GetComm(), &(req_P2PSend[val_iSend]));
break;
case COMM_TYPE_UNSIGNED_SHORT:
SU2_MPI::Isend(&(bufS_P2PSend[offset]), count, MPI_UNSIGNED_SHORT,
dest, tag, SU2_MPI::GetComm(), &(req_P2PSend[val_iSend]));
break;
default:
SU2_MPI::Error("Unrecognized data type for point-to-point MPI comms.",
CURRENT_FUNCTION);
break;
}
}
END_SU2_OMP_MASTER
}
void CGeometry::GetCommCountAndType(const CConfig* config,
unsigned short commType,
unsigned short &COUNT_PER_POINT,
unsigned short &MPI_TYPE) const {
switch (commType) {
case COORDINATES:
COUNT_PER_POINT = nDim;
MPI_TYPE = COMM_TYPE_DOUBLE;
break;
case GRID_VELOCITY:
COUNT_PER_POINT = nDim;
MPI_TYPE = COMM_TYPE_DOUBLE;
break;
case COORDINATES_OLD:
if (config->GetTime_Marching() == TIME_MARCHING::DT_STEPPING_2ND)
COUNT_PER_POINT = nDim*2;
else
COUNT_PER_POINT = nDim;
MPI_TYPE = COMM_TYPE_DOUBLE;
break;
case MAX_LENGTH:
COUNT_PER_POINT = 1;
MPI_TYPE = COMM_TYPE_DOUBLE;
break;
case NEIGHBORS:
COUNT_PER_POINT = 1;
MPI_TYPE = COMM_TYPE_UNSIGNED_SHORT;
break;
default:
SU2_MPI::Error("Unrecognized quantity for point-to-point MPI comms.",
CURRENT_FUNCTION);
break;
}
}
void CGeometry::InitiateComms(CGeometry *geometry,
const CConfig *config,
unsigned short commType) const {
if (nP2PSend == 0) return;
/*--- Local variables ---*/
unsigned short iDim;
unsigned short COUNT_PER_POINT = 0;
unsigned short MPI_TYPE = 0;
unsigned long iPoint, msg_offset, buf_offset;
int iMessage, iSend, nSend;
/*--- Set the size of the data packet and type depending on quantity. ---*/
GetCommCountAndType(config, commType, COUNT_PER_POINT, MPI_TYPE);
/*--- Check to make sure we have created a large enough buffer
for these comms during preprocessing. This is only for the su2double
buffer. It will be reallocated whenever we find a larger count
per point. After the first cycle of comms, this should be inactive. ---*/
geometry->AllocateP2PComms(COUNT_PER_POINT);
/*--- Set some local pointers to make access simpler. ---*/
su2double *bufDSend = geometry->bufD_P2PSend;
unsigned short *bufSSend = geometry->bufS_P2PSend;
su2double *vector = nullptr;
/*--- Load the specified quantity from the solver into the generic
communication buffer in the geometry class. ---*/
/*--- Post all non-blocking recvs first before sends. ---*/
geometry->PostP2PRecvs(geometry, config, MPI_TYPE, COUNT_PER_POINT, false);
for (iMessage = 0; iMessage < nP2PSend; iMessage++) {
/*--- Get the offset in the buffer for the start of this message. ---*/
msg_offset = nPoint_P2PSend[iMessage];
/*--- Total count can include multiple pieces of data per element. ---*/
nSend = (nPoint_P2PSend[iMessage+1] - nPoint_P2PSend[iMessage]);
SU2_OMP_FOR_STAT(OMP_MIN_SIZE)
for (iSend = 0; iSend < nSend; iSend++) {
/*--- Get the local index for this communicated data. ---*/
iPoint = geometry->Local_Point_P2PSend[msg_offset + iSend];
/*--- Compute the offset in the recv buffer for this point. ---*/
buf_offset = (msg_offset + iSend)*COUNT_PER_POINT;
switch (commType) {
case COORDINATES:
vector = nodes->GetCoord(iPoint);
for (iDim = 0; iDim < nDim; iDim++)
bufDSend[buf_offset+iDim] = vector[iDim];
break;
case GRID_VELOCITY:
vector = nodes->GetGridVel(iPoint);
for (iDim = 0; iDim < nDim; iDim++)
bufDSend[buf_offset+iDim] = vector[iDim];
break;
case COORDINATES_OLD:
vector = nodes->GetCoord_n(iPoint);
for (iDim = 0; iDim < nDim; iDim++) {
bufDSend[buf_offset+iDim] = vector[iDim];
}
if (config->GetTime_Marching() == TIME_MARCHING::DT_STEPPING_2ND) {
vector = nodes->GetCoord_n1(iPoint);
for (iDim = 0; iDim < nDim; iDim++) {
bufDSend[buf_offset+nDim+iDim] = vector[iDim];
}
}
break;
case MAX_LENGTH:
bufDSend[buf_offset] = nodes->GetMaxLength(iPoint);
break;
case NEIGHBORS:
bufSSend[buf_offset] = geometry->nodes->GetnNeighbor(iPoint);
break;
default:
SU2_MPI::Error("Unrecognized quantity for point-to-point MPI comms.",
CURRENT_FUNCTION);
break;
}
}
END_SU2_OMP_FOR
/*--- Launch the point-to-point MPI send for this message. ---*/
geometry->PostP2PSends(geometry, config, MPI_TYPE, COUNT_PER_POINT, iMessage, false);
}
}
void CGeometry::CompleteComms(CGeometry *geometry,
const CConfig *config,
unsigned short commType) {
if (nP2PRecv == 0) return;
/*--- Local variables ---*/
unsigned short iDim, COUNT_PER_POINT = 0, MPI_TYPE = 0;
unsigned long iPoint, iRecv, nRecv, msg_offset, buf_offset;
int ind, source, iMessage, jRecv;
/*--- Global status so all threads can see the result of Waitany. ---*/
static SU2_MPI::Status status;
/*--- Set the size of the data packet and type depending on quantity. ---*/
GetCommCountAndType(config, commType, COUNT_PER_POINT, MPI_TYPE);
/*--- Set some local pointers to make access simpler. ---*/
const su2double *bufDRecv = geometry->bufD_P2PRecv;
const unsigned short *bufSRecv = geometry->bufS_P2PRecv;
/*--- Store the data that was communicated into the appropriate
location within the local class data structures. Note that we
recv and store the data in any order to take advantage of the
non-blocking comms. ---*/
for (iMessage = 0; iMessage < nP2PRecv; iMessage++) {
/*--- For efficiency, recv the messages dynamically based on
the order they arrive. ---*/
SU2_OMP_SAFE_GLOBAL_ACCESS(SU2_MPI::Waitany(nP2PRecv, req_P2PRecv, &ind, &status);)
/*--- Once we have recv'd a message, get the source rank. ---*/
source = status.MPI_SOURCE;
/*--- We know the offsets based on the source rank. ---*/
jRecv = P2PRecv2Neighbor[source];
/*--- Get the offset in the buffer for the start of this message. ---*/
msg_offset = nPoint_P2PRecv[jRecv];
/*--- Get the number of packets to be received in this message. ---*/
nRecv = nPoint_P2PRecv[jRecv+1] - nPoint_P2PRecv[jRecv];
SU2_OMP_FOR_STAT(OMP_MIN_SIZE)
for (iRecv = 0; iRecv < nRecv; iRecv++) {
/*--- Get the local index for this communicated data. ---*/
iPoint = geometry->Local_Point_P2PRecv[msg_offset + iRecv];
/*--- Compute the total offset in the recv buffer for this point. ---*/
buf_offset = (msg_offset + iRecv)*COUNT_PER_POINT;
/*--- Store the data correctly depending on the quantity. ---*/
switch (commType) {
case COORDINATES:
for (iDim = 0; iDim < nDim; iDim++)
nodes->SetCoord(iPoint, iDim, bufDRecv[buf_offset+iDim]);
break;
case GRID_VELOCITY:
for (iDim = 0; iDim < nDim; iDim++)
nodes->SetGridVel(iPoint, iDim, bufDRecv[buf_offset+iDim]);
break;
case COORDINATES_OLD:
nodes->SetCoord_n(iPoint, &bufDRecv[buf_offset]);
if (config->GetTime_Marching() == TIME_MARCHING::DT_STEPPING_2ND)
nodes->SetCoord_n1(iPoint, &bufDRecv[buf_offset+nDim]);
break;
case MAX_LENGTH:
nodes->SetMaxLength(iPoint, bufDRecv[buf_offset]);
break;
case NEIGHBORS:
nodes->SetnNeighbor(iPoint, bufSRecv[buf_offset]);
break;
default:
SU2_MPI::Error("Unrecognized quantity for point-to-point MPI comms.",
CURRENT_FUNCTION);
break;
}
}
END_SU2_OMP_FOR
}
/*--- Verify that all non-blocking point-to-point sends have finished.
Note that this should be satisfied, as we have received all of the
data in the loop above at this point. ---*/
#ifdef HAVE_MPI
SU2_OMP_SAFE_GLOBAL_ACCESS(SU2_MPI::Waitall(nP2PSend, req_P2PSend, MPI_STATUS_IGNORE);)
#endif
}
void CGeometry::PreprocessPeriodicComms(CGeometry *geometry,
CConfig *config) {
/*--- We start with the send and receive lists already available in
the form of stored periodic point-donor pairs. We will loop through
these markers and establish the neighboring ranks and number of
send/recv points per pair. We will store this information and set
up persistent data structures so that we can reuse them throughout
the calculation for any periodic boundary communications. The goal
is to break the non-blocking comms into InitiatePeriodicComms() and
CompletePeriodicComms() in separate routines so that we can overlap the
communication and computation to hide the communication latency. ---*/
/*--- Local variables. ---*/
unsigned short iMarker;
unsigned long iPoint, iVertex, iPeriodic;
int iRank, iSend, iRecv, ii, jj;
/*--- Create some temporary structures for tracking sends/recvs. ---*/
int *nPoint_Send_All = new int[size+1]; nPoint_Send_All[0] = 0;
int *nPoint_Recv_All = new int[size+1]; nPoint_Recv_All[0] = 0;
int *nPoint_Flag = new int[size];
for (iRank = 0; iRank < size; iRank++) {
nPoint_Send_All[iRank] = 0;
nPoint_Recv_All[iRank] = 0;
nPoint_Flag[iRank]= -1;
}
nPoint_Send_All[size] = 0; nPoint_Recv_All[size] = 0;
/*--- Loop through all of our periodic markers and track
our sends with each rank. ---*/
for (iMarker = 0; iMarker < config->GetnMarker_All(); iMarker++) {
if (config->GetMarker_All_KindBC(iMarker) == PERIODIC_BOUNDARY) {
iPeriodic = config->GetMarker_All_PerBound(iMarker);
for (iVertex = 0; iVertex < geometry->nVertex[iMarker]; iVertex++) {
/*--- Get the current periodic point index. We only communicate
the owned nodes on a rank, as the MPI comms will take care of
the halos after completing the periodic comms. ---*/
iPoint = geometry->vertex[iMarker][iVertex]->GetNode();
if (geometry->nodes->GetDomain(iPoint)) {
/*--- Get the rank that holds the matching periodic point
on the other marker in the periodic pair. ---*/
iRank = (int)geometry->vertex[iMarker][iVertex]->GetDonorProcessor();
/*--- If we have not visited this point last, increment our
number of points that must be sent to a particular proc. ---*/
if ((nPoint_Flag[iRank] != (int)iPoint)) {
nPoint_Flag[iRank] = (int)iPoint;
nPoint_Send_All[iRank+1] += 1;
}
}
}
}
}
delete [] nPoint_Flag;
/*--- Communicate the number of points to be sent/recv'd amongst
all processors. After this communication, each proc knows how
many periodic points it will receive from each other processor. ---*/
SU2_MPI::Alltoall(&(nPoint_Send_All[1]), 1, MPI_INT,
&(nPoint_Recv_All[1]), 1, MPI_INT, SU2_MPI::GetComm());
/*--- Check how many messages we will be sending and receiving.
Here we also put the counters into cumulative storage format to
make the communications simpler. Note that we are allowing each
rank to communicate to themselves in these counters, although
it will not be done through MPI. ---*/
nPeriodicSend = 0; nPeriodicRecv = 0;
for (iRank = 0; iRank < size; iRank++) {
if ((nPoint_Send_All[iRank+1] > 0)) nPeriodicSend++;
if ((nPoint_Recv_All[iRank+1] > 0)) nPeriodicRecv++;
nPoint_Send_All[iRank+1] += nPoint_Send_All[iRank];
nPoint_Recv_All[iRank+1] += nPoint_Recv_All[iRank];
}
/*--- Allocate only as much memory as needed for the periodic neighbors. ---*/
nPoint_PeriodicSend = new int[nPeriodicSend+1]; nPoint_PeriodicSend[0] = 0;
nPoint_PeriodicRecv = new int[nPeriodicRecv+1]; nPoint_PeriodicRecv[0] = 0;
Neighbors_PeriodicSend = new int[nPeriodicSend];
Neighbors_PeriodicRecv = new int[nPeriodicRecv];
iSend = 0; iRecv = 0;
for (iRank = 0; iRank < size; iRank++) {
if ((nPoint_Send_All[iRank+1] > nPoint_Send_All[iRank])) {
Neighbors_PeriodicSend[iSend] = iRank;
nPoint_PeriodicSend[iSend+1] = nPoint_Send_All[iRank+1];
iSend++;
}
if ((nPoint_Recv_All[iRank+1] > nPoint_Recv_All[iRank])) {
Neighbors_PeriodicRecv[iRecv] = iRank;
nPoint_PeriodicRecv[iRecv+1] = nPoint_Recv_All[iRank+1];
iRecv++;
}
}
/*--- Create a reverse mapping of the message to the rank so that we
can quickly access the correct data in the buffers when receiving
messages dynamically later during the iterations. ---*/
PeriodicSend2Neighbor.clear();
for (iSend = 0; iSend < nPeriodicSend; iSend++)
PeriodicSend2Neighbor[Neighbors_PeriodicSend[iSend]] = iSend;
PeriodicRecv2Neighbor.clear();
for (iRecv = 0; iRecv < nPeriodicRecv; iRecv++)
PeriodicRecv2Neighbor[Neighbors_PeriodicRecv[iRecv]] = iRecv;
delete [] nPoint_Send_All;
delete [] nPoint_Recv_All;
/*--- Allocate the memory to store the local index values for both
the send and receive periodic points and periodic index. ---*/
Local_Point_PeriodicSend = nullptr;
Local_Point_PeriodicSend = new unsigned long[nPoint_PeriodicSend[nPeriodicSend]];
for (iSend = 0; iSend < nPoint_PeriodicSend[nPeriodicSend]; iSend++)
Local_Point_PeriodicSend[iSend] = 0;
Local_Marker_PeriodicSend = nullptr;
Local_Marker_PeriodicSend = new unsigned long[nPoint_PeriodicSend[nPeriodicSend]];
for (iSend = 0; iSend < nPoint_PeriodicSend[nPeriodicSend]; iSend++)
Local_Marker_PeriodicSend[iSend] = 0;
Local_Point_PeriodicRecv = nullptr;
Local_Point_PeriodicRecv = new unsigned long[nPoint_PeriodicRecv[nPeriodicRecv]];
for (iRecv = 0; iRecv < nPoint_PeriodicRecv[nPeriodicRecv]; iRecv++)
Local_Point_PeriodicRecv[iRecv] = 0;
Local_Marker_PeriodicRecv = nullptr;
Local_Marker_PeriodicRecv = new unsigned long[nPoint_PeriodicRecv[nPeriodicRecv]];
for (iRecv = 0; iRecv < nPoint_PeriodicRecv[nPeriodicRecv]; iRecv++)
Local_Marker_PeriodicRecv[iRecv] = 0;
/*--- We allocate the buffers for communicating values in a later step
once we know the maximum packet size that we need to communicate. This
memory is deallocated and reallocated automatically in the case that
the previously allocated memory is not sufficient. ---*/
bufD_PeriodicSend = nullptr;
bufD_PeriodicRecv = nullptr;
bufS_PeriodicSend = nullptr;
bufS_PeriodicRecv = nullptr;
/*--- Allocate memory for the MPI requests if we need to communicate. ---*/
if (nPeriodicSend > 0) {