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 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117
|
/****************************************************************/
/* Parallel Combinatorial BLAS Library (for Graph Computations) */
/* version 1.6 -------------------------------------------------*/
/* date: 6/15/2017 ---------------------------------------------*/
/* authors: Ariful Azad, Aydin Buluc --------------------------*/
/****************************************************************/
/*
Copyright (c) 2010-2017, The Regents of the University of California
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include "SpParMat.h"
#include "ParFriends.h"
#include "Operations.h"
#include "FileHeader.h"
extern "C" {
#include "mmio.h"
}
#include <sys/types.h>
#include <sys/stat.h>
#include <mpi.h>
#include <fstream>
#include <algorithm>
#include <set>
#include <stdexcept>
namespace combblas {
/**
* If every processor has a distinct triples file such as {A_0, A_1, A_2,... A_p} for p processors
**/
template <class IT, class NT, class DER>
SpParMat< IT,NT,DER >::SpParMat (std::ifstream & input, MPI_Comm & world)
{
assert( (sizeof(IT) >= sizeof(typename DER::LocalIT)) );
if(!input.is_open())
{
perror("Input file doesn't exist\n");
exit(-1);
}
commGrid.reset(new CommGrid(world, 0, 0));
input >> (*spSeq);
}
template <class IT, class NT, class DER>
SpParMat< IT,NT,DER >::SpParMat (DER * myseq, MPI_Comm & world): spSeq(myseq)
{
assert( (sizeof(IT) >= sizeof(typename DER::LocalIT)) );
commGrid.reset(new CommGrid(world, 0, 0));
}
template <class IT, class NT, class DER>
SpParMat< IT,NT,DER >::SpParMat (DER * myseq, std::shared_ptr<CommGrid> grid): spSeq(myseq)
{
assert( (sizeof(IT) >= sizeof(typename DER::LocalIT)) );
commGrid = grid;
}
template <class IT, class NT, class DER>
SpParMat< IT,NT,DER >::SpParMat (std::shared_ptr<CommGrid> grid)
{
assert( (sizeof(IT) >= sizeof(typename DER::LocalIT)) );
spSeq = new DER();
commGrid = grid;
}
//! Deprecated. Don't call the default constructor
template <class IT, class NT, class DER>
SpParMat< IT,NT,DER >::SpParMat ()
{
SpParHelper::Print("COMBBLAS Warning: It is dangerous to create (matrix) objects without specifying the communicator, are you sure you want to create this object in MPI_COMM_WORLD?\n");
assert( (sizeof(IT) >= sizeof(typename DER::LocalIT)) );
spSeq = new DER();
commGrid.reset(new CommGrid(MPI_COMM_WORLD, 0, 0));
}
/**
* If there is a single file read by the master process only, use this and then call ReadDistribute()
**/
template <class IT, class NT, class DER>
SpParMat< IT,NT,DER >::SpParMat (MPI_Comm world)
{
assert( (sizeof(IT) >= sizeof(typename DER::LocalIT)) );
spSeq = new DER();
commGrid.reset(new CommGrid(world, 0, 0));
}
template <class IT, class NT, class DER>
SpParMat< IT,NT,DER >::~SpParMat ()
{
if(spSeq != NULL) delete spSeq;
}
template <class IT, class NT, class DER>
void SpParMat< IT,NT,DER >::FreeMemory ()
{
if(spSeq != NULL) delete spSeq;
spSeq = NULL;
}
/**
* Private function to guide Select2 communication and avoid code duplication due to loop ends
* @param[int, out] klimits {per column k limit gets updated for the next iteration}
* @param[out] converged {items to remove from actcolsmap at next iteration{
**/
template <class IT, class NT, class DER>
template <typename VT, typename GIT> // GIT: global index type of vector
void SpParMat<IT,NT,DER>::TopKGather(std::vector<NT> & all_medians, std::vector<IT> & nnz_per_col, int & thischunk, int & chunksize,
const std::vector<NT> & activemedians, const std::vector<IT> & activennzperc, int itersuntil,
std::vector< std::vector<NT> > & localmat, const std::vector<IT> & actcolsmap, std::vector<IT> & klimits,
std::vector<IT> & toretain, std::vector<std::vector<std::pair<IT,NT>>> & tmppair, IT coffset, const FullyDistVec<GIT,VT> & rvec) const
{
int rankincol = commGrid->GetRankInProcCol();
int colneighs = commGrid->GetGridRows();
int nprocs = commGrid->GetSize();
std::vector<double> finalWeightedMedians(thischunk, 0.0);
MPI_Gather(activemedians.data() + itersuntil*chunksize, thischunk, MPIType<NT>(), all_medians.data(), thischunk, MPIType<NT>(), 0, commGrid->GetColWorld());
MPI_Gather(activennzperc.data() + itersuntil*chunksize, thischunk, MPIType<IT>(), nnz_per_col.data(), thischunk, MPIType<IT>(), 0, commGrid->GetColWorld());
if(rankincol == 0)
{
std::vector<double> columnCounts(thischunk, 0.0);
std::vector< std::pair<NT, double> > mediansNweights(colneighs); // (median,weight) pairs [to be reused at each iteration]
for(int j = 0; j < thischunk; ++j) // for each column
{
for(int k = 0; k<colneighs; ++k)
{
size_t fetchindex = k*thischunk+j;
columnCounts[j] += static_cast<double>(nnz_per_col[fetchindex]);
}
for(int k = 0; k<colneighs; ++k)
{
size_t fetchindex = k*thischunk+j;
mediansNweights[k] = std::make_pair(all_medians[fetchindex], static_cast<double>(nnz_per_col[fetchindex]) / columnCounts[j]);
}
sort(mediansNweights.begin(), mediansNweights.end()); // sort by median
double sumofweights = 0;
int k = 0;
while( k<colneighs && sumofweights < 0.5)
{
sumofweights += mediansNweights[k++].second;
}
finalWeightedMedians[j] = mediansNweights[k-1].first;
}
}
MPI_Bcast(finalWeightedMedians.data(), thischunk, MPIType<double>(), 0, commGrid->GetColWorld());
std::vector<IT> larger(thischunk, 0);
std::vector<IT> smaller(thischunk, 0);
std::vector<IT> equal(thischunk, 0);
#ifdef THREADED
#pragma omp parallel for
#endif
for(int j = 0; j < thischunk; ++j) // for each active column
{
size_t fetchindex = actcolsmap[j+itersuntil*chunksize];
for(size_t k = 0; k < localmat[fetchindex].size(); ++k)
{
// count those above/below/equal to the median
if(localmat[fetchindex][k] > finalWeightedMedians[j])
larger[j]++;
else if(localmat[fetchindex][k] < finalWeightedMedians[j])
smaller[j]++;
else
equal[j]++;
}
}
MPI_Allreduce(MPI_IN_PLACE, larger.data(), thischunk, MPIType<IT>(), MPI_SUM, commGrid->GetColWorld());
MPI_Allreduce(MPI_IN_PLACE, smaller.data(), thischunk, MPIType<IT>(), MPI_SUM, commGrid->GetColWorld());
MPI_Allreduce(MPI_IN_PLACE, equal.data(), thischunk, MPIType<IT>(), MPI_SUM, commGrid->GetColWorld());
int numThreads = 1; // default case
#ifdef THREADED
omp_lock_t lock[nprocs]; // a lock per recipient
for (int i=0; i<nprocs; i++)
omp_init_lock(&(lock[i]));
#pragma omp parallel
{
numThreads = omp_get_num_threads();
}
#endif
std::vector < std::vector<IT> > perthread2retain(numThreads);
#ifdef THREADED
#pragma omp parallel for
#endif
for(int j = 0; j < thischunk; ++j) // for each active column
{
#ifdef THREADED
int myThread = omp_get_thread_num();
#else
int myThread = 0;
#endif
// both clmapindex and fetchindex are unique for a given j (hence not shared among threads)
size_t clmapindex = j+itersuntil*chunksize; // klimits is of the same length as actcolsmap
size_t fetchindex = actcolsmap[clmapindex]; // localmat can only be dereferenced using the original indices.
// these following if/else checks are the same (because klimits/large/equal vectors are mirrored) on every processor along ColWorld
if(klimits[clmapindex] <= larger[j]) // the entries larger than Weighted-Median are plentiful, we can discard all the smaller/equal guys
{
std::vector<NT> survivors;
for(size_t k = 0; k < localmat[fetchindex].size(); ++k)
{
if(localmat[fetchindex][k] > finalWeightedMedians[j]) // keep only the large guys (even equal guys go)
survivors.push_back(localmat[fetchindex][k]);
}
localmat[fetchindex].swap(survivors);
perthread2retain[myThread].push_back(clmapindex); // items to retain in actcolsmap
}
else if (klimits[clmapindex] > larger[j] + equal[j]) // the elements that are either larger or equal-to are surely keepers, no need to reprocess them
{
std::vector<NT> survivors;
for(size_t k = 0; k < localmat[fetchindex].size(); ++k)
{
if(localmat[fetchindex][k] < finalWeightedMedians[j]) // keep only the small guys (even equal guys go)
survivors.push_back(localmat[fetchindex][k]);
}
localmat[fetchindex].swap(survivors);
klimits[clmapindex] -= (larger[j] + equal[j]); // update the k limit for this column only
perthread2retain[myThread].push_back(clmapindex); // items to retain in actcolsmap
}
else // larger[j] < klimits[clmapindex] && klimits[clmapindex] <= larger[j] + equal[j]
{ // we will always have equal[j] > 0 because the weighted median is part of the dataset so it has to be equal to itself.
std::vector<NT> survivors;
for(size_t k = 0; k < localmat[fetchindex].size(); ++k)
{
if(localmat[fetchindex][k] >= finalWeightedMedians[j]) // keep the larger and equal to guys (might exceed k-limit but that's fine according to MCL)
survivors.push_back(localmat[fetchindex][k]);
}
localmat[fetchindex].swap(survivors);
// We found it: the kth largest element in column (coffset + fetchindex) is finalWeightedMedians[j]
// But everyone in the same processor column has the information, only one of them should send it
IT n_perproc = getlocalcols() / colneighs; // find a typical processor's share
int assigned = std::max(static_cast<int>(fetchindex/n_perproc), colneighs-1);
if( assigned == rankincol)
{
IT locid;
int owner = rvec.Owner(coffset + fetchindex, locid);
#ifdef THREADED
omp_set_lock(&(lock[owner]));
#endif
tmppair[owner].emplace_back(std::make_pair(locid, finalWeightedMedians[j]));
#ifdef THREADED
omp_unset_lock(&(lock[owner]));
#endif
}
} // end_else
} // end_for
// ------ concatenate toretain "indices" processed by threads ------
std::vector<IT> tdisp(numThreads+1);
tdisp[0] = 0;
for(int i=0; i<numThreads; ++i)
{
tdisp[i+1] = tdisp[i] + perthread2retain[i].size();
}
toretain.resize(tdisp[numThreads]);
#pragma omp parallel for
for(int i=0; i< numThreads; i++)
{
std::copy(perthread2retain[i].data() , perthread2retain[i].data()+ perthread2retain[i].size(), toretain.data() + tdisp[i]);
}
#ifdef THREADED
for (int i=0; i<nprocs; i++) // destroy the locks
omp_destroy_lock(&(lock[i]));
#endif
}
//! identify the k-th maximum element in each column of a matrix
//! if the number of nonzeros in a column is less than k, return the numeric_limits<NT>::min()
//! This is an efficient implementation of the Saukas/Song algorithm
//! http://www.ime.usp.br/~einar/select/INDEX.HTM
//! Preferred for large k values
template <class IT, class NT, class DER>
template <typename VT, typename GIT> // GIT: global index type of vector
bool SpParMat<IT,NT,DER>::Kselect2(FullyDistVec<GIT,VT> & rvec, IT k_limit) const
{
if(*rvec.commGrid != *commGrid)
{
SpParHelper::Print("Grids are not comparable, SpParMat::Kselect() fails!", commGrid->GetWorld());
MPI_Abort(MPI_COMM_WORLD,GRIDMISMATCH);
}
int rankincol = commGrid->GetRankInProcCol();
int rankinrow = commGrid->GetRankInProcRow();
int rowneighs = commGrid->GetGridCols(); // get # of processors on the row
int colneighs = commGrid->GetGridRows();
int myrank = commGrid->GetRank();
int nprocs = commGrid->GetSize();
FullyDistVec<GIT,IT> colcnt(commGrid);
Reduce(colcnt, Column, std::plus<IT>(), (IT) 0, [](NT i){ return (IT) 1;});
// <begin> Gather vector along columns (Logic copied from DimApply)
int xsize = (int) colcnt.LocArrSize();
int trxsize = 0;
int diagneigh = colcnt.commGrid->GetComplementRank();
MPI_Status status;
MPI_Sendrecv(&xsize, 1, MPI_INT, diagneigh, TRX, &trxsize, 1, MPI_INT, diagneigh, TRX, commGrid->GetWorld(), &status);
IT * trxnums = new IT[trxsize];
MPI_Sendrecv(const_cast<IT*>(SpHelper::p2a(colcnt.arr)), xsize, MPIType<IT>(), diagneigh, TRX, trxnums, trxsize, MPIType<IT>(), diagneigh, TRX, commGrid->GetWorld(), &status);
int * colsize = new int[colneighs];
colsize[rankincol] = trxsize;
MPI_Allgather(MPI_IN_PLACE, 1, MPI_INT, colsize, 1, MPI_INT, commGrid->GetColWorld());
int * dpls = new int[colneighs](); // displacements (zero initialized pid)
std::partial_sum(colsize, colsize+colneighs-1, dpls+1);
int accsize = std::accumulate(colsize, colsize+colneighs, 0);
std::vector<IT> percsum(accsize); // per column sum of the number of entries
MPI_Allgatherv(trxnums, trxsize, MPIType<IT>(), percsum.data(), colsize, dpls, MPIType<VT>(), commGrid->GetColWorld());
DeleteAll(trxnums,colsize, dpls);
// <end> Gather vector along columns
IT locm = getlocalcols(); // length (number of columns) assigned to this processor (and processor column)
std::vector< std::vector<NT> > localmat(locm); // some sort of minimal local copy of matrix
#ifdef COMBBLAS_DEBUG
if(accsize != locm) std::cout << "Gather vector along columns logic is wrong" << std::endl;
#endif
for(typename DER::SpColIter colit = spSeq->begcol(); colit != spSeq->endcol(); ++colit) // iterate over columns
{
if(percsum[colit.colid()] >= k_limit) // don't make a copy of an inactive column
{
localmat[colit.colid()].reserve(colit.nnz());
for(typename DER::SpColIter::NzIter nzit = spSeq->begnz(colit); nzit < spSeq->endnz(colit); ++nzit)
{
localmat[colit.colid()].push_back(nzit.value());
}
}
}
int64_t activecols = std::count_if(percsum.begin(), percsum.end(), [k_limit](IT i){ return i >= k_limit;});
int64_t activennz = std::accumulate(percsum.begin(), percsum.end(), (int64_t) 0);
int64_t totactcols, totactnnzs;
MPI_Allreduce(&activecols, &totactcols, 1, MPIType<int64_t>(), MPI_SUM, commGrid->GetRowWorld());
if(myrank == 0) std::cout << "Number of initial active columns are " << totactcols << std::endl;
MPI_Allreduce(&activennz, &totactnnzs, 1, MPIType<int64_t>(), MPI_SUM, commGrid->GetRowWorld());
if(myrank == 0) std::cout << "Number of initial nonzeros are " << totactnnzs << std::endl;
#ifdef COMBBLAS_DEBUG
IT glactcols = colcnt.Count([k_limit](IT i){ return i >= k_limit;});
if(myrank == 0) std::cout << "Number of initial active columns are " << glactcols << std::endl;
if(glactcols != totactcols) if(myrank == 0) std::cout << "Wrong number of active columns are computed" << std::endl;
#endif
rvec = FullyDistVec<GIT,VT> ( rvec.getcommgrid(), getncol(), std::numeric_limits<NT>::min()); // set length of rvec correctly
#ifdef COMBBLAS_DEBUG
PrintInfo();
rvec.PrintInfo("rvector");
#endif
if(totactcols == 0)
{
std::ostringstream ss;
ss << "TopK: k_limit (" << k_limit <<")" << " >= maxNnzInColumn. Returning the result of Reduce(Column, minimum<NT>()) instead..." << std::endl;
SpParHelper::Print(ss.str());
return false; // no prune needed
}
std::vector<IT> actcolsmap(activecols); // the map that gives the original index of that active column (this map will shrink over iterations)
for (IT i=0, j=0; i< locm; ++i) {
if(percsum[i] >= k_limit)
actcolsmap[j++] = i;
}
std::vector<NT> all_medians;
std::vector<IT> nnz_per_col;
std::vector<IT> klimits(activecols, k_limit); // is distributed management of this vector needed?
int activecols_lowerbound = 10*colneighs;
IT * locncols = new IT[rowneighs];
locncols[rankinrow] = locm;
MPI_Allgather(MPI_IN_PLACE, 0, MPIType<IT>(),locncols, 1, MPIType<IT>(), commGrid->GetRowWorld());
IT coffset = std::accumulate(locncols, locncols+rankinrow, static_cast<IT>(0));
delete [] locncols;
/* Create/allocate variables for vector assignment */
MPI_Datatype MPI_pair;
MPI_Type_contiguous(sizeof(std::pair<IT,NT>), MPI_CHAR, &MPI_pair);
MPI_Type_commit(&MPI_pair);
int * sendcnt = new int[nprocs];
int * recvcnt = new int[nprocs];
int * sdispls = new int[nprocs]();
int * rdispls = new int[nprocs]();
while(totactcols > 0)
{
int chunksize, iterations, lastchunk;
if(activecols > activecols_lowerbound)
{
// two reasons for chunking:
// (1) keep memory limited to activecols (<= n/sqrt(p))
// (2) avoid overflow in sentcount
chunksize = static_cast<int>(activecols/colneighs); // invariant chunksize >= 10 (by activecols_lowerbound)
iterations = std::max(static_cast<int>(activecols/chunksize), 1);
lastchunk = activecols - (iterations-1)*chunksize; // lastchunk >= chunksize by construction
}
else
{
chunksize = activecols;
iterations = 1;
lastchunk = activecols;
}
std::vector<NT> activemedians(activecols); // one per "active" column
std::vector<IT> activennzperc(activecols);
#ifdef THREADED
#pragma omp parallel for
#endif
for(IT i=0; i< activecols; ++i) // recompute the medians and nnzperc
{
size_t orgindex = actcolsmap[i]; // assert: no two threads will share the same "orgindex"
if(localmat[orgindex].empty())
{
activemedians[i] = (NT) 0;
activennzperc[i] = 0;
}
else
{
// this actually *sorts* increasing but doesn't matter as long we solely care about the median as opposed to a general nth element
auto entriesincol(localmat[orgindex]); // create a temporary vector as nth_element modifies the vector
std::nth_element(entriesincol.begin(), entriesincol.begin() + entriesincol.size()/2, entriesincol.end());
activemedians[i] = entriesincol[entriesincol.size()/2];
activennzperc[i] = entriesincol.size();
}
}
percsum.resize(activecols, 0);
MPI_Allreduce(activennzperc.data(), percsum.data(), activecols, MPIType<IT>(), MPI_SUM, commGrid->GetColWorld());
activennz = std::accumulate(percsum.begin(), percsum.end(), (int64_t) 0);
#ifdef COMBBLAS_DEBUG
MPI_Allreduce(&activennz, &totactnnzs, 1, MPIType<int64_t>(), MPI_SUM, commGrid->GetRowWorld());
if(myrank == 0) std::cout << "Number of active nonzeros are " << totactnnzs << std::endl;
#endif
std::vector<IT> toretain;
if(rankincol == 0)
{
all_medians.resize(lastchunk*colneighs);
nnz_per_col.resize(lastchunk*colneighs);
}
std::vector< std::vector< std::pair<IT,NT> > > tmppair(nprocs);
for(int i=0; i< iterations-1; ++i) // this loop should not be parallelized if we want to keep storage small
{
TopKGather(all_medians, nnz_per_col, chunksize, chunksize, activemedians, activennzperc, i, localmat, actcolsmap, klimits, toretain, tmppair, coffset, rvec);
}
TopKGather(all_medians, nnz_per_col, lastchunk, chunksize, activemedians, activennzperc, iterations-1, localmat, actcolsmap, klimits, toretain, tmppair, coffset, rvec);
/* Set the newly found vector entries */
IT totsend = 0;
for(IT i=0; i<nprocs; ++i)
{
sendcnt[i] = tmppair[i].size();
totsend += tmppair[i].size();
}
MPI_Alltoall(sendcnt, 1, MPI_INT, recvcnt, 1, MPI_INT, commGrid->GetWorld());
std::partial_sum(sendcnt, sendcnt+nprocs-1, sdispls+1);
std::partial_sum(recvcnt, recvcnt+nprocs-1, rdispls+1);
IT totrecv = std::accumulate(recvcnt,recvcnt+nprocs, static_cast<IT>(0));
assert((totsend < std::numeric_limits<int>::max()));
assert((totrecv < std::numeric_limits<int>::max()));
std::pair<IT,NT> * sendpair = new std::pair<IT,NT>[totsend];
for(int i=0; i<nprocs; ++i)
{
std::copy(tmppair[i].begin(), tmppair[i].end(), sendpair+sdispls[i]);
std::vector< std::pair<IT,NT> >().swap(tmppair[i]); // clear memory
}
std::vector< std::pair<IT,NT> > recvpair(totrecv);
MPI_Alltoallv(sendpair, sendcnt, sdispls, MPI_pair, recvpair.data(), recvcnt, rdispls, MPI_pair, commGrid->GetWorld());
delete [] sendpair;
IT updated = 0;
for(auto & update : recvpair ) // Now, write these to rvec
{
updated++;
rvec.arr[update.first] = update.second;
}
#ifdef COMBBLAS_DEBUG
MPI_Allreduce(MPI_IN_PLACE, &updated, 1, MPIType<IT>(), MPI_SUM, commGrid->GetWorld());
if(myrank == 0) std::cout << "Total vector entries updated " << updated << std::endl;
#endif
/* End of setting up the newly found vector entries */
std::vector<IT> newactivecols(toretain.size());
std::vector<IT> newklimits(toretain.size());
IT newindex = 0;
for(auto & retind : toretain )
{
newactivecols[newindex] = actcolsmap[retind];
newklimits[newindex++] = klimits[retind];
}
actcolsmap.swap(newactivecols);
klimits.swap(newklimits);
activecols = actcolsmap.size();
MPI_Allreduce(&activecols, &totactcols, 1, MPIType<int64_t>(), MPI_SUM, commGrid->GetRowWorld());
#ifdef COMBBLAS_DEBUG
if(myrank == 0) std::cout << "Number of active columns are " << totactcols << std::endl;
#endif
}
DeleteAll(sendcnt, recvcnt, sdispls, rdispls);
MPI_Type_free(&MPI_pair);
#ifdef COMBBLAS_DEBUG
if(myrank == 0) std::cout << "Exiting kselect2"<< std::endl;
#endif
return true; // prune needed
}
template <class IT, class NT, class DER>
void SpParMat< IT,NT,DER >::Dump(std::string filename) const
{
MPI_Comm World = commGrid->GetWorld();
int rank = commGrid->GetRank();
int nprocs = commGrid->GetSize();
MPI_File thefile;
char * _fn = const_cast<char*>(filename.c_str());
MPI_File_open(World, _fn, MPI_MODE_CREATE | MPI_MODE_WRONLY, MPI_INFO_NULL, &thefile);
int rankinrow = commGrid->GetRankInProcRow();
int rankincol = commGrid->GetRankInProcCol();
int rowneighs = commGrid->GetGridCols(); // get # of processors on the row
int colneighs = commGrid->GetGridRows();
IT * colcnts = new IT[rowneighs];
IT * rowcnts = new IT[colneighs];
rowcnts[rankincol] = getlocalrows();
colcnts[rankinrow] = getlocalcols();
MPI_Allgather(MPI_IN_PLACE, 0, MPIType<IT>(), colcnts, 1, MPIType<IT>(), commGrid->GetRowWorld());
IT coloffset = std::accumulate(colcnts, colcnts+rankinrow, static_cast<IT>(0));
MPI_Allgather(MPI_IN_PLACE, 0, MPIType<IT>(), rowcnts, 1, MPIType<IT>(), commGrid->GetColWorld());
IT rowoffset = std::accumulate(rowcnts, rowcnts+rankincol, static_cast<IT>(0));
DeleteAll(colcnts, rowcnts);
IT * prelens = new IT[nprocs];
prelens[rank] = 2*getlocalnnz();
MPI_Allgather(MPI_IN_PLACE, 0, MPIType<IT>(), prelens, 1, MPIType<IT>(), commGrid->GetWorld());
IT lengthuntil = std::accumulate(prelens, prelens+rank, static_cast<IT>(0));
// The disp displacement argument specifies the position
// (absolute offset in bytes from the beginning of the file)
MPI_Offset disp = lengthuntil * sizeof(uint32_t);
char native[] = "native";
MPI_File_set_view(thefile, disp, MPI_UNSIGNED, MPI_UNSIGNED, native, MPI_INFO_NULL);
uint32_t * gen_edges = new uint32_t[prelens[rank]];
IT k = 0;
for(typename DER::SpColIter colit = spSeq->begcol(); colit != spSeq->endcol(); ++colit) // iterate over columns
{
for(typename DER::SpColIter::NzIter nzit = spSeq->begnz(colit); nzit != spSeq->endnz(colit); ++nzit)
{
gen_edges[k++] = (uint32_t) (nzit.rowid() + rowoffset);
gen_edges[k++] = (uint32_t) (colit.colid() + coloffset);
}
}
assert(k == prelens[rank]);
MPI_File_write(thefile, gen_edges, prelens[rank], MPI_UNSIGNED, NULL);
MPI_File_close(&thefile);
delete [] prelens;
delete [] gen_edges;
}
template <class IT, class NT, class DER>
void SpParMat< IT,NT,DER >::ParallelBinaryWrite(std::string filename) const
{
int myrank = commGrid->GetRank();
int nprocs = commGrid->GetSize();
IT totalm = getnrow();
IT totaln = getncol();
IT totnnz = getnnz();
const int64_t headersize = 52; // 52 is the size of the header, 4 characters + 6*8 integer space
int64_t elementsize = 2*sizeof(IT)+sizeof(NT);
int64_t localentries = getlocalnnz();
int64_t localbytes = localentries*elementsize ; // localsize in bytes
if(myrank == 0)
localbytes += headersize;
int64_t bytesuntil = 0;
MPI_Exscan( &localbytes, &bytesuntil, 1, MPIType<int64_t>(), MPI_SUM, commGrid->GetWorld());
if(myrank == 0) bytesuntil = 0; // because MPI_Exscan says the recvbuf in process 0 is undefined
int64_t bytestotal;
MPI_Allreduce(&localbytes, &bytestotal, 1, MPIType<int64_t>(), MPI_SUM, commGrid->GetWorld());
size_t writtensofar = 0;
char * localdata = new char[localbytes];
if(myrank ==0)
{
char start[5] = "HKDT";
uint64_t hdr[6];
hdr[0] = 2; // version: 2.0
hdr[1] = sizeof(NT); // object size
hdr[2] = 0; // format: binary
hdr[3] = totalm; // number of rows
hdr[4] = totaln; // number of columns
hdr[5] = totnnz; // number of nonzeros
std::memmove(localdata, start, 4);
std::memmove(localdata+4, hdr, sizeof(hdr));
writtensofar = headersize;
}
IT roffset = 0;
IT coffset = 0;
GetPlaceInGlobalGrid(roffset, coffset);
roffset += 1; // increment by 1 (binary format is 1-based)
coffset += 1;
for(typename DER::SpColIter colit = spSeq->begcol(); colit != spSeq->endcol(); ++colit) // iterate over nonempty subcolumns
{
for(typename DER::SpColIter::NzIter nzit = spSeq->begnz(colit); nzit != spSeq->endnz(colit); ++nzit)
{
IT glrowid = nzit.rowid() + roffset;
IT glcolid = colit.colid() + coffset;
NT glvalue = nzit.value();
std::memmove(localdata+writtensofar, &glrowid, sizeof(IT));
std::memmove(localdata+writtensofar+sizeof(IT), &glcolid, sizeof(IT));
std::memmove(localdata+writtensofar+2*sizeof(IT), &glvalue, sizeof(NT));
writtensofar += (2*sizeof(IT) + sizeof(NT));
}
}
#ifdef IODEBUG
if(myrank == 0)
cout << "local move happened..., writing to file\n";
#endif
MPI_File thefile;
MPI_File_open(commGrid->GetWorld(), (char*) filename.c_str(), MPI_MODE_CREATE | MPI_MODE_WRONLY, MPI_INFO_NULL, &thefile) ;
MPI_File_set_view(thefile, bytesuntil, MPI_CHAR, MPI_CHAR, (char*)"native", MPI_INFO_NULL);
int64_t batchSize = 256 * 1024 * 1024; // 256 MB (per processor)
size_t localfileptr = 0;
int64_t remaining = localbytes;
int64_t totalremaining = bytestotal;
while(totalremaining > 0)
{
#ifdef IODEBUG
if(myrank == 0)
std::cout << "Remaining " << totalremaining << " bytes to write in aggregate" << std::endl;
#endif
MPI_Status status;
int curBatch = std::min(batchSize, remaining);
MPI_File_write_all(thefile, localdata+localfileptr, curBatch, MPI_CHAR, &status);
int count;
MPI_Get_count(&status, MPI_CHAR, &count); // known bug: https://github.com/pmodels/mpich/issues/2332
assert( (curBatch == 0) || (count == curBatch) ); // count can return the previous/wrong value when 0 elements are written
localfileptr += curBatch;
remaining -= curBatch;
MPI_Allreduce(&remaining, &totalremaining, 1, MPIType<int64_t>(), MPI_SUM, commGrid->GetWorld());
}
MPI_File_close(&thefile);
delete [] localdata;
}
template <class IT, class NT, class DER>
SpParMat< IT,NT,DER >::SpParMat (const SpParMat< IT,NT,DER > & rhs)
{
if(rhs.spSeq != NULL)
spSeq = new DER(*(rhs.spSeq)); // Deep copy of local block
commGrid = rhs.commGrid;
}
template <class IT, class NT, class DER>
SpParMat< IT,NT,DER > & SpParMat< IT,NT,DER >::operator=(const SpParMat< IT,NT,DER > & rhs)
{
if(this != &rhs)
{
//! Check agains NULL is probably unneccessary, delete won't fail on NULL
//! But useful in the presence of a user defined "operator delete" which fails to check NULL
if(spSeq != NULL) delete spSeq;
if(rhs.spSeq != NULL)
spSeq = new DER(*(rhs.spSeq)); // Deep copy of local block
commGrid = rhs.commGrid;
}
return *this;
}
template <class IT, class NT, class DER>
SpParMat< IT,NT,DER > & SpParMat< IT,NT,DER >::operator+=(const SpParMat< IT,NT,DER > & rhs)
{
if(this != &rhs)
{
if(*commGrid == *rhs.commGrid)
{
(*spSeq) += (*(rhs.spSeq));
}
else
{
std::cout << "Grids are not comparable for parallel addition (A+B)" << std::endl;
}
}
else
{
std::cout<< "Missing feature (A+A): Use multiply with 2 instead !"<<std::endl;
}
return *this;
}
template <class IT, class NT, class DER>
float SpParMat< IT,NT,DER >::LoadImbalance() const
{
IT totnnz = getnnz(); // collective call
IT maxnnz = 0;
IT localnnz = (IT) spSeq->getnnz();
MPI_Allreduce( &localnnz, &maxnnz, 1, MPIType<IT>(), MPI_MAX, commGrid->GetWorld());
if(totnnz == 0) return 1;
return static_cast<float>((commGrid->GetSize() * maxnnz)) / static_cast<float>(totnnz);
}
template <class IT, class NT, class DER>
IT SpParMat< IT,NT,DER >::getnnz() const
{
IT totalnnz = 0;
IT localnnz = spSeq->getnnz();
MPI_Allreduce( &localnnz, &totalnnz, 1, MPIType<IT>(), MPI_SUM, commGrid->GetWorld());
return totalnnz;
}
template <class IT, class NT, class DER>
IT SpParMat< IT,NT,DER >::getnrow() const
{
IT totalrows = 0;
IT localrows = spSeq->getnrow();
MPI_Allreduce( &localrows, &totalrows, 1, MPIType<IT>(), MPI_SUM, commGrid->GetColWorld());
return totalrows;
}
template <class IT, class NT, class DER>
IT SpParMat< IT,NT,DER >::getncol() const
{
IT totalcols = 0;
IT localcols = spSeq->getncol();
MPI_Allreduce( &localcols, &totalcols, 1, MPIType<IT>(), MPI_SUM, commGrid->GetRowWorld());
return totalcols;
}
template <class IT, class NT, class DER>
template <typename _BinaryOperation>
void SpParMat<IT,NT,DER>::DimApply(Dim dim, const FullyDistVec<IT, NT>& x, _BinaryOperation __binary_op)
{
if(!(*commGrid == *(x.commGrid)))
{
std::cout << "Grids are not comparable for SpParMat::DimApply" << std::endl;
MPI_Abort(MPI_COMM_WORLD, GRIDMISMATCH);
}
MPI_Comm World = x.commGrid->GetWorld();
MPI_Comm ColWorld = x.commGrid->GetColWorld();
MPI_Comm RowWorld = x.commGrid->GetRowWorld();
switch(dim)
{
case Column: // scale each column
{
int xsize = (int) x.LocArrSize();
int trxsize = 0;
int diagneigh = x.commGrid->GetComplementRank();
MPI_Status status;
MPI_Sendrecv(&xsize, 1, MPI_INT, diagneigh, TRX, &trxsize, 1, MPI_INT, diagneigh, TRX, World, &status);
NT * trxnums = new NT[trxsize];
MPI_Sendrecv(const_cast<NT*>(SpHelper::p2a(x.arr)), xsize, MPIType<NT>(), diagneigh, TRX, trxnums, trxsize, MPIType<NT>(), diagneigh, TRX, World, &status);
int colneighs, colrank;
MPI_Comm_size(ColWorld, &colneighs);
MPI_Comm_rank(ColWorld, &colrank);
int * colsize = new int[colneighs];
colsize[colrank] = trxsize;
MPI_Allgather(MPI_IN_PLACE, 1, MPI_INT, colsize, 1, MPI_INT, ColWorld);
int * dpls = new int[colneighs](); // displacements (zero initialized pid)
std::partial_sum(colsize, colsize+colneighs-1, dpls+1);
int accsize = std::accumulate(colsize, colsize+colneighs, 0);
NT * scaler = new NT[accsize];
MPI_Allgatherv(trxnums, trxsize, MPIType<NT>(), scaler, colsize, dpls, MPIType<NT>(), ColWorld);
DeleteAll(trxnums,colsize, dpls);
for(typename DER::SpColIter colit = spSeq->begcol(); colit != spSeq->endcol(); ++colit) // iterate over columns
{
for(typename DER::SpColIter::NzIter nzit = spSeq->begnz(colit); nzit != spSeq->endnz(colit); ++nzit)
{
nzit.value() = __binary_op(nzit.value(), scaler[colit.colid()]);
}
}
delete [] scaler;
break;
}
case Row:
{
int xsize = (int) x.LocArrSize();
int rowneighs, rowrank;
MPI_Comm_size(RowWorld, &rowneighs);
MPI_Comm_rank(RowWorld, &rowrank);
int * rowsize = new int[rowneighs];
rowsize[rowrank] = xsize;
MPI_Allgather(MPI_IN_PLACE, 1, MPI_INT, rowsize, 1, MPI_INT, RowWorld);
int * dpls = new int[rowneighs](); // displacements (zero initialized pid)
std::partial_sum(rowsize, rowsize+rowneighs-1, dpls+1);
int accsize = std::accumulate(rowsize, rowsize+rowneighs, 0);
NT * scaler = new NT[accsize];
MPI_Allgatherv(const_cast<NT*>(SpHelper::p2a(x.arr)), xsize, MPIType<NT>(), scaler, rowsize, dpls, MPIType<NT>(), RowWorld);
DeleteAll(rowsize, dpls);
for(typename DER::SpColIter colit = spSeq->begcol(); colit != spSeq->endcol(); ++colit)
{
for(typename DER::SpColIter::NzIter nzit = spSeq->begnz(colit); nzit != spSeq->endnz(colit); ++nzit)
{
nzit.value() = __binary_op(nzit.value(), scaler[nzit.rowid()]);
}
}
delete [] scaler;
break;
}
default:
{
std::cout << "Unknown scaling dimension, returning..." << std::endl;
break;
}
}
}
template <class IT, class NT, class DER>
template <typename _BinaryOperation, typename _UnaryOperation >
FullyDistVec<IT,NT> SpParMat<IT,NT,DER>::Reduce(Dim dim, _BinaryOperation __binary_op, NT id, _UnaryOperation __unary_op) const
{
IT length;
switch(dim)
{
case Column:
{
length = getncol();
break;
}
case Row:
{
length = getnrow();
break;
}
default:
{
std::cout << "Unknown reduction dimension, returning empty vector" << std::endl;
break;
}
}
FullyDistVec<IT,NT> parvec(commGrid, length, id);
Reduce(parvec, dim, __binary_op, id, __unary_op);
return parvec;
}
template <class IT, class NT, class DER>
template <typename _BinaryOperation>
FullyDistVec<IT,NT> SpParMat<IT,NT,DER>::Reduce(Dim dim, _BinaryOperation __binary_op, NT id) const
{
IT length;
switch(dim)
{
case Column:
{
length = getncol();
break;
}
case Row:
{
length = getnrow();
break;
}
default:
{
std::cout << "Unknown reduction dimension, returning empty vector" << std::endl;
break;
}
}
FullyDistVec<IT,NT> parvec(commGrid, length, id);
Reduce(parvec, dim, __binary_op, id, myidentity<NT>()); // myidentity<NT>() is a no-op function
return parvec;
}
template <class IT, class NT, class DER>
template <typename VT, typename GIT, typename _BinaryOperation>
void SpParMat<IT,NT,DER>::Reduce(FullyDistVec<GIT,VT> & rvec, Dim dim, _BinaryOperation __binary_op, VT id) const
{
Reduce(rvec, dim, __binary_op, id, myidentity<NT>() );
}
template <class IT, class NT, class DER>
template <typename VT, typename GIT, typename _BinaryOperation, typename _UnaryOperation> // GIT: global index type of vector
void SpParMat<IT,NT,DER>::Reduce(FullyDistVec<GIT,VT> & rvec, Dim dim, _BinaryOperation __binary_op, VT id, _UnaryOperation __unary_op) const
{
Reduce(rvec, dim, __binary_op, id, __unary_op, MPIOp<_BinaryOperation, VT>::op() );
}
template <class IT, class NT, class DER>
template <typename VT, typename GIT, typename _BinaryOperation, typename _UnaryOperation> // GIT: global index type of vector
void SpParMat<IT,NT,DER>::Reduce(FullyDistVec<GIT,VT> & rvec, Dim dim, _BinaryOperation __binary_op, VT id, _UnaryOperation __unary_op, MPI_Op mympiop) const
{
if(*rvec.commGrid != *commGrid)
{
SpParHelper::Print("Grids are not comparable, SpParMat::Reduce() fails!", commGrid->GetWorld());
MPI_Abort(MPI_COMM_WORLD,GRIDMISMATCH);
}
switch(dim)
{
case Column: // pack along the columns, result is a vector of size n
{
// We can't use rvec's distribution (rows first, columns later) here
// because the ownership model of the vector has the order P(0,0), P(0,1),...
// column reduction will first generate vector distribution in P(0,0), P(1,0),... order.
IT n_thiscol = getlocalcols(); // length assigned to this processor column
int colneighs = commGrid->GetGridRows(); // including oneself
int colrank = commGrid->GetRankInProcCol();
GIT * loclens = new GIT[colneighs];
GIT * lensums = new GIT[colneighs+1](); // begin/end points of local lengths
GIT n_perproc = n_thiscol / colneighs; // length on a typical processor
if(colrank == colneighs-1)
loclens[colrank] = n_thiscol - (n_perproc*colrank);
else
loclens[colrank] = n_perproc;
MPI_Allgather(MPI_IN_PLACE, 0, MPIType<GIT>(), loclens, 1, MPIType<GIT>(), commGrid->GetColWorld());
std::partial_sum(loclens, loclens+colneighs, lensums+1); // loclens and lensums are different, but both would fit in 32-bits
std::vector<VT> trarr;
typename DER::SpColIter colit = spSeq->begcol();
for(int i=0; i< colneighs; ++i)
{
VT * sendbuf = new VT[loclens[i]];
std::fill(sendbuf, sendbuf+loclens[i], id); // fill with identity
for(; colit != spSeq->endcol() && colit.colid() < lensums[i+1]; ++colit) // iterate over a portion of columns
{
for(typename DER::SpColIter::NzIter nzit = spSeq->begnz(colit); nzit != spSeq->endnz(colit); ++nzit) // all nonzeros in this column
{
sendbuf[colit.colid()-lensums[i]] = __binary_op(static_cast<VT>(__unary_op(nzit.value())), sendbuf[colit.colid()-lensums[i]]);
}
}
VT * recvbuf = NULL;
if(colrank == i)
{
trarr.resize(loclens[i]);
recvbuf = SpHelper::p2a(trarr);
}
MPI_Reduce(sendbuf, recvbuf, loclens[i], MPIType<VT>(), mympiop, i, commGrid->GetColWorld()); // root = i
delete [] sendbuf;
}
DeleteAll(loclens, lensums);
GIT reallen; // Now we have to transpose the vector
GIT trlen = trarr.size();
int diagneigh = commGrid->GetComplementRank();
MPI_Status status;
MPI_Sendrecv(&trlen, 1, MPIType<IT>(), diagneigh, TRNNZ, &reallen, 1, MPIType<IT>(), diagneigh, TRNNZ, commGrid->GetWorld(), &status);
rvec.arr.resize(reallen);
MPI_Sendrecv(SpHelper::p2a(trarr), trlen, MPIType<VT>(), diagneigh, TRX, SpHelper::p2a(rvec.arr), reallen, MPIType<VT>(), diagneigh, TRX, commGrid->GetWorld(), &status);
rvec.glen = getncol(); // ABAB: Put a sanity check here
break;
}
case Row: // pack along the rows, result is a vector of size m
{
rvec.glen = getnrow();
int rowneighs = commGrid->GetGridCols();
int rowrank = commGrid->GetRankInProcRow();
GIT * loclens = new GIT[rowneighs];
GIT * lensums = new GIT[rowneighs+1](); // begin/end points of local lengths
loclens[rowrank] = rvec.MyLocLength();
MPI_Allgather(MPI_IN_PLACE, 0, MPIType<GIT>(), loclens, 1, MPIType<GIT>(), commGrid->GetRowWorld());
std::partial_sum(loclens, loclens+rowneighs, lensums+1);
try
{
rvec.arr.resize(loclens[rowrank], id);
// keeping track of all nonzero iterators within columns at once is unscalable w.r.t. memory (due to sqrt(p) scaling)
// thus we'll do batches of column as opposed to all columns at once. 5 million columns take 80MB (two pointers per column)
#define MAXCOLUMNBATCH 5 * 1024 * 1024
typename DER::SpColIter begfinger = spSeq->begcol(); // beginning finger to columns
// Each processor on the same processor row should execute the SAME number of reduce calls
int numreducecalls = (int) ceil(static_cast<float>(spSeq->getnzc()) / static_cast<float>(MAXCOLUMNBATCH));
int maxreducecalls;
MPI_Allreduce( &numreducecalls, &maxreducecalls, 1, MPI_INT, MPI_MAX, commGrid->GetRowWorld());
for(int k=0; k< maxreducecalls; ++k)
{
std::vector<typename DER::SpColIter::NzIter> nziters;
typename DER::SpColIter curfinger = begfinger;
for(; curfinger != spSeq->endcol() && nziters.size() < MAXCOLUMNBATCH ; ++curfinger)
{
nziters.push_back(spSeq->begnz(curfinger));
}
for(int i=0; i< rowneighs; ++i) // step by step to save memory
{
VT * sendbuf = new VT[loclens[i]];
std::fill(sendbuf, sendbuf+loclens[i], id); // fill with identity
typename DER::SpColIter colit = begfinger;
IT colcnt = 0; // "processed column" counter
for(; colit != curfinger; ++colit, ++colcnt) // iterate over this batch of columns until curfinger
{
typename DER::SpColIter::NzIter nzit = nziters[colcnt];
for(; nzit != spSeq->endnz(colit) && nzit.rowid() < lensums[i+1]; ++nzit) // a portion of nonzeros in this column
{
sendbuf[nzit.rowid()-lensums[i]] = __binary_op(static_cast<VT>(__unary_op(nzit.value())), sendbuf[nzit.rowid()-lensums[i]]);
}
nziters[colcnt] = nzit; // set the new finger
}
VT * recvbuf = NULL;
if(rowrank == i)
{
for(int j=0; j< loclens[i]; ++j)
{
sendbuf[j] = __binary_op(rvec.arr[j], sendbuf[j]); // rvec.arr will be overriden with MPI_Reduce, save its contents
}
recvbuf = SpHelper::p2a(rvec.arr);
}
MPI_Reduce(sendbuf, recvbuf, loclens[i], MPIType<VT>(), mympiop, i, commGrid->GetRowWorld()); // root = i
delete [] sendbuf;
}
begfinger = curfinger; // set the next begfilter
}
DeleteAll(loclens, lensums);
}
catch (std::length_error& le)
{
std::cerr << "Length error: " << le.what() << std::endl;
}
break;
}
default:
{
std::cout << "Unknown reduction dimension, returning empty vector" << std::endl;
break;
}
}
}
#ifndef KSELECTLIMIT
#define KSELECTLIMIT 10000
#endif
//! Kselect wrapper for a select columns of the matrix
//! Indices of the input sparse vectors kth denote the queried columns of the matrix
//! Upon return, values of kth stores the kth entries of the queried columns
//! Returns true if Kselect algorithm is invoked for at least one column
//! Otherwise, returns false
template <class IT, class NT, class DER>
template <typename VT, typename GIT>
bool SpParMat<IT,NT,DER>::Kselect(FullyDistSpVec<GIT,VT> & kth, IT k_limit, int kselectVersion) const
{
#ifdef COMBBLAS_DEBUG
FullyDistVec<GIT,VT> test1(kth.getcommgrid());
FullyDistVec<GIT,VT> test2(kth.getcommgrid());
Kselect1(test1, k_limit, myidentity<NT>());
Kselect2(test2, k_limit);
if(test1 == test2)
SpParHelper::Print("Kselect1 and Kselect2 producing same results\n");
else
{
SpParHelper::Print("WARNING: Kselect1 and Kselect2 producing DIFFERENT results\n");
test1.PrintToFile("test1");
test2.PrintToFile("test2");
}
#endif
if(kselectVersion==1 || k_limit < KSELECTLIMIT)
{
return Kselect1(kth, k_limit, myidentity<NT>());
}
else
{
FullyDistVec<GIT,VT> kthAll ( getcommgrid());
bool ret = Kselect2(kthAll, k_limit);
FullyDistSpVec<GIT,VT> temp = EWiseApply<VT>(kth, kthAll,
[](VT spval, VT dval){return dval;},
[](VT spval, VT dval){return true;},
false, NT());
kth = temp;
return ret;
}
}
//! Returns true if Kselect algorithm is invoked for at least one column
//! Otherwise, returns false
//! if false, rvec contains either the minimum entry in each column or zero
template <class IT, class NT, class DER>
template <typename VT, typename GIT>
bool SpParMat<IT,NT,DER>::Kselect(FullyDistVec<GIT,VT> & rvec, IT k_limit, int kselectVersion) const
{
#ifdef COMBBLAS_DEBUG
FullyDistVec<GIT,VT> test1(rvec.getcommgrid());
FullyDistVec<GIT,VT> test2(rvec.getcommgrid());
Kselect1(test1, k_limit, myidentity<NT>());
Kselect2(test2, k_limit);
if(test1 == test2)
SpParHelper::Print("Kselect1 and Kselect2 producing same results\n");
else
{
SpParHelper::Print("WARNING: Kselect1 and Kselect2 producing DIFFERENT results\n");
//test1.PrintToFile("test1");
//test2.PrintToFile("test2");
}
#endif
if(kselectVersion==1 || k_limit < KSELECTLIMIT)
return Kselect1(rvec, k_limit, myidentity<NT>());
else
return Kselect2(rvec, k_limit);
}
/* identify the k-th maximum element in each column of a matrix
** if the number of nonzeros in a column is less than or equal to k, return minimum entry
** Caution: this is a preliminary implementation: needs 3*(n/sqrt(p))*k memory per processor
** this memory requirement is too high for larger k
*/
template <class IT, class NT, class DER>
template <typename VT, typename GIT, typename _UnaryOperation> // GIT: global index type of vector
bool SpParMat<IT,NT,DER>::Kselect1(FullyDistVec<GIT,VT> & rvec, IT k, _UnaryOperation __unary_op) const
{
if(*rvec.commGrid != *commGrid)
{
SpParHelper::Print("Grids are not comparable, SpParMat::Kselect() fails!", commGrid->GetWorld());
MPI_Abort(MPI_COMM_WORLD,GRIDMISMATCH);
}
std::cerr << "Dense kslect is called!! " << std::endl;
FullyDistVec<IT, IT> nnzPerColumn (getcommgrid());
Reduce(nnzPerColumn, Column, std::plus<IT>(), (IT)0, [](NT val){return (IT)1;});
IT maxnnzPerColumn = nnzPerColumn.Reduce(maximum<IT>(), (IT)0);
if(k>maxnnzPerColumn)
{
SpParHelper::Print("Kselect: k is greater then maxNnzInColumn. Calling Reduce instead...\n");
Reduce(rvec, Column, minimum<NT>(), static_cast<NT>(0));
return false;
}
IT n_thiscol = getlocalcols(); // length (number of columns) assigned to this processor (and processor column)
// check, memory should be min(n_thiscol*k, local nnz)
// hence we will not overflow for very large k
std::vector<VT> sendbuf(n_thiscol*k);
std::vector<IT> send_coldisp(n_thiscol+1,0);
std::vector<IT> local_coldisp(n_thiscol+1,0);
//displacement of local columns
//local_coldisp is the displacement of all nonzeros per column
//send_coldisp is the displacement of k nonzeros per column
IT nzc = 0;
if(spSeq->getnnz()>0)
{
typename DER::SpColIter colit = spSeq->begcol();
for(IT i=0; i<n_thiscol; ++i)
{
local_coldisp[i+1] = local_coldisp[i];
send_coldisp[i+1] = send_coldisp[i];
if((colit != spSeq->endcol()) && (i==colit.colid()))
{
local_coldisp[i+1] += colit.nnz();
if(colit.nnz()>=k)
send_coldisp[i+1] += k;
else
send_coldisp[i+1] += colit.nnz();
colit++;
nzc++;
}
}
}
assert(local_coldisp[n_thiscol] == spSeq->getnnz());
// a copy of local part of the matrix
// this can be avoided if we write our own local kselect function instead of using partial_sort
std::vector<VT> localmat(spSeq->getnnz());
#ifdef THREADED
#pragma omp parallel for
#endif
for(IT i=0; i<nzc; i++)
//for(typename DER::SpColIter colit = spSeq->begcol(); colit != spSeq->endcol(); ++colit) // iterate over columns
{
typename DER::SpColIter colit = spSeq->begcol() + i;
IT colid = colit.colid();
IT idx = local_coldisp[colid];
for(typename DER::SpColIter::NzIter nzit = spSeq->begnz(colit); nzit < spSeq->endnz(colit); ++nzit)
{
localmat[idx++] = static_cast<VT>(__unary_op(nzit.value()));
}
if(colit.nnz()<=k)
{
std::sort(localmat.begin()+local_coldisp[colid], localmat.begin()+local_coldisp[colid+1], std::greater<VT>());
std::copy(localmat.begin()+local_coldisp[colid], localmat.begin()+local_coldisp[colid+1], sendbuf.begin()+send_coldisp[colid]);
}
else
{
std::partial_sort(localmat.begin()+local_coldisp[colid], localmat.begin()+local_coldisp[colid]+k, localmat.begin()+local_coldisp[colid+1], std::greater<VT>());
std::copy(localmat.begin()+local_coldisp[colid], localmat.begin()+local_coldisp[colid]+k, sendbuf.begin()+send_coldisp[colid]);
}
}
std::vector<VT>().swap(localmat);
std::vector<IT>().swap(local_coldisp);
std::vector<VT> recvbuf(n_thiscol*k);
std::vector<VT> tempbuf(n_thiscol*k);
std::vector<IT> recv_coldisp(n_thiscol+1);
std::vector<IT> templen(n_thiscol);
int colneighs = commGrid->GetGridRows();
int colrank = commGrid->GetRankInProcCol();
for(int p=2; p <= colneighs; p*=2)
{
if(colrank%p == p/2) // this processor is a sender in this round
{
int receiver = colrank - ceil(p/2);
MPI_Send(send_coldisp.data(), n_thiscol+1, MPIType<IT>(), receiver, 0, commGrid->GetColWorld());
MPI_Send(sendbuf.data(), send_coldisp[n_thiscol], MPIType<VT>(), receiver, 1, commGrid->GetColWorld());
//break;
}
else if(colrank%p == 0) // this processor is a receiver in this round
{
int sender = colrank + ceil(p/2);
if(sender < colneighs)
{
MPI_Recv(recv_coldisp.data(), n_thiscol+1, MPIType<IT>(), sender, 0, commGrid->GetColWorld(), MPI_STATUS_IGNORE);
MPI_Recv(recvbuf.data(), recv_coldisp[n_thiscol], MPIType<VT>(), sender, 1, commGrid->GetColWorld(), MPI_STATUS_IGNORE);
#ifdef THREADED
#pragma omp parallel for
#endif
for(IT i=0; i<n_thiscol; ++i)
{
// partial merge until first k elements
IT j=send_coldisp[i], l=recv_coldisp[i];
//IT templen[i] = k*i;
IT offset = k*i;
IT lid = 0;
for(; j<send_coldisp[i+1] && l<recv_coldisp[i+1] && lid<k;)
{
if(sendbuf[j] > recvbuf[l]) // decision
tempbuf[offset+lid++] = sendbuf[j++];
else
tempbuf[offset+lid++] = recvbuf[l++];
}
while(j<send_coldisp[i+1] && lid<k) tempbuf[offset+lid++] = sendbuf[j++];
while(l<recv_coldisp[i+1] && lid<k) tempbuf[offset+lid++] = recvbuf[l++];
templen[i] = lid;
}
send_coldisp[0] = 0;
for(IT i=0; i<n_thiscol; i++)
{
send_coldisp[i+1] = send_coldisp[i] + templen[i];
}
#ifdef THREADED
#pragma omp parallel for
#endif
for(IT i=0; i<n_thiscol; i++) // direct copy
{
IT offset = k*i;
std::copy(tempbuf.begin()+offset, tempbuf.begin()+offset+templen[i], sendbuf.begin() + send_coldisp[i]);
}
}
}
}
MPI_Barrier(commGrid->GetWorld());
std::vector<VT> kthItem(n_thiscol);
int root = commGrid->GetDiagOfProcCol();
if(root==0 && colrank==0) // rank 0
{
#ifdef THREADED
#pragma omp parallel for
#endif
for(IT i=0; i<n_thiscol; i++)
{
IT nitems = send_coldisp[i+1]-send_coldisp[i];
if(nitems >= k)
kthItem[i] = sendbuf[send_coldisp[i]+k-1];
else
kthItem[i] = std::numeric_limits<VT>::min(); // return minimum possible value if a column is empty or has less than k elements
}
}
else if(root>0 && colrank==0) // send to the diagonl processor of this processor column
{
#ifdef THREADED
#pragma omp parallel for
#endif
for(IT i=0; i<n_thiscol; i++)
{
IT nitems = send_coldisp[i+1]-send_coldisp[i];
if(nitems >= k)
kthItem[i] = sendbuf[send_coldisp[i]+k-1];
else
kthItem[i] = std::numeric_limits<VT>::min(); // return minimum possible value if a column is empty or has less than k elements
}
MPI_Send(kthItem.data(), n_thiscol, MPIType<VT>(), root, 0, commGrid->GetColWorld());
}
else if(root>0 && colrank==root)
{
MPI_Recv(kthItem.data(), n_thiscol, MPIType<VT>(), 0, 0, commGrid->GetColWorld(), MPI_STATUS_IGNORE);
}
std::vector <int> sendcnts;
std::vector <int> dpls;
if(colrank==root)
{
int proccols = commGrid->GetGridCols();
IT n_perproc = n_thiscol / proccols;
sendcnts.resize(proccols);
std::fill(sendcnts.data(), sendcnts.data()+proccols-1, n_perproc);
sendcnts[proccols-1] = n_thiscol - (n_perproc * (proccols-1));
dpls.resize(proccols,0); // displacements (zero initialized pid)
std::partial_sum(sendcnts.data(), sendcnts.data()+proccols-1, dpls.data()+1);
}
int rowroot = commGrid->GetDiagOfProcRow();
int recvcnts = 0;
// scatter received data size
MPI_Scatter(sendcnts.data(),1, MPI_INT, & recvcnts, 1, MPI_INT, rowroot, commGrid->GetRowWorld());
rvec.arr.resize(recvcnts);
MPI_Scatterv(kthItem.data(),sendcnts.data(), dpls.data(), MPIType<VT>(), rvec.arr.data(), rvec.arr.size(), MPIType<VT>(),rowroot, commGrid->GetRowWorld());
rvec.glen = getncol();
return true;
}
template <class IT, class NT, class DER>
template <typename VT, typename GIT, typename _UnaryOperation> // GIT: global index type of vector
bool SpParMat<IT,NT,DER>::Kselect1(FullyDistSpVec<GIT,VT> & rvec, IT k, _UnaryOperation __unary_op) const
{
int myrank;
MPI_Comm_rank(MPI_COMM_WORLD,&myrank);
if(*rvec.commGrid != *commGrid)
{
SpParHelper::Print("Grids are not comparable, SpParMat::Kselect() fails!", commGrid->GetWorld());
MPI_Abort(MPI_COMM_WORLD,GRIDMISMATCH);
}
/*
FullyDistVec<IT, IT> nnzPerColumn (getcommgrid());
Reduce(nnzPerColumn, Column, plus<IT>(), (IT)0, [](NT val){return (IT)1;});
IT maxnnzPerColumn = nnzPerColumn.Reduce(maximum<IT>(), (IT)0);
if(k>maxnnzPerColumn)
{
SpParHelper::Print("Kselect: k is greater then maxNnzInColumn. Calling Reduce instead...\n");
Reduce(rvec, Column, minimum<NT>(), static_cast<NT>(0));
return false;
}
*/
IT n_thiscol = getlocalcols(); // length (number of columns) assigned to this processor (and processor column)
MPI_Comm World = rvec.commGrid->GetWorld();
MPI_Comm ColWorld = rvec.commGrid->GetColWorld();
MPI_Comm RowWorld = rvec.commGrid->GetRowWorld();
int colneighs = commGrid->GetGridRows();
int colrank = commGrid->GetRankInProcCol();
int coldiagrank = commGrid->GetDiagOfProcCol();
//double memk = 3 * (double)n_thiscol*k*sizeof(VT)/1000000000;
//double maxmemk =0.0; // nnz in a process column
//MPI_Allreduce(&memk, &maxmemk , 1, MPIType<double>(), MPI_MAX, MPI_COMM_WORLD);
//int myrank;
//MPI_Comm_rank( MPI_COMM_WORLD, &myrank ) ;
//if(myrank==0)
// std::cerr << "Actual kselect memory: " << maxmemk << "GB " << " columns " << n_thiscol << " activecol: " << nActiveCols << " \n";
// MPI_Barrier(MPI_COMM_WORLD);
//replicate sparse indices along processor column
int accnz;
int32_t trxlocnz;
GIT lenuntil;
int32_t *trxinds, *activeCols;
VT *trxnums, *numacc=NULL;
TransposeVector(World, rvec, trxlocnz, lenuntil, trxinds, trxnums, true);
if(rvec.commGrid->GetGridRows() > 1)
{
//TODO: we only need to communicate indices
AllGatherVector(ColWorld, trxlocnz, lenuntil, trxinds, trxnums, activeCols, numacc, accnz, true); // trxindS/trxnums deallocated, indacc/numacc allocated, accnz set
}
else
{
accnz = trxlocnz;
activeCols = trxinds; //aliasing ptr
// since indexisvalue is set true in TransposeVector(), trxnums is never allocated
//numacc = trxnums; //aliasing ptr
}
std::vector<bool> isactive(n_thiscol,false);
for(int i=0; i<accnz ; i++)
{
isactive[activeCols[i]] = true;
}
IT nActiveCols = accnz;//count_if(isactive.begin(), isactive.end(), [](bool ac){return ac;});
int64_t lannz = getlocalnnz();
int64_t nnzColWorld=0; // nnz in a process column
MPI_Allreduce(&lannz, &nnzColWorld, 1, MPIType<int64_t>(), MPI_SUM, ColWorld);
int64_t maxPerProcMemory = std::min(nnzColWorld, (int64_t)nActiveCols*k) * sizeof(VT);
// hence we will not overflow for very large k
std::vector<IT> send_coldisp(n_thiscol+1,0);
std::vector<IT> local_coldisp(n_thiscol+1,0);
//vector<VT> sendbuf(nActiveCols*k);
//VT * sendbuf = static_cast<VT *> (::operator new (n_thiscol*k*sizeof(VT)));
//VT * sendbuf = static_cast<VT *> (::operator new (nActiveCols*k*sizeof(VT)));
VT * sendbuf = static_cast<VT *> (::operator new (maxPerProcMemory));
//displacement of local columns
//local_coldisp is the displacement of all nonzeros per column
//send_coldisp is the displacement of k nonzeros per column
IT nzc = 0;
if(spSeq->getnnz()>0)
{
typename DER::SpColIter colit = spSeq->begcol();
for(IT i=0; i<n_thiscol; ++i)
{
local_coldisp[i+1] = local_coldisp[i];
send_coldisp[i+1] = send_coldisp[i];
if( (colit != spSeq->endcol()) && (i==colit.colid()) )
{
if(isactive[i])
{
local_coldisp[i+1] += colit.nnz();
if(colit.nnz()>=k)
send_coldisp[i+1] += k;
else
send_coldisp[i+1] += colit.nnz();
}
colit++;
nzc++;
}
}
}
// a copy of local part of the matrix
// this can be avoided if we write our own local kselect function instead of using partial_sort
//vector<VT> localmat(local_coldisp[n_thiscol]);
VT * localmat = static_cast<VT *> (::operator new (local_coldisp[n_thiscol]*sizeof(VT)));
#ifdef THREADED
#pragma omp parallel for
#endif
for(IT i=0; i<nzc; i++)
//for(typename DER::SpColIter colit = spSeq->begcol(); colit != spSeq->endcol(); ++colit) // iterate over columns
{
typename DER::SpColIter colit = spSeq->begcol() + i;
IT colid = colit.colid();
if(isactive[colid])
{
IT idx = local_coldisp[colid];
for(typename DER::SpColIter::NzIter nzit = spSeq->begnz(colit); nzit < spSeq->endnz(colit); ++nzit)
{
localmat[idx++] = static_cast<VT>(__unary_op(nzit.value()));
}
if(colit.nnz()<=k)
{
sort(localmat+local_coldisp[colid], localmat+local_coldisp[colid+1], std::greater<VT>());
std::copy(localmat+local_coldisp[colid], localmat+local_coldisp[colid+1], sendbuf+send_coldisp[colid]);
}
else
{
partial_sort(localmat+local_coldisp[colid], localmat+local_coldisp[colid]+k, localmat+local_coldisp[colid+1], std::greater<VT>());
std::copy(localmat+local_coldisp[colid], localmat+local_coldisp[colid]+k, sendbuf+send_coldisp[colid]);
}
}
}
//vector<VT>().swap(localmat);
::operator delete(localmat);
std::vector<IT>().swap(local_coldisp);
//VT * recvbuf = static_cast<VT *> (::operator new (n_thiscol*k*sizeof(VT)));
//VT * tempbuf = static_cast<VT *> (::operator new (n_thiscol*k*sizeof(VT)));
//VT * recvbuf = static_cast<VT *> (::operator new ( nActiveCols*k*sizeof(VT)));
//VT * tempbuf = static_cast<VT *> (::operator new ( nActiveCols*k*sizeof(VT)));
VT * recvbuf = static_cast<VT *> (::operator new (maxPerProcMemory));
VT * tempbuf = static_cast<VT *> (::operator new (maxPerProcMemory));
//vector<VT> recvbuf(n_thiscol*k);
//vector<VT> tempbuf(n_thiscol*k);
std::vector<IT> recv_coldisp(n_thiscol+1);
std::vector<IT> temp_coldisp(n_thiscol+1);
//std::vector<IT> templen(n_thiscol);
// Put a barrier and then print sth
for(int p=2; p <= colneighs; p*=2)
{
if(colrank%p == p/2) // this processor is a sender in this round
{
int receiver = colrank - ceil(p/2);
MPI_Send(send_coldisp.data(), n_thiscol+1, MPIType<IT>(), receiver, 0, commGrid->GetColWorld());
MPI_Send(sendbuf, send_coldisp[n_thiscol], MPIType<VT>(), receiver, 1, commGrid->GetColWorld());
//break;
}
else if(colrank%p == 0) // this processor is a receiver in this round
{
int sender = colrank + ceil(p/2);
if(sender < colneighs)
{
MPI_Recv(recv_coldisp.data(), n_thiscol+1, MPIType<IT>(), sender, 0, commGrid->GetColWorld(), MPI_STATUS_IGNORE);
MPI_Recv(recvbuf, recv_coldisp[n_thiscol], MPIType<VT>(), sender, 1, commGrid->GetColWorld(), MPI_STATUS_IGNORE);
temp_coldisp[0] = 0;
for(IT i=0; i<n_thiscol; ++i)
{
IT sendlen = send_coldisp[i+1] - send_coldisp[i];
IT recvlen = recv_coldisp[i+1] - recv_coldisp[i];
IT templen = std::min((sendlen+recvlen), k);
temp_coldisp[i+1] = temp_coldisp[i] + templen;
}
#ifdef THREADED
#pragma omp parallel for
#endif
for(IT i=0; i<n_thiscol; ++i)
{
// partial merge until first k elements
IT j=send_coldisp[i], l=recv_coldisp[i];
//IT templen[i] = k*i;
//IT offset = k*i;
IT offset = temp_coldisp[i];
IT lid = 0;
for(; j<send_coldisp[i+1] && l<recv_coldisp[i+1] && lid<k;)
{
if(sendbuf[j] > recvbuf[l]) // decision
tempbuf[offset+lid++] = sendbuf[j++];
else
tempbuf[offset+lid++] = recvbuf[l++];
}
while(j<send_coldisp[i+1] && lid<k) tempbuf[offset+lid++] = sendbuf[j++];
while(l<recv_coldisp[i+1] && lid<k) tempbuf[offset+lid++] = recvbuf[l++];
//templen[i] = lid;
}
std::copy(temp_coldisp.begin(), temp_coldisp.end(), send_coldisp.begin());
std::copy(tempbuf, tempbuf+temp_coldisp[n_thiscol], sendbuf);
/*
send_coldisp[0] = 0;
for(IT i=0; i<n_thiscol; i++)
{
send_coldisp[i+1] = send_coldisp[i] + templen[i];
assert(send_coldisp[i+1] == temp_coldisp[i+1]);
}
#ifdef THREADED
#pragma omp parallel for
#endif
for(IT i=0; i<n_thiscol; i++) // direct copy
{
//IT offset = k*i;
IT offset = temp_coldisp[i];
std::copy(tempbuf+offset, tempbuf+offset+templen[i], sendbuf + send_coldisp[i]);
}
*/
}
}
}
MPI_Barrier(commGrid->GetWorld());
// Print sth here as well
/*--------------------------------------------------------
At this point, top k elements in every active column
are gathered on the first processor row, P(0,:).
Next step: At P(0,i) find the kth largest element in
active columns belonging to P(0,i).
If nnz in a column is less than k, keep the largest nonzero.
If a column is empty, keep the lowest numeric value.
--------------------------------------------------------*/
std::vector<VT> kthItem(nActiveCols); // kth elements of local active columns
if(colrank==0)
{
#ifdef THREADED
#pragma omp parallel for
#endif
for(IT i=0; i<nActiveCols; i++)
{
IT ai = activeCols[i]; // active column index
IT nitems = send_coldisp[ai+1]-send_coldisp[ai];
if(nitems >= k)
kthItem[i] = sendbuf[send_coldisp[ai]+k-1];
else if (nitems==0)
kthItem[i] = std::numeric_limits<VT>::min(); // return minimum possible value if a column is empty
else
kthItem[i] = sendbuf[send_coldisp[ai+1]-1]; // returning the last entry if nnz in this column is less than k
}
}
/*--------------------------------------------------------
At this point, kth largest elements in every active column
are gathered on the first processor row, P(0,:).
Next step: Send the kth largest elements from P(0,i) to P(i,i)
Nothing to do for P(0,0)
--------------------------------------------------------*/
if(coldiagrank>0 && colrank==0)
{
MPI_Send(kthItem.data(), nActiveCols, MPIType<VT>(), coldiagrank, 0, commGrid->GetColWorld());
}
else if(coldiagrank>0 && colrank==coldiagrank) // receive in the diagonal processor
{
MPI_Recv(kthItem.data(), nActiveCols, MPIType<VT>(), 0, 0, commGrid->GetColWorld(), MPI_STATUS_IGNORE);
}
// Put a barrier and print sth
/*--------------------------------------------------------
At this point, kth largest elements in every active column
are gathered on the diagonal processors P(i,i).
Next step: Scatter the kth largest elements from P(i,i)
to all processors in the ith row, P(i,:).
Each processor recevies exactly local nnz of rvec entries
so that the received data can be directly put in rvec.
--------------------------------------------------------*/
int rowroot = commGrid->GetDiagOfProcRow();
int proccols = commGrid->GetGridCols();
std::vector <int> sendcnts(proccols,0);
std::vector <int> dpls(proccols,0);
int lsize = rvec.ind.size();
// local sizes of the input vecotor will be sent from the doagonal processor
MPI_Gather(&lsize,1, MPI_INT, sendcnts.data(), 1, MPI_INT, rowroot, RowWorld);
std::partial_sum(sendcnts.data(), sendcnts.data()+proccols-1, dpls.data()+1);
MPI_Scatterv(kthItem.data(),sendcnts.data(), dpls.data(), MPIType<VT>(), rvec.num.data(), rvec.num.size(), MPIType<VT>(),rowroot, RowWorld);
delete [] activeCols;
delete [] numacc;
::operator delete(sendbuf);
::operator delete(recvbuf);
::operator delete(tempbuf);
//delete [] activeCols;
//delete [] numacc;
return true;
}
// only defined for symmetric matrix
template <class IT, class NT, class DER>
IT SpParMat<IT,NT,DER>::Bandwidth() const
{
IT upperlBW = -1;
IT lowerlBW = -1;
IT m_perproc = getnrow() / commGrid->GetGridRows();
IT n_perproc = getncol() / commGrid->GetGridCols();
IT moffset = commGrid->GetRankInProcCol() * m_perproc;
IT noffset = commGrid->GetRankInProcRow() * n_perproc;
for(typename DER::SpColIter colit = spSeq->begcol(); colit != spSeq->endcol(); ++colit) // iterate over columns
{
IT diagrow = colit.colid() + noffset;
typename DER::SpColIter::NzIter nzit = spSeq->begnz(colit);
if(nzit != spSeq->endnz(colit)) // nonempty column
{
IT firstrow = nzit.rowid() + moffset;
IT lastrow = (nzit+ colit.nnz()-1).rowid() + moffset;
if(firstrow <= diagrow) // upper diagonal
{
IT dev = diagrow - firstrow;
if(upperlBW < dev) upperlBW = dev;
}
if(lastrow >= diagrow) // lower diagonal
{
IT dev = lastrow - diagrow;
if(lowerlBW < dev) lowerlBW = dev;
}
}
}
IT upperBW;
//IT lowerBW;
MPI_Allreduce( &upperlBW, &upperBW, 1, MPIType<IT>(), MPI_MAX, commGrid->GetWorld());
//MPI_Allreduce( &lowerlBW, &lowerBW, 1, MPIType<IT>(), MPI_MAX, commGrid->GetWorld());
//return (upperBW + lowerBW + 1);
return (upperBW);
}
// only defined for symmetric matrix
template <class IT, class NT, class DER>
IT SpParMat<IT,NT,DER>::Profile() const
{
int colrank = commGrid->GetRankInProcRow();
IT cols = getncol();
IT rows = getnrow();
IT m_perproc = cols / commGrid->GetGridRows();
IT n_perproc = rows / commGrid->GetGridCols();
IT moffset = commGrid->GetRankInProcCol() * m_perproc;
IT noffset = colrank * n_perproc;
int pc = commGrid->GetGridCols();
IT n_thisproc;
if(colrank!=pc-1 ) n_thisproc = n_perproc;
else n_thisproc = cols - (pc-1)*n_perproc;
std::vector<IT> firstRowInCol(n_thisproc,getnrow());
std::vector<IT> lastRowInCol(n_thisproc,-1);
for(typename DER::SpColIter colit = spSeq->begcol(); colit != spSeq->endcol(); ++colit) // iterate over columns
{
IT diagrow = colit.colid() + noffset;
typename DER::SpColIter::NzIter nzit = spSeq->begnz(colit);
if(nzit != spSeq->endnz(colit)) // nonempty column
{
IT firstrow = nzit.rowid() + moffset;
IT lastrow = (nzit+ colit.nnz()-1).rowid() + moffset;
if(firstrow <= diagrow) // upper diagonal
{
firstRowInCol[colit.colid()] = firstrow;
}
if(lastrow >= diagrow) // lower diagonal
{
lastRowInCol[colit.colid()] = lastrow;
}
}
}
std::vector<IT> firstRowInCol_global(n_thisproc,getnrow());
//vector<IT> lastRowInCol_global(n_thisproc,-1);
MPI_Allreduce( firstRowInCol.data(), firstRowInCol_global.data(), n_thisproc, MPIType<IT>(), MPI_MIN, commGrid->colWorld);
//MPI_Allreduce( lastRowInCol.data(), lastRowInCol_global.data(), n_thisproc, MPIType<IT>(), MPI_MAX, commGrid->GetColWorld());
IT profile = 0;
for(IT i=0; i<n_thisproc; i++)
{
if(firstRowInCol_global[i]==rows) // empty column
profile++;
else
profile += (i + noffset - firstRowInCol_global[i]);
}
IT profile_global = 0;
MPI_Allreduce( &profile, &profile_global, 1, MPIType<IT>(), MPI_SUM, commGrid->rowWorld);
return (profile_global);
}
template <class IT, class NT, class DER>
template <typename VT, typename GIT, typename _BinaryOperation>
void SpParMat<IT,NT,DER>::MaskedReduce(FullyDistVec<GIT,VT> & rvec, FullyDistSpVec<GIT,VT> & mask, Dim dim, _BinaryOperation __binary_op, VT id, bool exclude) const
{
if (dim!=Column)
{
SpParHelper::Print("SpParMat::MaskedReduce() is only implemented for Colum\n");
MPI_Abort(MPI_COMM_WORLD,GRIDMISMATCH);
}
MaskedReduce(rvec, mask, dim, __binary_op, id, myidentity<NT>(), exclude);
}
/**
* Reduce along the column into a vector
* @param[in] mask {A sparse vector indicating row indices included/excluded (based on exclude argument) in the reduction }
* @param[in] __binary_op {the operation used for reduction; examples: max, min, plus, multiply, and, or. Its parameters and return type are all VT}
* @param[in] id {scalar that is used as the identity for __binary_op; examples: zero, infinity}
* @param[in] __unary_op {optional unary operation applied to nonzeros *before* the __binary_op; examples: 1/x, x^2}
* @param[in] exclude {if true, masked row indices are included in the reduction}
* @param[out] rvec {the return vector, specified as an output parameter to allow arbitrary return types via VT}
**/
template <class IT, class NT, class DER>
template <typename VT, typename GIT, typename _BinaryOperation, typename _UnaryOperation> // GIT: global index type of vector
void SpParMat<IT,NT,DER>::MaskedReduce(FullyDistVec<GIT,VT> & rvec, FullyDistSpVec<GIT,VT> & mask, Dim dim, _BinaryOperation __binary_op, VT id, _UnaryOperation __unary_op, bool exclude) const
{
MPI_Comm World = commGrid->GetWorld();
MPI_Comm ColWorld = commGrid->GetColWorld();
MPI_Comm RowWorld = commGrid->GetRowWorld();
if (dim!=Column)
{
SpParHelper::Print("SpParMat::MaskedReduce() is only implemented for Colum\n");
MPI_Abort(MPI_COMM_WORLD,GRIDMISMATCH);
}
if(*rvec.commGrid != *commGrid)
{
SpParHelper::Print("Grids are not comparable, SpParMat::MaskedReduce() fails!", commGrid->GetWorld());
MPI_Abort(MPI_COMM_WORLD,GRIDMISMATCH);
}
int rowneighs = commGrid->GetGridCols();
int rowrank = commGrid->GetRankInProcRow();
std::vector<int> rownz(rowneighs);
int locnnzMask = static_cast<int> (mask.getlocnnz());
rownz[rowrank] = locnnzMask;
MPI_Allgather(MPI_IN_PLACE, 1, MPI_INT, rownz.data(), 1, MPI_INT, RowWorld);
std::vector<int> dpls(rowneighs+1,0);
std::partial_sum(rownz.begin(), rownz.end(), dpls.data()+1);
int accnz = std::accumulate(rownz.begin(), rownz.end(), 0);
std::vector<GIT> sendInd(locnnzMask);
std::transform(mask.ind.begin(), mask.ind.end(),sendInd.begin(), bind2nd(std::plus<GIT>(), mask.RowLenUntil()));
std::vector<GIT> indMask(accnz);
MPI_Allgatherv(sendInd.data(), rownz[rowrank], MPIType<GIT>(), indMask.data(), rownz.data(), dpls.data(), MPIType<GIT>(), RowWorld);
// We can't use rvec's distribution (rows first, columns later) here
IT n_thiscol = getlocalcols(); // length assigned to this processor column
int colneighs = commGrid->GetGridRows(); // including oneself
int colrank = commGrid->GetRankInProcCol();
GIT * loclens = new GIT[colneighs];
GIT * lensums = new GIT[colneighs+1](); // begin/end points of local lengths
GIT n_perproc = n_thiscol / colneighs; // length on a typical processor
if(colrank == colneighs-1)
loclens[colrank] = n_thiscol - (n_perproc*colrank);
else
loclens[colrank] = n_perproc;
MPI_Allgather(MPI_IN_PLACE, 0, MPIType<GIT>(), loclens, 1, MPIType<GIT>(), commGrid->GetColWorld());
std::partial_sum(loclens, loclens+colneighs, lensums+1); // loclens and lensums are different, but both would fit in 32-bits
std::vector<VT> trarr;
typename DER::SpColIter colit = spSeq->begcol();
for(int i=0; i< colneighs; ++i)
{
VT * sendbuf = new VT[loclens[i]];
std::fill(sendbuf, sendbuf+loclens[i], id); // fill with identity
for(; colit != spSeq->endcol() && colit.colid() < lensums[i+1]; ++colit) // iterate over a portion of columns
{
int k=0;
typename DER::SpColIter::NzIter nzit = spSeq->begnz(colit);
for(; nzit != spSeq->endnz(colit) && k < indMask.size(); ) // all nonzeros in this column
{
if(nzit.rowid() < indMask[k])
{
if(exclude)
{
sendbuf[colit.colid()-lensums[i]] = __binary_op(static_cast<VT>(__unary_op(nzit.value())), sendbuf[colit.colid()-lensums[i]]);
}
++nzit;
}
else if(nzit.rowid() > indMask[k]) ++k;
else
{
if(!exclude)
{
sendbuf[colit.colid()-lensums[i]] = __binary_op(static_cast<VT>(__unary_op(nzit.value())), sendbuf[colit.colid()-lensums[i]]);
}
++k;
++nzit;
}
}
if(exclude)
{
while(nzit != spSeq->endnz(colit))
{
sendbuf[colit.colid()-lensums[i]] = __binary_op(static_cast<VT>(__unary_op(nzit.value())), sendbuf[colit.colid()-lensums[i]]);
++nzit;
}
}
}
VT * recvbuf = NULL;
if(colrank == i)
{
trarr.resize(loclens[i]);
recvbuf = SpHelper::p2a(trarr);
}
MPI_Reduce(sendbuf, recvbuf, loclens[i], MPIType<VT>(), MPIOp<_BinaryOperation, VT>::op(), i, commGrid->GetColWorld()); // root = i
delete [] sendbuf;
}
DeleteAll(loclens, lensums);
GIT reallen; // Now we have to transpose the vector
GIT trlen = trarr.size();
int diagneigh = commGrid->GetComplementRank();
MPI_Status status;
MPI_Sendrecv(&trlen, 1, MPIType<IT>(), diagneigh, TRNNZ, &reallen, 1, MPIType<IT>(), diagneigh, TRNNZ, commGrid->GetWorld(), &status);
rvec.arr.resize(reallen);
MPI_Sendrecv(SpHelper::p2a(trarr), trlen, MPIType<VT>(), diagneigh, TRX, SpHelper::p2a(rvec.arr), reallen, MPIType<VT>(), diagneigh, TRX, commGrid->GetWorld(), &status);
rvec.glen = getncol(); // ABAB: Put a sanity check here
}
template <class IT, class NT, class DER>
template <typename NNT,typename NDER>
SpParMat<IT,NT,DER>::operator SpParMat<IT,NNT,NDER> () const
{
NDER * convert = new NDER(*spSeq);
return SpParMat<IT,NNT,NDER> (convert, commGrid);
}
//! Change index type as well
template <class IT, class NT, class DER>
template <typename NIT, typename NNT,typename NDER>
SpParMat<IT,NT,DER>::operator SpParMat<NIT,NNT,NDER> () const
{
NDER * convert = new NDER(*spSeq);
return SpParMat<NIT,NNT,NDER> (convert, commGrid);
}
/**
* Create a submatrix of size m x (size(ci) * s) on a r x s processor grid
* Essentially fetches the columns ci[0], ci[1],... ci[size(ci)] from every submatrix
*/
template <class IT, class NT, class DER>
SpParMat<IT,NT,DER> SpParMat<IT,NT,DER>::SubsRefCol (const std::vector<IT> & ci) const
{
std::vector<IT> ri;
DER * tempseq = new DER((*spSeq)(ri, ci));
return SpParMat<IT,NT,DER> (tempseq, commGrid);
}
/**
* Generalized sparse matrix indexing (ri/ci are 0-based indexed)
* Both the storage and the actual values in FullyDistVec should be IT
* The index vectors are dense and FULLY distributed on all processors
* We can use this function to apply a permutation like A(p,q)
* Sequential indexing subroutine (via multiplication) is general enough.
*/
template <class IT, class NT, class DER>
template <typename PTNTBOOL, typename PTBOOLNT>
SpParMat<IT,NT,DER> SpParMat<IT,NT,DER>::SubsRef_SR (const FullyDistVec<IT,IT> & ri, const FullyDistVec<IT,IT> & ci, bool inplace)
{
typedef typename DER::LocalIT LIT;
// infer the concrete type SpMat<LIT,LIT>
typedef typename create_trait<DER, LIT, bool>::T_inferred DER_IT;
if((*(ri.commGrid) != *(commGrid)) || (*(ci.commGrid) != *(commGrid)))
{
SpParHelper::Print("Grids are not comparable, SpRef fails !");
MPI_Abort(MPI_COMM_WORLD, GRIDMISMATCH);
}
// Safety check
IT locmax_ri = 0;
IT locmax_ci = 0;
if(!ri.arr.empty())
locmax_ri = *std::max_element(ri.arr.begin(), ri.arr.end());
if(!ci.arr.empty())
locmax_ci = *std::max_element(ci.arr.begin(), ci.arr.end());
IT totalm = getnrow();
IT totaln = getncol();
if(locmax_ri > totalm || locmax_ci > totaln)
{
throw outofrangeexception();
}
// The indices for FullyDistVec are offset'd to 1/p pieces
// The matrix indices are offset'd to 1/sqrt(p) pieces
// Add the corresponding offset before sending the data
IT roffset = ri.RowLenUntil();
IT rrowlen = ri.MyRowLength();
IT coffset = ci.RowLenUntil();
IT crowlen = ci.MyRowLength();
// We create two boolean matrices P and Q
// Dimensions: P is size(ri) x m
// Q is n x size(ci)
// Range(ri) = {0,...,m-1}
// Range(ci) = {0,...,n-1}
IT rowneighs = commGrid->GetGridCols(); // number of neighbors along this processor row (including oneself)
IT m_perproccol = totalm / rowneighs;
IT n_perproccol = totaln / rowneighs;
// Get the right local dimensions
IT diagneigh = commGrid->GetComplementRank();
IT mylocalrows = getlocalrows();
IT mylocalcols = getlocalcols();
IT trlocalrows;
MPI_Status status;
MPI_Sendrecv(&mylocalrows, 1, MPIType<IT>(), diagneigh, TRROWX, &trlocalrows, 1, MPIType<IT>(), diagneigh, TRROWX, commGrid->GetWorld(), &status);
// we don't need trlocalcols because Q.Transpose() will take care of it
std::vector< std::vector<IT> > rowid(rowneighs); // reuse for P and Q
std::vector< std::vector<IT> > colid(rowneighs);
// Step 1: Create P
IT locvec = ri.arr.size(); // nnz in local vector
for(typename std::vector<IT>::size_type i=0; i< (unsigned)locvec; ++i)
{
// numerical values (permutation indices) are 0-based
// recipient alone progessor row
IT rowrec = (m_perproccol!=0) ? std::min(ri.arr[i] / m_perproccol, rowneighs-1) : (rowneighs-1);
// ri's numerical values give the colids and its local indices give rowids
rowid[rowrec].push_back( i + roffset);
colid[rowrec].push_back(ri.arr[i] - (rowrec * m_perproccol));
}
int * sendcnt = new int[rowneighs]; // reuse in Q as well
int * recvcnt = new int[rowneighs];
for(IT i=0; i<rowneighs; ++i)
sendcnt[i] = rowid[i].size();
MPI_Alltoall(sendcnt, 1, MPI_INT, recvcnt, 1, MPI_INT, commGrid->GetRowWorld()); // share the counts
int * sdispls = new int[rowneighs]();
int * rdispls = new int[rowneighs]();
std::partial_sum(sendcnt, sendcnt+rowneighs-1, sdispls+1);
std::partial_sum(recvcnt, recvcnt+rowneighs-1, rdispls+1);
IT p_nnz = std::accumulate(recvcnt,recvcnt+rowneighs, static_cast<IT>(0));
// create space for incoming data ...
IT * p_rows = new IT[p_nnz];
IT * p_cols = new IT[p_nnz];
IT * senddata = new IT[locvec]; // re-used for both rows and columns
for(int i=0; i<rowneighs; ++i)
{
std::copy(rowid[i].begin(), rowid[i].end(), senddata+sdispls[i]);
std::vector<IT>().swap(rowid[i]); // clear memory of rowid
}
MPI_Alltoallv(senddata, sendcnt, sdispls, MPIType<IT>(), p_rows, recvcnt, rdispls, MPIType<IT>(), commGrid->GetRowWorld());
for(int i=0; i<rowneighs; ++i)
{
std::copy(colid[i].begin(), colid[i].end(), senddata+sdispls[i]);
std::vector<IT>().swap(colid[i]); // clear memory of colid
}
MPI_Alltoallv(senddata, sendcnt, sdispls, MPIType<IT>(), p_cols, recvcnt, rdispls, MPIType<IT>(), commGrid->GetRowWorld());
delete [] senddata;
std::tuple<LIT,LIT,bool> * p_tuples = new std::tuple<LIT,LIT,bool>[p_nnz];
for(IT i=0; i< p_nnz; ++i)
{
p_tuples[i] = std::make_tuple(p_rows[i], p_cols[i], 1); // here we can convert to local indices
}
DeleteAll(p_rows, p_cols);
DER_IT * PSeq = new DER_IT();
PSeq->Create( p_nnz, rrowlen, trlocalrows, p_tuples); // deletion of tuples[] is handled by SpMat::Create
SpParMat<IT,NT,DER> PA(commGrid);
if(&ri == &ci) // Symmetric permutation
{
DeleteAll(sendcnt, recvcnt, sdispls, rdispls);
#ifdef SPREFDEBUG
SpParHelper::Print("Symmetric permutation\n", commGrid->GetWorld());
#endif
SpParMat<IT,bool,DER_IT> P (PSeq, commGrid);
if(inplace)
{
#ifdef SPREFDEBUG
SpParHelper::Print("In place multiplication\n", commGrid->GetWorld());
#endif
*this = Mult_AnXBn_DoubleBuff<PTBOOLNT, NT, DER>(P, *this, false, true); // clear the memory of *this
//ostringstream outb;
//outb << "P_after_" << commGrid->myrank;
//ofstream ofb(outb.str().c_str());
//P.put(ofb);
P.Transpose();
*this = Mult_AnXBn_DoubleBuff<PTNTBOOL, NT, DER>(*this, P, true, true); // clear the memory of both *this and P
return SpParMat<IT,NT,DER>(commGrid); // dummy return to match signature
}
else
{
PA = Mult_AnXBn_DoubleBuff<PTBOOLNT, NT, DER>(P,*this);
P.Transpose();
return Mult_AnXBn_DoubleBuff<PTNTBOOL, NT, DER>(PA, P);
}
}
else
{
// Intermediate step (to save memory): Form PA and store it in P
// Distributed matrix generation (collective call)
SpParMat<IT,bool,DER_IT> P (PSeq, commGrid);
// Do parallel matrix-matrix multiply
PA = Mult_AnXBn_DoubleBuff<PTBOOLNT, NT, DER>(P, *this);
} // P is destructed here
#ifndef NDEBUG
PA.PrintInfo();
#endif
// Step 2: Create Q (use the same row-wise communication and transpose at the end)
// This temporary to-be-transposed Q is size(ci) x n
locvec = ci.arr.size(); // nnz in local vector (reset variable)
for(typename std::vector<IT>::size_type i=0; i< (unsigned)locvec; ++i)
{
// numerical values (permutation indices) are 0-based
IT rowrec = (n_perproccol!=0) ? std::min(ci.arr[i] / n_perproccol, rowneighs-1) : (rowneighs-1);
// ri's numerical values give the colids and its local indices give rowids
rowid[rowrec].push_back( i + coffset);
colid[rowrec].push_back(ci.arr[i] - (rowrec * n_perproccol));
}
for(IT i=0; i<rowneighs; ++i)
sendcnt[i] = rowid[i].size(); // update with new sizes
MPI_Alltoall(sendcnt, 1, MPI_INT, recvcnt, 1, MPI_INT, commGrid->GetRowWorld()); // share the counts
std::fill(sdispls, sdispls+rowneighs, 0); // reset
std::fill(rdispls, rdispls+rowneighs, 0);
std::partial_sum(sendcnt, sendcnt+rowneighs-1, sdispls+1);
std::partial_sum(recvcnt, recvcnt+rowneighs-1, rdispls+1);
IT q_nnz = std::accumulate(recvcnt,recvcnt+rowneighs, static_cast<IT>(0));
// create space for incoming data ...
IT * q_rows = new IT[q_nnz];
IT * q_cols = new IT[q_nnz];
senddata = new IT[locvec];
for(int i=0; i<rowneighs; ++i)
{
std::copy(rowid[i].begin(), rowid[i].end(), senddata+sdispls[i]);
std::vector<IT>().swap(rowid[i]); // clear memory of rowid
}
MPI_Alltoallv(senddata, sendcnt, sdispls, MPIType<IT>(), q_rows, recvcnt, rdispls, MPIType<IT>(), commGrid->GetRowWorld());
for(int i=0; i<rowneighs; ++i)
{
std::copy(colid[i].begin(), colid[i].end(), senddata+sdispls[i]);
std::vector<IT>().swap(colid[i]); // clear memory of colid
}
MPI_Alltoallv(senddata, sendcnt, sdispls, MPIType<IT>(), q_cols, recvcnt, rdispls, MPIType<IT>(), commGrid->GetRowWorld());
DeleteAll(senddata, sendcnt, recvcnt, sdispls, rdispls);
std::tuple<LIT,LIT,bool> * q_tuples = new std::tuple<LIT,LIT,bool>[q_nnz]; // here we can convert to local indices (2018 note by Aydin)
for(IT i=0; i< q_nnz; ++i)
{
q_tuples[i] = std::make_tuple(q_rows[i], q_cols[i], 1);
}
DeleteAll(q_rows, q_cols);
DER_IT * QSeq = new DER_IT();
QSeq->Create( q_nnz, crowlen, mylocalcols, q_tuples); // Creating Q' instead
// Step 3: Form PAQ
// Distributed matrix generation (collective call)
SpParMat<IT,bool,DER_IT> Q (QSeq, commGrid);
Q.Transpose();
if(inplace)
{
*this = Mult_AnXBn_DoubleBuff<PTNTBOOL, NT, DER>(PA, Q, true, true); // clear the memory of both PA and P
return SpParMat<IT,NT,DER>(commGrid); // dummy return to match signature
}
else
{
return Mult_AnXBn_DoubleBuff<PTNTBOOL, NT, DER>(PA, Q);
}
}
template<class IT,
class NT,
class DER>
template<typename PTNTBOOL,
typename PTBOOLNT>
SpParMat<IT, NT, DER>
SpParMat<IT, NT, DER>::SubsRef_SR (
const FullyDistVec<IT, IT> &v,
Dim dim,
bool inplace
)
{
typedef typename DER::LocalIT LIT;
typedef typename create_trait<DER, LIT, bool>::T_inferred DER_IT;
if (*(v.commGrid) != *commGrid)
{
SpParHelper::Print("Grids are not comparable, SpRef fails!");
MPI_Abort(MPI_COMM_WORLD, GRIDMISMATCH);
}
IT locmax = 0;
if (!v.arr.empty())
locmax = *std::max_element(v.arr.begin(), v.arr.end());
IT offset = v.RowLenUntil();
IT rowlen = v.MyRowLength();
IT totalm = getnrow();
IT totaln = getncol();
IT rowneighs = commGrid->GetGridCols();
IT perproccol = -1;
IT dimy = -1;
IT diagneigh, tmp;
switch(dim)
{
case Row:
if (locmax > totalm)
throw outofrangeexception();
perproccol = totalm / rowneighs;
diagneigh = commGrid->GetComplementRank();
MPI_Status status;
tmp = getlocalrows();
MPI_Sendrecv(&tmp, 1, MPIType<IT>(), diagneigh, TRROWX,
&dimy, 1, MPIType<IT>(), diagneigh, TRROWX,
commGrid->GetWorld(), &status);
break;
case Column:
if (locmax > totaln)
throw outofrangeexception();
perproccol = totaln / rowneighs;
dimy = getlocalcols();
break;
default:
break;
}
// find owner processes and fill in the vectors
std::vector<std::vector<IT>> rowid(rowneighs);
std::vector<std::vector<IT>> colid(rowneighs);
IT locvec = v.arr.size();
for(typename std::vector<IT>::size_type i = 0; i < (unsigned)locvec; ++i)
{
IT rowrec = (perproccol != 0)
? std::min(v.arr[i] / perproccol, rowneighs - 1)
: (rowneighs - 1);
rowid[rowrec].push_back(i + offset);
colid[rowrec].push_back(v.arr[i] - (rowrec * perproccol));
}
// exchange data
int *sendcnt = new int[rowneighs];
int *recvcnt = new int[rowneighs];
for (IT i = 0; i < rowneighs; ++i)
sendcnt[i] = rowid[i].size();
MPI_Alltoall(sendcnt, 1, MPI_INT,
recvcnt, 1, MPI_INT,
commGrid->GetRowWorld());
int *sdispls = new int[rowneighs]();
int *rdispls = new int[rowneighs]();
std::partial_sum(sendcnt, sendcnt+rowneighs-1, sdispls+1);
std::partial_sum(recvcnt, recvcnt+rowneighs-1, rdispls+1);
IT v_nnz = std::accumulate(recvcnt, recvcnt+rowneighs, static_cast<IT>(0));
IT *v_rows = new IT[v_nnz];
IT *v_cols = new IT[v_nnz];
IT *senddata = new IT[locvec];
for(int i = 0; i < rowneighs; ++i)
{
std::copy(rowid[i].begin(), rowid[i].end(), senddata + sdispls[i]);
std::vector<IT>().swap(rowid[i]); // free memory
}
MPI_Alltoallv(senddata, sendcnt, sdispls, MPIType<IT>(),
v_rows, recvcnt, rdispls, MPIType<IT>(),
commGrid->GetRowWorld());
for(int i = 0; i < rowneighs; ++i)
{
std::copy(colid[i].begin(), colid[i].end(), senddata + sdispls[i]);
std::vector<IT>().swap(colid[i]); // free memory
}
MPI_Alltoallv(senddata, sendcnt, sdispls, MPIType<IT>(),
v_cols, recvcnt, rdispls, MPIType<IT>(),
commGrid->GetRowWorld());
delete [] senddata;
// form tuples and create the permutation matrix
std::tuple<LIT, LIT, bool> *v_tuples =
new std::tuple<LIT, LIT, bool>[v_nnz];
for(IT i = 0; i < v_nnz; ++i)
v_tuples[i] = std::make_tuple(v_rows[i], v_cols[i], 1);
DeleteAll(v_rows, v_cols);
DER_IT *vseq = new DER_IT();
vseq->Create(v_nnz, rowlen, dimy, v_tuples);
SpParMat<IT, bool, DER_IT> V(vseq, commGrid); // permutation matrix
// generate the final matrix
switch(dim)
{
case Row:
if (inplace)
{
*this = Mult_AnXBn_DoubleBuff<PTBOOLNT, NT, DER>(V, *this, true, true);
return SpParMat<IT, NT, DER>(commGrid); // dummy
}
else
return Mult_AnXBn_DoubleBuff<PTBOOLNT, NT, DER>(V, *this);
break;
case Column:
V.Transpose();
if (inplace)
{
*this = Mult_AnXBn_DoubleBuff<PTNTBOOL, NT, DER>(*this, V, true, true);
return SpParMat<IT, NT, DER>(commGrid); // dummy
}
else
return Mult_AnXBn_DoubleBuff<PTNTBOOL, NT, DER>(*this, V);
default:
break;
}
// should not reach at this point
return SpParMat<IT, NT, DER>(commGrid); // dummy
}
template <class IT, class NT, class DER>
void SpParMat<IT,NT,DER>::SpAsgn(const FullyDistVec<IT,IT> & ri, const FullyDistVec<IT,IT> & ci, SpParMat<IT,NT,DER> & B)
{
typedef PlusTimesSRing<NT, NT> PTRing;
if((*(ri.commGrid) != *(B.commGrid)) || (*(ci.commGrid) != *(B.commGrid)))
{
SpParHelper::Print("Grids are not comparable, SpAsgn fails !", commGrid->GetWorld());
MPI_Abort(MPI_COMM_WORLD, GRIDMISMATCH);
}
IT total_m_A = getnrow();
IT total_n_A = getncol();
IT total_m_B = B.getnrow();
IT total_n_B = B.getncol();
if(total_m_B != ri.TotalLength())
{
SpParHelper::Print("First dimension of B does NOT match the length of ri, SpAsgn fails !", commGrid->GetWorld());
MPI_Abort(MPI_COMM_WORLD, DIMMISMATCH);
}
if(total_n_B != ci.TotalLength())
{
SpParHelper::Print("Second dimension of B does NOT match the length of ci, SpAsgn fails !", commGrid->GetWorld());
MPI_Abort(MPI_COMM_WORLD, DIMMISMATCH);
}
Prune(ri, ci); // make a hole
// embed B to the size of A
FullyDistVec<IT,IT> * rvec = new FullyDistVec<IT,IT>(ri.commGrid);
rvec->iota(total_m_B, 0); // sparse() expects a zero based index
SpParMat<IT,NT,DER> R(total_m_A, total_m_B, ri, *rvec, 1);
delete rvec; // free memory
SpParMat<IT,NT,DER> RB = Mult_AnXBn_DoubleBuff<PTRing, NT, DER>(R, B, true, false); // clear memory of R but not B
FullyDistVec<IT,IT> * qvec = new FullyDistVec<IT,IT>(ri.commGrid);
qvec->iota(total_n_B, 0);
SpParMat<IT,NT,DER> Q(total_n_B, total_n_A, *qvec, ci, 1);
delete qvec; // free memory
SpParMat<IT,NT,DER> RBQ = Mult_AnXBn_DoubleBuff<PTRing, NT, DER>(RB, Q, true, true); // clear memory of RB and Q
*this += RBQ; // extend-add
}
// this only prunes the submatrix A[ri,ci] in matlab notation
// or if the input is an adjacency matrix and ri=ci, it removes the connections
// between the subgraph induced by ri
// if you need to remove all contents of rows in ri and columns in ci:
// then call PruneFull
template <class IT, class NT, class DER>
void SpParMat<IT,NT,DER>::Prune(const FullyDistVec<IT,IT> & ri, const FullyDistVec<IT,IT> & ci)
{
if((*(ri.commGrid) != *(commGrid)) || (*(ci.commGrid) != *(commGrid)))
{
SpParHelper::Print("Grids are not comparable, Prune fails!\n", commGrid->GetWorld());
MPI_Abort(MPI_COMM_WORLD, GRIDMISMATCH);
}
// Safety check
IT locmax_ri = 0;
IT locmax_ci = 0;
if(!ri.arr.empty())
locmax_ri = *std::max_element(ri.arr.begin(), ri.arr.end());
if(!ci.arr.empty())
locmax_ci = *std::max_element(ci.arr.begin(), ci.arr.end());
IT total_m = getnrow();
IT total_n = getncol();
if(locmax_ri > total_m || locmax_ci > total_n)
{
throw outofrangeexception();
}
// infer the concrete types to replace the value with bool
typedef typename DER::LocalIT LIT;
typedef typename create_trait<DER, LIT, bool>::T_inferred DER_BOOL;
typedef typename create_trait<DER, LIT, IT>::T_inferred DER_IT;
// create and downcast to boolean because this type of constructor can not be booleand as FullyDist can not be boolean
SpParMat<IT,bool,DER_BOOL> S = SpParMat<IT, IT, DER_IT> (total_m, total_m, ri, ri, 1);
SpParMat<IT,NT,DER> SA = Mult_AnXBn_DoubleBuff< BoolCopy2ndSRing<NT> , NT, DER>(S, *this, true, false); // clear memory of S but not *this
SpParMat<IT,bool,DER_BOOL> T = SpParMat<IT, IT, DER_IT> (total_n, total_n, ci, ci, 1);
SpParMat<IT,NT,DER> SAT = Mult_AnXBn_DoubleBuff< BoolCopy1stSRing<NT> , NT, DER>(SA, T, true, true); // clear memory of SA and T
// the type of the SAT matrix does not matter when calling set difference
// because it just copies the non-excluded values from (*this) matrix, without touching values in SAT
SetDifference(SAT);
}
// removes all nonzeros of rows in ri and columns in ci
// if A is an adjacency matrix and ri=ci,
// this prunes *all* (and not just the induced) connections of vertices in ri
// effectively rendering the vertices in ri *disconnected*
template <class IT, class NT, class DER>
void SpParMat<IT,NT,DER>::PruneFull(const FullyDistVec<IT,IT> & ri, const FullyDistVec<IT,IT> & ci)
{
if((*(ri.commGrid) != *(commGrid)) || (*(ci.commGrid) != *(commGrid)))
{
SpParHelper::Print("Grids are not comparable, Prune fails!\n", commGrid->GetWorld());
MPI_Abort(MPI_COMM_WORLD, GRIDMISMATCH);
}
// Safety check
IT locmax_ri = 0;
IT locmax_ci = 0;
if(!ri.arr.empty())
locmax_ri = *std::max_element(ri.arr.begin(), ri.arr.end());
if(!ci.arr.empty())
locmax_ci = *std::max_element(ci.arr.begin(), ci.arr.end());
IT total_m = getnrow();
IT total_n = getncol();
if(locmax_ri > total_m || locmax_ci > total_n)
{
throw outofrangeexception();
}
// infer the concrete types to replace the value with bool
typedef typename DER::LocalIT LIT;
typedef typename create_trait<DER, LIT, bool>::T_inferred DER_BOOL;
typedef typename create_trait<DER, LIT, IT>::T_inferred DER_IT;
// create and downcast to boolean because this type of constructor can not be booleand as FullyDist can not be boolean
SpParMat<IT,bool,DER_BOOL> S = SpParMat<IT, IT, DER_IT> (total_m, total_m, ri, ri, 1);
SpParMat<IT,NT,DER> SA = Mult_AnXBn_DoubleBuff< BoolCopy2ndSRing<NT> , NT, DER>(S, *this, true, false); // clear memory of S, but not *this
SpParMat<IT,bool,DER_BOOL> T = SpParMat<IT, IT, DER_IT> (total_n, total_n, ci, ci, 1);
SpParMat<IT,NT,DER> AT = Mult_AnXBn_DoubleBuff< BoolCopy1stSRing<NT> , NT, DER>(*this, T, false, true); // clear memory of T, but not *this
// SA extracted rows of A in ri
// AT extracted columns of A in ci
SetDifference(SA);
SetDifference(AT);
}
//! Prune every column of a sparse matrix based on pvals
template <class IT, class NT, class DER>
template <typename _BinaryOperation>
SpParMat<IT,NT,DER> SpParMat<IT,NT,DER>::PruneColumn(const FullyDistVec<IT,NT> & pvals, _BinaryOperation __binary_op, bool inPlace)
{
int myrank;
MPI_Comm_rank(MPI_COMM_WORLD,&myrank);
//MPI_Barrier(MPI_COMM_WORLD);
MPI_Comm World = pvals.commGrid->GetWorld();
MPI_Barrier(World);
if(getncol() != pvals.TotalLength())
{
std::ostringstream outs;
outs << "Can not prune column-by-column, dimensions does not match"<< std::endl;
outs << getncol() << " != " << pvals.TotalLength() << std::endl;
SpParHelper::Print(outs.str());
MPI_Abort(MPI_COMM_WORLD, DIMMISMATCH);
}
if(! ( *(getcommgrid()) == *(pvals.getcommgrid())) )
{
std::cout << "Grids are not comparable for PurneColumn" << std::endl;
MPI_Abort(MPI_COMM_WORLD, GRIDMISMATCH);
}
MPI_Comm ColWorld = pvals.commGrid->GetColWorld();
int xsize = (int) pvals.LocArrSize();
int trxsize = 0;
int diagneigh = pvals.commGrid->GetComplementRank();
MPI_Status status;
MPI_Sendrecv(&xsize, 1, MPI_INT, diagneigh, TRX, &trxsize, 1, MPI_INT, diagneigh, TRX, World, &status);
NT * trxnums = new NT[trxsize];
MPI_Sendrecv(const_cast<NT*>(SpHelper::p2a(pvals.arr)), xsize, MPIType<NT>(), diagneigh, TRX, trxnums, trxsize, MPIType<NT>(), diagneigh, TRX, World, &status);
int colneighs, colrank;
MPI_Comm_size(ColWorld, &colneighs);
MPI_Comm_rank(ColWorld, &colrank);
int * colsize = new int[colneighs];
colsize[colrank] = trxsize;
MPI_Allgather(MPI_IN_PLACE, 1, MPI_INT, colsize, 1, MPI_INT, ColWorld);
int * dpls = new int[colneighs](); // displacements (zero initialized pid)
std::partial_sum(colsize, colsize+colneighs-1, dpls+1);
int accsize = std::accumulate(colsize, colsize+colneighs, 0);
std::vector<NT> numacc(accsize);
#ifdef COMBBLAS_DEBUG
std::ostringstream outs2;
outs2 << "PruneColumn displacements: ";
for(int i=0; i< colneighs; ++i)
{
outs2 << dpls[i] << " ";
}
outs2 << std::endl;
SpParHelper::Print(outs2.str());
MPI_Barrier(World);
#endif
MPI_Allgatherv(trxnums, trxsize, MPIType<NT>(), numacc.data(), colsize, dpls, MPIType<NT>(), ColWorld);
delete [] trxnums;
delete [] colsize;
delete [] dpls;
//sanity check
if(accsize != getlocalcols()){
fprintf(stderr, "[PruneColumn]\tmyrank:%d\taccsize:%d\tgetlocalcols():%d\n", myrank, accsize, getlocalcols());
}
assert(accsize == getlocalcols());
if (inPlace)
{
spSeq->PruneColumn(numacc.data(), __binary_op, inPlace);
return SpParMat<IT,NT,DER>(getcommgrid()); // return blank to match signature
}
else
{
return SpParMat<IT,NT,DER>(spSeq->PruneColumn(numacc.data(), __binary_op, inPlace), commGrid);
}
}
template <class IT, class NT, class DER>
template <class IRRELEVANT_NT>
void SpParMat<IT,NT,DER>::PruneColumnByIndex(const FullyDistSpVec<IT,IRRELEVANT_NT>& ci)
{
MPI_Comm World = ci.commGrid->GetWorld();
MPI_Barrier(World);
if (getncol() != ci.TotalLength())
{
std::ostringstream outs;
outs << "Cannot prune column-by-column, dimensions do not match" << std::endl;
outs << getncol() << " != " << ci.TotalLength() << std::endl;
SpParHelper::Print(outs.str());
MPI_Abort(MPI_COMM_WORLD, DIMMISMATCH);
}
if (!(*(getcommgrid()) == *(ci.getcommgrid())))
{
std::cout << "Grids are not comparable for PruneColumnByIndex" << std::endl;
MPI_Abort(MPI_COMM_WORLD, GRIDMISMATCH);
}
MPI_Comm ColWorld = ci.commGrid->GetColWorld();
int diagneigh = ci.commGrid->GetComplementRank();
IT xlocnz = ci.getlocnnz();
IT xrofst = ci.RowLenUntil();
IT trxrofst;
IT trxlocnz = 0;
MPI_Sendrecv(&xrofst, 1, MPIType<IT>(), diagneigh, TROST, &trxrofst, 1, MPIType<IT>(), diagneigh, TROST, World, MPI_STATUS_IGNORE);
MPI_Sendrecv(&xlocnz, 1, MPIType<IT>(), diagneigh, TRNNZ, &trxlocnz, 1, MPIType<IT>(), diagneigh, TRNNZ, World, MPI_STATUS_IGNORE);
std::vector<IT> trxinds(trxlocnz);
MPI_Sendrecv(ci.ind.data(), xlocnz, MPIType<IT>(), diagneigh, TRI, trxinds.data(), trxlocnz, MPIType<IT>(), diagneigh, TRI, World, MPI_STATUS_IGNORE);
std::transform(trxinds.data(), trxinds.data() + trxlocnz, trxinds.data(), std::bind2nd(std::plus<IT>(), trxrofst));
int colneighs, colrank;
MPI_Comm_size(ColWorld, &colneighs);
MPI_Comm_rank(ColWorld, &colrank);
std::vector<int> colnz(colneighs);
colnz[colrank] = trxlocnz;
MPI_Allgather(MPI_IN_PLACE, 1, MPI_INT, colnz.data(), 1, MPI_INT, ColWorld);
std::vector<int> dpls(colneighs, 0);
std::partial_sum(colnz.begin(), colnz.end()-1, dpls.begin()+1);
IT accnz = std::accumulate(colnz.begin(), colnz.end(), 0);
std::vector<IT> indacc(accnz);
MPI_Allgatherv(trxinds.data(), trxlocnz, MPIType<IT>(), indacc.data(), colnz.data(), dpls.data(), MPIType<IT>(), ColWorld);
std::sort(indacc.begin(), indacc.end());
spSeq->PruneColumnByIndex(indacc);
}
//! Prune columns of a sparse matrix selected by nonzero indices of pvals
//! Each selected column is pruned by corresponding values in pvals
template <class IT, class NT, class DER>
template <typename _BinaryOperation>
SpParMat<IT,NT,DER> SpParMat<IT,NT,DER>::PruneColumn(const FullyDistSpVec<IT,NT> & pvals, _BinaryOperation __binary_op, bool inPlace)
{
//MPI_Barrier(MPI_COMM_WORLD);
MPI_Comm World = pvals.commGrid->GetWorld();
MPI_Barrier(World);
if(getncol() != pvals.TotalLength())
{
std::ostringstream outs;
outs << "Can not prune column-by-column, dimensions does not match"<< std::endl;
outs << getncol() << " != " << pvals.TotalLength() << std::endl;
SpParHelper::Print(outs.str());
MPI_Abort(MPI_COMM_WORLD, DIMMISMATCH);
}
if(! ( *(getcommgrid()) == *(pvals.getcommgrid())) )
{
std::cout << "Grids are not comparable for PurneColumn" << std::endl;
MPI_Abort(MPI_COMM_WORLD, GRIDMISMATCH);
}
MPI_Comm ColWorld = pvals.commGrid->GetColWorld();
int diagneigh = pvals.commGrid->GetComplementRank();
IT xlocnz = pvals.getlocnnz();
IT roffst = pvals.RowLenUntil();
IT roffset;
IT trxlocnz = 0;
MPI_Status status;
MPI_Sendrecv(&roffst, 1, MPIType<IT>(), diagneigh, TROST, &roffset, 1, MPIType<IT>(), diagneigh, TROST, World, &status);
MPI_Sendrecv(&xlocnz, 1, MPIType<IT>(), diagneigh, TRNNZ, &trxlocnz, 1, MPIType<IT>(), diagneigh, TRNNZ, World, &status);
std::vector<IT> trxinds (trxlocnz);
std::vector<NT> trxnums (trxlocnz);
MPI_Sendrecv(pvals.ind.data(), xlocnz, MPIType<IT>(), diagneigh, TRI, trxinds.data(), trxlocnz, MPIType<IT>(), diagneigh, TRI, World, &status);
MPI_Sendrecv(pvals.num.data(), xlocnz, MPIType<NT>(), diagneigh, TRX, trxnums.data(), trxlocnz, MPIType<NT>(), diagneigh, TRX, World, &status);
std::transform(trxinds.data(), trxinds.data()+trxlocnz, trxinds.data(), std::bind2nd(std::plus<IT>(), roffset));
int colneighs, colrank;
MPI_Comm_size(ColWorld, &colneighs);
MPI_Comm_rank(ColWorld, &colrank);
int * colnz = new int[colneighs];
colnz[colrank] = trxlocnz;
MPI_Allgather(MPI_IN_PLACE, 1, MPI_INT, colnz, 1, MPI_INT, ColWorld);
int * dpls = new int[colneighs](); // displacements (zero initialized pid)
std::partial_sum(colnz, colnz+colneighs-1, dpls+1);
IT accnz = std::accumulate(colnz, colnz+colneighs, 0);
std::vector<IT> indacc(accnz);
std::vector<NT> numacc(accnz);
MPI_Allgatherv(trxinds.data(), trxlocnz, MPIType<IT>(), indacc.data(), colnz, dpls, MPIType<IT>(), ColWorld);
MPI_Allgatherv(trxnums.data(), trxlocnz, MPIType<NT>(), numacc.data(), colnz, dpls, MPIType<NT>(), ColWorld);
delete [] colnz;
delete [] dpls;
if (inPlace)
{
spSeq->PruneColumn(indacc.data(), numacc.data(), __binary_op, inPlace);
return SpParMat<IT,NT,DER>(getcommgrid()); // return blank to match signature
}
else
{
return SpParMat<IT,NT,DER>(spSeq->PruneColumn(indacc.data(), numacc.data(), __binary_op, inPlace), commGrid);
}
}
// In-place version where rhs type is the same (no need for type promotion)
template <class IT, class NT, class DER>
void SpParMat<IT,NT,DER>::EWiseMult (const SpParMat< IT,NT,DER > & rhs, bool exclude)
{
if(*commGrid == *rhs.commGrid)
{
spSeq->EWiseMult(*(rhs.spSeq), exclude); // Dimension compatibility check performed by sequential function
}
else
{
std::cout << "Grids are not comparable, EWiseMult() fails !" << std::endl;
MPI_Abort(MPI_COMM_WORLD, GRIDMISMATCH);
}
}
// Aydin (June 2021):
// This currently duplicates the work of EWiseMult with exclude = true
// However, this is the right way of implementing it because it allows set difference when
// the types of two matrices do not have a valid multiplication operator defined
// set difference should not require such an operator so we will move all code
// bases that use EWiseMult(..., exclude=true) to this one
template <class IT, class NT, class DER>
void SpParMat<IT,NT,DER>::SetDifference(const SpParMat<IT,NT,DER> & rhs)
{
if(*commGrid == *rhs.commGrid)
{
spSeq->SetDifference(*(rhs.spSeq)); // Dimension compatibility check performed by sequential function
}
else
{
std::cout << "Grids are not comparable, SetDifference() fails !" << std::endl;
MPI_Abort(MPI_COMM_WORLD, GRIDMISMATCH);
}
}
template <class IT, class NT, class DER>
void SpParMat<IT,NT,DER>::EWiseScale(const DenseParMat<IT, NT> & rhs)
{
if(*commGrid == *rhs.commGrid)
{
spSeq->EWiseScale(rhs.array, rhs.m, rhs.n); // Dimension compatibility check performed by sequential function
}
else
{
std::cout << "Grids are not comparable, EWiseScale() fails !" << std::endl;
MPI_Abort(MPI_COMM_WORLD, GRIDMISMATCH);
}
}
template <class IT, class NT, class DER>
template <typename _BinaryOperation>
void SpParMat<IT,NT,DER>::UpdateDense(DenseParMat<IT, NT> & rhs, _BinaryOperation __binary_op) const
{
if(*commGrid == *rhs.commGrid)
{
if(getlocalrows() == rhs.m && getlocalcols() == rhs.n)
{
spSeq->UpdateDense(rhs.array, __binary_op);
}
else
{
std::cout << "Matrices have different dimensions, UpdateDense() fails !" << std::endl;
MPI_Abort(MPI_COMM_WORLD, DIMMISMATCH);
}
}
else
{
std::cout << "Grids are not comparable, UpdateDense() fails !" << std::endl;
MPI_Abort(MPI_COMM_WORLD, GRIDMISMATCH);
}
}
template <class IT, class NT, class DER>
void SpParMat<IT,NT,DER>::PrintInfo() const
{
IT mm = getnrow();
IT nn = getncol();
IT nznz = getnnz();
if (commGrid->myrank == 0)
std::cout << "As a whole: " << mm << " rows and "<< nn <<" columns and "<< nznz << " nonzeros" << std::endl;
#ifdef DEBUG
IT allprocs = commGrid->grrows * commGrid->grcols;
for(IT i=0; i< allprocs; ++i)
{
if (commGrid->myrank == i)
{
std::cout << "Processor (" << commGrid->GetRankInProcRow() << "," << commGrid->GetRankInProcCol() << ")'s data: " << std::endl;
spSeq->PrintInfo();
}
MPI_Barrier(commGrid->GetWorld());
}
#endif
}
template <class IT, class NT, class DER>
bool SpParMat<IT,NT,DER>::operator== (const SpParMat<IT,NT,DER> & rhs) const
{
int local = static_cast<int>((*spSeq) == (*(rhs.spSeq)));
int whole = 1;
MPI_Allreduce( &local, &whole, 1, MPI_INT, MPI_BAND, commGrid->GetWorld());
return static_cast<bool>(whole);
}
/**
** Private function that carries code common to different sparse() constructors
** Before this call, commGrid is already set
**/
template <class IT, class NT, class DER>
template <typename _BinaryOperation, typename LIT>
void SpParMat< IT,NT,DER >::SparseCommon(std::vector< std::vector < std::tuple<LIT,LIT,NT> > > & data, LIT locsize, IT total_m, IT total_n, _BinaryOperation BinOp)
//void SpParMat< IT,NT,DER >::SparseCommon(std::vector< std::vector < std::tuple<typename DER::LocalIT,typename DER::LocalIT,NT> > > & data, typename DER::LocalIT locsize, IT total_m, IT total_n, _BinaryOperation BinOp)
{
//typedef typename DER::LocalIT LIT;
int nprocs = commGrid->GetSize();
int * sendcnt = new int[nprocs];
int * recvcnt = new int[nprocs];
for(int i=0; i<nprocs; ++i)
sendcnt[i] = data[i].size(); // sizes are all the same
MPI_Alltoall(sendcnt, 1, MPI_INT, recvcnt, 1, MPI_INT, commGrid->GetWorld()); // share the counts
int * sdispls = new int[nprocs]();
int * rdispls = new int[nprocs]();
std::partial_sum(sendcnt, sendcnt+nprocs-1, sdispls+1);
std::partial_sum(recvcnt, recvcnt+nprocs-1, rdispls+1);
IT totrecv = std::accumulate(recvcnt,recvcnt+nprocs, static_cast<IT>(0));
IT totsent = std::accumulate(sendcnt,sendcnt+nprocs, static_cast<IT>(0));
assert((totsent < std::numeric_limits<int>::max()));
assert((totrecv < std::numeric_limits<int>::max()));
#if 0
ofstream oput;
commGrid->OpenDebugFile("Displacements", oput);
copy(sdispls, sdispls+nprocs, ostream_iterator<int>(oput, " ")); oput << endl;
copy(rdispls, rdispls+nprocs, ostream_iterator<int>(oput, " ")); oput << endl;
oput.close();
IT * gsizes;
if(commGrid->GetRank() == 0) gsizes = new IT[nprocs];
MPI_Gather(&totrecv, 1, MPIType<IT>(), gsizes, 1, MPIType<IT>(), 0, commGrid->GetWorld());
if(commGrid->GetRank() == 0) { std::copy(gsizes, gsizes+nprocs, std::ostream_iterator<IT>(std::cout, " ")); std::cout << std::endl; }
MPI_Barrier(commGrid->GetWorld());
MPI_Gather(&totsent, 1, MPIType<IT>(), gsizes, 1, MPIType<IT>(), 0, commGrid->GetWorld());
if(commGrid->GetRank() == 0) { copy(gsizes, gsizes+nprocs, ostream_iterator<IT>(cout, " ")); cout << endl; }
MPI_Barrier(commGrid->GetWorld());
if(commGrid->GetRank() == 0) delete [] gsizes;
#endif
std::tuple<LIT,LIT,NT> * senddata = new std::tuple<LIT,LIT,NT>[locsize]; // re-used for both rows and columns
for(int i=0; i<nprocs; ++i)
{
std::copy(data[i].begin(), data[i].end(), senddata+sdispls[i]);
data[i].clear(); // clear memory
data[i].shrink_to_fit();
}
MPI_Datatype MPI_triple;
MPI_Type_contiguous(sizeof(std::tuple<LIT,LIT,NT>), MPI_CHAR, &MPI_triple);
MPI_Type_commit(&MPI_triple);
std::tuple<LIT,LIT,NT> * recvdata = new std::tuple<LIT,LIT,NT>[totrecv];
MPI_Alltoallv(senddata, sendcnt, sdispls, MPI_triple, recvdata, recvcnt, rdispls, MPI_triple, commGrid->GetWorld());
DeleteAll(senddata, sendcnt, recvcnt, sdispls, rdispls);
MPI_Type_free(&MPI_triple);
int r = commGrid->GetGridRows();
int s = commGrid->GetGridCols();
IT m_perproc = total_m / r;
IT n_perproc = total_n / s;
int myprocrow = commGrid->GetRankInProcCol();
int myproccol = commGrid->GetRankInProcRow();
IT locrows, loccols;
if(myprocrow != r-1) locrows = m_perproc;
else locrows = total_m - myprocrow * m_perproc;
if(myproccol != s-1) loccols = n_perproc;
else loccols = total_n - myproccol * n_perproc;
SpTuples<LIT,NT> A(totrecv, locrows, loccols, recvdata); // It is ~SpTuples's job to deallocate
// the previous constructor sorts based on columns-first (but that doesn't matter as long as they are sorted one way or another)
A.RemoveDuplicates(BinOp);
spSeq = new DER(A,false); // Convert SpTuples to DER
}
template <class IT, class NT, class DER>
std::vector<std::vector<SpParMat<IT, NT, DER>>>
SpParMat<IT, NT, DER>::BlockSplit (int br, int bc)
{
IT g_nr = this->getnrow();
IT g_nc = this->getncol();
if (br == 1 && bc == 1 || (br > g_nr || bc > g_nc))
return std::vector<std::vector<SpParMat<IT, NT, DER>>>
(1, std::vector<SpParMat<IT, NT, DER>>(1, *this));
int np = commGrid->GetSize();
int rank = commGrid->GetRank();
std::vector<std::vector<SpParMat<IT, NT, DER>>>
bmats(br,
std::vector<SpParMat<IT, NT, DER>>
(bc, SpParMat<IT, NT, DER>(commGrid)));
std::vector<std::vector<std::vector<std::vector<std::tuple<IT, IT, NT>>>>>
btuples(br,
std::vector<std::vector<std::vector<std::tuple<IT, IT, NT>>>>
(bc, std::vector<std::vector<std::tuple<IT, IT, NT>>>
(np, std::vector<std::tuple<IT, IT, NT>>())));
assert(spSeq != NULL);
SpTuples<IT, NT> tuples(*spSeq);
IT g_rbeg = (g_nr/commGrid->GetGridRows()) * commGrid->GetRankInProcCol();
IT g_cbeg = (g_nc/commGrid->GetGridCols()) * commGrid->GetRankInProcRow();
IT br_sz = g_nr / br;
IT br_r = g_nr % br;
IT bc_sz = g_nc / bc;
IT bc_r = g_nc % bc;
std::vector<IT> br_sizes(br, br_sz);
std::vector<IT> bc_sizes(bc, bc_sz);
for (IT i = 0; i < br_r; ++i)
++br_sizes[i];
for (IT i = 0; i < bc_r; ++i)
++bc_sizes[i];
auto get_block = [](IT x, IT sz, IT r, IT &bid, IT &idx)
{
if (x < (r*(sz+1)))
{
bid = x / (sz+1);
idx = x % (sz+1);
}
else
{
bid = (x-r) / sz;
idx = (x-r) % sz;
}
};
// gather tuples
for (int64_t i = 0; i < tuples.getnnz(); ++i)
{
IT g_ridx = g_rbeg + tuples.rowindex(i);
IT g_cidx = g_cbeg + tuples.colindex(i);
IT rbid, ridx, ridx_l, cbid, cidx, cidx_l;
get_block(g_ridx, br_sz, br_r, rbid, ridx);
get_block(g_cidx, bc_sz, bc_r, cbid, cidx);
int owner = Owner(br_sizes[rbid], bc_sizes[cbid], ridx, cidx,
ridx_l, cidx_l);
btuples[rbid][cbid][owner].push_back({ridx_l, cidx_l, tuples.numvalue(i)});
}
// form matrices
for (int i = 0; i < br; ++i)
{
for (int j = 0; j < bc; ++j)
{
IT locsize = 0;
for (auto &el : btuples[i][j])
locsize += el.size();
auto &M = bmats[i][j];
M.SparseCommon(btuples[i][j], locsize, br_sizes[i], bc_sizes[j],
maximum<NT>()); // there are no duplicates
}
}
return bmats;
}
//! All vectors are zero-based indexed (as usual)
template <class IT, class NT, class DER>
SpParMat< IT,NT,DER >::SpParMat (IT total_m, IT total_n, const FullyDistVec<IT,IT> & distrows,
const FullyDistVec<IT,IT> & distcols, const FullyDistVec<IT,NT> & distvals, bool SumDuplicates)
{
if((*(distrows.commGrid) != *(distcols.commGrid)) || (*(distcols.commGrid) != *(distvals.commGrid)))
{
SpParHelper::Print("Grids are not comparable, Sparse() fails!\n"); // commGrid is not initialized yet
MPI_Abort(MPI_COMM_WORLD, GRIDMISMATCH);
}
if((distrows.TotalLength() != distcols.TotalLength()) || (distcols.TotalLength() != distvals.TotalLength()))
{
SpParHelper::Print("Vectors have different sizes, Sparse() fails!");
MPI_Abort(MPI_COMM_WORLD, DIMMISMATCH);
}
commGrid = distrows.commGrid;
int nprocs = commGrid->GetSize();
std::vector< std::vector < std::tuple<IT,IT,NT> > > data(nprocs);
IT locsize = distrows.LocArrSize();
for(IT i=0; i<locsize; ++i)
{
IT lrow, lcol;
int owner = Owner(total_m, total_n, distrows.arr[i], distcols.arr[i], lrow, lcol);
data[owner].push_back(std::make_tuple(lrow,lcol,distvals.arr[i]));
}
if(SumDuplicates)
{
SparseCommon(data, locsize, total_m, total_n, std::plus<NT>());
}
else
{
SparseCommon(data, locsize, total_m, total_n, maximum<NT>());
}
}
template <class IT, class NT, class DER>
SpParMat< IT,NT,DER >::SpParMat (IT total_m, IT total_n, const FullyDistVec<IT,IT> & distrows,
const FullyDistVec<IT,IT> & distcols, const NT & val, bool SumDuplicates)
{
if((*(distrows.commGrid) != *(distcols.commGrid)) )
{
SpParHelper::Print("Grids are not comparable, Sparse() fails!\n");
MPI_Abort(MPI_COMM_WORLD, GRIDMISMATCH);
}
if((distrows.TotalLength() != distcols.TotalLength()) )
{
SpParHelper::Print("Vectors have different sizes, Sparse() fails!\n");
MPI_Abort(MPI_COMM_WORLD, DIMMISMATCH);
}
commGrid = distrows.commGrid;
int nprocs = commGrid->GetSize();
std::vector< std::vector < std::tuple<IT,IT,NT> > > data(nprocs);
IT locsize = distrows.LocArrSize();
for(IT i=0; i<locsize; ++i)
{
IT lrow, lcol;
int owner = Owner(total_m, total_n, distrows.arr[i], distcols.arr[i], lrow, lcol);
data[owner].push_back(std::make_tuple(lrow,lcol,val));
}
if(SumDuplicates)
{
SparseCommon(data, locsize, total_m, total_n, std::plus<NT>());
}
else
{
SparseCommon(data, locsize, total_m, total_n, maximum<NT>());
}
}
template <class IT, class NT, class DER>
template <class DELIT>
SpParMat< IT,NT,DER >::SpParMat (const DistEdgeList<DELIT> & DEL, bool removeloops)
{
commGrid = DEL.commGrid;
typedef typename DER::LocalIT LIT;
int nprocs = commGrid->GetSize();
int gridrows = commGrid->GetGridRows();
int gridcols = commGrid->GetGridCols();
std::vector< std::vector<LIT> > data(nprocs); // enties are pre-converted to local indices before getting pushed into "data"
LIT m_perproc = DEL.getGlobalV() / gridrows;
LIT n_perproc = DEL.getGlobalV() / gridcols;
if(sizeof(LIT) < sizeof(DELIT))
{
std::ostringstream outs;
outs << "Warning: Using smaller indices for the matrix than DistEdgeList\n";
outs << "Local matrices are " << m_perproc << "-by-" << n_perproc << std::endl;
SpParHelper::Print(outs.str(), commGrid->GetWorld()); // commgrid initialized
}
LIT stages = MEM_EFFICIENT_STAGES; // to lower memory consumption, form sparse matrix in stages
// even if local indices (LIT) are 32-bits, we should work with 64-bits for global info
int64_t perstage = DEL.nedges / stages;
LIT totrecv = 0;
std::vector<LIT> alledges;
for(LIT s=0; s< stages; ++s)
{
int64_t n_befor = s*perstage;
int64_t n_after= ((s==(stages-1))? DEL.nedges : ((s+1)*perstage));
// clear the source vertex by setting it to -1
int realedges = 0; // these are "local" realedges
if(DEL.pedges)
{
for (int64_t i = n_befor; i < n_after; i++)
{
int64_t fr = get_v0_from_edge(&(DEL.pedges[i]));
int64_t to = get_v1_from_edge(&(DEL.pedges[i]));
if(fr >= 0 && to >= 0) // otherwise skip
{
IT lrow, lcol;
int owner = Owner(DEL.getGlobalV(), DEL.getGlobalV(), fr, to, lrow, lcol);
data[owner].push_back(lrow); // row_id
data[owner].push_back(lcol); // col_id
++realedges;
}
}
}
else
{
for (int64_t i = n_befor; i < n_after; i++)
{
if(DEL.edges[2*i+0] >= 0 && DEL.edges[2*i+1] >= 0) // otherwise skip
{
IT lrow, lcol;
int owner = Owner(DEL.getGlobalV(), DEL.getGlobalV(), DEL.edges[2*i+0], DEL.edges[2*i+1], lrow, lcol);
data[owner].push_back(lrow);
data[owner].push_back(lcol);
++realedges;
}
}
}
LIT * sendbuf = new LIT[2*realedges];
int * sendcnt = new int[nprocs];
int * sdispls = new int[nprocs];
for(int i=0; i<nprocs; ++i)
sendcnt[i] = data[i].size();
int * rdispls = new int[nprocs];
int * recvcnt = new int[nprocs];
MPI_Alltoall(sendcnt, 1, MPI_INT, recvcnt, 1, MPI_INT,commGrid->GetWorld()); // share the counts
sdispls[0] = 0;
rdispls[0] = 0;
for(int i=0; i<nprocs-1; ++i)
{
sdispls[i+1] = sdispls[i] + sendcnt[i];
rdispls[i+1] = rdispls[i] + recvcnt[i];
}
for(int i=0; i<nprocs; ++i)
std::copy(data[i].begin(), data[i].end(), sendbuf+sdispls[i]);
// clear memory
for(int i=0; i<nprocs; ++i)
std::vector<LIT>().swap(data[i]);
// ABAB: Total number of edges received might not be LIT-addressible
// However, each edge_id is LIT-addressible
IT thisrecv = std::accumulate(recvcnt,recvcnt+nprocs, static_cast<IT>(0)); // thisrecv = 2*locedges
LIT * recvbuf = new LIT[thisrecv];
totrecv += thisrecv;
MPI_Alltoallv(sendbuf, sendcnt, sdispls, MPIType<LIT>(), recvbuf, recvcnt, rdispls, MPIType<LIT>(), commGrid->GetWorld());
DeleteAll(sendcnt, recvcnt, sdispls, rdispls,sendbuf);
std::copy (recvbuf,recvbuf+thisrecv,std::back_inserter(alledges)); // copy to all edges
delete [] recvbuf;
}
int myprocrow = commGrid->GetRankInProcCol();
int myproccol = commGrid->GetRankInProcRow();
LIT locrows, loccols;
if(myprocrow != gridrows-1) locrows = m_perproc;
else locrows = DEL.getGlobalV() - myprocrow * m_perproc;
if(myproccol != gridcols-1) loccols = n_perproc;
else loccols = DEL.getGlobalV() - myproccol * n_perproc;
SpTuples<LIT,NT> A(totrecv/2, locrows, loccols, alledges, removeloops); // alledges is empty upon return
spSeq = new DER(A,false); // Convert SpTuples to DER
}
template <class IT, class NT, class DER>
IT SpParMat<IT,NT,DER>::RemoveLoops()
{
MPI_Comm DiagWorld = commGrid->GetDiagWorld();
IT totrem;
IT removed = 0;
if(DiagWorld != MPI_COMM_NULL) // Diagonal processors only
{
typedef typename DER::LocalIT LIT;
SpTuples<LIT,NT> tuples(*spSeq);
delete spSeq;
removed = tuples.RemoveLoops();
spSeq = new DER(tuples, false); // Convert to DER
}
MPI_Allreduce( &removed, & totrem, 1, MPIType<IT>(), MPI_SUM, commGrid->GetWorld());
return totrem;
}
template <class IT, class NT, class DER>
void SpParMat<IT,NT,DER>::AddLoops(NT loopval, bool replaceExisting)
{
MPI_Comm DiagWorld = commGrid->GetDiagWorld();
if(DiagWorld != MPI_COMM_NULL) // Diagonal processors only
{
typedef typename DER::LocalIT LIT;
SpTuples<LIT,NT> tuples(*spSeq);
delete spSeq;
tuples.AddLoops(loopval, replaceExisting);
tuples.SortColBased();
spSeq = new DER(tuples, false); // Convert to DER
}
}
// Different values on the diagonal
template <class IT, class NT, class DER>
void SpParMat<IT,NT,DER>::AddLoops(FullyDistVec<IT,NT> loopvals, bool replaceExisting)
{
if(*loopvals.commGrid != *commGrid)
{
SpParHelper::Print("Grids are not comparable, SpParMat::AddLoops() fails!\n", commGrid->GetWorld());
MPI_Abort(MPI_COMM_WORLD,GRIDMISMATCH);
}
if (getncol()!= loopvals.TotalLength())
{
SpParHelper::Print("The number of entries in loopvals is not equal to the number of diagonal entries.\n");
MPI_Abort(MPI_COMM_WORLD,DIMMISMATCH);
}
// Gather data on the diagonal processor
IT locsize = loopvals.LocArrSize();
int rowProcs = commGrid->GetGridCols();
std::vector<int> recvcnt(rowProcs, 0);
std::vector<int> rdpls(rowProcs, 0);
MPI_Gather(&locsize, 1, MPI_INT, recvcnt.data(), 1, MPI_INT, commGrid->GetDiagOfProcRow(), commGrid->GetRowWorld());
std::partial_sum(recvcnt.data(), recvcnt.data()+rowProcs-1, rdpls.data()+1);
IT totrecv = rdpls[rowProcs-1] + recvcnt[rowProcs-1];
assert((totrecv < std::numeric_limits<int>::max()));
std::vector<NT> rowvals(totrecv);
MPI_Gatherv(loopvals.arr.data(), locsize, MPIType<NT>(), rowvals.data(), recvcnt.data(), rdpls.data(),
MPIType<NT>(), commGrid->GetDiagOfProcRow(), commGrid->GetRowWorld());
MPI_Comm DiagWorld = commGrid->GetDiagWorld();
if(DiagWorld != MPI_COMM_NULL) // Diagonal processors only
{
typedef typename DER::LocalIT LIT;
SpTuples<LIT,NT> tuples(*spSeq);
delete spSeq;
tuples.AddLoops(rowvals, replaceExisting);
tuples.SortColBased();
spSeq = new DER(tuples, false); // Convert to DER
}
}
//! Pre-allocates buffers for row communication
//! additionally (if GATHERVOPT is defined, incomplete as of March 2016):
//! - Splits the local column indices to sparse & dense pieces to avoid redundant AllGather (sparse pieces get p2p)
template <class IT, class NT, class DER>
template <typename LIT, typename OT>
void SpParMat<IT,NT,DER>::OptimizeForGraph500(OptBuf<LIT,OT> & optbuf)
{
if(spSeq->getnsplit() > 0)
{
SpParHelper::Print("Can not declare preallocated buffers for multithreaded execution\n", commGrid->GetWorld());
return;
}
typedef typename DER::LocalIT LocIT; // ABAB: should match the type of LIT. Check?
// Set up communication buffers, one for all
LocIT mA = spSeq->getnrow();
LocIT nA = spSeq->getncol();
int p_c = commGrid->GetGridCols();
int p_r = commGrid->GetGridRows();
LocIT rwperproc = mA / p_c; // per processors in row-wise communication
LocIT cwperproc = nA / p_r; // per processors in column-wise communication
#ifdef GATHERVOPT
LocIT * colinds = seq->GetDCSC()->jc; // local nonzero column id's
LocIT locnzc = seq->getnzc();
LocIT cci = 0; // index to column id's array (cci: current column index)
int * gsizes = NULL;
IT * ents = NULL;
IT * dpls = NULL;
std::vector<LocIT> pack2send;
FullyDistSpVec<IT,IT> dummyRHS ( commGrid, getncol()); // dummy RHS vector to estimate index start position
IT recveclen;
for(int pid = 1; pid <= p_r; pid++)
{
IT diagoffset;
MPI_Status status;
IT offset = dummyRHS.RowLenUntil(pid-1);
int diagneigh = commGrid->GetComplementRank();
MPI_Sendrecv(&offset, 1, MPIType<IT>(), diagneigh, TRTAGNZ, &diagoffset, 1, MPIType<IT>(), diagneigh, TRTAGNZ, commGrid->GetWorld(), &status);
LocIT endind = (pid == p_r)? nA : static_cast<LocIT>(pid) * cwperproc; // the last one might have a larger share (is this fitting to the vector boundaries?)
while(cci < locnzc && colinds[cci] < endind)
{
pack2send.push_back(colinds[cci++]-diagoffset);
}
if(pid-1 == myrank) gsizes = new int[p_r];
MPI_Gather(&mysize, 1, MPI_INT, gsizes, 1, MPI_INT, pid-1, commGrid->GetColWorld());
if(pid-1 == myrank)
{
IT colcnt = std::accumulate(gsizes, gsizes+p_r, static_cast<IT>(0));
recvbuf = new IT[colcnt];
dpls = new IT[p_r](); // displacements (zero initialized pid)
std::partial_sum(gsizes, gsizes+p_r-1, dpls+1);
}
// int MPI_Gatherv (void* sbuf, int scount, MPI_Datatype stype, void* rbuf, int *rcount, int* displs, MPI_Datatype rtype, int root, MPI_Comm comm)
MPI_Gatherv(SpHelper::p2a(pack2send), mysize, MPIType<LocIT>(), recvbuf, gsizes, dpls, MPIType<LocIT>(), pid-1, commGrid->GetColWorld());
std::vector<LocIT>().swap(pack2send);
if(pid-1 == myrank)
{
recveclen = dummyRHS.MyLocLength();
std::vector< std::vector<LocIT> > service(recveclen);
for(int i=0; i< p_r; ++i)
{
for(int j=0; j< gsizes[i]; ++j)
{
IT colid2update = recvbuf[dpls[i]+j];
if(service[colid2update].size() < GATHERVNEIGHLIMIT)
{
service.push_back(i);
}
// else don't increase any further and mark it done after the iterations are complete
}
}
}
}
#endif
std::vector<bool> isthere(mA, false); // perhaps the only appropriate use of this crippled data structure
std::vector<int> maxlens(p_c,0); // maximum data size to be sent to any neighbor along the processor row
for(typename DER::SpColIter colit = spSeq->begcol(); colit != spSeq->endcol(); ++colit)
{
for(typename DER::SpColIter::NzIter nzit = spSeq->begnz(colit); nzit != spSeq->endnz(colit); ++nzit)
{
LocIT rowid = nzit.rowid();
if(!isthere[rowid])
{
LocIT owner = std::min(nzit.rowid() / rwperproc, (LocIT) p_c-1);
maxlens[owner]++;
isthere[rowid] = true;
}
}
}
SpParHelper::Print("Optimization buffers set\n", commGrid->GetWorld());
optbuf.Set(maxlens,mA);
}
template <class IT, class NT, class DER>
void SpParMat<IT,NT,DER>::ActivateThreading(int numsplits)
{
spSeq->RowSplit(numsplits);
}
/**
* Parallel routine that returns A*A on the semiring SR
* Uses only MPI-1 features (relies on simple blocking broadcast)
**/
template <class IT, class NT, class DER>
template <typename SR>
void SpParMat<IT,NT,DER>::Square ()
{
int stages, dummy; // last two parameters of productgrid are ignored for synchronous multiplication
std::shared_ptr<CommGrid> Grid = ProductGrid(commGrid.get(), commGrid.get(), stages, dummy, dummy);
typedef typename DER::LocalIT LIT;
LIT AA_m = spSeq->getnrow();
LIT AA_n = spSeq->getncol();
DER seqTrn = spSeq->TransposeConst(); // will be automatically discarded after going out of scope
MPI_Barrier(commGrid->GetWorld());
LIT ** NRecvSizes = SpHelper::allocate2D<LIT>(DER::esscount, stages);
LIT ** TRecvSizes = SpHelper::allocate2D<LIT>(DER::esscount, stages);
SpParHelper::GetSetSizes( *spSeq, NRecvSizes, commGrid->GetRowWorld());
SpParHelper::GetSetSizes( seqTrn, TRecvSizes, commGrid->GetColWorld());
// Remotely fetched matrices are stored as pointers
DER * NRecv;
DER * TRecv;
std::vector< SpTuples<LIT,NT> *> tomerge;
int Nself = commGrid->GetRankInProcRow();
int Tself = commGrid->GetRankInProcCol();
for(int i = 0; i < stages; ++i)
{
std::vector<LIT> ess;
if(i == Nself) NRecv = spSeq; // shallow-copy
else
{
ess.resize(DER::esscount);
for(int j=0; j< DER::esscount; ++j)
ess[j] = NRecvSizes[j][i]; // essentials of the ith matrix in this row
NRecv = new DER(); // first, create the object
}
SpParHelper::BCastMatrix(Grid->GetRowWorld(), *NRecv, ess, i); // then, broadcast its elements
ess.clear();
if(i == Tself) TRecv = &seqTrn; // shallow-copy
else
{
ess.resize(DER::esscount);
for(int j=0; j< DER::esscount; ++j)
ess[j] = TRecvSizes[j][i];
TRecv = new DER();
}
SpParHelper::BCastMatrix(Grid->GetColWorld(), *TRecv, ess, i);
SpTuples<LIT,NT> * AA_cont = MultiplyReturnTuples<SR, NT>(*NRecv, *TRecv, false, true);
if(!AA_cont->isZero())
tomerge.push_back(AA_cont);
if(i != Nself) delete NRecv;
if(i != Tself) delete TRecv;
}
SpHelper::deallocate2D(NRecvSizes, DER::esscount);
SpHelper::deallocate2D(TRecvSizes, DER::esscount);
delete spSeq;
spSeq = new DER(MergeAll<SR>(tomerge, AA_m, AA_n), false); // First get the result in SpTuples, then convert to UDER
for(unsigned int i=0; i<tomerge.size(); ++i)
delete tomerge[i];
}
template <class IT, class NT, class DER>
void SpParMat<IT,NT,DER>::Transpose()
{
if(commGrid->myproccol == commGrid->myprocrow) // Diagonal
{
spSeq->Transpose();
}
else
{
typedef typename DER::LocalIT LIT;
SpTuples<LIT,NT> Atuples(*spSeq);
LIT locnnz = Atuples.getnnz();
LIT * rows = new LIT[locnnz];
LIT * cols = new LIT[locnnz];
NT * vals = new NT[locnnz];
for(LIT i=0; i < locnnz; ++i)
{
rows[i] = Atuples.colindex(i); // swap (i,j) here
cols[i] = Atuples.rowindex(i);
vals[i] = Atuples.numvalue(i);
}
LIT locm = getlocalcols();
LIT locn = getlocalrows();
delete spSeq;
LIT remotem, remoten, remotennz;
std::swap(locm,locn);
int diagneigh = commGrid->GetComplementRank();
MPI_Status status;
MPI_Sendrecv(&locnnz, 1, MPIType<LIT>(), diagneigh, TRTAGNZ, &remotennz, 1, MPIType<LIT>(), diagneigh, TRTAGNZ, commGrid->GetWorld(), &status);
MPI_Sendrecv(&locn, 1, MPIType<LIT>(), diagneigh, TRTAGM, &remotem, 1, MPIType<LIT>(), diagneigh, TRTAGM, commGrid->GetWorld(), &status);
MPI_Sendrecv(&locm, 1, MPIType<LIT>(), diagneigh, TRTAGN, &remoten, 1, MPIType<LIT>(), diagneigh, TRTAGN, commGrid->GetWorld(), &status);
LIT * rowsrecv = new LIT[remotennz];
MPI_Sendrecv(rows, locnnz, MPIType<LIT>(), diagneigh, TRTAGROWS, rowsrecv, remotennz, MPIType<LIT>(), diagneigh, TRTAGROWS, commGrid->GetWorld(), &status);
delete [] rows;
LIT * colsrecv = new LIT[remotennz];
MPI_Sendrecv(cols, locnnz, MPIType<LIT>(), diagneigh, TRTAGCOLS, colsrecv, remotennz, MPIType<LIT>(), diagneigh, TRTAGCOLS, commGrid->GetWorld(), &status);
delete [] cols;
NT * valsrecv = new NT[remotennz];
MPI_Sendrecv(vals, locnnz, MPIType<NT>(), diagneigh, TRTAGVALS, valsrecv, remotennz, MPIType<NT>(), diagneigh, TRTAGVALS, commGrid->GetWorld(), &status);
delete [] vals;
std::tuple<LIT,LIT,NT> * arrtuples = new std::tuple<LIT,LIT,NT>[remotennz];
for(LIT i=0; i< remotennz; ++i)
{
arrtuples[i] = std::make_tuple(rowsrecv[i], colsrecv[i], valsrecv[i]);
}
DeleteAll(rowsrecv, colsrecv, valsrecv);
ColLexiCompare<LIT,NT> collexicogcmp;
sort(arrtuples , arrtuples+remotennz, collexicogcmp ); // sort w.r.t columns here
spSeq = new DER();
spSeq->Create( remotennz, remotem, remoten, arrtuples); // the deletion of arrtuples[] is handled by SpMat::Create
}
}
template <class IT, class NT, class DER>
template <class HANDLER>
void SpParMat< IT,NT,DER >::SaveGathered(std::string filename, HANDLER handler, bool transpose) const
{
int proccols = commGrid->GetGridCols();
int procrows = commGrid->GetGridRows();
IT totalm = getnrow();
IT totaln = getncol();
IT totnnz = getnnz();
int flinelen = 0;
std::ofstream out;
if(commGrid->GetRank() == 0)
{
std::string s;
std::stringstream strm;
strm << "%%MatrixMarket matrix coordinate real general" << std::endl;
strm << totalm << " " << totaln << " " << totnnz << std::endl;
s = strm.str();
out.open(filename.c_str(),std::ios_base::trunc);
flinelen = s.length();
out.write(s.c_str(), flinelen);
out.close();
}
int colrank = commGrid->GetRankInProcCol();
int colneighs = commGrid->GetGridRows();
IT * locnrows = new IT[colneighs]; // number of rows is calculated by a reduction among the processor column
locnrows[colrank] = (IT) getlocalrows();
MPI_Allgather(MPI_IN_PLACE, 0, MPIType<IT>(),locnrows, 1, MPIType<IT>(), commGrid->GetColWorld());
IT roffset = std::accumulate(locnrows, locnrows+colrank, 0);
delete [] locnrows;
MPI_Datatype datatype;
MPI_Type_contiguous(sizeof(std::pair<IT,NT>), MPI_CHAR, &datatype);
MPI_Type_commit(&datatype);
for(int i = 0; i < procrows; i++) // for all processor row (in order)
{
if(commGrid->GetRankInProcCol() == i) // only the ith processor row
{
IT localrows = spSeq->getnrow(); // same along the processor row
std::vector< std::vector< std::pair<IT,NT> > > csr(localrows);
if(commGrid->GetRankInProcRow() == 0) // get the head of processor row
{
IT localcols = spSeq->getncol(); // might be different on the last processor on this processor row
MPI_Bcast(&localcols, 1, MPIType<IT>(), 0, commGrid->GetRowWorld());
for(typename DER::SpColIter colit = spSeq->begcol(); colit != spSeq->endcol(); ++colit) // iterate over nonempty subcolumns
{
for(typename DER::SpColIter::NzIter nzit = spSeq->begnz(colit); nzit != spSeq->endnz(colit); ++nzit)
{
csr[nzit.rowid()].push_back( std::make_pair(colit.colid(), nzit.value()) );
}
}
}
else // get the rest of the processors
{
IT n_perproc;
MPI_Bcast(&n_perproc, 1, MPIType<IT>(), 0, commGrid->GetRowWorld());
IT noffset = commGrid->GetRankInProcRow() * n_perproc;
for(typename DER::SpColIter colit = spSeq->begcol(); colit != spSeq->endcol(); ++colit) // iterate over nonempty subcolumns
{
for(typename DER::SpColIter::NzIter nzit = spSeq->begnz(colit); nzit != spSeq->endnz(colit); ++nzit)
{
csr[nzit.rowid()].push_back( std::make_pair(colit.colid() + noffset, nzit.value()) );
}
}
}
std::pair<IT,NT> * ents = NULL;
int * gsizes = NULL, * dpls = NULL;
if(commGrid->GetRankInProcRow() == 0) // only the head of processor row
{
out.open(filename.c_str(),std::ios_base::app);
gsizes = new int[proccols];
dpls = new int[proccols](); // displacements (zero initialized pid)
}
for(int j = 0; j < localrows; ++j)
{
IT rowcnt = 0;
sort(csr[j].begin(), csr[j].end());
int mysize = csr[j].size();
MPI_Gather(&mysize, 1, MPI_INT, gsizes, 1, MPI_INT, 0, commGrid->GetRowWorld());
if(commGrid->GetRankInProcRow() == 0)
{
rowcnt = std::accumulate(gsizes, gsizes+proccols, static_cast<IT>(0));
std::partial_sum(gsizes, gsizes+proccols-1, dpls+1);
ents = new std::pair<IT,NT>[rowcnt]; // nonzero entries in the j'th local row
}
// int MPI_Gatherv (void* sbuf, int scount, MPI_Datatype stype,
// void* rbuf, int *rcount, int* displs, MPI_Datatype rtype, int root, MPI_Comm comm)
MPI_Gatherv(SpHelper::p2a(csr[j]), mysize, datatype, ents, gsizes, dpls, datatype, 0, commGrid->GetRowWorld());
if(commGrid->GetRankInProcRow() == 0)
{
for(int k=0; k< rowcnt; ++k)
{
//out << j + roffset + 1 << "\t" << ents[k].first + 1 <<"\t" << ents[k].second << endl;
if (!transpose)
// regular
out << j + roffset + 1 << "\t" << ents[k].first + 1 << "\t";
else
// transpose row/column
out << ents[k].first + 1 << "\t" << j + roffset + 1 << "\t";
handler.save(out, ents[k].second, j + roffset, ents[k].first);
out << std::endl;
}
delete [] ents;
}
}
if(commGrid->GetRankInProcRow() == 0)
{
DeleteAll(gsizes, dpls);
out.close();
}
} // end_if the ith processor row
MPI_Barrier(commGrid->GetWorld()); // signal the end of ith processor row iteration (so that all processors block)
}
}
//! Private subroutine of ReadGeneralizedTuples
//! totallength is the length of the dictionary, which we don't know in this labeled tuples format apriori
template <class IT, class NT, class DER>
MPI_File SpParMat< IT,NT,DER >::TupleRead1stPassNExchange (const std::string & filename, TYPE2SEND * & senddata, IT & totsend,
FullyDistVec<IT,STRASARRAY> & distmapper, uint64_t & totallength)
{
int myrank = commGrid->GetRank();
int nprocs = commGrid->GetSize();
MPI_Offset fpos, end_fpos;
struct stat st; // get file size
if (stat(filename.c_str(), &st) == -1)
{
MPI_Abort(MPI_COMM_WORLD, NOFILE);
}
int64_t file_size = st.st_size;
if(myrank == 0) // the offset needs to be for this rank
{
std::cout << "File is " << file_size << " bytes" << std::endl;
}
fpos = myrank * file_size / nprocs;
if(myrank != (nprocs-1)) end_fpos = (myrank + 1) * file_size / nprocs;
else end_fpos = file_size;
MPI_File mpi_fh;
MPI_File_open (commGrid->commWorld, const_cast<char*>(filename.c_str()), MPI_MODE_RDONLY, MPI_INFO_NULL, &mpi_fh);
typedef std::map<std::string, uint64_t> KEYMAP; // due to potential (but extremely unlikely) collusions in MurmurHash, make the key to the std:map the string itself
std::vector< KEYMAP > allkeys(nprocs); // map keeps the outgoing data unique, we could have applied this to HipMer too
std::vector<std::string> lines;
bool finished = SpParHelper::FetchBatch(mpi_fh, fpos, end_fpos, true, lines, myrank);
int64_t entriesread = lines.size();
SpHelper::ProcessLinesWithStringKeys(allkeys, lines,nprocs);
while(!finished)
{
finished = SpParHelper::FetchBatch(mpi_fh, fpos, end_fpos, false, lines, myrank);
entriesread += lines.size();
SpHelper::ProcessLinesWithStringKeys(allkeys, lines,nprocs);
}
int64_t allentriesread;
MPI_Reduce(&entriesread, &allentriesread, 1, MPIType<int64_t>(), MPI_SUM, 0, commGrid->commWorld);
#ifdef COMBBLAS_DEBUG
if(myrank == 0)
std::cout << "Initial reading finished. Total number of entries read across all processors is " << allentriesread << std::endl;
#endif
int * sendcnt = new int[nprocs];
int * recvcnt = new int[nprocs];
for(int i=0; i<nprocs; ++i)
sendcnt[i] = allkeys[i].size();
MPI_Alltoall(sendcnt, 1, MPI_INT, recvcnt, 1, MPI_INT, commGrid->GetWorld()); // share the counts
int * sdispls = new int[nprocs]();
int * rdispls = new int[nprocs]();
std::partial_sum(sendcnt, sendcnt+nprocs-1, sdispls+1);
std::partial_sum(recvcnt, recvcnt+nprocs-1, rdispls+1);
totsend = std::accumulate(sendcnt,sendcnt+nprocs, static_cast<IT>(0));
IT totrecv = std::accumulate(recvcnt,recvcnt+nprocs, static_cast<IT>(0));
assert((totsend < std::numeric_limits<int>::max()));
assert((totrecv < std::numeric_limits<int>::max()));
// The following are declared in SpParMat.h
// typedef std::array<char, MAXVERTNAME> STRASARRAY;
// typedef std::pair< STRASARRAY, uint64_t> TYPE2SEND;
senddata = new TYPE2SEND[totsend];
#pragma omp parallel for
for(int i=0; i<nprocs; ++i)
{
size_t j = 0;
for(auto pobj:allkeys[i])
{
// The naked C-style array type is not copyable or assignable, but pair will require it, hence used std::array
std::array<char, MAXVERTNAME> vname;
std::copy( pobj.first.begin(), pobj.first.end(), vname.begin() );
if(pobj.first.length() < MAXVERTNAME) vname[pobj.first.length()] = '\0'; // null termination
senddata[sdispls[i]+j] = TYPE2SEND(vname, pobj.second);
j++;
}
}
allkeys.clear(); // allkeys is no longer needed after this point
MPI_Datatype MPI_HASH;
MPI_Type_contiguous(sizeof(TYPE2SEND), MPI_CHAR, &MPI_HASH);
MPI_Type_commit(&MPI_HASH);
TYPE2SEND * recvdata = new TYPE2SEND[totrecv];
MPI_Alltoallv(senddata, sendcnt, sdispls, MPI_HASH, recvdata, recvcnt, rdispls, MPI_HASH, commGrid->GetWorld());
// do not delete send buffers yet as we will use them to recv back the data
std::set< std::pair<uint64_t, std::string> > uniqsorted;
for(IT i=0; i< totrecv; ++i)
{
auto locnull = std::find(recvdata[i].first.begin(), recvdata[i].first.end(), '\0'); // find the null character (or string::end)
std::string strtmp(recvdata[i].first.begin(), locnull); // range constructor
uniqsorted.insert(std::make_pair(recvdata[i].second, strtmp));
}
uint64_t uniqsize = uniqsorted.size();
#ifdef COMBBLAS_DEBUG
if(myrank == 0)
std::cout << "out of " << totrecv << " vertices received, " << uniqsize << " were unique" << std::endl;
#endif
uint64_t sizeuntil = 0;
totallength = 0;
MPI_Exscan( &uniqsize, &sizeuntil, 1, MPIType<uint64_t>(), MPI_SUM, commGrid->GetWorld() );
MPI_Allreduce(&uniqsize, &totallength, 1, MPIType<uint64_t>(), MPI_SUM, commGrid->GetWorld());
if(myrank == 0) sizeuntil = 0; // because MPI_Exscan says the recvbuf in process 0 is undefined
distmapper = FullyDistVec<IT,STRASARRAY>(commGrid, totallength,STRASARRAY{});
// invindex does not conform to FullyDistVec boundaries, otherwise its contents are essentially the same as distmapper
KEYMAP invindex; // KEYMAP is map<string, uint64_t>.
uint64_t locindex = 0;
std::vector< std::vector< IT > > locs_send(nprocs);
std::vector< std::vector< std::string > > data_send(nprocs);
int * map_scnt = new int[nprocs](); // send counts for this map only (to no confuse with the other sendcnt)
for(auto itr = uniqsorted.begin(); itr != uniqsorted.end(); ++itr)
{
uint64_t globalindex = sizeuntil + locindex;
invindex.insert(std::make_pair(itr->second, globalindex));
IT newlocid;
int owner = distmapper.Owner(globalindex, newlocid);
//if(myrank == 0)
// std::cout << "invindex received " << itr->second << " with global index " << globalindex << " to be owned by " << owner << " with index " << newlocid << std::endl;
locs_send[owner].push_back(newlocid);
data_send[owner].push_back(itr->second);
map_scnt[owner]++;
locindex++;
}
uniqsorted.clear(); // clear memory
/* BEGIN: Redistributing the permutation vector to fit the FullyDistVec semantics */
SpParHelper::ReDistributeToVector(map_scnt, locs_send, data_send, distmapper.arr, commGrid->GetWorld()); // map_scnt is deleted here
/* END: Redistributing the permutation vector to fit the FullyDistVec semantics */
for(IT i=0; i< totrecv; ++i)
{
auto locnull = std::find(recvdata[i].first.begin(), recvdata[i].first.end(), '\0');
std::string searchstr(recvdata[i].first.begin(), locnull); // range constructor
auto resp = invindex.find(searchstr); // recvdata[i] is of type pair< STRASARRAY, uint64_t>
if (resp != invindex.end())
{
recvdata[i].second = resp->second; // now instead of random numbers, recvdata's second entry will be its new index
}
else
std::cout << "Assertion failed at proc " << myrank << ": the absence of the entry in invindex is unexpected!!!" << std::endl;
}
MPI_Alltoallv(recvdata, recvcnt, rdispls, MPI_HASH, senddata, sendcnt, sdispls, MPI_HASH, commGrid->GetWorld());
DeleteAll(recvdata, sendcnt, recvcnt, sdispls, rdispls);
MPI_Type_free(&MPI_HASH);
// the following gets deleted here: allkeys
return mpi_fh;
}
//! Handles all sorts of orderings as long as there are no duplicates
//! Does not take matrix market banner (only tuples)
//! Data can be load imbalanced and the vertex labels can be arbitrary strings
//! Replaces ReadDistribute for imbalanced arbitrary input in tuples format
template <class IT, class NT, class DER>
template <typename _BinaryOperation>
FullyDistVec<IT,std::array<char, MAXVERTNAME> > SpParMat< IT,NT,DER >::ReadGeneralizedTuples (const std::string & filename, _BinaryOperation BinOp)
{
int myrank = commGrid->GetRank();
int nprocs = commGrid->GetSize();
TYPE2SEND * senddata;
IT totsend;
uint64_t totallength;
FullyDistVec<IT,STRASARRAY> distmapper(commGrid); // choice of array<char, MAXVERTNAME> over string = array is required to be a contiguous container and an aggregate
MPI_File mpi_fh = TupleRead1stPassNExchange(filename, senddata, totsend, distmapper, totallength);
typedef std::map<std::string, uint64_t> KEYMAP;
KEYMAP ultimateperm; // the ultimate permutation
for(IT i=0; i< totsend; ++i)
{
auto locnull = std::find(senddata[i].first.begin(), senddata[i].first.end(), '\0');
std::string searchstr(senddata[i].first.begin(), locnull);
auto ret = ultimateperm.emplace(std::make_pair(searchstr, senddata[i].second));
if(!ret.second) // the second is the boolean that tells success
{
// remember, we only sent unique vertex ids in the first place so we are expecting unique values in return
std::cout << "the duplication in ultimateperm is unexpected!!!" << std::endl;
}
}
delete [] senddata;
// rename the data now, first reset file pointers
MPI_Offset fpos, end_fpos;
struct stat st; // get file size
if (stat(filename.c_str(), &st) == -1)
{
MPI_Abort(MPI_COMM_WORLD, NOFILE);
}
int64_t file_size = st.st_size;
fpos = myrank * file_size / nprocs;
if(myrank != (nprocs-1)) end_fpos = (myrank + 1) * file_size / nprocs;
else end_fpos = file_size;
typedef typename DER::LocalIT LIT;
std::vector<LIT> rows;
std::vector<LIT> cols;
std::vector<NT> vals;
std::vector<std::string> lines;
bool finished = SpParHelper::FetchBatch(mpi_fh, fpos, end_fpos, true, lines, myrank);
int64_t entriesread = lines.size();
SpHelper::ProcessStrLinesNPermute(rows, cols, vals, lines, ultimateperm);
while(!finished)
{
finished = SpParHelper::FetchBatch(mpi_fh, fpos, end_fpos, false, lines, myrank);
entriesread += lines.size();
SpHelper::ProcessStrLinesNPermute(rows, cols, vals, lines, ultimateperm);
}
int64_t allentriesread;
MPI_Reduce(&entriesread, &allentriesread, 1, MPIType<int64_t>(), MPI_SUM, 0, commGrid->commWorld);
#ifdef COMBBLAS_DEBUG
if(myrank == 0)
std::cout << "Second reading finished. Total number of entries read across all processors is " << allentriesread << std::endl;
#endif
MPI_File_close(&mpi_fh);
std::vector< std::vector < std::tuple<LIT,LIT,NT> > > data(nprocs);
LIT locsize = rows.size(); // remember: locsize != entriesread (unless the matrix is unsymmetric)
for(LIT i=0; i<locsize; ++i)
{
LIT lrow, lcol;
int owner = Owner(totallength, totallength, rows[i], cols[i], lrow, lcol);
data[owner].push_back(std::make_tuple(lrow,lcol,vals[i]));
}
std::vector<LIT>().swap(rows);
std::vector<LIT>().swap(cols);
std::vector<NT>().swap(vals);
#ifdef COMBBLAS_DEBUG
if(myrank == 0)
std::cout << "Packing to recipients finished, about to send..." << std::endl;
#endif
if(spSeq) delete spSeq;
SparseCommon(data, locsize, totallength, totallength, BinOp);
// PrintInfo();
// distmapper.ParallelWrite("distmapper.mtx", 1, CharArraySaveHandler());
return distmapper;
}
//! Handles all sorts of orderings, even duplicates (what happens to them is determined by BinOp)
//! Requires proper matrix market banner at the moment
//! Replaces ReadDistribute for properly load balanced input in matrix market format
template <class IT, class NT, class DER>
template <typename _BinaryOperation>
void SpParMat< IT,NT,DER >::ParallelReadMM (const std::string & filename, bool onebased, _BinaryOperation BinOp)
{
int32_t type = -1;
int32_t symmetric = 0;
int64_t nrows, ncols, nonzeros;
int64_t linesread = 0;
FILE *f;
int myrank = commGrid->GetRank();
int nprocs = commGrid->GetSize();
if(myrank == 0)
{
MM_typecode matcode;
if ((f = fopen(filename.c_str(), "r")) == NULL)
{
printf("COMBBLAS: Matrix-market file %s can not be found\n", filename.c_str());
MPI_Abort(MPI_COMM_WORLD, NOFILE);
}
if (mm_read_banner(f, &matcode) != 0)
{
printf("Could not process Matrix Market banner.\n");
exit(1);
}
linesread++;
if (mm_is_complex(matcode))
{
printf("Sorry, this application does not support complext types");
printf("Market Market type: [%s]\n", mm_typecode_to_str(matcode));
}
else if(mm_is_real(matcode))
{
std::cout << "Matrix is Float" << std::endl;
type = 0;
}
else if(mm_is_integer(matcode))
{
std::cout << "Matrix is Integer" << std::endl;
type = 1;
}
else if(mm_is_pattern(matcode))
{
std::cout << "Matrix is Boolean" << std::endl;
type = 2;
}
if(mm_is_symmetric(matcode) || mm_is_hermitian(matcode))
{
std::cout << "Matrix is symmetric" << std::endl;
symmetric = 1;
}
int ret_code;
if ((ret_code = mm_read_mtx_crd_size(f, &nrows, &ncols, &nonzeros, &linesread)) !=0) // ABAB: mm_read_mtx_crd_size made 64-bit friendly
exit(1);
std::cout << "Total number of nonzeros expected across all processors is " << nonzeros << std::endl;
}
MPI_Bcast(&type, 1, MPI_INT, 0, commGrid->commWorld);
MPI_Bcast(&symmetric, 1, MPI_INT, 0, commGrid->commWorld);
MPI_Bcast(&nrows, 1, MPIType<int64_t>(), 0, commGrid->commWorld);
MPI_Bcast(&ncols, 1, MPIType<int64_t>(), 0, commGrid->commWorld);
MPI_Bcast(&nonzeros, 1, MPIType<int64_t>(), 0, commGrid->commWorld);
// Use fseek again to go backwards two bytes and check that byte with fgetc
struct stat st; // get file size
if (stat(filename.c_str(), &st) == -1)
{
MPI_Abort(MPI_COMM_WORLD, NOFILE);
}
int64_t file_size = st.st_size;
MPI_Offset fpos, end_fpos, endofheader;
if(commGrid->GetRank() == 0) // the offset needs to be for this rank
{
std::cout << "File is " << file_size << " bytes" << std::endl;
fpos = ftell(f);
endofheader = fpos;
MPI_Bcast(&endofheader, 1, MPIType<MPI_Offset>(), 0, commGrid->commWorld);
fclose(f);
}
else
{
MPI_Bcast(&endofheader, 1, MPIType<MPI_Offset>(), 0, commGrid->commWorld); // receive the file loc at the end of header
fpos = endofheader + myrank * (file_size-endofheader) / nprocs;
}
if(myrank != (nprocs-1)) end_fpos = endofheader + (myrank + 1) * (file_size-endofheader) / nprocs;
else end_fpos = file_size;
MPI_File mpi_fh;
MPI_File_open (commGrid->commWorld, const_cast<char*>(filename.c_str()), MPI_MODE_RDONLY, MPI_INFO_NULL, &mpi_fh);
typedef typename DER::LocalIT LIT;
std::vector<LIT> rows;
std::vector<LIT> cols;
std::vector<NT> vals;
std::vector<std::string> lines;
bool finished = SpParHelper::FetchBatch(mpi_fh, fpos, end_fpos, true, lines, myrank);
int64_t entriesread = lines.size();
SpHelper::ProcessLines(rows, cols, vals, lines, symmetric, type, onebased);
MPI_Barrier(commGrid->commWorld);
while(!finished)
{
finished = SpParHelper::FetchBatch(mpi_fh, fpos, end_fpos, false, lines, myrank);
entriesread += lines.size();
SpHelper::ProcessLines(rows, cols, vals, lines, symmetric, type, onebased);
}
int64_t allentriesread;
MPI_Reduce(&entriesread, &allentriesread, 1, MPIType<int64_t>(), MPI_SUM, 0, commGrid->commWorld);
#ifdef COMBBLAS_DEBUG
if(myrank == 0)
std::cout << "Reading finished. Total number of entries read across all processors is " << allentriesread << std::endl;
#endif
std::vector< std::vector < std::tuple<LIT,LIT,NT> > > data(nprocs);
LIT locsize = rows.size(); // remember: locsize != entriesread (unless the matrix is unsymmetric)
for(LIT i=0; i<locsize; ++i)
{
LIT lrow, lcol;
int owner = Owner(nrows, ncols, rows[i], cols[i], lrow, lcol);
data[owner].push_back(std::make_tuple(lrow,lcol,vals[i]));
}
std::vector<LIT>().swap(rows);
std::vector<LIT>().swap(cols);
std::vector<NT>().swap(vals);
#ifdef COMBBLAS_DEBUG
if(myrank == 0)
std::cout << "Packing to recepients finished, about to send..." << std::endl;
#endif
if(spSeq) delete spSeq;
SparseCommon(data, locsize, nrows, ncols, BinOp);
}
template <class IT, class NT, class DER>
template <class HANDLER>
void SpParMat< IT,NT,DER >::ParallelWriteMM(const std::string & filename, bool onebased, HANDLER handler)
{
int myrank = commGrid->GetRank();
int nprocs = commGrid->GetSize();
IT totalm = getnrow();
IT totaln = getncol();
IT totnnz = getnnz();
std::stringstream ss;
if(myrank == 0)
{
ss << "%%MatrixMarket matrix coordinate real general" << std::endl;
ss << totalm << " " << totaln << " " << totnnz << std::endl;
}
IT entries = getlocalnnz();
IT sizeuntil = 0;
MPI_Exscan( &entries, &sizeuntil, 1, MPIType<IT>(), MPI_SUM, commGrid->GetWorld() );
if(myrank == 0) sizeuntil = 0; // because MPI_Exscan says the recvbuf in process 0 is undefined
IT roffset = 0;
IT coffset = 0;
GetPlaceInGlobalGrid(roffset, coffset);
if(onebased)
{
roffset += 1; // increment by 1
coffset += 1;
}
for(typename DER::SpColIter colit = spSeq->begcol(); colit != spSeq->endcol(); ++colit) // iterate over nonempty subcolumns
{
for(typename DER::SpColIter::NzIter nzit = spSeq->begnz(colit); nzit != spSeq->endnz(colit); ++nzit)
{
IT glrowid = nzit.rowid() + roffset;
IT glcolid = colit.colid() + coffset;
ss << glrowid << '\t';
ss << glcolid << '\t';
handler.save(ss, nzit.value(), glrowid, glcolid);
ss << '\n';
}
}
std::string text = ss.str();
int64_t * bytes = new int64_t[nprocs];
bytes[myrank] = text.size();
MPI_Allgather(MPI_IN_PLACE, 1, MPIType<int64_t>(), bytes, 1, MPIType<int64_t>(), commGrid->GetWorld());
int64_t bytesuntil = std::accumulate(bytes, bytes+myrank, static_cast<int64_t>(0));
int64_t bytestotal = std::accumulate(bytes, bytes+nprocs, static_cast<int64_t>(0));
MPI_File thefile;
MPI_File_open(commGrid->GetWorld(), (char*) filename.c_str(), MPI_MODE_CREATE | MPI_MODE_WRONLY, MPI_INFO_NULL, &thefile) ;
int mpi_err = MPI_File_set_view(thefile, bytesuntil, MPI_CHAR, MPI_CHAR, (char*)"external32", MPI_INFO_NULL);
if (mpi_err == 51) {
// external32 datarep is not supported, use native instead
MPI_File_set_view(thefile, bytesuntil, MPI_CHAR, MPI_CHAR, (char*)"native", MPI_INFO_NULL);
}
int64_t batchSize = 256 * 1024 * 1024;
size_t localfileptr = 0;
int64_t remaining = bytes[myrank];
int64_t totalremaining = bytestotal;
while(totalremaining > 0)
{
#ifdef COMBBLAS_DEBUG
if(myrank == 0)
std::cout << "Remaining " << totalremaining << " bytes to write in aggregate" << std::endl;
#endif
MPI_Status status;
int curBatch = std::min(batchSize, remaining);
MPI_File_write_all(thefile, text.c_str()+localfileptr, curBatch, MPI_CHAR, &status);
int count;
MPI_Get_count(&status, MPI_CHAR, &count); // known bug: https://github.com/pmodels/mpich/issues/2332
assert( (curBatch == 0) || (count == curBatch) ); // count can return the previous/wrong value when 0 elements are written
localfileptr += curBatch;
remaining -= curBatch;
MPI_Allreduce(&remaining, &totalremaining, 1, MPIType<int64_t>(), MPI_SUM, commGrid->GetWorld());
}
MPI_File_close(&thefile);
delete [] bytes;
}
//! Handles all sorts of orderings as long as there are no duplicates
//! May perform better when the data is already reverse column-sorted (i.e. in decreasing order)
//! if nonum is true, then numerics are not supplied and they are assumed to be all 1's
template <class IT, class NT, class DER>
template <class HANDLER>
void SpParMat< IT,NT,DER >::ReadDistribute (const std::string & filename, int master, bool nonum, HANDLER handler, bool transpose, bool pario)
{
#ifdef TAU_PROFILE
TAU_PROFILE_TIMER(rdtimer, "ReadDistribute", "void SpParMat::ReadDistribute (const string & , int, bool, HANDLER, bool)", TAU_DEFAULT);
TAU_PROFILE_START(rdtimer);
#endif
std::ifstream infile;
FILE * binfile = NULL; // points to "past header" if the file is binary
int seeklength = 0;
HeaderInfo hfile;
if(commGrid->GetRank() == master) // 1 processor
{
hfile = ParseHeader(filename, binfile, seeklength);
}
MPI_Bcast(&seeklength, 1, MPI_INT, master, commGrid->commWorld);
IT total_m, total_n, total_nnz;
IT m_perproc = 0, n_perproc = 0;
int colneighs = commGrid->GetGridRows(); // number of neighbors along this processor column (including oneself)
int rowneighs = commGrid->GetGridCols(); // number of neighbors along this processor row (including oneself)
IT buffpercolneigh = MEMORYINBYTES / (colneighs * (2 * sizeof(IT) + sizeof(NT)));
IT buffperrowneigh = MEMORYINBYTES / (rowneighs * (2 * sizeof(IT) + sizeof(NT)));
if(pario)
{
// since all colneighs will be reading the data at the same time
// chances are they might all read the data that should go to one
// in that case buffperrowneigh > colneighs * buffpercolneigh
// in order not to overflow
buffpercolneigh /= colneighs;
if(seeklength == 0)
SpParHelper::Print("COMBBLAS: Parallel I/O requested but binary header is corrupted\n", commGrid->GetWorld());
}
// make sure that buffperrowneigh >= buffpercolneigh to cover for this patological case:
// -- all data received by a given column head (by vertical communication) are headed to a single processor along the row
// -- then making sure buffperrowneigh >= buffpercolneigh guarantees that the horizontal buffer will never overflow
buffperrowneigh = std::max(buffperrowneigh, buffpercolneigh);
if(std::max(buffpercolneigh * colneighs, buffperrowneigh * rowneighs) > std::numeric_limits<int>::max())
{
SpParHelper::Print("COMBBLAS: MPI doesn't support sending int64_t send/recv counts or displacements\n", commGrid->GetWorld());
}
int * cdispls = new int[colneighs];
for (IT i=0; i<colneighs; ++i) cdispls[i] = i*buffpercolneigh;
int * rdispls = new int[rowneighs];
for (IT i=0; i<rowneighs; ++i) rdispls[i] = i*buffperrowneigh;
int *ccurptrs = NULL, *rcurptrs = NULL;
int recvcount = 0;
IT * rows = NULL;
IT * cols = NULL;
NT * vals = NULL;
// Note: all other column heads that initiate the horizontal communication has the same "rankinrow" with the master
int rankincol = commGrid->GetRankInProcCol(master); // get master's rank in its processor column
int rankinrow = commGrid->GetRankInProcRow(master);
std::vector< std::tuple<IT, IT, NT> > localtuples;
if(commGrid->GetRank() == master) // 1 processor
{
if( !hfile.fileexists )
{
SpParHelper::Print( "COMBBLAS: Input file doesn't exist\n", commGrid->GetWorld());
total_n = 0; total_m = 0;
BcastEssentials(commGrid->commWorld, total_m, total_n, total_nnz, master);
return;
}
if (hfile.headerexists && hfile.format == 1)
{
SpParHelper::Print("COMBBLAS: Ascii input with binary headers is not supported\n", commGrid->GetWorld());
total_n = 0; total_m = 0;
BcastEssentials(commGrid->commWorld, total_m, total_n, total_nnz, master);
return;
}
if ( !hfile.headerexists ) // no header - ascii file (at this point, file exists)
{
infile.open(filename.c_str());
char comment[256];
infile.getline(comment,256);
while(comment[0] == '%')
{
infile.getline(comment,256);
}
std::stringstream ss;
ss << std::string(comment);
ss >> total_m >> total_n >> total_nnz;
if(pario)
{
SpParHelper::Print("COMBBLAS: Trying to read binary headerless file in parallel, aborting\n", commGrid->GetWorld());
total_n = 0; total_m = 0;
BcastEssentials(commGrid->commWorld, total_m, total_n, total_nnz, master);
return;
}
}
else // hfile.headerexists && hfile.format == 0
{
total_m = hfile.m;
total_n = hfile.n;
total_nnz = hfile.nnz;
}
m_perproc = total_m / colneighs;
n_perproc = total_n / rowneighs;
BcastEssentials(commGrid->commWorld, total_m, total_n, total_nnz, master);
AllocateSetBuffers(rows, cols, vals, rcurptrs, ccurptrs, rowneighs, colneighs, buffpercolneigh);
if(seeklength > 0 && pario) // sqrt(p) processors also do parallel binary i/o
{
IT entriestoread = total_nnz / colneighs;
#ifdef IODEBUG
std::ofstream oput;
commGrid->OpenDebugFile("Read", oput);
oput << "Total nnz: " << total_nnz << " entries to read: " << entriestoread << std::endl;
oput.close();
#endif
ReadAllMine(binfile, rows, cols, vals, localtuples, rcurptrs, ccurptrs, rdispls, cdispls, m_perproc, n_perproc,
rowneighs, colneighs, buffperrowneigh, buffpercolneigh, entriestoread, handler, rankinrow, transpose);
}
else // only this (master) is doing I/O (text or binary)
{
IT temprow, tempcol;
NT tempval;
IT ntrow = 0, ntcol = 0; // not transposed row and column index
char line[1024];
bool nonumline = nonum;
IT cnz = 0;
for(; cnz < total_nnz; ++cnz)
{
int colrec;
size_t commonindex;
std::stringstream linestream;
if( (!hfile.headerexists) && (!infile.eof()))
{
// read one line at a time so that missing numerical values can be detected
infile.getline(line, 1024);
linestream << line;
linestream >> temprow >> tempcol;
if (!nonum)
{
// see if this line has a value
linestream >> std::skipws;
nonumline = linestream.eof();
}
--temprow; // file is 1-based where C-arrays are 0-based
--tempcol;
ntrow = temprow;
ntcol = tempcol;
}
else if(hfile.headerexists && (!feof(binfile)) )
{
handler.binaryfill(binfile, temprow , tempcol, tempval);
}
if (transpose)
{
IT swap = temprow;
temprow = tempcol;
tempcol = swap;
}
colrec = std::min(static_cast<int>(temprow / m_perproc), colneighs-1); // precipient processor along the column
commonindex = colrec * buffpercolneigh + ccurptrs[colrec];
rows[ commonindex ] = temprow;
cols[ commonindex ] = tempcol;
if( (!hfile.headerexists) && (!infile.eof()))
{
vals[ commonindex ] = nonumline ? handler.getNoNum(ntrow, ntcol) : handler.read(linestream, ntrow, ntcol); //tempval;
}
else if(hfile.headerexists && (!feof(binfile)) )
{
vals[ commonindex ] = tempval;
}
++ (ccurptrs[colrec]);
if(ccurptrs[colrec] == buffpercolneigh || (cnz == (total_nnz-1)) ) // one buffer is full, or file is done !
{
MPI_Scatter(ccurptrs, 1, MPI_INT, &recvcount, 1, MPI_INT, rankincol, commGrid->colWorld); // first, send the receive counts
// generate space for own recv data ... (use arrays because vector<bool> is cripled, if NT=bool)
IT * temprows = new IT[recvcount];
IT * tempcols = new IT[recvcount];
NT * tempvals = new NT[recvcount];
// then, send all buffers that to their recipients ...
MPI_Scatterv(rows, ccurptrs, cdispls, MPIType<IT>(), temprows, recvcount, MPIType<IT>(), rankincol, commGrid->colWorld);
MPI_Scatterv(cols, ccurptrs, cdispls, MPIType<IT>(), tempcols, recvcount, MPIType<IT>(), rankincol, commGrid->colWorld);
MPI_Scatterv(vals, ccurptrs, cdispls, MPIType<NT>(), tempvals, recvcount, MPIType<NT>(), rankincol, commGrid->colWorld);
std::fill_n(ccurptrs, colneighs, 0); // finally, reset current pointers !
DeleteAll(rows, cols, vals);
HorizontalSend(rows, cols, vals,temprows, tempcols, tempvals, localtuples, rcurptrs, rdispls,
buffperrowneigh, rowneighs, recvcount, m_perproc, n_perproc, rankinrow);
if( cnz != (total_nnz-1) ) // otherwise the loop will exit with noone to claim memory back
{
// reuse these buffers for the next vertical communication
rows = new IT [ buffpercolneigh * colneighs ];
cols = new IT [ buffpercolneigh * colneighs ];
vals = new NT [ buffpercolneigh * colneighs ];
}
} // end_if for "send buffer is full" case
} // end_for for "cnz < entriestoread" case
assert (cnz == total_nnz);
// Signal the end of file to other processors along the column
std::fill_n(ccurptrs, colneighs, std::numeric_limits<int>::max());
MPI_Scatter(ccurptrs, 1, MPI_INT, &recvcount, 1, MPI_INT, rankincol, commGrid->colWorld);
// And along the row ...
std::fill_n(rcurptrs, rowneighs, std::numeric_limits<int>::max());
MPI_Scatter(rcurptrs, 1, MPI_INT, &recvcount, 1, MPI_INT, rankinrow, commGrid->rowWorld);
} // end of "else" (only one processor reads) block
} // end_if for "master processor" case
else if( commGrid->OnSameProcCol(master) ) // (r-1) processors
{
BcastEssentials(commGrid->commWorld, total_m, total_n, total_nnz, master);
m_perproc = total_m / colneighs;
n_perproc = total_n / rowneighs;
if(seeklength > 0 && pario) // these processors also do parallel binary i/o
{
binfile = fopen(filename.c_str(), "rb");
IT entrysize = handler.entrylength();
int myrankincol = commGrid->GetRankInProcCol();
IT perreader = total_nnz / colneighs;
IT read_offset = entrysize * static_cast<IT>(myrankincol) * perreader + seeklength;
IT entriestoread = perreader;
if (myrankincol == colneighs-1)
entriestoread = total_nnz - static_cast<IT>(myrankincol) * perreader;
fseek(binfile, read_offset, SEEK_SET);
#ifdef IODEBUG
std::ofstream oput;
commGrid->OpenDebugFile("Read", oput);
oput << "Total nnz: " << total_nnz << " OFFSET : " << read_offset << " entries to read: " << entriestoread << std::endl;
oput.close();
#endif
AllocateSetBuffers(rows, cols, vals, rcurptrs, ccurptrs, rowneighs, colneighs, buffpercolneigh);
ReadAllMine(binfile, rows, cols, vals, localtuples, rcurptrs, ccurptrs, rdispls, cdispls, m_perproc, n_perproc,
rowneighs, colneighs, buffperrowneigh, buffpercolneigh, entriestoread, handler, rankinrow, transpose);
}
else // only master does the I/O
{
while(total_n > 0 || total_m > 0) // otherwise input file does not exist !
{
// void MPI::Comm::Scatterv(const void* sendbuf, const int sendcounts[], const int displs[], const MPI::Datatype& sendtype,
// void* recvbuf, int recvcount, const MPI::Datatype & recvtype, int root) const
// The outcome is as if the root executed n send operations,
// MPI_Send(sendbuf + displs[i] * extent(sendtype), sendcounts[i], sendtype, i, ...)
// and each process executed a receive,
// MPI_Recv(recvbuf, recvcount, recvtype, root, ...)
// The send buffer is ignored for all nonroot processes.
MPI_Scatter(ccurptrs, 1, MPI_INT, &recvcount, 1, MPI_INT, rankincol, commGrid->colWorld); // first receive the receive counts ...
if( recvcount == std::numeric_limits<int>::max()) break;
// create space for incoming data ...
IT * temprows = new IT[recvcount];
IT * tempcols = new IT[recvcount];
NT * tempvals = new NT[recvcount];
// receive actual data ... (first 4 arguments are ignored in the receiver side)
MPI_Scatterv(rows, ccurptrs, cdispls, MPIType<IT>(), temprows, recvcount, MPIType<IT>(), rankincol, commGrid->colWorld);
MPI_Scatterv(cols, ccurptrs, cdispls, MPIType<IT>(), tempcols, recvcount, MPIType<IT>(), rankincol, commGrid->colWorld);
MPI_Scatterv(vals, ccurptrs, cdispls, MPIType<NT>(), tempvals, recvcount, MPIType<NT>(), rankincol, commGrid->colWorld);
// now, send the data along the horizontal
rcurptrs = new int[rowneighs];
std::fill_n(rcurptrs, rowneighs, 0);
// HorizontalSend frees the memory of temp_xxx arrays and then creates and frees memory of all the six arrays itself
HorizontalSend(rows, cols, vals,temprows, tempcols, tempvals, localtuples, rcurptrs, rdispls,
buffperrowneigh, rowneighs, recvcount, m_perproc, n_perproc, rankinrow);
}
}
// Signal the end of file to other processors along the row
std::fill_n(rcurptrs, rowneighs, std::numeric_limits<int>::max());
MPI_Scatter(rcurptrs, 1, MPI_INT, &recvcount, 1, MPI_INT, rankinrow, commGrid->rowWorld);
delete [] rcurptrs;
}
else // r * (s-1) processors that only participate in the horizontal communication step
{
BcastEssentials(commGrid->commWorld, total_m, total_n, total_nnz, master);
m_perproc = total_m / colneighs;
n_perproc = total_n / rowneighs;
while(total_n > 0 || total_m > 0) // otherwise input file does not exist !
{
// receive the receive count
MPI_Scatter(rcurptrs, 1, MPI_INT, &recvcount, 1, MPI_INT, rankinrow, commGrid->rowWorld);
if( recvcount == std::numeric_limits<int>::max())
break;
#ifdef IODEBUG
std::ofstream oput;
commGrid->OpenDebugFile("Read", oput);
oput << "Total nnz: " << total_nnz << " total_m : " << total_m << " recvcount: " << recvcount << std::endl;
oput.close();
#endif
// create space for incoming data ...
IT * temprows = new IT[recvcount];
IT * tempcols = new IT[recvcount];
NT * tempvals = new NT[recvcount];
MPI_Scatterv(rows, rcurptrs, rdispls, MPIType<IT>(), temprows, recvcount, MPIType<IT>(), rankinrow, commGrid->rowWorld);
MPI_Scatterv(cols, rcurptrs, rdispls, MPIType<IT>(), tempcols, recvcount, MPIType<IT>(), rankinrow, commGrid->rowWorld);
MPI_Scatterv(vals, rcurptrs, rdispls, MPIType<NT>(), tempvals, recvcount, MPIType<NT>(), rankinrow, commGrid->rowWorld);
// now push what is ours to tuples
IT moffset = commGrid->myprocrow * m_perproc;
IT noffset = commGrid->myproccol * n_perproc;
for(IT i=0; i< recvcount; ++i)
{
localtuples.push_back( std::make_tuple(temprows[i]-moffset, tempcols[i]-noffset, tempvals[i]) );
}
DeleteAll(temprows,tempcols,tempvals);
}
}
DeleteAll(cdispls, rdispls);
std::tuple<IT,IT,NT> * arrtuples = new std::tuple<IT,IT,NT>[localtuples.size()]; // the vector will go out of scope, make it stick !
std::copy(localtuples.begin(), localtuples.end(), arrtuples);
IT localm = (commGrid->myprocrow != (commGrid->grrows-1))? m_perproc: (total_m - (m_perproc * (commGrid->grrows-1)));
IT localn = (commGrid->myproccol != (commGrid->grcols-1))? n_perproc: (total_n - (n_perproc * (commGrid->grcols-1)));
spSeq->Create( localtuples.size(), localm, localn, arrtuples); // the deletion of arrtuples[] is handled by SpMat::Create
#ifdef TAU_PROFILE
TAU_PROFILE_STOP(rdtimer);
#endif
return;
}
template <class IT, class NT, class DER>
void SpParMat<IT,NT,DER>::AllocateSetBuffers(IT * & rows, IT * & cols, NT * & vals, int * & rcurptrs, int * & ccurptrs, int rowneighs, int colneighs, IT buffpercolneigh)
{
// allocate buffers on the heap as stack space is usually limited
rows = new IT [ buffpercolneigh * colneighs ];
cols = new IT [ buffpercolneigh * colneighs ];
vals = new NT [ buffpercolneigh * colneighs ];
ccurptrs = new int[colneighs];
rcurptrs = new int[rowneighs];
std::fill_n(ccurptrs, colneighs, 0); // fill with zero
std::fill_n(rcurptrs, rowneighs, 0);
}
template <class IT, class NT, class DER>
void SpParMat<IT,NT,DER>::BcastEssentials(MPI_Comm & world, IT & total_m, IT & total_n, IT & total_nnz, int master)
{
MPI_Bcast(&total_m, 1, MPIType<IT>(), master, world);
MPI_Bcast(&total_n, 1, MPIType<IT>(), master, world);
MPI_Bcast(&total_nnz, 1, MPIType<IT>(), master, world);
}
/*
* @post {rows, cols, vals are pre-allocated on the heap after this call}
* @post {ccurptrs are set to zero; so that if another call is made to this function without modifying ccurptrs, no data will be send from this procesor}
*/
template <class IT, class NT, class DER>
void SpParMat<IT,NT,DER>::VerticalSend(IT * & rows, IT * & cols, NT * & vals, std::vector< std::tuple<IT,IT,NT> > & localtuples, int * rcurptrs, int * ccurptrs, int * rdispls, int * cdispls,
IT m_perproc, IT n_perproc, int rowneighs, int colneighs, IT buffperrowneigh, IT buffpercolneigh, int rankinrow)
{
// first, send/recv the counts ...
int * colrecvdispls = new int[colneighs];
int * colrecvcounts = new int[colneighs];
MPI_Alltoall(ccurptrs, 1, MPI_INT, colrecvcounts, 1, MPI_INT, commGrid->colWorld); // share the request counts
int totrecv = std::accumulate(colrecvcounts,colrecvcounts+colneighs,0);
colrecvdispls[0] = 0; // receive displacements are exact whereas send displacements have slack
for(int i=0; i<colneighs-1; ++i)
colrecvdispls[i+1] = colrecvdispls[i] + colrecvcounts[i];
// generate space for own recv data ... (use arrays because vector<bool> is cripled, if NT=bool)
IT * temprows = new IT[totrecv];
IT * tempcols = new IT[totrecv];
NT * tempvals = new NT[totrecv];
// then, exchange all buffers that to their recipients ...
MPI_Alltoallv(rows, ccurptrs, cdispls, MPIType<IT>(), temprows, colrecvcounts, colrecvdispls, MPIType<IT>(), commGrid->colWorld);
MPI_Alltoallv(cols, ccurptrs, cdispls, MPIType<IT>(), tempcols, colrecvcounts, colrecvdispls, MPIType<IT>(), commGrid->colWorld);
MPI_Alltoallv(vals, ccurptrs, cdispls, MPIType<NT>(), tempvals, colrecvcounts, colrecvdispls, MPIType<NT>(), commGrid->colWorld);
// finally, reset current pointers !
std::fill_n(ccurptrs, colneighs, 0);
DeleteAll(colrecvdispls, colrecvcounts);
DeleteAll(rows, cols, vals);
// rcurptrs/rdispls are zero initialized scratch space
HorizontalSend(rows, cols, vals,temprows, tempcols, tempvals, localtuples, rcurptrs, rdispls, buffperrowneigh, rowneighs, totrecv, m_perproc, n_perproc, rankinrow);
// reuse these buffers for the next vertical communication
rows = new IT [ buffpercolneigh * colneighs ];
cols = new IT [ buffpercolneigh * colneighs ];
vals = new NT [ buffpercolneigh * colneighs ];
}
/**
* Private subroutine of ReadDistribute.
* Executed by p_r processors on the first processor column.
* @pre {rows, cols, vals are pre-allocated on the heap before this call}
* @param[in] rankinrow {row head's rank in its processor row - determines the scatter person}
*/
template <class IT, class NT, class DER>
template <class HANDLER>
void SpParMat<IT,NT,DER>::ReadAllMine(FILE * binfile, IT * & rows, IT * & cols, NT * & vals, std::vector< std::tuple<IT,IT,NT> > & localtuples, int * rcurptrs, int * ccurptrs, int * rdispls, int * cdispls,
IT m_perproc, IT n_perproc, int rowneighs, int colneighs, IT buffperrowneigh, IT buffpercolneigh, IT entriestoread, HANDLER handler, int rankinrow, bool transpose)
{
assert(entriestoread != 0);
IT cnz = 0;
IT temprow, tempcol;
NT tempval;
int finishedglobal = 1;
while(cnz < entriestoread && !feof(binfile)) // this loop will execute at least once
{
handler.binaryfill(binfile, temprow , tempcol, tempval);
if (transpose)
{
IT swap = temprow;
temprow = tempcol;
tempcol = swap;
}
int colrec = std::min(static_cast<int>(temprow / m_perproc), colneighs-1); // precipient processor along the column
size_t commonindex = colrec * buffpercolneigh + ccurptrs[colrec];
rows[ commonindex ] = temprow;
cols[ commonindex ] = tempcol;
vals[ commonindex ] = tempval;
++ (ccurptrs[colrec]);
if(ccurptrs[colrec] == buffpercolneigh || (cnz == (entriestoread-1)) ) // one buffer is full, or this processor's share is done !
{
#ifdef IODEBUG
std::ofstream oput;
commGrid->OpenDebugFile("Read", oput);
oput << "To column neighbors: ";
std::copy(ccurptrs, ccurptrs+colneighs, std::ostream_iterator<int>(oput, " ")); oput << std::endl;
oput.close();
#endif
VerticalSend(rows, cols, vals, localtuples, rcurptrs, ccurptrs, rdispls, cdispls, m_perproc, n_perproc,
rowneighs, colneighs, buffperrowneigh, buffpercolneigh, rankinrow);
if(cnz == (entriestoread-1)) // last execution of the outer loop
{
int finishedlocal = 1; // I am done, but let me check others
MPI_Allreduce( &finishedlocal, &finishedglobal, 1, MPI_INT, MPI_BAND, commGrid->colWorld);
while(!finishedglobal)
{
#ifdef IODEBUG
std::ofstream oput;
commGrid->OpenDebugFile("Read", oput);
oput << "To column neighbors: ";
std::copy(ccurptrs, ccurptrs+colneighs, std::ostream_iterator<int>(oput, " ")); oput << std::endl;
oput.close();
#endif
// postcondition of VerticalSend: ccurptrs are set to zero
// if another call is made to this function without modifying ccurptrs, no data will be send from this procesor
VerticalSend(rows, cols, vals, localtuples, rcurptrs, ccurptrs, rdispls, cdispls, m_perproc, n_perproc,
rowneighs, colneighs, buffperrowneigh, buffpercolneigh, rankinrow);
MPI_Allreduce( &finishedlocal, &finishedglobal, 1, MPI_INT, MPI_BAND, commGrid->colWorld);
}
}
else // the other loop will continue executing
{
int finishedlocal = 0;
MPI_Allreduce( &finishedlocal, &finishedglobal, 1, MPI_INT, MPI_BAND, commGrid->colWorld);
}
} // end_if for "send buffer is full" case
++cnz;
}
// signal the end to row neighbors
std::fill_n(rcurptrs, rowneighs, std::numeric_limits<int>::max());
int recvcount;
MPI_Scatter(rcurptrs, 1, MPI_INT, &recvcount, 1, MPI_INT, rankinrow, commGrid->rowWorld);
}
/**
* Private subroutine of ReadDistribute
* @param[in] rankinrow {Row head's rank in its processor row}
* Initially temp_xxx arrays carry data received along the proc. column AND needs to be sent along the proc. row
* After usage, function frees the memory of temp_xxx arrays and then creates and frees memory of all the six arrays itself
*/
template <class IT, class NT, class DER>
void SpParMat<IT,NT,DER>::HorizontalSend(IT * & rows, IT * & cols, NT * & vals, IT * & temprows, IT * & tempcols, NT * & tempvals, std::vector < std::tuple <IT,IT,NT> > & localtuples,
int * rcurptrs, int * rdispls, IT buffperrowneigh, int rowneighs, int recvcount, IT m_perproc, IT n_perproc, int rankinrow)
{
rows = new IT [ buffperrowneigh * rowneighs ];
cols = new IT [ buffperrowneigh * rowneighs ];
vals = new NT [ buffperrowneigh * rowneighs ];
// prepare to send the data along the horizontal
for(int i=0; i< recvcount; ++i)
{
int rowrec = std::min(static_cast<int>(tempcols[i] / n_perproc), rowneighs-1);
rows[ rowrec * buffperrowneigh + rcurptrs[rowrec] ] = temprows[i];
cols[ rowrec * buffperrowneigh + rcurptrs[rowrec] ] = tempcols[i];
vals[ rowrec * buffperrowneigh + rcurptrs[rowrec] ] = tempvals[i];
++ (rcurptrs[rowrec]);
}
#ifdef IODEBUG
std::ofstream oput;
commGrid->OpenDebugFile("Read", oput);
oput << "To row neighbors: ";
std::copy(rcurptrs, rcurptrs+rowneighs, std::ostream_iterator<int>(oput, " ")); oput << std::endl;
oput << "Row displacements were: ";
std::copy(rdispls, rdispls+rowneighs, std::ostream_iterator<int>(oput, " ")); oput << std::endl;
oput.close();
#endif
MPI_Scatter(rcurptrs, 1, MPI_INT, &recvcount, 1, MPI_INT, rankinrow, commGrid->rowWorld); // Send the receive counts for horizontal communication
// the data is now stored in rows/cols/vals, can reset temporaries
// sets size and capacity to new recvcount
DeleteAll(temprows, tempcols, tempvals);
temprows = new IT[recvcount];
tempcols = new IT[recvcount];
tempvals = new NT[recvcount];
// then, send all buffers that to their recipients ...
MPI_Scatterv(rows, rcurptrs, rdispls, MPIType<IT>(), temprows, recvcount, MPIType<IT>(), rankinrow, commGrid->rowWorld);
MPI_Scatterv(cols, rcurptrs, rdispls, MPIType<IT>(), tempcols, recvcount, MPIType<IT>(), rankinrow, commGrid->rowWorld);
MPI_Scatterv(vals, rcurptrs, rdispls, MPIType<NT>(), tempvals, recvcount, MPIType<NT>(), rankinrow, commGrid->rowWorld);
// now push what is ours to tuples
IT moffset = commGrid->myprocrow * m_perproc;
IT noffset = commGrid->myproccol * n_perproc;
for(int i=0; i< recvcount; ++i)
{
localtuples.push_back( std::make_tuple(temprows[i]-moffset, tempcols[i]-noffset, tempvals[i]) );
}
std::fill_n(rcurptrs, rowneighs, 0);
DeleteAll(rows, cols, vals, temprows, tempcols, tempvals);
}
//! The input parameters' identity (zero) elements as well as
//! their communication grid is preserved while outputting
template <class IT, class NT, class DER>
void SpParMat<IT,NT,DER>::Find (FullyDistVec<IT,IT> & distrows, FullyDistVec<IT,IT> & distcols, FullyDistVec<IT,NT> & distvals) const
{
if((*(distrows.commGrid) != *(distcols.commGrid)) || (*(distcols.commGrid) != *(distvals.commGrid)))
{
SpParHelper::Print("Grids are not comparable, Find() fails!", commGrid->GetWorld());
MPI_Abort(MPI_COMM_WORLD, GRIDMISMATCH);
}
IT globallen = getnnz();
SpTuples<IT,NT> Atuples(*spSeq);
FullyDistVec<IT,IT> nrows ( distrows.commGrid, globallen, 0);
FullyDistVec<IT,IT> ncols ( distcols.commGrid, globallen, 0);
FullyDistVec<IT,NT> nvals ( distvals.commGrid, globallen, NT());
IT prelen = Atuples.getnnz();
//IT postlen = nrows.MyLocLength();
int rank = commGrid->GetRank();
int nprocs = commGrid->GetSize();
IT * prelens = new IT[nprocs];
prelens[rank] = prelen;
MPI_Allgather(MPI_IN_PLACE, 0, MPIType<IT>(), prelens, 1, MPIType<IT>(), commGrid->GetWorld());
IT prelenuntil = std::accumulate(prelens, prelens+rank, static_cast<IT>(0));
int * sendcnt = new int[nprocs](); // zero initialize
IT * rows = new IT[prelen];
IT * cols = new IT[prelen];
NT * vals = new NT[prelen];
int rowrank = commGrid->GetRankInProcRow();
int colrank = commGrid->GetRankInProcCol();
int rowneighs = commGrid->GetGridCols();
int colneighs = commGrid->GetGridRows();
IT * locnrows = new IT[colneighs]; // number of rows is calculated by a reduction among the processor column
IT * locncols = new IT[rowneighs];
locnrows[colrank] = getlocalrows();
locncols[rowrank] = getlocalcols();
MPI_Allgather(MPI_IN_PLACE, 0, MPIType<IT>(),locnrows, 1, MPIType<IT>(), commGrid->GetColWorld());
MPI_Allgather(MPI_IN_PLACE, 0, MPIType<IT>(),locncols, 1, MPIType<IT>(), commGrid->GetRowWorld());
IT roffset = std::accumulate(locnrows, locnrows+colrank, static_cast<IT>(0));
IT coffset = std::accumulate(locncols, locncols+rowrank, static_cast<IT>(0));
DeleteAll(locnrows, locncols);
for(int i=0; i< prelen; ++i)
{
IT locid; // ignore local id, data will come in order
int owner = nrows.Owner(prelenuntil+i, locid);
sendcnt[owner]++;
rows[i] = Atuples.rowindex(i) + roffset; // need the global row index
cols[i] = Atuples.colindex(i) + coffset; // need the global col index
vals[i] = Atuples.numvalue(i);
}
int * recvcnt = new int[nprocs];
MPI_Alltoall(sendcnt, 1, MPI_INT, recvcnt, 1, MPI_INT, commGrid->GetWorld()); // get the recv counts
int * sdpls = new int[nprocs](); // displacements (zero initialized pid)
int * rdpls = new int[nprocs]();
std::partial_sum(sendcnt, sendcnt+nprocs-1, sdpls+1);
std::partial_sum(recvcnt, recvcnt+nprocs-1, rdpls+1);
MPI_Alltoallv(rows, sendcnt, sdpls, MPIType<IT>(), SpHelper::p2a(nrows.arr), recvcnt, rdpls, MPIType<IT>(), commGrid->GetWorld());
MPI_Alltoallv(cols, sendcnt, sdpls, MPIType<IT>(), SpHelper::p2a(ncols.arr), recvcnt, rdpls, MPIType<IT>(), commGrid->GetWorld());
MPI_Alltoallv(vals, sendcnt, sdpls, MPIType<NT>(), SpHelper::p2a(nvals.arr), recvcnt, rdpls, MPIType<NT>(), commGrid->GetWorld());
DeleteAll(sendcnt, recvcnt, sdpls, rdpls);
DeleteAll(prelens, rows, cols, vals);
distrows = nrows;
distcols = ncols;
distvals = nvals;
}
//! The input parameters' identity (zero) elements as well as
//! their communication grid is preserved while outputting
template <class IT, class NT, class DER>
void SpParMat<IT,NT,DER>::Find (FullyDistVec<IT,IT> & distrows, FullyDistVec<IT,IT> & distcols) const
{
if((*(distrows.commGrid) != *(distcols.commGrid)) )
{
SpParHelper::Print("Grids are not comparable, Find() fails!", commGrid->GetWorld());
MPI_Abort(MPI_COMM_WORLD, GRIDMISMATCH);
}
IT globallen = getnnz();
SpTuples<IT,NT> Atuples(*spSeq);
FullyDistVec<IT,IT> nrows ( distrows.commGrid, globallen, 0);
FullyDistVec<IT,IT> ncols ( distcols.commGrid, globallen, 0);
IT prelen = Atuples.getnnz();
int rank = commGrid->GetRank();
int nprocs = commGrid->GetSize();
IT * prelens = new IT[nprocs];
prelens[rank] = prelen;
MPI_Allgather(MPI_IN_PLACE, 0, MPIType<IT>(), prelens, 1, MPIType<IT>(), commGrid->GetWorld());
IT prelenuntil = std::accumulate(prelens, prelens+rank, static_cast<IT>(0));
int * sendcnt = new int[nprocs](); // zero initialize
IT * rows = new IT[prelen];
IT * cols = new IT[prelen];
NT * vals = new NT[prelen];
int rowrank = commGrid->GetRankInProcRow();
int colrank = commGrid->GetRankInProcCol();
int rowneighs = commGrid->GetGridCols();
int colneighs = commGrid->GetGridRows();
IT * locnrows = new IT[colneighs]; // number of rows is calculated by a reduction among the processor column
IT * locncols = new IT[rowneighs];
locnrows[colrank] = getlocalrows();
locncols[rowrank] = getlocalcols();
MPI_Allgather(MPI_IN_PLACE, 0, MPIType<IT>(),locnrows, 1, MPIType<IT>(), commGrid->GetColWorld());
MPI_Allgather(MPI_IN_PLACE, 0, MPIType<IT>(),locncols, 1, MPIType<IT>(), commGrid->GetColWorld());
IT roffset = std::accumulate(locnrows, locnrows+colrank, static_cast<IT>(0));
IT coffset = std::accumulate(locncols, locncols+rowrank, static_cast<IT>(0));
DeleteAll(locnrows, locncols);
for(int i=0; i< prelen; ++i)
{
IT locid; // ignore local id, data will come in order
int owner = nrows.Owner(prelenuntil+i, locid);
sendcnt[owner]++;
rows[i] = Atuples.rowindex(i) + roffset; // need the global row index
cols[i] = Atuples.colindex(i) + coffset; // need the global col index
}
int * recvcnt = new int[nprocs];
MPI_Alltoall(sendcnt, 1, MPI_INT, recvcnt, 1, MPI_INT, commGrid->GetWorld()); // get the recv counts
int * sdpls = new int[nprocs](); // displacements (zero initialized pid)
int * rdpls = new int[nprocs]();
std::partial_sum(sendcnt, sendcnt+nprocs-1, sdpls+1);
std::partial_sum(recvcnt, recvcnt+nprocs-1, rdpls+1);
MPI_Alltoallv(rows, sendcnt, sdpls, MPIType<IT>(), SpHelper::p2a(nrows.arr), recvcnt, rdpls, MPIType<IT>(), commGrid->GetWorld());
MPI_Alltoallv(cols, sendcnt, sdpls, MPIType<IT>(), SpHelper::p2a(ncols.arr), recvcnt, rdpls, MPIType<IT>(), commGrid->GetWorld());
DeleteAll(sendcnt, recvcnt, sdpls, rdpls);
DeleteAll(prelens, rows, cols, vals);
distrows = nrows;
distcols = ncols;
}
template <class IT, class NT, class DER>
DER SpParMat<IT,NT,DER>::InducedSubgraphs2Procs(const FullyDistVec<IT,IT>& Assignments, std::vector<IT>& LocalIdxs) const
{
int nprocs = commGrid->GetSize();
int myrank = commGrid->GetRank();
int nverts = getnrow();
if (nverts != getncol()) {
SpParHelper::Print("Number of rows and columns differ, not allowed for graphs!\n");
MPI_Abort(MPI_COMM_WORLD, DIMMISMATCH);
}
if (nverts != Assignments.TotalLength()) {
SpParHelper::Print("Assignments vector length does not match number of vertices!\n");
MPI_Abort(MPI_COMM_WORLD, DIMMISMATCH);
}
IT maxproc = Assignments.Reduce(maximum<IT>(), static_cast<IT>(0));
if (maxproc >= static_cast<IT>(nprocs)) {
SpParHelper::Print("Assignments vector assigns to process not not in this group!\n");
MPI_Abort(MPI_COMM_WORLD, INVALIDPARAMS);
}
MPI_Comm World = commGrid->GetWorld();
MPI_Comm RowWorld = commGrid->GetRowWorld();
MPI_Comm ColWorld = commGrid->GetColWorld();
int rowneighs, rowrank;
MPI_Comm_size(RowWorld, &rowneighs);
MPI_Comm_rank(RowWorld, &rowrank);
int mylocsize = Assignments.LocArrSize();
std::vector<int> rowvecs_counts(rowneighs, 0);
std::vector<int> rowvecs_displs(rowneighs, 0);
rowvecs_counts[rowrank] = mylocsize;
MPI_Allgather(MPI_IN_PLACE, 0, MPI_INT, rowvecs_counts.data(), 1, MPI_INT, RowWorld);
/* TODO GRGR: make this resilent if displacements don't fit 32 bits */
std::partial_sum(rowvecs_counts.begin(), rowvecs_counts.end()-1, rowvecs_displs.begin()+1);
size_t rowvecs_size = std::accumulate(rowvecs_counts.begin(), rowvecs_counts.end(), static_cast<size_t>(0));
std::vector<IT> rowvecs(rowvecs_size);
MPI_Allgatherv(Assignments.GetLocArr(), mylocsize, MPIType<IT>(), rowvecs.data(), rowvecs_counts.data(), rowvecs_displs.data(), MPIType<IT>(), RowWorld);
int complement_rank = commGrid->GetComplementRank();
int complement_rowvecs_size = 0;
MPI_Sendrecv(&rowvecs_size, 1, MPI_INT,
complement_rank, TRX,
&complement_rowvecs_size, 1, MPI_INT,
complement_rank, TRX,
World, MPI_STATUS_IGNORE);
std::vector<IT> complement_rowvecs(complement_rowvecs_size);
MPI_Sendrecv(rowvecs.data(), rowvecs_size, MPIType<IT>(),
complement_rank, TRX,
complement_rowvecs.data(), complement_rowvecs_size, MPIType<IT>(),
complement_rank, TRX,
World, MPI_STATUS_IGNORE);
std::vector<std::vector<std::tuple<IT,IT,NT>>> svec(nprocs);
std::vector<int> sendcounts(nprocs, 0);
std::vector<int> recvcounts(nprocs, 0);
std::vector<int> sdispls(nprocs, 0);
std::vector<int> rdispls(nprocs, 0);
int sbuflen = 0;
IT row_offset, col_offset;
GetPlaceInGlobalGrid(row_offset, col_offset);
for (auto colit = spSeq->begcol(); colit != spSeq->endcol(); ++colit) {
IT destproc = complement_rowvecs[colit.colid()];
if (destproc != -1)
for (auto nzit = spSeq->begnz(colit); nzit != spSeq->endnz(colit); ++nzit) {
if (destproc == rowvecs[nzit.rowid()]) {
svec[destproc].emplace_back(row_offset + nzit.rowid(), col_offset + colit.colid(), nzit.value());
sendcounts[destproc]++;
sbuflen++;
}
}
}
MPI_Alltoall(sendcounts.data(), 1, MPI_INT, recvcounts.data(), 1, MPI_INT, World);
size_t rbuflen = std::accumulate(recvcounts.begin(), recvcounts.end(), static_cast<size_t>(0));
std::partial_sum(sendcounts.begin(), sendcounts.end()-1, sdispls.begin()+1);
std::partial_sum(recvcounts.begin(), recvcounts.end()-1, rdispls.begin()+1);
std::tuple<IT,IT,NT> *sbuf = new std::tuple<IT,IT,NT>[sbuflen];
std::tuple<IT,IT,NT> *rbuf = new std::tuple<IT,IT,NT>[rbuflen];
for (int i = 0; i < nprocs; ++i)
std::copy(svec[i].begin(), svec[i].end(), sbuf + sdispls[i]);
MPI_Alltoallv(sbuf, sendcounts.data(), sdispls.data(), MPIType<std::tuple<IT,IT,NT>>(), rbuf, recvcounts.data(), rdispls.data(), MPIType<std::tuple<IT,IT,NT>>(), World);
delete[] sbuf;
LocalIdxs.clear();
LocalIdxs.shrink_to_fit();
std::unordered_map<IT, IT> locmap;
IT new_id = 0;
IT global_ids[2];
for (int i = 0; i < rbuflen; ++i) {
global_ids[0] = std::get<0>(rbuf[i]);
global_ids[1] = std::get<1>(rbuf[i]);
for (int j = 0; j < 2; ++j) {
if (locmap.find(global_ids[j]) == locmap.end()) {
locmap.insert(std::make_pair(global_ids[j], new_id++));
LocalIdxs.push_back(global_ids[j]);
}
}
std::get<0>(rbuf[i]) = locmap[global_ids[0]];
std::get<1>(rbuf[i]) = locmap[global_ids[1]];
}
IT localdim = LocalIdxs.size();
DER LocalMat;
LocalMat.Create(rbuflen, localdim, localdim, rbuf);
return LocalMat;
}
template <class IT, class NT, class DER>
std::ofstream& SpParMat<IT,NT,DER>::put(std::ofstream& outfile) const
{
outfile << (*spSeq) << std::endl;
return outfile;
}
template <class IU, class NU, class UDER>
std::ofstream& operator<<(std::ofstream& outfile, const SpParMat<IU, NU, UDER> & s)
{
return s.put(outfile) ; // use the right put() function
}
/**
* @param[in] grow {global row index}
* @param[in] gcol {global column index}
* @param[out] lrow {row index local to the owner}
* @param[out] lcol {col index local to the owner}
* @returns {owner processor id}
**/
template <class IT, class NT,class DER>
template <typename LIT>
int SpParMat<IT,NT,DER>::Owner(IT total_m, IT total_n, IT grow, IT gcol, LIT & lrow, LIT & lcol) const
{
int procrows = commGrid->GetGridRows();
int proccols = commGrid->GetGridCols();
IT m_perproc = total_m / procrows;
IT n_perproc = total_n / proccols;
int own_procrow; // owner's processor row
if(m_perproc != 0)
{
own_procrow = std::min(static_cast<int>(grow / m_perproc), procrows-1); // owner's processor row
}
else // all owned by the last processor row
{
own_procrow = procrows -1;
}
int own_proccol;
if(n_perproc != 0)
{
own_proccol = std::min(static_cast<int>(gcol / n_perproc), proccols-1);
}
else
{
own_proccol = proccols-1;
}
lrow = grow - (own_procrow * m_perproc);
lcol = gcol - (own_proccol * n_perproc);
return commGrid->GetRank(own_procrow, own_proccol);
}
/**
* @param[out] rowOffset {Row offset imposed by process grid. Global row index = rowOffset + local row index.}
* @param[out] colOffset {Column offset imposed by process grid. Global column index = colOffset + local column index.}
**/
template <class IT, class NT,class DER>
void SpParMat<IT,NT,DER>::GetPlaceInGlobalGrid(IT& rowOffset, IT& colOffset) const
{
IT total_rows = getnrow();
IT total_cols = getncol();
int procrows = commGrid->GetGridRows();
int proccols = commGrid->GetGridCols();
IT rows_perproc = total_rows / procrows;
IT cols_perproc = total_cols / proccols;
rowOffset = commGrid->GetRankInProcCol()*rows_perproc;
colOffset = commGrid->GetRankInProcRow()*cols_perproc;
}
}
|