/usr/include/trilinos/Zoltan2_TaskMapping.hpp is in libtrilinos-zoltan2-dev 12.10.1-3.
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 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 | #ifndef _ZOLTAN2_COORD_PARTITIONMAPPING_HPP_
#define _ZOLTAN2_COORD_PARTITIONMAPPING_HPP_
#include <fstream>
#include <ctime>
#include <vector>
#include "Zoltan2_AlgMultiJagged.hpp"
#include "Teuchos_ArrayViewDecl.hpp"
#include "Zoltan2_PartitionMapping.hpp"
#include "Zoltan2_MachineRepresentation.hpp"
#include "Teuchos_ReductionOp.hpp"
#include "Zoltan2_MappingSolution.hpp"
#include "Zoltan2_GraphModel.hpp"
#include <zoltan_dd.h>
#include <Zoltan2_TPLTraits.hpp>
#include "Teuchos_Comm.hpp"
#ifdef HAVE_ZOLTAN2_MPI
#include "Teuchos_DefaultMpiComm.hpp"
#endif // HAVE_ZOLTAN2_MPI
#include <Teuchos_DefaultSerialComm.hpp>
//#define gnuPlot
#include "Zoltan2_XpetraMultiVectorAdapter.hpp"
namespace Teuchos{
/*! \brief Zoltan2_ReduceBestMapping Class, reduces the minimum cost mapping, ties breaks with minimum proc id.
*/
template <typename Ordinal, typename T>
class Zoltan2_ReduceBestMapping : public ValueTypeReductionOp<Ordinal,T>
{
private:
T _EPSILON;
public:
/*! \brief Default Constructor
*/
Zoltan2_ReduceBestMapping ():_EPSILON (std::numeric_limits<T>::epsilon()){}
/*! \brief Implement Teuchos::ValueTypeReductionOp interface
*/
void reduce( const Ordinal count, const T inBuffer[], T inoutBuffer[]) const
{
for (Ordinal i=0; i < count; i++){
if (inBuffer[0] - inoutBuffer[0] < -_EPSILON){
inoutBuffer[0] = inBuffer[0];
inoutBuffer[1] = inBuffer[1];
} else if(inBuffer[0] - inoutBuffer[0] < _EPSILON &&
inBuffer[1] - inoutBuffer[1] < _EPSILON){
inoutBuffer[0] = inBuffer[0];
inoutBuffer[1] = inBuffer[1];
}
}
}
};
} // namespace Teuchos
namespace Zoltan2{
template <typename it>
inline it z2Fact(it x) {
return (x == 1 ? x : x * z2Fact<it>(x - 1));
}
template <typename gno_t, typename part_t>
class GNO_LNO_PAIR{
public:
gno_t gno;
part_t part;
};
//returns the ith permutation indices.
template <typename IT>
void ithPermutation(const IT n, IT i, IT *perm)
{
IT j, k = 0;
IT *fact = new IT[n];
// compute factorial numbers
fact[k] = 1;
while (++k < n)
fact[k] = fact[k - 1] * k;
// compute factorial code
for (k = 0; k < n; ++k)
{
perm[k] = i / fact[n - 1 - k];
i = i % fact[n - 1 - k];
}
// readjust values to obtain the permutation
// start from the end and check if preceding values are lower
for (k = n - 1; k > 0; --k)
for (j = k - 1; j >= 0; --j)
if (perm[j] <= perm[k])
perm[k]++;
delete [] fact;
}
template <typename part_t>
void getGridCommunicationGraph(part_t taskCount, part_t *&task_comm_xadj, part_t *&task_comm_adj, std::vector <int> grid_dims){
int dim = grid_dims.size();
int neighborCount = 2 * dim;
task_comm_xadj = allocMemory<part_t>(taskCount+1);
task_comm_adj = allocMemory<part_t>(taskCount * neighborCount);
part_t neighBorIndex = 0;
task_comm_xadj[0] = 0;
for (part_t i = 0; i < taskCount; ++i){
part_t prevDimMul = 1;
for (int j = 0; j < dim; ++j){
part_t lNeighbor = i - prevDimMul;
part_t rNeighbor = i + prevDimMul;
prevDimMul *= grid_dims[j];
if (lNeighbor >= 0 && lNeighbor/ prevDimMul == i / prevDimMul && lNeighbor < taskCount){
task_comm_adj[neighBorIndex++] = lNeighbor;
}
if (rNeighbor >= 0 && rNeighbor/ prevDimMul == i / prevDimMul && rNeighbor < taskCount){
task_comm_adj[neighBorIndex++] = rNeighbor;
}
}
task_comm_xadj[i+1] = neighBorIndex;
}
}
//returns the center of the parts.
template <typename Adapter, typename scalar_t, typename part_t>
void getSolutionCenterCoordinates(
const Environment *envConst,
const Teuchos::Comm<int> *comm,
const Zoltan2::CoordinateModel<typename Adapter::base_adapter_t> *coords,
//const Zoltan2::PartitioningSolution<Adapter> *soln_,
const part_t *parts,
int coordDim,
part_t ntasks,
scalar_t **partCenters){
typedef typename Adapter::lno_t lno_t;
typedef typename Adapter::gno_t gno_t;
typedef StridedData<lno_t, scalar_t> input_t;
ArrayView<const gno_t> gnos;
ArrayView<input_t> xyz;
ArrayView<input_t> wgts;
coords->getCoordinates(gnos, xyz, wgts);
//local and global num coordinates.
lno_t numLocalCoords = coords->getLocalNumCoordinates();
//gno_t numGlobalCoords = coords->getGlobalNumCoordinates();
//local number of points in each part.
gno_t *point_counts = allocMemory<gno_t>(ntasks);
memset(point_counts, 0, sizeof(gno_t) * ntasks);
//global number of points in each part.
gno_t *global_point_counts = allocMemory<gno_t>(ntasks);
scalar_t **multiJagged_coordinates = allocMemory<scalar_t *>(coordDim);
for (int dim=0; dim < coordDim; dim++){
ArrayRCP<const scalar_t> ar;
xyz[dim].getInputArray(ar);
//multiJagged coordinate values assignment
multiJagged_coordinates[dim] = (scalar_t *)ar.getRawPtr();
memset(partCenters[dim], 0, sizeof(scalar_t) * ntasks);
}
//get parts with parallel gnos.
//const part_t *parts = soln_->getPartListView();
/*
for (lno_t i=0; i < numLocalCoords; i++){
cout << "me:" << comm->getRank() << " gno:" << soln_gnos[i] << " tmp.part :" << parts[i]<< endl;
}
*/
envConst->timerStart(MACRO_TIMERS, "Mapping - Center Calculation");
for (lno_t i=0; i < numLocalCoords; i++){
gno_t g = gnos[i];
part_t p = parts[i];
//add up all coordinates in each part.
for(int j = 0; j < coordDim; ++j){
scalar_t c = multiJagged_coordinates[j][i];
partCenters[j][p] += c;
}
++point_counts[p];
}
//get global number of points in each part.
reduceAll<int, gno_t>(*comm, Teuchos::REDUCE_SUM,
ntasks, point_counts, global_point_counts
);
for(int j = 0; j < coordDim; ++j){
for (part_t i=0; i < ntasks; ++i){
partCenters[j][i] /= global_point_counts[i];
}
}
scalar_t *tmpCoords = allocMemory<scalar_t>(ntasks);
for(int j = 0; j < coordDim; ++j){
reduceAll<int, scalar_t>(*comm, Teuchos::REDUCE_SUM,
ntasks, partCenters[j], tmpCoords
);
scalar_t *tmp = partCenters[j];
partCenters[j] = tmpCoords;
tmpCoords = tmp;
}
envConst->timerStop(MACRO_TIMERS, "Mapping - Center Calculation");
freeArray<gno_t> (point_counts);
freeArray<gno_t> (global_point_counts);
freeArray<scalar_t> (tmpCoords);
freeArray<scalar_t *>(multiJagged_coordinates);
}
//returns the coarsend part graph.
template <typename Adapter, typename scalar_t, typename part_t>
void getCoarsenedPartGraph(
const Environment *envConst,
const Teuchos::Comm<int> *comm,
const Zoltan2::GraphModel<typename Adapter::base_adapter_t> *graph,
//const Zoltan2::PartitioningSolution<Adapter> *soln_,
part_t np,
const part_t *parts,
ArrayRCP<part_t> &g_part_xadj,
ArrayRCP<part_t> &g_part_adj,
ArrayRCP<scalar_t> &g_part_ew
){
typedef typename Adapter::lno_t t_lno_t;
typedef typename Adapter::gno_t t_gno_t;
typedef typename Adapter::scalar_t t_scalar_t;
typedef typename Zoltan2::GraphModel<typename Adapter::base_adapter_t>::input_t t_input_t;
//int numRanks = comm->getSize();
//int myRank = comm->getRank();
//get parts with parallel gnos.
/*
const part_t *parts = soln_->getPartListView();
part_t np = soln_->getActualGlobalNumberOfParts();
if (part_t (soln_->getTargetGlobalNumberOfParts()) > np){
np = soln_->getTargetGlobalNumberOfParts();
}
*/
t_lno_t localNumVertices = graph->getLocalNumVertices();
t_gno_t globalNumVertices = graph->getGlobalNumVertices();
t_lno_t localNumEdges = graph->getLocalNumEdges();
//get the vertex global ids, and weights
ArrayView<const t_gno_t> Ids;
ArrayView<t_input_t> v_wghts;
graph->getVertexList(Ids, v_wghts);
//get the edge ids, and weights
ArrayView<const t_gno_t> edgeIds;
ArrayView<const t_lno_t> offsets;
ArrayView<t_input_t> e_wgts;
graph->getEdgeList(edgeIds, offsets, e_wgts);
std::vector <t_scalar_t> edge_weights;
int numWeightPerEdge = graph->getNumWeightsPerEdge();
if (numWeightPerEdge > 0){
edge_weights = std::vector <t_scalar_t> (localNumEdges);
for (t_lno_t i = 0; i < localNumEdges; ++i){
edge_weights[i] = e_wgts[0][i];
}
}
//create a zoltan dictionary to get the parts of the vertices
//at the other end of edges
std::vector <part_t> e_parts (localNumEdges);
#ifdef HAVE_ZOLTAN2_MPI
if (comm->getSize() > 1)
{
Zoltan_DD_Struct *dd = NULL;
MPI_Comm mpicomm = Teuchos::getRawMpiComm(*comm);
int size_gnot = Zoltan2::TPL_Traits<ZOLTAN_ID_PTR, t_gno_t>::NUM_ID;
int debug_level = 0;
Zoltan_DD_Create(&dd, mpicomm,
size_gnot, 0,
sizeof(part_t), localNumVertices, debug_level);
ZOLTAN_ID_PTR ddnotneeded = NULL; // Local IDs not needed
Zoltan_DD_Update(
dd,
(ZOLTAN_ID_PTR) Ids.getRawPtr(),
ddnotneeded,
(char *) parts,
NULL,
int(localNumVertices));
Zoltan_DD_Find(
dd,
(ZOLTAN_ID_PTR) edgeIds.getRawPtr(),
ddnotneeded,
(char *)&(e_parts[0]),
NULL,
localNumEdges,
NULL
);
Zoltan_DD_Destroy(&dd);
} else
#endif
{
/*
std::cout << "localNumVertices:" << localNumVertices
<< " np:" << np
<< " globalNumVertices:" << globalNumVertices
<< " localNumEdges:" << localNumEdges << std::endl;
*/
for (t_lno_t i = 0; i < localNumEdges; ++i){
t_gno_t ei = edgeIds[i];
part_t p = parts[ei];
e_parts[i] = p;
}
//get the vertices in each part in my part.
std::vector <t_lno_t> part_begins(np, -1);
std::vector <t_lno_t> part_nexts(localNumVertices, -1);
//cluster vertices according to their parts.
//create local part graph.
for (t_lno_t i = 0; i < localNumVertices; ++i){
part_t ap = parts[i];
part_nexts[i] = part_begins[ap];
part_begins[ap] = i;
}
g_part_xadj = ArrayRCP<part_t> (np + 1);
g_part_adj = ArrayRCP<part_t> (localNumEdges);
g_part_ew = ArrayRCP<t_scalar_t> (localNumEdges);
part_t nindex = 0;
g_part_xadj[0] = 0;
std::vector <part_t> part_neighbors (np);
std::vector <t_scalar_t> part_neighbor_weights(np, 0);
std::vector <t_scalar_t> part_neighbor_weights_ordered(np);
//coarsen for all vertices in my part in order with parts.
for (t_lno_t i = 0; i < np; ++i){
part_t num_neighbor_parts = 0;
t_lno_t v = part_begins[i];
//get part i, and first vertex in this part v.
while (v != -1){
//now get the neightbors of v.
for (t_lno_t j = offsets[v]; j < offsets[v+1]; ++j){
//get the part of the second vertex.
part_t ep = e_parts[j];
t_scalar_t ew = 1;
if (numWeightPerEdge > 0){
ew = edge_weights[j];
}
//std::cout << "part:" << i << " v:" << v << " part2:" << ep << " v2:" << edgeIds[j] << " w:" << ew << std::endl;
//add it to my local part neighbors for part i.
if (part_neighbor_weights[ep] < 0.00001){
part_neighbors[num_neighbor_parts++] = ep;
}
part_neighbor_weights[ep] += ew;
}
v = part_nexts[v];
}
//now get the part list.
for (t_lno_t j = 0; j < num_neighbor_parts; ++j){
part_t neighbor_part = part_neighbors[j];
g_part_adj[nindex] = neighbor_part;
g_part_ew[nindex++] = part_neighbor_weights[neighbor_part];
part_neighbor_weights[neighbor_part] = 0;
}
g_part_xadj[i + 1] = nindex;
}
return;
}
RCP<const Teuchos::Comm<int> > tcomm = rcpFromRef(*comm);
typedef Tpetra::Map<>::node_type t_node_t;
typedef Tpetra::Map<part_t, part_t, t_node_t> map_t;
Teuchos::RCP<const map_t> map = Teuchos::rcp (new map_t (np, 0, tcomm));
typedef Tpetra::CrsMatrix<t_scalar_t, part_t, part_t, t_node_t> tcrsMatrix_t;
Teuchos::RCP<tcrsMatrix_t> tMatrix(new tcrsMatrix_t (map, 0));
envConst->timerStart(MACRO_TIMERS, "GRAPHCREATE Coarsen");
{
//get the vertices in each part in my part.
std::vector <t_lno_t> part_begins(np, -1);
std::vector <t_lno_t> part_nexts(localNumVertices, -1);
//cluster vertices according to their parts.
//create local part graph.
for (t_lno_t i = 0; i < localNumVertices; ++i){
part_t ap = parts[i];
part_nexts[i] = part_begins[ap];
part_begins[ap] = i;
}
std::vector <part_t> part_neighbors (np);
std::vector <t_scalar_t> part_neighbor_weights(np, 0);
std::vector <t_scalar_t> part_neighbor_weights_ordered(np);
//coarsen for all vertices in my part in order with parts.
for (t_lno_t i = 0; i < np; ++i){
part_t num_neighbor_parts = 0;
t_lno_t v = part_begins[i];
//get part i, and first vertex in this part v.
while (v != -1){
//now get the neightbors of v.
for (t_lno_t j = offsets[v]; j < offsets[v+1]; ++j){
//get the part of the second vertex.
part_t ep = e_parts[j];
t_scalar_t ew = 1;
if (numWeightPerEdge > 0){
ew = edge_weights[j];
}
//add it to my local part neighbors for part i.
if (part_neighbor_weights[ep] < 0.00001){
part_neighbors[num_neighbor_parts++] = ep;
}
part_neighbor_weights[ep] += ew;
}
v = part_nexts[v];
}
//now get the part list.
for (t_lno_t j = 0; j < num_neighbor_parts; ++j){
part_t neighbor_part = part_neighbors[j];
part_neighbor_weights_ordered[j] = part_neighbor_weights[neighbor_part];
part_neighbor_weights[neighbor_part] = 0;
}
//insert it to tpetra crsmatrix.
if (num_neighbor_parts > 0){
Teuchos::ArrayView<const part_t> destinations(
&(part_neighbors[0]), num_neighbor_parts);
Teuchos::ArrayView<const t_scalar_t>
vals(&(part_neighbor_weights_ordered[0]), num_neighbor_parts);
tMatrix->insertGlobalValues (i,destinations, vals);
}
}
}
envConst->timerStop(MACRO_TIMERS, "GRAPHCREATE Coarsen");
envConst->timerStart(MACRO_TIMERS, "GRAPHCREATE fillComplete");
tMatrix->fillComplete ();
envConst->timerStop(MACRO_TIMERS, "GRAPHCREATE fillComplete");
std::vector <part_t> part_indices(np);
for (part_t i = 0; i < np; ++i) part_indices[i] = i;
Teuchos::ArrayView<const part_t>
global_ids( &(part_indices[0]), np);
//create a map where all processors own all rows.
//so that we do a gatherAll for crsMatrix.
Teuchos::RCP<const map_t> gatherRowMap(new map_t (
Teuchos::OrdinalTraits<Tpetra::global_size_t>::invalid(), global_ids, 0, tcomm));
envConst->timerStart(MACRO_TIMERS, "GRAPHCREATE Import");
//create the importer for gatherAll
Teuchos::RCP<tcrsMatrix_t> A_gather =
Teuchos::rcp (new tcrsMatrix_t (gatherRowMap, 0));
typedef Tpetra::Import<typename map_t::local_ordinal_type,
typename map_t::global_ordinal_type,
typename map_t::node_type> import_type;
import_type import (map, gatherRowMap);
A_gather->doImport (*tMatrix, import, Tpetra::INSERT);
A_gather->fillComplete ();
envConst->timerStop(MACRO_TIMERS, "GRAPHCREATE Import");
//create the output part arrays.
//all processors owns whole copy.
g_part_xadj = ArrayRCP<part_t> (np + 1);
g_part_adj = ArrayRCP<part_t> (A_gather->getNodeNumEntries ());
g_part_ew = ArrayRCP<t_scalar_t> (A_gather->getNodeNumEntries ());
part_t *taskidx = g_part_xadj.getRawPtr();
part_t *taskadj = g_part_adj.getRawPtr();
t_scalar_t *taskadjwgt = g_part_ew.getRawPtr();
taskidx[0] = 0;
envConst->timerStart(MACRO_TIMERS, "GRAPHCREATE Import Copy");
for (part_t i = 0; i < np; i++) {
part_t length = A_gather->getNumEntriesInLocalRow(i); // Use Global to get same
size_t nentries;
taskidx[i+1] = taskidx[i] + length;
//get the indices
Teuchos::ArrayView<part_t> Indices(taskadj + taskidx[i], length);
Teuchos::ArrayView<t_scalar_t> Values(taskadjwgt + taskidx[i], length);
A_gather->getLocalRowCopy(i, Indices, Values, nentries);
}
envConst->timerStop(MACRO_TIMERS, "GRAPHCREATE Import Copy");
}
/*! \brief KmeansHeap Class, max heap, but holds the minimum values.
*/
template <class IT, class WT>
class KmeansHeap{
IT heapSize;
IT *indices;
WT *values;
WT _EPSILON;
public:
void setHeapsize(IT heapsize_){
this->heapSize = heapsize_;
this->indices = allocMemory<IT>(heapsize_ );
this->values = allocMemory<WT>(heapsize_ );
this->_EPSILON = std::numeric_limits<WT>::epsilon();
}
~KmeansHeap(){
freeArray<IT>(this->indices);
freeArray<WT>(this->values);
}
void addPoint(IT index, WT distance){
WT maxVal = this->values[0];
//add only the distance is smaller than the maximum distance.
//cout << "indeX:" << index << "distance:" <<distance << " maxVal:" << maxVal << endl;
if (distance >= maxVal) return;
else {
this->values[0] = distance;
this->indices[0] = index;
this->push_down(0);
}
}
//heap push down operation
void push_down(IT index_on_heap){
IT child_index1 = 2 * index_on_heap + 1;
IT child_index2 = 2 * index_on_heap + 2;
IT biggerIndex = -1;
if(child_index1 < this->heapSize && child_index2 < this->heapSize){
if (this->values[child_index1] < this->values[child_index2]){
biggerIndex = child_index2;
}
else {
biggerIndex = child_index1;
}
}
else if(child_index1 < this->heapSize){
biggerIndex = child_index1;
}
else if(child_index2 < this->heapSize){
biggerIndex = child_index2;
}
if (biggerIndex >= 0 && this->values[biggerIndex] > this->values[index_on_heap]){
WT tmpVal = this->values[biggerIndex];
this->values[biggerIndex] = this->values[index_on_heap];
this->values[index_on_heap] = tmpVal;
IT tmpIndex = this->indices[biggerIndex];
this->indices[biggerIndex] = this->indices[index_on_heap];
this->indices[index_on_heap] = tmpIndex;
this->push_down(biggerIndex);
}
}
void initValues(){
WT MAXVAL = std::numeric_limits<WT>::max();
for(IT i = 0; i < this->heapSize; ++i){
this->values[i] = MAXVAL;
this->indices[i] = -1;
}
}
//returns the total distance to center in the cluster.
WT getTotalDistance(){
WT nc = 0;
for(IT j = 0; j < this->heapSize; ++j){
nc += this->values[j];
//cout << "index:" << this->indices[j] << " distance:" << this->values[j] << endl;
}
return nc;
}
//returns the new center of the cluster.
bool getNewCenters(WT *center, WT **coords, int dimension){
bool moved = false;
for(int i = 0; i < dimension; ++i){
WT nc = 0;
for(IT j = 0; j < this->heapSize; ++j){
IT k = this->indices[j];
//cout << "i:" << i << " dim:" << dimension << " k:" << k << " heapSize:" << heapSize << endl;
nc += coords[i][k];
}
nc /= this->heapSize;
moved = (ZOLTAN2_ABS(center[i] - nc) > this->_EPSILON || moved );
center[i] = nc;
}
return moved;
}
void copyCoordinates(IT *permutation){
for(IT i = 0; i < this->heapSize; ++i){
permutation[i] = this->indices[i];
}
}
};
/*! \brief KMeansCluster Class
*/
template <class IT, class WT>
class KMeansCluster{
int dimension;
KmeansHeap<IT,WT> closestPoints;
public:
WT *center;
~KMeansCluster(){
freeArray<WT>(center);
}
void setParams(int dimension_, int heapsize){
this->dimension = dimension_;
this->center = allocMemory<WT>(dimension_);
this->closestPoints.setHeapsize(heapsize);
}
void clearHeap(){
this->closestPoints.initValues();
}
bool getNewCenters( WT **coords){
return this->closestPoints.getNewCenters(center, coords, dimension);
}
//returns the distance of the coordinate to the center.
//also adds it to the heap.
WT getDistance(IT index, WT **elementCoords){
WT distance = 0;
for (int i = 0; i < this->dimension; ++i){
WT d = (center[i] - elementCoords[i][index]);
distance += d * d;
}
distance = pow(distance, WT(1.0 / this->dimension));
closestPoints.addPoint(index, distance);
return distance;
}
WT getDistanceToCenter(){
return closestPoints.getTotalDistance();
}
void copyCoordinates(IT *permutation){
closestPoints.copyCoordinates(permutation);
}
};
/*! \brief KMeansAlgorithm Class that performs clustering of the coordinates, and returns the closest set of coordinates.
* Useful to filter the processors, when there are more processors than needed.
*/
template <class IT, class WT>
class KMeansAlgorithm{
int dim;
IT numElements;
WT **elementCoords;
IT numClusters;
IT required_elements;
KMeansCluster <IT,WT> *clusters;
WT *maxCoordinates;
WT *minCoordinates;
public:
~KMeansAlgorithm(){
freeArray<KMeansCluster <IT,WT> >(clusters);
freeArray<WT>(maxCoordinates);
freeArray<WT>(minCoordinates);
}
/*! \brief KMeansAlgorithm Constructor
*/
KMeansAlgorithm(
int dim_ ,
IT numElements_,
WT **elementCoords_,
IT required_elements_):
dim(dim_),
numElements(numElements_),
elementCoords(elementCoords_),
numClusters ((1 << dim_) + 1),
required_elements(required_elements_)
{
this->clusters = allocMemory<KMeansCluster <IT,WT> >(this->numClusters);
//set dimension and the number of required elements for all clusters.
for (int i = 0; i < numClusters; ++i){
this->clusters[i].setParams(this->dim, this->required_elements);
}
this->maxCoordinates = allocMemory <WT> (this->dim);
this->minCoordinates = allocMemory <WT> (this->dim);
//obtain the min and max coordiantes for each dimension.
for (int j = 0; j < dim; ++j){
this->minCoordinates[j] = this->maxCoordinates[j] = this->elementCoords[j][0];
for(IT i = 1; i < numElements; ++i){
WT t = this->elementCoords[j][i];
if(t > this->maxCoordinates[j]){
this->maxCoordinates[j] = t;
}
if (t < minCoordinates[j]){
this->minCoordinates[j] = t;
}
}
}
//assign initial cluster centers.
for (int j = 0; j < dim; ++j){
int mod = (1 << (j+1));
for (int i = 0; i < numClusters - 1; ++i){
WT c = 0;
if ( (i % mod) < mod / 2){
c = this->maxCoordinates[j];
//cout << "i:" << i << " j:" << j << " setting max:" << c << endl;
}
else {
c = this->minCoordinates[j];
}
this->clusters[i].center[j] = c;
}
}
//last cluster center is placed to middle.
for (int j = 0; j < dim; ++j){
this->clusters[numClusters - 1].center[j] = (this->maxCoordinates[j] + this->minCoordinates[j]) / 2;
}
/*
for (int i = 0; i < numClusters; ++i){
//cout << endl << "cluster:" << i << endl << "\t";
for (int j = 0; j < dim; ++j){
cout << this->clusters[i].center[j] << " ";
}
}
*/
}
//performs kmeans clustering of coordinates.
void kmeans(){
for(int it = 0; it < 10; ++it){
//cout << "it:" << it << endl;
for (IT j = 0; j < this->numClusters; ++j){
this->clusters[j].clearHeap();
}
for (IT i = 0; i < this->numElements; ++i){
//cout << "i:" << i << " numEl:" << this->numElements << endl;
for (IT j = 0; j < this->numClusters; ++j){
//cout << "j:" << j << " numClusters:" << this->numClusters << endl;
this->clusters[j].getDistance(i,this->elementCoords);
}
}
bool moved = false;
for (IT j = 0; j < this->numClusters; ++j){
moved =(this->clusters[j].getNewCenters(this->elementCoords) || moved );
}
if (!moved){
break;
}
}
}
//finds the cluster in which the coordinates are the closest to each other.
void getMinDistanceCluster(IT *procPermutation){
WT minDistance = this->clusters[0].getDistanceToCenter();
IT minCluster = 0;
//cout << "j:" << 0 << " minDistance:" << minDistance << " minTmpDistance:" << minDistance<< " minCluster:" << minCluster << endl;
for (IT j = 1; j < this->numClusters; ++j){
WT minTmpDistance = this->clusters[j].getDistanceToCenter();
//cout << "j:" << j << " minDistance:" << minDistance << " minTmpDistance:" << minTmpDistance<< " minCluster:" << minCluster << endl;
if(minTmpDistance < minDistance){
minDistance = minTmpDistance;
minCluster = j;
}
}
//cout << "minCluster:" << minCluster << endl;
this->clusters[minCluster].copyCoordinates(procPermutation);
}
};
#define MINOF(a,b) (((a)<(b))?(a):(b))
/*! \brief fillContinousArray function
*
* \param arr array to be filled in with values.
* \param arrSize the size of the array.
* \param val the pointer to the value to be filled. if given NULL, the filling performs arr[i] = i.
*/
template <typename T>
void fillContinousArray(T *arr, size_t arrSize, T *val){
if(val == NULL){
#ifdef HAVE_ZOLTAN2_OMP
#pragma omp parallel for
#endif
for(size_t i = 0; i < arrSize; ++i){
arr[i] = i;
}
}
else {
T v = *val;
#ifdef HAVE_ZOLTAN2_OMP
#pragma omp parallel for
#endif
for(size_t i = 0; i < arrSize; ++i){
//cout << "writing to i:" << i << " arr:" << arrSize << endl;
arr[i] = v;
}
}
}
/*! \brief CommunicationModel Base Class that performs mapping between the coordinate partitioning result.
*/
template <typename part_t, typename pcoord_t>
class CommunicationModel{
protected:
double commCost;
public:
part_t no_procs; //the number of processors
part_t no_tasks; //the number of taks.
CommunicationModel(): commCost(),no_procs(0), no_tasks(0){}
CommunicationModel(part_t no_procs_, part_t no_tasks_):
commCost(),
no_procs(no_procs_),
no_tasks(no_tasks_){}
virtual ~CommunicationModel(){}
part_t getNProcs() const{
return this->no_procs;
}
part_t getNTasks()const{
return this->no_tasks;
}
void calculateCommunicationCost(
part_t *task_to_proc,
part_t *task_communication_xadj,
part_t *task_communication_adj,
pcoord_t *task_communication_edge_weight){
double totalCost = 0;
part_t commCount = 0;
for (part_t task = 0; task < this->no_tasks; ++task){
int assigned_proc = task_to_proc[task];
//cout << "task:" << task << endl;
part_t task_adj_begin = task_communication_xadj[task];
part_t task_adj_end = task_communication_xadj[task+1];
commCount += task_adj_end - task_adj_begin;
//cout << "task:" << task << " proc:" << assigned_proc << endl;
for (part_t task2 = task_adj_begin; task2 < task_adj_end; ++task2){
//cout << "task2:" << task2 << endl;
part_t neighborTask = task_communication_adj[task2];
//cout << "neighborTask :" << neighborTask << endl;
int neighborProc = task_to_proc[neighborTask];
double distance = getProcDistance(assigned_proc, neighborProc);
if (task_communication_edge_weight == NULL){
totalCost += distance ;
}
else {
totalCost += distance * task_communication_edge_weight[task2];
/*
std::cout << "\ttask:" << task << " assigned_proc:" << assigned_proc <<
"task2:" << task << " neighborProc:" << neighborProc <<
" d:" << distance << " task_communication_edge_weight[task2]:" << task_communication_edge_weight[task2] <<
" wh:" << distance * task_communication_edge_weight[task2] <<
std::endl;
*/
}
}
}
this->commCost = totalCost;// commCount;
}
double getCommunicationCostMetric(){
return this->commCost;
}
virtual double getProcDistance(int procId1, int procId2) const = 0;
/*! \brief Function is called whenever nprocs > no_task.
* Function returns only the subset of processors that are closest to each other.
* \param proc_to_task_xadj holds the pointer to the task array
* \param proc_to_task_adj holds the indices of tasks wrt to proc_to_task_xadj array.
* \param task_to_proc holds the processors mapped to tasks.
*/
virtual void getMapping(
int myRank,
const RCP<const Environment> &env,
ArrayRCP <part_t> &proc_to_task_xadj, // = allocMemory<part_t> (this->no_procs+1); //holds the pointer to the task array
ArrayRCP <part_t> &proc_to_task_adj, // = allocMemory<part_t>(this->no_tasks); //holds the indices of tasks wrt to proc_to_task_xadj array.
ArrayRCP <part_t> &task_to_proc //allocMemory<part_t>(this->no_tasks); //holds the processors mapped to tasks.
) const = 0;
};
/*! \brief CoordinateModelInput Class that performs mapping between the coordinate partitioning result and mpi ranks
* base on the coordinate results and mpi physical coordinates.
*/
template <typename pcoord_t, typename tcoord_t, typename part_t>
class CoordinateCommunicationModel:public CommunicationModel<part_t, pcoord_t> {
public:
//private:
int proc_coord_dim; //dimension of the processors
pcoord_t **proc_coords; //the processor coordinates. allocated outside of the class.
int task_coord_dim; //dimension of the tasks coordinates.
tcoord_t **task_coords; //the task coordinates allocated outside of the class.
int partArraySize;
part_t *partNoArray;
int *machine_extent;
bool *machine_extent_wrap_around;
const MachineRepresentation<pcoord_t,part_t> *machine;
//public:
CoordinateCommunicationModel():
CommunicationModel<part_t, pcoord_t>(),
proc_coord_dim(0),
proc_coords(0),
task_coord_dim(0),
task_coords(0),
partArraySize(-1),
partNoArray(NULL),
machine_extent(NULL),
machine_extent_wrap_around(NULL),
machine(NULL){}
virtual ~CoordinateCommunicationModel(){}
/*! \brief Class Constructor:
* \param pcoord_dim_ the dimension of the processors
* \param pcoords_ the processor coordinates. allocated outside of the class.
* \param tcoord_dim_ dimension of the tasks coordinates.
* \param tcoords_ the task coordinates allocated outside of the class.
* \param no_procs_ the number of processors
* \param no_tasks_ the number of taks.
*/
CoordinateCommunicationModel(
int pcoord_dim_,
pcoord_t **pcoords_,
int tcoord_dim_,
tcoord_t **tcoords_,
part_t no_procs_,
part_t no_tasks_,
int *machine_extent_,
bool *machine_extent_wrap_around_,
const MachineRepresentation<pcoord_t,part_t> *machine_ = NULL
):
CommunicationModel<part_t, pcoord_t>(no_procs_, no_tasks_),
proc_coord_dim(pcoord_dim_), proc_coords(pcoords_),
task_coord_dim(tcoord_dim_), task_coords(tcoords_),
partArraySize(-1),
partNoArray(NULL),
machine_extent(machine_extent_),
machine_extent_wrap_around(machine_extent_wrap_around_),
machine(machine_){
}
void setPartArraySize(int psize){
this->partArraySize = psize;
}
void setPartArray(part_t *pNo){
this->partNoArray = pNo;
}
/*! \brief Function is called whenever nprocs > no_task.
* Function returns only the subset of processors that are closest to each other.
* \param proc_permutation holds the indices of the processors that are chosen.
* \param nprocs the number of processors.
* \param ntasks the number of taks.
*/
void getClosestSubset(part_t *proc_permutation, part_t nprocs, part_t ntasks) const{
//currently returns a random subset.
part_t minCoordDim = MINOF(this->task_coord_dim, this->proc_coord_dim);
KMeansAlgorithm<part_t, pcoord_t > kma(
minCoordDim, nprocs,
this->proc_coords, ntasks);
kma.kmeans();
kma.getMinDistanceCluster(proc_permutation);
for(int i = ntasks; i < nprocs; ++i){
proc_permutation[i] = -1;
}
/*
//fill array.
fillContinousArray<part_t>(proc_permutation, nprocs, NULL);
int _u_umpa_seed = 847449649;
srand (time(NULL));
int a = rand() % 1000 + 1;
_u_umpa_seed -= a;
//permute array randomly.
update_visit_order(proc_permutation, nprocs,_u_umpa_seed, 1);
*/
}
//temporary, necessary for random permutation.
static part_t umpa_uRandom(part_t l, int &_u_umpa_seed)
{
int a = 16807;
int m = 2147483647;
int q = 127773;
int r = 2836;
int lo, hi, test;
double d;
lo = _u_umpa_seed % q;
hi = _u_umpa_seed / q;
test = (a*lo)-(r*hi);
if (test>0)
_u_umpa_seed = test;
else
_u_umpa_seed = test + m;
d = (double) ((double) _u_umpa_seed / (double) m);
return (part_t) (d*(double)l);
}
virtual double getProcDistance(int procId1, int procId2) const{
pcoord_t distance = 0;
if (machine == NULL){
for (int i = 0 ; i < this->proc_coord_dim; ++i){
double d = ZOLTAN2_ABS(proc_coords[i][procId1] - proc_coords[i][procId2]);
if (machine_extent_wrap_around && machine_extent_wrap_around[i]){
if (machine_extent[i] - d < d){
d = machine_extent[i] - d;
}
}
distance += d;
}
}
else {
this->machine->getHopCount(procId1, procId2, distance);
}
return distance;
}
//temporary, does random permutation.
void update_visit_order(part_t* visitOrder, part_t n, int &_u_umpa_seed, part_t rndm) {
part_t *a = visitOrder;
if (rndm){
part_t i, u, v, tmp;
if (n <= 4)
return;
//srand ( time(NULL) );
//_u_umpa_seed = _u_umpa_seed1 - (rand()%100);
for (i=0; i<n; i+=16)
{
u = umpa_uRandom(n-4, _u_umpa_seed);
v = umpa_uRandom(n-4, _u_umpa_seed);
// FIXME (mfh 30 Sep 2015) This requires including Zoltan2_AlgMultiJagged.hpp.
ZOLTAN2_ALGMULTIJAGGED_SWAP(a[v], a[u], tmp);
ZOLTAN2_ALGMULTIJAGGED_SWAP(a[v+1], a[u+1], tmp);
ZOLTAN2_ALGMULTIJAGGED_SWAP(a[v+2], a[u+2], tmp);
ZOLTAN2_ALGMULTIJAGGED_SWAP(a[v+3], a[u+3], tmp);
}
}
else {
part_t i, end = n / 4;
for (i=1; i<end; i++)
{
part_t j=umpa_uRandom(n-i, _u_umpa_seed);
part_t t=a[j];
a[j] = a[n-i];
a[n-i] = t;
}
}
//PermuteInPlace(visitOrder, n);
}
/*! \brief Function is called whenever nprocs > no_task.
* Function returns only the subset of processors that are closest to each other.
* \param proc_to_task_xadj holds the pointer to the task array
* \param proc_to_task_xadj holds the indices of tasks wrt to proc_to_task_xadj array.
* \param task_to_proc holds the processors mapped to tasks.
*/
virtual void getMapping(
int myRank,
const RCP<const Environment> &env,
ArrayRCP <part_t> &rcp_proc_to_task_xadj, // = allocMemory<part_t> (this->no_procs+1); //holds the pointer to the task array
ArrayRCP <part_t> &rcp_proc_to_task_adj, // = allocMemory<part_t>(this->no_tasks); //holds the indices of tasks wrt to proc_to_task_xadj array.
ArrayRCP <part_t> &rcp_task_to_proc //allocMemory<part_t>(this->no_tasks); //holds the processors mapped to tasks.
) const{
rcp_proc_to_task_xadj = ArrayRCP <part_t> (this->no_procs+1);
rcp_proc_to_task_adj = ArrayRCP <part_t> (this->no_tasks);
rcp_task_to_proc = ArrayRCP <part_t> (this->no_tasks);
part_t *proc_to_task_xadj = rcp_proc_to_task_xadj.getRawPtr(); //holds the pointer to the task array
part_t *proc_to_task_adj = rcp_proc_to_task_adj.getRawPtr(); //holds the indices of tasks wrt to proc_to_task_xadj array.
part_t *task_to_proc = rcp_task_to_proc.getRawPtr(); //holds the processors mapped to tasks.);
part_t invalid = 0;
fillContinousArray<part_t> (proc_to_task_xadj, this->no_procs+1, &invalid);
//obtain the number of parts that should be divided.
part_t num_parts = MINOF(this->no_procs, this->no_tasks);
//obtain the min coordinate dim.
//No more want to do min coord dim. If machine dimension > task_dim,
//we end up with a long line.
//part_t minCoordDim = MINOF(this->task_coord_dim, this->proc_coord_dim);
int recursion_depth = partArraySize;
//if(partArraySize < minCoordDim) recursion_depth = minCoordDim;
if (partArraySize == -1)
recursion_depth = log(float(this->no_procs)) / log(2.0) + 1;
int taskPerm = z2Fact<int>(this->task_coord_dim); //get the number of different permutations for task dimension ordering
int procPerm = z2Fact<int>(this->proc_coord_dim); //get the number of different permutations for proc dimension ordering
int permutations = taskPerm * procPerm; //total number of permutations
//now add the ones, where we divide the processors with longest dimension,
//but task with order.
permutations += taskPerm;
//and divide tasks with longest dimension, and processors with order.
permutations += procPerm; //total number of permutations
//and both with longest dimension.
permutations += 1;
//add one also that partitions based the longest dimension.
//holds the pointers to proc_adjList
part_t *proc_xadj = allocMemory<part_t> (num_parts+1);
//holds the processors in parts according to the result of partitioning algorithm.
//the processors assigned to part x is at proc_adjList[ proc_xadj[x] : proc_xadj[x+1] ]
part_t *proc_adjList = allocMemory<part_t>(this->no_procs);
part_t used_num_procs = this->no_procs;
if(this->no_procs > this->no_tasks){
//obtain the subset of the processors that are closest to each other.
this->getClosestSubset(proc_adjList, this->no_procs, this->no_tasks);
used_num_procs = this->no_tasks;
}
else {
fillContinousArray<part_t>(proc_adjList,this->no_procs, NULL);
}
int myPermutation = myRank % permutations; //the index of the permutation
bool task_partition_along_longest_dim = false;
bool proc_partition_along_longest_dim = false;
int myProcPerm = 0;
int myTaskPerm = 0;
if (myPermutation == 0){
task_partition_along_longest_dim = true;
proc_partition_along_longest_dim = true;
}
else {
--myPermutation;
if (myPermutation < taskPerm){
proc_partition_along_longest_dim = true;
myTaskPerm = myPermutation; // the index of the task permutation
}
else{
myPermutation -= taskPerm;
if (myPermutation < procPerm){
task_partition_along_longest_dim = true;
myProcPerm = myPermutation; // the index of the task permutation
}
else {
myPermutation -= procPerm;
myProcPerm = myPermutation % procPerm; // the index of the proc permutation
myTaskPerm = myPermutation / procPerm; // the index of the task permutation
}
}
}
/*
if (task_partition_along_longest_dim && proc_partition_along_longest_dim){
std::cout <<"me:" << myRank << " task:longest proc:longest" << " numPerms:" << permutations << std::endl;
}
else if (proc_partition_along_longest_dim){
std::cout <<"me:" << myRank << " task:" << myTaskPerm << " proc:longest" << " numPerms:" << permutations << std::endl;
}
else if (task_partition_along_longest_dim){
std::cout <<"me:" << myRank << " task: longest" << " proc:" << myProcPerm << " numPerms:" << permutations << std::endl;
}
else {
std::cout <<"me:" << myRank << " task:" << myTaskPerm << " proc:" << myProcPerm << " numPerms:" << permutations << std::endl;
}
*/
int *permutation = allocMemory<int> ((this->proc_coord_dim > this->task_coord_dim)
? this->proc_coord_dim : this->task_coord_dim);
//get the permutation order from the proc permutation index.
ithPermutation<int>(this->proc_coord_dim, myProcPerm, permutation);
//reorder the coordinate dimensions.
pcoord_t **pcoords = allocMemory<pcoord_t *> (this->proc_coord_dim);
for(int i = 0; i < this->proc_coord_dim; ++i){
pcoords[i] = this->proc_coords[permutation[i]];
//cout << permutation[i] << " ";
}
//if (partNoArray == NULL) std::cout << "partNoArray is null" << std::endl;
//std::cout << "recursion_depth:" << recursion_depth << " partArraySize:" << partArraySize << std::endl;
//do the partitioning and renumber the parts.
env->timerStart(MACRO_TIMERS, "Mapping - Proc Partitioning");
AlgMJ<pcoord_t, part_t, part_t, part_t> mj_partitioner;
mj_partitioner.sequential_task_partitioning(
env,
this->no_procs,
used_num_procs,
num_parts,
this->proc_coord_dim,
//minCoordDim,
pcoords,//this->proc_coords,
proc_adjList,
proc_xadj,
recursion_depth,
partNoArray,
proc_partition_along_longest_dim
//,"proc_partitioning"
);
env->timerStop(MACRO_TIMERS, "Mapping - Proc Partitioning");
freeArray<pcoord_t *> (pcoords);
part_t *task_xadj = allocMemory<part_t> (num_parts+1);
part_t *task_adjList = allocMemory<part_t>(this->no_tasks);
//fill task_adjList st: task_adjList[i] <- i.
fillContinousArray<part_t>(task_adjList,this->no_tasks, NULL);
//get the permutation order from the task permutation index.
ithPermutation<int>(this->task_coord_dim, myTaskPerm, permutation);
//reorder task coordinate dimensions.
tcoord_t **tcoords = allocMemory<tcoord_t *> (this->task_coord_dim);
for(int i = 0; i < this->task_coord_dim; ++i){
tcoords[i] = this->task_coords[permutation[i]];
}
env->timerStart(MACRO_TIMERS, "Mapping - Task Partitioning");
//partitioning of tasks
mj_partitioner.sequential_task_partitioning(
env,
this->no_tasks,
this->no_tasks,
num_parts,
this->task_coord_dim,
//minCoordDim,
tcoords, //this->task_coords,
task_adjList,
task_xadj,
recursion_depth,
partNoArray,
task_partition_along_longest_dim
//,"task_partitioning"
);
env->timerStop(MACRO_TIMERS, "Mapping - Task Partitioning");
freeArray<pcoord_t *> (tcoords);
freeArray<int> (permutation);
//filling proc_to_task_xadj, proc_to_task_adj, task_to_proc arrays.
for(part_t i = 0; i < num_parts; ++i){
part_t proc_index_begin = proc_xadj[i];
part_t task_begin_index = task_xadj[i];
part_t proc_index_end = proc_xadj[i+1];
part_t task_end_index = task_xadj[i+1];
if(proc_index_end - proc_index_begin != 1){
std::cerr << "Error at partitioning of processors" << std::endl;
std::cerr << "PART:" << i << " is assigned to " << proc_index_end - proc_index_begin << " processors." << std::endl;
exit(1);
}
part_t assigned_proc = proc_adjList[proc_index_begin];
proc_to_task_xadj[assigned_proc] = task_end_index - task_begin_index;
}
//holds the pointer to the task array
//convert proc_to_task_xadj to CSR index array
part_t *proc_to_task_xadj_work = allocMemory<part_t> (this->no_procs);
part_t sum = 0;
for(part_t i = 0; i < this->no_procs; ++i){
part_t tmp = proc_to_task_xadj[i];
proc_to_task_xadj[i] = sum;
sum += tmp;
proc_to_task_xadj_work[i] = sum;
}
proc_to_task_xadj[this->no_procs] = sum;
for(part_t i = 0; i < num_parts; ++i){
part_t proc_index_begin = proc_xadj[i];
part_t task_begin_index = task_xadj[i];
part_t task_end_index = task_xadj[i+1];
part_t assigned_proc = proc_adjList[proc_index_begin];
for (part_t j = task_begin_index; j < task_end_index; ++j){
part_t taskId = task_adjList[j];
task_to_proc[taskId] = assigned_proc;
proc_to_task_adj [ --proc_to_task_xadj_work[assigned_proc] ] = taskId;
}
}
freeArray<part_t>(proc_to_task_xadj_work);
freeArray<part_t>(task_xadj);
freeArray<part_t>(task_adjList);
freeArray<part_t>(proc_xadj);
freeArray<part_t>(proc_adjList);
}
};
template <typename Adapter, typename part_t>
class CoordinateTaskMapper:public PartitionMapping<Adapter>{
protected:
#ifndef DOXYGEN_SHOULD_SKIP_THIS
typedef typename Adapter::scalar_t pcoord_t;
typedef typename Adapter::scalar_t tcoord_t;
typedef typename Adapter::scalar_t scalar_t;
#endif
//RCP<const Environment> env;
ArrayRCP<part_t> proc_to_task_xadj; // = allocMemory<part_t> (this->no_procs+1); //holds the pointer to the task array
ArrayRCP<part_t> proc_to_task_adj; // = allocMemory<part_t>(this->no_tasks); //holds the indices of tasks wrt to proc_to_task_xadj array.
ArrayRCP<part_t> task_to_proc; //allocMemory<part_t>(this->no_procs); //holds the processors mapped to tasks.
bool isOwnerofModel;
CoordinateCommunicationModel<pcoord_t,tcoord_t,part_t> *proc_task_comm;
part_t nprocs;
part_t ntasks;
ArrayRCP<part_t> task_communication_xadj;
ArrayRCP<part_t> task_communication_adj;
ArrayRCP<scalar_t> task_communication_edge_weight;
/*! \brief doMapping function, calls getMapping function of communicationModel object.
*/
void doMapping(int myRank){
if(this->proc_task_comm){
this->proc_task_comm->getMapping(
myRank,
this->env,
this->proc_to_task_xadj, // = allocMemory<part_t> (this->no_procs+1); //holds the pointer to the task array
this->proc_to_task_adj, // = allocMemory<part_t>(this->no_tasks); //holds the indices of tasks wrt to proc_to_task_xadj array.
this->task_to_proc //allocMemory<part_t>(this->no_procs); //holds the processors mapped to tasks.);
);
}
else {
std::cerr << "communicationModel is not specified in the Mapper" << std::endl;
exit(1);
}
}
/*! \brief creates and returns the subcommunicator for the processor group.
*/
RCP<Comm<int> > create_subCommunicator(){
int procDim = this->proc_task_comm->proc_coord_dim;
int taskDim = this->proc_task_comm->task_coord_dim;
int taskPerm = z2Fact<int>(procDim); //get the number of different permutations for task dimension ordering
int procPerm = z2Fact<int>(taskDim); //get the number of different permutations for proc dimension ordering
int idealGroupSize = taskPerm * procPerm; //total number of permutations
idealGroupSize += taskPerm + procPerm + 1; //for the one that does longest dimension partitioning.
int myRank = this->comm->getRank();
int commSize = this->comm->getSize();
int myGroupIndex = myRank / idealGroupSize;
int prevGroupBegin = (myGroupIndex - 1)* idealGroupSize;
if (prevGroupBegin < 0) prevGroupBegin = 0;
int myGroupBegin = myGroupIndex * idealGroupSize;
int myGroupEnd = (myGroupIndex + 1) * idealGroupSize;
int nextGroupEnd = (myGroupIndex + 2)* idealGroupSize;
if (myGroupEnd > commSize){
myGroupBegin = prevGroupBegin;
myGroupEnd = commSize;
}
if (nextGroupEnd > commSize){
myGroupEnd = commSize;
}
int myGroupSize = myGroupEnd - myGroupBegin;
part_t *myGroup = allocMemory<part_t>(myGroupSize);
for (int i = 0; i < myGroupSize; ++i){
myGroup[i] = myGroupBegin + i;
}
//cout << "me:" << myRank << " myGroupBegin:" << myGroupBegin << " myGroupEnd:" << myGroupEnd << endl;
ArrayView<const part_t> myGroupView(myGroup, myGroupSize);
RCP<Comm<int> > subComm = this->comm->createSubcommunicator(myGroupView);
freeArray<part_t>(myGroup);
return subComm;
}
/*! \brief finds the lowest cost mapping and broadcasts solution to everyone.
*/
void getBestMapping(){
//create the sub group.
RCP<Comm<int> > subComm = this->create_subCommunicator();
//calculate cost.
double myCost = this->proc_task_comm->getCommunicationCostMetric();
//std::cout << "me:" << this->comm->getRank() << " myCost:" << myCost << std::endl;
double localCost[2], globalCost[2];
localCost[0] = myCost;
localCost[1] = double(subComm->getRank());
globalCost[1] = globalCost[0] = std::numeric_limits<double>::max();
Teuchos::Zoltan2_ReduceBestMapping<int,double> reduceBest;
reduceAll<int, double>(*subComm, reduceBest,
2, localCost, globalCost);
int sender = int(globalCost[1]);
if ( this->comm->getRank() == 0){
std::cout << "me:" << localCost[1] <<
" localcost:" << localCost[0]<<
" bestcost:" << globalCost[0] <<
" Sender:" << sender <<
" procDim" << proc_task_comm->proc_coord_dim <<
" taskDim:" << proc_task_comm->task_coord_dim << std::endl;
}
//cout << "me:" << localCost[1] << " localcost:" << localCost[0]<< " bestcost:" << globalCost[0] << endl;
//cout << "me:" << localCost[1] << " proc:" << globalCost[1] << endl;
broadcast (*subComm, sender, this->ntasks, this->task_to_proc.getRawPtr());
broadcast (*subComm, sender, this->nprocs, this->proc_to_task_xadj.getRawPtr());
broadcast (*subComm, sender, this->ntasks, this->proc_to_task_adj.getRawPtr());
}
//write mapping to gnuPlot code to visualize.
void writeMapping(){
std::ofstream gnuPlotCode ("gnuPlot.plot", std::ofstream::out);
int mindim = MINOF(proc_task_comm->proc_coord_dim, proc_task_comm->task_coord_dim);
std::string ss = "";
for(part_t i = 0; i < this->nprocs; ++i){
std::string procFile = Teuchos::toString<int>(i) + "_mapping.txt";
if (i == 0){
gnuPlotCode << "plot \"" << procFile << "\"\n";
}
else {
gnuPlotCode << "replot \"" << procFile << "\"\n";
}
std::ofstream inpFile (procFile.c_str(), std::ofstream::out);
std::string gnuPlotArrow = "set arrow from ";
for(int j = 0; j < mindim; ++j){
if (j == mindim - 1){
inpFile << proc_task_comm->proc_coords[j][i];
gnuPlotArrow += Teuchos::toString<float>(proc_task_comm->proc_coords[j][i]);
}
else {
inpFile << proc_task_comm->proc_coords[j][i] << " ";
gnuPlotArrow += Teuchos::toString<float>(proc_task_comm->proc_coords[j][i]) +",";
}
}
gnuPlotArrow += " to ";
inpFile << std::endl;
ArrayView<part_t> a = this->getAssignedTasksForProc(i);
for(int k = 0; k < a.size(); ++k){
int j = a[k];
//cout << "i:" << i << " j:"
std::string gnuPlotArrow2 = gnuPlotArrow;
for(int z = 0; z < mindim; ++z){
if(z == mindim - 1){
//cout << "z:" << z << " j:" << j << " " << proc_task_comm->task_coords[z][j] << endl;
inpFile << proc_task_comm->task_coords[z][j];
gnuPlotArrow2 += Teuchos::toString<float>(proc_task_comm->task_coords[z][j]);
}
else{
inpFile << proc_task_comm->task_coords[z][j] << " ";
gnuPlotArrow2 += Teuchos::toString<float>(proc_task_comm->task_coords[z][j]) +",";
}
}
ss += gnuPlotArrow2 + "\n";
inpFile << std::endl;
}
inpFile.close();
}
gnuPlotCode << ss;
gnuPlotCode << "\nreplot\n pause -1 \n";
gnuPlotCode.close();
}
//write mapping to gnuPlot code to visualize.
void writeMapping2(int myRank){
std::string rankStr = Teuchos::toString<int>(myRank);
std::string gnuPlots = "gnuPlot", extentionS = ".plot";
std::string outF = gnuPlots + rankStr+ extentionS;
std::ofstream gnuPlotCode ( outF.c_str(), std::ofstream::out);
CoordinateCommunicationModel<pcoord_t, tcoord_t, part_t> *tmpproc_task_comm =
static_cast <CoordinateCommunicationModel<pcoord_t, tcoord_t, part_t> * > (proc_task_comm);
int mindim = MINOF(tmpproc_task_comm->proc_coord_dim, tmpproc_task_comm->task_coord_dim);
std::string ss = "";
std::string procs = "", parts = "";
for(part_t i = 0; i < this->nprocs; ++i){
//inpFile << std::endl;
ArrayView<part_t> a = this->getAssignedTasksForProc(i);
if (a.size() == 0){
continue;
}
//std::ofstream inpFile (procFile.c_str(), std::ofstream::out);
std::string gnuPlotArrow = "set arrow from ";
for(int j = 0; j < mindim; ++j){
if (j == mindim - 1){
//inpFile << proc_task_comm->proc_coords[j][i];
gnuPlotArrow += Teuchos::toString<float>(tmpproc_task_comm->proc_coords[j][i]);
procs += Teuchos::toString<float>(tmpproc_task_comm->proc_coords[j][i]);
}
else {
//inpFile << proc_task_comm->proc_coords[j][i] << " ";
gnuPlotArrow += Teuchos::toString<float>(tmpproc_task_comm->proc_coords[j][i]) +",";
procs += Teuchos::toString<float>(tmpproc_task_comm->proc_coords[j][i])+ " ";
}
}
procs += "\n";
gnuPlotArrow += " to ";
for(int k = 0; k < a.size(); ++k){
int j = a[k];
//cout << "i:" << i << " j:"
std::string gnuPlotArrow2 = gnuPlotArrow;
for(int z = 0; z < mindim; ++z){
if(z == mindim - 1){
//cout << "z:" << z << " j:" << j << " " << proc_task_comm->task_coords[z][j] << endl;
//inpFile << proc_task_comm->task_coords[z][j];
gnuPlotArrow2 += Teuchos::toString<float>(tmpproc_task_comm->task_coords[z][j]);
parts += Teuchos::toString<float>(tmpproc_task_comm->task_coords[z][j]);
}
else{
//inpFile << proc_task_comm->task_coords[z][j] << " ";
gnuPlotArrow2 += Teuchos::toString<float>(tmpproc_task_comm->task_coords[z][j]) +",";
parts += Teuchos::toString<float>(tmpproc_task_comm->task_coords[z][j]) + " ";
}
}
parts += "\n";
ss += gnuPlotArrow2 + " nohead\n";
//inpFile << std::endl;
}
//inpFile.close();
}
std::ofstream procFile ("procPlot.plot", std::ofstream::out);
procFile << procs << "\n";
procFile.close();
std::ofstream partFile ("partPlot.plot", std::ofstream::out);
partFile << parts<< "\n";
partFile.close();
std::ofstream extraProcFile ("allProc.plot", std::ofstream::out);
for(part_t j = 0; j < this->nprocs; ++j){
for(int i = 0; i < mindim; ++i){
extraProcFile << tmpproc_task_comm->proc_coords[i][j] << " ";
}
extraProcFile << std::endl;
}
extraProcFile.close();
gnuPlotCode << ss;
if(mindim == 2){
gnuPlotCode << "plot \"procPlot.plot\" with points pointsize 3\n";
} else {
gnuPlotCode << "splot \"procPlot.plot\" with points pointsize 3\n";
}
gnuPlotCode << "replot \"partPlot.plot\" with points pointsize 3\n";
gnuPlotCode << "replot \"allProc.plot\" with points pointsize 0.65\n";
gnuPlotCode << "\nreplot\n pause -1 \n";
gnuPlotCode.close();
}
// KDD Need to provide access to algorithm for getPartBoxes
#ifdef gnuPlot
void writeGnuPlot(
const Teuchos::Comm<int> *comm_,
const Zoltan2::PartitioningSolution<Adapter> *soln_,
int coordDim,
tcoord_t **partCenters
){
std::string file = "gggnuPlot";
std::string exten = ".plot";
std::ofstream mm("2d.txt");
file += Teuchos::toString<int>(comm_->getRank()) + exten;
std::ofstream ff(file.c_str());
//ff.seekg (0, ff.end);
std::vector <Zoltan2::coordinateModelPartBox <tcoord_t, part_t> > outPartBoxes = ((Zoltan2::PartitioningSolution<Adapter> *)soln_)->getPartBoxesView();
for (part_t i = 0; i < this->ntasks;++i){
outPartBoxes[i].writeGnuPlot(ff, mm);
}
if (coordDim == 2){
ff << "plot \"2d.txt\"" << std::endl;
//ff << "\n pause -1" << endl;
}
else {
ff << "splot \"2d.txt\"" << std::endl;
//ff << "\n pause -1" << endl;
}
mm.close();
ff << "set style arrow 5 nohead size screen 0.03,15,135 ls 1" << std::endl;
for (part_t i = 0; i < this->ntasks;++i){
part_t pb = task_communication_xadj[i];
part_t pe = task_communication_xadj[i+1];
for (part_t p = pb; p < pe; ++p){
part_t n = task_communication_adj[p];
//cout << "i:" << i << " n:" << n << endl;
std::string arrowline = "set arrow from ";
for (int j = 0; j < coordDim - 1; ++j){
arrowline += Teuchos::toString<tcoord_t>(partCenters[j][n]) + ",";
}
arrowline += Teuchos::toString<tcoord_t>(partCenters[coordDim -1][n]) + " to ";
for (int j = 0; j < coordDim - 1; ++j){
arrowline += Teuchos::toString<tcoord_t>(partCenters[j][i]) + ",";
}
arrowline += Teuchos::toString<tcoord_t>(partCenters[coordDim -1][i]) + " as 5\n";
//cout << "arrow:" << arrowline << endl;
ff << arrowline;
}
}
ff << "replot\n pause -1" << std::endl;
ff.close();
}
#endif // gnuPlot
public:
void getProcTask(part_t* &proc_to_task_xadj_, part_t* &proc_to_task_adj_){
proc_to_task_xadj_ = this->proc_to_task_xadj.getRawPtr();
proc_to_task_adj_ = this->proc_to_task_adj.getRawPtr();
}
void map(RCP<MappingSolution<Adapter> > &mappingsoln) {
// Mapping was already computed in the constructor; we need to store it
// in the solution.
mappingsoln->setMap_PartsForRank(this->proc_to_task_xadj,
this->proc_to_task_adj);
// KDDKDD TODO: Algorithm is also creating task_to_proc, which maybe
// KDDKDD is not needed once we use MappingSolution to answer queries
// KDDKDD instead of this algorithm.
// KDDKDD Ask Mehmet: what is the most efficient way to get the answer
// KDDKDD out of CoordinateTaskMapper and into the MappingSolution?
}
virtual ~CoordinateTaskMapper(){
//freeArray<part_t> (proc_to_task_xadj);
//freeArray<part_t> (proc_to_task_adj);
//freeArray<part_t> (task_to_proc);
if(this->isOwnerofModel){
delete this->proc_task_comm;
}
}
/*! \brief Constructor.
* When this constructor is called, in order to calculate the communication metric,
* the task adjacency graph is created based on the coordinate model input and partitioning of it.
* if the communication graph is already calculated, use the other constructors.
* \param comm_ is the communication object.
* \param machine_ is the machineRepresentation object. Stores the coordinates of machines.
* \param model_ is the input adapter.
* \param soln_ is the solution object. Holds the assignment of points.
* \param envConst_ is the environment object.
*/
CoordinateTaskMapper(
const Teuchos::RCP <const Teuchos::Comm<int> > comm_,
const Teuchos::RCP <const MachineRepresentation<pcoord_t,part_t> > machine_,
const Teuchos::RCP <const Adapter> input_adapter_,
const Teuchos::RCP <const Zoltan2::PartitioningSolution<Adapter> > soln_,
const Teuchos::RCP <const Environment> envConst):
PartitionMapping<Adapter> (comm_, machine_, input_adapter_, soln_, envConst),
proc_to_task_xadj(0),
proc_to_task_adj(0),
task_to_proc(0),
isOwnerofModel(true),
proc_task_comm(0),
task_communication_xadj(0),
task_communication_adj(0),
task_communication_edge_weight(0){
using namespace Teuchos;
typedef typename Adapter::base_adapter_t ctm_base_adapter_t;
RCP<Zoltan2::GraphModel<ctm_base_adapter_t> > graph_model_;
RCP<Zoltan2::CoordinateModel<ctm_base_adapter_t> > coordinateModel_ ;
RCP<const Teuchos::Comm<int> > rcp_comm = comm_;
RCP<const Environment> envConst_ = envConst;
RCP<const ctm_base_adapter_t> baseInputAdapter_ (
rcp(dynamic_cast<const ctm_base_adapter_t *>(input_adapter_.getRawPtr()), false));
modelFlag_t coordFlags_, graphFlags_;
//create coordinate model
//since this is coordinate task mapper,
//the adapter has to have the coordinates
coordinateModel_ = rcp(new CoordinateModel<ctm_base_adapter_t>(
baseInputAdapter_, envConst_, rcp_comm, coordFlags_));
//if the adapter has also graph model, we will use graph model
//to calculate the cost mapping.
BaseAdapterType inputType_ = input_adapter_->adapterType();
if (inputType_ == MatrixAdapterType ||
inputType_ == GraphAdapterType ||
inputType_ == MeshAdapterType)
{
graph_model_ = rcp(new GraphModel<ctm_base_adapter_t>(
baseInputAdapter_, envConst_, rcp_comm,
graphFlags_));
}
if (!machine_->hasMachineCoordinates()) {
throw std::runtime_error("Existing machine does not provide coordinates "
"for coordinate task mapping");
}
//if mapping type is 0 then it is coordinate mapping
int procDim = machine_->getMachineDim();
this->nprocs = machine_->getNumRanks();
//get processor coordinates.
pcoord_t **procCoordinates = NULL;
if (!machine_->getAllMachineCoordinatesView(procCoordinates)) {
throw std::runtime_error("Existing machine does not implement "
"getAllMachineCoordinatesView");
}
//get the machine extent.
//if we have machine extent,
//if the machine has wrap-around links, we would like to shift the coordinates,
//so that the largest hap would be the wrap-around.
std::vector <int> machine_extent_vec (procDim);
//std::vector <bool> machine_extent_wrap_around_vec(procDim, 0);
int *machine_extent = &(machine_extent_vec[0]);
bool *machine_extent_wrap_around = new bool[procDim];
machine_->getMachineExtentWrapArounds(machine_extent_wrap_around);
// KDDKDD ASK MEHMET: SHOULD WE GET AND USE machine_dimension HERE IF IT
// KDDKDD ASK MEHMET: IS PROVIDED BY THE MACHINE REPRESENTATION?
// KDDKDD ASK MEHMET: IF NOT HERE, THEN WHERE?
// MD: Yes, I ADDED BELOW:
if (machine_->getMachineExtent(machine_extent)) {
procCoordinates =
this->shiftMachineCoordinates (
procDim,
machine_extent,
machine_extent_wrap_around,
this->nprocs,
procCoordinates);
}
//get the tasks information, such as coordinate dimension,
//number of parts.
int coordDim = coordinateModel_->getCoordinateDim();
this->ntasks = soln_->getActualGlobalNumberOfParts();
if (part_t (soln_->getTargetGlobalNumberOfParts()) > this->ntasks){
this->ntasks = soln_->getTargetGlobalNumberOfParts();
}
this->solution_parts = soln_->getPartListView();
//we need to calculate the center of parts.
tcoord_t **partCenters = NULL;
partCenters = allocMemory<tcoord_t *>(coordDim);
for (int i = 0; i < coordDim; ++i){
partCenters[i] = allocMemory<tcoord_t>(this->ntasks);
}
typedef typename Adapter::scalar_t t_scalar_t;
envConst->timerStart(MACRO_TIMERS, "Mapping - Solution Center");
//get centers for the parts.
getSolutionCenterCoordinates<Adapter, t_scalar_t,part_t>(
envConst.getRawPtr(),
comm_.getRawPtr(),
coordinateModel_.getRawPtr(),
this->solution_parts,
//soln_->getPartListView();
//this->soln.getRawPtr(),
coordDim,
ntasks,
partCenters);
envConst->timerStop(MACRO_TIMERS, "Mapping - Solution Center");
//create the part graph
if (graph_model_.getRawPtr() != NULL){
getCoarsenedPartGraph<Adapter, t_scalar_t, part_t> (
envConst.getRawPtr(),
comm_.getRawPtr(),
graph_model_.getRawPtr(),
this->ntasks,
this->solution_parts,
//soln_->getPartListView(),
//this->soln.getRawPtr(),
task_communication_xadj,
task_communication_adj,
task_communication_edge_weight
);
}
//create coordinate communication model.
this->proc_task_comm =
new Zoltan2::CoordinateCommunicationModel<pcoord_t,tcoord_t,part_t>(
procDim,
procCoordinates,
coordDim,
partCenters,
this->nprocs,
this->ntasks,
machine_extent,
machine_extent_wrap_around,
machine_.getRawPtr());
int myRank = comm_->getRank();
envConst->timerStart(MACRO_TIMERS, "Mapping - Processor Task map");
this->doMapping(myRank);
envConst->timerStop(MACRO_TIMERS, "Mapping - Processor Task map");
envConst->timerStart(MACRO_TIMERS, "Mapping - Communication Graph");
/*soln_->getCommunicationGraph(task_communication_xadj,
task_communication_adj);
*/
envConst->timerStop(MACRO_TIMERS, "Mapping - Communication Graph");
#ifdef gnuPlot
if (comm_->getRank() == 0){
part_t taskCommCount = task_communication_xadj.size();
std::cout << " TotalComm:" << task_communication_xadj[taskCommCount] << std::endl;
part_t maxN = task_communication_xadj[0];
for (part_t i = 1; i <= taskCommCount; ++i){
part_t nc = task_communication_xadj[i] - task_communication_xadj[i-1];
if (maxN < nc) maxN = nc;
}
std::cout << " maxNeighbor:" << maxN << std::endl;
}
this->writeGnuPlot(comm_, soln_, coordDim, partCenters);
#endif
envConst->timerStart(MACRO_TIMERS, "Mapping - Communication Cost");
if (task_communication_xadj.getRawPtr() && task_communication_adj.getRawPtr()){
this->proc_task_comm->calculateCommunicationCost(
task_to_proc.getRawPtr(),
task_communication_xadj.getRawPtr(),
task_communication_adj.getRawPtr(),
task_communication_edge_weight.getRawPtr()
);
}
//std::cout << "me: " << comm_->getRank() << " cost:" << this->proc_task_comm->getCommunicationCostMetric() << std::endl;
envConst->timerStop(MACRO_TIMERS, "Mapping - Communication Cost");
//processors are divided into groups of size procDim! * coordDim!
//each processor in the group obtains a mapping with a different rotation
//and best one is broadcasted all processors.
this->getBestMapping();
/*
{
if (task_communication_xadj.getRawPtr() && task_communication_adj.getRawPtr())
this->proc_task_comm->calculateCommunicationCost(
task_to_proc.getRawPtr(),
task_communication_xadj.getRawPtr(),
task_communication_adj.getRawPtr(),
task_communication_edge_weight.getRawPtr()
);
std::cout << "me: " << comm_->getRank() << " cost:" << this->proc_task_comm->getCommunicationCostMetric() << std::endl;
}
*/
#ifdef gnuPlot
this->writeMapping2(comm_->getRank());
#endif
delete []machine_extent_wrap_around;
if (machine_->getMachineExtent(machine_extent)){
for (int i = 0; i < procDim; ++i){
delete [] procCoordinates[i];
}
delete [] procCoordinates;
}
for (int i = 0; i < coordDim; ++i){
freeArray<tcoord_t>(partCenters[i]);
}
freeArray<tcoord_t *>(partCenters);
}
/*! \brief Constructor. Instead of Solution we have two parameters, numparts
* When this constructor is called, in order to calculate the communication metric,
* the task adjacency graph is created based on the coordinate model input and partitioning of it.
* if the communication graph is already calculated, use the other constructors.
* \param comm_ is the communication object.
* \param machine_ is the machineRepresentation object. Stores the coordinates of machines.
* \param model_ is the input adapter.
* \param soln_ is the solution object. Holds the assignment of points.
* \param envConst_ is the environment object.
*/
CoordinateTaskMapper(
const Teuchos::RCP <const Teuchos::Comm<int> > comm_,
const Teuchos::RCP <const MachineRepresentation<pcoord_t,part_t> > machine_,
const Teuchos::RCP <const Adapter> input_adapter_,
const part_t num_parts_,
const part_t *result_parts,
const Teuchos::RCP <const Environment> envConst,
bool is_input_adapter_distributed = true):
PartitionMapping<Adapter> (comm_, machine_, input_adapter_, num_parts_, result_parts, envConst),
proc_to_task_xadj(0),
proc_to_task_adj(0),
task_to_proc(0),
isOwnerofModel(true),
proc_task_comm(0),
task_communication_xadj(0),
task_communication_adj(0),
task_communication_edge_weight(0){
using namespace Teuchos;
typedef typename Adapter::base_adapter_t ctm_base_adapter_t;
RCP<Zoltan2::GraphModel<ctm_base_adapter_t> > graph_model_;
RCP<Zoltan2::CoordinateModel<ctm_base_adapter_t> > coordinateModel_ ;
RCP<const Teuchos::Comm<int> > rcp_comm = comm_;
RCP<const Teuchos::Comm<int> > ia_comm = rcp_comm;
if (!is_input_adapter_distributed){
ia_comm = Teuchos::createSerialComm<int>();
}
RCP<const Environment> envConst_ = envConst;
RCP<const ctm_base_adapter_t> baseInputAdapter_ (
rcp(dynamic_cast<const ctm_base_adapter_t *>(input_adapter_.getRawPtr()), false));
modelFlag_t coordFlags_, graphFlags_;
//create coordinate model
//since this is coordinate task mapper,
//the adapter has to have the coordinates
coordinateModel_ = rcp(new CoordinateModel<ctm_base_adapter_t>(
baseInputAdapter_, envConst_, ia_comm, coordFlags_));
//if the adapter has also graph model, we will use graph model
//to calculate the cost mapping.
BaseAdapterType inputType_ = input_adapter_->adapterType();
if (inputType_ == MatrixAdapterType ||
inputType_ == GraphAdapterType ||
inputType_ == MeshAdapterType)
{
graph_model_ = rcp(new GraphModel<ctm_base_adapter_t>(
baseInputAdapter_, envConst_, ia_comm,
graphFlags_));
}
if (!machine_->hasMachineCoordinates()) {
throw std::runtime_error("Existing machine does not provide coordinates "
"for coordinate task mapping");
}
//if mapping type is 0 then it is coordinate mapping
int procDim = machine_->getMachineDim();
this->nprocs = machine_->getNumRanks();
//get processor coordinates.
pcoord_t **procCoordinates = NULL;
if (!machine_->getAllMachineCoordinatesView(procCoordinates)) {
throw std::runtime_error("Existing machine does not implement "
"getAllMachineCoordinatesView");
}
//get the machine extent.
//if we have machine extent,
//if the machine has wrap-around links, we would like to shift the coordinates,
//so that the largest hap would be the wrap-around.
std::vector <int> machine_extent_vec (procDim);
//std::vector <bool> machine_extent_wrap_around_vec(procDim, 0);
int *machine_extent = &(machine_extent_vec[0]);
bool *machine_extent_wrap_around = new bool[procDim];
machine_->getMachineExtentWrapArounds(machine_extent_wrap_around);
// KDDKDD ASK MEHMET: SHOULD WE GET AND USE machine_dimension HERE IF IT
// KDDKDD ASK MEHMET: IS PROVIDED BY THE MACHINE REPRESENTATION?
// KDDKDD ASK MEHMET: IF NOT HERE, THEN WHERE?
// MD: Yes, I ADDED BELOW:
if (machine_->getMachineExtent(machine_extent)) {
procCoordinates =
this->shiftMachineCoordinates (
procDim,
machine_extent,
machine_extent_wrap_around,
this->nprocs,
procCoordinates);
}
//get the tasks information, such as coordinate dimension,
//number of parts.
int coordDim = coordinateModel_->getCoordinateDim();
this->ntasks = num_parts_;
this->solution_parts = result_parts;
//we need to calculate the center of parts.
tcoord_t **partCenters = NULL;
partCenters = allocMemory<tcoord_t *>(coordDim);
for (int i = 0; i < coordDim; ++i){
partCenters[i] = allocMemory<tcoord_t>(this->ntasks);
}
typedef typename Adapter::scalar_t t_scalar_t;
envConst->timerStart(MACRO_TIMERS, "Mapping - Solution Center");
//get centers for the parts.
getSolutionCenterCoordinates<Adapter, t_scalar_t,part_t>(
envConst.getRawPtr(),
ia_comm.getRawPtr(),
coordinateModel_.getRawPtr(),
this->solution_parts,
//soln_->getPartListView();
//this->soln.getRawPtr(),
coordDim,
ntasks,
partCenters);
envConst->timerStop(MACRO_TIMERS, "Mapping - Solution Center");
envConst->timerStart(MACRO_TIMERS, "GRAPHCREATE");
//create the part graph
if (graph_model_.getRawPtr() != NULL){
getCoarsenedPartGraph<Adapter, t_scalar_t, part_t> (
envConst.getRawPtr(),
ia_comm.getRawPtr(),
graph_model_.getRawPtr(),
this->ntasks,
this->solution_parts,
//soln_->getPartListView(),
//this->soln.getRawPtr(),
task_communication_xadj,
task_communication_adj,
task_communication_edge_weight
);
}
envConst->timerStop(MACRO_TIMERS, "GRAPHCREATE");
envConst->timerStart(MACRO_TIMERS, "CoordinateCommunicationModel Create");
//create coordinate communication model.
this->proc_task_comm =
new Zoltan2::CoordinateCommunicationModel<pcoord_t,tcoord_t,part_t>(
procDim,
procCoordinates,
coordDim,
partCenters,
this->nprocs,
this->ntasks,
machine_extent,
machine_extent_wrap_around,
machine_.getRawPtr());
envConst->timerStop(MACRO_TIMERS, "CoordinateCommunicationModel Create");
int myRank = comm_->getRank();
envConst->timerStart(MACRO_TIMERS, "Mapping - Processor Task map");
this->doMapping(myRank);
envConst->timerStop(MACRO_TIMERS, "Mapping - Processor Task map");
envConst->timerStart(MACRO_TIMERS, "Mapping - Communication Graph");
/*soln_->getCommunicationGraph(task_communication_xadj,
task_communication_adj);
*/
envConst->timerStop(MACRO_TIMERS, "Mapping - Communication Graph");
#ifdef gnuPlot
if (comm_->getRank() == 0){
part_t taskCommCount = task_communication_xadj.size();
std::cout << " TotalComm:" << task_communication_xadj[taskCommCount] << std::endl;
part_t maxN = task_communication_xadj[0];
for (part_t i = 1; i <= taskCommCount; ++i){
part_t nc = task_communication_xadj[i] - task_communication_xadj[i-1];
if (maxN < nc) maxN = nc;
}
std::cout << " maxNeighbor:" << maxN << std::endl;
}
this->writeGnuPlot(comm_, soln_, coordDim, partCenters);
#endif
envConst->timerStart(MACRO_TIMERS, "Mapping - Communication Cost");
if (task_communication_xadj.getRawPtr() && task_communication_adj.getRawPtr()){
this->proc_task_comm->calculateCommunicationCost(
task_to_proc.getRawPtr(),
task_communication_xadj.getRawPtr(),
task_communication_adj.getRawPtr(),
task_communication_edge_weight.getRawPtr()
);
}
//std::cout << "me: " << comm_->getRank() << " cost:" << this->proc_task_comm->getCommunicationCostMetric() << std::endl;
envConst->timerStop(MACRO_TIMERS, "Mapping - Communication Cost");
//processors are divided into groups of size procDim! * coordDim!
//each processor in the group obtains a mapping with a different rotation
//and best one is broadcasted all processors.
this->getBestMapping();
/*
{
if (task_communication_xadj.getRawPtr() && task_communication_adj.getRawPtr())
this->proc_task_comm->calculateCommunicationCost(
task_to_proc.getRawPtr(),
task_communication_xadj.getRawPtr(),
task_communication_adj.getRawPtr(),
task_communication_edge_weight.getRawPtr()
);
std::cout << "me: " << comm_->getRank() << " cost:" << this->proc_task_comm->getCommunicationCostMetric() << std::endl;
}
*/
#ifdef gnuPlot
this->writeMapping2(comm_->getRank());
#endif
delete []machine_extent_wrap_around;
if (machine_->getMachineExtent(machine_extent)){
for (int i = 0; i < procDim; ++i){
delete [] procCoordinates[i];
}
delete [] procCoordinates;
}
for (int i = 0; i < coordDim; ++i){
freeArray<tcoord_t>(partCenters[i]);
}
freeArray<tcoord_t *>(partCenters);
}
/*! \brief Constructor
* The mapping constructor which will also perform the mapping operation.
* The result mapping can be obtained by
* --getAssignedProcForTask function: which returns the assigned processor id for the given task
* --getPartsForProc: which returns the assigned tasks with the number of tasks.
*
* -task_comm_xadj, task_comm_adj, task_communication_edge_weight_ can be provided NULL.
* In this case all processors will calculate the same mapping.
* -If task_comm_xadj, task_comm_adj and provided, algorithm will perform rotations,
* and processors will calculate different mappings, and best one will be reduced.
* -If task_communication_edge_weight_ is provided with task_comm_xadj, task_comm_adj
* this will be used when cost is calculated.
* -recursion_depth is a mandatory argument. In the case part_no_array is not null, this parameter
* should represent the length of part_no_array.
* If part_no_array is given as NULL, then this will give the recursion depth for the algorith,
* Maximum number is ceil(log_2(min(num_processors, num_tasks))), and providing a higher number will
* be equivalant to this. Partitioning algorithm will work as RCB when maximum number is given,
* which performs the best mapping results.
* -part_no_array: The best results are obtained when this parameter is given as NULL. But if this is
* provided, partitioning will use this array for partitioning each dimension to the given numbers.
* The multiplication of these numbers should be equal to min(num_processors, num_tasks).
* -machine_dimensions: This can be NULL, but if provided the algorithm will perform shift of the machine coords so that
* the largest gap is treated as wrap-around link.
*
* \param env_const_ the environment object.
* \param problemComm is the communication object.
* \param proc_dim dimensions of the processor coordinates.
* \param num_processors is the number of processors
* \param machine_coords is the coordinates of the processors.
*
* \param task_dim is the dimension of the tasks.
* \param num_tasks is the number of tasks.
* \param task_coords is the coordinates of the tasks.
* \param task_comm_xadj is the task communication graphs xadj array.
* (task i adjacency is between task_comm_xadj[i] and task_comm_xadj[i+1])
* \param task_comm_adj is task communication graphs adj array.
* \param task_communication_edge_weight_ is the weight of the communication in task graph.
* \param recursion_depth is the recursion depth that will be applied to partitioning.
* If part_no_array is provided, then it is the length of this array.
* \param part_no_array if part_no_array is provided, partitioning algorithm will be forced to use
* this array for partitioning. However, the multiplication of each entries in this array
* should be equal to min(num_processors, num_tasks).
* \param *machine_dimensions: the dimensions of the machine network. For example for hopper 17x8x24
* This can be NULL, but if provided the algorithm will perform shift of the machine coords so that
* the largest gap is treated as wrap-around link.
*/
CoordinateTaskMapper(
const Environment *env_const_,
const Teuchos::Comm<int> *problemComm,
int proc_dim,
int num_processors,
pcoord_t **machine_coords,
int task_dim,
part_t num_tasks,
tcoord_t **task_coords,
ArrayRCP<part_t>task_comm_xadj,
ArrayRCP<part_t>task_comm_adj,
pcoord_t *task_communication_edge_weight_,
int recursion_depth,
part_t *part_no_array,
const part_t *machine_dimensions
): PartitionMapping<Adapter>(
Teuchos::rcpFromRef<const Teuchos::Comm<int> >(*problemComm),
Teuchos::rcpFromRef<const Environment> (*env_const_)),
proc_to_task_xadj(0),
proc_to_task_adj(0),
task_to_proc(0),
isOwnerofModel(true),
proc_task_comm(0),
task_communication_xadj(task_comm_xadj),
task_communication_adj(task_comm_adj){
//if mapping type is 0 then it is coordinate mapping
pcoord_t ** virtual_machine_coordinates = machine_coords;
bool *wrap_arounds = new bool [proc_dim];
for (int i = 0; i < proc_dim; ++i) wrap_arounds[i] = true;
if (machine_dimensions){
virtual_machine_coordinates =
this->shiftMachineCoordinates (
proc_dim,
machine_dimensions,
wrap_arounds,
num_processors,
machine_coords);
}
this->nprocs = num_processors;
int coordDim = task_dim;
this->ntasks = num_tasks;
//alloc memory for part centers.
tcoord_t **partCenters = task_coords;
//create coordinate communication model.
this->proc_task_comm =
new Zoltan2::CoordinateCommunicationModel<pcoord_t,tcoord_t,part_t>(
proc_dim,
virtual_machine_coordinates,
coordDim,
partCenters,
this->nprocs,
this->ntasks, NULL, NULL
);
this->proc_task_comm->setPartArraySize(recursion_depth);
this->proc_task_comm->setPartArray(part_no_array);
int myRank = problemComm->getRank();
this->doMapping(myRank);
#ifdef gnuPlot
this->writeMapping2(myRank);
#endif
if (task_communication_xadj.getRawPtr() && task_communication_adj.getRawPtr()){
this->proc_task_comm->calculateCommunicationCost(
task_to_proc.getRawPtr(),
task_communication_xadj.getRawPtr(),
task_communication_adj.getRawPtr(),
task_communication_edge_weight_
);
this->getBestMapping();
/*
if (myRank == 0){
this->proc_task_comm->calculateCommunicationCost(
task_to_proc.getRawPtr(),
task_communication_xadj.getRawPtr(),
task_communication_adj.getRawPtr(),
task_communication_edge_weight_
);
cout << "me: " << problemComm->getRank() << " cost:" << this->proc_task_comm->getCommunicationCostMetric() << endl;
}
*/
}
delete [] wrap_arounds;
if (machine_dimensions){
for (int i = 0; i < proc_dim; ++i){
delete [] virtual_machine_coordinates[i];
}
delete [] virtual_machine_coordinates;
}
#ifdef gnuPlot
if(comm_->getRank() == 0)
this->writeMapping2(-1);
#endif
}
/*
double getCommunicationCostMetric(){
return this->proc_task_comm->getCommCost();
}
*/
/*! \brief Returns the number of parts to be assigned to this process.
*/
virtual size_t getLocalNumberOfParts() const{
return 0;
}
/*! \brief Using the machine dimensions provided, create virtual machine coordinates
* by assigning the largest gap to be as the wrap around link.
* \param machine_dim: the number of dimensions in the machine network.
* \param machine_dimensions: the dimension of the machien network. For example for hopper, 17,8,24
*
* \param numProcs: the number of allocated processors.
* \param mCoords: allocated machine coordinates.
*/
pcoord_t **shiftMachineCoordinates(
int machine_dim,
const part_t *machine_dimensions,
bool *machine_extent_wrap_around,
part_t numProcs,
pcoord_t **mCoords){
pcoord_t **result_machine_coords = NULL;
result_machine_coords = new pcoord_t*[machine_dim];
for (int i = 0; i < machine_dim; ++i){
result_machine_coords[i] = new pcoord_t [numProcs];
}
for (int i = 0; i < machine_dim; ++i){
part_t numMachinesAlongDim = machine_dimensions[i];
part_t *machineCounts= new part_t[numMachinesAlongDim];
memset(machineCounts, 0, sizeof(part_t) *numMachinesAlongDim);
int *filledCoordinates= new int[numMachinesAlongDim];
pcoord_t *coords = mCoords[i];
for(part_t j = 0; j < numProcs; ++j){
part_t mc = (part_t) coords[j];
++machineCounts[mc];
}
part_t filledCoordinateCount = 0;
for(part_t j = 0; j < numMachinesAlongDim; ++j){
if (machineCounts[j] > 0){
filledCoordinates[filledCoordinateCount++] = j;
}
}
part_t firstProcCoord = filledCoordinates[0];
part_t firstProcCount = machineCounts[firstProcCoord];
part_t lastProcCoord = filledCoordinates[filledCoordinateCount - 1];
part_t lastProcCount = machineCounts[lastProcCoord];
part_t firstLastGap = numMachinesAlongDim - lastProcCoord + firstProcCoord;
part_t firstLastGapProc = lastProcCount + firstProcCount;
part_t leftSideProcCoord = firstProcCoord;
part_t leftSideProcCount = firstProcCount;
part_t biggestGap = 0;
part_t biggestGapProc = numProcs;
part_t shiftBorderCoordinate = -1;
for(part_t j = 1; j < filledCoordinateCount; ++j){
part_t rightSideProcCoord= filledCoordinates[j];
part_t rightSideProcCount = machineCounts[rightSideProcCoord];
part_t gap = rightSideProcCoord - leftSideProcCoord;
part_t gapProc = rightSideProcCount + leftSideProcCount;
/* Pick the largest gap in this dimension. Use fewer process on either side
of the largest gap to break the tie. An easy addition to this would
be to weight the gap by the number of processes. */
if (gap > biggestGap || (gap == biggestGap && biggestGapProc > gapProc)){
shiftBorderCoordinate = rightSideProcCoord;
biggestGapProc = gapProc;
biggestGap = gap;
}
leftSideProcCoord = rightSideProcCoord;
leftSideProcCount = rightSideProcCount;
}
if (!(biggestGap > firstLastGap || (biggestGap == firstLastGap && biggestGapProc < firstLastGapProc))){
shiftBorderCoordinate = -1;
}
for(part_t j = 0; j < numProcs; ++j){
if (machine_extent_wrap_around[i] && coords[j] < shiftBorderCoordinate){
result_machine_coords[i][j] = coords[j] + numMachinesAlongDim;
}
else {
result_machine_coords[i][j] = coords[j];
}
//cout << "I:" << i << "j:" << j << " coord:" << coords[j] << " now:" << result_machine_coords[i][j] << endl;
}
delete [] machineCounts;
delete [] filledCoordinates;
}
return result_machine_coords;
}
/*! \brief getAssignedProcForTask function,
* returns the assigned tasks with the number of tasks.
* \param procId procId being queried.
* \param numProcs (output), the number of processor the part is assigned to.
* \param procs (output), the list of processors assigned to given part..
*/
virtual void getProcsForPart(part_t taskId, part_t &numProcs, part_t *&procs) const{
numProcs = 1;
procs = this->task_to_proc.getRawPtr() + taskId;
}
/*! \brief getAssignedProcForTask function, returns the assigned processor id for the given task
* \param taskId taskId being queried.
*/
inline part_t getAssignedProcForTask(part_t taskId){
return this->task_to_proc[taskId];
}
/*! \brief getAssignedProcForTask function,
* returns the assigned tasks with the number of tasks.
* \param procId procId being queried.
* \param numParts (output), the number of parts the processor is assigned to.
* \param parts (output), the list of parts assigned to given processor..
*/
virtual void getPartsForProc(int procId, part_t &numParts, part_t *&parts) const{
part_t task_begin = this->proc_to_task_xadj[procId];
part_t taskend = this->proc_to_task_xadj[procId+1];
parts = this->proc_to_task_adj.getRawPtr() + task_begin;
numParts = taskend - task_begin;
}
ArrayView<part_t> getAssignedTasksForProc(part_t procId){
part_t task_begin = this->proc_to_task_xadj[procId];
part_t taskend = this->proc_to_task_xadj[procId+1];
/*
cout << "part_t:" << procId << " taskCount:" << taskend - task_begin << endl;
for(part_t i = task_begin; i < taskend; ++i){
cout << "part_t:" << procId << " task:" << proc_to_task_adj[i] << endl;
}
*/
if (taskend - task_begin > 0){
ArrayView <part_t> assignedParts(this->proc_to_task_adj.getRawPtr() + task_begin, taskend - task_begin);
return assignedParts;
}
else {
ArrayView <part_t> assignedParts;
return assignedParts;
}
}
};
/*! \brief Constructor
* The interface function that calls CoordinateTaskMapper which will also perform the mapping operation.
* The result mapping can be obtained by
* -proc_to_task_xadj: which holds the beginning and end indices of
* tasks on proc_to_task_adj that is assigned to a processor.
* the tasks assigned to processor i are between proc_to_task_xadj[i] and
* proc_to_task_xadj[i+1] on proc_to_task_adj.
*
* -proc_to_task_adj: holds the task adj array.
*
* -task_comm_xadj, task_comm_adj, task_communication_edge_weight_
* can be provided NULL.
* In this case all processors will calculate the same mapping.
* -If task_comm_xadj, task_comm_adj and provided, algorithm will perform
* rotations, and processors will calculate different mappings, and
* best one will be reduced.
* -If task_communication_edge_weight_ is provided with
* task_comm_xadj, task_comm_adj, this will be used when cost is calculated.
* -recursion_depth is a mandatory argument. In the case part_no_array
* is not null, this parameter
* should represent the length of part_no_array.
* If part_no_array is given as NULL, then this will give the
* recursion depth for the algorithm,
* Maximum number is ceil(log_2(min(num_processors, num_tasks))),
* and providing a higher number will
* be equivalant to this. Partitioning algorithm will work as RCB
* when maximum number is given, which performs the best mapping results.
* -part_no_array: The best results are obtained when this parameter
* is given as NULL. But if this is provided, partitioning will use this
* array for partitioning each dimension to the given numbers.
* The multiplication of these numbers should be equal to
* min(num_processors, num_tasks).
* -machine_dimensions: This can be NULL, but if provided the algorithm
* will perform shift of the machine coords so that
* the largest gap is treated as wrap-around link.
*
* \param problemComm is the communication object.
* \param proc_dim dimensions of the processor coordinates.
* \param num_processors is the number of processors
* \param machine_coords is the coordinates of the processors.
*
* \param task_dim is the dimension of the tasks.
* \param num_tasks is the number of tasks.
* \param task_coords is the coordinates of the tasks.
* \param task_comm_xadj is the task communication graphs xadj array.
* (task i's adjacency is between task_comm_xadj[i] and task_comm_xadj[i+1])
* \param task_comm_adj is task communication graphs adj array.
* \param task_communication_edge_weight_ is the weight of the communication
* in task graph.
* \param proc_to_task_xadj is is the output for tasks showing which proc
* has the which parts.
* (proc-i will own the tasks from proc_to_task_xadj[i] to
* proc_to_task_xadj[i+1])
* \param proc_to_task_adj is the ouput list of tasks pointed by
* proc_to_task_xadj
* \param recursion_depth is the recursion depth that will be applied to
* partitioning.
* If part_no_array is provided, then it is the length of this array.
* \param part_no_array if part_no_array is provided, partitioning algorithm
* will be forced to use * this array for partitioning. However,
* the multiplication of each entries in this array
* should be equal to min(num_processors, num_tasks).
* \param *machine_dimensions: the dimensions of the machine network. For
* example for hopper 17x8x24
* This can be NULL, but if provided the algorithm will perform
* shift of the machine coords so that
* the largest gap is treated as wrap-around link.
*/
template <typename part_t, typename pcoord_t, typename tcoord_t>
void coordinateTaskMapperInterface(
RCP<const Teuchos::Comm<int> > problemComm,
int proc_dim,
int num_processors,
pcoord_t **machine_coords,
int task_dim,
part_t num_tasks,
tcoord_t **task_coords,
part_t *task_comm_xadj,
part_t *task_comm_adj,
pcoord_t *task_communication_edge_weight_, /*float-like, same size with task_communication_adj_ weight of the corresponding edge.*/
part_t *proc_to_task_xadj, /*output*/
part_t *proc_to_task_adj, /*output*/
int recursion_depth,
part_t *part_no_array,
const part_t *machine_dimensions
)
{
const Environment *envConst_ = new Environment();
// mfh 03 Mar 2015: It's OK to omit the Node template
// parameter in Tpetra, if you're just going to use the
// default Node.
typedef Tpetra::MultiVector<tcoord_t, part_t, part_t> tMVector_t;
Teuchos::ArrayRCP<part_t> task_communication_xadj (task_comm_xadj, 0, num_tasks+1, false);
Teuchos::ArrayRCP<part_t> task_communication_adj;
if (task_comm_xadj){
Teuchos::ArrayRCP<part_t> tmp_task_communication_adj (task_comm_adj, 0, task_comm_xadj[num_tasks], false);
task_communication_adj = tmp_task_communication_adj;
}
CoordinateTaskMapper<XpetraMultiVectorAdapter <tMVector_t>, part_t> *ctm =
new CoordinateTaskMapper<XpetraMultiVectorAdapter <tMVector_t>, part_t>(
envConst_,
problemComm.getRawPtr(),
proc_dim,
num_processors,
machine_coords,//machine_coords_,
task_dim,
num_tasks,
task_coords,
task_communication_xadj,
task_communication_adj,
task_communication_edge_weight_,
recursion_depth,
part_no_array,
machine_dimensions
);
part_t* proc_to_task_xadj_;
part_t* proc_to_task_adj_;
ctm->getProcTask(proc_to_task_xadj_, proc_to_task_adj_);
for (part_t i = 0; i <= num_processors; ++i){
proc_to_task_xadj[i] = proc_to_task_xadj_[i];
}
for (part_t i = 0; i < num_tasks; ++i){
proc_to_task_adj[i] = proc_to_task_adj_[i];
}
delete ctm;
delete envConst_;
}
}// namespace Zoltan2
#endif
|