/usr/lib/petscdir/3.4.2/include/sieve/Partitioner.hh is in libpetsc3.4.2-dev 3.4.2.dfsg1-8.1+b1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | #ifndef included_ALE_Partitioner_hh
#define included_ALE_Partitioner_hh
#ifndef included_ALE_Completion_hh
#include <sieve/Completion.hh>
#endif
#ifdef PETSC_HAVE_ZOLTAN
#include <zoltan.h>
extern "C" {
// Inputs
extern int nvtxs_Zoltan; // The number of vertices
extern int nhedges_Zoltan; // The number of hyperedges
extern int *eptr_Zoltan; // The offsets of each hyperedge
extern int *eind_Zoltan; // The vertices in each hyperedge, indexed by eptr
int getNumVertices_Zoltan(void *, int *);
void getLocalElements_Zoltan(void *, int, int, ZOLTAN_ID_PTR, ZOLTAN_ID_PTR, int, float *, int *);
void getHgSizes_Zoltan(void *, int *, int *, int *, int *);
void getHg_Zoltan(void *, int, int, int, int, ZOLTAN_ID_PTR, int *, ZOLTAN_ID_PTR, int *);
}
#endif
#ifdef PETSC_HAVE_CHACO
#ifdef PETSC_HAVE_UNISTD_H
#include <unistd.h>
#endif
/* Chaco does not have an include file */
extern "C" {
extern int interface(int nvtxs, int *start, int *adjacency, int *vwgts,
float *ewgts, float *x, float *y, float *z, char *outassignname,
char *outfilename, short *assignment, int architecture, int ndims_tot,
int mesh_dims[3], double *goal, int global_method, int local_method,
int rqi_flag, int vmax, int ndims, double eigtol, long seed);
extern int FREE_GRAPH;
}
#endif
#ifdef PETSC_HAVE_PARMETIS
#include <parmetis.h>
#endif
#ifdef PETSC_HAVE_HMETIS
extern "C" {
extern void HMETIS_PartKway(int nvtxs, int nhedges, int *vwgts, int *eptr, int *eind, int *hewgts, int nparts, int ubfactor, int *options, int *part, int *edgeCut);
}
#endif
namespace ALE {
#if 1
#ifdef PETSC_HAVE_CHACO
namespace Chaco {
template<typename Alloc_ = malloc_allocator<short int> >
class Partitioner {
public:
typedef short int part_type;
typedef Alloc_ alloc_type;
enum {DEFAULT_METHOD = 1, INERTIAL_METHOD = 3};
protected:
const int logSize;
char *msgLog;
int fd_stdout, fd_pipe[2];
alloc_type _allocator;
public:
Partitioner(): logSize(10000) {};
~Partitioner() {};
protected:
// Chaco outputs to stdout. We redirect this to a buffer.
// TODO: check error codes for UNIX calls
void startStdoutRedirect() {
#ifdef PETSC_HAVE_UNISTD_H
this->fd_stdout = dup(1);
pipe(this->fd_pipe);
close(1);
dup2(this->fd_pipe[1], 1);
#endif
};
void stopStdoutRedirect() {
#ifdef PETSC_HAVE_UNISTD_H
int count;
fflush(stdout);
this->msgLog = new char[this->logSize];
count = read(this->fd_pipe[0], this->msgLog, (this->logSize-1)*sizeof(char));
if (count < 0) count = 0;
this->msgLog[count] = 0;
close(1);
dup2(this->fd_stdout, 1);
close(this->fd_stdout);
close(this->fd_pipe[0]);
close(this->fd_pipe[1]);
//std::cout << this->msgLog << std::endl;
delete [] this->msgLog;
#endif
};
public:
static bool zeroBase() {return false;}
// This method returns the partition section mapping sieve points (here cells) to partitions
// start: start of edge list for each vertex
// adjacency: adj[start[v]] is edge list data for vertex v
// partition: this section is over the partitions and takes points as values
// TODO: Read global and local methods from options
template<typename Section, typename MeshManager>
void partition(const int numVertices, const int start[], const int adjacency[], const Obj<Section>& partition, const MeshManager& manager) {
FREE_GRAPH = 0; /* Do not let Chaco free my memory */
int nvtxs = numVertices; /* number of vertices in full graph */
int *vwgts = NULL; /* weights for all vertices */
float *ewgts = NULL; /* weights for all edges */
float *x = NULL, *y = NULL, *z = NULL; /* coordinates for inertial method */
char *outassignname = NULL; /* name of assignment output file */
char *outfilename = NULL; /* output file name */
int architecture = 1; /* 0 => hypercube, d => d-dimensional mesh */
int ndims_tot = 0; /* total number of cube dimensions to divide */
int mesh_dims[3]; /* dimensions of mesh of processors */
double *goal = NULL; /* desired set sizes for each set */
int global_method = 1; /* global partitioning algorithm */
int local_method = 1; /* local partitioning algorithm */
int rqi_flag = 0; /* should I use RQI/Symmlq eigensolver? */
int vmax = 200; /* how many vertices to coarsen down to? */
int ndims = 1; /* number of eigenvectors (2^d sets) */
double eigtol = 0.001; /* tolerance on eigenvectors */
long seed = 123636512; /* for random graph mutations */
int maxSize = 0;
if (global_method == INERTIAL_METHOD) {manager.createCellCoordinates(nvtxs, &x, &y, &z);}
mesh_dims[0] = partition->commSize(); mesh_dims[1] = 1; mesh_dims[2] = 1;
part_type *assignment = this->_allocator.allocate(nvtxs);
for(int i = 0; i < nvtxs; ++i) {this->_allocator.construct(assignment+i, 0);}
this->startStdoutRedirect();
interface(nvtxs, (int *) start, (int *) adjacency, vwgts, ewgts, x, y, z, outassignname, outfilename,
assignment, architecture, ndims_tot, mesh_dims, goal, global_method, local_method, rqi_flag,
vmax, ndims, eigtol, seed);
this->stopStdoutRedirect();
for(int v = 0; v < nvtxs; ++v) {partition->addFiberDimension(assignment[v], 1);}
partition->allocatePoint();
for(int p = 0; p < partition->commSize(); ++p) {
maxSize = std::max(maxSize, partition->getFiberDimension(p));
}
typename Section::value_type *values = new typename Section::value_type[maxSize];
for(int p = 0; p < partition->commSize(); ++p) {
int k = 0;
for(int v = 0; v < nvtxs; ++v) {
if (assignment[v] == p) values[k++] = manager.getCell(v);
}
if (k != partition->getFiberDimension(p)) throw ALE::Exception("Invalid partition");
partition->updatePoint(p, values);
}
delete [] values;
if (global_method == INERTIAL_METHOD) {manager.destroyCellCoordinates(nvtxs, &x, &y, &z);}
for(int i = 0; i < nvtxs; ++i) {this->_allocator.destroy(assignment+i);}
this->_allocator.deallocate(assignment, nvtxs);
};
};
}
#endif
#ifdef PETSC_HAVE_PARMETIS
namespace ParMetis {
template<typename Alloc_ = malloc_allocator<int> >
class Partitioner {
public:
typedef int part_type;
typedef Alloc_ alloc_type;
protected:
alloc_type _allocator;
public:
static bool zeroBase() {return true;}
// This method returns the partition section mapping sieve points (here cells) to partitions
// start: start of edge list for each vertex
// adjacency: adj[start[v]] is edge list data for vertex v
// partition: this section is over the partitions and takes points as values
// TODO: Read parameters from options
template<typename Section, typename MeshManager>
void partition(const PetscInt numVertices, const PetscInt start[], const PetscInt adjacency[], const Obj<Section>& partition, const MeshManager& manager) {
//static part_type *partitionSieve(const Obj<bundle_type>& bundle, const int dim) {
PetscInt nvtxs = numVertices; // The number of vertices in full graph
PetscInt *vtxdist; // Distribution of vertices across processes
PetscInt *xadj = const_cast<PetscInt*>(start); // Start of edge list for each vertex
PetscInt *adjncy = const_cast<PetscInt*>(adjacency); // Edge lists for all vertices
PetscInt *vwgt = NULL; // Vertex weights
PetscInt *adjwgt = NULL; // Edge weights
PetscInt wgtflag = 0; // Indicates which weights are present
PetscInt numflag = 0; // Indicates initial offset (0 or 1)
PetscInt ncon = 1; // The number of weights per vertex
PetscInt nparts = partition->commSize(); // The number of partitions
PetscReal *tpwgts; // The fraction of vertex weights assigned to each partition
PetscReal *ubvec; // The balance intolerance for vertex weights
PetscInt options[5]; // Options
PetscInt maxSize = 0;
// Outputs
PetscInt edgeCut; // The number of edges cut by the partition
part_type *assignment;
options[0] = 0; // Use all defaults
// Calculate vertex distribution
// Not sure this still works in parallel
vtxdist = new PetscInt[nparts+1];
vtxdist[0] = 0;
MPI_Allgather(&nvtxs, 1, MPIU_INT, &vtxdist[1], 1, MPIU_INT, partition->comm());
for(PetscInt p = 2; p <= nparts; ++p) {
vtxdist[p] += vtxdist[p-1];
}
// Calculate weights
tpwgts = new PetscReal[ncon*nparts];
for(int p = 0; p < nparts; ++p) {
tpwgts[p] = 1.0/nparts;
}
ubvec = new PetscReal[ncon];
ubvec[0] = 1.05;
assignment = this->_allocator.allocate(nvtxs);
for(PetscInt i = 0; i < nvtxs; ++i) {this->_allocator.construct(assignment+i, 0);}
if (partition->commSize() == 1) {
PetscMemzero(assignment, nvtxs * sizeof(part_type));
} else {
if (partition->debug() && nvtxs) {
for(int p = 0; p <= nvtxs; ++p) {
std::cout << "["<<partition->commRank()<<"]xadj["<<p<<"] = " << xadj[p] << std::endl;
}
for(int i = 0; i < xadj[nvtxs]; ++i) {
std::cout << "["<<partition->commRank()<<"]adjncy["<<i<<"] = " << adjncy[i] << std::endl;
}
}
if (vtxdist[1] == vtxdist[nparts]) {
if (partition->commRank() == 0) {
/* Parameters changes (Matt, check to make sure works):
* (removed) numflags
* (changed) options -> NULL implies all defaults (only for METIS, not ParMETIS!)
*/
METIS_PartGraphKway(&nvtxs, &ncon, xadj, adjncy, vwgt, NULL, adjwgt, &nparts, tpwgts, ubvec, NULL, &edgeCut, assignment);
if (partition->debug()) {std::cout << "Metis: edgecut is " << edgeCut << std::endl;}
}
} else {
MPI_Comm comm = partition->comm();
ParMETIS_V3_PartKway(vtxdist, xadj, adjncy, vwgt, adjwgt, &wgtflag, &numflag, &ncon, &nparts, tpwgts, ubvec, options, &edgeCut, assignment, &comm);
if (partition->debug()) {std::cout << "ParMetis: edgecut is " << edgeCut << std::endl;}
}
}
delete [] vtxdist;
delete [] tpwgts;
delete [] ubvec;
for(PetscInt v = 0; v < nvtxs; ++v) {partition->addFiberDimension(assignment[v], 1);}
partition->allocatePoint();
for(PetscInt p = 0; p < partition->commSize(); ++p) {
maxSize = std::max(maxSize, partition->getFiberDimension(p));
}
typename Section::value_type *values = new typename Section::value_type[maxSize];
for(PetscInt p = 0; p < partition->commSize(); ++p) {
int k = 0;
for(PetscInt v = 0; v < nvtxs; ++v) {
if (assignment[v] == p) values[k++] = manager.getCell(v);
}
if (k != partition->getFiberDimension(p)) throw ALE::Exception("Invalid partition");
partition->updatePoint(p, values);
}
delete [] values;
for(PetscInt i = 0; i < nvtxs; ++i) {this->_allocator.destroy(assignment+i);}
this->_allocator.deallocate(assignment, nvtxs);
};
};
};
#endif
namespace Simple {
template<typename Alloc_ = malloc_allocator<short int> >
class Partitioner {
public:
typedef int part_type;
typedef Alloc_ alloc_type;
protected:
alloc_type _allocator;
public:
Partitioner() {};
~Partitioner() {};
public:
static bool zeroBase() {return true;}
template<typename Section, typename MeshManager>
void partition(const int numVertices, const int start[], const int adjacency[], const Obj<Section>& partition, const MeshManager& manager) {
const int numProcs = partition->commSize();
const int rank = partition->commRank();
int maxSize = 0;
for(int p = 0; p < numProcs; ++p) {
partition->setFiberDimension(p, numVertices/numProcs + ((numVertices % numProcs) > rank));
maxSize = std::max(maxSize, partition->getFiberDimension(p));
}
partition->allocatePoint();
typename Section::value_type *values = new typename Section::value_type[maxSize];
for(int p = 0; p < partition->commSize(); ++p) {
const int start = p*(numVertices/numProcs) + p*((numVertices % numProcs) > p+1);
const int end = (p+1)*(numVertices/numProcs) + (p+1)*((numVertices % numProcs) > p+2);
int k = 0;
for(int v = start; v < end; ++v, ++k) {
values[k] = manager.getCell(v);
}
if (k != partition->getFiberDimension(p)) throw ALE::Exception("Invalid partition");
partition->updatePoint(p, values);
}
delete [] values;
}
};
}
#ifdef PETSC_HAVE_CHACO
template<typename GraphPartitioner = ALE::Chaco::Partitioner<>, typename Alloc_ = malloc_allocator<int> >
#elif defined(PETSC_HAVE_PARMETIS)
template<typename GraphPartitioner = ALE::ParMetis::Partitioner<>, typename Alloc_ = malloc_allocator<int> >
#else
template<typename GraphPartitioner = ALE::Simple::Partitioner<>, typename Alloc_ = malloc_allocator<int> >
#endif
class Partitioner {
public:
typedef Alloc_ alloc_type;
typedef GraphPartitioner graph_partitioner_type;
typedef typename GraphPartitioner::part_type part_type;
template<typename Mesh>
class MeshManager {
public:
typedef typename Mesh::point_type point_type;
protected:
const Obj<Mesh>& mesh;
bool simpleCellNumbering;
point_type *cells;
std::map<point_type, point_type> numbers;
protected:
void createCells(const int height) {
const Obj<typename Mesh::label_sequence>& mcells = mesh->heightStratum(height);
const typename Mesh::label_sequence::iterator cEnd = mcells->end();
const int numCells = mcells->size();
int c = 0;
this->cells = NULL;
this->simpleCellNumbering = true;
for(typename Mesh::label_sequence::iterator c_iter = mcells->begin(); c_iter != cEnd; ++c_iter, ++c) {
if (*c_iter != c) {
this->simpleCellNumbering = false;
break;
}
}
if (!this->simpleCellNumbering) {
this->cells = new point_type[numCells];
c = 0;
for(typename Mesh::label_sequence::iterator c_iter = mcells->begin(); c_iter != cEnd; ++c_iter, ++c) {
// OPT: Could use map only for exceptional points
this->cells[c] = *c_iter;
this->numbers[*c_iter] = c;
}
}
};
public:
MeshManager(const Obj<Mesh>& mesh, const int height = 0): mesh(mesh) {
this->createCells(height);
};
~MeshManager() {
if (this->cells) {delete [] this->cells;}
};
public:
template<typename Float>
void createCellCoordinates(const int numVertices, Float *X[], Float *Y[], Float *Z[]) const {
const Obj<typename Mesh::real_section_type>& coordinates = mesh->getRealSection("coordinates");
const int dim = mesh->getDimension();
typedef typename alloc_type::template rebind<Float>::other float_alloc_type;
Float *x = float_alloc_type().allocate(numVertices*3);
for(int i = 0; i < numVertices*3; ++i) {float_alloc_type().construct(x+i, 0.0);}
Float *y = x+numVertices;
Float *z = y+numVertices;
Float *vCoords[3];
vCoords[0] = x; vCoords[1] = y; vCoords[2] = z;
const Obj<typename Mesh::label_sequence>& cells = mesh->heightStratum(0);
const int corners = mesh->size(coordinates, *(cells->begin()))/dim;
int c = 0;
for(typename Mesh::label_sequence::iterator c_iter = cells->begin(); c_iter !=cells->end(); ++c_iter, ++c) {
const typename Mesh::real_section_type::value_type *coords = mesh->restrictClosure(coordinates, *c_iter);
for(int d = 0; d < dim; ++d) {
vCoords[d][c] = 0.0;
}
for(int v = 0; v < corners; ++v) {
for(int d = 0; d < dim; ++d) {
vCoords[d][c] += coords[v*dim+d];
}
}
for(int d = 0; d < dim; ++d) {
vCoords[d][c] /= corners;
}
}
*X = x;
*Y = y;
*Z = z;
}
template<typename Float>
void destroyCellCoordinates(const int numVertices, Float *X[], Float *Y[], Float *Z[]) const {
typedef typename alloc_type::template rebind<Float>::other float_alloc_type;
Float *x = *X;
for(int i = 0; i < numVertices*3; ++i) {float_alloc_type().destroy(x+i);}
float_alloc_type().deallocate(x, numVertices*3);
}
point_type getCell(const point_type cellNumber) const {
if (this->simpleCellNumbering) {
return cellNumber;
}
return this->cells[cellNumber];
}
point_type getNumber(const point_type cell) {
if (this->simpleCellNumbering) {
return cell;
}
return this->numbers[cell];
}
};
template<typename Sieve>
class OffsetVisitor {
const Sieve& sieve;
const Sieve& overlapSieve;
int *offsets;
public:
OffsetVisitor(const Sieve& s, const Sieve& ovS, int off[]) : sieve(s), overlapSieve(ovS), offsets(off) {};
void visitPoint(const typename Sieve::point_type& point) {};
void visitArrow(const typename Sieve::arrow_type& arrow) {
const typename Sieve::point_type cell = arrow.target;
const typename Sieve::point_type face = arrow.source;
const int size = this->sieve.getSupportSize(face);
const int ovSize = this->overlapSieve.getSupportSize(face);
if (size == 2) {
offsets[cell+1]++;
} else if ((size == 1) && (ovSize == 1)) {
offsets[cell+1]++;
}
};
};
template<typename Sieve>
class AdjVisitor {
protected:
typename Sieve::point_type cell;
int *adjacency;
const int cellOffset;
int offset;
public:
AdjVisitor(int adj[], const bool zeroBase) : adjacency(adj), cellOffset(zeroBase ? 0 : 1), offset(0) {};
void visitPoint(const typename Sieve::point_type& point) {};
void visitArrow(const typename Sieve::arrow_type& arrow) {
const int neighbor = arrow.target;
if (neighbor != this->cell) {
//std::cout << "Adding dual edge from " << cell << " to " << neighbor << std::endl;
this->adjacency[this->offset++] = neighbor + this->cellOffset;
}
};
public:
void setCell(const typename Sieve::point_type cell) {this->cell = cell;};
int getOffset() {return this->offset;}
};
template<typename Mesh, int maxSize = 10>
class FaceRecognizer {
public:
typedef typename Mesh::point_type point_type;
protected:
int numCases;
int numFaceVertices[maxSize];
public:
FaceRecognizer(Mesh& mesh, const int debug = 0) : numCases(0) {
if (mesh.depth() != 1) {throw ALE::Exception("Only works for depth 1 meshes");}
const int dim = mesh.getDimension();
const Obj<typename Mesh::sieve_type>& sieve = mesh.getSieve();
const Obj<typename Mesh::label_sequence>& cells = mesh.heightStratum(0);
std::set<int> cornersSeen;
if (debug) {std::cout << "Building Recognizer" << std::endl;}
for(typename Mesh::label_sequence::iterator c_iter = cells->begin(); c_iter != cells->end(); ++c_iter) {
const int corners = sieve->getConeSize(*c_iter);
if (cornersSeen.find(corners) == cornersSeen.end()) {
if (numCases >= maxSize) {throw ALE::Exception("Exceeded maximum number of cases");}
cornersSeen.insert(corners);
if (corners == dim+1) {
numFaceVertices[numCases] = dim;
if (debug) {std::cout << " Recognizing simplices" << std::endl;}
} else if ((dim == 1) && (corners == 3)) {
numFaceVertices[numCases] = 3;
if (debug) {std::cout << " Recognizing quadratic edges" << std::endl;}
} else if ((dim == 2) && (corners == 4)) {
numFaceVertices[numCases] = 2;
if (debug) {std::cout << " Recognizing quads" << std::endl;}
} else if ((dim == 2) && (corners == 6)) {
numFaceVertices[numCases] = 3;
if (debug) {std::cout << " Recognizing tri and quad cohesive Lagrange cells" << std::endl;}
} else if ((dim == 2) && (corners == 9)) {
numFaceVertices[numCases] = 3;
if (debug) {std::cout << " Recognizing quadratic quads and quadratic quad cohesive Lagrange cells" << std::endl;}
} else if ((dim == 3) && (corners == 6)) {
numFaceVertices[numCases] = 4;
if (debug) {std::cout << " Recognizing tet cohesive cells" << std::endl;}
} else if ((dim == 3) && (corners == 8)) {
numFaceVertices[numCases] = 4;
if (debug) {std::cout << " Recognizing hexes" << std::endl;}
} else if ((dim == 3) && (corners == 9)) {
numFaceVertices[numCases] = 6;
if (debug) {std::cout << " Recognizing tet cohesive Lagrange cells" << std::endl;}
} else if ((dim == 3) && (corners == 10)) {
numFaceVertices[numCases] = 6;
if (debug) {std::cout << " Recognizing quadratic tets" << std::endl;}
} else if ((dim == 3) && (corners == 12)) {
numFaceVertices[numCases] = 6;
if (debug) {std::cout << " Recognizing hex cohesive Lagrange cells" << std::endl;}
} else if ((dim == 3) && (corners == 18)) {
numFaceVertices[numCases] = 6;
if (debug) {std::cout << " Recognizing quadratic tet cohesive Lagrange cells" << std::endl;}
} else if ((dim == 3) && (corners == 27)) {
numFaceVertices[numCases] = 9;
if (debug) {std::cout << " Recognizing quadratic hexes and quadratic hex cohesive Lagrange cells" << std::endl;}
} else {
throw ALE::Exception("Could not determine number of face vertices");
}
++numCases;
}
}
};
~FaceRecognizer() {};
public:
bool operator()(point_type cellA, point_type cellB, int meetSize) {
// Could concievably make this depend on the cells, but it seems slow
for(int i = 0; i < numCases; ++i) {
if (meetSize == numFaceVertices[i]) {
//std::cout << " Recognized case " << i <<"("<<numFaceVertices[i]<<") for <" << cellA <<","<< cellB << ">" << std::endl;
return true;
}
}
return false;
};
};
template<typename Mesh>
class SimpleFaceRecognizer {
public:
typedef typename Mesh::point_type point_type;
protected:
int dim;
public:
SimpleFaceRecognizer(Mesh& mesh, const int debug = 0) {
if (mesh.depth() != 1) {throw ALE::Exception("Only works for depth 1 meshes");}
this->dim = mesh.getDimension();
};
~SimpleFaceRecognizer() {};
public:
bool operator()(point_type cellA, point_type cellB, int meetSize) {
if (meetSize >= dim) {
return true;
}
return false;
};
};
template<typename Sieve, typename Manager, typename Recognizer>
class MeetVisitor {
public:
typedef std::set<typename Sieve::point_type> neighbors_type;
protected:
const Sieve& sieve;
Manager& manager;
Recognizer& faceRecognizer;
const int numCells;
neighbors_type *neighborCells;
typename ISieveVisitor::PointRetriever<Sieve> *pR;
typename Sieve::point_type cell;
public:
MeetVisitor(const Sieve& s, Manager& manager, Recognizer& faceRecognizer, const int n) : sieve(s), manager(manager), faceRecognizer(faceRecognizer), numCells(n) {
this->neighborCells = new std::set<typename Sieve::point_type>[numCells];
this->pR = new typename ISieveVisitor::PointRetriever<Sieve>(this->sieve.getMaxConeSize());
};
~MeetVisitor() {delete [] this->neighborCells; delete this->pR;};
void visitArrow(const typename Sieve::arrow_type& arrow) {};
void visitPoint(const typename Sieve::point_type& point) {
const typename Sieve::point_type& neighbor = point;
if (this->cell == neighbor) return;
this->pR->clear();
this->sieve.meet(this->cell, neighbor, *this->pR);
if (faceRecognizer(this->cell, neighbor, this->pR->getSize())) {
this->neighborCells[this->manager.getNumber(this->cell)].insert(this->manager.getNumber(neighbor));
}
};
public:
void setCell(const typename Sieve::point_type& c) {this->cell = c;};
const neighbors_type *getNeighbors() {return this->neighborCells;};
};
public: // Creating overlaps
// Create a partition point overlap for distribution
// This is the default overlap which comes from distributing a serial mesh on process 0
template<typename SendOverlap, typename RecvOverlap>
static void createDistributionPartOverlap(const Obj<SendOverlap>& sendOverlap, const Obj<RecvOverlap>& recvOverlap) {
const int rank = sendOverlap->commRank();
const int size = sendOverlap->commSize();
if (rank == 0) {
for(int p = 1; p < size; p++) {
// The arrow is from local partition point p (source) to remote partition point p (color) on rank p (target)
sendOverlap->addCone(p, p, p);
}
}
if (rank != 0) {
// The arrow is from remote partition point rank (color) on rank 0 (source) to local partition point rank (target)
recvOverlap->addCone(0, rank, rank);
}
}
// Create a mesh point overlap for distribution
// A local numbering is created for the remote points
// This is the default overlap which comes from distributing a serial mesh on process 0
template<typename Section, typename RecvPartOverlap, typename Renumbering, typename SendOverlap, typename RecvOverlap>
static void createDistributionMeshOverlap(const Obj<Section>& partition, const Obj<RecvPartOverlap>& recvPartOverlap, Renumbering& renumbering, const Obj<Section>& overlapPartition, const Obj<SendOverlap>& sendOverlap, const Obj<RecvOverlap>& recvOverlap) {
const typename Section::chart_type& chart = partition->getChart();
int numRanks = 0;
for(typename Section::chart_type::const_iterator p_iter = chart.begin(); p_iter != chart.end(); ++p_iter) {
if (*p_iter != sendOverlap->commRank() && partition->getFiberDimension(*p_iter)) numRanks++;
}
sendOverlap->setBaseSize(numRanks); // setNumRanks
for(typename Section::chart_type::const_iterator p_iter = chart.begin(); p_iter != chart.end(); ++p_iter) {
const int coneSize = partition->getFiberDimension(*p_iter);
if (*p_iter != sendOverlap->commRank() && coneSize) {
sendOverlap->setConeSize(*p_iter, coneSize); // setNumPoints
}
}
sendOverlap->assemble();
for(typename Section::chart_type::const_iterator p_iter = chart.begin(); p_iter != chart.end(); ++p_iter) {
if (*p_iter == sendOverlap->commRank()) continue;
const typename Section::value_type *points = partition->restrictPoint(*p_iter);
const int numPoints = partition->getFiberDimension(*p_iter);
for(int i = 0; i < numPoints; ++i) {
// Notice here that we do not know the local renumbering (but we do not use it)
sendOverlap->addArrow(points[i], *p_iter, points[i]);
}
}
sendOverlap->assemblePoints();
if (sendOverlap->debug()) {sendOverlap->view("Send mesh overlap");}
const typename RecvPartOverlap::capSequence::iterator rBegin = recvPartOverlap->capBegin();
const typename RecvPartOverlap::capSequence::iterator rEnd = recvPartOverlap->capEnd();
recvOverlap->setCapSize(recvPartOverlap->getCapSize()); // setNumRanks
for(typename RecvPartOverlap::capSequence::iterator r_iter = rBegin; r_iter != rEnd; ++r_iter) {
const int rank = *r_iter;
const typename RecvPartOverlap::supportSequence::iterator pBegin = recvPartOverlap->supportBegin(*r_iter);
const typename RecvPartOverlap::supportSequence::iterator pEnd = recvPartOverlap->supportEnd(*r_iter);
for(typename RecvPartOverlap::supportSequence::iterator p_iter = pBegin; p_iter != pEnd; ++p_iter) {
const typename Section::point_type& remotePartPoint = p_iter.color();
const int numPoints = overlapPartition->getFiberDimension(remotePartPoint);
recvOverlap->setSupportSize(rank, numPoints); // setNumPoints
}
}
recvOverlap->assemble();
for(typename RecvPartOverlap::capSequence::iterator r_iter = rBegin; r_iter != rEnd; ++r_iter) {
const int rank = *r_iter;
const typename RecvPartOverlap::supportSequence::iterator pBegin = recvPartOverlap->supportBegin(*r_iter);
const typename RecvPartOverlap::supportSequence::iterator pEnd = recvPartOverlap->supportEnd(*r_iter);
for(typename RecvPartOverlap::supportSequence::iterator p_iter = pBegin; p_iter != pEnd; ++p_iter) {
const typename Section::point_type& remotePartPoint = p_iter.color();
const typename Section::value_type *points = overlapPartition->restrictPoint(remotePartPoint);
const int numPoints = overlapPartition->getFiberDimension(remotePartPoint);
for(int i = 0; i < numPoints; ++i) {
recvOverlap->addArrow(rank, renumbering[points[i]], points[i]);
}
}
}
recvOverlap->assemblePoints();
if (recvOverlap->debug()) {recvOverlap->view("Receive mesh overlap");}
}
// Create a partition point overlap from a partition
// The intention is to create an overlap which enables exchange of redistribution information
template<typename Section, typename SendOverlap, typename RecvOverlap>
static void createPartitionPartOverlap(const Obj<Section>& partition, const Obj<SendOverlap>& sendOverlap, const Obj<RecvOverlap>& recvOverlap) {
const typename Section::chart_type& chart = partition->getChart();
const int rank = partition->commRank();
const int size = partition->commSize();
int *adj = new int[size];
int *recvCounts = new int[size];
int numNeighbors;
for(int p = 0; p < size; ++p) {
adj[p] = 0;
recvCounts[p] = 1;
}
for(typename Section::chart_type::const_iterator p_iter = chart.begin(); p_iter != chart->end(); ++p_iter) {
const typename Section::value_type& p = partition->restrictPoint(*p_iter)[0];
// The arrow is from local partition point p (source) to remote partition point p (color) on rank p (target)
sendOverlap->addCone(p, p, p);
adj[p] = 1;
}
MPI_Reduce_Scatter(adj, &numNeighbors, recvCounts, size, MPI_INT, MPI_SUM, partition->comm());
MPI_Request *recvRequests = new MPI_Request[numNeighbors];
int dummy = 0;
// TODO: Get a unique tag
for(int n = 0; n < numNeighbors; ++n) {
MPI_Irecv(&dummy, 1, MPI_INT, MPI_ANY_SOURCE, 1, partition->comm(), &recvRequests[n]);
}
const Obj<typename SendOverlap::traits::baseSequence> ranks = sendOverlap->base();
const typename SendOverlap::traits::baseSequence::iterator rEnd = ranks->end();
MPI_Request *sendRequests = new MPI_Request[ranks->size()];
int s = 0;
for(typename SendOverlap::traits::baseSequence::iterator r_iter = ranks->begin(); r_iter != rEnd; ++r_iter, ++s) {
MPI_Isend(&dummy, 1, MPI_INT, *r_iter, 1, partition->comm(), &sendRequests[s]);
}
for(int n = 0; n < numNeighbors; ++n) {
MPI_Status status;
int idx;
MPI_Waitany(numNeighbors, recvRequests, &idx, &status);
// The arrow is from remote partition point rank (color) on rank p (source) to local partition point rank (target)
recvOverlap->addCone(status.MPI_SOURCE, rank, rank);
}
MPI_Waitall(ranks->size(), sendRequests, MPI_STATUSES_IGNORE);
delete [] sendRequests;
delete [] recvRequests;
delete [] adj;
delete [] recvCounts;
}
public: // Building CSR meshes
// This produces the dual graph (each cell is a vertex and each face is an edge)
// numbering: A contiguous numbering of the cells (not yet included)
// numVertices: The number of vertices in the graph (cells in the mesh)
// adjacency: The vertices adjacent to each vertex (cells adjacent to each mesh cell)
// - We allow an exception to contiguous numbering.
// If the cell id > numElements, we assign a new number starting at
// the top and going downward. I know these might not match up with
// the iterator order, but we can fix it later.
template<typename Mesh>
static void buildDualCSR(const Obj<Mesh>& mesh, int *numVertices, int **offsets, int **adjacency, const bool zeroBase = true) {
const Obj<typename Mesh::sieve_type>& sieve = mesh->getSieve();
const Obj<typename Mesh::label_sequence>& cells = mesh->heightStratum(0);
const typename Mesh::label_sequence::iterator cEnd = cells->end();
const int numCells = cells->size();
int newCell = numCells;
Obj<typename Mesh::sieve_type> overlapSieve = new typename Mesh::sieve_type(mesh->comm(), mesh->debug());
int offset = 0;
const int cellOffset = zeroBase ? 0 : 1;
const int dim = mesh->getDimension();
std::map<typename Mesh::point_type, typename Mesh::point_type> newCells;
// TODO: This is necessary for parallel partitioning
//completion::scatterSupports(sieve, overlapSieve, mesh->getSendOverlap(), mesh->getRecvOverlap(), mesh);
if (numCells == 0) {
*numVertices = 0;
*offsets = NULL;
*adjacency = NULL;
return;
}
int *off = alloc_type().allocate(numCells+1);
int *adj;
for(int i = 0; i < numCells+1; ++i) {alloc_type().construct(off+i, 0);}
if (mesh->depth() == dim) {
int c = 1;
for(typename Mesh::label_sequence::iterator c_iter = cells->begin(); c_iter != cEnd; ++c_iter, ++c) {
const Obj<typename Mesh::sieve_type::traits::coneSequence>& faces = sieve->cone(*c_iter);
const typename Mesh::sieve_type::traits::coneSequence::iterator fEnd = faces->end();
off[c] = off[c-1];
for(typename Mesh::sieve_type::traits::coneSequence::iterator f_iter = faces->begin(); f_iter != fEnd; ++f_iter) {
if (sieve->support(*f_iter)->size() == 2) {
off[c]++;
} else if ((sieve->support(*f_iter)->size() == 1) && (overlapSieve->support(*f_iter)->size() == 1)) {
off[c]++;
}
}
}
adj = alloc_type().allocate(off[numCells]);
for(int i = 0; i < off[numCells]; ++i) {alloc_type().construct(adj+i, 0);}
for(typename Mesh::label_sequence::iterator c_iter = cells->begin(); c_iter != cEnd; ++c_iter) {
const Obj<typename Mesh::sieve_type::traits::coneSequence>& faces = sieve->cone(*c_iter);
const typename Mesh::sieve_type::traits::coneSequence::iterator fEnd = faces->end();
for(typename Mesh::sieve_type::traits::coneSequence::iterator f_iter = faces->begin(); f_iter != fEnd; ++f_iter) {
const Obj<typename Mesh::sieve_type::traits::supportSequence>& neighbors = sieve->support(*f_iter);
const typename Mesh::sieve_type::traits::supportSequence::iterator nEnd = neighbors->end();
for(typename Mesh::sieve_type::traits::supportSequence::iterator n_iter = neighbors->begin(); n_iter != nEnd; ++n_iter) {
if (*n_iter != *c_iter) adj[offset++] = *n_iter + cellOffset;
}
const Obj<typename Mesh::sieve_type::traits::supportSequence>& oNeighbors = overlapSieve->support(*f_iter);
const typename Mesh::sieve_type::traits::supportSequence::iterator onEnd = oNeighbors->end();
for(typename Mesh::sieve_type::traits::supportSequence::iterator n_iter = oNeighbors->begin(); n_iter != onEnd; ++n_iter) {
adj[offset++] = *n_iter + cellOffset;
}
}
}
} else if (mesh->depth() == 1) {
std::set<typename Mesh::point_type> *neighborCells = new std::set<typename Mesh::point_type>[numCells];
const int corners = sieve->cone(*cells->begin())->size();
int faceVertices;
if (corners == dim+1) {
faceVertices = dim;
} else if ((dim == 2) && (corners == 4)) {
faceVertices = 2;
} else if ((dim == 3) && (corners == 8)) {
faceVertices = 4;
} else if ((dim == 2) && (corners == 6)) {
faceVertices = 3;
} else if ((dim == 2) && (corners == 9)) {
faceVertices = 3;
} else if ((dim == 3) && (corners == 10)) {
faceVertices = 6;
} else if ((dim == 3) && (corners == 27)) {
faceVertices = 9;
} else {
throw ALE::Exception("Could not determine number of face vertices");
}
for(typename Mesh::label_sequence::iterator c_iter = cells->begin(); c_iter != cells->end(); ++c_iter) {
const Obj<typename Mesh::sieve_type::traits::coneSequence>& vertices = sieve->cone(*c_iter);
const typename Mesh::sieve_type::traits::coneSequence::iterator vEnd = vertices->end();
for(typename Mesh::sieve_type::traits::coneSequence::iterator v_iter = vertices->begin(); v_iter != vEnd; ++v_iter) {
const Obj<typename Mesh::sieve_type::traits::supportSequence>& neighbors = sieve->support(*v_iter);
const typename Mesh::sieve_type::traits::supportSequence::iterator nEnd = neighbors->end();
for(typename Mesh::sieve_type::traits::supportSequence::iterator n_iter = neighbors->begin(); n_iter != nEnd; ++n_iter) {
if (*c_iter == *n_iter) continue;
if ((int) sieve->nMeet(*c_iter, *n_iter, 1)->size() == faceVertices) {
if ((*c_iter < numCells) && (*n_iter < numCells)) {
neighborCells[*c_iter].insert(*n_iter);
} else {
typename Mesh::point_type e = *c_iter, n = *n_iter;
if (*c_iter >= numCells) {
if (newCells.find(*c_iter) == newCells.end()) newCells[*c_iter] = --newCell;
e = newCells[*c_iter];
}
if (*n_iter >= numCells) {
if (newCells.find(*n_iter) == newCells.end()) newCells[*n_iter] = --newCell;
n = newCells[*n_iter];
}
neighborCells[e].insert(n);
}
}
}
}
}
off[0] = 0;
for(int c = 1; c <= numCells; c++) {
off[c] = neighborCells[c-1].size() + off[c-1];
}
adj = alloc_type().allocate(off[numCells]);
for(int i = 0; i < off[numCells]; ++i) {alloc_type().construct(adj+i, 0);}
for(int c = 0; c < numCells; c++) {
for(typename std::set<typename Mesh::point_type>::iterator n_iter = neighborCells[c].begin(); n_iter != neighborCells[c].end(); ++n_iter) {
adj[offset++] = *n_iter + cellOffset;
}
}
delete [] neighborCells;
} else {
throw ALE::Exception("Dual creation not defined for partially interpolated meshes");
}
if (offset != off[numCells]) {
ostringstream msg;
msg << "ERROR: Total number of neighbors " << offset << " does not match the offset array " << off[numCells];
throw ALE::Exception(msg.str().c_str());
}
*numVertices = numCells;
*offsets = off;
*adjacency = adj;
}
template<typename Mesh>
static void buildDualCSRV(const Obj<Mesh>& mesh, MeshManager<Mesh>& manager, int *numVertices, int **offsets, int **adjacency, const bool zeroBase = true) {
const Obj<typename Mesh::sieve_type>& sieve = mesh->getSieve();
const Obj<typename Mesh::label_sequence>& cells = mesh->heightStratum(0);
const typename Mesh::label_sequence::iterator cEnd = cells->end();
const int numCells = cells->size();
Obj<typename Mesh::sieve_type> overlapSieve = new typename Mesh::sieve_type(mesh->comm(), mesh->debug());
int offset = 0;
const int cellOffset = zeroBase ? 0 : 1;
const int dim = mesh->getDimension();
std::map<typename Mesh::point_type, typename Mesh::point_type> newCells;
// TODO: This is necessary for parallel partitioning
//completion::scatterSupports(sieve, overlapSieve, mesh->getSendOverlap(), mesh->getRecvOverlap(), mesh);
overlapSieve->setChart(sieve->getChart());
overlapSieve->allocate();
if (numCells == 0) {
*numVertices = 0;
*offsets = NULL;
*adjacency = NULL;
return;
}
int *off = alloc_type().allocate(numCells+1);
int *adj;
for(int i = 0; i < numCells+1; ++i) {alloc_type().construct(off+i, 0);}
if (mesh->depth() == dim) {
OffsetVisitor<typename Mesh::sieve_type> oV(*sieve, *overlapSieve, off);
for(typename Mesh::label_sequence::iterator c_iter = cells->begin(); c_iter != cEnd; ++c_iter) {
sieve->cone(*c_iter, oV);
}
for(int p = 1; p <= numCells; ++p) {
off[p] = off[p] + off[p-1];
}
adj = alloc_type().allocate(off[numCells]);
for(int i = 0; i < off[numCells]; ++i) {alloc_type().construct(adj+i, 0);}
AdjVisitor<typename Mesh::sieve_type> aV(adj, zeroBase);
ISieveVisitor::SupportVisitor<typename Mesh::sieve_type, AdjVisitor<typename Mesh::sieve_type> > sV(*sieve, aV);
ISieveVisitor::SupportVisitor<typename Mesh::sieve_type, AdjVisitor<typename Mesh::sieve_type> > ovSV(*overlapSieve, aV);
for(typename Mesh::label_sequence::iterator c_iter = cells->begin(); c_iter != cEnd; ++c_iter) {
aV.setCell(*c_iter);
sieve->cone(*c_iter, sV);
sieve->cone(*c_iter, ovSV);
}
offset = aV.getOffset();
} else if (mesh->depth() == 1) {
typedef MeetVisitor<typename Mesh::sieve_type, MeshManager<Mesh>, SimpleFaceRecognizer<Mesh> > mv_type;
typedef typename ISieveVisitor::SupportVisitor<typename Mesh::sieve_type, mv_type> sv_type;
SimpleFaceRecognizer<Mesh> faceRecognizer(*mesh);
mv_type mV(*sieve, manager, faceRecognizer, numCells);
sv_type sV(*sieve, mV);
for(typename Mesh::label_sequence::iterator c_iter = cells->begin(); c_iter != cells->end(); ++c_iter) {
mV.setCell(*c_iter);
sieve->cone(*c_iter, sV);
}
const typename mv_type::neighbors_type *neighborCells = mV.getNeighbors();
off[0] = 0;
for(int c = 1; c <= numCells; c++) {
off[c] = neighborCells[c-1].size() + off[c-1];
}
adj = alloc_type().allocate(off[numCells]);
for(int i = 0; i < off[numCells]; ++i) {alloc_type().construct(adj+i, 0);}
for(int c = 0; c < numCells; c++) {
for(typename mv_type::neighbors_type::const_iterator n_iter = neighborCells[c].begin(); n_iter != neighborCells[c].end(); ++n_iter) {
//std::cout << "Adding dual edge from " << c << " to " << *n_iter << std::endl;
adj[offset++] = *n_iter + cellOffset;
}
}
} else {
throw ALE::Exception("Dual creation not defined for partially interpolated meshes");
}
if (offset != off[numCells]) {
ostringstream msg;
msg << "ERROR: Total number of neighbors " << offset << " does not match the offset array " << off[numCells];
throw ALE::Exception(msg.str().c_str());
}
*numVertices = numCells;
*offsets = off;
*adjacency = adj;
}
// This produces a hypergraph (each face is a vertex and each cell is a hyperedge)
// numbering: A contiguous numbering of the faces
// numEdges: The number of edges in the hypergraph
// adjacency: The vertices in each edge
template<typename Mesh>
static void buildFaceDualCSR(const Obj<Mesh>& mesh, const Obj<typename Mesh::numbering_type>& numbering, int *numEdges, int **offsets, int **adjacency, const bool zeroBase = true) {
const Obj<typename Mesh::sieve_type>& sieve = mesh->getSieve();
const Obj<typename Mesh::label_sequence>& cells = mesh->heightStratum(0);
const typename Mesh::label_sequence::iterator cEnd = cells->end();
const int faceOffset = zeroBase ? 0 : 1;
int numCells = cells->size();
int c = 1;
if (mesh->depth() != mesh->getDimension()) {throw ALE::Exception("Not yet implemented for non-interpolated meshes");}
int *off = alloc_type().allocate(numCells+1);
for(int i = 0; i < numCells+1; ++i) {alloc_type().construct(off+i, 0);}
for(typename Mesh::label_sequence::iterator c_iter = cells->begin(); c_iter != cEnd; ++c_iter, ++c) {
off[c] = sieve->cone(*c_iter)->size() + off[c-1];
}
int *adj = alloc_type().allocate(off[numCells]);
for(int i = 0; i < off[numCells]; ++i) {alloc_type().construct(adj+i, 0);}
int offset = 0;
for(typename Mesh::label_sequence::iterator c_iter = cells->begin(); c_iter != cEnd; ++c_iter) {
const Obj<typename Mesh::sieve_type::traits::coneSequence>& faces = sieve->cone(*c_iter);
const typename Mesh::sieve_type::traits::coneSequence::iterator fEnd = faces->end();
for(typename Mesh::sieve_type::traits::coneSequence::iterator f_iter = faces->begin(); f_iter != fEnd; ++f_iter, ++offset) {
adj[offset] = numbering->getIndex(*f_iter) + faceOffset;
}
}
if (offset != off[numCells]) {
ostringstream msg;
msg << "ERROR: Total number of neighbors " << offset << " does not match the offset array " << off[numCells];
throw ALE::Exception(msg.str().c_str());
}
*numEdges = numCells;
*offsets = off;
*adjacency = adj;
}
template<typename Mesh>
static void buildFaceDualCSRV(const Obj<Mesh>& mesh, const Obj<typename Mesh::numbering_type>& numbering, int *numEdges, int **offsets, int **adjacency, const bool zeroBase = true) {
throw ALE::Exception("Not implemented");
}
static void destroyCSR(int numPoints, int *offsets, int *adjacency) {
if (adjacency) {
for(int i = 0; i < offsets[numPoints]; ++i) {alloc_type().destroy(adjacency+i);}
alloc_type().deallocate(adjacency, offsets[numPoints]);
}
if (offsets) {
for(int i = 0; i < numPoints+1; ++i) {alloc_type().destroy(offsets+i);}
alloc_type().deallocate(offsets, numPoints+1);
}
}
template<typename OldSection, typename Partition, typename Renumbering, typename NewSection>
static void createLocalSection(const Obj<OldSection>& oldSection, const Obj<Partition>& partition, Renumbering& renumbering, const Obj<NewSection>& newSection) {
const typename Partition::value_type *points = partition->restrictPoint(oldSection->commRank());
const int numPoints = partition->getFiberDimension(oldSection->commRank());
for(int p = 0; p < numPoints; ++p) {
if (oldSection->hasPoint(points[p])) {
newSection->setFiberDimension(renumbering[points[p]], oldSection->getFiberDimension(points[p]));
}
}
if (numPoints) {newSection->allocatePoint();}
for(int p = 0; p < numPoints; ++p) {
if (oldSection->hasPoint(points[p])) {
newSection->updatePointAll(renumbering[points[p]], oldSection->restrictPoint(points[p]));
}
}
}
// Specialize to ArrowSection
template<typename OldSection, typename Partition, typename Renumbering>
static void createLocalSection(const Obj<OldSection>& oldSection, const Obj<Partition>& partition, Renumbering& renumbering, const Obj<UniformSection<MinimalArrow<int,int>,int> >& newSection) {
typedef UniformSection<MinimalArrow<int,int>,int> NewSection;
const typename Partition::value_type *points = partition->restrictPoint(oldSection->commRank());
const int numPoints = partition->getFiberDimension(oldSection->commRank());
const typename OldSection::chart_type& oldChart = oldSection->getChart();
std::set<typename Partition::value_type> myPoints;
for(int p = 0; p < numPoints; ++p) {
myPoints.insert(points[p]);
}
for(typename OldSection::chart_type::const_iterator c_iter = oldChart.begin(); c_iter != oldChart.end(); ++c_iter) {
if (myPoints.count(c_iter->source) && myPoints.count(c_iter->target)) {
newSection->setFiberDimension(typename OldSection::point_type(renumbering[c_iter->source], renumbering[c_iter->target]), oldSection->getFiberDimension(*c_iter));
}
}
if (oldChart.size()) {newSection->allocatePoint();}
for(typename OldSection::chart_type::const_iterator c_iter = oldChart.begin(); c_iter != oldChart.end(); ++c_iter) {
if (myPoints.count(c_iter->source) && myPoints.count(c_iter->target)) {
const typename OldSection::value_type *values = oldSection->restrictPoint(*c_iter);
newSection->updatePointAll(typename OldSection::point_type(renumbering[c_iter->source], renumbering[c_iter->target]), values);
}
}
}
template<typename Sifter, typename Section, typename Renumbering>
static void createLocalSifter(const Obj<Sifter>& sifter, const Obj<Section>& partition, Renumbering& renumbering, const Obj<Sifter>& localSifter) {
const typename Section::value_type *points = partition->restrictPoint(sifter->commRank());
const int numPoints = partition->getFiberDimension(sifter->commRank());
for(int p = 0; p < numPoints; ++p) {
const Obj<typename Sifter::traits::coneSequence>& cone = sifter->cone(points[p]);
const typename Sifter::traits::coneSequence::iterator cEnd = cone->end();
for(typename Sifter::traits::coneSequence::iterator c_iter = cone->begin(); c_iter != cEnd; ++c_iter) {
localSifter->addArrow(*c_iter, renumbering[points[p]]);
}
}
}
template<typename Sieve, typename Section, typename Renumbering>
static void createLocalSieve(const Obj<Sieve>& sieve, const Obj<Section>& partition, Renumbering& renumbering, const Obj<Sieve>& localSieve, const int height = 0) {
const typename Section::value_type *points = partition->restrictPoint(sieve->commRank());
const int numPoints = partition->getFiberDimension(sieve->commRank());
for(int p = 0; p < numPoints; ++p) {
Obj<typename Sieve::coneSet> current = new typename Sieve::coneSet();
Obj<typename Sieve::coneSet> next = new typename Sieve::coneSet();
Obj<typename Sieve::coneSet> tmp;
current->insert(points[p]);
while(current->size()) {
for(typename Sieve::coneSet::const_iterator p_iter = current->begin(); p_iter != current->end(); ++p_iter) {
const Obj<typename Sieve::traits::coneSequence>& cone = sieve->cone(*p_iter);
for(typename Sieve::traits::coneSequence::iterator c_iter = cone->begin(); c_iter != cone->end(); ++c_iter) {
localSieve->addArrow(renumbering[*c_iter], renumbering[*p_iter], c_iter.color());
next->insert(*c_iter);
}
}
tmp = current; current = next; next = tmp;
next->clear();
}
if (height) {
current->insert(points[p]);
while(current->size()) {
for(typename Sieve::coneSet::const_iterator p_iter = current->begin(); p_iter != current->end(); ++p_iter) {
const Obj<typename Sieve::traits::supportSequence>& support = sieve->support(*p_iter);
for(typename Sieve::traits::supportSequence::iterator s_iter = support->begin(); s_iter != support->end(); ++s_iter) {
localSieve->addArrow(renumbering[*p_iter], renumbering[*s_iter], s_iter.color());
next->insert(*s_iter);
}
}
tmp = current; current = next; next = tmp;
next->clear();
}
}
}
}
template<typename Mesh, typename Section, typename Renumbering>
static void createLocalMesh(const Obj<Mesh>& mesh, const Obj<Section>& partition, Renumbering& renumbering, const Obj<Mesh>& localMesh, const int height = 0) {
const Obj<typename Mesh::sieve_type>& sieve = mesh->getSieve();
const Obj<typename Mesh::sieve_type>& localSieve = localMesh->getSieve();
createLocalSieve(sieve, partition, renumbering, localSieve, height);
}
template<typename Sieve, typename Section, typename Renumbering>
static void sizeLocalSieveV(const Obj<Sieve>& sieve, const Obj<Section>& partition, Renumbering& renumbering, const Obj<Sieve>& localSieve, const int height = 0) {
typedef std::set<typename Sieve::point_type> pointSet;
const typename Section::value_type *points = partition->restrictPoint(sieve->commRank());
const int numPoints = partition->getFiberDimension(sieve->commRank());
int maxSize = std::max(0, std::max(sieve->getMaxConeSize(), sieve->getMaxSupportSize()));
const pointSet pSet(points, &points[numPoints]);
ISieveVisitor::FilteredPointRetriever<Sieve,pointSet,Renumbering> fV(pSet, renumbering, maxSize);
for(int p = 0; p < numPoints; ++p) {
sieve->cone(points[p], fV);
localSieve->setConeSize(renumbering[points[p]], fV.getSize());
fV.clear();
sieve->support(points[p], fV);
localSieve->setSupportSize(renumbering[points[p]], fV.getSize());
fV.clear();
}
}
template<typename Mesh, typename Section, typename Renumbering>
static void sizeLocalMeshV(const Obj<Mesh>& mesh, const Obj<Section>& partition, Renumbering& renumbering, const Obj<Mesh>& localMesh, const int height = 0) {
const Obj<typename Mesh::sieve_type>& sieve = mesh->getSieve();
const Obj<typename Mesh::sieve_type>& localSieve = localMesh->getSieve();
sizeLocalSieveV(sieve, partition, renumbering, localSieve, height);
}
template<typename Sieve, typename Section, typename Renumbering>
static void createLocalLabelV(const Obj<Sieve>& sieve, const Obj<Section>& partition, Renumbering& renumbering, const Obj<Sieve>& localSieve, const int height = 0) {
typedef std::set<typename Sieve::point_type> pointSet;
typedef ISieveVisitor::FilteredPointRetriever<Sieve,pointSet,Renumbering> visitor_type;
const typename Section::value_type *points = partition->restrictPoint(sieve->commRank());
const int numPoints = partition->getFiberDimension(sieve->commRank());
int maxSize = std::max(0, std::max(sieve->getMaxConeSize(), sieve->getMaxSupportSize()));
typename Sieve::point_type *oPoints = new typename Sieve::point_type[std::max(1, sieve->getMaxConeSize())];
int *oOrients = new int[std::max(1, sieve->getMaxConeSize())];
const pointSet pSet(points, &points[numPoints]);
visitor_type fV(pSet, renumbering, maxSize);
for(int p = 0; p < numPoints; ++p) {
fV.useRenumbering(false);
sieve->orientedCone(points[p], fV);
const typename visitor_type::oriented_point_type *q = fV.getOrientedPoints();
const int n = fV.getOrientedSize();
for(int i = 0; i < n; ++i) {
oPoints[i] = q[i].first;
oOrients[i] = q[i].second;
}
localSieve->setCone(oPoints, renumbering[points[p]]);
localSieve->setConeOrientation(oOrients, renumbering[points[p]]);
fV.clear();
fV.useRenumbering(true);
sieve->support(points[p], fV);
if (fV.getSize()) {localSieve->setSupport(points[p], fV.getPoints());}
fV.clear();
}
delete [] oPoints;
delete [] oOrients;
}
template<typename Sieve, typename Section, typename Renumbering>
static void createLocalSieveV(const Obj<Sieve>& sieve, const Obj<Section>& partition, Renumbering& renumbering, const Obj<Sieve>& localSieve, const int height = 0) {
typedef std::set<typename Sieve::point_type> pointSet;
typedef ISieveVisitor::FilteredPointRetriever<Sieve,pointSet,Renumbering> visitor_type;
const typename Section::value_type *points = partition->restrictPoint(sieve->commRank());
const int numPoints = partition->getFiberDimension(sieve->commRank());
int maxSize = std::max(0, std::max(sieve->getMaxConeSize(), sieve->getMaxSupportSize()));
typename Sieve::point_type *oPoints = new typename Sieve::point_type[std::max(1, sieve->getMaxConeSize())];
int *oOrients = new int[std::max(1, sieve->getMaxConeSize())];
const pointSet pSet(points, &points[numPoints]);
visitor_type fV(pSet, renumbering, maxSize);
for(int p = 0; p < numPoints; ++p) {
///sieve->cone(points[p], fV);
///localSiaeve->setCone(fV.getPoints(), renumbering[points[p]]);
sieve->orientedCone(points[p], fV);
const typename visitor_type::oriented_point_type *q = fV.getOrientedPoints();
const int n = fV.getOrientedSize();
for(int i = 0; i < n; ++i) {
oPoints[i] = q[i].first;
oOrients[i] = q[i].second;
}
localSieve->setCone(oPoints, renumbering[points[p]]);
localSieve->setConeOrientation(oOrients, renumbering[points[p]]);
fV.clear();
sieve->support(points[p], fV);
localSieve->setSupport(renumbering[points[p]], fV.getPoints());
fV.clear();
}
delete [] oPoints;
delete [] oOrients;
}
template<typename Mesh, typename Section, typename Renumbering>
static void createLocalMeshV(const Obj<Mesh>& mesh, const Obj<Section>& partition, Renumbering& renumbering, const Obj<Mesh>& localMesh, const int height = 0) {
const Obj<typename Mesh::sieve_type>& sieve = mesh->getSieve();
const Obj<typename Mesh::sieve_type>& localSieve = localMesh->getSieve();
createLocalSieveV(sieve, partition, renumbering, localSieve, height);
}
public: // Partitioning
// partition: Should be properly allocated on input
// height: Height of the point set to uniquely partition
// TODO: Could rebind assignment section to the type of the output
template<typename Mesh, typename Section>
static void createPartition(const Obj<Mesh>& mesh, const Obj<Section>& partition, const int height = 0) {
MeshManager<Mesh> manager(mesh, height);
int *start = NULL;
int *adjacency = NULL;
if (height == 0) {
int numVertices;
buildDualCSR(mesh, &numVertices, &start, &adjacency, GraphPartitioner::zeroBase());
GraphPartitioner().partition(numVertices, start, adjacency, partition, manager);
destroyCSR(numVertices, start, adjacency);
} else if (height == 1) {
int numEdges;
buildFaceDualCSR(mesh, mesh->getFactory()->getNumbering(mesh, mesh->depth()-1), &numEdges, &start, &adjacency, GraphPartitioner::zeroBase());
GraphPartitioner().partition(numEdges, start, adjacency, partition, manager);
destroyCSR(numEdges, start, adjacency);
} else {
throw ALE::Exception("Invalid partition height");
}
}
template<typename Mesh, typename Section>
static void createPartitionV(const Obj<Mesh>& mesh, const Obj<Section>& partition, const int height = 0) {
MeshManager<Mesh> manager(mesh, height);
int *start = NULL;
int *adjacency = NULL;
PETSc::Log::Event("PartitionCreate").begin();
if (height == 0) {
int numVertices;
buildDualCSRV(mesh, manager, &numVertices, &start, &adjacency, GraphPartitioner::zeroBase());
GraphPartitioner().partition(numVertices, start, adjacency, partition, manager);
destroyCSR(numVertices, start, adjacency);
} else if (height == 1) {
int numEdges;
throw ALE::Exception("Not yet implemented");
#if 0
buildFaceDualCSRV(mesh, mesh->getFactory()->getNumbering(mesh, mesh->depth()-1), &numEdges, &start, &adjacency, GraphPartitioner::zeroBase());
#endif
GraphPartitioner().partition(numEdges, start, adjacency, partition, manager);
destroyCSR(numEdges, start, adjacency);
} else {
throw ALE::Exception("Invalid partition height");
}
PETSc::Log::Event("PartitionCreate").end();
}
// Add in the points in the closure (and star) of the partitioned points
template<typename Mesh, typename Section>
static void createPartitionClosure(const Obj<Mesh>& mesh, const Obj<Section>& pointPartition, const Obj<Section>& partition, const int height = 0) {
const Obj<typename Mesh::sieve_type>& sieve = mesh->getSieve();
const typename Section::chart_type& chart = pointPartition->getChart();
size_t size = 0;
for(typename Section::chart_type::const_iterator r_iter = chart.begin(); r_iter != chart.end(); ++r_iter) {
const typename Section::value_type *points = pointPartition->restrictPoint(*r_iter);
const int numPoints = pointPartition->getFiberDimension(*r_iter);
std::set<typename Section::value_type> closure;
// TODO: Use Quiver's closure() here instead
for(int p = 0; p < numPoints; ++p) {
Obj<typename Mesh::sieve_type::coneSet> current = new typename Mesh::sieve_type::coneSet();
Obj<typename Mesh::sieve_type::coneSet> next = new typename Mesh::sieve_type::coneSet();
Obj<typename Mesh::sieve_type::coneSet> tmp;
current->insert(points[p]);
closure.insert(points[p]);
while(current->size()) {
for(typename Mesh::sieve_type::coneSet::const_iterator p_iter = current->begin(); p_iter != current->end(); ++p_iter) {
const Obj<typename Mesh::sieve_type::traits::coneSequence>& cone = sieve->cone(*p_iter);
for(typename Mesh::sieve_type::traits::coneSequence::iterator c_iter = cone->begin(); c_iter != cone->end(); ++c_iter) {
closure.insert(*c_iter);
next->insert(*c_iter);
}
}
tmp = current; current = next; next = tmp;
next->clear();
}
if (height) {
current->insert(points[p]);
while(current->size()) {
for(typename Mesh::sieve_type::coneSet::const_iterator p_iter = current->begin(); p_iter != current->end(); ++p_iter) {
const Obj<typename Mesh::sieve_type::traits::supportSequence>& support = sieve->support(*p_iter);
for(typename Mesh::sieve_type::traits::supportSequence::iterator s_iter = support->begin(); s_iter != support->end(); ++s_iter) {
closure.insert(*s_iter);
next->insert(*s_iter);
}
}
tmp = current; current = next; next = tmp;
next->clear();
}
}
}
partition->setFiberDimension(*r_iter, closure.size());
size = std::max(size, closure.size());
}
partition->allocatePoint();
typename Section::value_type *values = new typename Section::value_type[size];
for(typename Section::chart_type::const_iterator r_iter = chart.begin(); r_iter != chart.end(); ++r_iter) {
const typename Section::value_type *points = pointPartition->restrictPoint(*r_iter);
const int numPoints = pointPartition->getFiberDimension(*r_iter);
std::set<typename Section::value_type> closure;
// TODO: Use Quiver's closure() here instead
for(int p = 0; p < numPoints; ++p) {
Obj<typename Mesh::sieve_type::coneSet> current = new typename Mesh::sieve_type::coneSet();
Obj<typename Mesh::sieve_type::coneSet> next = new typename Mesh::sieve_type::coneSet();
Obj<typename Mesh::sieve_type::coneSet> tmp;
current->insert(points[p]);
closure.insert(points[p]);
while(current->size()) {
for(typename Mesh::sieve_type::coneSet::const_iterator p_iter = current->begin(); p_iter != current->end(); ++p_iter) {
const Obj<typename Mesh::sieve_type::traits::coneSequence>& cone = sieve->cone(*p_iter);
for(typename Mesh::sieve_type::traits::coneSequence::iterator c_iter = cone->begin(); c_iter != cone->end(); ++c_iter) {
closure.insert(*c_iter);
next->insert(*c_iter);
}
}
tmp = current; current = next; next = tmp;
next->clear();
}
if (height) {
current->insert(points[p]);
while(current->size()) {
for(typename Mesh::sieve_type::coneSet::const_iterator p_iter = current->begin(); p_iter != current->end(); ++p_iter) {
const Obj<typename Mesh::sieve_type::traits::supportSequence>& support = sieve->support(*p_iter);
for(typename Mesh::sieve_type::traits::supportSequence::iterator s_iter = support->begin(); s_iter != support->end(); ++s_iter) {
closure.insert(*s_iter);
next->insert(*s_iter);
}
}
tmp = current; current = next; next = tmp;
next->clear();
}
}
}
int i = 0;
for(typename std::set<typename Section::value_type>::const_iterator p_iter = closure.begin(); p_iter != closure.end(); ++p_iter, ++i) {
values[i] = *p_iter;
}
partition->updatePoint(*r_iter, values);
}
delete [] values;
}
template<typename Mesh, typename Section>
static void createPartitionClosureV(const Obj<Mesh>& mesh, const Obj<Section>& pointPartition, const Obj<Section>& partition, const int height = 0) {
typedef ISieveVisitor::TransitiveClosureVisitor<typename Mesh::sieve_type> visitor_type;
const Obj<typename Mesh::sieve_type>& sieve = mesh->getSieve();
const typename Section::chart_type& chart = pointPartition->getChart();
size_t size = 0;
PETSc::Log::Event("PartitionClosure").begin();
for(typename Section::chart_type::const_iterator r_iter = chart.begin(); r_iter != chart.end(); ++r_iter) {
const typename Section::value_type *points = pointPartition->restrictPoint(*r_iter);
const int numPoints = pointPartition->getFiberDimension(*r_iter);
typename visitor_type::visitor_type nV;
visitor_type cV(*sieve, nV);
for(int p = 0; p < numPoints; ++p) {
sieve->cone(points[p], cV);
if (height) {
cV.setIsCone(false);
sieve->support(points[p], cV);
}
}
partition->setFiberDimension(*r_iter, cV.getPoints().size());
size = std::max(size, cV.getPoints().size());
}
partition->allocatePoint();
typename Section::value_type *values = new typename Section::value_type[size];
for(typename Section::chart_type::const_iterator r_iter = chart.begin(); r_iter != chart.end(); ++r_iter) {
const typename Section::value_type *points = pointPartition->restrictPoint(*r_iter);
const int numPoints = pointPartition->getFiberDimension(*r_iter);
typename visitor_type::visitor_type nV;
visitor_type cV(*sieve, nV);
for(int p = 0; p < numPoints; ++p) {
sieve->cone(points[p], cV);
if (height) {
cV.setIsCone(false);
sieve->support(points[p], cV);
}
}
int i = 0;
for(typename std::set<typename Mesh::point_type>::const_iterator p_iter = cV.getPoints().begin(); p_iter != cV.getPoints().end(); ++p_iter, ++i) {
values[i] = *p_iter;
}
partition->updatePoint(*r_iter, values);
}
delete [] values;
PETSc::Log::Event("PartitionClosure").end();
}
#undef __FUNCT__
#define __FUNCT__ "createPartitionClosureV"
template<typename Mesh>
static PetscErrorCode createPartitionClosureV(const Obj<Mesh>& mesh, PetscSection pointSection, IS pointPartition, PetscSection *section, IS *partition, const int height = 0) {
typedef ISieveVisitor::TransitiveClosureVisitor<typename Mesh::sieve_type> visitor_type;
const Obj<typename Mesh::sieve_type>& sieve = mesh->getSieve();
const PetscInt *partArray;
PetscInt *allPoints;
PetscInt rStart, rEnd, size;
PetscErrorCode ierr;
PetscFunctionBegin;
PETSc::Log::Event("PartitionClosure").begin();
ierr = PetscSectionGetChart(pointSection, &rStart, &rEnd);CHKERRQ(ierr);
ierr = ISGetIndices(pointPartition, &partArray);CHKERRQ(ierr);
ierr = PetscSectionCreate(mesh->comm(), section);CHKERRQ(ierr);
ierr = PetscSectionSetChart(*section, rStart, rEnd);CHKERRQ(ierr);
for(PetscInt rank = rStart; rank < rEnd; ++rank) {
PetscInt numPoints, offset;
ierr = PetscSectionGetDof(pointSection, rank, &numPoints);CHKERRQ(ierr);
ierr = PetscSectionGetOffset(pointSection, rank, &offset);CHKERRQ(ierr);
{
const PetscInt *points = &partArray[offset];
typename visitor_type::visitor_type nV;
visitor_type cV(*sieve, nV);
for(PetscInt p = 0; p < numPoints; ++p) {
sieve->cone(points[p], cV);
if (height) {
cV.setIsCone(false);
sieve->support(points[p], cV);
}
}
ierr = PetscSectionSetDof(*section, rank, cV.getPoints().size());CHKERRQ(ierr);
}
}
ierr = PetscSectionSetUp(*section);CHKERRQ(ierr);
ierr = PetscSectionGetStorageSize(*section, &size);CHKERRQ(ierr);
ierr = PetscMalloc(size * sizeof(PetscInt), &allPoints);CHKERRQ(ierr);
for(PetscInt rank = rStart; rank < rEnd; ++rank) {
PetscInt numPoints, offset, newOffset;
ierr = PetscSectionGetDof(pointSection, rank, &numPoints);CHKERRQ(ierr);
ierr = PetscSectionGetOffset(pointSection, rank, &offset);CHKERRQ(ierr);
{
const PetscInt *points = &partArray[offset];
typename visitor_type::visitor_type nV;
visitor_type cV(*sieve, nV);
for(int p = 0; p < numPoints; ++p) {
sieve->cone(points[p], cV);
if (height) {
cV.setIsCone(false);
sieve->support(points[p], cV);
}
}
ierr = PetscSectionGetOffset(*section, rank, &newOffset);CHKERRQ(ierr);
for(typename std::set<typename Mesh::point_type>::const_iterator p_iter = cV.getPoints().begin(); p_iter != cV.getPoints().end(); ++p_iter, ++newOffset) {
allPoints[newOffset] = *p_iter;
}
}
}
ierr = ISRestoreIndices(pointPartition, &partArray);CHKERRQ(ierr);
ierr = ISCreateGeneral(mesh->comm(), size, allPoints, PETSC_OWN_POINTER, partition);CHKERRQ(ierr);
PETSc::Log::Event("PartitionClosure").end();
PetscFunctionReturn(0);
}
// Create a section mapping points to partitions
template<typename Section, typename MapSection>
static void createPartitionMap(const Obj<Section>& partition, const Obj<MapSection>& partitionMap) {
const typename Section::chart_type& chart = partition->getChart();
for(typename Section::chart_type::const_iterator p_iter = chart.begin(); p_iter != chart.end(); ++p_iter) {
partitionMap->setFiberDimension(*p_iter, 1);
}
partitionMap->allocatePoint();
for(typename Section::chart_type::const_iterator p_iter = chart.begin(); p_iter != chart.end(); ++p_iter) {
const typename Section::value_type *points = partition->restrictPoint(*p_iter);
const int size = partition->getFiberDimension(*p_iter);
const typename Section::point_type part = *p_iter;
for(int i = 0; i < size; ++i) {
partitionMap->updatePoint(points[i], &part);
}
}
}
};
#endif
namespace New {
template<typename Bundle_, typename Alloc_ = typename Bundle_::alloc_type>
class Partitioner {
public:
typedef Bundle_ bundle_type;
typedef Alloc_ alloc_type;
typedef typename bundle_type::sieve_type sieve_type;
typedef typename bundle_type::point_type point_type;
public:
#undef __FUNCT__
#define __FUNCT__ "buildDualCSR"
// This creates a CSR representation of the adjacency matrix for cells
// - We allow an exception to contiguous numbering.
// If the cell id > numElements, we assign a new number starting at
// the top and going downward. I know these might not match up with
// the iterator order, but we can fix it later.
static void buildDualCSR(const Obj<bundle_type>& bundle, const int dim, int **offsets, int **adjacency) {
ALE_LOG_EVENT_BEGIN;
typedef typename ALE::New::Completion<bundle_type, point_type, alloc_type> completion;
const Obj<sieve_type>& sieve = bundle->getSieve();
const Obj<typename bundle_type::label_sequence>& elements = bundle->heightStratum(0);
Obj<sieve_type> overlapSieve = new sieve_type(bundle->comm(), bundle->debug());
std::map<point_type, point_type> newCells;
int numElements = elements->size();
int newCell = numElements;
int *off = new int[numElements+1];
int offset = 0;
int *adj;
completion::scatterSupports(sieve, overlapSieve, bundle->getSendOverlap(), bundle->getRecvOverlap(), bundle);
if (numElements == 0) {
*offsets = NULL;
*adjacency = NULL;
ALE_LOG_EVENT_END;
return;
}
if (bundle->depth() == dim) {
int e = 1;
off[0] = 0;
for(typename bundle_type::label_sequence::iterator e_iter = elements->begin(); e_iter != elements->end(); ++e_iter) {
const Obj<typename sieve_type::traits::coneSequence>& faces = sieve->cone(*e_iter);
typename sieve_type::traits::coneSequence::iterator fBegin = faces->begin();
typename sieve_type::traits::coneSequence::iterator fEnd = faces->end();
off[e] = off[e-1];
for(typename sieve_type::traits::coneSequence::iterator f_iter = fBegin; f_iter != fEnd; ++f_iter) {
if (sieve->support(*f_iter)->size() == 2) {
off[e]++;
} else if ((sieve->support(*f_iter)->size() == 1) && (overlapSieve->support(*f_iter)->size() == 1)) {
off[e]++;
}
}
e++;
}
adj = new int[off[numElements]];
for(typename bundle_type::label_sequence::iterator e_iter = elements->begin(); e_iter != elements->end(); ++e_iter) {
const Obj<typename sieve_type::traits::coneSequence>& faces = sieve->cone(*e_iter);
typename sieve_type::traits::coneSequence::iterator fBegin = faces->begin();
typename sieve_type::traits::coneSequence::iterator fEnd = faces->end();
for(typename sieve_type::traits::coneSequence::iterator f_iter = fBegin; f_iter != fEnd; ++f_iter) {
const Obj<typename sieve_type::traits::supportSequence>& neighbors = sieve->support(*f_iter);
typename sieve_type::traits::supportSequence::iterator nBegin = neighbors->begin();
typename sieve_type::traits::supportSequence::iterator nEnd = neighbors->end();
for(typename sieve_type::traits::supportSequence::iterator n_iter = nBegin; n_iter != nEnd; ++n_iter) {
if (*n_iter != *e_iter) adj[offset++] = *n_iter;
}
const Obj<typename sieve_type::traits::supportSequence>& oNeighbors = overlapSieve->support(*f_iter);
typename sieve_type::traits::supportSequence::iterator onBegin = oNeighbors->begin();
typename sieve_type::traits::supportSequence::iterator onEnd = oNeighbors->end();
for(typename sieve_type::traits::supportSequence::iterator n_iter = onBegin; n_iter != onEnd; ++n_iter) {
adj[offset++] = *n_iter;
}
}
}
} else if (bundle->depth() == 1) {
std::set<point_type> *neighborCells = new std::set<point_type>[numElements];
int corners = sieve->cone(*elements->begin())->size();
int faceVertices = -1;
if (corners == dim+1) {
faceVertices = dim;
} else if ((dim == 2) && (corners == 4)) {
faceVertices = 2;
} else if ((dim == 3) && (corners == 8)) {
faceVertices = 4;
} else if ((dim == 2) && (corners == 6)) {
faceVertices = 3;
} else if ((dim == 2) && (corners == 9)) {
faceVertices = 3;
} else if ((dim == 3) && (corners == 10)) {
faceVertices = 6;
} else if ((dim == 3) && (corners == 27)) {
faceVertices = 9;
} else {
throw ALE::Exception("Could not determine number of face vertices");
}
for(typename bundle_type::label_sequence::iterator e_iter = elements->begin(); e_iter != elements->end(); ++e_iter) {
const Obj<typename sieve_type::traits::coneSequence>& vertices = sieve->cone(*e_iter);
typename sieve_type::traits::coneSequence::iterator vEnd = vertices->end();
for(typename sieve_type::traits::coneSequence::iterator v_iter = vertices->begin(); v_iter != vEnd; ++v_iter) {
const Obj<typename sieve_type::traits::supportSequence>& neighbors = sieve->support(*v_iter);
typename sieve_type::traits::supportSequence::iterator nEnd = neighbors->end();
for(typename sieve_type::traits::supportSequence::iterator n_iter = neighbors->begin(); n_iter != nEnd; ++n_iter) {
if (*e_iter == *n_iter) continue;
if ((int) sieve->nMeet(*e_iter, *n_iter, 1)->size() == faceVertices) {
if ((*e_iter < numElements) && (*n_iter < numElements)) {
neighborCells[*e_iter].insert(*n_iter);
} else {
point_type e = *e_iter, n = *n_iter;
if (*e_iter >= numElements) {
if (newCells.find(*e_iter) == newCells.end()) newCells[*e_iter] = --newCell;
e = newCells[*e_iter];
}
if (*n_iter >= numElements) {
if (newCells.find(*n_iter) == newCells.end()) newCells[*n_iter] = --newCell;
n = newCells[*n_iter];
}
neighborCells[e].insert(n);
}
}
}
}
}
off[0] = 0;
for(int e = 1; e <= numElements; e++) {
off[e] = neighborCells[e-1].size() + off[e-1];
}
adj = new int[off[numElements]];
for(int e = 0; e < numElements; e++) {
for(typename std::set<point_type>::iterator n_iter = neighborCells[e].begin(); n_iter != neighborCells[e].end(); ++n_iter) {
adj[offset++] = *n_iter;
}
}
delete [] neighborCells;
} else {
throw ALE::Exception("Dual creation not defined for partially interpolated meshes");
}
if (offset != off[numElements]) {
ostringstream msg;
msg << "ERROR: Total number of neighbors " << offset << " does not match the offset array " << off[numElements];
throw ALE::Exception(msg.str().c_str());
}
//std::cout << "numElements: " << numElements << " newCell: " << newCell << std::endl;
*offsets = off;
*adjacency = adj;
ALE_LOG_EVENT_END;
};
#undef __FUNCT__
#define __FUNCT__ "buildFaceCSR"
// This creates a CSR representation of the adjacency hypergraph for faces
static void buildFaceCSR(const Obj<bundle_type>& bundle, const int dim, const Obj<typename bundle_type::numbering_type>& fNumbering, int *numEdges, int **offsets, int **adjacency) {
ALE_LOG_EVENT_BEGIN;
const Obj<sieve_type>& sieve = bundle->getSieve();
const Obj<typename bundle_type::label_sequence>& elements = bundle->heightStratum(0);
int numElements = elements->size();
int *off = new int[numElements+1];
int e;
if (bundle->depth() != dim) {
throw ALE::Exception("Not yet implemented for non-interpolated meshes");
}
off[0] = 0;
e = 1;
for(typename bundle_type::label_sequence::iterator e_iter = elements->begin(); e_iter != elements->end(); ++e_iter) {
off[e] = sieve->cone(*e_iter)->size() + off[e-1];
e++;
}
int *adj = new int[off[numElements]];
int offset = 0;
for(typename bundle_type::label_sequence::iterator e_iter = elements->begin(); e_iter != elements->end(); ++e_iter) {
const Obj<typename sieve_type::traits::coneSequence>& faces = sieve->cone(*e_iter);
typename sieve_type::traits::coneSequence::iterator fEnd = faces->end();
for(typename sieve_type::traits::coneSequence::iterator f_iter = faces->begin(); f_iter != fEnd; ++f_iter) {
adj[offset++] = fNumbering->getIndex(*f_iter);
}
}
if (offset != off[numElements]) {
ostringstream msg;
msg << "ERROR: Total number of neighbors " << offset << " does not match the offset array " << off[numElements];
throw ALE::Exception(msg.str().c_str());
}
*numEdges = numElements;
*offsets = off;
*adjacency = adj;
ALE_LOG_EVENT_END;
};
template<typename PartitionType>
static PartitionType *subordinatePartition(const Obj<bundle_type>& bundle, int levels, const Obj<bundle_type>& subBundle, PartitionType assignment[]) {
const Obj<typename bundle_type::numbering_type>& cNumbering = bundle->getFactory()->getLocalNumbering(bundle, bundle->depth());
const Obj<typename bundle_type::label_sequence>& cells = subBundle->heightStratum(0);
const Obj<typename bundle_type::numbering_type>& sNumbering = bundle->getFactory()->getLocalNumbering(subBundle, subBundle->depth());
const int numCells = cells->size();
PartitionType *subAssignment = new PartitionType[numCells];
if (levels != 1) {
throw ALE::Exception("Cannot calculate subordinate partition for any level separation other than 1");
} else {
const Obj<typename bundle_type::sieve_type>& sieve = bundle->getSieve();
const Obj<typename bundle_type::sieve_type>& subSieve = subBundle->getSieve();
Obj<typename bundle_type::sieve_type::coneSet> tmpSet = new typename bundle_type::sieve_type::coneSet();
Obj<typename bundle_type::sieve_type::coneSet> tmpSet2 = new typename bundle_type::sieve_type::coneSet();
for(typename bundle_type::label_sequence::iterator c_iter = cells->begin(); c_iter != cells->end(); ++c_iter) {
const Obj<typename bundle_type::sieve_type::coneSequence>& cone = subSieve->cone(*c_iter);
Obj<typename bundle_type::sieve_type::supportSet> cell = sieve->nJoin1(cone);
if (cell->size() != 1) {
std::cout << "Indeterminate subordinate partition for face " << *c_iter << std::endl;
for(typename bundle_type::sieve_type::supportSet::iterator s_iter = cell->begin(); s_iter != cell->end(); ++s_iter) {
std::cout << " cell " << *s_iter << std::endl;
}
// Could relax this to choosing the first one
throw ALE::Exception("Indeterminate subordinate partition");
}
subAssignment[sNumbering->getIndex(*c_iter)] = assignment[cNumbering->getIndex(*cell->begin())];
tmpSet->clear();
tmpSet2->clear();
}
}
return subAssignment;
}
};
#ifdef PETSC_HAVE_CHACO
namespace Chaco {
template<typename Bundle_>
class Partitioner {
public:
typedef Bundle_ bundle_type;
typedef typename bundle_type::sieve_type sieve_type;
typedef typename bundle_type::point_type point_type;
typedef short int part_type;
public:
#undef __FUNCT__
#define __FUNCT__ "ChacoPartitionSieve"
static part_type *partitionSieve(const Obj<bundle_type>& bundle, const int dim) {
part_type *assignment = NULL; /* set number of each vtx (length n) */
int *start; /* start of edge list for each vertex */
int *adjacency; /* = adj -> j; edge list data */
ALE_LOG_EVENT_BEGIN;
ALE::New::Partitioner<bundle_type>::buildDualCSR(bundle, dim, &start, &adjacency);
if (bundle->commRank() == 0) {
/* arguments for Chaco library */
FREE_GRAPH = 0; /* Do not let Chaco free my memory */
int nvtxs; /* number of vertices in full graph */
int *vwgts = NULL; /* weights for all vertices */
float *ewgts = NULL; /* weights for all edges */
float *x = NULL, *y = NULL, *z = NULL; /* coordinates for inertial method */
char *outassignname = NULL; /* name of assignment output file */
char *outfilename = NULL; /* output file name */
int architecture = dim; /* 0 => hypercube, d => d-dimensional mesh */
int ndims_tot = 0; /* total number of cube dimensions to divide */
int mesh_dims[3]; /* dimensions of mesh of processors */
double *goal = NULL; /* desired set sizes for each set */
int global_method = 1; /* global partitioning algorithm */
int local_method = 1; /* local partitioning algorithm */
int rqi_flag = 0; /* should I use RQI/Symmlq eigensolver? */
int vmax = 200; /* how many vertices to coarsen down to? */
int ndims = 1; /* number of eigenvectors (2^d sets) */
double eigtol = 0.001; /* tolerance on eigenvectors */
long seed = 123636512; /* for random graph mutations */
float *vCoords[3];
PetscErrorCode ierr;
ierr = PetscOptionsGetInt(NULL, "-partitioner_chaco_global_method", &global_method, NULL);CHKERROR(ierr, "Error in PetscOptionsGetInt");
ierr = PetscOptionsGetInt(NULL, "-partitioner_chaco_local_method", &local_method, NULL);CHKERROR(ierr, "Error in PetscOptionsGetInt");
if (global_method == 3) {
// Inertial Partitioning
ierr = PetscMalloc3(nvtxs,float,&x,nvtxs,float,&y,nvtxs,float,&z);CHKERROR(ierr, "Error in PetscMalloc");
vCoords[0] = x; vCoords[1] = y; vCoords[2] = z;
const Obj<typename bundle_type::label_sequence>& cells = bundle->heightStratum(0);
const Obj<typename bundle_type::real_section_type>& coordinates = bundle->getRealSection("coordinates");
const int corners = bundle->size(coordinates, *(cells->begin()))/dim;
int c = 0;
for(typename bundle_type::label_sequence::iterator c_iter = cells->begin(); c_iter !=cells->end(); ++c_iter, ++c) {
const double *coords = bundle->restrictClosure(coordinates, *c_iter);
for(int d = 0; d < dim; ++d) {
vCoords[d][c] = 0.0;
}
for(int v = 0; v < corners; ++v) {
for(int d = 0; d < dim; ++d) {
vCoords[d][c] += coords[v*dim+d];
}
}
for(int d = 0; d < dim; ++d) {
vCoords[d][c] /= corners;
}
}
}
nvtxs = bundle->heightStratum(0)->size();
mesh_dims[0] = bundle->commSize(); mesh_dims[1] = 1; mesh_dims[2] = 1;
for(int e = 0; e < start[nvtxs]; e++) {
adjacency[e]++;
}
assignment = new part_type[nvtxs];
ierr = PetscMemzero(assignment, nvtxs * sizeof(part_type));CHKERROR(ierr, "Error in PetscMemzero");
/* redirect output to buffer: chaco -> msgLog */
#ifdef PETSC_HAVE_UNISTD_H
char *msgLog;
int fd_stdout, fd_pipe[2], count;
fd_stdout = dup(1);
pipe(fd_pipe);
close(1);
dup2(fd_pipe[1], 1);
msgLog = new char[16284];
#endif
ierr = interface(nvtxs, start, adjacency, vwgts, ewgts, x, y, z,
outassignname, outfilename, assignment, architecture, ndims_tot,
mesh_dims, goal, global_method, local_method, rqi_flag, vmax, ndims,
eigtol, seed);
#ifdef PETSC_HAVE_UNISTD_H
int SIZE_LOG = 10000;
fflush(stdout);
count = read(fd_pipe[0], msgLog, (SIZE_LOG - 1) * sizeof(char));
if (count < 0) count = 0;
msgLog[count] = 0;
close(1);
dup2(fd_stdout, 1);
close(fd_stdout);
close(fd_pipe[0]);
close(fd_pipe[1]);
if (bundle->debug()) {
std::cout << msgLog << std::endl;
}
delete [] msgLog;
#endif
if (global_method == 3) {
// Inertial Partitioning
ierr = PetscFree3(x, y, z);CHKERROR(ierr, "Error in PetscFree");
}
}
if (adjacency) delete [] adjacency;
if (start) delete [] start;
ALE_LOG_EVENT_END;
return assignment;
};
static part_type *partitionSieveByFace(const Obj<bundle_type>& bundle, const int dim) {
throw ALE::Exception("Chaco cannot partition a mesh by faces");
};
};
};
#endif
#ifdef PETSC_HAVE_PARMETIS
namespace ParMetis {
template<typename Bundle_>
class Partitioner {
public:
typedef Bundle_ bundle_type;
typedef typename bundle_type::sieve_type sieve_type;
typedef typename bundle_type::point_type point_type;
typedef int part_type;
public:
#undef __FUNCT__
#define __FUNCT__ "ParMetisPartitionSieve"
static part_type *partitionSieve(const Obj<bundle_type>& bundle, const int dim) {
PetscInt nvtxs = 0; // The number of vertices in full graph
PetscInt *vtxdist; // Distribution of vertices across processes
PetscInt *xadj; // Start of edge list for each vertex
PetscInt *adjncy; // Edge lists for all vertices
PetscInt *vwgt = NULL; // Vertex weights
PetscInt *adjwgt = NULL; // Edge weights
PetscInt wgtflag = 0; // Indicates which weights are present
PetscInt numflag = 0; // Indicates initial offset (0 or 1)
PetscInt ncon = 1; // The number of weights per vertex
PetscInt nparts = bundle->commSize(); // The number of partitions
PetscReal *tpwgts; // The fraction of vertex weights assigned to each partition
PetscReal *ubvec; // The balance intolerance for vertex weights
int options[5]; // Options
// Outputs
PetscInt edgeCut; // The number of edges cut by the partition
PetscInt *assignment = NULL; // The vertex partition
options[0] = 0; // Use all defaults
vtxdist = new PetscInt[nparts+1];
vtxdist[0] = 0;
tpwgts = new PetscReal[ncon*nparts];
for(PetscInt p = 0; p < nparts; ++p) {
tpwgts[p] = 1.0/nparts;
}
ubvec = new PetscReal[ncon];
ubvec[0] = 1.05;
nvtxs = bundle->heightStratum(0)->size();
assignment = new part_type[nvtxs];
MPI_Allgather(&nvtxs, 1, MPIU_INT, &vtxdist[1], 1, MPIU_INT, bundle->comm());
for(PetscInt p = 2; p <= nparts; ++p) {
vtxdist[p] += vtxdist[p-1];
}
if (bundle->commSize() == 1) {
PetscMemzero(assignment, nvtxs * sizeof(part_type));
} else {
ALE::New::Partitioner<bundle_type>::buildDualCSR(bundle, dim, &xadj, &adjncy);
if (bundle->debug() && nvtxs) {
for(PetscInt p = 0; p <= nvtxs; ++p) {
std::cout << "["<<bundle->commRank()<<"]xadj["<<p<<"] = " << xadj[p] << std::endl;
}
for(PetscInt i = 0; i < xadj[nvtxs]; ++i) {
std::cout << "["<<bundle->commRank()<<"]adjncy["<<i<<"] = " << adjncy[i] << std::endl;
}
}
if (vtxdist[1] == vtxdist[nparts]) {
if (bundle->commRank() == 0) {
/* Parameters changes (Matt, check to make sure it's right):
* (removed) numflags
*/
METIS_PartGraphKway(&nvtxs, &ncon, xadj, adjncy, vwgt, NULL, adjwgt, &nparts, tpwgts, ubvec, NULL, &edgeCut, assignment);
if (bundle->debug()) {std::cout << "Metis: edgecut is " << edgeCut << std::endl;}
}
} else {
MPI_Comm comm = bundle->comm();
ParMETIS_V3_PartKway(vtxdist, xadj, adjncy, vwgt, adjwgt, &wgtflag, &numflag, &ncon, &nparts, tpwgts, ubvec, options, &edgeCut, assignment, &comm);
if (bundle->debug()) {std::cout << "ParMetis: edgecut is " << edgeCut << std::endl;}
}
if (xadj != NULL) delete [] xadj;
if (adjncy != NULL) delete [] adjncy;
}
delete [] vtxdist;
delete [] tpwgts;
delete [] ubvec;
return assignment;
};
#undef __FUNCT__
#define __FUNCT__ "ParMetisPartitionSieveByFace"
static part_type *partitionSieveByFace(const Obj<bundle_type>& bundle, const int dim) {
#ifdef PETSC_HAVE_HMETIS
int *assignment = NULL; // The vertex partition
int nvtxs; // The number of vertices
int nhedges; // The number of hyperedges
int *vwgts; // The vertex weights
int *eptr; // The offsets of each hyperedge
int *eind; // The vertices in each hyperedge, indexed by eptr
int *hewgts; // The hyperedge weights
int nparts; // The number of partitions
int ubfactor; // The allowed load imbalance (1-50)
int options[9]; // Options
// Outputs
int edgeCut; // The number of edges cut by the partition
const Obj<ALE::Mesh::numbering_type>& fNumbering = bundle->getFactory()->getNumbering(bundle, bundle->depth()-1);
if (topology->commRank() == 0) {
nvtxs = bundle->heightStratum(1)->size();
vwgts = NULL;
hewgts = NULL;
nparts = bundle->commSize();
ubfactor = 5;
options[0] = 1; // Use all defaults
options[1] = 10; // Number of bisections tested
options[2] = 1; // Vertex grouping scheme
options[3] = 1; // Objective function
options[4] = 1; // V-cycle refinement
options[5] = 0;
options[6] = 0;
options[7] = 1; // Random seed
options[8] = 24; // Debugging level
assignment = new part_type[nvtxs];
if (bundle->commSize() == 1) {
PetscMemzero(assignment, nvtxs * sizeof(part_type));
} else {
ALE::New::Partitioner<bundle_type>::buildFaceCSR(bundle, dim, fNumbering, &nhedges, &eptr, &eind);
HMETIS_PartKway(nvtxs, nhedges, vwgts, eptr, eind, hewgts, nparts, ubfactor, options, assignment, &edgeCut);
delete [] eptr;
delete [] eind;
}
if (bundle->debug()) {for (int i = 0; i<nvtxs; i++) printf("[%d] %d\n", PetscGlobalRank, assignment[i]);}
} else {
assignment = NULL;
}
return assignment;
#else
throw ALE::Exception("hmetis partitioner is not available.");
#endif
};
};
};
#endif
#ifdef PETSC_HAVE_ZOLTAN
namespace Zoltan {
template<typename Bundle_>
class Partitioner {
public:
typedef Bundle_ bundle_type;
typedef typename bundle_type::sieve_type sieve_type;
typedef typename bundle_type::point_type point_type;
typedef int part_type;
public:
static part_type *partitionSieve(const Obj<bundle_type>& bundle, const int dim) {
throw ALE::Exception("Zoltan partition by cells not implemented");
};
#undef __FUNCT__
#define __FUNCT__ "ZoltanPartitionSieveByFace"
static part_type *partitionSieveByFace(const Obj<bundle_type>& bundle, const int dim) {
// Outputs
float version; // The library version
int changed; // Did the partition change?
int numGidEntries; // Number of array entries for a single global ID (1)
int numLidEntries; // Number of array entries for a single local ID (1)
int numImport; // The number of imported points
ZOLTAN_ID_PTR import_global_ids; // The imported points
ZOLTAN_ID_PTR import_local_ids; // The imported points
int *import_procs; // The proc each point was imported from
int *import_to_part; // The partition of each imported point
int numExport; // The number of exported points
ZOLTAN_ID_PTR export_global_ids; // The exported points
ZOLTAN_ID_PTR export_local_ids; // The exported points
int *export_procs; // The proc each point was exported to
int *export_to_part; // The partition assignment of all local points
int *assignment; // The partition assignment of all local points
const Obj<typename bundle_type::numbering_type>& fNumbering = bundle->getFactory()->getNumbering(bundle, bundle->depth()-1);
if (bundle->commSize() == 1) {
PetscMemzero(assignment, bundle->heightStratum(1)->size() * sizeof(part_type));
} else {
if (bundle->commRank() == 0) {
nvtxs_Zoltan = bundle->heightStratum(1)->size();
ALE::New::Partitioner<bundle_type>::buildFaceCSR(bundle, dim, fNumbering, &nhedges_Zoltan, &eptr_Zoltan, &eind_Zoltan);
assignment = new int[nvtxs_Zoltan];
} else {
nvtxs_Zoltan = bundle->heightStratum(1)->size();
nhedges_Zoltan = 0;
eptr_Zoltan = new int[1];
eind_Zoltan = new int[1];
eptr_Zoltan[0] = 0;
assignment = NULL;
}
int ierr = Zoltan_Initialize(0, NULL, &version);
struct Zoltan_Struct *zz = Zoltan_Create(bundle->comm());
// General parameters
Zoltan_Set_Param(zz, "DEBUG_LEVEL", "2");
Zoltan_Set_Param(zz, "LB_METHOD", "PHG");
Zoltan_Set_Param(zz, "RETURN_LISTS", "PARTITION");
// PHG parameters
Zoltan_Set_Param(zz, "PHG_OUTPUT_LEVEL", "2");
Zoltan_Set_Param(zz, "PHG_EDGE_SIZE_THRESHOLD", "1.0"); // Do not throw out dense edges
// Call backs
Zoltan_Set_Num_Obj_Fn(zz, getNumVertices_Zoltan, NULL);
Zoltan_Set_Obj_List_Fn(zz, getLocalElements_Zoltan, NULL);
Zoltan_Set_HG_Size_CS_Fn(zz, getHgSizes_Zoltan, NULL);
Zoltan_Set_HG_CS_Fn(zz, getHg_Zoltan, NULL);
// Debugging
//Zoltan_Generate_Files(zz, "zoltan.debug", 1, 0, 0, 1); // if using hypergraph callbacks
ierr = Zoltan_LB_Partition(zz, &changed, &numGidEntries, &numLidEntries,
&numImport, &import_global_ids, &import_local_ids, &import_procs, &import_to_part,
&numExport, &export_global_ids, &export_local_ids, &export_procs, &export_to_part);
for(int v = 0; v < nvtxs_Zoltan; ++v) {
assignment[v] = export_to_part[v];
}
Zoltan_LB_Free_Part(&import_global_ids, &import_local_ids, &import_procs, &import_to_part);
Zoltan_LB_Free_Part(&export_global_ids, &export_local_ids, &export_procs, &export_to_part);
Zoltan_Destroy(&zz);
delete [] eptr_Zoltan;
delete [] eind_Zoltan;
}
if (assignment) {for (int i=0; i<nvtxs_Zoltan; i++) printf("[%d] %d\n",PetscGlobalRank,assignment[i]);}
return assignment;
};
};
};
#endif
}
}
#endif
|