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 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192
|
#ifdef PARALLEL
#include <meshing.hpp>
#include "paralleltop.hpp"
// #define METIS4
#ifdef METIS
namespace metis {
extern "C" {
#include <metis.h>
#if METIS_VER_MAJOR >= 5
#define METIS5
typedef idx_t idxtype;
#else
#define METIS4
typedef idxtype idx_t;
#endif
}
}
using namespace metis;
#endif
/*
namespace ngcore {
template <> struct MPI_typetrait<netgen::PointIndex> {
static MPI_Datatype MPIType () { return MPI_INT; } };
}
*/
namespace ngcore
{
/** An MPI-Package for a Surface element **/
class SurfPointPackage
{
public:
int num; // point number
int trignum; // STL geo info
double u, v; // OCC geo info
SurfPointPackage () { ; }
SurfPointPackage & operator = (const SurfPointPackage & other) {
num = other.num;
trignum = other.trignum;
u = other.u;
v = other.v;
return *this;
}
}; // class SurfPointPackage
template<> struct MPI_typetrait<SurfPointPackage> {
static NG_MPI_Datatype MPIType () {
static NG_MPI_Datatype MPI_T = 0;
if (!MPI_T)
{
int block_len[2] = { 2, 2 };
NG_MPI_Aint displs[3] = { 0, 2*sizeof(int) };
NG_MPI_Datatype types[2] = { NG_MPI_INT, NG_MPI_DOUBLE };
NG_MPI_Type_create_struct(2, block_len, displs, types, &MPI_T);
NG_MPI_Type_commit(&MPI_T);
}
return MPI_T;
}
}; // struct MPI_typetrait<SurfPointPackage>
class SelPackage
{
public:
int sei;
int index;
int np;
/** we send too much here, especially in 2d! **/
SurfPointPackage points[ELEMENT2D_MAXPOINTS];
SelPackage () { ; }
SelPackage (const netgen::Mesh & mesh, netgen::SurfaceElementIndex _sei)
{
const netgen::Element2d & el = mesh[_sei];
sei = _sei;
index = el.GetIndex();
np = el.GetNP();
for (int k : Range(1, np+1)) {
auto & pnt = points[k-1];;
pnt.num = el.PNum(k);
pnt.trignum = el.GeomInfoPi(k).trignum;
pnt.u = el.GeomInfoPi(k).u;
pnt.v = el.GeomInfoPi(k).v;
}
/** otherwise, we use uninitialized values **/
for (int k : Range(np, ELEMENT2D_MAXPOINTS)) {
points[k].num = -1;
points[k].trignum = -1;
points[k].u = -1;
points[k].v = -1;
}
}
void Unpack (netgen::Element2d & el) const {
el.SetIndex(index);
for (int k : Range(1, np + 1)) {
auto & pnt = points[k-1];
el.PNum(k) = pnt.num;
el.GeomInfoPi(k).trignum = pnt.trignum;
el.GeomInfoPi(k).u = pnt.u;
el.GeomInfoPi(k).v = pnt.v;
}
}
SelPackage & operator = (const SelPackage & other) {
sei = other.sei;
index = other.index;
np = other.np;
for (int k : Range(ELEMENT2D_MAXPOINTS))
{ points[k] = other.points[k]; }
return *this;
}
}; // class SelPackage
template<> struct MPI_typetrait<SelPackage> {
static NG_MPI_Datatype MPIType () {
static NG_MPI_Datatype MPI_T = 0;
if (!MPI_T)
{
int block_len[2] = { 3, ELEMENT2D_MAXPOINTS };
NG_MPI_Aint displs[3] = { 0, 3*sizeof(int) };
NG_MPI_Datatype types[2] = { NG_MPI_INT, GetMPIType<SurfPointPackage>() };
NG_MPI_Type_create_struct(2, block_len, displs, types, &MPI_T);
NG_MPI_Type_commit(&MPI_T);
}
return MPI_T;
}
}; // MPI_typetrait<SelPackage>
class PointElPackage
{
public:
netgen::PointIndex pnum;
int index;
PointElPackage () { pnum = -1; index = -1; }
PointElPackage (const netgen::Element0d & el)
{ pnum = el.pnum; index = el.index; }
}; // class PointElPackage
template<> struct MPI_typetrait<PointElPackage> {
static NG_MPI_Datatype MPIType () {
static NG_MPI_Datatype MPI_T = 0;
if (!MPI_T)
{
int block_len[2] = { 1, 1 };
NG_MPI_Aint displs[3] = { 0, sizeof(netgen::PointIndex) };
NG_MPI_Datatype types[2] = { GetMPIType<netgen::PointIndex>(), NG_MPI_INT };
NG_MPI_Type_create_struct(2, block_len, displs, types, &MPI_T);
NG_MPI_Type_commit(&MPI_T);
}
return MPI_T;
}
}; // MPI_typetrait<Element0d>
} // namespace ngcore
namespace netgen
{
/*
template <>
inline MPI_Datatype MyGetMPIType<PointIndex> ( )
{ return MPI_INT; }
*/
void Mesh :: SendRecvMesh ()
{
int id = GetCommunicator().Rank();
int np = GetCommunicator().Size();
if (np == 1) {
throw NgException("SendRecvMesh called, but only one rank in communicator!!");
}
if (id == 0)
PrintMessage (1, "Send/Receive mesh");
// Why is this here??
if (id == 0)
{
paralleltop -> SetNV (GetNV());
paralleltop -> SetNV_Loc2Glob (GetNV());
paralleltop -> SetNE (GetNE());
paralleltop -> SetNSegm (GetNSeg());
paralleltop -> SetNSE (GetNSE());
}
if (id == 0)
SendMesh ();
else
ReceiveParallelMesh();
paralleltop -> UpdateCoarseGrid();
}
void Mesh :: SendMesh () const
{
static Timer tsend("SendMesh"); RegionTimer reg(tsend);
static Timer tbuildvertex("SendMesh::BuildVertex");
static Timer tbuildvertexa("SendMesh::BuildVertex a");
static Timer tbuildvertexb("SendMesh::BuildVertex b");
static Timer tbuilddistpnums("SendMesh::Build_distpnums");
static Timer tbuildelementtable("SendMesh::Build_elementtable");
NgMPI_Comm comm = GetCommunicator();
// int id = comm.Rank();
int ntasks = comm.Size();
int dim = GetDimension();
comm.Bcast(dim);
NgMPI_Requests sendrequests; // (8*(ntasks-1));
// sendrequests.SetSize0();
// If the topology is not already updated, we do not need to
// build edges/faces.
auto & top = const_cast<MeshTopology&>(GetTopology());
if(top.NeedsUpdate()) {
top.SetBuildVertex2Element(false);
top.SetBuildEdges(false);
top.SetBuildFaces(false);
top.Update();
}
PrintMessage ( 3, "Sending nr of elements");
Array<int> num_els_on_proc(ntasks);
num_els_on_proc = 0;
for (ElementIndex ei = 0; ei < GetNE(); ei++)
num_els_on_proc[vol_partition[ei]]++;
comm.ScatterRoot (num_els_on_proc);
Table<ElementIndex> els_of_proc (num_els_on_proc);
num_els_on_proc = 0;
for (ElementIndex ei = 0; ei < GetNE(); ei++)
{
auto nr = vol_partition[ei];
els_of_proc[nr][num_els_on_proc[nr]++] = ei;
}
PrintMessage ( 3, "Building vertex/proc mapping");
Array<int> num_sels_on_proc(ntasks);
num_sels_on_proc = 0;
for (SurfaceElementIndex ei = 0; ei < GetNSE(); ei++)
num_sels_on_proc[surf_partition[ei]]++;
Table<SurfaceElementIndex> sels_of_proc (num_sels_on_proc);
num_sels_on_proc = 0;
for (SurfaceElementIndex ei = 0; ei < GetNSE(); ei++)
{
auto nr = surf_partition[ei];
sels_of_proc[nr][num_sels_on_proc[nr]++] = ei;
}
NgArray<int> num_segs_on_proc(ntasks);
num_segs_on_proc = 0;
for (SegmentIndex ei = 0; ei < GetNSeg(); ei++)
// num_segs_on_proc[(*this)[ei].GetPartition()]++;
num_segs_on_proc[seg_partition[ei]]++;
TABLE<SegmentIndex> segs_of_proc (num_segs_on_proc);
for (SegmentIndex ei = 0; ei < GetNSeg(); ei++)
segs_of_proc.Add (seg_partition[ei], ei);
/**
----- STRATEGY FOR PERIODIC MESHES -----
Whenever two vertices are identified by periodicity, any proc
that gets one of the vertices actually gets both of them.
This has to be transitive, that is, if
a <-> b and b <-> c,
then any proc that has vertex a also has vertices b and c!
Surfaceelements and Segments that are identified by
periodicity are treated the same way.
We need to duplicate these so we have containers to
hold the edges/facets. Afaik, a mesh cannot have nodes
that are not part of some sort of element.
**/
/** First, we build tables for vertex identification. **/
NgArray<INDEX_2> per_pairs;
NgArray<INDEX_2> pp2;
auto & idents = GetIdentifications();
bool has_periodic = false;
for (int idnr = 1; idnr < idents.GetMaxNr()+1; idnr++)
{
if(idents.GetType(idnr)!=Identifications::PERIODIC) continue;
has_periodic = true;
idents.GetPairs(idnr, pp2);
per_pairs.Append(pp2);
}
NgArray<int, PointIndex::BASE> npvs(GetNV());
npvs = 0;
for (int k = 0; k < per_pairs.Size(); k++) {
npvs[per_pairs[k].I1()]++;
npvs[per_pairs[k].I2()]++;
}
/** for each vertex, gives us all identified vertices **/
TABLE<PointIndex, PointIndex::BASE> per_verts(npvs);
for (int k = 0; k < per_pairs.Size(); k++) {
per_verts.Add(per_pairs[k].I1(), per_pairs[k].I2());
per_verts.Add(per_pairs[k].I2(), per_pairs[k].I1());
}
for (int k = PointIndex::BASE; k < GetNV()+PointIndex::BASE; k++) {
BubbleSort(per_verts[k]);
}
/** The same table as per_verts, but TRANSITIVE!! **/
auto iterate_per_verts_trans = [&](auto f){
NgArray<int> allvs;
// for (int k = PointIndex::BASE; k < GetNV()+PointIndex::BASE; k++)
for (PointIndex k = IndexBASE<PointIndex>();
k < GetNV()+IndexBASE<PointIndex>(); k++)
{
allvs.SetSize(0);
allvs.Append(per_verts[k]);
bool changed = true;
while(changed) {
changed = false;
for (int j = 0; j<allvs.Size(); j++)
{
auto pervs2 = per_verts[allvs[j]];
for (int l = 0; l < pervs2.Size(); l++)
{
auto addv = pervs2[l];
if (allvs.Contains(addv) || addv==k) continue;
changed = true;
allvs.Append(addv);
}
}
}
f(k, allvs);
}
};
iterate_per_verts_trans([&](auto k, auto & allvs) {
npvs[k] = allvs.Size();
});
TABLE<PointIndex, PointIndex::BASE> per_verts_trans(npvs);
iterate_per_verts_trans([&](auto k, auto & allvs) {
for (int j = 0; j<allvs.Size(); j++)
per_verts_trans.Add(k, allvs[j]);
});
for (int k = PointIndex::BASE; k < GetNV()+PointIndex::BASE; k++) {
BubbleSort(per_verts_trans[k]);
}
/** Now we build the vertex-data to send to the workers. **/
tbuildvertex.Start();
NgArray<int, PointIndex::BASE> vert_flag (GetNV());
NgArray<int, PointIndex::BASE> num_procs_on_vert (GetNV());
NgArray<int> num_verts_on_proc (ntasks);
num_verts_on_proc = 0;
num_procs_on_vert = 0;
auto iterate_vertices = [&](auto f) {
vert_flag = -1;
for (int dest = 1; dest < ntasks; dest++)
{
for (auto ei : els_of_proc[dest])
for (auto pnum : (*this)[ei].PNums())
f(pnum, dest);
for (auto ei : sels_of_proc[dest])
for (auto pnum : (*this)[ei].PNums())
f(pnum, dest);
/*
NgFlatArray<SegmentIndex> segs = segs_of_proc[dest];
for (int hi = 0; hi < segs.Size(); hi++)
{
const Segment & el = (*this) [segs[hi]];
for (int i = 0; i < 2; i++)
f(el[i], dest);
}
*/
for (auto segi : segs_of_proc[dest])
for (auto pnum : (*this)[segi].PNums())
f(pnum, dest);
}
};
/** count vertices per proc and procs per vertex **/
tbuildvertexa.Start();
iterate_vertices([&](auto vertex, auto dest){
auto countit = [&] (auto vertex, auto dest) {
if (vert_flag[vertex] < dest)
{
vert_flag[vertex] = dest;
num_verts_on_proc[dest]++;
num_procs_on_vert[vertex]++;
}
};
countit(vertex, dest);
for (auto v : per_verts_trans[vertex])
countit(v, dest);
});
tbuildvertexa.Stop();
tbuildvertexb.Start();
TABLE<PointIndex> verts_of_proc (num_verts_on_proc);
TABLE<int, PointIndex::BASE> procs_of_vert (num_procs_on_vert);
TABLE<int, PointIndex::BASE> loc_num_of_vert (num_procs_on_vert);
/** Write vertex/proc mappingfs to tables **/
iterate_vertices([&](auto vertex, auto dest) {
auto addit = [&] (auto vertex, auto dest) {
if (vert_flag[vertex] < dest)
{
vert_flag[vertex] = dest;
procs_of_vert.Add (vertex, dest);
}
};
addit(vertex, dest);
for (auto v : per_verts_trans[vertex])
addit(v, dest);
});
tbuildvertexb.Stop();
/**
local vertex numbers on distant procs
(I think this was only used for debugging??)
**/
// for (int vert = 1; vert <= GetNP(); vert++ )
for (PointIndex vert : Points().Range())
{
NgFlatArray<int> procs = procs_of_vert[vert];
for (int j = 0; j < procs.Size(); j++)
{
int dest = procs[j];
// !! we also use this as offsets for MPI-type, if this is changed, also change ReceiveParallelMesh
verts_of_proc.Add (dest, vert - IndexBASE<T_POINTS::index_type>());
loc_num_of_vert.Add (vert, verts_of_proc[dest].Size() -1+IndexBASE<T_POINTS::index_type>());
}
}
tbuildvertex.Stop();
PrintMessage ( 3, "Sending Vertices - vertices");
Array<NG_MPI_Datatype> point_types(ntasks-1);
for (int dest = 1; dest < ntasks; dest++)
{
NgFlatArray<PointIndex> verts = verts_of_proc[dest];
// sendrequests.Append (MyMPI_ISend (verts, dest, NG_MPI_TAG_MESH+1, comm));
sendrequests += comm.ISend (FlatArray<PointIndex>(verts), dest, NG_MPI_TAG_MESH+1);
NG_MPI_Datatype mptype = MeshPoint::MyGetMPIType();
int numv = verts.Size();
NgArray<int> blocklen (numv);
blocklen = 1;
NG_MPI_Type_indexed (numv, (numv == 0) ? nullptr : &blocklen[0],
(numv == 0) ? nullptr : reinterpret_cast<int*> (&verts[0]),
mptype, &point_types[dest-1]);
NG_MPI_Type_commit (&point_types[dest-1]);
NG_MPI_Request request;
NG_MPI_Isend( points.Data(), 1, point_types[dest-1], dest, NG_MPI_TAG_MESH+1, comm, &request);
sendrequests += request;
}
/**
Next, we send the identifications themselves.
Info about periodic identifications sent to each proc is an array of
integers.
- maxidentnr
- type for each identification
- nr of pairs for each identification (each pair is local!)
- pairs for each periodic ident (global numbers)
**/
PrintMessage ( 3, "Sending Vertices - identifications");
int maxidentnr = idents.GetMaxNr();
NgArray<int> ppd_sizes(ntasks);
ppd_sizes = 1 + 2*maxidentnr;
for (int idnr = 1; idnr < idents.GetMaxNr()+1; idnr++)
{
if(idents.GetType(idnr)!=Identifications::PERIODIC) continue;
idents.GetPairs(idnr, pp2);
for(int j = 0; j<pp2.Size(); j++)
{
INDEX_2 & pair = pp2[j];
// both are on same procs!
auto ps = procs_of_vert[pair.I1()];
for (int l = 0; l < ps.Size(); l++)
{
ppd_sizes[ps[l]] += 2;
}
}
}
TABLE<int> pp_data(ppd_sizes);
for(int dest = 0; dest < ntasks; dest++)
pp_data.Add(dest, maxidentnr);
for (int dest = 0; dest < ntasks; dest++)
{
for (int idnr = 1; idnr < idents.GetMaxNr()+1; idnr++)
pp_data.Add(dest, idents.GetType(idnr));
for (int idnr = 1; idnr < idents.GetMaxNr()+1; idnr++)
pp_data.Add(dest, 0);
}
for (int idnr = 1; idnr < idents.GetMaxNr()+1; idnr++)
{
if(idents.GetType(idnr)!=Identifications::PERIODIC) continue;
idents.GetPairs(idnr, pp2);
for(int j = 0; j<pp2.Size(); j++)
{
INDEX_2 & pair = pp2[j];
auto ps = procs_of_vert[pair.I1()];
for (int l = 0; l < ps.Size(); l++)
{
auto p = ps[l];
pp_data[p][maxidentnr + idnr]++;
pp_data.Add(p, pair.I1());
pp_data.Add(p, pair.I2());
}
}
}
NgMPI_Requests req_per;
for(int dest = 1; dest < ntasks; dest++)
// req_per.Append(MyMPI_ISend(pp_data[dest], dest, NG_MPI_TAG_MESH+1, comm));
req_per += comm.ISend(FlatArray<int>(pp_data[dest]), dest, NG_MPI_TAG_MESH+1);
req_per.WaitAll();
PrintMessage ( 3, "Sending Vertices - distprocs");
tbuilddistpnums.Start();
Array<int> num_distpnums(ntasks);
num_distpnums = 0;
// for (int vert = 1; vert <= GetNP(); vert++)
for (PointIndex vert : Points().Range())
{
FlatArray<int> procs = procs_of_vert[vert];
for (auto p : procs)
num_distpnums[p] += 3 * (procs.Size()-1);
}
DynamicTable<int> distpnums (num_distpnums);
// for (int vert = 1; vert <= GetNP(); vert++)
for (PointIndex vert : Points().Range())
{
NgFlatArray<int> procs = procs_of_vert[vert];
for (int j = 0; j < procs.Size(); j++)
for (int k = 0; k < procs.Size(); k++)
if (j != k)
{
distpnums.Add (procs[j], loc_num_of_vert[vert][j]);
distpnums.Add (procs[j], procs_of_vert[vert][k]);
distpnums.Add (procs[j], loc_num_of_vert[vert][k]);
}
}
tbuilddistpnums.Stop();
for ( int dest = 1; dest < ntasks; dest ++ )
sendrequests += comm.ISend (distpnums[dest], dest, NG_MPI_TAG_MESH+1);
PrintMessage ( 3, "Sending elements" );
tbuildelementtable.Start();
Array<int> elarraysize (ntasks);
elarraysize = 0;
// for (int ei = 1; ei <= GetNE(); ei++)
for (ElementIndex ei : VolumeElements().Range())
{
const Element & el = VolumeElement (ei);
// int dest = el.GetPartition();
int dest = vol_partition[ei];
elarraysize[dest] += 3 + el.GetNP();
}
DynamicTable<int> elementarrays(elarraysize);
for (ElementIndex ei : VolumeElements().Range())
{
const Element & el = VolumeElement (ei);
int dest = vol_partition[ei];
elementarrays.Add (dest, int(ei+1));
elementarrays.Add (dest, el.GetIndex());
elementarrays.Add (dest, el.GetNP());
for (PointIndex pi : el.PNums())
elementarrays.Add (dest, pi);
}
tbuildelementtable.Stop();
for (int dest = 1; dest < ntasks; dest ++ )
sendrequests += comm.ISend (elementarrays[dest], dest, NG_MPI_TAG_MESH+2);
PrintMessage ( 3, "Sending Face Descriptors" );
Array<double> fddata (6 * GetNFD());
for (int fdi = 1; fdi <= GetNFD(); fdi++)
{
fddata[6*fdi-6] = GetFaceDescriptor(fdi).SurfNr();
fddata[6*fdi-5] = GetFaceDescriptor(fdi).DomainIn();
fddata[6*fdi-4] = GetFaceDescriptor(fdi).DomainOut();
fddata[6*fdi-3] = GetFaceDescriptor(fdi).BCProperty();
fddata[6*fdi-2] = GetFaceDescriptor(fdi).domin_singular;
fddata[6*fdi-1] = GetFaceDescriptor(fdi).domout_singular;
}
for (int dest = 1; dest < ntasks; dest++)
sendrequests += comm.ISend (fddata, dest, NG_MPI_TAG_MESH+3);
/** Surface Elements **/
PrintMessage ( 3, "Sending Surface elements" );
// build sel-identification
size_t nse = GetNSE();
NgArray<SurfaceElementIndex> ided_sel(nse);
ided_sel = -1;
[[maybe_unused]] bool has_ided_sels = false;
if(GetNE() && has_periodic) //we can only have identified surf-els if we have vol-els (right?)
{
Array<SurfaceElementIndex> os1, os2;
for(SurfaceElementIndex sei = 0; sei < GetNSE(); sei++)
{
if(ided_sel[sei]!=-1) continue;
const Element2d & sel = (*this)[sei];
auto points = sel.PNums();
auto ided1 = per_verts[points[0]];
os1.SetSize(0);
for (int j = 0; j < ided1.Size(); j++)
os1.Append(GetTopology().GetVertexSurfaceElements(ided1[j]));
for (int j = 1; j < points.Size(); j++)
{
os2.SetSize(0);
auto p2 = points[j];
auto ided2 = per_verts[p2];
for (int l = 0; l < ided2.Size(); l++)
os2.Append(GetTopology().GetVertexSurfaceElements(ided2[l]));
for (int m = 0; m<os1.Size(); m++) {
if(!os2.Contains(os1[m])) {
os1.DeleteElement(m);
m--;
}
}
}
if(!os1.Size()) continue;
if(os1.Size()>1) {
throw NgException("SurfaceElement identified with more than one other??");
}
// const Element2d & sel2 = (*this)[sei];
// auto points2 = sel2.PNums();
has_ided_sels = true;
ided_sel[sei] = os1[0];
ided_sel[os1[0]] = sei;
}
}
// build sel data to send
auto iterate_sels = [&](auto f) {
for (SurfaceElementIndex sei = 0; sei < GetNSE(); sei++ )
{
const Element2d & sel = (*this)[sei];
// int dest = (*this)[sei].GetPartition();
int dest = surf_partition[sei];
f(sei, sel, dest);
if(ided_sel[sei]!=-1)
{
// int dest2 = (*this)[ided_sel[sei]].GetPartition();
int dest2 = surf_partition[ided_sel[sei]];
f(sei, sel, dest2);
}
}
};
Array <int> nlocsel(ntasks), bufsize(ntasks);
nlocsel = 0;
bufsize = 0;
iterate_sels([&](SurfaceElementIndex sei, const Element2d & sel, int dest){
nlocsel[dest]++;
bufsize[dest]++;
});
DynamicTable<SelPackage> selbuf(bufsize);
iterate_sels([&](SurfaceElementIndex sei, const auto & sel, int dest) {
selbuf.Add (dest, SelPackage(*this, sei));
});
// distribute sel data
for (int dest = 1; dest < ntasks; dest++)
sendrequests += comm.ISend(selbuf[dest], dest, NG_MPI_TAG_MESH+4);
/** Segments **/
PrintMessage ( 3, "Sending Edge Segments");
auto iterate_segs1 = [&](auto f) {
NgArray<SegmentIndex> osegs1, osegs2, osegs_both;
NgArray<int> type1, type2;
for(SegmentIndex segi = 0; segi < GetNSeg(); segi++)
{
const Segment & seg = (*this)[segi];
int segnp = seg.GetNP();
PointIndex pi1 = seg[0];
auto ided1 = per_verts[pi1];
PointIndex pi2 = seg[1];
auto ided2 = per_verts[pi2];
if (!(ided1.Size() && ided2.Size())) continue;
osegs1.SetSize(0);
type1.SetSize(0);
for (int l = 0; l<ided1.Size(); l++)
{
auto ospart = GetTopology().GetVertexSegments(ided1[l]);
for(int j=0; j<ospart.Size(); j++)
{
if(osegs1.Contains(ospart[j]))
throw NgException("Periodic Mesh did something weird.");
osegs1.Append(ospart[j]);
type1.Append(idents.GetSymmetric(pi1, ided1[l]));
}
}
osegs2.SetSize(0);
type2.SetSize(0);
for (int l = 0; l<ided2.Size(); l++)
{
auto ospart = GetTopology().GetVertexSegments(ided2[l]);
for(int j=0; j<ospart.Size(); j++)
{
if(osegs2.Contains(ospart[j]))
throw NgException("Periodic Mesh did something weird.");
osegs2.Append(ospart[j]);
type2.Append(idents.GetSymmetric(pi2, ided2[l]));
}
}
osegs_both.SetSize(0);
for (int l = 0; l<osegs1.Size(); l++) {
auto pos = osegs2.Pos(osegs1[l]);
if (pos == -1) continue;
if (type1[l] != type2[pos]) continue;
osegs_both.Append(osegs1[l]);
}
for(int l = 0; l<osegs_both.Size(); l++) {
int segnp2 = (*this)[osegs_both[l]].GetNP();
if(segnp!=segnp2)
throw NgException("Tried to identify non-curved and curved Segment!");
}
for(int l = 0; l<osegs_both.Size(); l++) {
f(segi, osegs_both[l]);
}
}
};
NgArray<int> per_seg_size(GetNSeg());
per_seg_size = 0;
iterate_segs1([&](SegmentIndex segi1, SegmentIndex segi2)
{ per_seg_size[segi1]++; });
TABLE<SegmentIndex> per_seg(per_seg_size);
iterate_segs1([&](SegmentIndex segi1, SegmentIndex segi2)
{ per_seg.Add(segi1, segi2); });
// make per_seg transitive
auto iterate_per_seg_trans = [&](auto f){
NgArray<SegmentIndex> allsegs;
for (SegmentIndex segi = 0; segi < GetNSeg(); segi++)
{
allsegs.SetSize(0);
allsegs.Append(per_seg[segi]);
bool changed = true;
while (changed)
{
changed = false;
for (int j = 0; j<allsegs.Size(); j++)
{
auto persegs2 = per_seg[allsegs[j]];
for (int l = 0; l<persegs2.Size(); l++)
{
auto addseg = persegs2[l];
if (allsegs.Contains(addseg) || addseg==segi) continue;
allsegs.Append(addseg);
changed = true;
}
}
}
f(segi, allsegs);
}
};
iterate_per_seg_trans([&](SegmentIndex segi, NgArray<SegmentIndex> & segs){
for (int j = 0; j < segs.Size(); j++)
per_seg_size[segi] = segs.Size();
});
TABLE<SegmentIndex> per_seg_trans(per_seg_size);
iterate_per_seg_trans([&](SegmentIndex segi, NgArray<SegmentIndex> & segs){
for (int j = 0; j < segs.Size(); j++)
per_seg_trans.Add(segi, segs[j]);
});
// build segment data
NgArray<int> dests;
auto iterate_segs2 = [&](auto f)
{
for (SegmentIndex segi = 0; segi<GetNSeg(); segi++)
{
const Segment & seg = (*this)[segi];
dests.SetSize(0);
// dests.Append(seg.GetPartition());
dests.Append(seg_partition[segi]);
for (int l = 0; l < per_seg_trans[segi].Size(); l++)
{
// int dest2 = (*this)[per_seg_trans[segi][l]].GetPartition();
int dest2 = seg_partition[per_seg_trans[segi][l]];
if(!dests.Contains(dest2))
dests.Append(dest2);
}
for (int l = 0; l < dests.Size(); l++)
f(segi, seg, dests[l]);
}
};
NgArray<int> nloc_seg(ntasks);
// bufsize = 1; //was originally this - why??
bufsize = 0;
nloc_seg = 0;
iterate_segs2([&](auto segi, const auto & seg, int dest)
{
nloc_seg[dest]++;
bufsize[dest] += 14;
});
DynamicTable<double> segm_buf(bufsize);
iterate_segs2([&](auto segi, const auto & seg, int dest)
{
segm_buf.Add (dest, segi);
segm_buf.Add (dest, seg.si);
segm_buf.Add (dest, seg.pnums[0]);
segm_buf.Add (dest, seg.pnums[1]);
segm_buf.Add (dest, seg.geominfo[0].trignum);
segm_buf.Add (dest, seg.geominfo[1].trignum);
segm_buf.Add (dest, seg.surfnr1);
segm_buf.Add (dest, seg.surfnr2);
segm_buf.Add (dest, seg.edgenr);
segm_buf.Add (dest, seg.epgeominfo[0].dist);
segm_buf.Add (dest, seg.epgeominfo[1].edgenr);
segm_buf.Add (dest, seg.epgeominfo[1].dist);
segm_buf.Add (dest, seg.singedge_right);
segm_buf.Add (dest, seg.singedge_left);
});
// distribute segment data
for (int dest = 1; dest < ntasks; dest++)
sendrequests += comm.ISend(segm_buf[dest], dest, NG_MPI_TAG_MESH+5);
/** Point-Elements **/
PrintMessage ( 3, "Point-Elements ...");
auto iterate_zdes = [&](auto f) {
for (auto k : Range(pointelements)) {
auto & el = pointelements[k];
PointElPackage pack(el);
auto dests = procs_of_vert[el.pnum];
for (auto dest : dests)
{ f(pack, dest); }
}
};
bufsize = 0;
iterate_zdes([&](const auto & pack, auto dest) { bufsize[dest]++; });
DynamicTable<PointElPackage> zde_buf(bufsize); // zero dim elements
iterate_zdes([&](const auto & pack, auto dest) { zde_buf.Add(dest, pack); });
for (int dest = 1; dest < ntasks; dest++)
sendrequests += comm.ISend(zde_buf[dest], dest, NG_MPI_TAG_MESH+6);
PrintMessage ( 3, "now wait ...");
sendrequests.WaitAll();
// clean up MPI-datatypes we allocated earlier
for (auto t : point_types)
{ NG_MPI_Type_free(&t); }
paralleltop -> SetNV_Loc2Glob (0);
paralleltop -> SetNV (0);
paralleltop -> EnumeratePointsGlobally();
PrintMessage ( 3, "Sending names");
/** Send bc/mat/cd*-names **/
// nr of names
std::array<int,4> nnames{0,0,0,0};
nnames[0] = materials.Size();
nnames[1] = bcnames.Size();
nnames[2] = GetNCD2Names();
nnames[3] = GetNCD3Names();
int tot_nn = nnames[0] + nnames[1] + nnames[2] + nnames[3];
NgMPI_Requests requ;
requ += comm.IBcast (nnames);
auto iterate_names = [&](auto func) {
for (int k = 0; k < nnames[0]; k++) func(materials[k]);
for (int k = 0; k < nnames[1]; k++) func(bcnames[k]);
for (int k = 0; k < nnames[2]; k++) func(cd2names[k]);
for (int k = 0; k < nnames[3]; k++) func(cd3names[k]);
};
// sizes of names
Array<int> name_sizes(tot_nn);
tot_nn = 0;
iterate_names([&](auto ptr) { name_sizes[tot_nn++] = (ptr==NULL) ? 0 : ptr->size(); });
requ += comm.IBcast (name_sizes);
// names
int strs = 0;
iterate_names([&](auto ptr) { strs += (ptr==NULL) ? 0 : ptr->size(); });
Array<char> compiled_names(strs);
strs = 0;
iterate_names([&](auto ptr) {
if (ptr==NULL) return;
auto& name = *ptr;
for (int j=0; j < name.size(); j++) compiled_names[strs++] = name[j];
});
requ += comm.IBcast (compiled_names);
PrintMessage ( 3, "wait for names");
requ.WaitAll();
comm.Barrier();
PrintMessage( 3, "Clean up local memory");
auto & self = const_cast<Mesh&>(*this);
self.points = T_POINTS(0);
self.surfelements = Array<Element2d>(0);
self.volelements = Array<Element>(0);
self.segments = Array<Segment>(0);
self.pointelements = Array<Element0d>(0);
self.lockedpoints = Array<PointIndex>(0);
/*
auto cleanup_ptr = [](auto & ptr) {
if (ptr != nullptr) {
delete ptr;
ptr = nullptr;
}
};
cleanup_ptr(self.boundaryedges);
cleanup_ptr(self.segmentht);
cleanup_ptr(self.surfelementht);
*/
self.boundaryedges = nullptr;
self.segmentht = nullptr;
self.surfelementht = nullptr;
self.openelements = NgArray<Element2d>(0);
self.opensegments = NgArray<Segment>(0);
self.numvertices = 0;
self.mlbetweennodes = Array<PointIndices<2>,PointIndex> (0);
self.mlparentelement = Array<ElementIndex, ElementIndex>(0);
self.mlparentsurfaceelement = Array<SurfaceElementIndex, SurfaceElementIndex>(0);
self.curvedelems = make_unique<CurvedElements> (self);
self.clusters = make_unique<AnisotropicClusters> (self);
self.ident = make_unique<Identifications> (self);
self.topology = MeshTopology(*this);
self.topology.Update();
self.BuildElementSearchTree(3);
// const_cast<Mesh&>(*this).DeleteMesh();
// paralleltop -> SetNV (0);
// paralleltop->EnumeratePointsGlobally();
PrintMessage( 3, "send mesh complete");
}
// workers receive the mesh from the master
void Mesh :: ReceiveParallelMesh ( )
{
Timer timer("ReceiveParallelMesh");
Timer timer_pts("Receive points");
Timer timer_els("Receive elements");
Timer timer_sels("Receive surface elements");
RegionTimer reg(timer);
NgMPI_Comm comm = GetCommunicator();
int id = comm.Rank();
// int ntasks = comm.Size();
int dim;
comm.Bcast(dim);
SetDimension(dim);
// Receive number of local elements
int nelloc;
comm.Scatter (nelloc);
paralleltop -> SetNE (nelloc);
// receive vertices
timer_pts.Start();
Array<int> verts;
comm.Recv (verts, 0, NG_MPI_TAG_MESH+1);
int numvert = verts.Size();
paralleltop -> SetNV (numvert);
paralleltop -> SetNV_Loc2Glob (numvert);
// INDEX_CLOSED_HASHTABLE<int> glob2loc_vert_ht (3*numvert+1);
INDEX_HASHTABLE<int> glob2loc_vert_ht (3*numvert+1);
for (int vert = 0; vert < numvert; vert++)
{
PointIndex globvert = verts[vert] + IndexBASE<T_POINTS::index_type>();
// paralleltop->SetLoc2Glob_Vert ( vert+1, globvert );
paralleltop->L2G (PointIndex(vert+PointIndex::BASE)) = globvert;
glob2loc_vert_ht.Set (globvert, vert+PointIndex::BASE);
}
for (int i = 0; i < numvert; i++)
AddPoint (netgen::Point<3> (0,0,0));
NG_MPI_Datatype mptype = MeshPoint::MyGetMPIType();
NG_MPI_Status status;
NG_MPI_Recv( points.Data(), numvert, mptype, 0, NG_MPI_TAG_MESH+1, comm, &status);
Array<int> pp_data;
comm.Recv(pp_data, 0, NG_MPI_TAG_MESH+1);
int maxidentnr = pp_data[0];
auto & idents = GetIdentifications();
for (int idnr = 1; idnr < maxidentnr+1; idnr++)
idents.SetType(idnr, (Identifications::ID_TYPE)pp_data[idnr]);
int offset = 2*maxidentnr+1;
for(int idnr = 1; idnr < maxidentnr+1; idnr++)
{
int npairs = pp_data[maxidentnr+idnr];
NgFlatArray<int> pairdata(2*npairs, &pp_data[offset]);
offset += 2*npairs;
for (int k = 0; k<npairs; k++) {
PointIndex loc1 = glob2loc_vert_ht.Get(pairdata[2*k]);
PointIndex loc2 = glob2loc_vert_ht.Get(pairdata[2*k+1]);
idents.Add(loc1, loc2, idnr);
}
}
Array<int> dist_pnums;
comm.Recv (dist_pnums, 0, NG_MPI_TAG_MESH+1);
for (int hi = 0; hi < dist_pnums.Size(); hi += 3)
paralleltop ->
// SetDistantPNum (dist_pnums[hi+1], dist_pnums[hi]); // , dist_pnums[hi+2]);
AddDistantProc (PointIndex(dist_pnums[hi]), dist_pnums[hi+1]);
timer_pts.Stop();
*testout << "got " << numvert << " vertices" << endl;
{
RegionTimer reg(timer_els);
Array<int> elarray;
comm.Recv (elarray, 0, NG_MPI_TAG_MESH+2);
for (int ind = 0, elnum = 1; ind < elarray.Size(); elnum++)
{
paralleltop->SetLoc2Glob_VolEl ( elnum, elarray[ind++]);
int index = elarray[ind++];
Element el(elarray[ind++]);
el.SetIndex(index);
for ( int j = 0; j < el.GetNP(); j++)
el[j] = glob2loc_vert_ht.Get (elarray[ind++]);
AddVolumeElement (el);
}
}
{
Array<double> fddata;
comm.Recv (fddata, 0, NG_MPI_TAG_MESH+3);
for (int i = 0; i < fddata.Size(); i += 6)
{
int faceind = AddFaceDescriptor
(FaceDescriptor(int(fddata[i]), int(fddata[i+1]), int(fddata[i+2]), 0));
GetFaceDescriptor(faceind).SetBCProperty (int(fddata[i+3]));
GetFaceDescriptor(faceind).domin_singular = fddata[i+4];
GetFaceDescriptor(faceind).domout_singular = fddata[i+5];
}
}
{
RegionTimer reg(timer_sels);
Array<SelPackage> selbuf;
comm.Recv ( selbuf, 0, NG_MPI_TAG_MESH+4);
int nlocsel = selbuf.Size();
paralleltop -> SetNSE ( nlocsel );
int sel = 0;
for (auto k : Range(selbuf)) {
auto & pack = selbuf[k];
Element2d el(pack.np);
pack.Unpack(el);
/** map global point numbers to local ones **/
for (int k : Range(1, 1+el.GetNP()))
{ el.PNum(k) = glob2loc_vert_ht.Get(el.PNum(k)); }
paralleltop->SetLoc2Glob_SurfEl (sel+1, pack.sei);
AddSurfaceElement (el);
sel++;
}
}
{
// NgArray<double> segmbuf;
// MyMPI_Recv ( segmbuf, 0, NG_MPI_TAG_MESH+5, comm);
Array<double> segmbuf;
comm.Recv (segmbuf, 0, NG_MPI_TAG_MESH+5);
Segment seg;
int globsegi;
int ii = 0;
int segi = 1;
int nsegloc = int ( segmbuf.Size() / 14 ) ;
paralleltop -> SetNSegm ( nsegloc );
while ( ii < segmbuf.Size() )
{
globsegi = int (segmbuf[ii++]);
seg.si = int (segmbuf[ii++]);
seg.pnums[0] = glob2loc_vert_ht.Get (int(segmbuf[ii++]));
seg.pnums[1] = glob2loc_vert_ht.Get (int(segmbuf[ii++]));
seg.geominfo[0].trignum = int( segmbuf[ii++] );
seg.geominfo[1].trignum = int ( segmbuf[ii++]);
seg.surfnr1 = int ( segmbuf[ii++]);
seg.surfnr2 = int ( segmbuf[ii++]);
seg.edgenr = int ( segmbuf[ii++]);
seg.epgeominfo[0].dist = segmbuf[ii++];
seg.epgeominfo[1].edgenr = int (segmbuf[ii++]);
seg.epgeominfo[1].dist = segmbuf[ii++];
seg.singedge_left = segmbuf[ii++];
seg.singedge_right = segmbuf[ii++];
seg.epgeominfo[0].edgenr = seg.epgeominfo[1].edgenr;
seg.domin = seg.surfnr1;
seg.domout = seg.surfnr2;
if ( seg.pnums[0].IsValid() && seg.pnums[1].IsValid() )
{
paralleltop-> SetLoc2Glob_Segm ( segi, globsegi );
AddSegment (seg);
segi++;
}
}
}
{ /** 0d-Elements **/
Array<PointElPackage> zdes;
comm.Recv ( zdes, 0, NG_MPI_TAG_MESH+6);
pointelements.SetSize(zdes.Size());
for (auto k : Range(pointelements)) {
auto & el = pointelements[k];
el.pnum = glob2loc_vert_ht.Get(zdes[k].pnum);
el.index = zdes[k].index;
}
}
// paralleltop -> SetNV_Loc2Glob (0);
paralleltop -> EnumeratePointsGlobally();
/** Recv bc-names **/
/*
ArrayMem<int,4> nnames{0,0,0,0};
// NG_MPI_Recv(nnames, 4, NG_MPI_INT, 0, NG_MPI_TAG_MESH+6, comm, NG_MPI_STATUS_IGNORE);
comm.Recv(nnames, 0, NG_MPI_TAG_MESH+7);
*/
// Array<NG_MPI_Request> recvrequests(1);
std::array<int,4> nnames;
/*
recvrequests[0] = comm.IBcast (nnames);
MyMPI_WaitAll (recvrequests);
*/
comm.IBcast (nnames).Wait();
// cout << "nnames = " << FlatArray(nnames) << endl;
materials.SetSize(nnames[0]);
bcnames.SetSize(nnames[1]);
cd2names.SetSize(nnames[2]);
cd3names.SetSize(nnames[3]);
int tot_nn = nnames[0] + nnames[1] + nnames[2] + nnames[3];
Array<int> name_sizes(tot_nn);
// NG_MPI_Recv(&name_sizes[0], tot_nn, NG_MPI_INT, 0, NG_MPI_TAG_MESH+7, comm, NG_MPI_STATUS_IGNORE);
/*
recvrequests[0] = comm.IBcast (name_sizes);
MyMPI_WaitAll (recvrequests);
*/
comm.IBcast (name_sizes).Wait();
int tot_size = 0;
for (int k = 0; k < tot_nn; k++) tot_size += name_sizes[k];
// NgArray<char> compiled_names(tot_size);
// NG_MPI_Recv(&(compiled_names[0]), tot_size, NG_MPI_CHAR, 0, NG_MPI_TAG_MESH+7, comm, NG_MPI_STATUS_IGNORE);
Array<char> compiled_names(tot_size);
// recvrequests[0] = comm.IBcast (compiled_names);
// MyMPI_WaitAll (recvrequests);
comm.IBcast (compiled_names).Wait();
tot_nn = tot_size = 0;
auto write_names = [&] (auto & array) {
for (int k = 0; k < array.Size(); k++) {
int s = name_sizes[tot_nn];
array[k] = s ? new string(&compiled_names[tot_size], s) : new string("");
tot_nn++;
tot_size += s;
}
};
write_names(materials);
write_names(bcnames);
write_names(cd2names);
write_names(cd3names);
comm.Barrier();
int timerloc = NgProfiler::CreateTimer ("Update local mesh");
int timerloc2 = NgProfiler::CreateTimer ("CalcSurfacesOfNode");
NgProfiler::RegionTimer regloc(timerloc);
stringstream str;
str << "p" << id << ": got " << GetNE() << " elements and "
<< GetNSE() << " surface elements";
PrintMessage(2, str.str());
// cout << str.str() << endl;
// PrintMessage (2, "Got ", GetNE(), " elements and ", GetNSE(), " surface elements");
// PrintMessage (2, "Got ", GetNSE(), " surface elements");
NgProfiler::StartTimer (timerloc2);
CalcSurfacesOfNode ();
NgProfiler::StopTimer (timerloc2);
topology.Update();
clusters -> Update();
// paralleltop -> UpdateCoarseGrid();
// paralleltop->EnumeratePointsGlobally();
SetNextMajorTimeStamp();
}
// distribute the mesh to the worker processors
// call it only for the master !
void Mesh :: Distribute ()
{
NgMPI_Comm comm = GetCommunicator();
int id = comm.Rank();
int ntasks = comm.Size();
if (id != 0 || ntasks == 1 ) return;
#ifdef METIS
if (vol_partition.Size() < GetNE() || surf_partition.Size() < GetNSE() ||
seg_partition.Size() < GetNSeg())
ParallelMetis (comm.Size());
#else
for (ElementIndex ei = 0; ei < GetNE(); ei++)
(*this)[ei].SetPartition(ntasks * ei/GetNE() + 1);
#endif
/*
for (ElementIndex ei = 0; ei < GetNE(); ei++)
*testout << "el(" << ei << ") is in part " << (*this)[ei].GetPartition() << endl;
for (SurfaceElementIndex ei = 0; ei < GetNSE(); ei++)
*testout << "sel(" << int(ei) << ") is in part " << (*this)[ei].GetPartition() << endl;
*/
// MyMPI_SendCmd ("mesh");
SendRecvMesh ();
}
#ifdef METIS5
void Mesh :: ParallelMetis (int nproc)
{
PrintMessage (3, "call metis 5 ...");
int timer = NgProfiler::CreateTimer ("Mesh::Partition");
NgProfiler::RegionTimer reg(timer);
idx_t ne = GetNE() + GetNSE() + GetNSeg();
idx_t nn = GetNP();
NgArray<idx_t> eptr, eind;
for (int i = 0; i < GetNE(); i++)
{
eptr.Append (eind.Size());
const Element & el = VolumeElement(i+1);
for (int j = 0; j < el.GetNP(); j++)
eind.Append (el[j]-IndexBASE<PointIndex>());
}
for (int i = 0; i < GetNSE(); i++)
{
eptr.Append (eind.Size());
const Element2d & el = SurfaceElement(i+1);
for (int j = 0; j < el.GetNP(); j++)
eind.Append (el[j]-IndexBASE<PointIndex>());
}
for (int i = 0; i < GetNSeg(); i++)
{
eptr.Append (eind.Size());
const Segment & el = LineSegment(i+1);
eind.Append (el[0]-IndexBASE<PointIndex>());
eind.Append (el[1]-IndexBASE<PointIndex>());
}
eptr.Append (eind.Size());
NgArray<idx_t> epart(ne), npart(nn);
idxtype nparts = nproc-1; // GetCommunicator().Size()-1;
vol_partition.SetSize(GetNE());
surf_partition.SetSize(GetNSE());
seg_partition.SetSize(GetNSeg());
if (nparts == 1)
{
for (int i = 0; i < GetNE(); i++)
vol_partition[i]= 1;
for (int i = 0; i < GetNSE(); i++)
surf_partition[i] = 1;
for (int i = 0; i < GetNSeg(); i++)
seg_partition[i] = 1;
}
else
{
idxtype edgecut;
idxtype ncommon = GetDimension();
PrintMessage (3, "metis start");
static Timer tm("metis library");
tm.Start();
METIS_PartMeshDual (&ne, &nn, &eptr[0], &eind[0], NULL, NULL, &ncommon, &nparts,
NULL, NULL,
&edgecut, &epart[0], &npart[0]);
tm.Stop();
PrintMessage (3, "metis complete");
for (int i = 0; i < GetNE(); i++)
vol_partition[i]= epart[i] + 1;
for (int i = 0; i < GetNSE(); i++)
surf_partition[i] = epart[i+GetNE()] + 1;
for (int i = 0; i < GetNSeg(); i++)
seg_partition[i] = epart[i+GetNE()+GetNSE()] + 1;
}
// surface elements attached to volume elements
NgArray<bool, PointIndex::BASE> boundarypoints (GetNP());
boundarypoints = false;
if(GetDimension() == 3)
for (SurfaceElementIndex sei = 0; sei < GetNSE(); sei++)
{
const Element2d & el = (*this)[sei];
for (int j = 0; j < el.GetNP(); j++)
boundarypoints[el[j]] = true;
}
else
for (SegmentIndex segi = 0; segi < GetNSeg(); segi++)
{
const Segment & seg = (*this)[segi];
for (int j = 0; j < 2; j++)
boundarypoints[seg[j]] = true;
}
// Build Pnt2Element table, boundary points only
NgArray<int, PointIndex::BASE> cnt(GetNP());
cnt = 0;
auto loop_els_2d = [&](auto f) {
for (SurfaceElementIndex sei = 0; sei < GetNSE(); sei++)
{
const Element2d & el = (*this)[sei];
for (int j = 0; j < el.GetNP(); j++) {
f(el[j], sei);
}
}
};
auto loop_els_3d = [&](auto f) {
for (ElementIndex ei = 0; ei < GetNE(); ei++)
{
const Element & el = (*this)[ei];
for (int j = 0; j < el.GetNP(); j++)
f(el[j], ei);
}
};
auto loop_els = [&](auto f)
{
if (GetDimension() == 3 )
loop_els_3d(f);
else
loop_els_2d(f);
};
loop_els([&](auto vertex, int index)
{
if(boundarypoints[vertex])
cnt[vertex]++;
});
TABLE<int, PointIndex::BASE> pnt2el(cnt);
loop_els([&](auto vertex, int index)
{
if(boundarypoints[vertex])
pnt2el.Add(vertex, index);
});
if (GetDimension() == 3)
{
for (SurfaceElementIndex sei = 0; sei < GetNSE(); sei++)
{
Element2d & sel = (*this)[sei];
PointIndex pi1 = sel[0];
// NgFlatArray<ElementIndex> els = pnt2el[pi1];
NgFlatArray<int> els = pnt2el[pi1];
// sel.SetPartition (-1);
surf_partition[sei] = -1;
for (int j = 0; j < els.Size(); j++)
{
const Element & el = (*this)[ElementIndex(els[j])];
bool hasall = true;
for (int k = 0; k < sel.GetNP(); k++)
{
bool haspi = false;
for (int l = 0; l < el.GetNP(); l++)
if (sel[k] == el[l])
haspi = true;
if (!haspi) hasall = false;
}
if (hasall)
{
// sel.SetPartition (el.GetPartition());
surf_partition[sei] = vol_partition[ElementIndex(els[j])];
break;
}
}
// if (sel.GetPartition() == -1)
if (surf_partition[sei] == -1)
cerr << "no volume element found" << endl;
}
for (SegmentIndex si = 0; si < GetNSeg(); si++)
{
Segment & sel = (*this)[si];
PointIndex pi1 = sel[0];
NgFlatArray<int> els = pnt2el[pi1];
// sel.SetPartition (-1);
seg_partition[si] = -1;
for (int j = 0; j < els.Size(); j++)
{
const Element & el = (*this)[ElementIndex(els[j])];
bool haspi[9] = { false }; // max surfnp
for (int k = 0; k < 2; k++)
for (int l = 0; l < el.GetNP(); l++)
if (sel[k] == el[l])
haspi[k] = true;
bool hasall = true;
for (int k = 0; k < sel.GetNP(); k++)
if (!haspi[k]) hasall = false;
if (hasall)
{
// sel.SetPartition (el.GetPartition());
seg_partition[si] = vol_partition[ElementIndex(els[j])];
break;
}
}
// if (sel.GetPartition() == -1)
if (seg_partition[si] == -1)
cerr << "no volume element found" << endl;
}
}
else
{
for (SegmentIndex segi = 0; segi < GetNSeg(); segi++)
{
Segment & seg = (*this)[segi];
// seg.SetPartition(-1);
seg_partition[segi] = -1;
PointIndex pi1 = seg[0];
NgFlatArray<int> sels = pnt2el[pi1];
for (int j = 0; j < sels.Size(); j++)
{
SurfaceElementIndex sei = sels[j];
Element2d & se = (*this)[sei];
bool found = false;
for (int l = 0; l < se.GetNP(); l++ && !found)
found |= (se[l]==seg[1]);
if(found) {
// seg.SetPartition(se.GetPartition());
seg_partition[segi] = surf_partition[sei];
break;
}
}
// if (seg.GetPartition() == -1) {
if (seg_partition[segi] == -1) {
cout << endl << "segi: " << segi << endl;
cout << "points: " << seg[0] << " " << seg[1] << endl;
cout << "surfels: " << endl << sels << endl;
throw NgException("no surface element found");
}
}
}
}
#endif
//========================== weights =================================================================
// distribute the mesh to the worker processors
// call it only for the master !
void Mesh :: Distribute (NgArray<int> & volume_weights , NgArray<int> & surface_weights, NgArray<int> & segment_weights)
{
NgMPI_Comm comm = GetCommunicator();
int id = comm.Rank();
int ntasks = comm.Size();
if (id != 0 || ntasks == 1 ) return;
#ifdef METIS
ParallelMetis (volume_weights, surface_weights, segment_weights);
#else
for (ElementIndex ei = 0; ei < GetNE(); ei++)
(*this)[ei].SetPartition(ntasks * ei/GetNE() + 1);
#endif
/*
for (ElementIndex ei = 0; ei < GetNE(); ei++)
*testout << "el(" << ei << ") is in part " << (*this)[ei].GetPartition() << endl;
for (SurfaceElementIndex ei = 0; ei < GetNSE(); ei++)
*testout << "sel(" << int(ei) << ") is in part " << (*this)[ei].GetPartition() << endl;
*/
// MyMPI_SendCmd ("mesh");
SendRecvMesh ();
}
#ifdef METIS5
void Mesh :: ParallelMetis (NgArray<int> & volume_weights , NgArray<int> & surface_weights, NgArray<int> & segment_weights)
{
PrintMessage (3, "call metis 5 with weights ...");
// cout << "segment_weights " << segment_weights << endl;
// cout << "surface_weights " << surface_weights << endl;
// cout << "volume_weights " << volume_weights << endl;
int timer = NgProfiler::CreateTimer ("Mesh::Partition");
NgProfiler::RegionTimer reg(timer);
idx_t ne = GetNE() + GetNSE() + GetNSeg();
idx_t nn = GetNP();
NgArray<idx_t> eptr, eind , nwgt;
for (int i = 0; i < GetNE(); i++)
{
eptr.Append (eind.Size());
const Element & el = VolumeElement(i+1);
int ind = el.GetIndex();
if (volume_weights.Size()<ind)
nwgt.Append(0);
else
nwgt.Append (volume_weights[ind -1]);
for (int j = 0; j < el.GetNP(); j++)
eind.Append (el[j]-1);
}
for (int i = 0; i < GetNSE(); i++)
{
eptr.Append (eind.Size());
const Element2d & el = SurfaceElement(i+1);
int ind = el.GetIndex();
ind = GetFaceDescriptor(ind).BCProperty();
if (surface_weights.Size()<ind)
nwgt.Append(0);
else
nwgt.Append (surface_weights[ind -1]);
for (int j = 0; j < el.GetNP(); j++)
eind.Append (el[j]-1);
}
for (int i = 0; i < GetNSeg(); i++)
{
eptr.Append (eind.Size());
const Segment & el = LineSegment(i+1);
int ind = el.si;
if (segment_weights.Size()<ind)
nwgt.Append(0);
else
nwgt.Append (segment_weights[ind -1]);
eind.Append (el[0]);
eind.Append (el[1]);
}
eptr.Append (eind.Size());
NgArray<idx_t> epart(ne), npart(nn);
idxtype nparts = GetCommunicator().Size()-1;
vol_partition.SetSize(GetNE());
surf_partition.SetSize(GetNSE());
seg_partition.SetSize(GetNSeg());
if (nparts == 1)
{
for (int i = 0; i < GetNE(); i++)
// VolumeElement(i+1).SetPartition(1);
vol_partition[i] = 1;
for (int i = 0; i < GetNSE(); i++)
// SurfaceElement(i+1).SetPartition(1);
surf_partition[i] = 1;
for (int i = 0; i < GetNSeg(); i++)
// LineSegment(i+1).SetPartition(1);
seg_partition[i] = 1;
return;
}
idxtype edgecut;
idxtype ncommon = 3;
METIS_PartMeshDual (&ne, &nn, &eptr[0], &eind[0], &nwgt[0], NULL, &ncommon, &nparts,
NULL, NULL,
&edgecut, &epart[0], &npart[0]);
/*
METIS_PartMeshNodal (&ne, &nn, &eptr[0], &eind[0], NULL, NULL, &nparts,
NULL, NULL,
&edgecut, &epart[0], &npart[0]);
*/
PrintMessage (3, "metis complete");
// cout << "done" << endl;
for (int i = 0; i < GetNE(); i++)
// VolumeElement(i+1).SetPartition(epart[i] + 1);
vol_partition[i] = epart[i] + 1;
for (int i = 0; i < GetNSE(); i++)
// SurfaceElement(i+1).SetPartition(epart[i+GetNE()] + 1);
surf_partition[i] = epart[i+GetNE()] + 1;
for (int i = 0; i < GetNSeg(); i++)
// LineSegment(i+1).SetPartition(epart[i+GetNE()+GetNSE()] + 1);
seg_partition[i] = epart[i+GetNE()+GetNSE()] + 1;
}
#endif
//===========================================================================================
#ifdef METIS4
void Mesh :: ParallelMetis ( )
{
int timer = NgProfiler::CreateTimer ("Mesh::Partition");
NgProfiler::RegionTimer reg(timer);
PrintMessage (3, "Metis called");
if (GetDimension() == 2)
{
PartDualHybridMesh2D ( ); // neloc );
return;
}
idx_t ne = GetNE();
idx_t nn = GetNP();
if (ntasks <= 2 || ne <= 1)
{
if (ntasks == 1) return;
for (int i=1; i<=ne; i++)
VolumeElement(i).SetPartition(1);
for (int i=1; i<=GetNSE(); i++)
SurfaceElement(i).SetPartition(1);
return;
}
bool uniform_els = true;
ELEMENT_TYPE elementtype = TET;
for (int el = 1; el <= GetNE(); el++)
if (VolumeElement(el).GetType() != elementtype)
{
uniform_els = false;
break;
}
if (!uniform_els)
{
PartHybridMesh ();
}
else
{
// uniform (TET) mesh, JS
int npe = VolumeElement(1).GetNP();
NgArray<idxtype> elmnts(ne*npe);
int etype;
if (elementtype == TET)
etype = 2;
else if (elementtype == HEX)
etype = 3;
for (int i=1; i<=ne; i++)
for (int j=1; j<=npe; j++)
elmnts[(i-1)*npe+(j-1)] = VolumeElement(i).PNum(j)-1;
int numflag = 0;
int nparts = ntasks-1;
int ncommon = 3;
int edgecut;
NgArray<idxtype> epart(ne), npart(nn);
// if ( ntasks == 1 )
// {
// (*this) = *mastermesh;
// nparts = 4;
// metis :: METIS_PartMeshDual (&ne, &nn, elmnts, &etype, &numflag, &nparts,
// &edgecut, epart, npart);
// cout << "done" << endl;
// cout << "edge-cut: " << edgecut << ", balance: " << metis :: ComputeElementBalance(ne, nparts, epart) << endl;
// for (int i=1; i<=ne; i++)
// {
// mastermesh->VolumeElement(i).SetPartition(epart[i-1]);
// }
// return;
// }
int timermetis = NgProfiler::CreateTimer ("Metis itself");
NgProfiler::StartTimer (timermetis);
#ifdef METIS4
cout << "call metis(4)_PartMeshDual ... " << flush;
METIS_PartMeshDual (&ne, &nn, &elmnts[0], &etype, &numflag, &nparts,
&edgecut, &epart[0], &npart[0]);
#else
cout << "call metis(5)_PartMeshDual ... " << endl;
// idx_t options[METIS_NOPTIONS];
NgArray<idx_t> eptr(ne+1);
for (int j = 0; j < ne+1; j++)
eptr[j] = 4*j;
METIS_PartMeshDual (&ne, &nn, &eptr[0], &elmnts[0], NULL, NULL, &ncommon, &nparts,
NULL, NULL,
&edgecut, &epart[0], &npart[0]);
#endif
NgProfiler::StopTimer (timermetis);
cout << "complete" << endl;
#ifdef METIS4
cout << "edge-cut: " << edgecut << ", balance: "
<< ComputeElementBalance(ne, nparts, &epart[0]) << endl;
#endif
// partition numbering by metis : 0 ... ntasks - 1
// we want: 1 ... ntasks
for (int i=1; i<=ne; i++)
VolumeElement(i).SetPartition(epart[i-1] + 1);
}
for (int sei = 1; sei <= GetNSE(); sei++ )
{
int ei1, ei2;
GetTopology().GetSurface2VolumeElement (sei, ei1, ei2);
Element2d & sel = SurfaceElement (sei);
for (int j = 0; j < 2; j++)
{
int ei = (j == 0) ? ei1 : ei2;
if ( ei > 0 && ei <= GetNE() )
{
sel.SetPartition (VolumeElement(ei).GetPartition());
break;
}
}
}
}
#endif
void Mesh :: PartHybridMesh ()
{
throw Exception("PartHybridMesh not supported");
#ifdef METISxxx
int ne = GetNE();
int nn = GetNP();
int nedges = topology.GetNEdges();
idxtype *xadj, * adjacency;
// idxtype *v_weights = NULL, *e_weights = NULL;
int weightflag = 0;
int numflag = 0;
int nparts = ntasks - 1;
int options[5];
options[0] = 0;
int edgecut;
idxtype * part;
xadj = new idxtype[nn+1];
part = new idxtype[nn];
NgArray<int> cnt(nn+1);
cnt = 0;
for ( int edge = 0; edge < nedges; edge++ )
{
// int v1, v2;
// topology.GetEdgeVertices ( edge, v1, v2);
auto [v1,v2] = topology.GetEdgeVertices(edge);
cnt[v1-1] ++;
cnt[v2-1] ++;
}
xadj[0] = 0;
for ( int n = 1; n <= nn; n++ )
{
xadj[n] = idxtype(xadj[n-1] + cnt[n-1]);
}
adjacency = new idxtype[xadj[nn]];
cnt = 0;
for ( int edge = 0; edge < nedges; edge++ )
{
// int v1, v2;
// topology.GetEdgeVertices ( edge, v1, v2);
auto [v1,v2] = topology.GetEdgeVertices(edge);
adjacency[ xadj[v1-1] + cnt[v1-1] ] = v2-1;
adjacency[ xadj[v2-1] + cnt[v2-1] ] = v1-1;
cnt[v1-1]++;
cnt[v2-1]++;
}
for ( int vert = 0; vert < nn; vert++ )
{
NgFlatArray<idxtype> array ( cnt[vert], &adjacency[ xadj[vert] ] );
BubbleSort(array);
}
#ifdef METIS4
METIS_PartGraphKway ( &nn, xadj, adjacency, v_weights, e_weights, &weightflag,
&numflag, &nparts, options, &edgecut, part );
#else
cout << "currently not supported (metis5), A" << endl;
#endif
NgArray<int> nodesinpart(ntasks);
vol_partition.SetSize(ne);
for ( int el = 1; el <= ne; el++ )
{
Element & volel = VolumeElement(el);
nodesinpart = 0;
int el_np = volel.GetNP();
int partition = 0;
for ( int i = 0; i < el_np; i++ )
nodesinpart[ part[volel[i]-1]+1 ] ++;
for ( int i = 1; i < ntasks; i++ )
if ( nodesinpart[i] > nodesinpart[partition] )
partition = i;
// volel.SetPartition(partition);
vol_partition[el-1] = partition;
}
delete [] xadj;
delete [] part;
delete [] adjacency;
#else
cout << "parthybridmesh not available" << endl;
#endif
}
void Mesh :: PartDualHybridMesh ( ) // NgArray<int> & neloc )
{
throw Exception("PartDualHybridMesh not supported");
#ifdef OLD
#ifdef METIS
int ne = GetNE();
// int nn = GetNP();
// int nedges = topology->GetNEdges();
int nfaces = topology.GetNFaces();
idxtype *xadj, * adjacency, *v_weights = NULL, *e_weights = NULL;
int weightflag = 0;
// int numflag = 0;
int nparts = ntasks - 1;
int options[5];
options[0] = 0;
int edgecut;
idxtype * part;
NgArray<int, 0> facevolels1(nfaces), facevolels2(nfaces);
facevolels1 = -1;
facevolels2 = -1;
// NgArray<int, 0> elfaces;
xadj = new idxtype[ne+1];
part = new idxtype[ne];
NgArray<int, 0> cnt(ne+1);
cnt = 0;
for ( int el=1; el <= ne; el++ )
{
Element volel = VolumeElement(el);
// topology.GetElementFaces(el, elfaces);
auto elfaces = topology.GetFaces (ElementIndex(el-1));
for ( int i = 0; i < elfaces.Size(); i++ )
{
if ( facevolels1[elfaces[i]] == -1 )
facevolels1[elfaces[i]] = el;
else
{
facevolels2[elfaces[i]] = el;
cnt[facevolels1[elfaces[i]]-1]++;
cnt[facevolels2[elfaces[i]]-1]++;
}
}
}
xadj[0] = 0;
for ( int n = 1; n <= ne; n++ )
{
xadj[n] = idxtype(xadj[n-1] + cnt[n-1]);
}
adjacency = new idxtype[xadj[ne]];
cnt = 0;
for ( int face = 1; face <= nfaces; face++ )
{
int e1, e2;
e1 = facevolels1[face-1];
e2 = facevolels2[face-1];
if ( e2 == -1 ) continue;
adjacency[ xadj[e1-1] + cnt[e1-1] ] = e2-1;
adjacency[ xadj[e2-1] + cnt[e2-1] ] = e1-1;
cnt[e1-1]++;
cnt[e2-1]++;
}
for ( int el = 0; el < ne; el++ )
{
NgFlatArray<idxtype> array ( cnt[el], &adjacency[ xadj[el] ] );
BubbleSort(array);
}
int timermetis = NgProfiler::CreateTimer ("Metis itself");
NgProfiler::StartTimer (timermetis);
#ifdef METIS4
METIS_PartGraphKway ( &ne, xadj, adjacency, v_weights, e_weights, &weightflag,
&numflag, &nparts, options, &edgecut, part );
#else
cout << "currently not supported (metis5), B" << endl;
#endif
NgProfiler::StopTimer (timermetis);
NgArray<int> nodesinpart(ntasks);
vol_partition.SetSize(ne);
for ( int el = 1; el <= ne; el++ )
{
// Element & volel = VolumeElement(el);
nodesinpart = 0;
// VolumeElement(el).SetPartition(part[el-1 ] + 1);
vol_partition[el-1] = part[el-1 ] + 1;
}
/*
for ( int i=1; i<=ne; i++)
{
neloc[ VolumeElement(i).GetPartition() ] ++;
}
*/
delete [] xadj;
delete [] part;
delete [] adjacency;
#else
cout << "partdualmesh not available" << endl;
#endif
#endif
}
void Mesh :: PartDualHybridMesh2D ( )
{
#ifdef METIS
idxtype ne = GetNSE();
int nv = GetNV();
NgArray<idxtype> xadj(ne+1);
NgArray<idxtype> adjacency(ne*4);
// first, build the vertex 2 element table:
NgArray<int, PointIndex::BASE> cnt(nv);
cnt = 0;
for (SurfaceElementIndex sei = 0; sei < GetNSE(); sei++)
for (int j = 0; j < (*this)[sei].GetNP(); j++)
cnt[ (*this)[sei][j] ] ++;
TABLE<SurfaceElementIndex, PointIndex::BASE> vert2els(cnt);
for (SurfaceElementIndex sei = 0; sei < GetNSE(); sei++)
for (int j = 0; j < (*this)[sei].GetNP(); j++)
vert2els.Add ((*this)[sei][j], sei);
// find all neighbour elements
int cntnb = 0;
NgArray<int> marks(ne); // to visit each neighbour just once
marks = -1;
for (SurfaceElementIndex sei = 0; sei < ne; sei++)
{
xadj[sei] = cntnb;
for (int j = 0; j < (*this)[sei].GetNP(); j++)
{
PointIndex vnr = (*this)[sei][j];
// all elements with at least one common vertex
for (int k = 0; k < vert2els[vnr].Size(); k++)
{
SurfaceElementIndex sei2 = vert2els[vnr][k];
if (sei == sei2) continue;
if (marks[sei2] == sei) continue;
// neighbour, if two common vertices
int common = 0;
for (int m1 = 0; m1 < (*this)[sei].GetNP(); m1++)
for (int m2 = 0; m2 < (*this)[sei2].GetNP(); m2++)
if ( (*this)[sei][m1] == (*this)[sei2][m2])
common++;
if (common >= 2)
{
marks[sei2] = sei; // mark as visited
adjacency[cntnb++] = sei2;
}
}
}
}
xadj[ne] = cntnb;
idxtype *v_weights = NULL, *e_weights = NULL;
// int numflag = 0;
idxtype nparts = ntasks - 1;
idxtype edgecut;
NgArray<idxtype> part(ne);
for ( int el = 0; el < ne; el++ )
BubbleSort (adjacency.Range (xadj[el], xadj[el+1]));
#ifdef METIS4
idxtype weightflag = 0;
int options[5];
options[0] = 0;
METIS_PartGraphKway ( &ne, &xadj[0], &adjacency[0], v_weights, e_weights, &weightflag,
&numflag, &nparts, options, &edgecut, &part[0] );
#else
idx_t ncon = 1;
METIS_PartGraphKway ( &ne, &ncon, &xadj[0], &adjacency[0],
v_weights, NULL, e_weights,
&nparts,
NULL, NULL, NULL,
&edgecut, &part[0] );
#endif
surf_partition.SetSize(ne);
for (SurfaceElementIndex sei = 0; sei < ne; sei++)
// (*this) [sei].SetPartition (part[sei]+1);
surf_partition[sei] = part[sei]+1;
#else
cout << "partdualmesh not available" << endl;
#endif
}
}
#endif
|