-
Notifications
You must be signed in to change notification settings - Fork 4
/
PUML.h
1491 lines (1239 loc) · 41.8 KB
/
PUML.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
/**
* @file
* This file is part of PUML
*
* For conditions of distribution and use, please see the copyright
* notice in the file 'COPYING' at the root directory of this package
* and the copyright notice at https://github.com/TUM-I5/PUMGen
*
* @copyright 2017 Technische Universitaet Muenchen
* @author Sebastian Rettenberger <[email protected]>
*/
#ifndef PUML_PUML_H
#define PUML_PUML_H
#include "TypeInference.h"
#include <cstddef>
#include <type_traits>
#ifdef USE_MPI
#include <mpi.h>
#endif // USE_MPI
#include <algorithm>
#include <cassert>
#include <limits>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include <cstdlib>
#include <hdf5.h>
#include "utils/logger.h"
#include "utils/stringutils.h"
#include "DownElement.h"
#include "Element.h"
#include "Topology.h"
#include "VertexElementMap.h"
namespace PUML
{
enum DataType
{
CELL = 0,
VERTEX = 1
};
/**
* Distributes a number of mesh entities (i.e. elements or vertices) to a given number of ranks
* For E entities and R ranks, the first E%R ranks will read E/R+1 entities.
* The remaining ranks will read E/R entities.
*/
class Distributor {
public:
Distributor() = default;
Distributor(unsigned long newNumEntities, unsigned long newNumRanks) : numEntities(newNumEntities),
numRanks(newNumRanks),
entitiesPerRank(numEntities / numRanks),
missingEntities(numEntities % numRanks) {
assert(numEntities > numRanks);
}
/**
* Gives the offset and size of data where the rank should read data.
*/
std::pair<unsigned long, unsigned long> offsetAndSize(unsigned long rank) {
assert(rank < numRanks);
unsigned long offset = 0;
unsigned long size = 0;
if (rank < missingEntities) {
offset = rank * (entitiesPerRank + 1);
size = std::min(entitiesPerRank + 1, numEntities - offset);
} else {
offset = missingEntities * (entitiesPerRank + 1) + (rank - missingEntities) * entitiesPerRank;
size = std::min(entitiesPerRank, numEntities - offset);
}
assert(offset + size <= numEntities);
return {offset, size};
}
/**
* Gives the rank, which has read the entity with the given globalId.
*/
unsigned long rankOfEntity(unsigned long globalId) {
assert(globalId < numEntities);
unsigned long rank = 0;
if (globalId < missingEntities * (entitiesPerRank + 1)) {
rank = globalId / (entitiesPerRank + 1);
} else {
rank = (globalId - missingEntities * (entitiesPerRank + 1)) / entitiesPerRank + missingEntities;
}
assert(rank < numRanks);
return rank;
}
/**
* Gives the local id of a mesh entity for the given globalId.
*/
unsigned long globalToLocalId(unsigned long rank, unsigned long globalId) {
assert(globalId < numEntities);
auto[offset, size] = offsetAndSize(rank);
assert (globalId >= offset);
assert (globalId < offset + size);
return globalId - offset;
}
private:
const unsigned long numEntities;
const unsigned long numRanks;
const unsigned long entitiesPerRank;
const unsigned long missingEntities;
};
#define checkH5Err(...) _checkH5Err(__VA_ARGS__, __FILE__, __LINE__, rank)
/**
* @todo Handle non-MPI case correct
*/
template<TopoType Topo>
class PUML
{
public:
/** The cell type from the file */
typedef unsigned long ocell_t[internal::Topology<Topo>::cellvertices()];
/** The vertex type from the file */
typedef double overtex_t[3];
/** Internal cell type */
typedef Cell<Topo> cell_t;
/** Internal face type */
typedef Face face_t;
/** Internal edge type */
typedef Edge edge_t;
/** Internal vertex type */
typedef Vertex vertex_t;
private:
#ifdef USE_MPI
MPI_Comm m_comm;
#endif // USE_MPI
/** The original cells from the file */
ocell_t* m_originalCells;
/** The original vertices from the file */
overtex_t* m_originalVertices;
/** The original number of cells/vertices on each node */
unsigned int m_originalSize[2];
/** The original number of total cells/vertices */
unsigned long m_originalTotalSize[2];
typedef std::unordered_map<unsigned long, unsigned int> g2l_t;
/** The list of all local cells */
std::vector<cell_t> m_cells;
/** The list of all local faces */
std::vector<face_t> m_faces;
/** Mapping from global face ids to local face ids */
g2l_t m_facesg2l;
/** List of all local edges */
std::vector<edge_t> m_edges;
/** Mapping from global edge ids to local edge ids */
g2l_t m_edgesg2l;
/** List of all local vertices */
std::vector<vertex_t> m_vertices;
/** Mapping from global vertex ids to locl vertex ids */
g2l_t m_verticesg2l;
/** Maps from local vertex ids to to a local face ids */
internal::VertexElementMap<internal::Topology<Topo>::facevertices()> m_v2f;
/** Maps from local vertex ids to local edge ids */
internal::VertexElementMap<2> m_v2e;
/** User cell data */
std::vector<void*> m_cellData;
/** User vertex data */
std::vector<void*> m_vertexData;
/** Original user vertex data */
std::vector<void*> m_originalVertexData;
std::vector<std::size_t> m_cellDataSize;
std::vector<std::size_t> m_vertexDataSize;
#ifdef USE_MPI
std::vector<MPI_Datatype> m_cellDataType;
std::vector<bool> m_cellDataTypeDerived;
std::vector<MPI_Datatype> m_vertexDataType;
std::vector<bool> m_vertexDataTypeDerived;
#endif
std::pair<MPI_Datatype, bool> createDatatypeArray(MPI_Datatype baseType, std::size_t elemSize) {
if (elemSize == 1) {
return {baseType, false};
}
MPI_Datatype newType;
MPI_Type_contiguous(elemSize, baseType, &newType);
MPI_Type_commit(&newType);
return {newType, true};
}
public:
PUML() :
#ifdef USE_MPI
m_comm(MPI_COMM_WORLD),
#endif // USE_MPI
m_originalCells(0L),
m_originalVertices(0L)
{ }
virtual ~PUML()
{
delete [] m_originalCells;
delete [] m_originalVertices;
for (const auto& i : m_cellData) {
std::free(i);
}
for (const auto& i : m_vertexData) {
std::free(i);
}
for (const auto& i : m_originalVertexData) {
std::free(i);
}
#ifdef USE_MPI
for (size_t i = 0; i < m_cellDataType.size(); ++i) {
if (m_cellDataTypeDerived[i]) {
MPI_Type_free(&m_cellDataType[i]);
}
}
for (size_t i = 0; i < m_vertexDataType.size(); ++i) {
if (m_vertexDataTypeDerived[i]) {
MPI_Type_free(&m_vertexDataType[i]);
}
}
#endif
}
#ifdef USE_MPI
void setComm(MPI_Comm comm)
{
m_comm = comm;
}
#endif // USE_MPI
void open(const char* cellName, const char* vertexName)
{
int rank = 0;
int procs = 1;
#ifdef USE_MPI
MPI_Comm_rank(m_comm, &rank);
MPI_Comm_size(m_comm, &procs);
#endif // USE_MPI
std::vector<std::string> cellNames = utils::StringUtils::split(cellName, ':');
if (cellNames.size() != 2) {
logError() << "Cells name must have the form \"filename:/dataset\"";
}
std::vector<std::string> vertexNames = utils::StringUtils::split(vertexName, ':');
if (vertexNames.size() != 2) {
logError() << "Vertices name must have the form \"filename:/dataset\"";
}
// Open the cell file
hid_t h5plist = H5Pcreate(H5P_FILE_ACCESS);
checkH5Err(h5plist);
#ifdef USE_MPI
checkH5Err(H5Pset_fapl_mpio(h5plist, m_comm, MPI_INFO_NULL));
#endif // USE_MPI
hid_t h5file = H5Fopen(cellNames[0].c_str(), H5F_ACC_RDONLY, h5plist);
checkH5Err(h5file);
// Get cell dataset
hid_t h5dataset = H5Dopen(h5file, cellNames[1].c_str(), H5P_DEFAULT);
checkH5Err(h5dataset);
// Check the size of cell dataset
hid_t h5space = H5Dget_space(h5dataset);
checkH5Err(h5space);
if (H5Sget_simple_extent_ndims(h5space) != 2) {
logError() << "Cell dataset must have 2 dimensions";
}
hsize_t dims[2];
checkH5Err(H5Sget_simple_extent_dims(h5space, dims, 0L));
if (dims[1] != internal::Topology<Topo>::cellvertices()) {
logError() << "Each cell must have" << internal::Topology<Topo>::cellvertices() << "vertices";
}
logInfo(rank) << "Found" << dims[0] << "cells";
auto cellDistributor = Distributor(dims[0], procs);
// Read the cells
m_originalTotalSize[0] = dims[0];
auto[offsetCells, sizeCells] = cellDistributor.offsetAndSize(rank);
m_originalSize[0] = sizeCells;
hsize_t start[2] = {offsetCells, 0};
hsize_t count[2] = {m_originalSize[0], internal::Topology<Topo>::cellvertices()};
checkH5Err(H5Sselect_hyperslab(h5space, H5S_SELECT_SET, start, 0L, count, 0L));
hid_t h5memspace = H5Screate_simple(2, count, 0L);
checkH5Err(h5memspace);
hid_t h5alist = H5Pcreate(H5P_DATASET_XFER);
checkH5Err(h5alist);
#ifdef USE_MPI
checkH5Err(H5Pset_dxpl_mpio(h5alist, H5FD_MPIO_COLLECTIVE));
#endif // USE_MPI
m_originalCells = new ocell_t[m_originalSize[0]];
checkH5Err(H5Dread(h5dataset, H5T_NATIVE_ULONG, h5memspace, h5space, h5alist, m_originalCells));
// Close cells
checkH5Err(H5Sclose(h5space));
checkH5Err(H5Sclose(h5memspace));
checkH5Err(H5Dclose(h5dataset));
checkH5Err(H5Fclose(h5file));
// Open the vertex file
h5file = H5Fopen(vertexNames[0].c_str(), H5F_ACC_RDONLY, h5plist);
checkH5Err(h5file);
// Get vertex dataset
h5dataset = H5Dopen(h5file, vertexNames[1].c_str(), H5P_DEFAULT);
checkH5Err(h5dataset);
// Check the size of vertex dataset
h5space = H5Dget_space(h5dataset);
checkH5Err(h5space);
if (H5Sget_simple_extent_ndims(h5space) != 2) {
logError() << "Vertex dataset must have 2 dimensions";
}
checkH5Err(H5Sget_simple_extent_dims(h5space, dims, 0L));
if (dims[1] != 3) {
logError() << "Each vertex must have xyz coordinate";
}
logInfo(rank) << "Found" << dims[0] << "vertices";
auto vertexDistributor = Distributor(dims[0], procs);
// Read the vertices
m_originalTotalSize[1] = dims[0];
auto[offsetVertices, sizeVertices] = vertexDistributor.offsetAndSize(rank);
m_originalSize[1] = sizeVertices;
start[0] = offsetVertices;
count[0] = m_originalSize[1]; count[1] = 3;
checkH5Err(H5Sselect_hyperslab(h5space, H5S_SELECT_SET, start, 0L, count, 0L));
h5memspace = H5Screate_simple(2, count, 0L);
checkH5Err(h5memspace);
m_originalVertices = new overtex_t[m_originalSize[1]];
checkH5Err(H5Dread(h5dataset, H5T_NATIVE_DOUBLE, h5memspace, h5space, h5alist, m_originalVertices));
// Close vertices
checkH5Err(H5Sclose(h5space));
checkH5Err(H5Sclose(h5memspace));
checkH5Err(H5Dclose(h5dataset));
checkH5Err(H5Fclose(h5file));
// Close other H5 stuff
checkH5Err(H5Pclose(h5plist));
checkH5Err(H5Pclose(h5alist));
}
template<typename T>
void addDataArray(const T* rawData, DataType type, const std::vector<size_t>& sizes
#ifdef USE_MPI
, MPI_Datatype mpiType = MPITypeInfer<T>::type()
#endif
)
{
static_assert(std::is_trivially_copyable_v<T>, "T needs to be trivially copyable");
static_assert(std::is_trivially_default_constructible_v<T>, "T needs to be trivially default constructible");
int rank = 0;
int procs = 1;
#ifdef USE_MPI
MPI_Comm_rank(m_comm, &rank);
MPI_Comm_size(m_comm, &procs);
#endif // USE_MPI
auto cellDistributor = Distributor(m_originalTotalSize[type], procs);
auto[offset, localSize] = cellDistributor.offsetAndSize(rank);
size_t elemSize = 1;
for (auto size : sizes) {
elemSize *= size;
}
void* data = std::malloc(sizeof(T) * localSize * elemSize);
std::memcpy(data, rawData, sizeof(T) * localSize * elemSize);
switch (type) {
case CELL:
{
m_cellData.push_back(data);
m_cellDataSize.push_back(sizeof(T) * elemSize);
#ifdef USE_MPI
auto [type, derived] = createDatatypeArray(mpiType, elemSize);
m_cellDataType.push_back(type);
m_cellDataTypeDerived.push_back(derived);
#endif
}
break;
case VERTEX:
{
m_originalVertexData.push_back(data);
m_vertexDataSize.push_back(sizeof(T) * elemSize);
#ifdef USE_MPI
auto [type, derived] = createDatatypeArray(mpiType, elemSize);
m_vertexDataType.push_back(type);
m_vertexDataTypeDerived.push_back(derived);
#endif
}
break;
}
}
template<typename T = int>
void addData(const char* dataName, DataType type, const std::vector<size_t>& sizes
#ifdef USE_MPI
, MPI_Datatype mpiType = MPITypeInfer<T>::type()
#endif
, hid_t hdf5Type = HDF5TypeInfer<T>::type()
)
{
static_assert(std::is_trivially_copyable_v<T>, "T needs to be trivially copyable");
static_assert(std::is_trivially_default_constructible_v<T>, "T needs to be trivially default constructible");
int rank = 0;
int procs = 1;
#ifdef USE_MPI
MPI_Comm_rank(m_comm, &rank);
MPI_Comm_size(m_comm, &procs);
#endif // USE_MPI
auto cellDistributor = Distributor(m_originalTotalSize[0], procs);
std::vector<std::string> dataNames = utils::StringUtils::split(dataName, ':');
if (dataNames.size() != 2) {
logError() << "Data name must have the form \"filename:/dataset\"";
}
// Open the cell file
hid_t h5plist = H5Pcreate(H5P_FILE_ACCESS);
checkH5Err(h5plist);
#ifdef USE_MPI
checkH5Err(H5Pset_fapl_mpio(h5plist, m_comm, MPI_INFO_NULL));
#endif // USE_MPI
hid_t h5file = H5Fopen(dataNames[0].c_str(), H5F_ACC_RDONLY, h5plist);
checkH5Err(h5file);
unsigned long totalSize = m_originalTotalSize[type];
// Get cell dataset
hid_t h5dataset = H5Dopen(h5file, dataNames[1].c_str(), H5P_DEFAULT);
checkH5Err(h5dataset);
// Check the size of cell dataset
hid_t h5space = H5Dget_space(h5dataset);
checkH5Err(h5space);
if (H5Sget_simple_extent_ndims(h5space) != 1) {
logError() << "Dataset must have 1 dimension";
}
hsize_t dim;
checkH5Err(H5Sget_simple_extent_dims(h5space, &dim, 0L));
if (dim != totalSize) {
logError() << "Dataset has the wrong size";
}
// Read the cells
auto[offset, localSize] = cellDistributor.offsetAndSize(rank);
size_t elemSize = 1;
for (auto size : sizes) {
elemSize *= size;
}
std::vector<hsize_t> start = {offset};
std::vector<hsize_t> count = {localSize};
for (auto size : sizes) {
start.push_back(0);
count.push_back(size);
}
checkH5Err(H5Sselect_hyperslab(h5space, H5S_SELECT_SET, start.data(), 0L, count.data(), 0L));
hid_t h5memspace = H5Screate_simple(count.size(), count.data(), 0L);
checkH5Err(h5memspace);
hid_t h5alist = H5Pcreate(H5P_DATASET_XFER);
checkH5Err(h5alist);
#ifdef USE_MPI
checkH5Err(H5Pset_dxpl_mpio(h5alist, H5FD_MPIO_COLLECTIVE));
#endif // USE_MPI
void* data = std::malloc(sizeof(T) * localSize * elemSize);
checkH5Err(H5Dread(h5dataset, hdf5Type, h5memspace, h5space, h5alist, data));
// Close data
checkH5Err(H5Sclose(h5space));
checkH5Err(H5Sclose(h5memspace));
checkH5Err(H5Dclose(h5dataset));
checkH5Err(H5Fclose(h5file));
// Close other H5 stuff
checkH5Err(H5Pclose(h5plist));
checkH5Err(H5Pclose(h5alist));
switch (type) {
case CELL:
{
m_cellData.push_back(data);
m_cellDataSize.push_back(sizeof(T) * elemSize);
#ifdef USE_MPI
auto [type, derived] = createDatatypeArray(mpiType, elemSize);
m_cellDataType.push_back(type);
m_cellDataTypeDerived.push_back(derived);
#endif
}
break;
case VERTEX:
{
m_originalVertexData.push_back(data);
m_vertexDataSize.push_back(sizeof(T) * elemSize);
#ifdef USE_MPI
auto [type, derived] = createDatatypeArray(mpiType, elemSize);
m_vertexDataType.push_back(type);
m_vertexDataTypeDerived.push_back(derived);
#endif
}
break;
}
}
void partition(int* partition)
{
int rank = 0;
int procs = 1;
#ifdef USE_MPI
MPI_Comm_rank(m_comm, &rank);
MPI_Comm_size(m_comm, &procs);
#endif // USE_MPI
// Create sorting indices
unsigned int* indices = new unsigned int[m_originalSize[0]];
for (unsigned int i = 0; i < m_originalSize[0]; i++) {
indices[i] = i;
}
std::sort(indices, indices+m_originalSize[0], [&](unsigned int i1, unsigned int i2)
{
return partition[i1] < partition[i2];
});
// Sort cells
ocell_t* newCells = new ocell_t[m_originalSize[0]];
for (unsigned int i = 0; i < m_originalSize[0]; i++) {
std::memcpy(newCells[i], m_originalCells[indices[i]], sizeof(ocell_t));
}
delete [] m_originalCells;
m_originalCells = newCells;
// Sort other data
for (std::size_t j = 0; j < m_cellData.size(); ++j) {
void* newData = std::malloc(m_originalSize[0] * m_cellDataSize[j]);
for (unsigned int i = 0; i < m_originalSize[0]; i++) {
std::memcpy(reinterpret_cast<char*>(newData) + m_cellDataSize[j] * i, reinterpret_cast<char*>(m_cellData[j]) + m_cellDataSize[j] * indices[i], m_cellDataSize[j]);
}
std::free(m_cellData[j]);
m_cellData[j] = newData;
}
delete [] indices;
// Compute exchange info
int* sendCount = new int[procs];
memset(sendCount, 0, procs * sizeof(int));
for (unsigned int i = 0; i < m_originalSize[0]; i++) {
assert(partition[i] < procs);
sendCount[partition[i]]++;
}
int* recvCount = new int[procs];
#ifdef USE_MPI
MPI_Alltoall(sendCount, 1, MPI_INT, recvCount, 1, MPI_INT, m_comm);
#else // USE_MPI
recvCount[0] = sendCount[0];
#endif // USE_MPI
int *sDispls = new int[procs];
int *rDispls = new int[procs];
sDispls[0] = 0;
rDispls[0] = 0;
for (int i = 1; i < procs; i++) {
sDispls[i] = sDispls[i-1] + sendCount[i-1];
rDispls[i] = rDispls[i-1] + recvCount[i-1];
}
m_originalSize[0] = rDispls[procs-1] + recvCount[procs-1];
#ifdef USE_MPI
// Exchange the cells
MPI_Datatype cellType;
MPI_Type_contiguous(internal::Topology<Topo>::cellvertices(), MPI_UNSIGNED_LONG, &cellType);
MPI_Type_commit(&cellType);
newCells = new ocell_t[m_originalSize[0]];
MPI_Alltoallv(m_originalCells, sendCount, sDispls, cellType,
newCells, recvCount, rDispls, cellType,
m_comm);
delete [] m_originalCells;
m_originalCells = newCells;
MPI_Type_free(&cellType);
// Exchange cell data
for (std::size_t j = 0; j < m_cellData.size(); ++j) {
void* newData = std::malloc(m_originalSize[0] * m_cellDataSize[j]);
MPI_Alltoallv(m_cellData[j], sendCount, sDispls, m_cellDataType[j],
newData, recvCount, rDispls, m_cellDataType[j],
m_comm);
std::free(m_cellData[j]);
m_cellData[j] = newData;
}
#endif // USE_MPI
delete [] sendCount;
delete [] recvCount;
delete [] sDispls;
delete [] rDispls;
}
void generateMesh()
{
int rank = 0;
int procs = 1;
#ifdef USE_MPI
MPI_Comm_rank(m_comm, &rank);
MPI_Comm_size(m_comm, &procs);
#endif // USE_MPI
auto vertexDistributor = Distributor(m_originalTotalSize[1], procs);
// Generate a list of vertices we need from other processors
std::unordered_set<unsigned long>* requiredVertexSets = new std::unordered_set<unsigned long>[procs];
for (unsigned int i = 0; i < m_originalSize[0]; i++) {
for (unsigned int j = 0; j < internal::Topology<Topo>::cellvertices(); j++) {
int proc = vertexDistributor.rankOfEntity(m_originalCells[i][j]);
assert(proc < procs);
requiredVertexSets[proc].insert(m_originalCells[i][j]); // Convert to local vid
}
}
// Generate information for requesting vertices
unsigned int totalVertices = requiredVertexSets[0].size();
for (int i = 1; i < procs; i++)
totalVertices += requiredVertexSets[i].size();
int* sendCount = new int[procs];
unsigned long* requiredVertices = new unsigned long[totalVertices];
unsigned int k = 0;
for (int i = 0; i < procs; i++) {
sendCount[i] = requiredVertexSets[i].size();
for (std::unordered_set<unsigned long>::const_iterator it = requiredVertexSets[i].begin();
it != requiredVertexSets[i].end(); ++it) {
assert(k < totalVertices);
requiredVertices[k++] = *it;
}
}
delete [] requiredVertexSets;
// Exchange required vertex information
int* recvCount = new int[procs];
#ifdef USE_MPI
MPI_Alltoall(sendCount, 1, MPI_INT, recvCount, 1, MPI_INT, m_comm);
#else // USE_MPI
recvCount[0] = sendCount[0];
#endif // USE_MPI
int *sDispls = new int[procs];
int *rDispls = new int[procs];
sDispls[0] = 0;
rDispls[0] = 0;
for (int i = 1; i < procs; i++) {
sDispls[i] = sDispls[i-1] + sendCount[i-1];
rDispls[i] = rDispls[i-1] + recvCount[i-1];
}
unsigned int totalRecv = rDispls[procs-1] + recvCount[procs-1];
unsigned long* distribVertexIds = new unsigned long[totalRecv];
#ifdef USE_MPI
MPI_Alltoallv(requiredVertices, sendCount, sDispls, MPI_UNSIGNED_LONG,
distribVertexIds, recvCount, rDispls, MPI_UNSIGNED_LONG,
m_comm);
#endif // USE_MPI
// Send back vertex coordinates (an other data)
overtex_t* distribVertices = new overtex_t[totalRecv];
std::vector<void*> distribData;
distribData.resize(m_originalVertexData.size());
for (unsigned int i = 0; i < m_originalVertexData.size(); i++) {
distribData[i] = std::malloc(totalRecv * m_vertexDataSize[i]);
}
std::vector<int>* sharedRanks = new std::vector<int>[m_originalSize[1]];
k = 0;
for (int i = 0; i < procs; i++) {
for (int j = 0; j < recvCount[i]; j++) {
assert(k < totalRecv);
distribVertexIds[k] = vertexDistributor.globalToLocalId(rank, distribVertexIds[k]);
assert(distribVertexIds[k] < m_originalSize[1]);
std::memcpy(distribVertices[k], m_originalVertices[distribVertexIds[k]], sizeof(overtex_t));
// Handle other vertex data
for (unsigned int l = 0; l < m_originalVertexData.size(); l++) {
std::memcpy(reinterpret_cast<char*>(distribData[l]) + m_vertexDataSize[l] * k, reinterpret_cast<char*>(m_originalVertexData[l]) + m_vertexDataSize[l] * distribVertexIds[k], m_vertexDataSize[l]);
}
// Save all ranks for each vertex
sharedRanks[distribVertexIds[k]].push_back(i);
k++;
}
}
overtex_t* recvVertices = new overtex_t[totalVertices];
for (auto& it : m_vertexData) {
std::free(it);
}
m_vertexData.resize(m_originalVertexData.size());
for (unsigned int i = 0; i < m_originalVertexData.size(); i++) {
m_vertexData[i] = std::malloc(totalVertices * m_vertexDataSize[i]);
}
#ifdef USE_MPI
MPI_Datatype vertexType;
MPI_Type_contiguous(3, MPI_DOUBLE, &vertexType);
MPI_Type_commit(&vertexType);
MPI_Alltoallv(distribVertices, recvCount, rDispls, vertexType,
recvVertices, sendCount, sDispls, vertexType,
m_comm);
MPI_Type_free(&vertexType);
for (unsigned int i = 0; i < m_originalVertexData.size(); i++) {
MPI_Alltoallv(distribData[i], recvCount, rDispls, m_vertexDataType[i],
m_vertexData[i], sendCount, sDispls, m_vertexDataType[i],
m_comm);
}
#endif // USE_MPI
delete [] distribVertices;
for (auto& it : distribData) {
std::free(it);
}
distribData.clear();
// Send back the number of shared ranks for each vertex
unsigned int* distNsharedRanks = new unsigned int[totalRecv];
unsigned int distTotalSharedRanks = 0;
for (unsigned int i = 0; i < totalRecv; i++) {
assert(distribVertexIds[i] < m_originalSize[1]);
distNsharedRanks[i] = sharedRanks[distribVertexIds[i]].size();
distTotalSharedRanks += distNsharedRanks[i];
}
unsigned int* recvNsharedRanks = new unsigned int[totalVertices];
#ifdef USE_MPI
MPI_Alltoallv(distNsharedRanks, recvCount, rDispls, MPI_UNSIGNED,
recvNsharedRanks, sendCount, sDispls, MPI_UNSIGNED,
m_comm);
#endif // USE_MPI
delete [] distNsharedRanks;
// Setup buffers for exchanging shared ranks
int* sharedSendCount = new int[procs];
memset(sharedSendCount, 0, procs * sizeof(int));
int* distSharedRanks = new int[distTotalSharedRanks];
k = 0;
unsigned int l = 0;
for (int i = 0; i < procs; i++) {
for (int j = 0; j < recvCount[i]; j++) {
assert(k < totalRecv);
assert(l + sharedRanks[distribVertexIds[k]].size() <= distTotalSharedRanks);
memcpy(&distSharedRanks[l], &sharedRanks[distribVertexIds[k]][0], sharedRanks[distribVertexIds[k]].size() * sizeof(int));
l += sharedRanks[distribVertexIds[k]].size();
sharedSendCount[i] += sharedRanks[distribVertexIds[k]].size();
k++;
}
}
delete [] distribVertexIds;
delete [] sharedRanks;
delete [] recvCount;
int* sharedRecvCount = new int[procs];
memset(sharedRecvCount, 0, procs * sizeof(int));
unsigned int recvTotalSharedRanks = 0;
k = 0;
for (int i = 0; i < procs; i++) {
for (int j = 0; j < sendCount[i]; j++) {
assert(k < totalVertices);
recvTotalSharedRanks += recvNsharedRanks[k];
sharedRecvCount[i] += recvNsharedRanks[k];
k++;
}
}
delete [] sendCount;
int* recvSharedRanks = new int[recvTotalSharedRanks];
sDispls[0] = 0;
rDispls[0] = 0;
for (int i = 1; i < procs; i++) {
sDispls[i] = sDispls[i-1] + sharedSendCount[i-1];
rDispls[i] = rDispls[i-1] + sharedRecvCount[i-1];
}
#ifdef USE_MPI
MPI_Alltoallv(distSharedRanks, sharedSendCount, sDispls, MPI_INT,
recvSharedRanks, sharedRecvCount, rDispls, MPI_INT,
m_comm);
#endif // USE_MPI
delete [] distSharedRanks;
delete [] sharedSendCount;
delete [] sharedRecvCount;
delete [] sDispls;
delete [] rDispls;
// Generate the vertex array
m_vertices.resize(totalVertices);
k = 0;
for (unsigned int i = 0; i < totalVertices; i++) {
m_vertices[i].m_gid = requiredVertices[i];
memcpy(m_vertices[i].m_coordinate, recvVertices[i], sizeof(overtex_t));
m_vertices[i].m_sharedRanks.resize(recvNsharedRanks[i]-1);
unsigned int l = 0;
for (unsigned int j = 0; j < recvNsharedRanks[i]; j++) {
if (recvSharedRanks[k] != rank)
m_vertices[i].m_sharedRanks[l++] = recvSharedRanks[k];
k++;
}
std::sort(m_vertices[i].m_sharedRanks.begin(), m_vertices[i].m_sharedRanks.end());
}
delete [] requiredVertices;
delete [] recvVertices;
delete [] recvSharedRanks;
delete [] recvNsharedRanks;
// Construct to g2l map for the vertices
constructG2L(m_vertices, m_verticesg2l);
// Create the cell, face and edge list
m_cells.resize(m_originalSize[0]);
m_v2f.clear();
m_faces.clear();
m_v2e.clear();
unsigned long cellOffset = m_originalSize[0];
#ifdef USE_MPI
MPI_Scan(MPI_IN_PLACE, &cellOffset, 1, MPI_UNSIGNED_LONG, MPI_SUM, m_comm);
#endif // USE_MPI
cellOffset -= m_originalSize[0];
std::vector<std::set<unsigned int> > edgeUpward;
std::set<unsigned int>* vertexUpward = new std::set<unsigned int>[m_vertices.size()];
for (unsigned int i = 0; i < m_originalSize[0]; i++) {
m_cells[i].m_gid = i + cellOffset;
for (unsigned int j = 0; j < internal::Topology<Topo>::cellvertices(); j++)
m_cells[i].m_vertices[j] = m_verticesg2l[m_originalCells[i][j]];
// TODO adapt for hex
// Faces
unsigned int v[internal::Topology<Topo>::facevertices()];
unsigned int faces[internal::Topology<Topo>::cellfaces()];
v[0] = m_cells[i].m_vertices[1];
v[1] = m_cells[i].m_vertices[0];
v[2] = m_cells[i].m_vertices[2];
faces[0] = addFace(m_v2f.add(v), i);
v[0] = m_cells[i].m_vertices[0];
v[1] = m_cells[i].m_vertices[1];
v[2] = m_cells[i].m_vertices[3];
faces[1] = addFace(m_v2f.add(v), i);
v[0] = m_cells[i].m_vertices[1];
v[1] = m_cells[i].m_vertices[2];
v[2] = m_cells[i].m_vertices[3];
faces[2] = addFace(m_v2f.add(v), i);
v[0] = m_cells[i].m_vertices[2];
v[1] = m_cells[i].m_vertices[0];
v[2] = m_cells[i].m_vertices[3];
faces[3] = addFace(m_v2f.add(v), i);
// Edges
unsigned int edges[internal::Topology<Topo>::celledges()];
v[0] = m_cells[i].m_vertices[0];
v[1] = m_cells[i].m_vertices[1];
edges[0] = addEdge(edgeUpward, m_v2e.add(v), faces[0], faces[1]);
v[0] = m_cells[i].m_vertices[1];
v[1] = m_cells[i].m_vertices[2];
edges[1] = addEdge(edgeUpward, m_v2e.add(v), faces[0], faces[2]);
v[0] = m_cells[i].m_vertices[2];
v[1] = m_cells[i].m_vertices[0];
edges[2] = addEdge(edgeUpward, m_v2e.add(v), faces[0], faces[3]);
v[0] = m_cells[i].m_vertices[0];
v[1] = m_cells[i].m_vertices[3];
edges[3] = addEdge(edgeUpward, m_v2e.add(v), faces[1], faces[3]);
v[0] = m_cells[i].m_vertices[1];
v[1] = m_cells[i].m_vertices[3];
edges[4] = addEdge(edgeUpward, m_v2e.add(v), faces[1], faces[2]);
v[0] = m_cells[i].m_vertices[2];
v[1] = m_cells[i].m_vertices[3];
edges[5] = addEdge(edgeUpward, m_v2e.add(v), faces[2], faces[3]);
// Vertices (upward information)
vertexUpward[m_cells[i].m_vertices[0]].insert(edges[0]);
vertexUpward[m_cells[i].m_vertices[0]].insert(edges[2]);
vertexUpward[m_cells[i].m_vertices[0]].insert(edges[3]);
vertexUpward[m_cells[i].m_vertices[1]].insert(edges[0]);
vertexUpward[m_cells[i].m_vertices[1]].insert(edges[1]);
vertexUpward[m_cells[i].m_vertices[1]].insert(edges[4]);
vertexUpward[m_cells[i].m_vertices[2]].insert(edges[1]);
vertexUpward[m_cells[i].m_vertices[2]].insert(edges[2]);
vertexUpward[m_cells[i].m_vertices[2]].insert(edges[5]);
vertexUpward[m_cells[i].m_vertices[3]].insert(edges[3]);
vertexUpward[m_cells[i].m_vertices[3]].insert(edges[4]);
vertexUpward[m_cells[i].m_vertices[3]].insert(edges[5]);
}
// Create edges
m_edges.clear();
m_edges.resize(edgeUpward.size());
for (unsigned int i = 0; i < m_edges.size(); i++) {
assert(m_edges[i].m_upward.empty());
m_edges[i].m_upward.resize(edgeUpward[i].size());
unsigned int j = 0;
for (std::set<unsigned int>::const_iterator it = edgeUpward[i].begin();
it != edgeUpward[i].end(); ++it, j++) {
m_edges[i].m_upward[j] = *it;
}
}
edgeUpward.clear(); // Free memory
// Set vertex upward information
for (unsigned int i = 0; i < m_vertices.size(); i++) {
m_vertices[i].m_upward.resize(vertexUpward[i].size());
unsigned int j = 0;
for (std::set<unsigned int>::const_iterator it = vertexUpward[i].begin();
it != vertexUpward[i].end(); ++it, j++) {
m_vertices[i].m_upward[j] = *it;
}