1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214
|
/* mplrs.c: MPI version
This program 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 2
of the License, or (at your option) any later version.
This program 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.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Author: Charles Jordan skip@res.otaru-uc.ac.jp
Based on plrs.cpp by Gary Roumanis
Initial lrs Author: David Avis avis@cs.mcgill.ca
*/
/* #include "lrslib.h" */ /* included in mplrs.h */
#include "mplrs.h"
#include <mpi.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <sys/time.h>
#include <time.h>
#include <ctype.h>
/* global variables */
mplrsv mplrs; /* state of this process */
masterv master; /* state of the master */
consumerv consumer; /* state of the consumer */
char argv0[] = "mplrs-internal"; /* warning removal for C++ */
/******************
* initialization *
******************/
int main(int argc, char **argv)
{
mplrs_init(argc, argv);
mprintf2(("%d: initialized on %s\n",mplrs.rank,mplrs.host));
if (mplrs.size<3)
return mplrs_fallback();
if (mplrs.rank == MASTER)
return mplrs_master();
else if (mplrs.rank == CONSUMER)
return mplrs_consumer();
else
return mplrs_worker();
}
void mplrs_init(int argc, char **argv)
{
int count;
int header[9];
time_t curt = time(NULL);
char *tim, *tim1;
long *wred = NULL; /* redineq for redund */
/* make timestamp for filenames */
tim = ctime(&curt);
tim1 = tim+4;
tim1[3] = tim1[6] = '_';
tim1[15]='\0';
/* start MPI */
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &mplrs.rank);
MPI_Comm_size(MPI_COMM_WORLD, &mplrs.size);
MPI_Get_processor_name(mplrs.host, &count);
/* lrs_mp_init to get correct MAXDl etc when CONS not defined */
lrs_mp_init(0, stdin, stdout);
/* allocate mp for volume calculation, from plrs */
lrs_alloc_mp(mplrs.tN); lrs_alloc_mp(mplrs.tD);
lrs_alloc_mp(mplrs.Vnum); lrs_alloc_mp(mplrs.Vden);
lrs_alloc_mp(mplrs.Tnum); lrs_alloc_mp(mplrs.Tden);
itomp(ZERO, mplrs.Vnum); itomp(ONE, mplrs.Vden);
gettimeofday(&mplrs.start, NULL);
mplrs_initstrucs(); /* initialize default values of globals */
/* process commandline arguments, set input and output files */
mplrs_commandline(argc, argv);
if (mplrs.rank == MASTER)
{
/* open input file for reading on master, histogram
* file for writing on master, if used.
*/
mplrs_initfiles();
master_sendfile();
/* may have produced warnings from stage 0 lrs, flush them. */
if (mplrs.output_list == NULL) /* need to prod consumer */
post_output("warning"," "); /* see waiting_initial */
mplrs.dummyout=0; /*avoid unitialized valgrind message*/
process_output(); /* before starting a flush */
clean_outgoing_buffers();
}
else
{
/* open output file for writing on consumer, open histogram file
* for writing on consumer, if used.
*/
if (mplrs.rank == CONSUMER)
mplrs_initfiles();
/* receive input file from master */
MPI_Recv(header, 9, MPI_INT, 0, 20, MPI_COMM_WORLD,
MPI_STATUS_IGNORE);
count = header[0];
mplrs.abortinit = header[3];
mplrs.fel = header[4];
mplrs.nohiddenl = header[7];
mplrs.input = malloc(sizeof(char)*(count+1));
if (!mplrs.abortinit) /* don't need if aborting, may be big */
MPI_Recv(mplrs.input, header[0], MPI_CHAR, 0, 20,
MPI_COMM_WORLD, MPI_STATUS_IGNORE);
mplrs.input[count] = '\0';
mplrs.redund = mplrs.fel; /* TODO: this is a hack; do better */
mplrs.renumber = header[5];
mplrs.testlin = header[6];
if (mplrs.rank == CONSUMER)
{
consumer.felfallback = header[8];
if (mplrs.abortinit)
consumer.final_print = 0;
}
/* workers don't need to look for non-existent linearities */
if (!mplrs.abortinit && mplrs.nohiddenl == 1 &&
mplrs.rank!=CONSUMER)
remove_option(mplrs.input, count, "testlin");
mprintf2(("%d: redund,fel,testlin,renumber,nohiddenl:%d,%d,%d,%d,%d\n",
mplrs.rank, mplrs.redund, mplrs.fel, mplrs.testlin,
mplrs.renumber, mplrs.nohiddenl));
mplrs.m = header[2];
/* get temporary filename needed for worker files */
mplrs_init_tfn(tim1);
if (mplrs.fel && mplrs.rank == CONSUMER)
{
consumer.m = header[2];
consumer.redineq = calloc(header[2]+1,
sizeof(long));
}
else if (header[1]>0 && mplrs.rank == CONSUMER) /* sigh ... */
{
consumer.redineq = calloc((header[2]+1),
sizeof(long));
mplrs.redund = 1;
}
else if (header[1]>0)
mplrs.redund = 1;
/* we will need a secound parallel round */
/* not used on consumer */
if (mplrs.redund && mplrs.testlin && !mplrs.nohiddenl &&
mplrs.rank!=CONSUMER)
{
mplrs.minrep_nonfinal = 1;
mplrs.redineq = calloc(header[2]+1, sizeof(long));
}
}
/* setup signals --
* TERM checkpoint to checkpoint file, or output file if none & exit
* HUP ditto
*/
signal(SIGTERM, mplrs_caughtsig);
signal(SIGHUP, mplrs_caughtsig);
free(wred);
}
/* if we catch a signal, set a flag to exit */
void mplrs_caughtsig(int sig)
{
if (mplrs.caughtsig<2)
mplrs.caughtsig = 1;
signal(sig, mplrs_caughtsig);
return;
}
/* if we've caught a signal, tell the master about it */
void mplrs_handlesigs(void)
{
float junk = 0; /* 0: caught a signal */
if (mplrs.caughtsig != 1)
return;
/* we want to stop, so no need to hurry and queue the send */
MPI_Send(&junk, 1, MPI_FLOAT, MASTER, 9, MPI_COMM_WORLD);
mplrs.caughtsig = 2; /* handled */
return;
}
/* signal the master that we want to stop,
* can try to checkpoint. Use mplrs to give
* output if desired.
* Does not return to caller ... ugly
*/
/* not used and untested as of 3/16/2023 */
void mplrs_cleanstop(int checkpoint)
{
MPI_Request req = MPI_REQUEST_NULL;
char *cobasis = NULL;
float junk;
int header[8]={0};
int flag = 0, len;
unsigned long ncob = 0;
mprintf(("%d: in cleanstop\n", mplrs.rank));
if (checkpoint)
junk = 1;
else
junk = -1;
/* inform master, wait for reply */
MPI_Send(&junk, 1, MPI_FLOAT, MASTER, 9, MPI_COMM_WORLD);
MPI_Recv(&junk, 1, MPI_FLOAT,MASTER,9,MPI_COMM_WORLD,MPI_STATUS_IGNORE);
/* needs to do usual cleanup / exit of a worker. callstack is
* unpredictable though, so exit() and don't return.
*/
/* leaks: starting_cobasis (mplrs_worker)
*/
/* do_work cleanup*/
remove(mplrs.tfn); /* delete temporary file */
/* mplrs_worker cleanup */
mplrs.outputblock = 0;
process_output();
process_curwarn();
return_unfinished_cobases();
clean_outgoing_buffers();
/* mplrs_worker start of loop ... */
MPI_Isend(&ncob, 1, MPI_UNSIGNED, MASTER, 6, MPI_COMM_WORLD, &req);
while (1)
{
MPI_Test(&req, &flag, MPI_STATUS_IGNORE);
if (flag)
{
mprintf3(("%d: clean_stop incoming message from 0\n",
mplrs.rank));
MPI_Recv(header, 8, MPI_INT, MASTER, MPI_ANY_TAG,
MPI_COMM_WORLD, MPI_STATUS_IGNORE);
if (header[0] == -1) /* no more work to do */
{
mplrs_worker_finished();
exit(0);
}
else /* forget this cobasis */
{
len = header[0];
cobasis = malloc(sizeof(char)*(len+1));
MPI_Recv(cobasis, len, MPI_CHAR, MASTER,
MPI_ANY_TAG, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
cobasis[len] = '\0';
mprintf3(("%d: got %s, throwing away\n",
mplrs.rank, cobasis));
free(cobasis);
return_unfinished_cobases();
clean_outgoing_buffers();
MPI_Isend(&ncob, 1, MPI_UNSIGNED, MASTER, 6,
MPI_COMM_WORLD, &req);
}
}
clean_outgoing_buffers();
}
/* mplrs_worker */
}
/* terminate immediately and ungracefully after printing the string */
void mplrs_emergencystop(const char *msg)
{
int ret;
fprintf(stderr, "%s",msg);
ret = MPI_Abort(MPI_COMM_WORLD, 1);
exit(ret);
}
/* we're in a redund run, make blocks of <= master.rows
* from redineq and add to L
* if master.rows = 0, split evenly for testing
*/
void add_redund_jobs(long rcount, long m)
{
long *nums;
long nlinearities = mplrs.R->count[6]; /* secret hidden bit */
int i=1,j,k,h;
long c = 0;
unsigned long extra = 0;
unsigned int njobs;
unsigned int cutoff = 0; /* number of jobs before lastp, lastrows */
int ex = 0;
if (rcount == 0)
{
/* can happen in round 2 in minrep mode - we may find hidden
* linearities but no weakly redundant lines.
* could happen with redund 0 0 on empty H-reps, but lrs rejects
* them in stage 0
*/
mprintf(("M: in add_redund_jobs but rcount==0, no jobs to make\n"));
return;
}
/* option -j overrides -rows -lastrows -lastp */
if (master.j > 0)
{
master.rows = master.lastrows = rcount / (master.j * (mplrs.size - 2));
extra = rcount % (master.j * (mplrs.size - 2));
mprintf(("M: -j option, setting rows to %u, splitting %lu extra rows evenly to get %u jobs per worker\n", master.rows, extra, master.j));
}
if (master.rows > rcount/(mplrs.size-2))
master.rows = 0;
if (master.rows==0)
{
if (m>mplrs.size-2)
{
master.rows = rcount / (mplrs.size-2);
extra = rcount%(mplrs.size-2);
if (master.rows == 0) /* prevent divide by 0 below,
* min job size is 1 row */
master.rows = 1; /* note rcount is not 0 */
}
else
master.rows = 1;
}
if (master.lastrows == 0 || master.lastrows > master.rows ||
master.lastp<=0 || master.lastp>100) /* disable bad values */
master.lastrows = master.rows;
njobs = (rcount / master.rows) + (rcount%master.rows>0 ? 1 : 0);
if (extra>0) /* if we're splitting evenly and doesn't divide evenly,*/
{ /* put ex (1) many extra rows in the first extra rows */
ex = 1;
njobs--;
}
cutoff = (float)(100-master.lastp)/100.0 * njobs;
while (c<rcount)
{
if (master.lastrows!=master.rows && master.size_L+1>cutoff)
{
mprintf3(("M: after %lu jobs switching to lastrows (%u %u %u)\n", master.size_L, njobs, master.lastp, master.lastrows));
master.rows = master.lastrows;
}
nums = malloc(sizeof(long)*(master.rows+ex+nlinearities));
for (j=0; j<(master.rows+ex) && c<rcount;)
{
while (i<=m)
{
if (mplrs.R->redineq[i++]==1)
{
nums[j++] = i-1;
c++;
break;
}
}
}
for (k=0; k<nlinearities;)
for (h=0; h<=m; h++)
if (mplrs.R->redineq[h] == 2)
nums[j + (k++)] = -1 * h;
if (extra>0)
extra--;
if (extra==0)
ex=0;
mprintf3(("M: adding redund job (%u rows) to L:", j));
for (k=0; k<j+nlinearities; k++)
mprintf3((" %ld", nums[k]));
mprintf3(("\n"));
master.L = addlist_tail_L(new_job(1, NULL, nums,
j+nlinearities,-1));
master.tot_L++; master.size_L++;
}
}
/* send the contents of the input file to all workers */
void master_sendfile(void)
{
char *buf;
char *buf_testlin=NULL; /* for adding testlin option */
char *orig=NULL; /* to send consumer in fel mode with hidden linearity*/
int count=0;
int extra=0;
int header[9]={0};
/* header: input file size, redund, R->m,
abortinit, fel, renumber, testlin, nohiddenl,
felfallback */
/* felfallback normally 0, 1 if actually a fel run but found hidden
* linearities, so fell back to minrep. used by consumer to print
* informative message at end of run.
*/
int c, i;
long rcount = 0, *wred = NULL;
long m;
char *argv[] = {argv0, mplrs.tfn};
time_t curt = time(NULL);
char *tim, *tim1;
int know_hiddenl = 0; /* now, fel falls through to minrep if hidden
*linearities exist - don't check for them again*/
/* make timestamp for filenames */
tim = ctime(&curt);
tim1 = tim+4;
tim1[3] = tim1[6] = '_';
tim1[15]='\0';
mplrs_init_tfn(tim1); /* for pure redund we need to add an option */
c = fgetc(master.input); /*avoid doing it in the original input file..*/
while (c!=EOF)
{
count++;
c = fgetc(master.input);
}
if (mplrs.countonly)
extra = 10; /* \ncountonly */
buf = malloc(sizeof(char)*(count+extra+1));
fseek(master.input, 0, SEEK_SET);
for (i=0; i<count; i++)
{
c = fgetc(master.input);
buf[i]=c;
}
if (mplrs.countonly)
sprintf(buf+i, "\ncountonly");
mplrs.input=buf;
if (master.fminrep || master.fredund)
{ /* README 11.23.2023 says -minrep and -redund over-ride any
* project/eliminate option in input file, so remove them here
*/
/* but 2.6.2024 says the consumer should get the original input */
orig = malloc(sizeof(char)*(count+extra+1));
memcpy(orig, mplrs.input, count+extra+1);
// remove_option(mplrs.input, count, "eliminate");
// remove_option(mplrs.input, count, "project");
}
/* get m from lrs */
mplrs_worker_init(); /* we're the master but okay */
master.m_messages = 0;/* no more messages: fel mode has multiple calls*/
if (mplrs.overflow != 3 && mplrs.abortinit == 0)
m = mplrs.P->m_A;
else /* overflow in mplrs_worker_init and non-hybrid, can't start run */
m = 0;
header[2] = m;
header[7] = -1; /* not checked */
if (mplrs.overflow!=3 && mplrs.R->testlin && mplrs.abortinit == 0)
master.testlin = 1; /* can be with/without redund see mplrs.h */
if (master.minrep)
{
master.testlin = 1;
master.redund = 1;
}
if (mplrs.overflow!=3 && mplrs.R->fel && mplrs.abortinit == 0)
{
mprintf(("M: in fel mode, stage 0 OK, checking for hidden linearities\n"));
mplrs.lrs_main(0,NULL,&mplrs.P,&mplrs.Q,0,2,NULL,mplrs.R);
mplrs.specialmode = 2; /* force fel on, testlin on in phase 0 */
mplrs_worker_init();
run_lrs(2, argv, 0, 1, NULL, NULL, NULL);
mplrs.specialmode = 0; /* back to normal mode */
if (mplrs.overflow == 3)
mplrs.abortinit = 1;
mplrs.nohiddenl = mplrs.R->redundphase;
master.fel = 1;
if (!mplrs.abortinit && !mplrs.nohiddenl)
{
header[7] = mplrs.nohiddenl;
mprintf(("M: found hidden linearities, will re-init in minrep run\n"));
master.fminrep = master.minrep = 1;
know_hiddenl = 1;
if (orig==NULL)
orig = malloc(sizeof(char)*(count+extra+1));
memcpy(orig, mplrs.input, count+extra+1);
// remove_option(mplrs.input, count, "eliminate");
// remove_option(mplrs.input, count, "project");
mplrs.lrs_main(0,NULL,&mplrs.P,&mplrs.Q,0,2,NULL,mplrs.R);
mplrs_worker_init();
if (mplrs.overflow!=3 && mplrs.abortinit==0)
m = mplrs.P->m_A;
else
m = 0;
header[2] = m;
header[7] = mplrs.nohiddenl;
header[8] = 1;
master.testlin = 1;
master.redund = 1;
master.fel = 0;
}
else if (!mplrs.abortinit)
{
/* clean up and restart after hidden linearity check,
* m changes in fel mode
*/
mplrs.lrs_main(0,NULL,&mplrs.P,&mplrs.Q,0,2,NULL,mplrs.R);
mplrs.specialmode = 3; /* force fel on, testlin off */
mplrs_worker_init();
mprintf(("M: in fel mode, stage 0.5 OK, getting new m..."));
/* run_lrs stage 1 to get new m_A? */
run_lrs(2, argv, 0, 1, NULL, NULL, NULL);
mplrs.specialmode = 0; /* back to normal */
if (mplrs.overflow==3)
mplrs.abortinit = 1;/* if mplrs1 overflows here */
master.fel = 1;
m = mplrs.R->m; /* fel gives a new, bigger m_A */
mprintf(("done (%ld)\n",m));
header[2] = m; /* tell consumer to allocate this size */
for (i=1; i<=m; i++)
if (mplrs.R->redineq[i] == 1)
rcount++;
}
}
if (mplrs.overflow!=3 && mplrs.R->redund && mplrs.abortinit == 0)
{
master.redund = 1;
for (i=1; i<=m; i++)
if (mplrs.R->redineq[i] == 1)
rcount++;
if (master.testlin)
{
master.minrep = 1;
mprintf(("M: in minrep mode\n"));
}
else
mprintf(("M: in redund mode\n"));
if (!know_hiddenl) /* fel mode already checked - don't repeat */
{
/* check if there are hidden linearities */
if (master.minrep || !mplrs.R->testlin) /* need to add it to input and re-init */
{
buf_testlin = dupstr(buf);
/* don't add testlin now: add_testlin_option(buf, count+extra);*/
mplrs.lrs_main(0,NULL,&mplrs.P,&mplrs.Q,0,2,NULL,mplrs.R);
mplrs.input = buf_testlin;
mplrs_worker_init();
}
mplrs.R->redund = 0;
mplrs.R->testlin = 1;
mprintf(("M: checking if hidden linearities exist\n"));
mplrs.specialmode = 1; /* force mplrs.R->redund=0 on overflows*/
run_lrs(2, argv, 0, 1, NULL, NULL, NULL);
mplrs.R->testlin = master.testlin;
mplrs.specialmode = 0;
if (mplrs.overflow==3) /* can overflow fatally here */
mplrs.abortinit = 1;
else
mplrs.nohiddenl = mplrs.R->redundphase;
header[7] = mplrs.nohiddenl;
if (master.minrep || !mplrs.R->testlin)
{
free(buf_testlin);
mplrs.input = buf;
mplrs.lrs_main(0,NULL,&mplrs.P,&mplrs.Q,0,2,NULL,mplrs.R);
mplrs_worker_init();
}
}
if (mplrs.nohiddenl)
{
master.minrep_round = 2;
mprintf(("M: no hidden linearities exist, doing fast redund/minrep\n"));
}
else
mprintf(("M: hidden linearities exist, doing two-round redund/minrep\n"));
}
header[3] = mplrs.abortinit; /* tell workers to abort if bad input */
header[5] = mplrs.R->printcobasis;
mplrs.renumber = header[5];
header[0] = count+extra;
if (master.redund || master.fel) /* add redund jobs to L */
add_redund_jobs(rcount,m);
header[4] = master.fel;
header[6] = master.testlin;
if (master.redund && master.testlin)
{
master.redineq = calloc(m+1, sizeof(long));
master.merged_redineq = calloc(mplrs.size, sizeof(long));
}
for (i=0; i<mplrs.size; i++)
{
header[1] = 0;
if (i==MASTER)
continue;
else if (master.redund)
{
header[1] = 1;
}
mprintf2(("M: Sending input file to %d\n", i));
MPI_Send(header, 9, MPI_INT, i, 20, MPI_COMM_WORLD);
/* in fel mode, if found hidden linearities, we do minrep.
* but send the consumer the original file, so it gets the
* final print done with project/eliminate included (2/5/24).
*/
if (!mplrs.abortinit && i==CONSUMER && orig!=NULL)
MPI_Send(orig, count+extra, MPI_CHAR, i, 20,
MPI_COMM_WORLD);
else if (!mplrs.abortinit) /* don't if aborting, may be big */
MPI_Send(buf, count+extra, MPI_CHAR, i, 20,
MPI_COMM_WORLD);
}
/* fseek(master.input, 0, SEEK_SET); */
fclose(master.input);
free(wred);
free(buf);
free(orig);
}
job *new_job(int type, char *cob, long *nums, unsigned int nnums, long depth)
{
job *ret = malloc(sizeof(job));
ret->type = type;
ret->cob = cob;
ret->depth = depth;
ret->nums = nums;
ret->nnums = nnums;
return ret;
}
/* initialize default values of global structures, before commandline */
void mplrs_initstrucs(void)
{
mplrs.cobasis_list = NULL;
mplrs.lrs_main = mplrs_init_lrs_main;
mplrs.P = NULL;
mplrs.Q = NULL;
mplrs.R = NULL;
mplrs.caughtsig = 0;
mplrs.abortinit = 0;
mplrs.overflow = 0;
mplrs.renumber = 0;
mplrs.rays = 0;
mplrs.vertices = 0;
mplrs.bases = 0;
mplrs.facets = 0;
mplrs.linearities = 0;
mplrs.intvertices = 0;
mplrs.deepest = 0;
mplrs.deepest_vertex = 0;
mplrs.my_tag = 100;
mplrs.tfn_prefix = DEF_TEMP;
mplrs.tfn = NULL;
mplrs.tfile = NULL;
mplrs.input_filename = DEF_INPUT;
mplrs.input = NULL;
mplrs.output_list = NULL;
mplrs.ol_tail = NULL;
mplrs.outnum = 0;
mplrs.finalwarn = malloc(sizeof(char)*32);
mplrs.finalwarn_len = 32;
mplrs.curwarn = malloc(sizeof(char)*32);
mplrs.curwarn_len = 32;
mplrs.finalwarn[0] = '\0';
mplrs.curwarn[0] = '\0';
mplrs.maxbuf = DEF_MAXBUF; /* maximum # lines to buffer */
mplrs.outputblock = 0; /* don't block initial output */
mplrs.redund = 0;
mplrs.testlin = 0;
mplrs.fel = 0;
mplrs.minrep_nonfinal = 0;
mplrs.nohiddenl = -1;
mplrs.redineq = NULL;
mplrs.countonly = 0;
mplrs.minheight = 0;
mplrs.m = 0;
master.L = NULL;
master.tail_L = NULL;
master.size_L = 0;
master.tot_L = 0;
master.num_empty = 0;
master.num_producers = 0;
master.checkpointing = 0;
master.cleanstop = 0;
master.messages = 1;
master.lmin = DEF_LMIN;
master.lmax = DEF_LMAX;
master.orig_lmax = 1;
master.scalec = DEF_SCALEC;
master.initdepth = DEF_ID;
master.maxdepth = DEF_MAXD;
master.maxcobases = DEF_MAXC;
master.rows = DEF_ROWS;
master.lastp = DEF_LASTP;
master.lastrows = DEF_LASTROWS;
master.j = DEF_J;
master.maxncob = DEF_MAXNCOB;
master.queue = 0;
master.lponly = 0;
master.fredund = master.redund = 0;
master.testlin = 0;
master.fminrep = master.minrep = 0;
master.fel = 0;
master.cfel = 0;
master.m_messages = 1;
master.redineq = NULL;
master.merged_redineq = NULL;
master.n_merged = 0;
master.n_minprep = 0;
master.minrep_round = 0;
master.time_limit = 0;
master.hist_filename = DEF_HIST;
master.hist = NULL;
master.doing_histogram = 0;
master.freq_filename = DEF_FREQ;
master.freq = NULL;
master.flipstart = 0;
master.restart_filename = DEF_RESTART;
master.restart = NULL;
master.checkp_filename = DEF_CHECKP;
master.checkp = NULL;
master.stop_filename = DEF_STOP;
master.stop = NULL;
master.input = NULL;
consumer.prodreq = NULL;
consumer.prodibf = NULL;
consumer.incoming = NULL;
consumer.output_filename = DEF_OUTPUT;
consumer.output = stdout;
consumer.oflow_flag = 0;
consumer.num_producers = 0;
consumer.waiting_initial = 2; /* 2: waiting initial and master warnings */
consumer.rjobcount = 3; /* start at 3 */
consumer.final_print = 1;
consumer.final_redundcheck = 0;
consumer.m = -1;
consumer.rays = 0;
consumer.vertices = 0;
consumer.bases = 0;
consumer.felfallback = 0;
}
/* process commandline arguments */
void mplrs_commandline(int argc, char **argv)
{
int i, arg, firstfile=1;
for (i=1; i<argc; i++)
{
if (!strcmp(argv[i], "-lmin"))
{
arg = atoi(argv[i+1]);
i++;
if (arg < 1 )
bad_args();
master.lmin = arg;
if (master.lmin > master.lmax)
master.lmax = arg;
continue;
}
else if (!strcmp(argv[i], "-lmax"))
{
arg = atoi(argv[i+1]);
i++;
if (arg<1 )
bad_args();
master.lmax = arg;
master.orig_lmax = 0;
if (master.lmin > master.lmax)
master.lmin = arg;
continue;
}
else if (!strcmp(argv[i], "-scale"))
{
arg = atoi(argv[i+1]);
i++;
if (arg<1)
bad_args();
master.scalec = arg;
continue;
}
else if (!strcmp(argv[i], "-hist"))
{
master.hist_filename = argv[i+1];
i++;
continue;
}
else if (!strcmp(argv[i], "-freq"))
{
master.freq_filename = argv[i+1];
i++;
continue;
}
else if (!strcmp(argv[i], "-stopafter"))
{
arg = atoi(argv[i+1]);
i++;
if (arg<1)
bad_args();
master.maxncob = arg;
continue;
}
else if (!strcmp(argv[i], "-countonly"))
{
mplrs.countonly = 1;
continue;
}
else if (!strcmp(argv[i], "-maxbuf"))
{
arg = atoi(argv[i+1]);
i++;
if (arg<1)
bad_args();
mplrs.maxbuf = arg;
continue;
}
else if (!strcmp(argv[i], "-id"))
{
arg = atoi(argv[i+1]);
i++;
if (arg<0)
bad_args();
master.initdepth = arg;
continue;
}
else if (!strcmp(argv[i], "-maxd"))
{
arg = atoi(argv[i+1]);
i++;
if (arg<1)
bad_args();
master.maxdepth = arg;
continue;
}
else if (!strcmp(argv[i], "-maxc"))
{
arg = atoi(argv[i+1]);
i++;
if (arg<0)
bad_args();
master.maxcobases = arg;
continue;
}
else if (!strcmp(argv[i], "-rows"))
{
arg = atoi(argv[i+1]);
i++;
if (arg<0)
bad_args();
master.rows = arg;
continue;
}
else if (!strcmp(argv[i], "-lastp"))
{
arg = atoi(argv[i+1]);
i++;
if (arg<0)
bad_args();
master.lastp = arg;
continue;
}
else if (!strcmp(argv[i], "-lastrows"))
{
arg = atoi(argv[i+1]);
i++;
if (arg<0)
bad_args();
master.lastrows = arg;
continue;
}
else if (!strcmp(argv[i], "-j"))
{
arg = atoi(argv[i+1]);
i++;
if (arg<0)
bad_args();
master.j = arg;
continue;
}
else if (!strcmp(argv[i], "-checkp"))
{
master.checkp_filename = argv[i+1];
i++;
continue;
}
else if (!strcmp(argv[i], "-lponly"))
{
master.lponly = 1;
continue;
}
else if (!strcmp(argv[i], "-minrep"))
{
master.fminrep = master.minrep = 1;
continue;
}
else if (!strcmp(argv[i], "-redund"))
{
if (mplrs.rank==CONSUMER)
printf("*warning: mplrs -redund mode has been removed, consider using -minrep instead\n");
bad_args(); /* die on -redund now */
continue;
}
else if (!strcmp(argv[i], "-fel"))
{
master.cfel = 1;
continue;
}
else if (!strcmp(argv[i], "-stop"))
{
master.stop_filename = argv[i+1];
i++;
continue;
}
else if (!strcmp(argv[i], "-time"))
{
arg = atoi(argv[i+1]);
i++;
if (arg<1)
bad_args();
master.time_limit = arg;
continue;
}
else if (!strcmp(argv[i], "-restart"))
{
master.restart_filename = argv[i+1];
i++;
continue;
}
else if (!strcmp(argv[i], "-flipstart"))
{
master.restart_filename = argv[i+1];
i++;
master.flipstart = 1;
continue;
}
else if (!strcmp(argv[i], "-queue"))
{
if (!mplrs.minheight)
master.queue = 1;
continue;
}
else if (!strcmp(argv[i], "-minheight"))
{
if (!master.queue)
mplrs.minheight = 1;
continue;
}
else if (!strcmp(argv[i], "-temp"))
{
mplrs.tfn_prefix = argv[i+1];
i++;
continue;
}
else if (firstfile == 1)
{
mplrs.input_filename = argv[i];
firstfile = 2;
}
else if (firstfile == 2)
{
consumer.output_filename = argv[i];
firstfile = 3;
}
else
bad_args();
}
if (master.orig_lmax) /* default lmax behavior is lmax=lmin */
master.lmax = (master.lmin>0? master.lmin: 0);
if (mplrs.input_filename==NULL) /* need an input file */
bad_args();
if ((master.stop_filename!=NULL || master.time_limit!=0) &&
master.checkp_filename==NULL)
bad_args(); /* need checkpoint file if stop condition given */
}
/* open input file on master, histogram (if exists) on master,
* output (if exists) on consumer
*/
void mplrs_initfiles(void)
{
if (mplrs.rank == MASTER)
{
master.input = fopen(mplrs.input_filename, "r");
if (master.input == NULL)
{
printf("Unable to open %s for reading [%s].\n",
mplrs.input_filename, mplrs.host);
/* MPI_Finalize(); */
exit(0);
}
if (master.hist_filename != NULL)
{
master.hist = fopen(master.hist_filename, "w");
if (master.hist == NULL)
{
printf("Unable to open %s for writing [%s].\n",
master.hist_filename, mplrs.host);
/* MPI_Finalize(); */
exit(0);
}
master.doing_histogram = 1;
mprintf2(("M: Prepared histogram (%s)\n", master.hist_filename));
}
if (master.freq_filename != NULL)
{
master.freq = fopen(master.freq_filename, "w");
if (master.freq == NULL)
{
printf("Unable to open %s for writing [%s].\n",
master.freq_filename, mplrs.host);
exit(0);
}
mprintf2(("M: Prepared frequency file (%s)\n",
master.freq_filename));
}
if (master.checkp_filename !=NULL)
{
master.checkp = fopen(master.checkp_filename, "w");
if (master.checkp == NULL)
{
printf("Unable to open %s for writing [%s].\n",
master.checkp_filename, mplrs.host);
/* MPI_Finalize(); */
exit(0);
}
mprintf2(("M: Prepared checkpoint file (%s)\n",
master.checkp_filename));
}
if (master.restart_filename != NULL)
{
master.restart = fopen(master.restart_filename, "r");
if (master.restart == NULL)
{
printf("Unable to open %s for reading [%s].\n",
master.restart_filename, mplrs.host);
/* MPI_Finalize(); */
exit(0);
}
mprintf2(("M: Opened restart file (%s)\n",
master.restart_filename));
}
}
if (mplrs.rank == CONSUMER)
{
if (consumer.output_filename == NULL)
return;
consumer.output = fopen(consumer.output_filename, "w");
if (consumer.output == NULL)
{
printf("Unable to open %s for writing [%s].\n",
consumer.output_filename, mplrs.host);
/* MPI_Finalize(); */
exit(0);
}
}
}
/* free the lrs_mp allocated in mplrs_init */
void mplrs_freemps(void)
{
lrs_clear_mp(mplrs.tN); lrs_clear_mp(mplrs.tD);
lrs_clear_mp(mplrs.Vnum); lrs_clear_mp(mplrs.Vden);
lrs_clear_mp(mplrs.Tnum); lrs_clear_mp(mplrs.Tden);
}
/* Bad commandline arguments. Complain and die. */
void bad_args(void)
{
if (mplrs.rank == CONSUMER)
printf("Invalid arguments.\n%s\n", USAGE);
mplrs_freemps();
MPI_Finalize();
exit(0);
}
/* fallback -- meaningless if <3 processes
* better would be to fallback to normal lrs if <4 processes
*/
int mplrs_fallback(void)
{
if (mplrs.rank==0)
printf("mplrs requires at least 3 processes.\n");
mplrs_freemps();
MPI_Finalize();
exit(0);
return 0; /* unreachable */
}
/**********
* master *
**********/
int mplrs_master(void)
{
int i;
int loopiter = 0; /* do some things only sometimes */
MPI_Request ign = MPI_REQUEST_NULL; /* a request we'll ignore */
struct timeval cur, last; /* for histograms */
int flag = 0;;
int phase = 1; /* In phase1? */
int done = -1; /* need a negative int to send to finished workers */
int ncob=0; /* for printing sizes of sub-problems and for -stopafter*/
unsigned long tot_ncob = 0;
int want_stop = 0;
int check = 0; /* inform consumer whether checkpointing */
gettimeofday(&last, NULL);
master.num_producers = 0; /* nobody working right now */
master.act_producers = malloc(sizeof(unsigned int)*mplrs.size);
master.live_workers = mplrs.size - 2;
master.workin = malloc(sizeof(int)*mplrs.size);
master.mworkers = malloc(sizeof(MPI_Request)*mplrs.size);
master.incoming = NULL;
master.sigbuf = malloc(sizeof(float)*mplrs.size);
master.sigcheck = malloc(sizeof(MPI_Request)*mplrs.size);
if (master.restart!=NULL)
{
master_restart();
phase = 0;
}
for (i=0; i<mplrs.size; i++)
{
master.act_producers[i] = 0;
if (i==MASTER)
continue;
MPI_Irecv(master.sigbuf+i, 1, MPI_FLOAT, i, 9, MPI_COMM_WORLD,
master.sigcheck+i);
if (i==CONSUMER)
continue;
MPI_Irecv(master.workin+i, 1, MPI_UNSIGNED, i, 6, MPI_COMM_WORLD,
master.mworkers+i);
}
if (mplrs.abortinit)
{
want_stop = 1;
master_stop_consumer(0);
}
if (master.fel || master.redund)
phase = 0; /* no phase 1 in these runs */
while ((master.L!=NULL && !master.checkpointing && !want_stop) ||
master.num_producers>0 || master.live_workers>0)
{
loopiter++;
/* sometimes check if we should update histogram etc */
if (!(loopiter&0x1ff))
{
if (master.maxncob>0 && master.maxncob<=tot_ncob)
want_stop = 1;
if (master.doing_histogram)
print_histogram(&cur, &last);
}
/* sometimes check if we should checkpoint */
/* don't check in phase 1 to ensure the consumer sees a 'begin' and exits */
if (!(loopiter&0x7ff) && phase!=1)
// && !master.checkpointing && !master.cleanstop)
{
if (master.stop_filename!=NULL || master.time_limit!=0)
check_stop();
master_checksigs();
if (master.cleanstop)
want_stop = 1;
}
#if 0
/* for debugging minheight */
fprintf(stderr, "M: L is sorted: %d\n", L_sorted());
#endif
recv_producer_lists();
/* check if anyone wants work */
for (i=0; i<mplrs.size; i++)
{ /* consumer and master don't want work */
if (i==CONSUMER || i==MASTER)
continue;
/* workers that have exited can't work */
if (master.mworkers[i]==MPI_REQUEST_NULL)
continue;
if (master.num_producers>0 && master.L==NULL)
{
break; /* no work to give now, but some may
* appear later
*/
}
if (phase==1 && i!=INITIAL && master.cleanstop==0)
continue; /* INITIAL gets the first bit */
/* need cleanstop condition for
* overflows in mplrs_worker_init()
*/
MPI_Test(master.mworkers+i, &flag, MPI_STATUS_IGNORE);
if (!flag)
continue; /* i is not ready for more work */
ncob = master.workin[i];
tot_ncob+=ncob;
mprintf2(("M: %d looking for work\n", i));
if ((master.L!=NULL || phase==1) &&
!master.checkpointing && !want_stop
&& !(master.minrep_round==1 &&
master.merged_redineq[i]!=2))
/* don't send round 2 minrep jobs before telling worker
* the new linearities */
{ /* and not checkpointing! */
send_work(i,phase,0);
MPI_Irecv(master.workin+i, 1, MPI_UNSIGNED,i, 6,
MPI_COMM_WORLD, master.mworkers+i);
phase=0;
if (master.freq!=NULL && ncob>0)
fprintf(master.freq, "%d\n", ncob);
continue;
}
else if (master.redund && master.testlin &&
master.minrep_round==0 &&
master.merged_redineq[i]==0)
{
master_minrep_mergeredineq(i);
MPI_Irecv(master.workin+i, 1, MPI_UNSIGNED,i, 6,
MPI_COMM_WORLD, master.mworkers+i);
continue;
}
else if (master.redund && master.testlin &&
master.minrep_round==1 &&
master.merged_redineq[i]!=2)
{
master_minrep_sendredineq(i);
MPI_Irecv(master.workin+i, 1, MPI_UNSIGNED,i, 6,
MPI_COMM_WORLD, master.mworkers+i);
continue;
}
/* else tell worker we've finished */
mprintf(("M: Saying goodbye to %d, %d left\n", i,
master.live_workers-1));
MPI_Isend(&done, 1, MPI_INT, i, 8, MPI_COMM_WORLD,&ign);
MPI_Request_free(&ign);
master.live_workers--;
if (master.freq!=NULL && ncob>0)
fprintf(master.freq, "%d\n", ncob);
}
clean_outgoing_buffers();
}
/* don't checkpoint if we actually finished the run */
if (master.checkpointing && master.L==NULL)
master.checkpointing = 0;
if (master.checkpointing) /* checkpointing */
check = CHECKFLAG;
/* inform consumer if checkpointing or not */
mprintf2(("M: Telling consumer whether or not checkpointing (%d)\n", check));
MPI_Send(&check, 1, MPI_INT, CONSUMER, 1, MPI_COMM_WORLD);
if (master.checkpointing)
master_checkpoint();
send_master_stats();
mplrs_freemps();
if (master.freq != NULL)
fclose(master.freq);
if (master.hist != NULL)
fclose(master.hist);
if (master.checkp != NULL)
fclose(master.checkp);
free(mplrs.finalwarn);
free(mplrs.curwarn);
if (!mplrs.abortinit)
mplrs.lrs_main(0,NULL,&mplrs.P,&mplrs.Q,0,2,NULL,mplrs.R);
free(mplrs.R->redineq);
free(mplrs.R->facet);
free(mplrs.R);
MPI_Finalize();
free(master.workin);
free(master.mworkers);
free(master.act_producers);
free(master.sigbuf);
free(master.sigcheck);
return 0;
}
/* prepare to receive remaining cobases from target.
* Since we don't yet know the size of buffers needed, we
* only Irecv the header and will Irecv the actual cobases later
*/
void master_add_incoming(int target)
{
msgbuf *msg = malloc(sizeof(msgbuf));
msg->req = malloc(sizeof(MPI_Request)*4);
msg->buf = malloc(sizeof(void *)*4);
msg->buf[0] = malloc(sizeof(int) * 3); /* (strlen,lengths,tag) */
msg->buf[1] = NULL; /* sizes not known yet */
msg->buf[2] = NULL;
msg->buf[3] = NULL;
msg->count = 4;
msg->target = target;
msg->queue = 1;
msg->tags = NULL;
msg->sizes = NULL;
msg->types = NULL;
msg->current_count = NULL;
msg->next = master.incoming;
master.incoming = msg;
MPI_Irecv(msg->buf[0], 3, MPI_INT, target, 10, MPI_COMM_WORLD,msg->req); return;
}
/* check our list of incoming messages from producers about cobases
* to add to L. Add any from messages that have completed.
* If the header has completed (msg->queue==1 and header completed),
* add the remaining MPI_Irecv's.
* Update num_producers to keep track of how many messages the master
* is owed (workers are not allowed to exit until num_producers==0 and
* L is empty).
* Also update size_L
*/
void recv_producer_lists(void)
{
msgbuf *msg, *prev=NULL, *next;
int *header;
int flag;
for (msg = master.incoming; msg; msg=next)
{
next = msg->next;
MPI_Test(msg->req, &flag, MPI_STATUS_IGNORE);
if (!flag) /* header has not completed yet */
{
prev = msg;
continue;
}
header = (int *)msg->buf[0];
if (msg->queue) /* header completed, and need to Irecv now */
{
if (header[0]==-1) /* producer returns NOTHING */
{
master.num_producers--;
master.act_producers[msg->target]--;
free_msgbuf(msg);
if (prev)
prev->next = next;
else
master.incoming = next;
continue;
}
msg->buf[1]= malloc(sizeof(char)*header[1]);
msg->buf[2]= malloc(sizeof(int)*header[0]);
msg->buf[3]= malloc(sizeof(long)*header[0]);
MPI_Irecv(msg->buf[1], header[1], MPI_CHAR, msg->target,
header[2], MPI_COMM_WORLD, msg->req+1);
MPI_Irecv(msg->buf[2], header[0], MPI_INT, msg->target,
header[2], MPI_COMM_WORLD, msg->req+2);
MPI_Irecv(msg->buf[3], header[0], MPI_LONG, msg->target,
header[2], MPI_COMM_WORLD, msg->req+3);
msg->queue=0;
prev = msg;
continue;
}
/* header completed, did the rest? */
MPI_Testall(3, msg->req+1, &flag, MPI_STATUSES_IGNORE);
if (!flag) /* not yet */
{
prev = msg;
continue;
}
mprintf2(("M: %d returned non-empty producer list (%d, %d)\n",
msg->target, header[0], header[1]));
process_returned_cobases(msg);
mprintf2(("M: Now have size_L=%lu\n",master.size_L));
if (prev)
prev->next = next;
else
master.incoming = next;
master.num_producers--;
master.act_producers[msg->target]--;
free_msgbuf(msg);
}
return;
}
/* msg is a completed, non-empty buffer containing cobases to add to
* L. Process it, add them to L, and update size_L
* Basically the inverse of return_unfinished_cobases()
*/
void process_returned_cobases(msgbuf *msg)
{
int *header = (int *)msg->buf[0];
char *str = (char *)msg->buf[1];
int *lengths = (int *)msg->buf[2];
long *depths = msg->buf[3];
job *njob;
slist *mjob=master.L, *pjob=NULL, *tjob;
int i;
char *cob;
for (i=0; i<header[0]; i++)
{
cob = malloc(sizeof(char)*(lengths[i]+1));
strncpy(cob, str, lengths[i]);
cob[lengths[i]] = '\0';
str+=lengths[i];
mprintf2(("M: Adding to L: %s\n",cob));
njob = new_job(0, cob, NULL, 0, depths[i]);
if (master.queue == 0 && mplrs.minheight == 0)
master.L = addlist(master.L, njob);
else if (master.queue == 1)
master.L = addlist_tail_L(njob);
else /* minheight, basically merge L and the returned msg */
{
while (mjob && ((job*)mjob->data)->depth<depths[i])
{
pjob = mjob;
mjob = mjob->next;
}
tjob = addlist(mjob, njob);
if (pjob!=NULL)
{
pjob->next = tjob;
pjob = tjob;
}
else
{
mprintf2(("M: adding at head!\n"));
master.L = pjob = tjob;
}
}
}
master.size_L += header[0];
master.tot_L += header[0];
return;
}
/* tell target to take our redineq, merge new linearities */
/* not called until round 1 finishes, so no more incoming linearities - safe to
* Isend master.redineq (it doesn't change)
*/
void master_minrep_sendredineq(int target)
{
msgbuf *msg = calloc(1, sizeof(msgbuf));
long m = mplrs.R->m;
msg->target = target;
msg->count = 1;
msg->req = malloc(sizeof(MPI_Request));
msg->buf = malloc(sizeof(void *));
msg->buf[0] = NULL;
send_work(target, 0, 2);
/*MPI_Send(master.redineq, m+1, MPI_LONG, target, 6, MPI_COMM_WORLD);*/
MPI_Isend(master.redineq, m+1, MPI_LONG, target, 6, MPI_COMM_WORLD,
msg->req);
master.merged_redineq[target] = 2;
master.n_minprep++;
msg->next = mplrs.outgoing;
mplrs.outgoing = msg;
return;
}
/* tell target to send us their redineq, get it, merge it and
* update master.{merged_redineq[target],n_merged,minrep_round}
*/
void master_minrep_mergeredineq(int target)
{
long i, m = mplrs.R->m, rcount=0;
long *tmp = malloc(sizeof(long)*(m+1));
long n_oldlin, n_newlin;
send_work(target, 0, 1);
mprintf(("M: minrep mode(m=%ld); merging redineq from %d\n", m, target));
MPI_Recv(tmp, m+1, MPI_LONG, target, 6, MPI_COMM_WORLD,
MPI_STATUS_IGNORE);
master.merged_redineq[target] = 1;
master.n_merged++;
mprintf2(("M: merging values "));
for (i=1; i<=m; i++)
{
mprintf2((" %ld<-%ld", master.redineq[i], tmp[i]));
if (master.redineq[i] == 2 || tmp[i] == 0 ||
master.redineq[i] == tmp[i])
continue;
else if (master.redineq[i] == 0)
master.redineq[i] = tmp[i];
else if (tmp[i] == 2)
master.redineq[i] = tmp[i]; /* linearity wins */
else if (tmp[i] == -1)
master.redineq[i] = tmp[i]; /* strong overwrites weak */
/* otherwise not needed */
}
mprintf2(("\n"));
free(tmp);
if (master.n_merged == mplrs.size - 2)
{
master.minrep_round = 1;
mprintf(("M: minrep round 1 finished\n"));
n_oldlin = 0, n_newlin = 0;
for (i=1; i<=m; i++)
{
if (mplrs.R->redineq[i] == 2)
n_oldlin++;
if (master.redineq[i] == 2 || master.redineq[i]==-2)
n_newlin++;
}
mprintf(("M: minrep mode found %ld new linearities\n", n_newlin-n_oldlin));
if (n_oldlin > n_newlin)
{
printf("mplrs: number of linearities decreased!\n");
exit(1);
}
else if (n_oldlin == n_newlin)
{
if (!mplrs.nohiddenl)
printf("*warning: lrslib said hidden linearities but we found none!\n");
master.minrep_round = 2; /* all done */
return;
}
/* otherwise we're going to do another parallel redund run,
* checking the weakly redundant lines with the hidden
* linearities now explicit.
*
* let's hide the number of linearities in the same
* hidden place as lrs, to help add_redund_jobs later...
*/
if (mplrs.nohiddenl)
printf("warning: lrs said no hidden linearities but we found some - likely to break\n");
mplrs.R->count[6] = n_newlin;
mprintf(("M: minrep mode, ready for final parallel run\n"));
mprintf2(("M: adding second round redund jobs on:"));
for (i=0; i<=m; i++)
{
mplrs.R->redineq[i] = master.redineq[i];
if (master.redineq[i] == 1)
{
rcount++;
mprintf2((" %ld", i));
}
}
mprintf2(("\n"));
add_redund_jobs(rcount, m);
}
return;
}
/* send one unit from L to target. if phase!=0, this is the first
* unit we're sending (i.e. phase 1). usually, phase==0.
* get_newlin=1: tell worker to send current redineq, doesn't send other work
* get_newlin=2: send master.redineq to the worker
* get_newlin=0: normal case as in previous versions
* Parameters are scaled and sent in the header.
*/
void send_work(int target, int phase, int get_newlin)
{
slist *cob;
msgbuf *msg = malloc(sizeof(msgbuf));
job *jsend;
int *header;
MPI_Datatype type = MPI_CHAR;
msg->req = malloc(sizeof(MPI_Request)*2);
msg->buf = malloc(sizeof(void *)*2);
/*{length of work, int maxdepth, int maxcobases, bool lponly,
bool messages, int type, 2x future use} */
/* type: 0 normal, 1 redund, 2 fel,
* 3 report current redineq (to get new linearities, minrep mode)
* 4 receive master's redineq and merge (new linearities)
*/
msg->buf[0] = calloc(8, sizeof(int)); /*warning removal - all init now*/
header = (int *)msg->buf[0];
header[4] = master.messages;
header[5] = 0;
master.messages = 0;
if (get_newlin==1) /* after first parallel round in minrep mode,
* we need to check for the new linearities,
* merging them all on the master. then we redo
* a parallel redund run on the weakly redundant
* lines
*/
{
header[4] = 0;
header[5] = 3; /* type */
msg->count = 1;
}
else if (get_newlin==2) /* after first parallel round in minrep mode,
* we got new linearities: need to send them
* around. to target for now.
*/
{
header[4] = 0;
header[5] = 4; /* type */
msg->count = 1;
}
else if (master.redund || master.fel)
{ /* should assert work unit type 1/2, or use that condition */
cob = master.L;
master.L = cob->next;
if (cob==master.tail_L)
master.tail_L = NULL;
jsend = (job*)cob->data;
header[0] = jsend->nnums;
setparams(header);
header[5] = (master.fel? 2 : 1);
type = MPI_LONG;
msg->buf[1] = jsend->nums;
msg->count = 2;
master.size_L--;
if (master.size_L == 0)
master.num_empty++;
mprintf(("M: Sending redund work to %d (%d)\n",
target, header[0]));
free(jsend); free(cob);
}
else if (phase==0) /* normal case */
{
cob = master.L;
if (cob == master.tail_L)
master.tail_L = NULL;
master.L = cob->next;
jsend = (job*)cob->data;
header[0] = strlen((char *)jsend->cob);
msg->buf[1] = jsend->cob;
setparams(header); /* scale if needed */
master.size_L--;
if (master.size_L == 0)
master.num_empty++;
mprintf(("M: Sending work to %d (%d,%d,%d) %s\n",
target, header[0], header[1], header[2],
(char*)msg->buf[1]));
msg->count = 2;
free(jsend); free(cob);
}
else /* phase 1 */
{
header[0] = 0; /* header[0]==0 means initial phase 1 */
header[1] = master.initdepth;
header[2] = master.maxcobases;
if (master.redund) /* redund turns off budget */
header[1] = header[2] = 0;
mprintf(("M: Sending phase 1 to %d (%d,%d)\n", target,
header[1], header[2]));
msg->buf[1] = NULL;
msg->count = 1;
}
msg->target = target;
msg->queue = 0;
msg->tags = NULL;
msg->sizes = NULL;
msg->types = NULL;
msg->current_count = NULL;
/* ready to send */
MPI_Isend(header, 8, MPI_INT, target, 1, MPI_COMM_WORLD, msg->req);
if ( (phase==0 || master.redund || master.fel) && !get_newlin)
MPI_Isend(msg->buf[1], header[0], type, target, 1,
MPI_COMM_WORLD, msg->req+1);
master_add_incoming(target); /* prepare to receive remaining cobases */
msg->next = mplrs.outgoing;
mplrs.outgoing = msg;
master.act_producers[target]++;
master.num_producers++;
return;
}
/* header is a work header (length, maxd, maxc) not yet set.
* Set the parameters (maxd, maxc) as desired.
*/
void setparams(int *header)
{
/* if L is too small, use maxdepth */
if (master.lmin>0 && (master.size_L < mplrs.size*master.lmin))
header[1] = master.maxdepth;
else /* don't have too small L, so no maxdepth */
header[1] = 0;
header[2] = master.maxcobases;
if (master.lmax>0 && (master.size_L > mplrs.size * master.lmax))
header[2] = header[2] * master.scalec;
header[3] = master.lponly;
}
/* check if we want to stop now.
* if master.stop_filename exists or time limit exceeded,
* set master.checkpointing = 1.
* this is not an immediate stop -- we wait for current workers to
* complete the tasks they've been assigned, stopping after that.
*/
void check_stop(void)
{
struct timeval cur;
int flag = 0;
if (master.checkpointing || master.cleanstop)
return; /* already stopping */
if (master.stop_filename)
{
mprintf2(("M: checking stop file %s\n", master.stop_filename));
master.stop = fopen(master.stop_filename, "r");
if (master.stop!=NULL)
{
flag=1;
fclose(master.stop);
}
}
if (master.time_limit!=0)
{
mprintf2(("M: checking if time exceeded\n"));
gettimeofday(&cur, NULL);
if (cur.tv_sec - mplrs.start.tv_sec > master.time_limit)
flag=1;
}
if (flag!=0)
{
mprintf(("M: Stop condition detected, checkpointing!\n"));
master.checkpointing = 1;
}
}
void master_stop_consumer(int already_stopping)
{
MPI_Request ign;
int check[7] = {STOPFLAG,0,0,0,0,0,0};
mprintf2(("M: telling consumer to stop, already_stopping:%d checkpointing:%d\n", already_stopping, master.checkpointing));
if (!already_stopping)
MPI_Isend(check, 7, MPI_INT, CONSUMER, 7,
MPI_COMM_WORLD, &ign);
master.cleanstop = 1;
}
/* check if we've caught a signal, or we've received a message from someone
* that has. If so, we want to checkpoint like above
* The similar mplrs_cleanstop also uses this...
*/
void master_checksigs(void)
{
int i, flag, size=mplrs.size;
float junk=0;
MPI_Request ign;
int already_stopping = master.checkpointing || master.cleanstop;
if (mplrs.caughtsig == 1 && !already_stopping)
{
mprintf(("M: I caught signal, checkpointing!\n"));
/* this master.checkpointing must be inside a !already_stopping
* condition, see comment below
*/
master.checkpointing = 1;
already_stopping = 1;
}
for (i=1; i<size; i++)
{
MPI_Test(master.sigcheck+i, &flag, MPI_STATUS_IGNORE);
if (flag)
MPI_Irecv(master.sigbuf+i, 1, MPI_FLOAT, i, 9, MPI_COMM_WORLD,
master.sigcheck+i);
if (flag && master.sigbuf[i]==0 && !already_stopping)
{
mprintf(("M: %d caught signal, checkpointing!\n",i));
/* this master.checkpointing must also be inside a
* !already_stopping condition, see comment below
*/
master.checkpointing = 1;
already_stopping = 1;
}
else if (flag && master.sigbuf[i]==-1)
{
mprintf(("M: %d wants cleanstop, no checkpoint\n",i));
/* still needed, to tell consumer if overflow occurs
* in initial job
*/
master_stop_consumer(already_stopping);
MPI_Isend(&junk, 1, MPI_FLOAT, i,9,MPI_COMM_WORLD,&ign);
already_stopping = 1;
/* disable a checkpoint in case we notice overflow
* after deciding to checkpoint but before workers
* return. in that case we can't guarantee all
* output/unfinished cobases are produced so avoid
* chance of making an incorrect checkpoint
*/
/* note this is not in an !already_stopping condition*/
master.checkpointing = 0;
}
else if (flag && master.sigbuf[i]==1)
{
mprintf(("M: %d wants cleanstop, checkpointing!\n",i));
master.cleanstop = 1;
/* don't produce a checkpoint if overflow has
* already occurred, even if a checkpoint is
* requested after overflow but before shutdown,
* as in comment above
*/
if (!already_stopping)
master.checkpointing = 1;
already_stopping = 1;
}
}
}
/* we're checkpointing, receive stats from consumer and output checkpoint file
*/
/* two cases: we have a file to checkpoint to (normal)
* or, we caught a signal and want to checkpoint but have no checkpoint file
* in this case we append to the output file via the consumer, with
* comments on where to cut
*/
/* mplrs1 format: counts are 'unsigned int' (%d unfortunately)
* mplrs2 format: counts are 'unsigned long' (%lu)
* so mplrs1 files can be read by mplrs2 readers not necc. reverse
*/
void master_checkpoint(void)
{
int fin = -1;
mprintf(("M: making checkpoint file\n"));
recv_counting_stats(CONSUMER);
if (master.checkp_filename != NULL)
{
MPI_Send(&fin, 1, MPI_INT, CONSUMER, 1, MPI_COMM_WORLD);
master_checkpointfile();
return;
}
else
{
fin = 1;
MPI_Send(&fin, 1, MPI_INT, CONSUMER, 1, MPI_COMM_WORLD);
master_checkpointconsumer();
return;
}
}
/* note: master_checkpointconsumer and master_checkpointfile should
* produce the *same* format. First two lines of checkpointconsumer
* done by the consumer directly for now.
*/
void master_checkpointconsumer(void)
{
int len;
char *str;
slist *list, *next;
job *jb;
for (list=master.L; list; list=next)
{
next = list->next;
jb = list->data;
if (jb->type != 0) /* for now don't do fel/redund */
continue;
str = jb->cob;
len = strlen(str)+1; /* include \0 */
MPI_Send(&len, 1, MPI_INT, CONSUMER, 1, MPI_COMM_WORLD);
MPI_Send(str, len, MPI_CHAR, CONSUMER, 1, MPI_COMM_WORLD);
free(str);
free(jb);
free(list);
}
len = -1;
MPI_Send(&len, 1, MPI_INT, CONSUMER, 1, MPI_COMM_WORLD);
}
void master_checkpointfile(void)
{
slist *list, *next;
char *vol = cprat("", mplrs.Vnum, mplrs.Vden);
job *jb;
fprintf(master.checkp, "mplrs5\n%llu %llu %llu %llu %llu\n%s\n%llu\n%llu\n",
mplrs.rays, mplrs.vertices, mplrs.bases, mplrs.facets,
mplrs.intvertices,vol,mplrs.deepest, mplrs.deepest_vertex);
free(vol);
for (list=master.L; list; list=next)
{
next = list->next;
jb = list->data;
if (jb->type == 0) /* cobasis */
fprintf(master.checkp, "%s\n", jb->cob);
free(jb->cob);
free(jb);
free(list);
master.size_L--;
}
/* fclose(master.checkp); */ /* not here */
return;
}
/* return the depth in cob */
long getdepth(const char *cob)
{
int len, i;
long depth=-1;
len = strlen(cob);
for (i=0; i<len-1; i++)
if (cob[i]=='!')
{
depth = strtol(cob+i+1, NULL, 10);
break;
}
mprintf3(("%d: depth %ld in %s\n", mplrs.rank, depth, cob));
return depth;
}
/* we want to restart. load L and counting stats from restart file,
* send counting stats to consumer (after notifying consumer of restart)
*/
void master_restart(void)
{
char *line=NULL;
char *vol=NULL;
job *njob;
size_t size=0, vsize=0;
ssize_t len=0;
int restart[7] = {RESTARTFLAG,0,0,0,0,0,0};
int ver, rc;
/* check 'mplrs1' header */
len = getline(&line, &size, master.restart);
if (len!=7 || (strcmp("mplrs1\n",line) && strcmp("mplrs2\n",line) &&
strcmp("mplrs3\n",line) && strcmp("mplrs4\n",line) &&
strcmp("mplrs5\n",line)))
{
printf("Unknown checkpoint format\n");
/* MPI_Finalize(); */
exit(0);
}
mprintf2(("M: found checkpoint header\n"));
sscanf(line,"mplrs%d\n",&ver);
/* get counting stats */
rc = fscanf(master.restart, "%llu %llu %llu %llu %llu\n", &mplrs.rays,
&mplrs.vertices, &mplrs.bases, &mplrs.facets,
&mplrs.intvertices);
if (rc != 5)
printf("*Broken checkpoint file, results may be strange\n");
if (ver<3) /* volume added in mplrs3 */
printf("*Old checkpoint file, volume may be incorrect\n");
else /* get volume */
{
len = getline(&vol, &vsize, master.restart);
if (len<=1)
{
printf("Broken checkpoint file\n");
exit(0);
}
vol[len-1] = '\0'; /* remove '\n' */
#if defined(MA) || defined(GMP) || defined(FLINT)
plrs_readrat(mplrs.Tnum, mplrs.Tden, vol);
copy(mplrs.tN, mplrs.Vnum); copy(mplrs.tD, mplrs.Vden);
linrat(mplrs.tN, mplrs.tD, 1L, mplrs.Tnum, mplrs.Tden,
1L, mplrs.Vnum, mplrs.Vden);
#else
if (strcmp(vol,"0"))
printf("*Checkpoint volume ignored, use gmp or hybrid mplrs\n");
#endif
free(vol);
}
if (ver<4) /* tree depth added in mplrs4 */
printf("*Old checkpoint file, tree depth may be incorrect\n");
else
{
rc = fscanf(master.restart, "%llu\n", &mplrs.deepest);
if (rc != 1)
printf("*Broken checkpoint file, results may be strange\n");
}
if (ver<5) /* depth of deepest vertex added in mplrs5 */
printf("*Old checkpoint file, depth of deepest vertex may be incorrect\n");
else
{
rc = fscanf(master.restart, "%llu\n", &mplrs.deepest_vertex);
if (rc != 1)
printf("*Broken checkpoint file, results may be strange\n");
}
/* get L */
while((len = getline(&line, &size, master.restart))!= -1)
{
if (line[0]=='\n') /* ignore blank lines */
{
free(line);
line = NULL;
size=0;
continue;
}
line[strlen(line)-1]='\0'; /* replace \n by \0 */
njob = new_job(0, line, NULL, 0, getdepth(line));
if (master.flipstart)
master.L = addlist(master.L, njob);
else
master.L = addlist_tail_L(njob);
master.size_L++;
line = NULL;
size = 0;
}
master.tot_L = master.size_L; /* maybe should save and retrieve */
mprintf(("M: Restarted with |L|=%lu\n",master.size_L));
fclose(master.restart);
if (mplrs.minheight == 1 && !L_sorted())
sort_L(master.size_L);
MPI_Send(restart, 7, MPI_INT, CONSUMER, 7, MPI_COMM_WORLD);
send_counting_stats(CONSUMER);
mplrs.rays = 0;
mplrs.vertices = 0;
mplrs.bases = 0;
mplrs.facets = 0;
mplrs.intvertices = 0;
return;
}
/* check if we should update the histogram and then do it */
void print_histogram(struct timeval *cur, struct timeval *last)
{
float sec;
int i;
int act;
gettimeofday(cur,NULL);
if (cur->tv_sec > last->tv_sec)
{
sec = (float)(cur->tv_sec - mplrs.start.tv_sec) + ((float)(cur->tv_usec - mplrs.start.tv_usec))/1000000;
act=0;
for (i=0; i<mplrs.size; i++)
if (master.act_producers[i])
act++;
/* time (seconds), number of active producers, number of
* producers owing us a message about remaining cobases,
* 0, 0 (existed in mplrs.cpp but no longer)
*/
fprintf(master.hist, "%f %d %lu %d %d %d %lu\n",
sec, act, master.size_L, master.num_producers, 0, 0, master.tot_L);
fflush(master.hist);
last->tv_sec = cur->tv_sec;
last->tv_usec = cur->tv_usec;
}
}
/**********
* worker *
**********/
int mplrs_worker(void)
{
char *starting_cobasis;
long *nums=NULL;
/* header for incoming work:
* {length of work, int maxdepth, int maxcobases, bool messages,
* int type, 3xfuture use}
*/
int header[8]={0,0,0,0,0,0,0,0};
MPI_Request req = MPI_REQUEST_NULL;
unsigned int ncob=0; /* used for # cobases in prev. job */
unsigned int tot_ncob=0;
int len;
int flag;
if (!mplrs.abortinit) /* didn't receive file if parsing failed */
mplrs_worker_init();
while (1)
{
mplrs.dummyout = 0; /* for renumbering jobs with no output */
ncob = mplrs.bases - tot_ncob; /* #cobases in last job */
tot_ncob = mplrs.bases; /* #cobases done so far */
/* check signals */
mplrs_handlesigs();
/* ask for work */
mprintf2(("%d: Asking master for work\n",mplrs.rank));
MPI_Isend(&ncob, 1, MPI_UNSIGNED, MASTER, 6, MPI_COMM_WORLD,
&req);
flag = 0;
while (1) /* was MPI_Wait(&req, MPI_STATUS_IGNORE); */
{
MPI_Test(&req, &flag, MPI_STATUS_IGNORE);
if (flag)
break;
clean_outgoing_buffers();
}
starting_cobasis = NULL;
/* get response */
MPI_Recv(header, 8, MPI_INT, MASTER, MPI_ANY_TAG,
MPI_COMM_WORLD, MPI_STATUS_IGNORE);
mprintf2(("%d: Message received from master\n",mplrs.rank));
len = header[0];
if (len == -1) /* no more work to do */
return mplrs_worker_finished();
if (header[5] == 3) /* minrep mode - master wants our redineq */
{ /* to get all new linearities, will then */
send_redineq(mplrs.redineq, MASTER);
/* schedule new parallel run */
/* sent so wait for master to tell us what */
memset(mplrs.R->redineq,0, (mplrs.R->m+1)*sizeof(long));
} /* to do next */
else if (header[5] == 4) /* minrep mode - get new redineq */
{
get_redineq(MASTER);
mplrs.minrep_nonfinal = 0; /* now in final run */
}
else if (header[5] == 0 && len>0)
{
starting_cobasis = malloc(sizeof(char)*(len+1));
MPI_Recv(starting_cobasis, len, MPI_CHAR, MASTER,
MPI_ANY_TAG, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
starting_cobasis[len] = '\0';
}
else if (header[5] == 1 || header[5] == 2)
{
nums = malloc(sizeof(long)*len);
MPI_Recv(nums, len, MPI_LONG, MASTER, MPI_ANY_TAG,
MPI_COMM_WORLD, MPI_STATUS_IGNORE);
}
mplrs.outputblock = 0; /* enable maxbuf-based flushing */
/* do work */
if (header[5]!=3 && header[5]!=4)
do_work(header, nums, starting_cobasis);
free(starting_cobasis);
free(nums);
starting_cobasis=NULL;
nums=NULL; /* only needed for minrep runs, where we use nums
* then free, then in round 1 free again */
if (mplrs.redund)
mplrs_worker_send_redineq();
/* send output and unfinished cobases */
mplrs.outputblock = 0; /* enable maxbuf-based flushing */
process_output();
process_curwarn();
return_unfinished_cobases();
clean_outgoing_buffers(); /* check buffered sends,
* free if finished
*/
}
return 0; /* unreachable */
}
/* run lrs_main.
* easy in the non-hybrid case. in the hybrid case, runs the appropriate one,
* cleaning up and proceeding to the next arithmetic package as needed until
* finishing the run.
* for now only use with stage != 0, TODO: handle init run too
* header, starting_cobasis only used if header non-NULL
* nums is for redund runs, only used if header[5]==1
*/
void run_lrs(int argc, char **argv, long o, long stage,
const int *header, long *nums, char *starting_cobasis)
{
long ret = 1;
#ifndef MA
if (mplrs.overflow != 3)
ret = mplrs.lrs_main(argc, argv, &mplrs.P, &mplrs.Q, o, stage,
NULL, mplrs.R);
if (ret == 1 || (ret==2 && mplrs.redund))
{
mplrs.overflow = 3;
mplrs.lrs_main(argc, argv, &mplrs.P, &mplrs.Q, o, 2, NULL,
mplrs.R);
worker_report_overflow();
}
return;
#else /* hybrid */
while ((ret == 1) || (ret==2 && mplrs.redund)) /* overflow */
{
ret = mplrs.lrs_main(argc, argv, &mplrs.P, &mplrs.Q, o, stage,
NULL, mplrs.R);
mprintf3(("%d: lrs_main returned %ld (overflow status %d)\n",
mplrs.rank, ret, mplrs.overflow));
if (ret == 0) /* done */
break;
mplrs.lrs_main(argc, argv, &mplrs.P, &mplrs.Q, o, 2, NULL,
mplrs.R);
if (stage!=0)
overflow_cleanup();
mprintf2(("%d: overflow in run_lrs, trying next\n",
mplrs.rank));
#ifdef B128
if (mplrs.lrs_main == lrs1_main)
{
mplrs.overflow = 1;
mplrs.lrs_main = lrs2_main;
mplrs_worker_init(); /* re-init */
if (header!=NULL)
set_restart(header, nums, starting_cobasis);
if (consumer.final_redundcheck)
consumer_setredineq();
continue;
}
#endif
mplrs.overflow = 2;
mplrs.lrs_main = lrsgmp_main;
mplrs_worker_init(); /* re-init */
if (header != NULL)
set_restart(header, nums, starting_cobasis);
if (consumer.final_redundcheck)
consumer_setredineq();
continue;
}
mprintf2(("%d: lrs_main finished, package status %d\n",
mplrs.rank, mplrs.overflow));
return;
#endif
}
void mplrs_worker_init(void)
{
char *argv[] = {argv0, mplrs.tfn};
long o = 1;
if ((mplrs.rank == MASTER && master.cfel) ||
(mplrs.rank == CONSUMER && mplrs.fel && !consumer.felfallback) ||
(mplrs.rank!=MASTER && mplrs.rank!=CONSUMER && mplrs.fel &&
mplrs.nohiddenl))
argv[0] = "fel"; /* hack for -fel */
if ((mplrs.rank == MASTER && (master.redund || master.minrep)) ||
(mplrs.rank == CONSUMER && mplrs.redund && (!mplrs.fel || consumer.felfallback)) ||
(mplrs.rank!=MASTER && mplrs.rank!=CONSUMER && mplrs.redund && (!mplrs.fel || !mplrs.nohiddenl)))
argv[0] = "minrep"; /* hack for -minrep */
mprintf3(("%d: mplrs.fel=%d, mplrs.nohiddenl=%d, mplrs.redund=%d consumer.felfallback=%d, my argv[0] is %s\n", mplrs.rank, mplrs.fel, mplrs.nohiddenl, mplrs.redund, consumer.felfallback, argv[0]));
if (mplrs.R != NULL)
{ /* overflow happened, free and re-init */
free(mplrs.R->redineq);
free(mplrs.R->facet);
free(mplrs.R);
}
mplrs.R = lrs_alloc_restart();
mplrs.R->size = mplrs.size;
mplrs.R->rank = mplrs.rank;
mplrs.R->testlin = 0; /*default. must always inititialize to something*/
mprintf2(("%d: calling lrs_main to setup P & Q\n", mplrs.rank));
/* temp hack to test message requests */
/* initialization done on the master to get the full redund line.
* stage=1 done on INITIAL because the master never does stage=1
*/
/* note we must be careful about returns below in order to fix this
* up on MASTER and INITIAL
*/
if(mplrs.rank == MASTER)
mplrs.R->messages=master.m_messages;
else
mplrs.R->messages=0;
mplrs.tfile = fopen(mplrs.tfn, "w");
fprintf(mplrs.tfile, "%s", mplrs.input);
fclose(mplrs.tfile);
while (o != 0)
{
if (mplrs.specialmode == 2) /* setting up fel, looking for */
{
mplrs.R->testlin=1; /* hidden linearities. need */
mplrs.R->fel=1; /* testlin and fel on */
}
else if (mplrs.specialmode == 3) /* fel phase 1, only fel on */
{
mplrs.R->testlin=0;
mplrs.R->fel=1;
}
o = mplrs.lrs_main(2,argv,&mplrs.P,&mplrs.Q,0,0,NULL,mplrs.R);
if (o == -1)
{
mprintf(("%d: failed lrs setup, want to exit\n",
mplrs.rank));
mplrs.abortinit = 1;
break;
}
if (o == 1)
{
mplrs.lrs_main(2,argv,&mplrs.P,&mplrs.Q,0,2,NULL,mplrs.R);
#ifdef MA
mprintf2(("%d: overflow in init, trying next arithmetic\n", mplrs.rank));
overflow_cleanup(); /* 2020.6.1 : avoid multiple
warnings etc when master overflows in setup */
#ifdef B128 /* hybrid with B128 */
if (mplrs.lrs_main == lrs1_main)
{
mplrs.lrs_main = lrs2_main;
mplrs.overflow = 1;
}
else
{
mplrs.lrs_main = lrsgmp_main;
mplrs.overflow = 2;
}
#else /* hybrid but no B128 */
mplrs.lrs_main = lrsgmp_main;
mplrs.overflow = 2;
#endif
#else /* not hybrid, stop on overflow */
mprintf(("%d: overflow in init, fatal\n", mplrs.rank));
worker_report_overflow();
mplrs.overflow = 3; /* fatal overflow */
break;
#endif
}
}
mprintf3(("%d: lrs_main setup finished (%ld)\n", mplrs.rank, o));
mplrs.R->facet = calloc(mplrs.R->d+1, sizeof(long));
remove(mplrs.tfn);
mplrs.R->overide = 1;
if (mplrs.rank == CONSUMER && mplrs.fel)
{
/* mplrs.R->redineq needs the right dimension before stage 1
* but lrs only tells us in stage 1, so reallocate with
* dimension the master told us
*/
free(mplrs.R->redineq);
/* add 1 extra since lrs seems to use it sometimes ... */
mplrs.R->redineq = calloc(consumer.m+2, sizeof(long));
}
if (mplrs.specialmode == 1) /* we're in master minrep/felmode,
* checking if there are hidden linearities.
* so we want testlin ON, redund OFF
* regardless of what the input file says.
*/
{
mplrs.R->testlin = 1;
mplrs.R->redund = 0;
mplrs.R->fel = 0;
}
if (mplrs.nohiddenl != -1) /*we know whether there's an interior point*/
mplrs.R->redundphase = mplrs.nohiddenl;
/* so tell lrs (redund/minrep/felmode) */
process_curwarn();
}
/* receive redineq from target - matches send_redineq and also the master's
* version that sends the merged version from master.redineq
*/
void get_redineq(int target)
{
mprintf2(("%d: getting redineq from %d\n", mplrs.rank, target));
if (mplrs.redineq == NULL) /* shouldn't happen */
mplrs.redineq = malloc(sizeof(long)* (mplrs.R->m+1));
MPI_Recv(mplrs.redineq, mplrs.R->m+1, MPI_LONG, target, 6,
MPI_COMM_WORLD, MPI_STATUS_IGNORE);
return;
}
/* Send our mplrs.R->redineq[i] to the specified recipient.
* Sends directly as longs, eventually mplrs_worker_send_redineq() should
* just use this
*/
void send_redineq(long *redineq, int target)
{
long m = mplrs.R->m;
mprintf3(("%d: sending redineq(m=%ld) to %d\n", mplrs.rank, m, target));
MPI_Send(redineq, m+1, MPI_LONG, target,
6, MPI_COMM_WORLD);
return;
}
/* Send this worker's redineq to the consumer.
* For now just uses the 071 code, but should be redone since
* we're no longer using post_output("redund",...)
*/
void mplrs_worker_send_redineq(void)
{
char *redund_string = malloc(sizeof(char)*8);
int i, m = mplrs.R->m, len=0;
char *tmp;
tmp = malloc(sizeof(char)*snprintf(NULL, 0, " %d -%d ", m, m));
redund_string[0] = '\0';
mprintf2(("%d (nonfinal=%d): got redineq array ",mplrs.rank,mplrs.minrep_nonfinal));
for (i=1; i<=m; i++)
{
mprintf2((" %ld",mplrs.R->redineq[i]));
if (mplrs.R->redineq[i] != 0)
{
if (mplrs.redineq!=NULL && mplrs.minrep_nonfinal)
{
/* save to send the master later */
if (mplrs.redineq[i]!=-1)
mplrs.redineq[i]=mplrs.R->redineq[i];
}
/* don't send weakly redundant to consumer in the
* first round of a minrep run with hidden linearities-
* send them the second time
*/
/* could just not send anything the first round */
if (mplrs.R->redineq[i] == 1 && mplrs.minrep_nonfinal)
continue;
/* quick hack, send linearities as -2 */
/* avoids confusing consumer re 1s sent from proc 2 */
if (mplrs.R->redineq[i] == 2)
mplrs.R->redineq[i] = -2;
sprintf(tmp, " %d %ld", i, mplrs.R->redineq[i]);
/*mprintf3(("%d: adding %s to %s\n", mplrs.rank, tmp,
redund_string));*/
redund_string = append_out(redund_string, &len,
tmp);
}
}
mprintf2(("\n"));
if (len>0)
{
mprintf3(("%d: sending redund_string: %s\n", mplrs.rank,
redund_string));
send_output(3, redund_string);
}
else
free(redund_string);
free(tmp);
}
/* This worker has finished. Tell the consumer, send counting stats
* and exit.
*/
int mplrs_worker_finished(void)
{
int done[7] = {-1};
mprintf((" %d: All finished! Informing consumer.\n",mplrs.rank));
while (mplrs.outgoing) /* needed? negligible in any case */
{
clean_outgoing_buffers();
}
MPI_Send(&done, 7, MPI_INT, CONSUMER, 7, MPI_COMM_WORLD);
send_counting_stats(CONSUMER);
/* free P & Q */
if (mplrs.overflow != 3 && !mplrs.abortinit)
{
mplrs.lrs_main(0,NULL,&mplrs.P,&mplrs.Q,0,2,NULL,mplrs.R);
}
if (mplrs.R != NULL)
{
free(mplrs.R->redineq);
free(mplrs.R->facet);
free(mplrs.R);
}
free(mplrs.tfn);
free(mplrs.input);
mplrs_freemps();
free(mplrs.finalwarn);
free(mplrs.curwarn);
free(mplrs.redineq);
MPI_Finalize();
return 0;
}
/* Go through our outgoing MPI_Isends, free anything that has completed.
* Also, if any of these are queued and the header has completed, then
* send the remaining data.
* Don't use with incoming buffers.
*/
void clean_outgoing_buffers(void)
{
msgbuf *msg, *next, *prev=NULL;
for (msg = mplrs.outgoing; msg; msg=next)
{
next = msg->next;
if (!outgoing_msgbuf_completed(msg))
{
prev = msg;
continue;
}
if (prev)
prev->next = next;
else
mplrs.outgoing = next;
free_msgbuf(msg);
}
}
/* had an overflow; discard any buffered output and
* unfinished cobases. some output may have already been
* sent (due to -maxbuf) so duplication still possible.
* also resets mplrs.outputblock = 0
*/
void overflow_cleanup(void)
{
outlist *out = mplrs.output_list, *next;
slist *list, *lnext;
mplrs.outputblock = 0;
/* discard output */
mplrs.outnum = 0; /* clearing buffer */
mplrs.output_list = NULL;
mplrs.ol_tail = NULL;
for (; out; out=next)
{
next = out->next;
free(out->type);
free(out->data);
free(out);
}
/* discard cobases */
for (list=mplrs.cobasis_list; list; list=lnext)
{
lnext = list->next;
free(list->data);
free(list);
}
mplrs.cobasis_list = NULL;
/* discard curwarn */
mplrs.curwarn[0] = '\0';
}
/* set up restart parameters in mplrs.R */
/* header[1] gives maxdepth, header[2] gives maxcobases,
* if header[0] > 0,
* starting_cobasis gives the starting cobasis.
* if header[0] == 0, starting at initial input (phase 1)
* header[4] gives desired bool for R->messages
* header[5] gives type: 0 normal, 1 for redund, 2 for fel
* if header[5]==1, then use nums to re-init redineq, header[0] size of nums
* if header[5]==2, use nums to fake (rank,size) for fel (likely to change)
* if header[5]==1 or 2, negative indices in nums are linearities, set to 2
*
* if mplrs.redineq!=NULL and mplrs.minrep_nonfinal==0, then we're in minrep
* mode round 2, and need to copy linearities from mplrs.redineq
* (master's version)
* to mplrs.R->redineq since we may have found hidden linearities that
* are needed in round 2.
*/
void set_restart(const int *header, long *nums, char *starting_cobasis)
{
lrs_restart_dat *R = mplrs.R;
long *tmp;
int i, j, len=header[0];
long depth = 0; /* if restarting from earlier checkpoint,
* then we want to fall back to old depth-0
* behavior */
/* prepare input file */
mplrs.initializing = 0;
/* reset counts */
for (i=0; i<10; i++)
R->count[i] = 0;
R->messages = header[4];
if (!mplrs.minrep_nonfinal && mplrs.redineq != NULL) /* see comment about minrep mode above */
{
for (i=0; i<=R->m; i++)
if (mplrs.redineq[i]==2)
mplrs.R->redineq[i] = mplrs.redineq[i];
/* second round, no more hidden linearities */
mplrs.R->testlin = 0;
mplrs.R->redundphase = 1;
}
else if (mplrs.minrep_nonfinal) /* may have hidden linearities */
{ /* should already be set? */
mplrs.R->redund = mplrs.R->testlin = 1;
mplrs.R->redundphase = 0;
}
if (header[5] == 1 || header[5] == 2) /* redund or fel run */
{
mplrs.R->overide = 1;
mplrs.R->redund = 1;
if (mplrs.m > mplrs.R->m)
{ /* sometimes in fel runs m increases, need to realloc
* this is only found in stage=1 so we get it from
* the master
*/
tmp = realloc(mplrs.R->redineq, sizeof(long)*(mplrs.m+1));
if (tmp==NULL) /* uh oh ... */
MPI_Abort(MPI_COMM_WORLD,2);
mplrs.R->redineq = tmp;
}
mplrs.R->m = mplrs.m;
for (i=0; i<=mplrs.R->m; i++)
mplrs.R->redineq[i] = 0;
for (i=0; i<len; i++)
{
if (nums[i]>0) /* normal */
mplrs.R->redineq[nums[i]] = 1;
else /* linearity */
mplrs.R->redineq[-1 * nums[i]] = 2;
}
mprintf3(("%d: set redund line: ",mplrs.rank));
for (i=1; i<=mplrs.R->m; i++)
if (mplrs.R->redineq[i]==1)
mprintf3((" %d", i));
mprintf3(("\n"));
}
else if (header[0]>0)
{
/* ugly: recover depth after '!' marker */
len = strlen(starting_cobasis);
for (i=0; i<len-1; i++)
if (starting_cobasis[i]=='!')
{
depth = strtol(starting_cobasis+i+1, NULL, 10);
starting_cobasis[i] = ' ';
break;
}
mprintf3(("%d: depth %ld in %s\n", mplrs.rank, depth,
starting_cobasis));
R->depth = depth;
R->mindepth = depth;
R->restart = 1;
if (i<len-1) /* reset in case redo because of overflow */
starting_cobasis[i] = '!';
/* 2018.4.28 recover restart line */
/* TODO: should be sent as longs, no more need for string */
mprintf3(("%d: facets \"", mplrs.rank));
for (; i<len && starting_cobasis[i]!=' '; i++);
for (; i<len && starting_cobasis[i]==' '; i++);
for (j=0; j<R->d; j++)
{
if (i<len)
R->facet[j] = atol(starting_cobasis+i);
else
R->facet[j] = 0;
mprintf3(("%ld ", R->facet[j]));
for (; i<len && starting_cobasis[i]!=' '; i++);
i++;
}
mprintf3(("\" from %s\n", starting_cobasis));
}
else
{
mplrs.initializing = 1; /* phase 1 output handled different */
R->count[2] = 1; /* why? to get begin line... */
R->depth = 0;
R->restart = 0;
}
if (header[1]>0)
R->maxdepth = header[1] + depth;
if (header[2]>0)
R->maxcobases = header[2];
return;
}
/* update counts in mplrs.rays, etc via mplrs.R */
/* see lrsrestart.h */
void update_counts(void)
{
lrs_restart_dat *R = mplrs.R;
int hull = R->count[5];
long deepest = mplrs.deepest; /* silly, avoid sign comparison warning */
long linearities = mplrs.linearities; /* silly, avoid warning */
long deepv = mplrs.deepest_vertex;
if (!hull)
mplrs.rays += R->count[0];
else
mplrs.facets += R->count[0];
if (!hull)
mplrs.vertices += R->count[1];
mplrs.bases += R->count[2];
mplrs.intvertices += R->count[4];
if (linearities < R->count[6])
mplrs.linearities = R->count[6];
if (deepest < R->count[7])
mplrs.deepest = R->count[7];
if (deepv < R->count[8])
mplrs.deepest_vertex = R->count[8];
}
/* header[1] gives maxdepth, header[2] gives maxcobases,
* if header[0] > 0,
* starting_cobasis gives the starting cobasis.
* if header[0] == 0, starting at initial input (phase 1)
* header[4] gives desired bool for R->messages
* header[5] gives type of work unit, 0 usual, 1 for redund
*/
void do_work(const int *header, long *nums, char *starting_cobasis)
{
char *argv[] = {argv0, mplrs.tfn};
mprintf3(("%d: Received work (%d,%d,%d)\n",mplrs.rank,header[0],
header[1],header[2]));
if (mplrs.testlin && mplrs.redund)
{ /* not needed for first stage 1 run, but later ones need new P,Q*/
mplrs.lrs_main(0,NULL,&mplrs.P,&mplrs.Q,0,2,NULL,mplrs.R);
mplrs_worker_init();
}
set_restart(header, nums, starting_cobasis);
mprintf2(("%d: Calling run_lrs\n",mplrs.rank));
run_lrs(2, argv, 0, 1, header, nums, starting_cobasis);
mprintf2(("%d: run_lrs returned, updating counts\n",mplrs.rank));
update_counts();
}
/* lrs reported overflow; tell the master and prepare to finish */
void worker_report_overflow(void)
{
float junk = -1;
mprintf(("%d: reporting overflow\n", mplrs.rank));
if (mplrs.rank == MASTER)
{ /* possible now since master does worker_init to get m and redund options */
mplrs.overflow = 3;
return;
}
send_output(2, dupstr("*possible overflow: try using gmp or hybrid mplrs\n"));
/* inform master, wait for reply */
MPI_Send(&junk, 1, MPI_FLOAT, MASTER, 9, MPI_COMM_WORLD);
MPI_Recv(&junk, 1, MPI_FLOAT,MASTER,9,MPI_COMM_WORLD,MPI_STATUS_IGNORE);
}
/* The worker has finished its work. Process the output, preparing
* and sending the output to the consumer, and preparing the unfinished
* cobases for return_unfinished_cobases().
*/
void process_output(void)
{
outlist *out = mplrs.output_list, *next;
char *out_string=NULL; /* for output file if exists */
char *serr_string=NULL; /* for stdout */
const char *type;
const char *data;
int len = 1024;
int len2 = 256;
mplrs.outnum = 0; /* clearing buffer */
mplrs.output_list = NULL;
mplrs.ol_tail = NULL;
out_string = malloc(sizeof(char)*len);
out_string[0]='\0';
serr_string = malloc(sizeof(char)*len2);
serr_string[0]='\0';
while (out)
{
type = out->type;
data = out->data;
if (!strcmp(type, "vertex"))
out_string = append_out(out_string, &len, data);
else if (!strcmp(type, "ray")) /* unused */
out_string = append_out(out_string, &len, data);
else if (!strcmp(type, "cobasis"))
out_string = append_out(out_string, &len, data);
else if (!strcmp(type, "V cobasis")) /* unused */
out_string = append_out(out_string, &len, data);
else if (!strcmp(type, "volume")) /* still used */
{
#if defined(MA) || defined(GMP) || defined(FLINT)
plrs_readrat(mplrs.Tnum, mplrs.Tden, data);
copy(mplrs.tN, mplrs.Vnum); copy(mplrs.tD, mplrs.Vden);
linrat(mplrs.tN, mplrs.tD, 1L, mplrs.Tnum, mplrs.Tden,
1L, mplrs.Vnum, mplrs.Vden);
#endif
}
else if (!strcmp(type, "options warning")) /* unused */
{
/* only do warnings once, otherwise repeated */
if (mplrs.initializing)
out_string = append_out(out_string, &len, data);
}
else if (!strcmp(type, "header"))
{
/*only do header if initializing, otherwise repeated*/
if (mplrs.initializing)
out_string = append_out(out_string, &len, data);
}
else if (!strcmp(type, "debug")) /* currently unused */
{
out_string = append_out(out_string, &len, data);
}
else if (!strcmp(type, "warning"))
{ /* warnings always go to output file and stderr if output is not stdout */
out_string = append_out(out_string, &len, data);
if(consumer.output != stdout)
serr_string = append_out(serr_string, &len2, data);
}
else if (!strcmp(type, "flush"))
{
out_string = append_out(out_string, &len, data);
}
else if (!strcmp(type, "finalwarn"))
{ /* these are printed at end of run */
mplrs.curwarn = append_out(mplrs.curwarn,
&mplrs.curwarn_len,
data);
/* but also to stderr now */
serr_string = append_out(serr_string, &len2, data);
}
next = out->next;
free(out->type);
free(out->data);
free(out);
out = next;
}
if (mplrs.renumber && mplrs.dummyout==0 &&
strlen(out_string)<1 && strlen(serr_string)<1)
{
strcpy(out_string, "?"); /* special mark, just update count*/
mplrs.dummyout = 1;
}
if (strlen(out_string)>0 && strcmp(out_string, "\n"))
send_output(1, out_string);
else
free(out_string);
if (strlen(serr_string)>0 && strcmp(serr_string, "\n"))
send_output(0, serr_string);
else
free(serr_string);
}
/* if we've produced anything for finalwarn, add it to finalwarn now.
* buffered in curwarn to clear on overflow, avoiding multiple copies
*/
void process_curwarn(void)
{
if (strlen(mplrs.curwarn)>0)
{
mplrs.finalwarn = append_out(mplrs.finalwarn,
&mplrs.finalwarn_len,mplrs.curwarn);
mplrs.curwarn[0] = '\0';
}
}
/* send this string to the consumer to output.
* If dest==1, then it goes to the output file (stdout if no output file).
* If dest==0, then it goes to stderr.
* If dest==2, it's an overflow message: only the first one printed (stderr)
* If dest==3, it's a redund output: consumer handles specially
*
* The pointer str is surrendered to send_output and should not be changed
* It will be freed once the send is complete.
*/
/* str should not be NULL */
void send_output(int dest, char *str)
{
msgbuf *msg = malloc(sizeof(msgbuf));
int *header = calloc(7, sizeof(int));
header[0] = dest;
header[1] = strlen(str);
header[2] = mplrs.my_tag; /* to ensure the dest/str pair
* remains intact even if another
* send happens in between
*/
if (mplrs.rank != MASTER)
{
header[3] = mplrs.R->count[0]; /* rays/facets counts for -renumber */
header[4] = mplrs.R->count[1]; /* #verts */
header[5] = mplrs.R->count[2]; /* #bases */
header[6] = mplrs.R->count[5]; /* hull bool */
}
msg->req = malloc(sizeof(MPI_Request)*2);
msg->buf = malloc(sizeof(void *)*2);
msg->buf[0] = header;
msg->buf[1] = str;
msg->count = 2;
msg->target = CONSUMER;
msg->queue = 1;
msg->tags = malloc(sizeof(int)*2);
msg->sizes = malloc(sizeof(int)*2);
msg->types = malloc(sizeof(MPI_Datatype)*2);
msg->current_count = NULL;
msg->types[1] = MPI_CHAR;
msg->sizes[1] = header[1]+1;
msg->tags[1] = mplrs.my_tag;
mplrs.my_tag++;
msg->next = mplrs.outgoing;
mplrs.outgoing = msg;
MPI_Isend(header, 7, MPI_INT, CONSUMER, 7, MPI_COMM_WORLD,
msg->req);
}
/* lrs returned this unexplored cobasis - send it along.
* For now converts to a string and re-uses the old code,
* but avoids horrible process_cobasis(). TODO: send along as
* longs instead.
*/
void post_R(lrs_restart_dat *cob)
{
int i=0, offs=0;
int arg = 0;
int hull = cob->count[5];
char *newcob = NULL;
cobs *ncobs = NULL;
while (arg == 0)
{
arg = offs;
if (hull == 0)
offs = snprintf(newcob,arg," %ld %ld %ld!%ld ",
cob->count[1],cob->count[0],
cob->count[2],cob->depth);
else
offs = snprintf(newcob,arg," %ld %ld!%ld ",
cob->count[1],cob->count[2],
cob->depth);
for (i=0; i<cob->d; i++)
offs += snprintf(newcob+offs,arg,"%ld ", cob->facet[i]);
if (newcob == NULL)
newcob = malloc(sizeof(char)*(offs+1));
}
ncobs = malloc(sizeof(cobs));
ncobs->str = newcob;
ncobs->depth = cob->depth;
mplrs.cobasis_list = addlist(mplrs.cobasis_list, ncobs);
}
slist *addlist(slist *list, void *buf)
{
slist *n = malloc(sizeof(struct slist));
n->data = buf;
n->next = list;
return n;
}
/* add item to tail of master.L,, update master.L and master.tail_L,
* returns master.L
*/
slist *addlist_tail_L(void *buf)
{
slist *n = malloc(sizeof(struct slist));
n->data = buf;
n->next = NULL;
if (master.tail_L != NULL)
{
master.tail_L->next = n;
master.tail_L = n;
return master.L;
}
/* L is empty */
master.L = n;
master.tail_L = n;
return master.L;
}
/* compare depth of left and right in L, to sort jobs */
int L_compare_depth(const void *left, const void *right)
{
const job *leftj = *(const job **)left,
*rightj = *(const job **)right;
return leftj->depth - rightj->depth;
}
/* sort_L(size_L)
* mplrs.minheight is set, and we restarted with this L.
* Unfortunately it's not sorted so sort it.
*/
void sort_L(int size_L)
{
slist *list;
int i;
job **array=malloc(sizeof(job *)*size_L);
for (i=0, list=master.L; i<size_L; i++, list=list->next)
array[i] = list->data;
qsort(array, size_L, sizeof(job *), L_compare_depth);
for (list=master.L, i=0; i<size_L; i++, list=list->next)
list->data = array[i];
free(array);
}
/* check if master.L is sorted */
int L_sorted(void)
{
slist *list;
job *tmp1, *tmp2;
#ifdef MPLRSALWAYSSORT
return 0;
#endif
for (list=master.L; list && list->next; list=list->next)
{
tmp1 = list->data;
tmp2 = list->next->data;
if (tmp1->depth>tmp2->depth)
return 0;
}
return 1;
}
/* check if mplrs.cobasis_list is already sorted. usually it is,
* so avoid qsort in that case
*/
int worker_sorted(void)
{
slist *list;
cobs *tmp1, *tmp2;
#ifdef MPLRSALWAYSSORT
return 0;
#endif
for (list=mplrs.cobasis_list; list && list->next; list=list->next)
{
tmp1=list->data;
tmp2=list->next->data;
if (tmp1->depth>tmp2->depth)
return 0;
}
return 1;
}
/* mplrs.cobasis_list may have things to send to the master.
* Send the header, and then the cobases to add to L.
*/
void return_unfinished_cobases(void)
{
int listsize;
slist *list, *next;
int *lengths=NULL;
long *depths=NULL;
char *cobases=NULL;
cobs *tmpc;
int size = 0;
int i;
int start;
/* header is (strlen(cobases), length of lengths, mplrs.my_tag) */
int *header = malloc(sizeof(int)*3);
msgbuf *msg = malloc(sizeof(msgbuf));
msg->target = MASTER;
for (listsize=0, list=mplrs.cobasis_list; list; list=list->next)
{
listsize++;
size += strlen((char *)((cobs*)list->data)->str);
}
#if 0
/* TODO REMOVE THIS - workers always sorted, so not needed */
if (mplrs.minheight == 1 && listsize>1 &&
!worker_sorted()) /*lists size <2 already sorted*/
worker_sort_cobases(listsize);
#endif
if (listsize == 0)
{
header[0] = -1;
header[1] = -1;
header[2] = -1;
msg->buf = malloc(sizeof(void *));
msg->buf[0] = header;
msg->count = 1;
msg->req = malloc(sizeof(MPI_Request));
msg->queue = 0;
msg->tags = NULL;
msg->sizes = NULL;
msg->types = NULL;
msg->current_count = NULL;
MPI_Isend(header, 3, MPI_INT, MASTER, 10, MPI_COMM_WORLD,
msg->req);
msg->next = mplrs.outgoing;
mplrs.outgoing = msg;
return;
}
lengths = malloc(sizeof(int)*listsize); /*allows unconcatenate*/
depths = malloc(sizeof(long)*listsize);
cobases = malloc(sizeof(char)*(size+1));/*concatenated + 1 \0*/
for (start=0, i=0, list=mplrs.cobasis_list; list; list=next, i++)
{
next = list->next;
tmpc = list->data;
strcpy(cobases+start, tmpc->str);
lengths[i] = strlen(tmpc->str);
depths[i] = tmpc->depth;
start+=lengths[i];
free(tmpc->str);
free(list->data);
free(list);
}
/* final \0 is there */
header[0] = listsize;
header[1] = size+1;
header[2] = mplrs.my_tag;
msg->req = malloc(sizeof(MPI_Request) * 4);
msg->buf = malloc(sizeof(void *) * 4);
msg->buf[0] = header;
msg->buf[1] = cobases;
msg->buf[2] = lengths;
msg->buf[3] = depths;
msg->count = 4;
msg->queue = 0;
msg->tags = NULL;
msg->sizes = NULL;
msg->types = NULL;
msg->current_count = NULL;
mprintf2(("%d: Queued send of %d cobases for L\n",mplrs.rank,listsize));
MPI_Isend(header, 3, MPI_INT, MASTER, 10, MPI_COMM_WORLD, msg->req);
MPI_Isend(cobases, header[1], MPI_CHAR, MASTER, mplrs.my_tag,
MPI_COMM_WORLD, msg->req+1);
MPI_Isend(lengths, listsize, MPI_INT, MASTER, mplrs.my_tag,
MPI_COMM_WORLD, msg->req+2);
MPI_Isend(depths, listsize, MPI_LONG, MASTER, mplrs.my_tag,
MPI_COMM_WORLD, msg->req+3);
mplrs.my_tag++;
msg->next = mplrs.outgoing;
mplrs.outgoing = msg;
mplrs.cobasis_list = NULL;
}
/* dest is a string in a buffer with size *size.
* Append src and a newline to the string, realloc()ing as necessary,
* returning the new pointer and updating size.
*/
char *append_out(char *dest, int *size, const char *src)
{
int len1 = strlen(dest);
int len2 = strlen(src);
unsigned int newsize = *size;
char *newp = dest;
if (src[len2-1]=='\n') /* remove trailing \n, added below */
len2--;
if (len1 + len2 + 2 > *size)
{
newsize = newsize<<1;
while ((newsize < len1+len2+2) && newsize)
newsize = newsize<<1;
if (!newsize)
newsize = len1+len2+2;
newp = realloc(dest, sizeof(char) * (newsize+4));
if (!newp)
{
newsize = len1+len2+2;
newp = realloc(dest, sizeof(char) * newsize);
if (!newp)
{
printf("%d: Error no memory (%d)\n",mplrs.rank,
newsize);
/* MPI_Finalize(); */
exit(2);
}
}
*size = newsize;
}
strncat(newp, src, len2);
newp[len1+len2]='\n';
newp[len1+len2+1]='\0';
return newp;
}
/************
* consumer *
************/
int mplrs_consumer(void)
{
int i;
int check = 0;
initial_print(); /* print version and other information */
/* initialize MPI_Requests and 3*int buffers for incoming messages */
consumer.prodreq = malloc(sizeof(MPI_Request)*mplrs.size);
consumer.prodibf = malloc(sizeof(int)*7*mplrs.size);
consumer.num_producers = mplrs.size - 2;
consumer.overflow = malloc(sizeof(int)*mplrs.size);
if (mplrs.redund || mplrs.fel || mplrs.testlin) /* don't wait for a
* begin in these modes */
consumer.waiting_initial = 0;
for (i=0; i<mplrs.size; i++)
{
consumer.overflow[i] = 0;
if (i==CONSUMER)
continue;
MPI_Irecv(consumer.prodibf+(i*7), 7, MPI_INT, i, 7,
MPI_COMM_WORLD, consumer.prodreq+i);
}
/* final_print condition handles overflow cleanstop in initial
* job
*/
while (consumer.num_producers>0 || consumer.incoming ||
(consumer.waiting_initial && consumer.final_print))
{
/*printf("%d %d %d %d\n", consumer.num_producers, consumer.incoming,
consumer.waiting_initial, consumer.final_print);
*/
/* check if someone is trying to send us output */
/* if so, queue any incoming messages */
consumer_start_incoming();
/* check for completed message to process */
consumer_proc_messages();
/* check signals */
mplrs_handlesigs();
}
mprintf2(("C: getting stats and exiting\n"));
for (i=0; i<mplrs.size; i++)
{
if (i==CONSUMER || i==MASTER)
continue;
recv_counting_stats(i);
}
free(consumer.prodreq);
free(consumer.prodibf);
consumer.prodreq = NULL; consumer.prodibf = NULL;
mprintf2(("C: checking with master whether checkpointing ...\n"));
MPI_Recv(&check, 1, MPI_INT, MASTER,1,MPI_COMM_WORLD,MPI_STATUS_IGNORE);
mprintf2(("C: checkpointing flag is %d\n", check));
if (check == CHECKFLAG)
return consumer_checkpoint();
recv_master_stats(); /* gets stats on size of L, etc */
if (consumer.final_print)
final_print();
free(mplrs.input); /* must be after final_print, for redund check */
if (consumer.output!=stdout)
fclose(consumer.output);
free(consumer.overflow);
free(mplrs.tfn);
if (mplrs.R != NULL) /* redund final_print calls mplrs_worker_init */
{
free(mplrs.R->redineq);
free(mplrs.R->facet);
free(mplrs.R);
}
free(consumer.redineq);
mplrs_freemps();
free(mplrs.finalwarn);
free(mplrs.curwarn);
MPI_Finalize();
return 0;
}
/* check if anyone is trying to send us their output */
/* if master is trying, probably means we're going to checkpoint */
/* in any case, queue any incoming messages */
void consumer_start_incoming(void)
{
int i;
int flag;
for (i=0; i<mplrs.size; i++)
{
/* don't check ourself and workers that have exited */
if (i==CONSUMER || consumer.prodreq[i] == MPI_REQUEST_NULL)
continue;
MPI_Test(consumer.prodreq+i, &flag, MPI_STATUS_IGNORE);
if (!flag) /* not incoming, check next */
continue;
mprintf3(("C: received message from %d\n",i));
if (i==MASTER && consumer.prodibf[0]==STOPFLAG)
{
/* reachable, since consumer must know if
* overflow happens in initial job ... */
consumer.final_print = 0;
mprintf(("C: stopping...\n"));
continue;
}
else if (i==MASTER && consumer.prodibf[0]==RESTARTFLAG)
{
/* get counting stats for restart */
recv_counting_stats(MASTER);
consumer.waiting_initial = 0;
mprintf(("C: Restarted\n"));
/* master may restart and later checkpoint */
MPI_Irecv(consumer.prodibf+(i*7), 7, MPI_INT, i, 7,
MPI_COMM_WORLD, consumer.prodreq+i);
continue;
}
if (consumer.prodibf[7*i]<=0 && consumer.prodibf[7*i+1]<=0)
{
/* producer i has finished and will exit */
consumer.num_producers--;
mprintf(("C: Producer %d reported exiting, %d left.\n",
i, consumer.num_producers));
continue;
}
/* otherwise, we have the normal situation -- the producer
* wants to send us some output.
*/
consumer.incoming =
consumer_queue_incoming(consumer.prodibf+7*i, i);
MPI_Irecv(consumer.prodibf+(i*7), 7, MPI_INT, i, 7,
MPI_COMM_WORLD, consumer.prodreq+i);
}
}
/* target has sent us this header. Queue the corresponding receive and
* return the new value of consumer.incoming.
*/
msgbuf *consumer_queue_incoming(int *header, int target)
{
msgbuf *curhead = consumer.incoming;
msgbuf *newmsg = malloc(sizeof(msgbuf));
newmsg->req = malloc(sizeof(MPI_Request)*2);
newmsg->buf = malloc(sizeof(void *));
newmsg->buf[0] = malloc(sizeof(char)*(header[1]+1));
newmsg->count = 1;
newmsg->target = target;
newmsg->next = curhead;
newmsg->current_count = malloc(sizeof(long)*4);
newmsg->queue = 0;
newmsg->tags = NULL;
newmsg->sizes = NULL;
newmsg->types = NULL;
newmsg->current_count[0] = header[3];
newmsg->current_count[1] = header[4];
newmsg->current_count[2] = header[5];
newmsg->current_count[3] = header[6];
newmsg->data = header[0]; /* bound for stdout or output file */
/* get my_tag from producer via header[2] to uniquely identify msg */
MPI_Irecv(newmsg->buf[0], header[1]+1, MPI_CHAR, target, header[2],
MPI_COMM_WORLD, newmsg->req);
mprintf3(("C: Receiving from %d (%d,%d,%d)",target,header[0],header[1],
header[2]));
return newmsg;
}
/* update consumer.redineq with the redundant inequalities in rstring */
/* tag with rjobcount, used for an optimization
*/
void consumer_process_redund(const char *rstring)
{
const char *start=rstring;
char *endptr=NULL;
long index, value;
/* int i=0; */
mprintf3(("C: processing redund_string %s\n", rstring));
do {
index = strtol(start, &endptr, 10);
if (index!=0)
{
start = endptr;
value = strtol(start, &endptr, 10);
mprintf3(("C: got redundant inequality %ld %ld\n",
index, value));
/* i++; */
if (value == 1)
consumer.redineq[index] = consumer.rjobcount;
else
consumer.redineq[index] = value;
start = endptr;
}
} while (index!=0);
/*
mprintf2(("C: got %d redundant inequalities tagged %llu\n", i,
consumer.rjobcount));
*/
consumer.rjobcount++;
}
#define dig_advance() \
for (i++;i<len;i++) if (output[i]!='#'&&!isdigit((int)output[i])) break;
/* Renumber any V# F# B# etc in output, send it to consumer.output .
* counts give the final counts for this subjob, needed to update
* the consumer's overall count used when renumbering.
*/
void consumer_renumber(char *output, long *counts)
{
long i, len=strlen(output);
long rays = counts[0]; /* #rays / #facets */
long vertices = counts[1];
long bases = counts[2];
/* int hull = counts[3]; */
unsigned long tmp;
int ret;
int brk = !strcmp(output, "?"); /* extra message to get
* running tally, don't print */
for (i=0; i<len; i++)
{
if (brk)
break;
putc(output[i], consumer.output);
switch (output[i])
{
case '\0':
break;
case 'V':
ret = sscanf(output+i+1, "#%lu", &tmp);
if (ret!=1)
break;
fprintf(consumer.output, "#%lu",
tmp+consumer.vertices);
dig_advance();
i--;
break;
case 'B':
ret = sscanf(output+i+1, "#%lu", &tmp);
if (ret!=1)
break;
fprintf(consumer.output, "#%lu",
tmp+consumer.bases);
dig_advance();
i--;
break;
case 'F': /* overloaded in lrslib, rays/facets */
case 'R': /* share a count */
ret = sscanf(output+i+1, "#%lu", &tmp);
if (ret!=1)
break;
fprintf(consumer.output, "#%lu",
tmp+consumer.rays);
dig_advance();
i--;
break;
default:
break;
}
}
/* update consumer's overall counts */
consumer.rays += rays;
consumer.vertices += vertices;
consumer.bases += bases;
}
/* check our incoming messages, process and remove anything that
* has completed
*/
void consumer_proc_messages(void)
{
msgbuf *msg, *prev=NULL, *next;
int i,len,omit;
for (msg=consumer.incoming; msg; msg=next)
{
omit = 0;
next=msg->next;
if (outgoing_msgbuf_completed(msg))
{
if (consumer.waiting_initial &&
consumer.final_print &&
msg->target != INITIAL && msg->target != MASTER)
{
prev = msg; /* 2020.5.29 need to update prev
* so we don't lose this msg */
continue;
}
/* final_print condition is false when overflow
* has occurred. No longer important to print all
* output, but need to print or discard it in case
* overflow prevents us from seeing "begin"
*/
/* we wait on all other output until we've printed
* the initial output containing ...begin\n
* to ensure pretty output, if this is the begin,
* flip the flag.
*/
if (consumer.waiting_initial == 1 && msg->target == INITIAL)
{
len=strlen((char *)msg->buf[0])-5;
for (i=0; i<len; i++)
if (!strncmp("begin\n",
(char*)msg->buf[0]+i,6))
{
mprintf3(("C: found begin\n"));
phase1_print();
consumer.waiting_initial = 0;
break;
}
if (consumer.waiting_initial) /* no begin here*/
{ /* can happen in 7.1 */
prev=msg; /* wait for a begin line */
continue;
}
}
else if (consumer.waiting_initial == 2 && msg->target == INITIAL)
{ /* could combine with other condition above */
/* maybe easier to read if separate */
prev = msg; /* 2020.5.29 same here*/
continue; /* wait for master to report warnings */
}
else if (consumer.waiting_initial == 2 && msg->target == MASTER)
{
consumer.waiting_initial = 1;
/* if no master warning messages produced,
* master sends a space to prod us along.
* don't print that space.
*/
if (!strcmp(msg->buf[0], " \n"))
omit = 1;
}
/* print the 'begin' only after phase1_print */
if (msg->data == 1 && !omit)
{
if (mplrs.renumber == 0) /* normal */
fprintf(consumer.output, "%s",
(char*)msg->buf[0]);
else
consumer_renumber((char*)msg->buf[0],
msg->current_count);
/* flush to get more streaminess to output
* file and not break inside a line, as
* requested
*/
fflush(consumer.output);
}
else if (msg->data == 2 && !omit)
{
if (!consumer.oflow_flag)
{
consumer.oflow_flag = 1;
fprintf(stderr,"%s",(char*)msg->buf[0]);
}
}
else if (msg->data == 3 && !omit) /* redund string */
consumer_process_redund((char*)msg->buf[0]);
else if (!omit) /* headed to stderr */
fprintf(stderr, "%s", (char*)msg->buf[0]);
free_msgbuf(msg);
if (prev)
prev->next = next;
else
consumer.incoming = next;
continue;
}
prev=msg;
}
}
/* We are checkpointing instead of a normal exit.
* Send counting stats to master, then exit quietly
*/
int consumer_checkpoint(void)
{
int len;
char *str;
char *vol = cprat("", mplrs.Vnum, mplrs.Vden);
send_counting_stats(MASTER);
mprintf(("C: in consumer_checkpoint\n"));
MPI_Recv(&len, 1, MPI_INT, MASTER, 1, MPI_COMM_WORLD,
MPI_STATUS_IGNORE);
if (len == -1) /* master produces checkpoint file */
{
mplrs_freemps();
MPI_Finalize();
return 0;
}
fprintf(consumer.output, "*Checkpoint file follows this line\n");
fprintf(consumer.output, "mplrs5\n%llu %llu %llu %llu %llu\n%s\n%llu\n%llu\n",
mplrs.rays, mplrs.vertices, mplrs.bases, mplrs.facets,
mplrs.intvertices,vol,mplrs.deepest,mplrs.deepest_vertex);
free(vol);
while (1)
{
MPI_Recv(&len, 1, MPI_INT, MASTER, 1, MPI_COMM_WORLD,
MPI_STATUS_IGNORE);
if (len<0)
break;
str = malloc(sizeof(char)*len);
MPI_Recv(str, len, MPI_CHAR, MASTER, 1, MPI_COMM_WORLD,
MPI_STATUS_IGNORE);
fprintf(consumer.output,"%s\n",str);
free(str);
}
fprintf(consumer.output,"*Checkpoint finished above this line\n");
mplrs_freemps();
MPI_Finalize();
return 0;
}
/* check whether this outgoing msgbuf has completed.
* If the msg is queued (msg->queue == 1),
* then if the first part has completed, send the remaining parts
* Don't use with *queued* incoming msgbuf.
*/
int outgoing_msgbuf_completed(msgbuf *msg)
{
int flag;
int count = msg->count;
int i;
if (msg->queue != 1)
{
MPI_Testall(count, msg->req, &flag, MPI_STATUSES_IGNORE);
return flag;
}
MPI_Test(msg->req, &flag, MPI_STATUS_IGNORE);
if (!flag)
return flag;
/* first completed, send the rest of the queued send */
mprintf3(("%d: Sending second part of queued send to %d\n",
mplrs.rank, msg->target));
for (i=1; i<count; i++)
{
if (msg->buf[i])
MPI_Isend(msg->buf[i], msg->sizes[i], msg->types[i],
msg->target, msg->tags[i], MPI_COMM_WORLD,
msg->req+i);
}
msg->queue = 0;
return 0;
}
void free_msgbuf(msgbuf *msg)
{
int i;
for (i=0; i<msg->count; i++)
free(msg->buf[i]);
free(msg->buf);
free(msg->req);
free(msg->tags);
free(msg->sizes);
free(msg->types);
free(msg->current_count);
free(msg);
return;
}
/* send stats on size of L, etc */
void send_master_stats(void)
{
unsigned long stats[4] = {master.tot_L, master.num_empty, 0, 0};
mprintf3(("M: Sending master_stats to consumer\n"));
MPI_Send(stats, 4, MPI_UNSIGNED_LONG, CONSUMER, 1, MPI_COMM_WORLD);
return;
}
/* get master stats on size of L, etc */
void recv_master_stats(void)
{
unsigned long stats[4] = {0,0,0,0};
MPI_Recv(stats, 4, MPI_UNSIGNED_LONG, MASTER, 1, MPI_COMM_WORLD,
MPI_STATUS_IGNORE);
master.tot_L = stats[0];
master.num_empty = stats[1];
return;
}
/* send stats to target for final print */
void send_counting_stats(int target)
{
char *vol = cprat("", mplrs.Vnum, mplrs.Vden);
unsigned long long stats[11] = {mplrs.rays, mplrs.vertices, mplrs.bases,
mplrs.facets, mplrs.intvertices,
strlen(vol)+1, mplrs.deepest, mplrs.overflow,
mplrs.linearities, strlen(mplrs.finalwarn)+1,
mplrs.deepest_vertex};
mprintf3(("%d: sending counting stats to %d\n", mplrs.rank, target));
MPI_Send(stats, 11, MPI_UNSIGNED_LONG_LONG, target, 1, MPI_COMM_WORLD);
MPI_Send(vol, stats[5], MPI_CHAR, target, 1, MPI_COMM_WORLD);
MPI_Send(mplrs.finalwarn, stats[9], MPI_CHAR, target, 1, MPI_COMM_WORLD);
free(vol);
return;
}
/* gets counting stats from target */
void recv_counting_stats(int target)
{
char *vol, *finalwarn;
unsigned long long stats[11];
MPI_Recv(stats, 11, MPI_UNSIGNED_LONG_LONG, target, 1, MPI_COMM_WORLD,
MPI_STATUS_IGNORE);
mprintf3(("%d: got counting stats from %d\n", mplrs.rank, target));
mplrs.rays+=stats[0];
mplrs.vertices+=stats[1];
mplrs.bases+=stats[2];
mplrs.facets+=stats[3];
mplrs.intvertices+=stats[4];
if (stats[6] > mplrs.deepest)
mplrs.deepest = stats[6];
if (stats[10] > mplrs.deepest_vertex)
mplrs.deepest_vertex = stats[10];
if (mplrs.rank == CONSUMER)
consumer.overflow[target] = stats[7];
if (stats[8] > mplrs.linearities)
mplrs.linearities = stats[8];
vol = malloc(sizeof(char)*stats[5]);
MPI_Recv(vol, stats[5], MPI_CHAR, target, 1, MPI_COMM_WORLD,
MPI_STATUS_IGNORE);
/* following safe even #ifdef LRSLONG, then always 0/1 */
plrs_readrat(mplrs.Tnum, mplrs.Tden, vol);
copy(mplrs.tN, mplrs.Vnum); copy(mplrs.tD, mplrs.Vden);
linrat(mplrs.tN, mplrs.tD, 1L, mplrs.Tnum, mplrs.Tden,
1L, mplrs.Vnum, mplrs.Vden);
free(vol);
finalwarn = malloc(sizeof(char)*stats[9]);
MPI_Recv(finalwarn, stats[9], MPI_CHAR, target, 1, MPI_COMM_WORLD,
MPI_STATUS_IGNORE);
if (stats[9]>2)
mplrs.finalwarn = append_out(mplrs.finalwarn,
&mplrs.finalwarn_len,
finalwarn);
free(finalwarn);
return;
}
/* print who we are */
void id_print(FILE *f)
{
fprintf(f, "*mplrs:%s%s(", TITLE, VERSION);
#ifdef B128
fprintf(f, "128bit");
#else
fprintf(f, "64bit");
#endif
#ifdef MA
fprintf(f, ",hybrid arithmetic");
#endif
#ifdef GMP
#ifdef MGMP
fprintf(f, ",mini-gmp");
#else
fprintf(f, ",gmp v.%d.%d",__GNU_MP_VERSION,__GNU_MP_VERSION_MINOR);
#endif
#endif
#ifdef MP
fprintf(f, ",lrsmp");
#endif
#ifdef FLINT
fprintf(f, ",%dbit flint v.%s",FLINT_BITS,FLINT_VERSION);
#endif
#ifndef MA
#ifdef SAFE
fprintf(f, ",overflow checking");
#else
fprintf(f, ",no overflow checking");
#endif
#endif
fprintf(f, ")%d processes\n", mplrs.size);
}
/* do the initial print */
void init_print(FILE *f)
{
id_print(f);
fprintf(f, "*Input taken from %s\n",
mplrs.input_filename);
if (f == stdout && consumer.output!=stdout)
fprintf(f,"*Output written to: %s\n",consumer.output_filename);
if (mplrs.redund == 0)
{
fprintf(f, "*Starting depth of %d maxcobases=%d ",
master.initdepth, master.maxcobases);
fprintf(f, "maxdepth=%d lmin=%d lmax=%d scale=%d\n",
master.maxdepth, master.lmin,
master.lmax, master.scalec);
if (mplrs.countonly)
fprintf(f, "*countonly\n");
}
else
fprintf(f, "*rows=%u lastp=%u lastrows=%u j=%u\n", master.rows,
master.lastp, master.lastrows, master.j);
if (mplrs.redund && !mplrs.fel)
fprintf(f, "*minrep\n");
}
void initial_print(void)
{
init_print(consumer.output);
if (consumer.output==stdout)
return;
init_print(stdout);
}
/* do the "*Phase 1 time: " print */
void phase1_print(void)
{
struct timeval cur;
gettimeofday(&cur, NULL);
fprintf(consumer.output, "*Phase 1 time: %ld seconds.\n",
cur.tv_sec-mplrs.start.tv_sec);
if (consumer.output_filename == NULL)
return;
printf("*Phase 1 time: %ld seconds.\n",cur.tv_sec-mplrs.start.tv_sec);
return;
}
/* set R->redineq using consumer.redineq, for final check */
void consumer_setredineq(void)
{
int i, m, max, maxi;
int *counts = calloc(consumer.rjobcount, sizeof(int));
m = mplrs.P->m_A;
if (mplrs.fel) /* get bigger m */
{
m = consumer.m;
mplrs.R->m = m;
}
/* hack output to file (if using file).
* done here in case of re-init on overflow (eg redund on mit.ine)
*/
lrs_ofp = consumer.output;
/* optimization: with current way of splitting redund
* we can choose the redund job (maxi) that produced the most redundant
* inequalities and not recheck those at the end
*/
for (i=1; i<=m; i++)
{
if (mplrs.testlin || mplrs.nohiddenl)
{ /* we track which job produced the item - any positive number was a 1 */
if (consumer.redineq[i]>0) /* so reset those to definitely redundant here */
consumer.redineq[i] = -1;
}
if (consumer.redineq[i]>0)
counts[consumer.redineq[i]]++;
}
max = -1;
maxi = -1; /* for warning removal only */
for (i=0; i<consumer.rjobcount; i++)
if (counts[i]>max)
{
max=counts[i];
maxi = i;
}
free(counts);
mprintf(("C: resetting redineq for final check (maxi:%d,max:%d) on:",
maxi,max));
for (i=1; i<=m; i++)
{
if (consumer.redineq[i] == -1)
mplrs.R->redineq[i] = -1;
/* linearities sent as -2 to avoid confusion with proc 2 */
else if (consumer.redineq[i] == -2)
mplrs.R->redineq[i] = 2;
else if (consumer.redineq[i] == maxi && max>0) /* DA: max=0 means no redundancies found*/
mplrs.R->redineq[i] = -1;
else if (consumer.redineq[i] > 0)
{
mplrs.R->redineq[i] = 1;
mprintf((" %d", i));
}
else if (consumer.redineq[i] == 0)
mplrs.R->redineq[i] = 0;
else
mprintf(("C: don't know what to do about %ld\n",
consumer.redineq[i]));
}
mprintf(("\n"));
}
/* do the final print */
void final_print(void)
{
char *argv[] = {argv0};
struct timeval end;
char *vol=NULL;
#ifdef MA
int i, num64=0, num128=0, numgmp=0;
#endif
if (mplrs.redund)
{
mplrs_worker_init();
consumer_setredineq();
mprintf(("C: calling lrs_main for final redund check\n"));
lrs_ofp = consumer.output; /* HACK to get redund output in
* output file ... */
consumer.final_redundcheck = 1;
run_lrs(1, argv, 0, 1, NULL, NULL, NULL);
mprintf(("C: lrs_main returned from final redund check\n"));
if (mplrs.overflow != 3)
mplrs.lrs_main(0,NULL,&mplrs.P,&mplrs.Q,0,2,NULL,mplrs.R);
}
else if (!mplrs.testlin)
fprintf(consumer.output, "end\n");
if (strlen(mplrs.finalwarn)>1) /*avoid spurious newline from append_out*/
fprintf(consumer.output, "%s", mplrs.finalwarn);
if (consumer.felfallback) /* was a fel run, but hidden linearities so
* did minrep instead. tell the user to avoid
* confusion.
*/
{
fprintf(consumer.output, "*Input had hidden linearities so a minimum representation was output\n");
fprintf(consumer.output, "*Rerun with this file to do projections\n");
if (consumer.output_filename != NULL)
{
printf("*Input had hidden linearities so a minimum representation was output\n");
printf("*Rerun with this file to do projections\n");
}
}
/* after the (expensive) final redund check */
gettimeofday(&end, NULL);
fprintf(consumer.output, "\n*Total number of jobs: %lu, L became empty %lu times, tree depth %llu, deepest output depth %llu\n", master.tot_L, master.num_empty,mplrs.deepest,mplrs.deepest_vertex);
if (consumer.output_filename != NULL)
printf("\n*Total number of jobs: %lu, L became empty %lu times, tree depth %llu, deepest vertex depth %llu\n",
master.tot_L, master.num_empty,mplrs.deepest, mplrs.deepest_vertex);
#ifdef MA
for (i=0; i<mplrs.size; i++)
{
if (i==MASTER || i==CONSUMER)
continue;
if (consumer.overflow[i]==0)
num64++;
else if (consumer.overflow[i]==1)
num128++;
else
numgmp++;
}
#ifdef B128
fprintf(consumer.output, "*Finished with %d 64-bit, %d 128-bit, %d GMP workers\n",
num64, num128, numgmp);
if (consumer.output_filename != NULL)
printf("*Finished with %d 64-bit, %d 128-bit, %d GMP workers\n",
num64, num128, numgmp);
#else
fprintf(consumer.output, "*Finished with %d 64-bit, %d GMP workers\n",
num64, numgmp);
if (consumer.output_filename != NULL)
printf("*Finished with %d 64-bit, %d GMP workers\n",
num64, numgmp);
#endif
#endif
if (mplrs.facets>0)
{
if(!zero(mplrs.Vnum))
{
vol = cprat("*Volume=",mplrs.Vnum,mplrs.Vden);
fprintf(consumer.output,"%s\n",vol);
free(vol);
}
fprintf(consumer.output,"*Totals: facets=%llu bases=%llu",
mplrs.facets, mplrs.bases);
if (mplrs.linearities > 0)
fprintf(consumer.output,
" linearities=%llu facets+linearities=%llu",
mplrs.linearities,mplrs.linearities+mplrs.facets);
fputc('\n', consumer.output);
}
else if (mplrs.redund == 0 && (mplrs.rays+mplrs.vertices>0))
/*DA: seems like V-rep output not redund!?*/
{ /* yes, H-rep output is above, V-rep here,
* just 'else' would also get redund runs */
fprintf(consumer.output, "*Totals: vertices=%llu rays=%llu bases=%llu integer-vertices=%llu",
mplrs.vertices,mplrs.rays,mplrs.bases,mplrs.intvertices);
if (mplrs.linearities > 0)
fprintf(consumer.output,
" linearities=%llu", mplrs.linearities);
if (mplrs.rays+mplrs.linearities>0)
{
fprintf(consumer.output," vertices+rays");
if (mplrs.linearities>0)
fprintf(consumer.output, "+linearities");
fprintf(consumer.output, "=%llu",
mplrs.vertices+mplrs.rays+mplrs.linearities);
}
fputc('\n', consumer.output);
}
id_print(consumer.output);
fprintf(consumer.output, "*Elapsed time: %ld seconds.\n",
end.tv_sec - mplrs.start.tv_sec);
if (consumer.output_filename == NULL)
return;
if (mplrs.facets>0)
{
if (!zero(mplrs.Vnum))
{
vol = cprat("*Volume=",mplrs.Vnum,mplrs.Vden);
printf("%s\n", vol);
free(vol);
}
printf("*Totals: facets=%llu bases=%llu", mplrs.facets,
mplrs.bases);
if (mplrs.linearities > 0)
printf(" linearities=%llu facets+linearities=%llu",
mplrs.linearities,mplrs.linearities+mplrs.facets);
putchar('\n');
}
else if (mplrs.redund == 0 && (mplrs.rays+mplrs.vertices>0))
{
printf("*Totals: vertices=%llu rays=%llu bases=%llu integer-vertices=%llu",
mplrs.vertices,mplrs.rays,mplrs.bases,mplrs.intvertices);
if (mplrs.linearities > 0)
printf(" linearities=%llu", mplrs.linearities);
if (mplrs.rays+mplrs.linearities>0)
{
printf(" vertices+rays");
if (mplrs.linearities>0)
printf("+linearities");
printf("=%llu",
mplrs.vertices+mplrs.rays+mplrs.linearities);
}
putchar('\n');
}
id_print(stdout);
printf("*Elapsed time: %ld seconds.\n", end.tv_sec - mplrs.start.tv_sec);
}
/* create a new string version of the input (which is length length),
* add "testlin" option before begin.
* note: lrs is happy with input, so it has a begin line, etc.
*/
char *add_testlin_option(char *input, int length)
{
long i, j, line=0;
char *ret = malloc(sizeof(char)*(length+9));
for (i=0,j=0; i<length; )
{
if (line==0)
{
do {
ret[j++] = input[i];
} while (input[i++]!='\n');
line=1;
continue;
}
if (!strncmp(input+i,"begin",5))
{
sprintf(ret+j, "testlin\nb");
j+=9;
i++;
continue;
}
ret[j++] = input[i++];
}
ret[j] = '\0';
mprintf3(("added testlin to file as follows: %s\n", ret));
return ret;
}
/* input is an lrs input file, length n, option is a string.
* any occurence of "option" after the first line is replaced by
* "*ption"
*/
void remove_option(char *input, long n, const char *option)
{
long i, line = 0, len=strlen(option);
for (i=0; i<n; i++)
{
if (line==0) /* ignore first line */
{
if (input[i]=='\n')
line=1;
continue;
}
if (!strncmp(input+i, option, len))
input[i]='*';
}
}
void open_outputblock(void)
{
mplrs.outputblock++;
}
void close_outputblock(void)
{
mplrs.outputblock--;
if (okay_to_flush())
{
process_output(); /* before starting a flush */
clean_outgoing_buffers();
}
}
/* the condition on whether we should do a maxbuf-based
* flush of the output. Bit complicated and used twice
* so broken out here.
*/
int okay_to_flush(void)
{
return
(mplrs.outnum++ > mplrs.maxbuf && /* buffer <maxbuf output lines */
mplrs.outputblock <= 0 && /* don't flush if open output block */
mplrs.initializing != 1 && /* don't flush in phase 1 */
mplrs.rank != MASTER && /* need initial warnings together */
mplrs.renumber == 0 /* avoid flushing to avoid hassle with counts */
#ifdef MA
&& mplrs.overflow == 2 /* avoid duplicate output lines if
* overflow possible, only flush when
* using GMP; hurts performance
*/
#endif
);
/* not in phase 1 (need 'begin' first) */
/* must not flush during phase 1 to avoid multiple 'begin'
* statements. otherwise, an overflow could happen after
* the flush and we'd produce another 'begin'
*/
/* master doesn't flush here to ensure one block with any
* messages from startup, avoiding splitting warnings around 'begin'
*/
}
void mplrs_init_tfn(char *tim1)
{
int i,j;
char c;
j = mplrs.size;
for (i=1; j>9; i++)
j = j/10;
i += 6+strlen(tim1); /* mplrs_TIMESTAMP */
i += strlen(mplrs.tfn_prefix) + strlen(mplrs.input_filename) +
i + 6; /* _%d.ine\0 */
mplrs.tfn = malloc(sizeof(char) * i);
sprintf(mplrs.tfn, "%smplrs_%s%s_%d.ine",
mplrs.tfn_prefix, tim1,
mplrs.input_filename,
mplrs.rank);
/* flatten directory structure in mplrs.input_filename
* for mplrs.tfn, to prevent writing to non-existent
* subdirectories in e.g. /tmp
*/
i = strlen(mplrs.tfn_prefix) + 6 + strlen(tim1);
j = strlen(mplrs.tfn);
for (; i<j; i++)
{
c = mplrs.tfn[i];
if (c == '/' || c == '\\')
mplrs.tfn[i] = '_';
}
return;
}
void post_output(const char *type, const char *data)
{
outlist *out;
out = malloc(sizeof(outlist));
out->type = dupstr(type);
out->data = dupstr(data);
out->next = NULL;
if (mplrs.output_list == NULL)
mplrs.output_list = out;
else
mplrs.ol_tail->next = out;
mplrs.ol_tail = out;
if ((okay_to_flush() && data[strlen(data)-1]=='\n') ||
!strcmp(type,"flush")) /* "flush" flushes always, no matter what*/
{
process_output(); /* before starting a flush */
clean_outgoing_buffers();
}
}
/* strdup */
char *dupstr(const char *str)
{
char *buf = malloc(sizeof(char)*(strlen(str)+1));
strcpy(buf,str);
return buf;
}
|