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
|
//******************************************************************************
//
// File: Comm.java
// Package: edu.rit.pj
// Unit: Class edu.rit.pj.Comm
//
// This Java source file is copyright (C) 2008 by Alan Kaminsky. All rights
// reserved. For further information, contact the author, Alan Kaminsky, at
// ark@cs.rit.edu.
//
// This Java source file is part of the Parallel Java Library ("PJ"). PJ is free
// software; you can redistribute it and/or modify it under the terms of the GNU
// General Public License as published by the Free Software Foundation; either
// version 3 of the License, or (at your option) any later version.
//
// PJ is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
// A PARTICULAR PURPOSE. See the GNU General Public License for more details.
//
// A copy of the GNU General Public License is provided in the file gpl.txt. You
// may also obtain a copy of the GNU General Public License on the World Wide
// Web at http://www.gnu.org/licenses/gpl.html.
//
//******************************************************************************
package edu.rit.pj;
import edu.rit.mp.Buf;
import edu.rit.mp.Channel;
import edu.rit.mp.ChannelGroup;
import edu.rit.mp.ConnectListener;
import edu.rit.mp.IORequest;
import edu.rit.mp.IntegerBuf;
import edu.rit.mp.ObjectBuf;
import edu.rit.mp.Status;
import edu.rit.pj.cluster.CommPattern;
import edu.rit.pj.cluster.JobBackend;
import edu.rit.pj.cluster.JobFrontend;
import edu.rit.pj.cluster.JobSchedulerException;
import edu.rit.pj.reduction.IntegerOp;
import edu.rit.pj.reduction.Op;
import edu.rit.util.Range;
import java.io.IOException;
import java.io.InterruptedIOException;
import java.io.PrintStream;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.util.HashMap;
import java.util.LinkedList;
/**
* Class Comm provides a communicator for a PJ cluster parallel program. Class
* Comm provides a method to initialize the PJ message passing middleware and
* run the parallel program on multiple processors of a cluster parallel
* computer. Class Comm also provides methods for passing messages between the
* processes of the parallel program.
* <P>
* <HR>
* <P>
* <B>BASIC CONCEPTS</B>
* <P>
* A <B>cluster parallel computer</B> typically consists of a <B>frontend
* processor</B> and a number of <B>backend processors</B> connected via a
* dedicated high-speed network. A user logs into the frontend processor and
* runs a PJ program there. The PJ message passing middleware causes copies of
* the PJ program to run in a number of separate processes, each process on a
* different backend processor. The backend processes run the PJ program, using
* the PJ middleware to send messages amongst themselves. The PJ middleware
* redirects the backend processes' standard output and standard error streams
* to the frontend process. The frontend process does not actually execute the
* PJ program, but merely displays all the backend processes' standard output
* and standard error streams on the frontend process's own standard output and
* standard error.
* <P>
* For the PJ message passing middleware to work, certain server processes must
* be running. See package {@linkplain edu.rit.pj.cluster} for further
* information.
* <P>
* To initialize the PJ message passing middleware, the program must first call
* the static <TT>Comm.init()</TT> method, passing in the command line
* arguments.
* <P>
* A <B>communicator</B> is associated with a group of backend processes. The
* communicator's <B>size</B> is the number of processes in the group. Each
* process in the communicator has a different <B>rank</B> in the range 0 ..
* <I>size</I>-1. A process may obtain the size and rank by calling the
* communicator's <TT>size()</TT> and <TT>rank()</TT> methods.
* <P>
* There is one predefined communicator, the <B>world communicator,</B>
* consisting of all the backend processes in the parallel program. A process
* may obtain a reference to the world communicator by calling the static
* <TT>Comm.world()</TT> method. Typically, the first few lines in a PJ cluster
* parallel program look like this:
* <PRE>
* public class AParallelProgram
* {
* public static void main
* (String[] args)
* throws Exception
* {
* Comm.init (args);
* Comm world = Comm.world();
* int size = world.size();
* int rank = world.rank();
* . . .</PRE>
* <P>
* The number of processes in the parallel program -- that is, the size of the
* world communicator -- is specified by the <TT>"pj.np"</TT> property, which
* must be an integer greater than or equal to 1. You can specify the number of
* processes on the Java command line like this:
* <PRE>
* java -Dpj.np=4 . . .</PRE>
* <P>
* If the <TT>"pj.np"</TT> property is not specified, the default is 1.
* <P>
* The PJ program will run on the specified number of backend processors as
* described above. To determine which backend processors to use, the PJ program
* interacts with a <B>Job Scheduler</B> server process running on the frontend
* processor. When the PJ program starts and calls the <TT>Comm.init()</TT>
* method, the middleware first prints the job number on the standard error. The
* middleware then waits until the required number of backend processors are
* ready to run a job. As each backend processor becomes ready, the middleware
* prints on the standard error the name of each backend processor assigned to
* the job. Once all are ready, the PJ program starts running on those backend
* processors, and all further output comes from the PJ program. Since each PJ
* program interacts with the Job Scheduler, the Job Scheduler can ensure that
* each backend processor is running a backend process for only one job at a
* time.
* <P>
* Depending on the system load, your PJ program may have to wait in the Job
* Scheduler's queue for a while until enough backend processors become ready.
* If you get tired of waiting, you can kill your PJ program (e.g., by typing
* CTRL-C), which will remove your PJ program from the Job Scheduler's queue.
* <P>
* The Job Scheduler has a web interface that lets you examine the cluster
* status. Just point your web browser at this URL:
* <P>
* <TT> http://<hostname>:8080/</TT>
* <P>
* where <TT><hostname></TT> is replaced by the host name of the frontend
* processor. The default port for the cluster status web interface is port
* 8080. The Job Scheduler can be configured to use a different port. For
* further information, see package {@linkplain edu.rit.pj.cluster}.
* <P>
* If the PJ program is executed on a host where no Job Scheduler is running,
* the PJ program will run in <I>one</I> process on that host (i.e., the machine
* you're logged into), rather than on the backend processors. The message
* passing methods in class Comm will still work, though. This option can be
* useful for debugging a PJ program's logic on a non-parallel machine before
* running the PJ program on a cluster.
* <P>
* <HR>
* <P>
* <B>MESSAGE PASSING</B>
* <P>
* PJ provides two categories of communication, <B>point-to-point
* communication</B> and <B>collective communication.</B> The following methods
* of class Comm are used for point-to-point communication:
* <UL>
* <LI><TT>send()</TT>
* <LI><TT>receive()</TT>
* <LI><TT>sendReceive()</TT>
* <LI><TT>floodSend()</TT>
* <LI><TT>floodReceive()</TT>
* </UL>
* The following methods are used for collective communication:
* <UL>
* <LI><TT>broadcast()</TT>
* <LI><TT>scatter()</TT>
* <LI><TT>gather()</TT>
* <LI><TT>allGather()</TT>
* <LI><TT>reduce()</TT>
* <LI><TT>allReduce()</TT>
* <LI><TT>allToAll()</TT>
* <LI><TT>scan()</TT>
* <LI><TT>exclusiveScan()</TT>
* <LI><TT>barrier()</TT>
* </UL>
* These methods are described further in the sections below.
* <P>
* In addition, you can create a new communicator consisting of all, or a subset
* of, the processes in an existing communicator. Message passing in the new
* communicator is completely independent of message passing in the original
* communicator. The following method creates a new communicator:
* <UL>
* <LI><TT>createComm()</TT>
* </UL>
* <P>
* <HR>
* <P>
* <B>POINT-TO-POINT COMMUNICATION</B>
* <P>
* One process in a PJ cluster parallel program, the <B>source process</B>, may
* use a communicator to send a message to another process in the program, the
* <B>destination process</B>. This is called a <B>point-to-point
* communication</B> because just the two processes are involved (as opposed to
* a collective communication, which involves all the processes). Five
* point-to-point communication methods are available in this release: send,
* receive, send-receive, flood-send, and flood-receive.
* <P>
* <B>Send and Receive</B>
* <P>
* To do a point-to-point communication, the source process calls the
* <TT>send()</TT> method on a certain communicator, such as the world
* communicator. The source process specifies the destination process's rank,
* the <B>tag</B> for the message, and a <B>buffer</B> containing the data items
* to be sent (type {@linkplain edu.rit.mp.Buf}). Likewise, the destination
* process calls the <TT>receive()</TT> method on the same communicator as the
* source process. The destination process specifies the source process's rank,
* the message tag which must be the same as in the source process, and the
* buffer for the data items to be received.
* <P>
* A <TT>send()</TT> method call and a <TT>receive()</TT> method call are said
* to <B>match</B> if (a) the rank passed to the <TT>send()</TT> method equals
* the rank of the process calling <TT>receive()</TT>, (b) the rank passed to
* the <TT>receive()</TT> method equals the rank of the process calling
* <TT>send()</TT>, (c) the item data type in the source buffer is the same as
* the item data type in the destination buffer, and (d) the send message tag
* equals the receive message tag. A <TT>receive()</TT> method call will block
* until a matching <TT>send()</TT> method call occurs. If more than one
* <TT>send()</TT> method call matches a <TT>receive()</TT> method call, one of
* the matching <TT>send()</TT> method calls is picked in an unspecified manner.
* A <TT>send()</TT> method call <I>may</I> block until a matching
* <TT>receive()</TT> method call occurs due to flow control in the underlying
* network communication.
* <P>
* The message tag can be used to distinguish different kinds of messages. A
* <TT>receive()</TT> method call will only match a <TT>send()</TT> method call
* with the same tag. If there is no need to distinguish different kinds of
* messages, omit the tag (it will default to 0).
* <P>
* Once a <TT>send()</TT> method call and a <TT>receive()</TT> method call have
* been matched together, the actual message data transfer takes place. Each
* item in the source buffer, starting at index 0 and continuing for the entire
* length of the source buffer, is written to the message. At the other end,
* each item in the destination buffer, starting at index 0, is read from the
* message.
* <P>
* The <TT>receive()</TT> method returns a {@linkplain CommStatus} object. The
* status object gives the actual rank of the process that sent the message, the
* actual message tag that was received, and the actual number of data items in
* the message. If the actual number of data items in the message is less than
* the length of the destination buffer, nothing is stored into the extra data
* items at the end of the destination buffer. If the actual number of data
* items in the message is greater than the length of the destination buffer,
* the extra data items at the end of the message are discarded.
* <P>
* The <TT>send()</TT> method does not return until all the message elements
* have been written from the source buffer. Likewise, the <TT>receive()</TT>
* method does not return until all the message elements have been read into the
* destination buffer. However, you cannot assume that because the
* <TT>send()</TT> method has returned, the matching <TT>receive()</TT> method
* has also returned. Because of buffering in the underlying network
* communication, not all the destination items might have been received even
* though all the source items have been sent.
* <P>
* The destination process, instead of specifying a particular source process,
* can declare that it will receive a message from any source process by
* specifying null for the source process rank in the <TT>receive()</TT> method
* call. This is called a <B>wildcard source</B>. The destination process,
* instead of specifying a particular message tag, can declare that it will
* receive a message with any tag by specifying null for the tag in the
* <TT>receive()</TT> method call. This is called a <B>wildcard tag</B>. The
* destination process can specify either a wildcard source, a wildcard tag, or
* both. In this case the <TT>receive()</TT> method call's returned status
* object will indicate the actual source process that sent the message and the
* actual message tag that was sent.
* <P>
* A process can send a message to itself. In this case one thread must call
* <TT>send()</TT> (specifying the process's own rank as the destination) and a
* different thread must call <TT>receive()</TT> (specifying the process's own
* rank as the source), otherwise a deadlock will ensue.
* <P>
* <B>Send-Receive</B>
* <P>
* By calling the <TT>sendReceive()</TT> method, a process can send a buffer of
* outgoing message items to a destination process while simultaneously
* receiving a buffer of incoming message items from a source process. The
* destination process may be the same as the source process, or different from
* the source process. The outgoing message items must come from a different
* place than where the incoming message items will be stored, otherwise the
* incoming message items may overwrite the outgoing message items before they
* can be sent. When the <TT>sendReceive()</TT> method returns, the outgoing
* message items have been fully sent, but they may not yet have been fully
* received; and the incoming message items have been fully received.
* <P>
* Unlike the <TT>receive()</TT> method, a process cannot receive a message from
* a wildcard source or receive a message with a wildcard tag. The process
* calling <TT>sendReceive()</TT> must know the rank of the source process and
* the message tag if any. However, the <TT>sendReceive()</TT> method does
* return a status object giving the outcome of the receive half of the
* send-receive operation, just as the <TT>receive()</TT> method does.
* <P>
* A process can send-receive messages with itself. In this case one thread must
* call <TT>sendReceive()</TT> (specifying the process's own rank as the source
* and destination) and a different thread must also call <TT>sendReceive()</TT>
* (specifying the process's own rank as the source and destination), otherwise
* a deadlock will ensue.
* <P>
* <B>Non-Blocking Communication</B>
* <P>
* The <TT>send()</TT>, <TT>receive()</TT>, and <TT>sendReceive()</TT> methods
* each have a non-blocking version. A non-blocking communication method
* initiates the communication operation and immediately returns, storing the
* state of the communication operation in a {@linkplain CommRequest} object.
* The communicator then performs the communication operation in a separate
* thread. This allows the calling thread to do other work while the
* communication operation is in progress. To wait for the send and receive
* operations to finish, call the CommRequest object's <TT>waitForFinish()</TT>
* method.
* <P>
* <B>Flood-Send and Flood-Receive</B>
* <P>
* Any process can send a message to all processes in the communicator. This is
* called "flooding" the message. First, all processes must start a
* flood-receive operation, either by calling the non-blocking
* <TT>floodReceive()</TT> method, or by having a separate thread call the
* blocking <TT>floodReceive()</TT> method. Then, one process (any process) must
* call the <TT>floodSend()</TT> method. The data items in the flood-send
* operation's outgoing buffer are copied into the flood-receive operation's
* incoming buffer in all processes.
* <P>
* Message flooding is similar to the "broadcast" collective communication
* operation (see below). The differences are these: Broadcasting combines
* sending and receiving in a single operation; flooding uses separate send and
* receive operations. For broadcasting, every process must know which process
* is sending the outgoing data items; for flooding, the receiving processes do
* not need to know which process is sending (any process can send).
* <P>
* <HR>
* <P>
* <B>COLLECTIVE COMMUNICATION</B>
* <P>
* A PJ cluster parallel program may use a communicator to send a message among
* all the processes in the program at the same time. This is called a
* <B>collective communication</B> because all the processes in the communicator
* are involved (as opposed to a point-to-point communication). Ten collective
* communication methods are available in this release: broadcast, scatter,
* gather, all-gather, reduce, all-reduce, all-to-all, scan, exclusive-scan, and
* barrier. Further collective communication methods will be added to class Comm
* in a later release.
* <P>
* <B>Broadcast</B>
* <P>
* One process in the communicator, the <B>root</B> process, has a source buffer
* (type {@linkplain edu.rit.mp.Buf Buf}) filled with data. The other processes
* in the communicator each have a destination buffer with the same length and
* the same item data type as the source buffer. Each process calls the
* communicator's <TT>broadcast()</TT> method. Afterwards, all the destination
* buffers contain the same data as the source buffer.
* <P>
* <TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0>
* <TR>
* <TD ALIGN="left" VALIGN="top">
* Before:
* <FONT SIZE="-1">
* <PRE>
* Process Process Process Process
* 0 (root) 1 2 3
* +----+ +----+ +----+ +----+
* | 1 | | | | | | |
* | 2 | | | | | | |
* | 3 | | | | | | |
* | 4 | | | | | | |
* | 5 | | | | | | |
* | 6 | | | | | | |
* | 7 | | | | | | |
* | 8 | | | | | | |
* +----+ +----+ +----+ +----+</PRE>
* </FONT>
* </TD>
* <TD WIDTH=50> </TD>
* <TD ALIGN="left" VALIGN="top">
* After:
* <FONT SIZE="-1">
* <PRE>
* Process Process Process Process
* 0 (root) 1 2 3
* +----+ +----+ +----+ +----+
* | 1 | | 1 | | 1 | | 1 |
* | 2 | | 2 | | 2 | | 2 |
* | 3 | | 3 | | 3 | | 3 |
* | 4 | | 4 | | 4 | | 4 |
* | 5 | | 5 | | 5 | | 5 |
* | 6 | | 6 | | 6 | | 6 |
* | 7 | | 7 | | 7 | | 7 |
* | 8 | | 8 | | 8 | | 8 |
* +----+ +----+ +----+ +----+</PRE>
* </FONT>
* </TD>
* </TR>
* </TABLE>
* <I>Note:</I> Any process can be the root of the broadcast. The above is only
* one example with process 0 as the root.
* <P>
* <B>Scatter</B>
* <P>
* One process in the communicator, the root process, has <I>K</I> source
* buffers (type {@linkplain edu.rit.mp.Buf Buf}) filled with data, where
* <I>K</I> is the size of the communicator. For example, the source buffers
* could be different portions of an array. Each process in the communicator
* (including the root process) has a destination buffer with the same length
* and the same item data type as the corresponding source buffer. Each process
* calls the communicator's <TT>scatter()</TT> method. Afterwards, each
* process's destination buffer contains the same data as the corresponding
* source buffer in the root process.
* <P>
* <TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0>
* <TR>
* <TD ALIGN="left" VALIGN="top">
* Before:
* <FONT SIZE="-1">
* <PRE>
* Process Process Process Process
* 0 (root) 1 2 3
* +----+
* | 1 |
* | 2 |
* +----+
* | 3 |
* | 4 |
* +----+
* | 5 |
* | 6 |
* +----+
* | 7 |
* | 8 |
* +----+
*
* +----+ +----+ +----+ +----+
* | | | | | | | |
* | | | | | | | |
* +----+ +----+ +----+ +----+</PRE>
* </FONT>
* </TD>
* <TD WIDTH=50> </TD>
* <TD ALIGN="left" VALIGN="top">
* After:
* <FONT SIZE="-1">
* <PRE>
* Process Process Process Process
* 0 (root) 1 2 3
* +----+
* | 1 |
* | 2 |
* +----+
* | 3 |
* | 4 |
* +----+
* | 5 |
* | 6 |
* +----+
* | 7 |
* | 8 |
* +----+
*
* +----+ +----+ +----+ +----+
* | 1 | | 3 | | 5 | | 7 |
* | 2 | | 4 | | 6 | | 8 |
* +----+ +----+ +----+ +----+</PRE>
* </FONT>
* </TD>
* </TR>
* </TABLE>
* In the root process, the destination buffer can be the same as the source
* buffer:
* <P>
* <TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0>
* <TR>
* <TD ALIGN="left" VALIGN="top">
* Before:
* <FONT SIZE="-1">
* <PRE>
* Process Process Process Process
* 0 (root) 1 2 3
* +----+
* | 1 |
* | 2 |
* +----+ +----+
* | 3 | | |
* | 4 | | |
* +----+ +----+ +----+
* | 5 | | |
* | 6 | | |
* +----+ +----+ +----+
* | 7 | | |
* | 8 | | |
* +----+ +----+</PRE>
* </FONT>
* </TD>
* <TD WIDTH=50> </TD>
* <TD ALIGN="left" VALIGN="top">
* After:
* <FONT SIZE="-1">
* <PRE>
* Process Process Process Process
* 0 (root) 1 2 3
* +----+
* | 1 |
* | 2 |
* +----+ +----+
* | 3 | | 3 |
* | 4 | | 4 |
* +----+ +----+ +----+
* | 5 | | 5 |
* | 6 | | 6 |
* +----+ +----+ +----+
* | 7 | | 7 |
* | 8 | | 8 |
* +----+ +----+</PRE>
* </FONT>
* </TD>
* </TR>
* </TABLE>
* <I>Note:</I> Any process can be the root of the scatter. The above is only
* one example with process 0 as the root.
* <P>
* <B>Gather</B>
* <P>
* Gather is the opposite of scatter. One process in the communicator, the root
* process, has <I>K</I> destination buffers (type {@linkplain edu.rit.mp.Buf
* Buf}), where <I>K</I> is the size of the communicator. For example, the
* destination buffers could be different portions of an array. Each process in
* the communicator (including the root process) has a source buffer with the
* same length and the same item data type as the corresponding destination
* buffer, filled with data. Each process calls the communicator's
* <TT>gather()</TT> method. Afterwards, each destination buffer in the root
* process contains the same data as the corresponding source buffer.
* <P>
* <TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0>
* <TR>
* <TD ALIGN="left" VALIGN="top">
* Before:
* <FONT SIZE="-1">
* <PRE>
* Process Process Process Process
* 0 (root) 1 2 3
* +----+ +----+ +----+ +----+
* | 1 | | 3 | | 5 | | 7 |
* | 2 | | 4 | | 6 | | 8 |
* +----+ +----+ +----+ +----+
*
* +----+
* | |
* | |
* +----+
* | |
* | |
* +----+
* | |
* | |
* +----+
* | |
* | |
* +----+</PRE>
* </FONT>
* </TD>
* <TD WIDTH=50> </TD>
* <TD ALIGN="left" VALIGN="top">
* After:
* <FONT SIZE="-1">
* <PRE>
* Process Process Process Process
* 0 (root) 1 2 3
* +----+ +----+ +----+ +----+
* | 1 | | 3 | | 5 | | 7 |
* | 2 | | 4 | | 6 | | 8 |
* +----+ +----+ +----+ +----+
*
* +----+
* | 1 |
* | 2 |
* +----+
* | 3 |
* | 4 |
* +----+
* | 5 |
* | 6 |
* +----+
* | 7 |
* | 8 |
* +----+</PRE>
* </FONT>
* </TD>
* </TR>
* </TABLE>
* In the root process, the destination buffer can be the same as the source
* buffer:
* <P>
* <TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0>
* <TR>
* <TD ALIGN="left" VALIGN="top">
* Before:
* <FONT SIZE="-1">
* <PRE>
* Process Process Process Process
* 0 (root) 1 2 3
* +----+
* | 1 |
* | 2 |
* +----+ +----+
* | | | 3 |
* | | | 4 |
* +----+ +----+ +----+
* | | | 5 |
* | | | 6 |
* +----+ +----+ +----+
* | | | 7 |
* | | | 8 |
* +----+ +----+</PRE>
* </FONT>
* </TD>
* <TD WIDTH=50> </TD>
* <TD ALIGN="left" VALIGN="top">
* After:
* <FONT SIZE="-1">
* <PRE>
* Process Process Process Process
* 0 (root) 1 2 3
* +----+
* | 1 |
* | 2 |
* +----+ +----+
* | 3 | | 3 |
* | 4 | | 4 |
* +----+ +----+ +----+
* | 5 | | 5 |
* | 6 | | 6 |
* +----+ +----+ +----+
* | 7 | | 7 |
* | 8 | | 8 |
* +----+ +----+</PRE>
* </FONT>
* </TD>
* </TR>
* </TABLE>
* <I>Note:</I> Any process can be the root of the gather. The above is only
* one example with process 0 as the root.
* <P>
* <B>All-Gather</B>
* <P>
* All-gather is the same as gather, except that every process has an array of
* destination buffers, and every process receives the results of the gather.
* Each process in the communicator has a source buffer (type {@linkplain
* edu.rit.mp.Buf Buf}) filled with data. Each process in the communicator has
* <I>K</I> destination buffers, where <I>K</I> is the size of the communicator.
* For example, the destination buffers could be different portions of an array.
* Each destination buffer has the same length and the same item data type as
* the corresponding source buffer. Each process calls the communicator's
* <TT>allGather()</TT> method. Afterwards, each destination buffer in every
* process contains the same data as the corresponding source buffer.
* <P>
* <TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0>
* <TR>
* <TD ALIGN="left" VALIGN="top">
* Before:
* <FONT SIZE="-1">
* <PRE>
* Process Process Process Process
* 0 1 2 3
* +----+ +----+ +----+ +----+
* | 1 | | 3 | | 5 | | 7 |
* | 2 | | 4 | | 6 | | 8 |
* +----+ +----+ +----+ +----+
*
* +----+ +----+ +----+ +----+
* | | | | | | | |
* | | | | | | | |
* +----+ +----+ +----+ +----+
* | | | | | | | |
* | | | | | | | |
* +----+ +----+ +----+ +----+
* | | | | | | | |
* | | | | | | | |
* +----+ +----+ +----+ +----+
* | | | | | | | |
* | | | | | | | |
* +----+ +----+ +----+ +----+</PRE>
* </FONT>
* </TD>
* <TD WIDTH=50> </TD>
* <TD ALIGN="left" VALIGN="top">
* After:
* <FONT SIZE="-1">
* <PRE>
* Process Process Process Process
* 0 1 2 3
* +----+ +----+ +----+ +----+
* | 1 | | 3 | | 5 | | 7 |
* | 2 | | 4 | | 6 | | 8 |
* +----+ +----+ +----+ +----+
*
* +----+ +----+ +----+ +----+
* | 1 | | 1 | | 1 | | 1 |
* | 2 | | 2 | | 2 | | 2 |
* +----+ +----+ +----+ +----+
* | 3 | | 3 | | 3 | | 3 |
* | 4 | | 4 | | 4 | | 4 |
* +----+ +----+ +----+ +----+
* | 5 | | 5 | | 5 | | 5 |
* | 6 | | 6 | | 6 | | 6 |
* +----+ +----+ +----+ +----+
* | 7 | | 7 | | 7 | | 7 |
* | 8 | | 8 | | 8 | | 8 |
* +----+ +----+ +----+ +----+</PRE>
* </FONT>
* </TD>
* </TR>
* </TABLE>
* The destination buffer can be the same as the source buffer in each process:
* <P>
* <TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0>
* <TR>
* <TD ALIGN="left" VALIGN="top">
* Before:
* <FONT SIZE="-1">
* <PRE>
* Process Process Process Process
* 0 1 2 3
* +----+ +----+ +----+ +----+
* | 1 | | | | | | |
* | 2 | | | | | | |
* +----+ +----+ +----+ +----+
* | | | 3 | | | | |
* | | | 4 | | | | |
* +----+ +----+ +----+ +----+
* | | | | | 5 | | |
* | | | | | 6 | | |
* +----+ +----+ +----+ +----+
* | | | | | | | 7 |
* | | | | | | | 8 |
* +----+ +----+ +----+ +----+</PRE>
* </FONT>
* </TD>
* <TD WIDTH=50> </TD>
* <TD ALIGN="left" VALIGN="top">
* After:
* <FONT SIZE="-1">
* <PRE>
* Process Process Process Process
* 0 1 2 3
* +----+ +----+ +----+ +----+
* | 1 | | 1 | | 1 | | 1 |
* | 2 | | 2 | | 2 | | 2 |
* +----+ +----+ +----+ +----+
* | 3 | | 3 | | 3 | | 3 |
* | 4 | | 4 | | 4 | | 4 |
* +----+ +----+ +----+ +----+
* | 5 | | 5 | | 5 | | 5 |
* | 6 | | 6 | | 6 | | 6 |
* +----+ +----+ +----+ +----+
* | 7 | | 7 | | 7 | | 7 |
* | 8 | | 8 | | 8 | | 8 |
* +----+ +----+ +----+ +----+</PRE>
* </FONT>
* </TD>
* </TR>
* </TABLE>
* <P>
* <B>Reduce</B>
* <P>
* Reduce is like gather, except the buffers' contents are combined together
* instead of just copied. Each process in the communicator has a buffer (type
* {@linkplain edu.rit.mp.Buf Buf}) filled with data. Each process calls the
* communicator's <TT>reduce()</TT> method, specifying some binary operation
* (type {@linkplain edu.rit.pj.reduction.Op Op}) for combining the data.
* Afterwards, each element of the buffer in the root process contains the
* result of combining all the corresponding elements in all the buffers using
* the specified binary operation. For example, if the operation is addition,
* each buffer element in the root process ends up being the sum of the
* corresponding buffer elements in all the processes. In the non-root
* processes, the buffers' contents may be changed from their original contents.
* <P>
* <TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0>
* <TR>
* <TD ALIGN="left" VALIGN="top">
* Before:
* <FONT SIZE="-1">
* <PRE>
* Process Process Process Process
* 0 (root) 1 2 3
* +----+ +----+ +----+ +----+
* | 1 | | 3 | | 5 | | 7 |
* | 2 | | 4 | | 6 | | 8 |
* +----+ +----+ +----+ +----+</PRE>
* </FONT>
* </TD>
* <TD WIDTH=50> </TD>
* <TD ALIGN="left" VALIGN="top">
* After:
* <FONT SIZE="-1">
* <PRE>
* Process Process Process Process
* 0 (root) 1 2 3
* +----+ +----+ +----+ +----+
* | 16 | | ?? | | ?? | | ?? |
* | 20 | | ?? | | ?? | | ?? |
* +----+ +----+ +----+ +----+</PRE>
* </FONT>
* </TD>
* </TR>
* </TABLE>
* <I>Note:</I> Any process can be the root of the reduction. The above is only
* one example with process 0 as the root.
* <P>
* <B>All-Reduce</B>
* <P>
* All-reduce is the same as reduce, except that every process receives the
* results of the reduction. Each process in the communicator has a buffer (type
* {@linkplain edu.rit.mp.Buf Buf}) filled with data. Each process calls the
* communicator's <TT>allReduce()</TT> method, specifying some binary operation
* (type {@linkplain edu.rit.pj.reduction.Op Op}) for combining the data.
* Afterwards, each element of the buffer in each process contains the result of
* combining all the corresponding elements in all the buffers using the
* specified binary operation. For example, if the operation is addition, each
* buffer element ends up being the sum of the corresponding buffer elements.
* <P>
* <TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0>
* <TR>
* <TD ALIGN="left" VALIGN="top">
* Before:
* <FONT SIZE="-1">
* <PRE>
* Process Process Process Process
* 0 1 2 3
* +----+ +----+ +----+ +----+
* | 1 | | 3 | | 5 | | 7 |
* | 2 | | 4 | | 6 | | 8 |
* +----+ +----+ +----+ +----+</PRE>
* </FONT>
* </TD>
* <TD WIDTH=50> </TD>
* <TD ALIGN="left" VALIGN="top">
* After:
* <FONT SIZE="-1">
* <PRE>
* Process Process Process Process
* 0 1 2 3
* +----+ +----+ +----+ +----+
* | 16 | | 16 | | 16 | | 16 |
* | 20 | | 20 | | 20 | | 20 |
* +----+ +----+ +----+ +----+</PRE>
* </FONT>
* </TD>
* </TR>
* </TABLE>
* <P>
* <B>All-to-All</B>
* <P>
* Every process in the communicator has <I>K</I> source buffers (type
* {@linkplain edu.rit.mp.Buf Buf}) filled with data, where <I>K</I> is the size
* of the communicator. Every process in the communicator also has <I>K</I>
* destination buffers (type {@linkplain edu.rit.mp.Buf Buf}). The source
* buffers and the destination buffers must refer to different storage. For
* example, the source buffers could be portions of an array, and the
* destination buffers could be portions of a different array. Each process
* calls the communicator's <TT>allToAll()</TT> method. Afterwards, for each
* process rank <I>k</I>, 0 <= <I>k</I> <= <I>K</I>-1, and each buffer
* index <I>i</I>, 0 <= <I>i</I> <= <I>K</I>-1, destination buffer
* <I>i</I> in process <I>k</I> contains the same data as source buffer <I>k</I>
* in process <I>i</I>.
* <P>
* <TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0>
* <TR>
* <TD ALIGN="left" VALIGN="top">
* Before:
* <FONT SIZE="-1">
* <PRE>
* Process Process Process Process
* 0 1 2 3
* +----+ +----+ +----+ +----+
* | 1 | | 9 | | 17 | | 25 |
* | 2 | | 10 | | 18 | | 26 |
* +----+ +----+ +----+ +----+
* | 3 | | 11 | | 19 | | 27 |
* | 4 | | 12 | | 20 | | 28 |
* +----+ +----+ +----+ +----+
* | 5 | | 13 | | 21 | | 29 |
* | 6 | | 14 | | 22 | | 30 |
* +----+ +----+ +----+ +----+
* | 7 | | 15 | | 23 | | 31 |
* | 8 | | 16 | | 24 | | 32 |
* +----+ +----+ +----+ +----+
*
* +----+ +----+ +----+ +----+
* | | | | | | | |
* | | | | | | | |
* +----+ +----+ +----+ +----+
* | | | | | | | |
* | | | | | | | |
* +----+ +----+ +----+ +----+
* | | | | | | | |
* | | | | | | | |
* +----+ +----+ +----+ +----+
* | | | | | | | |
* | | | | | | | |
* +----+ +----+ +----+ +----+</PRE>
* </FONT>
* </TD>
* <TD WIDTH=50> </TD>
* <TD ALIGN="left" VALIGN="top">
* After:
* <FONT SIZE="-1">
* <PRE>
* Process Process Process Process
* 0 1 2 3
* +----+ +----+ +----+ +----+
* | 1 | | 9 | | 17 | | 25 |
* | 2 | | 10 | | 18 | | 26 |
* +----+ +----+ +----+ +----+
* | 3 | | 11 | | 19 | | 27 |
* | 4 | | 12 | | 20 | | 28 |
* +----+ +----+ +----+ +----+
* | 5 | | 13 | | 21 | | 29 |
* | 6 | | 14 | | 22 | | 30 |
* +----+ +----+ +----+ +----+
* | 7 | | 15 | | 23 | | 31 |
* | 8 | | 16 | | 24 | | 32 |
* +----+ +----+ +----+ +----+
*
* +----+ +----+ +----+ +----+
* | 1 | | 3 | | 5 | | 7 |
* | 2 | | 4 | | 6 | | 8 |
* +----+ +----+ +----+ +----+
* | 9 | | 11 | | 13 | | 15 |
* | 10 | | 12 | | 14 | | 16 |
* +----+ +----+ +----+ +----+
* | 17 | | 19 | | 21 | | 23 |
* | 18 | | 20 | | 22 | | 24 |
* +----+ +----+ +----+ +----+
* | 25 | | 27 | | 29 | | 31 |
* | 26 | | 28 | | 30 | | 32 |
* +----+ +----+ +----+ +----+</PRE>
* </FONT>
* </TD>
* </TR>
* </TABLE>
* <P>
* <B>Scan</B>
* <P>
* Each process in the communicator has a buffer (type {@linkplain
* edu.rit.mp.Buf Buf}) filled with data. Each process calls the communicator's
* <TT>scan()</TT> method, specifying some binary operation (type {@linkplain
* edu.rit.pj.reduction.Op Op}) for combining the data. Afterwards, each element
* of the buffer in a particular process contains the result of combining all
* the corresponding elements in its own and all lower-ranked processes' buffers
* using the specified binary operation. For example, if the operation is
* addition, each buffer element ends up being the sum of its own and all
* lower-ranked processes' buffer elements.
* <P>
* <TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0>
* <TR>
* <TD ALIGN="left" VALIGN="top">
* Before:
* <FONT SIZE="-1">
* <PRE>
* Process Process Process Process
* 0 1 2 3
* +----+ +----+ +----+ +----+
* | 1 | | 3 | | 5 | | 7 |
* | 2 | | 4 | | 6 | | 8 |
* +----+ +----+ +----+ +----+</PRE>
* </FONT>
* </TD>
* <TD WIDTH=50> </TD>
* <TD ALIGN="left" VALIGN="top">
* After:
* <FONT SIZE="-1">
* <PRE>
* Process Process Process Process
* 0 1 2 3
* +----+ +----+ +----+ +----+
* | 1 | | 4 | | 9 | | 16 |
* | 2 | | 6 | | 12 | | 20 |
* +----+ +----+ +----+ +----+</PRE>
* </FONT>
* </TD>
* </TR>
* </TABLE>
* The scan operation is also known as "prefix scan" or "inclusive prefix scan"
* -- "inclusive" because the process's own element is included in the result.
* <P>
* <B>Exclusive-Scan</B>
* <P>
* The exclusive-scan operation is a variation of the scan operation. Each
* process in the communicator has a buffer (type {@linkplain edu.rit.mp.Buf
* Buf}) filled with data. Each process calls the communicator's
* <TT>exclusiveScan()</TT> method, specifying some binary operation (type
* {@linkplain edu.rit.pj.reduction.Op Op}) for combining the data, and
* specifying an initial data value. Afterwards, each element of the buffer in a
* particular process contains the result of combining all the corresponding
* elements in all lower-ranked processes' buffers using the specified binary
* operation, except in process 0 each element of the buffer contains the
* initial data value. For example, if the operation is addition and the initial
* data value is 0, each buffer element ends up being the sum of all
* lower-ranked processes' buffer elements.
* <P>
* <TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0>
* <TR>
* <TD ALIGN="left" VALIGN="top">
* Before:
* <FONT SIZE="-1">
* <PRE>
* Process Process Process Process
* 0 1 2 3
* +----+ +----+ +----+ +----+
* | 1 | | 3 | | 5 | | 7 |
* | 2 | | 4 | | 6 | | 8 |
* +----+ +----+ +----+ +----+</PRE>
* </FONT>
* </TD>
* <TD WIDTH=50> </TD>
* <TD ALIGN="left" VALIGN="top">
* After:
* <FONT SIZE="-1">
* <PRE>
* Process Process Process Process
* 0 1 2 3
* +----+ +----+ +----+ +----+
* | 0 | | 1 | | 4 | | 9 |
* | 0 | | 2 | | 6 | | 12 |
* +----+ +----+ +----+ +----+</PRE>
* </FONT>
* </TD>
* </TR>
* </TABLE>
* This version of the scan operation is also known as "exclusive prefix scan"
* -- "exclusive" because the process's own element is excluded from the result.
* <P>
* <B>Barrier</B>
* <P>
* The barrier operation causes all the processes to synchronize with each
* other. Each process calls the communicator's <TT>barrier()</TT> method. The
* calling thread blocks until all processes in the communicator have called the
* <TT>barrier()</TT> method. Then the calling thread unblocks and returns from
* the <TT>barrier()</TT> method call.
*
* @author Alan Kaminsky
* @version 21-May-2008
*/
public class Comm
{
// Hidden data members.
// Predefined communicators.
private static Comm theWorldCommunicator;
private static Comm theFrontendCommunicator;
// This communicator's size, rank, and host.
private int mySize;
private int myRank;
private String myHost;
// The largest power of 2 less than or equal to this communicator's size.
private int mySizePowerOf2;
// Channel group for message passing in this communicator.
private ChannelGroup myChannelGroup;
// Map from rank (array index) to channel group address (array element).
private InetSocketAddress[] myAddressForRank;
// Map from rank (array index) to channel for communicating with the process
// at that rank (array element).
private Channel[] myChannelForRank;
// Broadcast trees for flood-send, flood-receive, broadcast, and reduce
// operations, indexed by root.
private int[][] myBroadcastTree;
// Hidden constructors.
/**
* Construct a new communicator.
*
* @param size
* Communicator's size.
* @param rank
* Current process's rank in the communicator.
* @param host
* Host name.
* @param channelgroup
* Channel group for message passing in this communicator.
* @param address
* Map from rank (array index) to channel group address (array element).
*/
private Comm
(int size,
int rank,
String host,
ChannelGroup channelgroup,
InetSocketAddress[] address)
{
// Record size, rank, channel group.
mySize = size;
myRank = rank;
myHost = host;
myChannelGroup = channelgroup;
// Determine the largest power of 2 less than or equal to this
// communicator's size.
int p2 = 1;
while (p2 <= size) p2 <<= 1;
mySizePowerOf2 = p2 >>> 1;
// Set channel group ID equal to rank.
myChannelGroup.setChannelGroupId (rank);
// Set up connect listener.
myChannelGroup.setConnectListener (new ConnectListener()
{
public void nearEndConnected
(ChannelGroup theChannelGroup,
Channel theChannel)
throws IOException
{
}
public void farEndConnected
(ChannelGroup theChannelGroup,
Channel theChannel)
throws IOException
{
doFarEndConnected (theChannel);
}
});
// Record socket address for each process rank.
myAddressForRank = address;
// Set up channel for each process rank.
myChannelForRank = new Channel [size];
// Populate channel at my own rank with the loopback channel.
myChannelForRank[myRank] = channelgroup.loopbackChannel();
// If there's more than one process, start listening for incoming
// connections.
if (mySize > 1) myChannelGroup.startListening();
}
// Exported operations.
/**
* Initialize the PJ message passing middleware. Certain Java system
* properties specify the middleware's behavior; these properties are
* typically given on the Java command line with the <TT>"-D"</TT> flag.
* For further information, see class {@linkplain PJProperties}.
*
* @param args Command line arguments.
*
* @exception NullPointerException
* (unchecked exception) Thrown if <TT>args</TT> is null.
* @exception IllegalArgumentException
* (unchecked exception) Thrown if the value of one of the Java system
* properties is illegal.
* @exception IOException
* Thrown if an I/O error occurred.
*/
public static void init
(String[] args)
throws IOException
{
// Verify preconditions.
if (args == null)
{
throw new NullPointerException ("Comm.init(): args is null");
}
// Get the job backend object.
JobBackend backend = JobBackend.getJobBackend();
if (backend == null)
{
// We're running on the frontend processor.
// Prepare constructor arguments for the Job Frontend object.
String username = System.getProperty ("user.name");
int Nn = PJProperties.getPjNn();
int Np = PJProperties.getPjNp();
int Nt = PJProperties.getPjNt();
boolean hasFrontendComm = false;
// Examine the call stack to find the main program class name.
StackTraceElement[] stack =
Thread.currentThread().getStackTrace();
StackTraceElement bottom = stack[stack.length-1];
if (! bottom.getMethodName().equals ("main"))
{
throw new IllegalStateException
("Comm.init(): Not called from main program");
}
String mainClassName = bottom.getClassName();
// Set up the Job Frontend object.
JobFrontend frontend = null;
try
{
frontend =
new JobFrontend
(username, Nn, Np, Nt, hasFrontendComm, mainClassName,
args);
// We were able to contact the Job Scheduler.
// Run the job frontend in this process, then exit.
frontend.run();
System.exit (0);
}
catch (JobSchedulerException exc)
{
// We were not able to contact the Job Scheduler.
System.err.println
("No Job Scheduler at " +
PJProperties.getPjHost() + ":" +
PJProperties.getPjPort() +
", running in this (one) process");
// Set up world communicator.
theWorldCommunicator =
new Comm
(/*size */ 1,
/*rank */ 0,
/*host */ "<unknown>",
/*channelgroup*/ new ChannelGroup(),
/*address */
new InetSocketAddress[]
{new InetSocketAddress (0)});
}
}
else
{
// We're running on a backend processor.
// Set up world communicator.
theWorldCommunicator =
new Comm
(/*size */ backend.getK(),
/*rank */ backend.getRank(),
/*host */ backend.getBackendHost(),
/*channelgroup*/ backend.getWorldChannelGroup(),
/*address */ backend.getWorldAddress());
}
}
/**
* Obtain a reference to the world communicator.
*
* @return World communicator.
*
* @exception IllegalStateException
* (unchecked exception) Thrown if <TT>Comm.init()</TT> has not been
* called. Thrown if <TT>world()</TT> is called in the job frontend
* process; the world communicator does not exist in the job frontend
* process.
*/
public static Comm world()
{
if (theWorldCommunicator != null)
{
return theWorldCommunicator;
}
else if (JobBackend.getJobBackend() != null)
{
throw new IllegalStateException
("Comm.world(): Didn't call Comm.init()");
}
else
{
throw new IllegalStateException
("Comm.world(): World communicator doesn't exist in job frontend process");
}
}
/**
* Obtain the number of processes in this communicator.
*
* @return Size.
*/
public int size()
{
return mySize;
}
/**
* Obtain the current process's rank in this communicator.
*
* @return Rank.
*/
public int rank()
{
return myRank;
}
/**
* Obtain the host name of this communicator's backend processor. If this
* communicator is not running on a cluster backend processor, the host name
* is <TT>"<unknown>"</TT>.
*
* @return Host name.
*/
public String host()
{
return myHost;
}
/**
* Create a new communicator. <I>Every</I> process in this communicator must
* call the <TT>createComm()</TT> method. Each process passes true or false
* for the <TT>participate</TT> argument to state whether the process will
* participate in the new communicator. At least one process must
* participate in the new communicator. Messages to set up the new
* communicator are sent to all processes in this communicator, using a
* message tag of 0.
* <P>
* In processes participating in the new communicator, the new communicator
* is returned. The participating processes appear in the same order by rank
* in the new communicator as in this communicator. The process can call the
* new communicator's <TT>rank()</TT> method to determine the process's rank
* in the new communicator.
* <P>
* In processes not participating in the new communicator, null is returned.
*
* @param participate True if this process will participate in the new
* communicator; false otherwise.
*
* @return New communicator if this process will participate in the new
* communicator; null otherwise.
*
* @exception IOException
* Thrown if an I/O error occurred.
*/
public Comm createComm
(boolean participate)
throws IOException
{
return createComm (participate, 0);
}
/**
* Create a new communicator. <I>Every</I> process in this communicator must
* call the <TT>createComm()</TT> method. Each process passes true or false
* for the <TT>participate</TT> argument to state whether the process will
* participate in the new communicator. At least one process must
* participate in the new communicator. Messages to set up the new
* communicator are sent to all processes in this communicator, using the
* given message tag.
* <P>
* In processes participating in the new communicator, the new communicator
* is returned. The participating processes appear in the same order by rank
* in the new communicator as in this communicator. The process can call the
* new communicator's <TT>rank()</TT> method to determine the process's rank
* in the new communicator.
* <P>
* In processes not participating in the new communicator, null is returned.
*
* @param participate True if this process will participate in the new
* communicator; false otherwise.
* @param tag Message tag.
*
* @return New communicator if this process will participate in the new
* communicator; null otherwise.
*
* @exception IOException
* Thrown if an I/O error occurred.
*/
public Comm createComm
(boolean participate,
int tag)
throws IOException
{
// Set up array of socket addresses for all processes.
InetSocketAddress[] address = new InetSocketAddress [mySize];
ObjectBuf<InetSocketAddress>[] addressbuf =
ObjectBuf.sliceBuffers
(address,
new Range (0, mySize-1) .subranges (mySize));
// Create channel group for new communicator, if participating.
ChannelGroup channelgroup = null;
InetSocketAddress myaddress = null;
if (participate)
{
channelgroup =
new ChannelGroup
(new InetSocketAddress
(myChannelGroup.listenAddress().getAddress(), 0));
myaddress = channelgroup.listenAddress();
address[myRank] = myaddress;
}
// All-gather channel group socket addresses into every process.
allGather (tag, addressbuf[myRank], addressbuf);
// Close up gaps in the socket address array if any.
int off = 0;
int newsize = 0;
int newrank = -1;
for (int i = 0; i < mySize; ++ i)
{
if (address[i] == null)
{
++ off;
}
else
{
if (i == myRank) newrank = i-off;
address[i-off] = address[i];
++ newsize;
}
}
// Verify size of new communicator.
if (newsize == 0)
{
throw new IOException
("Comm.createComm(): No processes in communicator");
}
// Return new communicator if participating.
if (participate)
{
return new Comm (newsize, newrank, myHost, channelgroup, address);
}
// Return null if not participating.
else
{
return null;
}
}
/**
* Send a message to the process at the given rank in this communicator. The
* message uses a tag of 0. The message items come from the given buffer. To
* receive the message, the destination process must call the
* <TT>receive()</TT> method. When the <TT>send()</TT> method returns, the
* message has been fully sent, but it may not yet have been fully received.
* <P>
* A process can send a message to itself; in this case a different thread
* must call the <TT>receive()</TT> method on this communicator.
*
* @param toRank Destination process's rank in this communicator.
* @param buffer Buffer of data items to be sent.
*
* @exception IndexOutOfBoundsException
* (unchecked exception) Thrown if <TT>toRank</TT> is not in the range 0
* .. <TT>size()</TT>-1.
* @exception NullPointerException
* (unchecked exception) Thrown if <TT>buffer</TT> is null.
* @exception IOException
* Thrown if an I/O error occurred.
*/
public void send
(int toRank,
Buf buffer)
throws IOException
{
send (toRank, 0, buffer);
}
/**
* Send a message to the process at the given rank in this communicator with
* the given message tag. The message items come from the given buffer. To
* receive the message, the destination process must call the
* <TT>receive()</TT> method. When the <TT>send()</TT> method returns, the
* message has been fully sent, but it may not yet have been fully received.
* <P>
* A process can send a message to itself; in this case a different thread
* must call the <TT>receive()</TT> method on this communicator.
*
* @param toRank Destination process's rank in this communicator.
* @param tag Message tag.
* @param buffer Buffer of data items to be sent.
*
* @exception IndexOutOfBoundsException
* (unchecked exception) Thrown if <TT>toRank</TT> is not in the range 0
* .. <TT>size()</TT>-1.
* @exception NullPointerException
* (unchecked exception) Thrown if <TT>buffer</TT> is null.
* @exception IOException
* Thrown if an I/O error occurred.
*/
public void send
(int toRank,
int tag,
Buf buffer)
throws IOException
{
myChannelGroup.send (getChannelConnect (toRank), tag, buffer);
}
/**
* Send a message to the process at the given rank in this communicator
* (non-blocking). A message tag of 0 is used. The message items come from
* the given buffer. To receive the message, the destination process must
* call the <TT>receive()</TT> method.
* <P>
* The <TT>send()</TT> method initiates the send operation and immediately
* returns a {@linkplain CommRequest} object. The send operation is
* performed by a separate thread. To wait for the send operation to finish,
* call the returned {@linkplain CommRequest} object's
* <TT>waitForFinish()</TT> method. When that method returns, the message
* has been fully sent, but it may not yet have been fully received.
* <P>
* A process can send a message to itself; in this case a different thread
* must call the <TT>receive()</TT> method on this communicator.
*
* @param toRank Destination process's rank in this communicator.
* @param buffer Buffer of data items to be sent.
* @param request CommRequest object to use to wait for the operation to
* finish; in this case <TT>request</TT> is returned. If
* <TT>request</TT> is null, a new CommRequest object is
* created and returned.
*
* @return CommRequest object to use to wait for the operation to finish.
*
* @exception IndexOutOfBoundsException
* (unchecked exception) Thrown if <TT>toRank</TT> is not in the range 0
* .. <TT>size()</TT>-1.
* @exception NullPointerException
* (unchecked exception) Thrown if <TT>buffer</TT> is null.
* @exception IOException
* Thrown if an I/O error occurred.
*/
public CommRequest send
(int toRank,
Buf buffer,
CommRequest request)
throws IOException
{
return send (toRank, 0, buffer, request);
}
/**
* Send a message to the process at the given rank in this communicator with
* the given message tag (non-blocking). The message items come from the
* given buffer. To receive the message, the destination process must call
* the <TT>receive()</TT> method.
* <P>
* The <TT>send()</TT> method initiates the send operation and immediately
* returns a {@linkplain CommRequest} object. The send operation is
* performed by a separate thread. To wait for the send operation to finish,
* call the returned {@linkplain CommRequest} object's
* <TT>waitForFinish()</TT> method. When that method returns, the message
* has been fully sent, but it may not yet have been fully received.
* <P>
* A process can send a message to itself; in this case a different thread
* must call the <TT>receive()</TT> method on this communicator.
*
* @param toRank Destination process's rank in this communicator.
* @param tag Message tag.
* @param buffer Buffer of data items to be sent.
* @param request CommRequest object to use to wait for the operation to
* finish; in this case <TT>request</TT> is returned. If
* <TT>request</TT> is null, a new CommRequest object is
* created and returned.
*
* @return CommRequest object to use to wait for the operation to finish.
*
* @exception IndexOutOfBoundsException
* (unchecked exception) Thrown if <TT>toRank</TT> is not in the range 0
* .. <TT>size()</TT>-1.
* @exception NullPointerException
* (unchecked exception) Thrown if <TT>buffer</TT> is null.
* @exception IOException
* Thrown if an I/O error occurred.
*/
public CommRequest send
(int toRank,
int tag,
Buf buffer,
CommRequest request)
throws IOException
{
// Set up CommRequest object.
CommRequest req = request == null ? new CommRequest() : request;
// Send message (non-blocking).
req.mySendRequest = new IORequest();
req.myRecvRequest = null;
myChannelGroup.sendNoWait
(getChannelConnect (toRank), tag, buffer, req.mySendRequest);
// Return CommRequest object.
return req;
}
/**
* Receive a message from the process at the given rank in this
* communicator. If <TT>rank</TT> is null, a message will be received from
* any process in this communicator. The message must have a tag of 0. The
* received message items are stored in the given buffer. To send the
* message, the source process must call the <TT>send()</TT> method. When
* the <TT>receive()</TT> method returns, the message has been fully
* received.
* <P>
* A {@linkplain CommStatus} object is returned. The status object gives the
* actual rank of the process that sent the message, the actual message tag
* that was received, and the actual number of data items in the message. If
* the actual number of data items in the message is less than the length of
* the buffer, nothing is stored into the extra data items at the end of the
* buffer. If the actual number of data items in the message is greater than
* the length of the buffer, the extra data items at the end of the message
* are discarded.
* <P>
* A process can receive a message from itself; in this case a different
* thread must call the <TT>send()</TT> method on this communicator.
*
* @param fromRank Source process's rank in this communicator, or null to
* receive from any process.
* @param buffer Buffer of data items to be received.
*
* @return Status object giving the outcome of the message reception.
*
* @exception IndexOutOfBoundsException
* (unchecked exception) Thrown if <TT>fromRank</TT> is not null and is
* not in the range 0 .. <TT>size()</TT>-1.
* @exception NullPointerException
* (unchecked exception) Thrown if <TT>buffer</TT> is null.
* @exception IOException
* Thrown if an I/O error occurred.
*/
public CommStatus receive
(Integer fromRank,
Buf buffer)
throws IOException
{
return receive (fromRank, 0, buffer);
}
/**
* Receive a message from the process at the given rank in this communicator
* with the given message tag. If <TT>rank</TT> is null, a message will be
* received from any process in this communicator. If <TT>tag</TT> is null,
* a message will be received with any tag. The received message items are
* stored in the given buffer. To send the message, the source process must
* call the <TT>send()</TT> method. When the <TT>receive()</TT> method
* returns, the message has been fully received.
* <P>
* A {@linkplain CommStatus} object is returned. The status object gives the
* actual rank of the process that sent the message, the actual message tag
* that was received, and the actual number of data items in the message. If
* the actual number of data items in the message is less than the length of
* the buffer, nothing is stored into the extra data items at the end of the
* buffer. If the actual number of data items in the message is greater than
* the length of the buffer, the extra data items at the end of the message
* are discarded.
* <P>
* A process can receive a message from itself; in this case a different
* thread must call the <TT>send()</TT> method on this communicator.
*
* @param fromRank Source process's rank in this communicator, or null to
* receive from any process.
* @param tag Message tag, or null to receive any tag.
* @param buffer Buffer of data items to be received.
*
* @return Status object giving the outcome of the message reception.
*
* @exception IndexOutOfBoundsException
* (unchecked exception) Thrown if <TT>fromRank</TT> is not null and is
* not in the range 0 .. <TT>size()</TT>-1.
* @exception NullPointerException
* (unchecked exception) Thrown if <TT>buffer</TT> is null.
* @exception IOException
* Thrown if an I/O error occurred.
*/
public CommStatus receive
(Integer fromRank,
Integer tag,
Buf buffer)
throws IOException
{
Status status =
myChannelGroup.receive
(fromRank == null ? null : getChannelWait (fromRank),
tag,
buffer);
return new CommStatus
(getFarRank (status.channel),
status.tag,
status.length);
}
/**
* Receive a message from the process at the given rank in this communicator
* (non-blocking). A message tag of 0 is used. If <TT>rank</TT> is null, a
* message will be received from any process in this communicator. If
* <TT>tag</TT> is null, a message will be received with any tag. The
* received message items are stored in the given buffer. To send the
* message, the source process must call the <TT>send()</TT> method.
* <P>
* The <TT>receive()</TT> method initiates the receive operation and
* immediately returns a {@linkplain CommRequest} object. The receive
* operation is performed by a separate thread. To wait for the receive
* operation to finish, call the returned {@linkplain CommRequest} object's
* <TT>waitForFinish()</TT> method. When that method returns, the incoming
* message items have been fully received.
* <P>
* A process can receive a message from itself; in this case a different
* thread must call the <TT>send()</TT> method on this communicator.
*
* @param fromRank Source process's rank in this communicator, or null to
* receive from any process.
* @param buffer Buffer of data items to be received.
* @param request CommRequest object to use to wait for the operation to
* finish; in this case <TT>request</TT> is returned. If
* <TT>request</TT> is null, a new CommRequest object is
* created and returned.
*
* @return CommRequest object to use to wait for the operation to finish.
*
* @exception IndexOutOfBoundsException
* (unchecked exception) Thrown if <TT>fromRank</TT> is not null and is
* not in the range 0 .. <TT>size()</TT>-1.
* @exception NullPointerException
* (unchecked exception) Thrown if <TT>buffer</TT> is null.
* @exception IOException
* Thrown if an I/O error occurred.
*/
public CommRequest receive
(Integer fromRank,
Buf buffer,
CommRequest request)
throws IOException
{
return receive (fromRank, 0, buffer, request);
}
/**
* Receive a message from the process at the given rank in this communicator
* with the given message tag (non-blocking). If <TT>rank</TT> is null, a
* message will be received from any process in this communicator. If
* <TT>tag</TT> is null, a message will be received with any tag. The
* received message items are stored in the given buffer. To send the
* message, the source process must call the <TT>send()</TT> method.
* <P>
* The <TT>receive()</TT> method initiates the receive operation and
* immediately returns a {@linkplain CommRequest} object. The receive
* operation is performed by a separate thread. To wait for the receive
* operation to finish, call the returned {@linkplain CommRequest} object's
* <TT>waitForFinish()</TT> method. When that method returns, the incoming
* message items have been fully received.
* <P>
* A process can receive a message from itself; in this case a different
* thread must call the <TT>send()</TT> method on this communicator.
*
* @param fromRank Source process's rank in this communicator, or null to
* receive from any process.
* @param tag Message tag, or null to receive any tag.
* @param buffer Buffer of data items to be received.
* @param request CommRequest object to use to wait for the operation to
* finish; in this case <TT>request</TT> is returned. If
* <TT>request</TT> is null, a new CommRequest object is
* created and returned.
*
* @return CommRequest object to use to wait for the operation to finish.
*
* @exception IndexOutOfBoundsException
* (unchecked exception) Thrown if <TT>fromRank</TT> is not null and is
* not in the range 0 .. <TT>size()</TT>-1.
* @exception NullPointerException
* (unchecked exception) Thrown if <TT>buffer</TT> is null.
* @exception IOException
* Thrown if an I/O error occurred.
*/
public CommRequest receive
(Integer fromRank,
Integer tag,
Buf buffer,
CommRequest request)
throws IOException
{
// Set up CommRequest object.
CommRequest req = request == null ? new CommRequest() : request;
// Receive message (non-blocking).
req.mySendRequest = null;
req.myRecvRequest = new IORequest();
myChannelGroup.receiveNoWait
(fromRank == null ? null : getChannelWait (fromRank),
tag,
buffer,
req.myRecvRequest);
// Return CommRequest object.
return req;
}
/**
* Send a message to the process at the given rank in this communicator, and
* receive a message from the process at the given rank in this
* communicator. A message tag of 0 is used. The outgoing message items come
* from the buffer <TT>sendbuf</TT>. The incoming message items go into the
* buffer <TT>recvbuf</TT>. The outgoing message items must come from a
* different place than where the incoming message items will be stored. The
* destination process (process <TT>toRank</TT>) must call a method to
* receive this process's outgoing message items. The source process
* (process <TT>fromRank</TT>) must call a method to send this process's
* incoming message items. When the <TT>sendReceive()</TT> method returns,
* the outgoing message items have been fully sent, but they may not yet
* have been fully received; and the incoming message items have been fully
* received.
* <P>
* A {@linkplain CommStatus} object is returned giving the results of the
* receive half of the operation. The status object gives the rank of the
* process that sent the incoming message, the message tag that was
* received, and the actual number of data items in the message. If the
* actual number of data items in the message is less than the length of the
* receive buffer, nothing is stored into the extra data items at the end of
* the receive buffer. If the actual number of data items in the message is
* greater than the length of the receive buffer, the extra data items at
* the end of the message are discarded.
* <P>
* A process can send-receive messages with itself; in this case a different
* thread must call the <TT>sendReceive()</TT> method on this communicator.
*
* @param toRank Destination process's rank in this communicator.
* @param sendBuf Buffer of data items to be sent.
* @param fromRank Source process's rank in this communicator.
* @param recvBuf Buffer of data items to be received.
*
* @return Status object giving the outcome of the message reception.
*
* @exception IndexOutOfBoundsException
* (unchecked exception) Thrown if <TT>toRank</TT> or <TT>fromRank</TT>
* is not in the range 0 .. <TT>size()</TT>-1.
* @exception NullPointerException
* (unchecked exception) Thrown if <TT>sendBuf</TT> or <TT>recvBuf</TT>
* is null.
* @exception IOException
* Thrown if an I/O error occurred.
*/
public CommStatus sendReceive
(int toRank,
Buf sendBuf,
int fromRank,
Buf recvBuf)
throws IOException
{
return sendReceive (toRank, 0, sendBuf, fromRank, 0, recvBuf);
}
/**
* Send a message to the process at the given rank in this communicator with
* the given message tag, and receive a message from the process at the
* given rank in this communicator with the given message tag. The outgoing
* message items come from the buffer <TT>sendbuf</TT>. The incoming message
* items go into the buffer <TT>recvbuf</TT>. The outgoing message items
* must come from a different place than where the incoming message items
* will be stored. The destination process (process <TT>toRank</TT>) must
* call a method to receive this process's outgoing message items. The
* source process (process <TT>fromRank</TT>) must call a method to send
* this process's incoming message items. When the <TT>sendReceive()</TT>
* method returns, the outgoing message items have been fully sent, but they
* may not yet have been fully received; and the incoming message items have
* been fully received.
* <P>
* A {@linkplain CommStatus} object is returned giving the results of the
* receive half of the operation. The status object gives the rank of the
* process that sent the incoming message, the message tag that was
* received, and the actual number of data items in the message. If the
* actual number of data items in the message is less than the length of the
* receive buffer, nothing is stored into the extra data items at the end of
* the receive buffer. If the actual number of data items in the message is
* greater than the length of the receive buffer, the extra data items at
* the end of the message are discarded.
* <P>
* A process can send-receive messages with itself; in this case a different
* thread must call the <TT>sendReceive()</TT> method on this communicator.
*
* @param toRank Destination process's rank in this communicator.
* @param sendTag Message tag for outgoing message.
* @param sendBuf Buffer of data items to be sent.
* @param fromRank Source process's rank in this communicator.
* @param recvTag Message tag for incoming message.
* @param recvBuf Buffer of data items to be received.
*
* @return Status object giving the outcome of the message reception.
*
* @exception IndexOutOfBoundsException
* (unchecked exception) Thrown if <TT>toRank</TT> or <TT>fromRank</TT>
* is not in the range 0 .. <TT>size()</TT>-1.
* @exception NullPointerException
* (unchecked exception) Thrown if <TT>sendBuf</TT> or <TT>recvBuf</TT>
* is null.
* @exception IOException
* Thrown if an I/O error occurred.
*/
public CommStatus sendReceive
(int toRank,
int sendTag,
Buf sendBuf,
int fromRank,
int recvTag,
Buf recvBuf)
throws IOException
{
// Get the outgoing and incoming channels.
Channel sendChannel =
toRank != fromRank || myRank < toRank ?
getChannelConnect (toRank) :
getChannelWait (toRank);
Channel recvChannel = getChannelWait (fromRank);
// Send the outgoing message (non-blocking).
IORequest sendRequest = new IORequest();
myChannelGroup.sendNoWait
(sendChannel,
sendTag,
sendBuf,
sendRequest);
// Receive the outgoing message (non-blocking).
IORequest recvRequest = new IORequest();
myChannelGroup.receiveNoWait
(recvChannel,
recvTag,
recvBuf,
recvRequest);
// Wait for both messages to finish.
sendRequest.waitForFinish();
Status status = recvRequest.waitForFinish();
return new CommStatus
(getFarRank (status.channel),
status.tag,
status.length);
}
/**
* Send a message to the process at the given rank in this communicator, and
* receive a message from the process at the given rank in this communicator
* (non-blocking). A message tag of 0 is used. The outgoing message items
* come from the buffer <TT>sendbuf</TT>. The incoming message items go into
* the buffer <TT>recvbuf</TT>. The outgoing message items must come from a
* different place than where the incoming message items will be stored. The
* destination process (process <TT>toRank</TT>) must call a method to
* receive this process's outgoing message items. The source process
* (process <TT>fromRank</TT>) must call a method to send this process's
* incoming message items.
* <P>
* The <TT>sendReceive()</TT> method initiates the send and receive
* operations and immediately returns a {@linkplain CommRequest} object. The
* send and receive operations are performed by a separate thread. To wait
* for the send and receive operations to finish, call the returned
* {@linkplain CommRequest} object's <TT>waitForFinish()</TT> method. When
* that method returns, the outgoing message items have been fully sent, but
* they may not yet have been fully received; and the incoming message items
* have been fully received.
* <P>
* A process can send-receive messages with itself; in this case a different
* thread must call the <TT>sendReceive()</TT> method on this communicator.
*
* @param toRank Destination process's rank in this communicator.
* @param sendBuf Buffer of data items to be sent.
* @param fromRank Source process's rank in this communicator.
* @param recvBuf Buffer of data items to be received.
* @param request CommRequest object to use to wait for the operation to
* finish; in this case <TT>request</TT> is returned. If
* <TT>request</TT> is null, a new CommRequest object is
* created and returned.
*
* @return CommRequest object to use to wait for the operation to finish.
*
* @exception IndexOutOfBoundsException
* (unchecked exception) Thrown if <TT>toRank</TT> or <TT>fromRank</TT>
* is not in the range 0 .. <TT>size()</TT>-1.
* @exception NullPointerException
* (unchecked exception) Thrown if <TT>sendBuf</TT> or <TT>recvBuf</TT>
* is null.
* @exception IOException
* Thrown if an I/O error occurred.
*/
public CommRequest sendReceive
(int toRank,
Buf sendBuf,
int fromRank,
Buf recvBuf,
CommRequest request)
throws IOException
{
return sendReceive (toRank, 0, sendBuf, fromRank, 0, recvBuf, request);
}
/**
* Send a message to the process at the given rank in this communicator with
* the given message tag, and receive a message from the process at the
* given rank in this communicator with the given message tag
* (non-blocking). The outgoing message items come from the buffer
* <TT>sendbuf</TT>. The incoming message items go into the buffer
* <TT>recvbuf</TT>. The outgoing message items must come from a different
* place than where the incoming message items will be stored. The
* destination process (process <TT>toRank</TT>) must call a method to
* receive this process's outgoing message items. The source process
* (process <TT>fromRank</TT>) must call a method to send this process's
* incoming message items.
* <P>
* The <TT>sendReceive()</TT> method initiates the send and receive
* operations and immediately returns a {@linkplain CommRequest} object. The
* send and receive operations are performed by a separate thread. To wait
* for the send and receive operations to finish, call the returned
* {@linkplain CommRequest} object's <TT>waitForFinish()</TT> method. When
* that method returns, the outgoing message items have been fully sent, but
* they may not yet have been fully received; and the incoming message items
* have been fully received.
* <P>
* A process can send-receive messages with itself; in this case a different
* thread must call the <TT>sendReceive()</TT> method on this communicator.
*
* @param toRank Destination process's rank in this communicator.
* @param sendTag Message tag for outgoing message.
* @param sendBuf Buffer of data items to be sent.
* @param fromRank Source process's rank in this communicator.
* @param recvTag Message tag for incoming message.
* @param recvBuf Buffer of data items to be received.
* @param request CommRequest object to use to wait for the operation to
* finish; in this case <TT>request</TT> is returned. If
* <TT>request</TT> is null, a new CommRequest object is
* created and returned.
*
* @return CommRequest object to use to wait for the operation to finish.
*
* @exception IndexOutOfBoundsException
* (unchecked exception) Thrown if <TT>toRank</TT> or <TT>fromRank</TT>
* is not in the range 0 .. <TT>size()</TT>-1.
* @exception NullPointerException
* (unchecked exception) Thrown if <TT>sendBuf</TT> or <TT>recvBuf</TT>
* is null.
* @exception IOException
* Thrown if an I/O error occurred.
*/
public CommRequest sendReceive
(int toRank,
int sendTag,
Buf sendBuf,
int fromRank,
int recvTag,
Buf recvBuf,
CommRequest request)
throws IOException
{
// Set up CommRequest object.
CommRequest req = request == null ? new CommRequest() : request;
// Send the outgoing message (non-blocking).
req.mySendRequest = new IORequest();
myChannelGroup.sendNoWait
(toRank != fromRank || myRank < toRank ?
getChannelConnect (toRank) :
getChannelWait (toRank),
sendTag,
sendBuf,
req.mySendRequest);
// Receive the outgoing message (non-blocking).
req.myRecvRequest = new IORequest();
myChannelGroup.receiveNoWait
(getChannelWait (fromRank),
recvTag,
recvBuf,
req.myRecvRequest);
// Return CommRequest object.
return req;
}
/**
* Flood-send a message to all processes in this communicator. The message
* uses a tag of 0. The message items come from the given buffer. To receive
* the message, every process (including the sending process) must call the
* <TT>floodReceive()</TT> method. When the <TT>floodSend()</TT> method
* returns, the message has been fully sent, but it may not yet have been
* fully received in all processes.
* <P>
* <I>Note:</I> The length of the incoming buffer in the
* <TT>floodReceive()</TT> method call must be the same as the length of the
* outgoing buffer in the <TT>floodSend()</TT> method call.
*
* @param buffer Buffer of data items to be sent.
*
* @exception NullPointerException
* (unchecked exception) Thrown if <TT>buffer</TT> is null.
* @exception IOException
* Thrown if an I/O error occurred.
*/
public void floodSend
(Buf buffer)
throws IOException
{
floodSend (0, buffer, null) .waitForFinish();
}
/**
* Flood-send a message to all processes in this communicator with the given
* message tag. The message items come from the given buffer. To receive the
* message, every process (including the sending process) must call the
* <TT>floodReceive()</TT> method. When the <TT>floodSend()</TT> method
* returns, the message has been fully sent, but it may not yet have been
* fully received in all processes.
* <P>
* <I>Note:</I> The length of the incoming buffer in the
* <TT>floodReceive()</TT> method call must be the same as the length of the
* outgoing buffer in the <TT>floodSend()</TT> method call.
*
* @param tag Message tag.
* @param buffer Buffer of data items to be sent.
*
* @exception NullPointerException
* (unchecked exception) Thrown if <TT>buffer</TT> is null.
* @exception IOException
* Thrown if an I/O error occurred.
*/
public void floodSend
(int tag,
Buf buffer)
throws IOException
{
floodSend (tag, buffer, null) .waitForFinish();
}
/**
* Flood-send a message to all processes in this communicator
* (non-blocking). A message tag of 0 is used. The message items come from
* the given buffer. To receive the message, every process (including the
* sending process) must call the <TT>floodReceive()</TT> method.
* <P>
* The <TT>floodSend()</TT> method initiates the flood-send operation and
* immediately returns a {@linkplain CommRequest} object. The flood-send
* operation is performed by a separate thread. To wait for the flood-send
* operation to finish, call the returned {@linkplain CommRequest} object's
* <TT>waitForFinish()</TT> method. When that method returns, the message
* has been fully sent, but it may not yet have been fully received in all
* processes.
* <P>
* <I>Note:</I> The length of the incoming buffer in the
* <TT>floodReceive()</TT> method call must be the same as the length of the
* outgoing buffer in the <TT>floodSend()</TT> method call.
*
* @param buffer Buffer of data items to be sent.
* @param request CommRequest object to use to wait for the operation to
* finish; in this case <TT>request</TT> is returned. If
* <TT>request</TT> is null, a new CommRequest object is
* created and returned.
*
* @return CommRequest object to use to wait for the operation to finish.
*
* @exception NullPointerException
* (unchecked exception) Thrown if <TT>buffer</TT> is null.
* @exception IOException
* Thrown if an I/O error occurred.
*/
public CommRequest floodSend
(Buf buffer,
CommRequest request)
throws IOException
{
return floodSend (0, buffer, request);
}
/**
* Flood-send a message to all processes in this communicator with the given
* message tag (non-blocking). The message items come from the given buffer.
* To receive the message, every process (including the sending process)
* must call the <TT>floodReceive()</TT> method.
* <P>
* The <TT>floodSend()</TT> method initiates the flood-send operation and
* immediately returns a {@linkplain CommRequest} object. The flood-send
* operation is performed by a separate thread. To wait for the flood-send
* operation to finish, call the returned {@linkplain CommRequest} object's
* <TT>waitForFinish()</TT> method. When that method returns, the message
* has been fully sent, but it may not yet have been fully received in all
* processes.
* <P>
* <I>Note:</I> The length of the incoming buffer in the
* <TT>floodReceive()</TT> method call must be the same as the length of the
* outgoing buffer in the <TT>floodSend()</TT> method call.
*
* @param tag Message tag.
* @param buffer Buffer of data items to be sent.
* @param request CommRequest object to use to wait for the operation to
* finish; in this case <TT>request</TT> is returned. If
* <TT>request</TT> is null, a new CommRequest object is
* created and returned.
*
* @return CommRequest object to use to wait for the operation to finish.
*
* @exception NullPointerException
* (unchecked exception) Thrown if <TT>buffer</TT> is null.
* @exception IOException
* Thrown if an I/O error occurred.
*/
public CommRequest floodSend
(int tag,
Buf buffer,
CommRequest request)
throws IOException
{
// Get broadcast tree for root=0. If process 0 is the parent process,
// wait for an incoming connection from process 0, otherwise set up an
// outgoing connection to process 0.
int[] tree = getBroadcastTree (0);
Channel sendChannel =
tree[0] == 0 ? getChannelWait (0) : getChannelConnect (0);
// Set up CommRequest object.
CommRequest req = request == null ? new CommRequest() : request;
req.myRecvRequest = null;
// Send data to process 0. Process 0's flood-receive I/O request object
// will forward the data to all the processes.
req.mySendRequest = new IORequest();
myChannelGroup.sendNoWait (sendChannel, tag, buffer, req.mySendRequest);
// Return CommRequest object.
return req;
}
/**
* Flood-receive a message from any process in this communicator. The
* message must have a tag of 0. The received message items are stored in
* the given buffer. To send the message, the source process must call the
* <TT>floodSend()</TT> method. When the <TT>floodReceive()</TT> method
* returns, the message has been fully received.
* <P>
* A {@linkplain CommStatus} object is returned. The status object gives the
* actual rank of the process that sent the message, the actual message tag
* that was received, and the actual number of data items in the message.
* <P>
* <I>Note:</I> The length of the incoming buffer in the
* <TT>floodReceive()</TT> method call must be the same as the length of the
* outgoing buffer in the <TT>floodSend()</TT> method call.
*
* @param buffer Buffer of data items to be received.
*
* @return Status object giving the outcome of the message reception.
*
* @exception NullPointerException
* (unchecked exception) Thrown if <TT>buffer</TT> is null.
* @exception IOException
* Thrown if an I/O error occurred.
*/
public CommStatus floodReceive
(Buf buffer)
throws IOException
{
return floodReceive (0, buffer, null) .waitForFinish();
}
/**
* Flood-receive a message from any process in this communicator with the
* given message tag. If <TT>tag</TT> is null, a message will be received
* with any tag. The received message items are stored in the given buffer.
* To send the message, the source process must call the
* <TT>floodSend()</TT> method. When the <TT>floodReceive()</TT> method
* returns, the message has been fully received.
* <P>
* A {@linkplain CommStatus} object is returned. The status object gives the
* actual rank of the process that sent the message, the actual message tag
* that was received, and the actual number of data items in the message.
* <P>
* <I>Note:</I> The length of the incoming buffer in the
* <TT>floodReceive()</TT> method call must be the same as the length of the
* outgoing buffer in the <TT>floodSend()</TT> method call.
*
* @param tag Message tag, or null to receive any tag.
* @param buffer Buffer of data items to be received.
*
* @return Status object giving the outcome of the message reception.
*
* @exception NullPointerException
* (unchecked exception) Thrown if <TT>buffer</TT> is null.
* @exception IOException
* Thrown if an I/O error occurred.
*/
public CommStatus floodReceive
(Integer tag,
Buf buffer)
throws IOException
{
return floodReceive (tag, buffer, null) .waitForFinish();
}
/**
* Flood-receive a message from any process in this communicator
* (non-blocking). A message tag of 0 is used. If <TT>tag</TT> is null, a
* message will be received with any tag. The received message items are
* stored in the given buffer. To send the message, the source process must
* call the <TT>floodSend()</TT> method.
* <P>
* The <TT>floodReceive()</TT> method initiates the flood-receive operation
* and immediately returns a {@linkplain CommRequest} object. The
* flood-receive operation is performed by a separate thread. To wait for
* the flood-receive operation to finish, call the returned {@linkplain
* CommRequest} object's <TT>waitForFinish()</TT> method. When that method
* returns, the incoming message items have been fully received.
* <P>
* <I>Note:</I> The length of the incoming buffer in the
* <TT>floodReceive()</TT> method call must be the same as the length of the
* outgoing buffer in the <TT>floodSend()</TT> method call.
*
* @param buffer Buffer of data items to be received.
* @param request CommRequest object to use to wait for the operation to
* finish; in this case <TT>request</TT> is returned. If
* <TT>request</TT> is null, a new CommRequest object is
* created and returned.
*
* @return CommRequest object to use to wait for the operation to finish.
*
* @exception NullPointerException
* (unchecked exception) Thrown if <TT>buffer</TT> is null.
* @exception IOException
* Thrown if an I/O error occurred.
*/
public CommRequest floodReceive
(Buf buffer,
CommRequest request)
throws IOException
{
return floodReceive (0, buffer, request);
}
/**
* Flood-receive a message from any process in this communicator with the
* given message tag (non-blocking). If <TT>tag</TT> is null, a message will
* be received with any tag. The received message items are stored in the
* given buffer. To send the message, the source process must call the
* <TT>floodSend()</TT> method.
* <P>
* The <TT>floodReceive()</TT> method initiates the flood-receive operation
* and immediately returns a {@linkplain CommRequest} object. The
* flood-receive operation is performed by a separate thread. To wait for
* the flood-receive operation to finish, call the returned {@linkplain
* CommRequest} object's <TT>waitForFinish()</TT> method. When that method
* returns, the incoming message items have been fully received.
* <P>
* <I>Note:</I> The length of the incoming buffer in the
* <TT>floodReceive()</TT> method call must be the same as the length of the
* outgoing buffer in the <TT>floodSend()</TT> method call.
*
* @param tag Message tag, or null to receive any tag.
* @param buffer Buffer of data items to be received.
* @param request CommRequest object to use to wait for the operation to
* finish; in this case <TT>request</TT> is returned. If
* <TT>request</TT> is null, a new CommRequest object is
* created and returned.
*
* @return CommRequest object to use to wait for the operation to finish.
*
* @exception NullPointerException
* (unchecked exception) Thrown if <TT>buffer</TT> is null.
* @exception IOException
* Thrown if an I/O error occurred.
*/
public CommRequest floodReceive
(Integer tag,
Buf buffer,
CommRequest request)
throws IOException
{
// Get broadcast tree for root=0 and set up outgoing connections to the
// child processes if any.
int[] tree = getBroadcastTree (0);
int n = tree.length;
for (int i = 1; i < n; ++ i)
{
getChannelConnect (tree[i]);
}
// Set up CommRequest object.
final CommRequest req = request == null ? new CommRequest() : request;
req.mySendRequest = null;
// In process 0, receive a message from any process. In other processes,
// receive a message from the parent process in the broadcast tree; the
// sending process initiates the connection.
Channel recvChannel = myRank == 0 ? null : getChannelWait (tree[0]);
// Receive a message (non-blocking) with a special I/O request object
// that forwards the message down the broadcast tree.
req.myRecvRequest = new FloodReceiveIORequest (tree);
myChannelGroup.receiveNoWait
(/*theChannel */ recvChannel,
/*theTag */ tag,
/*theDst */ buffer,
/*theIORequest*/ req.myRecvRequest);
// Return CommRequest object.
return req;
}
/**
* Class FloodReceiveIORequest overrides the methods of class IORequest with
* additional processing to forward the message when a message is received.
*/
private class FloodReceiveIORequest
extends IORequest
{
// Broadcast tree.
private int[] tree;
// List of zero or more additional I/O requests to forward copies of the
// received message.
private LinkedList<IORequest> myForwardedIORequests =
new LinkedList<IORequest>();
/**
* Construct a new I/O request object.
*
* @param tree Broadcast tree.
*/
public FloodReceiveIORequest
(int[] tree)
{
super();
this.tree = tree;
}
/**
* Determine if this I/O request has finished.
*
* @return False if this I/O request has not finished, true if this I/O
* request has finished successfully.
*
* @exception IOException
* Thrown if this I/O request has finished and an I/O error
* occurred.
*/
public synchronized boolean isFinished()
throws IOException
{
if (! super.isFinished()) return false;
for (IORequest req : myForwardedIORequests)
{
if (! req.isFinished()) return false;
}
return true;
}
/**
* Wait until the send or receive operation corresponding to this I/O
* request has finished. For a receive operation, a {@linkplain Status}
* object containing the results of the receive operation is returned;
* for a send operation, null is returned.
*
* @return Receive status for a receive operation, or null for a send
* operation.
*
* @exception IOException
* Thrown if an I/O error occurred.
*/
public synchronized Status waitForFinish()
throws IOException
{
Status status = super.waitForFinish();
for (IORequest req : myForwardedIORequests)
{
req.waitForFinish();
}
return status;
}
/**
* Report that this I/O request succeeded.
*/
protected synchronized void reportSuccess()
{
try
{
super.reportSuccess();
// Get message tag.
int msgtag = myStatus.tag;
// Flood the message to every child process in the broadcast
// tree.
int n = tree.length;
for (int i = 1; i < n; ++ i)
{
IORequest req = new IORequest();
myForwardedIORequests.add (req);
myChannelGroup.sendNoWait
(/*theChannel */ getChannelConnect (tree[i]),
/*theTag */ msgtag,
/*theSrc */ myBuf,
/*theIORequest*/ req);
}
}
catch (IOException exc)
{
reportFailure (exc);
}
}
}
/**
* Broadcast a message to all processes in this communicator. The broadcast
* uses a message tag of 0. All processes must call <TT>broadcast()</TT>
* with the same value for <TT>root</TT> and with a buffer of the same
* length and the same item data type.
* <P>
* The root process (the process whose rank in this communicator is
* <TT>root</TT>) sends the message items. The message items come from the
* given buffer. When the <TT>broadcast()</TT> method returns, the message
* has been fully sent, but it may not yet have been fully received by all
* processes.
* <P>
* Each non-root process receives the message items. The message items are
* stored in the given buffer. When the <TT>broadcast()</TT> method returns,
* the message has been fully received.
*
* @param root Root process's rank in this communicator.
* @param buffer Buffer of data items to be sent (root process) or
* received (non-root processes).
*
* @exception IndexOutOfBoundsException
* (unchecked exception) Thrown if <TT>root</TT> is not in the range 0
* .. <TT>size()</TT>-1.
* @exception NullPointerException
* (unchecked exception) Thrown if <TT>buffer</TT> is null.
* @exception IOException
* Thrown if an I/O error occurred.
*/
public void broadcast
(int root,
Buf buffer)
throws IOException
{
broadcast (root, 0, buffer);
}
/**
* Broadcast a message to all processes in this communicator using the given
* message tag. All processes must call <TT>broadcast()</TT> with the same
* values for <TT>root</TT> and <TT>tag</TT> and with a buffer of the same
* length and the same item data type.
* <P>
* The root process (the process whose rank in this communicator is
* <TT>root</TT>) sends the message items. The message items come from the
* given buffer. When the <TT>broadcast()</TT> method returns, the message
* has been fully sent, but it may not yet have been fully received by all
* processes.
* <P>
* Each non-root process receives the message items. The message items are
* stored in the given buffer. When the <TT>broadcast()</TT> method returns,
* the message has been fully received.
*
* @param root Root process's rank in this communicator.
* @param tag Message tag.
* @param buffer Buffer of data items to be sent (root process) or
* received (non-root processes).
*
* @exception IndexOutOfBoundsException
* (unchecked exception) Thrown if <TT>root</TT> is not in the range 0
* .. <TT>size()</TT>-1.
* @exception NullPointerException
* (unchecked exception) Thrown if <TT>buffer</TT> is null.
* @exception IOException
* Thrown if an I/O error occurred.
*/
public void broadcast
(int root,
int tag,
Buf buffer)
throws IOException
{
// Verify preconditions.
if (0 > root || root >= mySize)
{
throw new IndexOutOfBoundsException
("Comm.broadcast(): root = " + root + " out of bounds");
}
// Early return if only one process.
if (mySize == 1) return;
// A broadcast is done as a series of point-to-point messages. The
// messages are organized into rounds. The number of rounds is
// ceil(log_2(mySize)). In each round, processes send messages to other
// processes in parallel. Here is the message pattern for a communicator
// with 8 processes doing a broadcast from root process 0:
//
// Process
// 0 1 2 3 4 5 6 7
// | | | | | | | |
// |---->| | | | | | | Round 1
// | | | | | | | |
// |---------->| | | | | | Round 2
// | |---------->| | | | |
// | | | | | | | |
// |---------------------->| | | | Round 3
// | |---------------------->| | |
// | | |---------------------->| |
// | | | |---------------------->|
// | | | | | | | |
//
// If a process other than process 0 is the root, the message pattern is
// the same, except the process ranks are circularly rotated.
// Get array of process ranks in the broadcast tree.
int[] broadcasttree = getBroadcastTree (root);
int n = broadcasttree.length;
// Receive data from parent if any (blocking).
int parent = broadcasttree[0];
if (parent != -1)
{
myChannelGroup.receive (getChannelWait (parent), tag, buffer);
}
// Send data to children if any (non-blocking).
IORequest[] iorequest = new IORequest [n];
for (int i = 1; i < n; ++ i)
{
int child = broadcasttree[i];
iorequest[i] = new IORequest();
myChannelGroup.sendNoWait
(getChannelConnect (child), tag, buffer, iorequest[i]);
}
// Wait for sends to finish if any.
for (int i = 1; i < n; ++ i)
{
iorequest[i].waitForFinish();
}
}
/**
* Scatter messages to all processes in this communicator. The scatter
* uses a message tag of 0. All processes must call <TT>scatter()</TT>
* with the same value for <TT>root</TT>.
* <P>
* The root process (the process whose rank in this communicator is
* <TT>root</TT>) sends the message items. The message items sent to process
* <I>i</I> come from the source buffer at index <I>i</I> in the given array
* of source buffers. When the <TT>scatter()</TT> method returns, the
* messages have been fully sent, but they may not yet have been fully
* received by all processes.
* <P>
* Each process, including the root process, receives the message items. The
* message items are stored in the given destination buffer. This must have
* the same length and the same item data type as the corresponding source
* buffer. When the <TT>scatter()</TT> method returns, the message has been
* fully received.
* <P>
* In the non-root processes, the source buffer array is ignored and may be
* null.
*
* @param root Root process's rank in this communicator.
* @param srcarray Array of source buffers to be sent by the root process.
* Ignored in the non-root processes.
* @param dst Destination buffer to be received.
*
* @exception IndexOutOfBoundsException
* (unchecked exception) Thrown if <TT>root</TT> is not in the range 0
* .. <TT>size()</TT>-1. Thrown if <TT>srcarray</TT>'s length does not
* equal the size of this communicator.
* @exception NullPointerException
* (unchecked exception) Thrown if <TT>srcarray</TT> or any element
* thereof is null. Thrown if <TT>dst</TT> is null.
* @exception IOException
* Thrown if an I/O error occurred.
*/
public void scatter
(int root,
Buf[] srcarray,
Buf dst)
throws IOException
{
scatter (root, 0, srcarray, dst);
}
/**
* Scatter messages to all processes in this communicator using the given
* message tag. All processes must call <TT>scatter()</TT> with the same
* values for <TT>root</TT> and <TT>tag</TT>.
* <P>
* The root process (the process whose rank in this communicator is
* <TT>root</TT>) sends the message items. The message items sent to process
* <I>i</I> come from the source buffer at index <I>i</I> in the given array
* of source buffers. When the <TT>scatter()</TT> method returns, the
* messages have been fully sent, but they may not yet have been fully
* received by all processes.
* <P>
* Each process, including the root process, receives the message items. The
* message items are stored in the given destination buffer. This must have
* the same length and the same item data type as the corresponding source
* buffer. When the <TT>scatter()</TT> method returns, the message has been
* fully received.
* <P>
* In the non-root processes, the source buffer array is ignored and may be
* null.
*
* @param root Root process's rank in this communicator.
* @param tag Message tag.
* @param srcarray Array of source buffers to be sent by the root process.
* Ignored in the non-root processes.
* @param dst Destination buffer to be received.
*
* @exception IndexOutOfBoundsException
* (unchecked exception) Thrown if <TT>root</TT> is not in the range 0
* .. <TT>size()</TT>-1. Thrown if <TT>srcarray</TT>'s length does not
* equal the size of this communicator.
* @exception NullPointerException
* (unchecked exception) Thrown if <TT>srcarray</TT> or any element
* thereof is null. Thrown if <TT>dst</TT> is null.
* @exception IOException
* Thrown if an I/O error occurred.
*/
public void scatter
(int root,
int tag,
Buf[] srcarray,
Buf dst)
throws IOException
{
// Verify preconditions.
if (0 > root || root >= mySize)
{
throw new IndexOutOfBoundsException
("Comm.scatter(): root = " + root + " out of bounds");
}
// A scatter is done as a series of point-to-point messages. The root
// process sends a separate message to every other process. Here is the
// message pattern for a communicator with 8 processes scattering from
// root process 0:
//
// Process
// 0 1 2 3 4 5 6 7
// | | | | | | | |
// |---->| | | | | | |
// | | | | | | | |
// |---------->| | | | | |
// | | | | | | | |
// |---------------->| | | | |
// | | | | | | | |
// |---------------------->| | | |
// | | | | | | | |
// |---------------------------->| | |
// | | | | | | | |
// |---------------------------------->| |
// | | | | | | | |
// |---------------------------------------->|
// | | | | | | | |
// Root process sends all messages.
if (myRank == root)
{
// Array of IORequest objects for non-blocking sends.
IORequest[] iorequest = new IORequest [mySize];
// Initiate sends to lower-ranked processes.
for (int rank = 0; rank < myRank; ++ rank)
{
iorequest[rank] = new IORequest();
myChannelGroup.sendNoWait
(getChannelConnect (rank),
tag,
srcarray[rank],
iorequest[rank]);
}
// Initiate sends to higher-ranked processes.
for (int rank = myRank+1; rank < mySize; ++ rank)
{
iorequest[rank] = new IORequest();
myChannelGroup.sendNoWait
(getChannelConnect (rank),
tag,
srcarray[rank],
iorequest[rank]);
}
// Copy to itself.
dst.copy (srcarray[myRank]);
// Wait for completion of sends to lower-ranked processes.
for (int rank = 0; rank < myRank; ++ rank)
{
iorequest[rank].waitForFinish();
}
// Wait for completion of sends to higher-ranked processes.
for (int rank = myRank+1; rank < mySize; ++ rank)
{
iorequest[rank].waitForFinish();
}
}
// Non-root process receives one message.
else
{
myChannelGroup.receive (getChannelWait (root), tag, dst);
}
}
/**
* Gather messages from all processes in this communicator. The gather uses
* a message tag of 0. All processes must call <TT>gather()</TT> with the
* same value for <TT>root</TT>.
* <P>
* The root process (the process whose rank in this communicator is
* <TT>root</TT>) receives the message items. The message items received
* from process <I>i</I> are stored in the destination buffer at index
* <I>i</I> in the given array of destination buffers. When the
* <TT>gather()</TT> method returns, all the messages have been fully
* received.
* <P>
* Each process, including the root process, sends the message items. The
* message items come from the given source buffer. This must have the same
* length and the same item data type as the corresponding destination
* buffer. When the <TT>gather()</TT> method returns, the message has been
* fully sent, but it may not yet have been fully received by the root
* process.
* <P>
* In the non-root processes, the destination buffer array is ignored and
* may be null.
*
* @param root Root process's rank in this communicator.
* @param src Source buffer to be sent.
* @param dstarray Array of destination buffers to be received by the root
* process. Ignored in the non-root processes.
*
* @exception IndexOutOfBoundsException
* (unchecked exception) Thrown if <TT>root</TT> is not in the range 0
* .. <TT>size()</TT>-1. Thrown if <TT>dstarray</TT>'s length does not
* equal the size of this communicator.
* @exception NullPointerException
* (unchecked exception) Thrown if <TT>dstarray</TT> or any element
* thereof is null. Thrown if <TT>src</TT> is null.
* @exception IOException
* Thrown if an I/O error occurred.
*/
public void gather
(int root,
Buf src,
Buf[] dstarray)
throws IOException
{
gather (root, 0, src, dstarray);
}
/**
* Gather messages from all processes in this communicator using the given
* message tag. All processes must call <TT>gather()</TT> with the same
* values for <TT>root</TT> and <TT>tag</TT>.
* <P>
* The root process (the process whose rank in this communicator is
* <TT>root</TT>) receives the message items. The message items received
* from process <I>i</I> are stored in the destination buffer at index
* <I>i</I> in the given array of destination buffers. When the
* <TT>gather()</TT> method returns, all the messages have been fully
* received.
* <P>
* Each process, including the root process, sends the message items. The
* message items come from the given source buffer. This must have the same
* length and the same item data type as the corresponding destination
* buffer. When the <TT>gather()</TT> method returns, the message has been
* fully sent, but it may not yet have been fully received by the root
* process.
* <P>
* In the non-root processes, the destination buffer array is ignored and
* may be null.
*
* @param root Root process's rank in this communicator.
* @param tag Message tag.
* @param src Source buffer to be sent.
* @param dstarray Array of destination buffers to be received by the root
* process. Ignored in the non-root processes.
*
* @exception IndexOutOfBoundsException
* (unchecked exception) Thrown if <TT>root</TT> is not in the range 0
* .. <TT>size()</TT>-1. Thrown if <TT>dstarray</TT>'s length does not
* equal the size of this communicator.
* @exception NullPointerException
* (unchecked exception) Thrown if <TT>dstarray</TT> or any element
* thereof is null. Thrown if <TT>src</TT> is null.
* @exception IOException
* Thrown if an I/O error occurred.
*/
public void gather
(int root,
int tag,
Buf src,
Buf[] dstarray)
throws IOException
{
// Verify preconditions.
if (0 > root || root >= mySize)
{
throw new IndexOutOfBoundsException
("Comm.gather(): root = " + root + " out of bounds");
}
// A gather is done as a series of point-to-point messages. The root
// process receives a separate message from every other process. Here is
// the message pattern for a communicator with 8 processes gathering
// into root process 0:
//
// Process
// 0 1 2 3 4 5 6 7
// | | | | | | | |
// |<----| | | | | | |
// | | | | | | | |
// |<----------| | | | | |
// | | | | | | | |
// |<----------------| | | | |
// | | | | | | | |
// |<----------------------| | | |
// | | | | | | | |
// |<----------------------------| | |
// | | | | | | | |
// |<----------------------------------| |
// | | | | | | | |
// |<----------------------------------------|
// | | | | | | | |
// Root process receives all messages.
if (myRank == root)
{
// Array of IORequest objects for non-blocking receives.
IORequest[] iorequest = new IORequest [mySize];
// Initiate receives from lower-ranked processes.
for (int rank = 0; rank < myRank; ++ rank)
{
iorequest[rank] = new IORequest();
myChannelGroup.receiveNoWait
(getChannelWait (rank),
tag,
dstarray[rank],
iorequest[rank]);
}
// Initiate receives from higher-ranked processes.
for (int rank = myRank+1; rank < mySize; ++ rank)
{
iorequest[rank] = new IORequest();
myChannelGroup.receiveNoWait
(getChannelWait (rank),
tag,
dstarray[rank],
iorequest[rank]);
}
// Copy to itself.
dstarray[myRank].copy (src);
// Wait for completion of receives from lower-ranked processes.
for (int rank = 0; rank < myRank; ++ rank)
{
iorequest[rank].waitForFinish();
}
// Wait for completion of receives from higher-ranked processes.
for (int rank = myRank+1; rank < mySize; ++ rank)
{
iorequest[rank].waitForFinish();
}
}
// Non-root process sends one message.
else
{
myChannelGroup.send (getChannelConnect (root), tag, src);
}
}
/**
* All-gather messages from each process to all processes in this
* communicator. A message tag of 0 is used. All processes must call
* <TT>allGather()</TT>.
* <P>
* Each process sends the message items in the given source buffer. When the
* <TT>allGather()</TT> method returns, the source buffer has been fully
* sent.
* <P>
* Each process receives message items from the other processes. The message
* items received from process <I>i</I> are stored in the destination buffer
* at index <I>i</I> in the given array of destination buffers. This
* destination buffer must have the same length and the same item data type
* as the source buffer in process <I>i</I>. When the <TT>allGather()</TT>
* method returns, all the destination buffers have been fully received.
* <P>
* All-gather is the same as gather, except that every process has an array
* of destination buffers, and every process receives the results of the
* gather.
*
* @param src Source buffer to be sent.
* @param dstarray Array of destination buffers to be received.
*
* @exception IndexOutOfBoundsException
* (unchecked exception) Thrown if <TT>dstarray</TT>'s length does not
* equal the size of this communicator.
* @exception NullPointerException
* (unchecked exception) Thrown if <TT>dstarray</TT> or any element
* thereof is null. Thrown if <TT>src</TT> is null.
* @exception IOException
* Thrown if an I/O error occurred.
*/
public void allGather
(Buf src,
Buf[] dstarray)
throws IOException
{
allGather (0, src, dstarray);
}
/**
* All-gather messages from each process to all processes in this
* communicator using the given message tag. All processes must call
* <TT>allGather()</TT> with the same value for <TT>tag</TT>.
* <P>
* Each process sends the message items in the given source buffer. When the
* <TT>allGather()</TT> method returns, the source buffer has been fully
* sent.
* <P>
* Each process receives message items from the other processes. The message
* items received from process <I>i</I> are stored in the destination buffer
* at index <I>i</I> in the given array of destination buffers. This
* destination buffer must have the same length and the same item data type
* as the source buffer in process <I>i</I>. When the <TT>allGather()</TT>
* method returns, all the destination buffers have been fully received.
* <P>
* All-gather is the same as gather, except that every process has an array
* of destination buffers, and every process receives the results of the
* gather.
*
* @param tag Message tag.
* @param src Source buffer to be sent.
* @param dstarray Array of destination buffers to be received.
*
* @exception IndexOutOfBoundsException
* (unchecked exception) Thrown if <TT>dstarray</TT>'s length does not
* equal the size of this communicator.
* @exception NullPointerException
* (unchecked exception) Thrown if <TT>dstarray</TT> or any element
* thereof is null. Thrown if <TT>src</TT> is null.
* @exception IOException
* Thrown if an I/O error occurred.
*/
public void allGather
(int tag,
Buf src,
Buf[] dstarray)
throws IOException
{
// Get ranks of predecessor and successor processes.
int pred = (myRank - 1 + mySize) % mySize;
int succ = (myRank + 1) % mySize;
// Copy source buffer into destination buffer at my own rank.
dstarray[myRank].copy (src);
// Do (mySize-1) message rounds. Messages are sent in a pipelined
// fashion from each process to its predecessor until each process's
// source data has arrived in every process. Each outgoing message is
// overlapped with an incoming message.
for (int i = 1; i < mySize; ++ i)
{
sendReceive
(/*toRank */ pred,
/*sendTag */ tag,
/*sendBuf */ dstarray[(myRank+i-1) % mySize],
/*fromRank*/ succ,
/*recvTag */ tag,
/*recvBuf */ dstarray[(myRank+i) % mySize]);
}
}
/**
* Perform a reduction on all processes in this communicator. The reduction
* uses a message tag of 0. All processes must call <TT>reduce()</TT> with
* the same value for <TT>root</TT>, with a buffer of the same length and
* the same item data type, and with the same binary operation (class
* {@linkplain edu.rit.pj.reduction.Op Op}).
* <P>
* Before calling <TT>reduce()</TT>, each process has a buffer filled with
* data items. After <TT>reduce()</TT> returns, each data item in the root
* process's buffer has been set to the <B>reduction</B> of the
* corresponding data items in all the processes' buffers. The reduction is
* calculated by this formula:
* <P>
* <I>item</I><SUB>0</SUB> <I>op</I>
* <I>item</I><SUB>1</SUB> <I>op</I> <I>item</I><SUB>2</SUB> <I>op</I> . . .
* <P>
* where <I>op</I> is the binary operation passed in as an argument and
* <I>item</I><SUB>0</SUB>, <I>item</I><SUB>1</SUB>,
* <I>item</I><SUB>2</SUB>, and so on are the data items in the buffers of
* process rank 0, 1, 2, and so on. However, the order in which the data
* items actually are combined is not specified. Therefore, the binary
* operation must be such that the answer will be the same regardless of the
* order in which the data items are combined; that is, the binary operation
* must be commutative and associative.
* <P>
* In the root process, the reduce operation always changes the buffer's
* contents as described above. In the non-root processes, the reduce
* operation may or may not change the buffer's contents; the final contents
* of the buffer in the non-root processes is not specified.
* <P>
* When the <TT>reduce()</TT> method returns in the root process, the
* reduction has been fully performed as described above. When the
* <TT>reduce()</TT> method returns in a non-root process, the non-root
* process has sent all its data items into the reduction, but the reduction
* may not be fully complete in the root process yet.
*
* @param root Root process's rank in this communicator.
* @param buffer Buffer of data items to be reduced.
* @param op Binary operation.
*
* @exception IndexOutOfBoundsException
* (unchecked exception) Thrown if <TT>root</TT> is not in the range 0
* .. <TT>size()</TT>-1.
* @exception NullPointerException
* (unchecked exception) Thrown if <TT>buf</TT> is null or <TT>op</TT>
* is null.
* @exception ClassCastException
* (unchecked exception) Thrown if <TT>buf</TT> and <TT>op</TT> do not
* use the same item data type.
* @exception IOException
* Thrown if an I/O error occurred.
*/
public void reduce
(int root,
Buf buffer,
Op op)
throws IOException
{
reduce (root, 0, buffer, op);
}
/**
* Perform a reduction on all processes in this communicator using the given
* message tag. All processes must call <TT>reduce()</TT> with the same
* value for <TT>root</TT>, with a buffer of the same length and the same
* item data type, and with the same binary operation (class {@linkplain
* edu.rit.pj.reduction.Op Op}).
* <P>
* Before calling <TT>reduce()</TT>, each process has a buffer filled with
* data items. After <TT>reduce()</TT> returns, each data item in the root
* process's buffer has been set to the <B>reduction</B> of the
* corresponding data items in all the processes' buffers. The reduction is
* calculated by this formula:
* <P>
* <I>item</I><SUB>0</SUB> <I>op</I>
* <I>item</I><SUB>1</SUB> <I>op</I> <I>item</I><SUB>2</SUB> <I>op</I> . . .
* <P>
* where <I>op</I> is the binary operation passed in as an argument and
* <I>item</I><SUB>0</SUB>, <I>item</I><SUB>1</SUB>,
* <I>item</I><SUB>2</SUB>, and so on are the data items in the buffers of
* process rank 0, 1, 2, and so on. However, the order in which the data
* items actually are combined is not specified. Therefore, the binary
* operation must be such that the answer will be the same regardless of the
* order in which the data items are combined; that is, the binary operation
* must be commutative and associative.
* <P>
* In the root process, the reduce operation always changes the buffer's
* contents as described above. In the non-root processes, the reduce
* operation may or may not change the buffer's contents; the final contents
* of the buffer in the non-root processes is not specified.
* <P>
* When the <TT>reduce()</TT> method returns in the root process, the
* reduction has been fully performed as described above. When the
* <TT>reduce()</TT> method returns in a non-root process, the non-root
* process has sent all its data items into the reduction, but the reduction
* may not be fully complete in the root process yet.
*
* @param root Root process's rank in this communicator.
* @param tag Message tag.
* @param buffer Buffer of data items to be reduced.
* @param op Binary operation.
*
* @exception IndexOutOfBoundsException
* (unchecked exception) Thrown if <TT>root</TT> is not in the range 0
* .. <TT>size()</TT>-1.
* @exception NullPointerException
* (unchecked exception) Thrown if <TT>buf</TT> is null or <TT>op</TT>
* is null.
* @exception ClassCastException
* (unchecked exception) Thrown if <TT>buf</TT> and <TT>op</TT> do not
* use the same item data type.
* @exception IOException
* Thrown if an I/O error occurred.
*/
public void reduce
(int root,
int tag,
Buf buffer,
Op op)
throws IOException
{
// Verify preconditions.
if (0 > root || root >= mySize)
{
throw new IndexOutOfBoundsException
("Comm.reduce(): root = " + root + " out of bounds");
}
// Early return if only one process.
if (mySize == 1) return;
// A reduction is done as a series of point-to-point messages. The
// messages are organized into rounds. The number of rounds is
// ceil(log_2(mySize)). The message pattern is the reverse of the
// broadcast message pattern. In each round, processes receive messages
// from other processes and reduce the data items into their accumulator
// buffers in parallel. When a process has received all messages, it
// sends the reduced results on. Here is the message pattern for a
// communicator with 8 processes doing a reduction into root process 0:
//
// Process
// 0 1 2 3 4 5 6 7
// | | | | | | | |
// |<----------------------| | | | Round 1
// | |<----------------------| | |
// | | |<----------------------| |
// | | | |<----------------------|
// | | | | | | | |
// |<----------| | | | | | Round 2
// | |<----------| | | | |
// | | | | | | | |
// |<----| | | | | | | Round 3
// | | | | | | | |
//
// If a process other than process 0 is the root, the message pattern is
// the same, except the process ranks are circularly rotated.
// Get array of process ranks in the broadcast tree.
int[] broadcasttree = getBroadcastTree (root);
int n = broadcasttree.length;
// Set up reduction buffer on top of source buffer.
Buf reductionbuf = buffer.getReductionBuf (op);
// Receive data from children if any, one at a time in reverse order.
for (int i = n-1; i >= 1; -- i)
{
int child = broadcasttree[i];
myChannelGroup.receive
(getChannelWait (child),
tag,
reductionbuf);
}
// Send data to parent if any.
int parent = broadcasttree[0];
if (parent != -1)
{
myChannelGroup.send (getChannelConnect (parent), tag, buffer);
}
}
/**
* Perform an all-reduce on all processes in this communicator. The
* all-reduce uses a message tag of 0. All processes must call
* <TT>allReduce()</TT> with a buffer of the same length and the same item
* data type, and with the same binary operation (class {@linkplain
* edu.rit.pj.reduction.Op Op}).
* <P>
* Before calling <TT>allReduce()</TT>, each process has a buffer filled
* with data items. After <TT>allReduce()</TT> returns, each data item in
* the calling process's buffer has been set to the <B>reduction</B> of the
* corresponding data items in all the processes' buffers. The reduction is
* calculated by this formula:
* <P>
* <I>item</I><SUB>0</SUB> <I>op</I>
* <I>item</I><SUB>1</SUB> <I>op</I> <I>item</I><SUB>2</SUB> <I>op</I> . . .
* <P>
* where <I>op</I> is the binary operation passed in as an argument and
* <I>item</I><SUB>0</SUB>, <I>item</I><SUB>1</SUB>,
* <I>item</I><SUB>2</SUB>, and so on are the data items in the buffers of
* process rank 0, 1, 2, and so on. However, the order in which the data
* items actually are combined is not specified. Therefore, the binary
* operation must be such that the answer will be the same regardless of the
* order in which the data items are combined; that is, the binary operation
* must be commutative and associative.
* <P>
* The <TT>allReduce()</TT> method is similar to the <TT>reduce()</TT>
* method, except the results are stored in all the processes' buffers, not
* just the one root process's buffer.
*
* @param buffer Buffer of data items to be reduced.
* @param op Binary operation.
*
* @exception NullPointerException
* (unchecked exception) Thrown if <TT>buf</TT> is null or <TT>op</TT>
* is null.
* @exception ClassCastException
* (unchecked exception) Thrown if <TT>buf</TT> and <TT>op</TT> do not
* use the same item data type.
* @exception IOException
* Thrown if an I/O error occurred.
*/
public void allReduce
(Buf buffer,
Op op)
throws IOException
{
allReduce (0, buffer, op);
}
/**
* Perform an all-reduce on all processes in this communicator using the
* given message tag. All processes must call <TT>allReduce()</TT> with a
* buffer of the same length and the same item data type, and with the same
* binary operation (class {@linkplain edu.rit.pj.reduction.Op Op}).
* <P>
* Before calling <TT>allReduce()</TT>, each process has a buffer filled
* with data items. After <TT>allReduce()</TT> returns, each data item in
* the calling process's buffer has been set to the <B>reduction</B> of the
* corresponding data items in all the processes' buffers. The reduction is
* calculated by this formula:
* <P>
* <I>item</I><SUB>0</SUB> <I>op</I>
* <I>item</I><SUB>1</SUB> <I>op</I> <I>item</I><SUB>2</SUB> <I>op</I> . . .
* <P>
* where <I>op</I> is the binary operation passed in as an argument and
* <I>item</I><SUB>0</SUB>, <I>item</I><SUB>1</SUB>,
* <I>item</I><SUB>2</SUB>, and so on are the data items in the buffers of
* process rank 0, 1, 2, and so on. However, the order in which the data
* items actually are combined is not specified. Therefore, the binary
* operation must be such that the answer will be the same regardless of the
* order in which the data items are combined; that is, the binary operation
* must be commutative and associative.
* <P>
* The <TT>allReduce()</TT> method is similar to the <TT>reduce()</TT>
* method, except the results are stored in all the processes' buffers, not
* just the one root process's buffer.
*
* @param tag Message tag.
* @param buffer Buffer of data items to be reduced.
* @param op Binary operation.
*
* @exception NullPointerException
* (unchecked exception) Thrown if <TT>buf</TT> is null or <TT>op</TT>
* is null.
* @exception ClassCastException
* (unchecked exception) Thrown if <TT>buf</TT> and <TT>op</TT> do not
* use the same item data type.
* @exception IOException
* Thrown if an I/O error occurred.
*/
public void allReduce
(int tag,
Buf buffer,
Op op)
throws IOException
{
// An all-reduce is done using a "butterfly" message passing pattern.
// Consider the case of K=8 processes. In the first round, processes one
// rank apart exchange data, then each processes accumulates the data
// from the other process using the reduction operator. In the second
// round, processes two ranks apart exchange and accumulate data. In the
// third round, processes four ranks apart exchange and accumulate data.
//
// Process
// 0 1 2 3 4 5 6 7
// | | | | | | | |
// |<--->| |<--->| |<--->| |<--->| Round 1
// | | | | | | | |
// |<--------->| | |<--------->| | Round 2
// | |<--------->| | |<--------->|
// | | | | | | | |
// |<--------------------->| | | | Round 3
// | |<--------------------->| | |
// | | |<--------------------->| |
// | | | |<--------------------->|
// | | | | | | | |
//
// The butterfly pattern works only if the number of processes is a
// power of two. If this is not the case, there are two extra message
// rounds. Each process outside the butterfly pattern sends its data to
// its counterpart inside the butterfly pattern, which accumulates the
// data using the reduction operator. Then the butterfly pattern takes
// place. Afterwards, each process outside the butterfly pattern
// receives the final result from its counterpart inside the butterfly
// pattern. For the case of K=10 processes:
//
// Process
// 0 1 2 3 4 5 6 7 8 9
// | | | | | | | | | |
// |<----------------------------------------------| | Pre
// | |<----------------------------------------------|
// | | | | | | | | | |
// |<--->| |<--->| |<--->| |<--->| | | Round 1
// | | | | | | | | | |
// |<--------->| | |<--------->| | | | Round 2
// | |<--------->| | |<--------->| | |
// | | | | | | | | | |
// |<--------------------->| | | | | | Round 3
// | |<--------------------->| | | | |
// | | |<--------------------->| | | |
// | | | |<--------------------->| | |
// | | | | | | | | | |
// |---------------------------------------------->| | Post
// | |---------------------------------------------->|
// | | | | | | | | | |
//
// If K is a power of two, the all-reduce takes (log_2 K) rounds. If K
// is not a power of two, the all-reduce takes floor(log_2 K)+2 rounds.
// Early exit if only one process.
if (mySize == 1) return;
// Determine the highest power of 2 less than or equal to this
// communicator's size. Processes at this rank and above will be outside
// the butterfly message passing pattern.
int outside = mySizePowerOf2;
// For processes outside the butterfly:
if (myRank >= outside)
{
int insideRank = myRank - outside;
// Send initial data to counterpart inside.
System.out.println ("allReduce(): "+myRank+" --> "+insideRank);
send (insideRank, tag, buffer);
// Receive reduced result from counterpart inside.
receive (insideRank, tag, buffer);
System.out.println ("allReduce(): "+myRank+" <-- "+insideRank);
}
// For processes inside the butterfly:
else
{
// Set up temporary receive buffer.
Buf receiveBuf = buffer.getTemporaryBuf();
// Set up reduction buffer on top of data buffer.
Buf reductionBuf = buffer.getReductionBuf (op);
// If there is a counterpart process outside, receive and accumulate
// its initial data.
int outsideRank = myRank + outside;
if (outsideRank < mySize)
{
receive (outsideRank, tag, reductionBuf);
System.out.println ("allReduce(): "+myRank+" <-- "+outsideRank);
}
// Perform butterfly message passing rounds with counterpart
// processes inside.
int round = 1;
while (round < outside)
{
int otherRank = myRank ^ round;
System.out.println ("allReduce(): "+myRank+" --> "+otherRank);
sendReceive
(otherRank, tag, buffer, otherRank, tag, receiveBuf);
System.out.println ("allReduce(): "+myRank+" <-- "+otherRank);
reductionBuf.copy (receiveBuf);
round <<= 1;
}
// If there is a counterpart process outside, send the reduced
// result.
if (outsideRank < mySize)
{
System.out.println ("allReduce(): "+myRank+" --> "+outsideRank);
send (outsideRank, tag, buffer);
}
}
}
/**
* Do an all-to-all among all processes in this communicator. A message tag
* of 0 is used.
* <P>
* <TT>srcarray</TT> must be an array of <I>K</I> buffers, where <I>K</I> is
* the size of this communicator. <TT>dstarray</TT> must be an array of
* <I>K</I> buffers referring to different storage from the source buffers.
* For each process rank <I>k</I>, 0 <= <I>k</I> <= <I>K</I>, and each
* buffer index <I>i</I>, 0 <= <I>i</I> <= <I>K</I>, the contents of
* <TT>srcarray[k]</TT> in process <I>i</I> are sent to <TT>dstarray[i]</TT>
* in process <I>k</I>.
*
* @param srcarray Array of source buffers to be sent by this process.
* @param dstarray Array of destination buffers to be received by this
* process.
*
* @exception IndexOutOfBoundsException
* (unchecked exception) Thrown if <TT>srcarray</TT>'s length does not
* equal the size of this communicator. Thrown if <TT>dstarray</TT>'s
* length does not equal the size of this communicator.
* @exception NullPointerException
* (unchecked exception) Thrown if <TT>srcarray</TT> or any element
* thereof is null. Thrown if <TT>dstarray</TT> or any element thereof
* is null.
* @exception IOException
* Thrown if an I/O error occurred.
*/
public void allToAll
(Buf[] srcarray,
Buf[] dstarray)
throws IOException
{
allToAll (0, srcarray, dstarray);
}
/**
* Do an all-to-all among all processes in this communicator using the given
* message tag. All processes must call <TT>allToAll()</TT> with the same
* value for <TT>tag</TT>.
* <P>
* <TT>srcarray</TT> must be an array of <I>K</I> buffers, where <I>K</I> is
* the size of this communicator. <TT>dstarray</TT> must be an array of
* <I>K</I> buffers referring to different storage from the source buffers.
* For each process rank <I>k</I>, 0 <= <I>k</I> <= <I>K</I>, and each
* buffer index <I>i</I>, 0 <= <I>i</I> <= <I>K</I>, the contents of
* <TT>srcarray[k]</TT> in process <I>i</I> are sent to <TT>dstarray[i]</TT>
* in process <I>k</I>.
*
* @param tag Message tag.
* @param srcarray Array of source buffers to be sent by this process.
* @param dstarray Array of destination buffers to be received by this
* process.
*
* @exception IndexOutOfBoundsException
* (unchecked exception) Thrown if <TT>srcarray</TT>'s length does not
* equal the size of this communicator. Thrown if <TT>dstarray</TT>'s
* length does not equal the size of this communicator.
* @exception NullPointerException
* (unchecked exception) Thrown if <TT>srcarray</TT> or any element
* thereof is null. Thrown if <TT>dstarray</TT> or any element thereof
* is null.
* @exception IOException
* Thrown if an I/O error occurred.
*/
public void allToAll
(int tag,
Buf[] srcarray,
Buf[] dstarray)
throws IOException
{
// An all-to-all is done as a series of send-receives. Each process
// sends the appropriate buffer to the process one ahead and receives
// the appropriate buffer from the process one behind. Then each process
// sends the appropriate buffer to the process two ahead and receives
// the appropriate buffer from the process two behind. And so on. Here
// is the message pattern for a communicator with 4 processes doing an
// all-to-all:
//
// Process
// 0 1 2 3
// | | | |
// |---->| | | Round 1
// | |---->| |
// | | |---->|
// - -->| | | |--- -
// | | | |
// | | | |
// |---------->| | Round 2
// | |---------->|
// - -->| | |--------- -
// - -------->| | |--- -
// | | | |
// | | | |
// |---------------->| Round 3
// - -->| |--------------- -
// - -------->| |--------- -
// - -------------->| |--- -
// | | | |
// Copy source to destination at this process's own rank.
dstarray[myRank].copy (srcarray[myRank]);
// Initiate K-1 non-blocking send-receives.
CommRequest[] commrequest = new CommRequest [mySize];
for (int i = 1; i < mySize; ++ i)
{
int toRank = (myRank + i) % mySize;
int fromRank = (myRank - i + mySize) % mySize;
commrequest[i] =
sendReceive
(toRank, tag, srcarray[toRank],
fromRank, tag, dstarray[fromRank],
(CommRequest) null);
}
// Wait for completion of all send-receives.
for (int i = 1; i < mySize; ++ i)
{
commrequest[i].waitForFinish();
}
}
/**
* Perform a scan on all processes in this communicator. A message tag of 0
* is used. All processes must call <TT>scan()</TT> with a buffer of the
* same length and the same item data type, and with the same binary
* operation (class {@linkplain edu.rit.pj.reduction.Op Op}).
* <P>
* Before calling <TT>scan()</TT>, each process has a buffer filled with
* data items. After <TT>scan()</TT> returns, each data item in the buffer
* of process rank <I>i</I> has been set to the <B>reduction</B> of the
* corresponding data items in the buffers of process ranks 0 through
* <I>i</I>. The reduction is calculated by this formula:
* <P>
* <I>item</I><SUB>0</SUB> <I>op</I>
* <I>item</I><SUB>1</SUB> <I>op</I> <I>item</I><SUB>2</SUB> <I>op</I> . . .
* <P>
* where <I>op</I> is the binary operation passed in as an argument and
* <I>item</I><SUB>0</SUB>, <I>item</I><SUB>1</SUB>,
* <I>item</I><SUB>2</SUB>, and so on are the data items in the buffers of
* process ranks 0 through <I>i</I>. However, the order in which the data
* items actually are combined is not specified. Therefore, the binary
* operation must be such that the answer will be the same regardless of the
* order in which the data items are combined; that is, the binary operation
* must be commutative and associative.
*
* @param buf Buffer of data items to be scanned.
* @param op Binary operation.
*
* @exception NullPointerException
* (unchecked exception) Thrown if <TT>buf</TT> is null or <TT>op</TT>
* is null.
* @exception ClassCastException
* (unchecked exception) Thrown if <TT>buf</TT> and <TT>op</TT> do not
* use the same item data type.
* @exception IOException
* Thrown if an I/O error occurred.
*/
public void scan
(Buf buf,
Op op)
throws IOException
{
scan (0, buf, op);
}
/**
* Perform a scan on all processes in this communicator using the given
* message tag. All processes must call <TT>scan()</TT> with the same value
* for <TT>tag</TT>, with a buffer of the same length and the same item data
* type, and with the same binary operation (class {@linkplain
* edu.rit.pj.reduction.Op Op}).
* <P>
* Before calling <TT>scan()</TT>, each process has a buffer filled with
* data items. After <TT>scan()</TT> returns, each data item in the buffer
* of process rank <I>i</I> has been set to the <B>reduction</B> of the
* corresponding data items in the buffers of process ranks 0 through
* <I>i</I>. The reduction is calculated by this formula:
* <P>
* <I>item</I><SUB>0</SUB> <I>op</I>
* <I>item</I><SUB>1</SUB> <I>op</I> <I>item</I><SUB>2</SUB> <I>op</I> . . .
* <P>
* where <I>op</I> is the binary operation passed in as an argument and
* <I>item</I><SUB>0</SUB>, <I>item</I><SUB>1</SUB>,
* <I>item</I><SUB>2</SUB>, and so on are the data items in the buffers of
* process ranks 0 through <I>i</I>. However, the order in which the data
* items actually are combined is not specified. Therefore, the binary
* operation must be such that the answer will be the same regardless of the
* order in which the data items are combined; that is, the binary operation
* must be commutative and associative.
*
* @param tag Message tag.
* @param buf Buffer of data items to be scanned.
* @param op Binary operation.
*
* @exception NullPointerException
* (unchecked exception) Thrown if <TT>buf</TT> is null or <TT>op</TT>
* is null.
* @exception ClassCastException
* (unchecked exception) Thrown if <TT>buf</TT> and <TT>op</TT> do not
* use the same item data type.
* @exception IOException
* Thrown if an I/O error occurred.
*/
public void scan
(int tag,
Buf buf,
Op op)
throws IOException
{
// Early return if only one process.
if (mySize == 1) return;
// A scan is done as a series of point-to-point messages. The messages
// are organized into rounds. The number of rounds is
// ceil(log_2(mySize)). In the first round, each process sends its data
// to the process one rank ahead, and the incoming data is combined with
// the process's data using the reduction operator. In the second round,
// each process sends its data to the process two ranks ahead. In the
// third round, each process sends its data to process four ranks ahead.
// And so on. Here is the message pattern for a communicator with 8
// processes:
//
// Process
// 0 1 2 3 4 5 6 7
// | | | | | | | |
// |---->|---->|---->|---->|---->|---->|---->| Round 1
// | | | | | | | |
// |---------->| | | | | | Round 2
// | |---------->| | | | |
// | | |---------->| | | |
// | | | |---------->| | |
// | | | | |---------->| |
// | | | | | |---------->|
// | | | | | | | |
// |---------------------->| | | | Round 3
// | |---------------------->| | |
// | | |---------------------->| |
// | | | |---------------------->|
//
// Get temporary buffer for holding incoming data items.
Buf tempbuf = buf.getTemporaryBuf();
// Get reduction buffer for combining data items.
Buf reductionbuf = buf.getReductionBuf (op);
// Do rounds of message passing and reduction.
int skip = 1;
for (;;)
{
int toRank = myRank + skip;
int fromRank = myRank - skip;
boolean toExists = 0 <= toRank && toRank < mySize;
boolean fromExists = 0 <= fromRank && fromRank < mySize;
if (toExists && fromExists)
{
sendReceive (toRank, tag, buf, fromRank, tag, tempbuf);
reductionbuf.copy (tempbuf);
}
else if (fromExists)
{
receive (fromRank, tag, reductionbuf);
}
else if (toExists)
{
send (toRank, tag, buf);
}
else break;
skip <<= 1;
}
}
/**
* Perform an exclusive scan on all processes in this communicator. A
* message tag of 0 is used. All processes must call
* <TT>exclusiveScan()</TT> with a buffer of the same length and the same
* item data type, with the same binary operation (class {@linkplain
* edu.rit.pj.reduction.Op Op}), and with the same initial data value.
* <P>
* Before calling <TT>exclusiveScan()</TT>, each process has a buffer filled
* with data items. After <TT>exclusiveScan()</TT> returns, each data item
* in the buffer of process rank <I>i</I> > 0 has been set to the
* <B>reduction</B> of the corresponding data items in the buffers of
* process ranks 0 through <I>i</I>-1. The reduction is calculated by this
* formula:
* <P>
* <I>item</I><SUB>0</SUB> <I>op</I>
* <I>item</I><SUB>1</SUB> <I>op</I> <I>item</I><SUB>2</SUB> <I>op</I> . . .
* <P>
* where <I>op</I> is the binary operation passed in as an argument and
* <I>item</I><SUB>0</SUB>, <I>item</I><SUB>1</SUB>,
* <I>item</I><SUB>2</SUB>, and so on are the data items in the buffers of
* process ranks 0 through <I>i</I>-1. However, the order in which the data
* items actually are combined is not specified. Therefore, the binary
* operation must be such that the answer will be the same regardless of the
* order in which the data items are combined; that is, the binary operation
* must be commutative and associative.
* <P>
* In process 0, each data item in the buffer has been set to the initial
* data value using the buffer's <TT>fill()</TT> method.
* <P>
* If the buffer's item data type is a primitive type, the <TT>item</TT>
* must be an instance of the corresponding primitive wrapper class -- class
* Integer for type <TT>int</TT>, class Double for type <TT>double</TT>, and
* so on. If the <TT>item</TT> is null, the item data type's default initial
* value is assigned to each element in the buffer.
* <P>
* If the buffer's item data type is a nonprimitive type, the <TT>item</TT>
* must be an instance of the item class or a subclass thereof. The
* <TT>item</TT> may be null. Note that since <TT>item</TT> is
* <I>assigned</I> to every buffer element, every buffer element ends up
* referring to the same <TT>item</TT>.
*
* @param buf Buffer of data items to be scanned.
* @param op Binary operation.
* @param item Initial data value.
*
* @exception NullPointerException
* (unchecked exception) Thrown if <TT>buf</TT> is null or <TT>op</TT>
* is null.
* @exception ClassCastException
* (unchecked exception) Thrown if <TT>buf</TT> and <TT>op</TT> do not
* use the same item data type.
* @exception IOException
* Thrown if an I/O error occurred.
*/
public void exclusiveScan
(Buf buf,
Op op,
Object item)
throws IOException
{
exclusiveScan (0, buf, op, item);
}
/**
* Perform an exclusive scan on all processes in this communicator using the
* given message tag. All processes must call <TT>exclusiveScan()</TT> with
* the same value for <TT>tag</TT>, with a buffer of the same length and the
* same item data type, with the same binary operation (class {@linkplain
* edu.rit.pj.reduction.Op Op}), and with the same initial data value.
* <P>
* Before calling <TT>exclusiveScan()</TT>, each process has a buffer filled
* with data items. After <TT>exclusiveScan()</TT> returns, each data item
* in the buffer of process rank <I>i</I> > 0 has been set to the
* <B>reduction</B> of the corresponding data items in the buffers of
* process ranks 0 through <I>i</I>-1. The reduction is calculated by this
* formula:
* <P>
* <I>item</I><SUB>0</SUB> <I>op</I>
* <I>item</I><SUB>1</SUB> <I>op</I> <I>item</I><SUB>2</SUB> <I>op</I> . . .
* <P>
* where <I>op</I> is the binary operation passed in as an argument and
* <I>item</I><SUB>0</SUB>, <I>item</I><SUB>1</SUB>,
* <I>item</I><SUB>2</SUB>, and so on are the data items in the buffers of
* process ranks 0 through <I>i</I>-1. However, the order in which the data
* items actually are combined is not specified. Therefore, the binary
* operation must be such that the answer will be the same regardless of the
* order in which the data items are combined; that is, the binary operation
* must be commutative and associative.
* <P>
* In process 0, each data item in the buffer has been set to the initial
* data value using the buffer's <TT>fill()</TT> method.
* <P>
* If the buffer's item data type is a primitive type, the <TT>item</TT>
* must be an instance of the corresponding primitive wrapper class -- class
* Integer for type <TT>int</TT>, class Double for type <TT>double</TT>, and
* so on. If the <TT>item</TT> is null, the item data type's default initial
* value is assigned to each element in the buffer.
* <P>
* If the buffer's item data type is a nonprimitive type, the <TT>item</TT>
* must be an instance of the item class or a subclass thereof. The
* <TT>item</TT> may be null. Note that since <TT>item</TT> is
* <I>assigned</I> to every buffer element, every buffer element ends up
* referring to the same <TT>item</TT>.
*
* @param tag Message tag.
* @param buf Buffer of data items to be scanned.
* @param op Binary operation.
* @param item Initial data value.
*
* @exception NullPointerException
* (unchecked exception) Thrown if <TT>buf</TT> is null or <TT>op</TT>
* is null.
* @exception ClassCastException
* (unchecked exception) Thrown if <TT>buf</TT> and <TT>op</TT> do not
* use the same item data type.
* @exception IOException
* Thrown if an I/O error occurred.
*/
public void exclusiveScan
(int tag,
Buf buf,
Op op,
Object item)
throws IOException
{
// An exclusive scan begins with each process sending its buffer to the
// next higher process. Then process 0 fills its buffer with the initial
// data value, while processes 1 and higher do an inclusive scan.
int toRank;
int fromRank;
boolean toExists;
boolean fromExists;
// Process 0 does this.
if (myRank == 0)
{
// Send buffer to next higher process.
toRank = 1;
toExists = toRank < mySize;
if (toExists)
{
send (toRank, tag, buf);
}
// Fill buffer with initial data value.
buf.fill (item);
}
// Processes 1 and higher do this.
else
{
// Get temporary buffer for holding incoming data items.
Buf tempbuf = buf.getTemporaryBuf();
// Get reduction buffer for combining data items.
Buf reductionbuf = buf.getReductionBuf (op);
// Send buffer to next higher process.
toRank = myRank + 1;
fromRank = myRank - 1;
toExists = 0 <= toRank && toRank < mySize;
if (toExists)
{
sendReceive (toRank, tag, buf, fromRank, tag, tempbuf);
buf.copy (tempbuf);
}
else
{
receive (fromRank, tag, buf);
}
// Do rounds of message passing and reduction.
int skip = 1;
for (;;)
{
toRank = myRank + skip;
fromRank = myRank - skip;
toExists = 1 <= toRank && toRank < mySize;
fromExists = 1 <= fromRank && fromRank < mySize;
if (toExists && fromExists)
{
sendReceive (toRank, tag, buf, fromRank, tag, tempbuf);
reductionbuf.copy (tempbuf);
}
else if (fromExists)
{
receive (fromRank, tag, reductionbuf);
}
else if (toExists)
{
send (toRank, tag, buf);
}
else break;
skip <<= 1;
}
}
}
/**
* Cause all processes in this communicator to wait at a barrier. The
* barrier uses a message tag of 0. All processes must call
* <TT>barrier()</TT>. The calling thread blocks until every process has
* called <TT>barrier()</TT>, then the calling thread unblocks and returns
* from the <TT>barrier()</TT> call.
*
* @exception IOException
* Thrown if an I/O error occurred.
*/
public void barrier()
throws IOException
{
barrier (0);
}
/**
* Cause all processes in this communicator to wait at a barrier, using the
* given message tag. All processes must call <TT>barrier()</TT> with the
* same tag. The calling thread blocks until every process has called
* <TT>barrier()</TT>, then the calling thread unblocks and returns from the
* <TT>barrier()</TT> call.
*
* @param tag Message tag.
*
* @exception IOException
* Thrown if an I/O error occurred.
*/
public void barrier
(int tag)
throws IOException
{
// A barrier is done as an all-reduce of an empty buffer.
allReduce (tag, IntegerBuf.emptyBuffer(), IntegerOp.SUM);
}
/**
* Returns a string version of this communicator. The string includes the
* communicator's size, the current process's rank, and the host and port of
* each backend process.
*
* @return String version.
*/
public String toString()
{
StringBuilder buf = new StringBuilder();
buf.append ("Comm(size=");
buf.append (mySize);
buf.append (",rank=");
buf.append (myRank);
buf.append (",backend");
for (int i = 0; i < mySize; ++ i)
{
if (i > 0) buf.append (',');
buf.append ('[');
buf.append (i);
buf.append ("]=");
buf.append (myAddressForRank[i]);
}
buf.append (')');
return buf.toString();
}
/**
* Dump the state of this communicator on the given print stream. For
* debugging.
*
* @param out Print stream.
* @param prefix String to print at the beginning of each line.
*/
public void dump
(PrintStream out,
String prefix)
{
out.println();
out.println (prefix+getClass().getName()+"@"+Integer.toHexString(System.identityHashCode(this)));
out.println (prefix+"mySize = "+mySize);
out.println (prefix+"myRank = "+myRank);
out.println (prefix+"myHost = "+myHost);
out.println (prefix+"mySizePowerOf2 = "+mySizePowerOf2);
out.println (prefix+"myChannelGroup = "+myChannelGroup);
out.println (prefix+"myAddressForRank:");
for (int i = 0; i < myAddressForRank.length; ++ i)
{
out.println (prefix+"\t["+i+"] "+myAddressForRank[i]);
}
out.println (prefix+"myChannelForRank:");
for (int i = 0; i < myChannelForRank.length; ++ i)
{
out.println (prefix+"\t["+i+"] "+myChannelForRank[i]);
}
out.println (prefix+"myBroadcastTree:");
for (int i = 0; i < myBroadcastTree.length; ++ i)
{
out.print (prefix+"\t["+i+"]");
int[] tree = myBroadcastTree[i];
if (tree == null)
{
out.print (" null");
}
else
{
for (int j = 0; j < tree.length; ++ j)
{
out.print (" "+tree[j]);
}
}
out.println();
}
out.println();
myChannelGroup.dump (out, prefix);
}
// Hidden operations.
/**
* Notify that another process connected a channel to this process.
*
* @param theChannel Channel.
*
* @exception IOException
* Thrown if an I/O error occurred.
*/
private synchronized void doFarEndConnected
(Channel theChannel)
throws IOException
{
// Record channel and rank.
myChannelForRank[getFarRank(theChannel)] = theChannel;
// Notify any threads waiting in getChannelWait().
notifyAll();
}
/**
* Get the channel for communicating with the process at the given rank. If
* the channel does not exist yet, initiate the outgoing connection. In
* general, the sending process initiates the outgoing connection, but there
* are some exceptions.
*
* @param farrank Rank of far end process.
*
* @exception IOException
* Thrown if an I/O error occurred.
*/
private synchronized Channel getChannelConnect
(int farrank)
throws IOException
{
// Get channel from channel array.
Channel channel = myChannelForRank[farrank];
// If the channel does not exist:
if (channel == null)
{
// Set up outgoing connection to far end.
InetSocketAddress faraddress = myAddressForRank[farrank];
channel = myChannelGroup.connect (faraddress);
// Record channel and rank.
myChannelForRank[farrank] = channel;
}
return channel;
}
/**
* Get the channel for communicating with the process at the given rank. If
* the channel does not exist yet, wait for the incoming connection. In
* general, the receiving process waits for the incoming connection, but
* there are some exceptions.
*
* @param farrank Rank of far end process.
*
* @exception IOException
* Thrown if an I/O error occurred.
*/
private synchronized Channel getChannelWait
(int farrank)
throws IOException
{
// Get channel from channel array.
Channel channel = myChannelForRank[farrank];
// If the channel does not exist:
if (channel == null)
{
// Wait for the channel to show up.
try
{
while (channel == null)
{
wait();
channel = myChannelForRank[farrank];
}
}
catch (InterruptedException exc)
{
IOException exc2 = new InterruptedIOException();
exc2.initCause (exc);
throw exc2;
}
}
return channel;
}
/**
* Get the rank of the process at the far end of the given channel.
*
* @param channel Channel.
*
* @return Far end process rank.
*/
static int getFarRank
(Channel channel)
{
return channel.farEndChannelGroupId();
}
/**
* Get an array of process ranks in the broadcast tree for the given root.
* The broadcast tree is cached in the field myBroadcastTree for later use.
*
* @param root Root process's rank.
*
* @return Broadcast tree.
*/
private synchronized int[] getBroadcastTree
(int root)
{
if (myBroadcastTree == null)
{
myBroadcastTree = new int [mySize] [];
}
int[] broadcasttree = myBroadcastTree[root];
if (broadcasttree == null)
{
broadcasttree = CommPattern.broadcastPattern (mySize, myRank, root);
myBroadcastTree[root] = broadcasttree;
}
return broadcasttree;
}
}
|