agents-splitgo-mpi.c 64.8 KB
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
#include <pthread.h>

#include <mpi.h>

#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <errno.h>
#include <semaphore.h>

#include <assert.h>

#include "fdc_int.h"
#include "variables.h"
#include "values.h"
#include "store.h"
#include "splitting.h"
#include "bound.h"

#include "util.h"

#ifndef COMPACT_DOMAINS
#error "only works with COMPACT_DOMAINS"
#endif

#if !defined(PACK_PROBLEM) || !defined(USE_STORE)
#error "must PACK_PROBLEM and USE_STORE"
#endif

/* sizes are taken from the MPI-2 standard */
#if UNIT_BITS == 64
   /* some MPI implementations, such as New Madeleine's, don't have
      MPI_UNSIGNED_LONG_LONG */
#  ifndef MPI_UNSIGNED_LONG_LONG
#    ifndef MPI_LONG_LONG
#      warning "neither MPI_UNSIGNED_LONG_LONG nor MPI_LONG_LONG are defined"
#    endif

#    define MPI_DOMAIN_TYPE MPI_LONG_LONG
#  else
#    define MPI_DOMAIN_TYPE MPI_UNSIGNED_LONG_LONG
#  endif
#elif UNIT_BITS == 32
#  define MPI_DOMAIN_TYPE MPI_UNSIGNED_LONG
#else
#  error "don't know which MPI type to use for MPI_DOMAIN_TYPE"
#endif

#define MAX_AGENTS 1024

// number of workers to use
int fd__workers = -1;

// events within the main loop
#define EV_INT_SUCCESS	0x001	  // an agent found a solution
#define EV_INT_FAIL	0x002	  // an agent exhausted its search space
#define EV_EXT_SUCCESS	0x010	  // another process found a solution
#define EV_EXT_REQ_WORK	0x020	  // another process exhausted its search space
#define EV_EXT_NO_WORK	0x040	  // there is no work to give to another process
#define EV_EXT_STORE	0x080	  // this process received more work
#define EV_EXT_FAIL	0x100	  // there is no more work available
#define EV_EXT_DONE	0x200	  // the search has succeeded and should stop
#define EV_EXT_QUIT	0x300	  // no more work is available for sharing
#define EV_EXT_READY	0x600	  // process has no work and nothing pending

#define EV_EXT_BOUND	0x500	  // best value of the objective function

#define EV_INT_COUNT	0x004	  // solutions count from an agent
#define EV_EXT_COUNT	0x400	  // solutions count from a process

#define EV_EXT_REQ_SHARE 0x030	  // a process couldn't find work
#define EV_EXT_NO_SHARE	0x050	  // a process has no work to share

#define EV_INT_TIMEOUT	0x008	  // some timeout

#define WAIT_NORM 1000000L // pause between event probes (1ms)
#define WAIT_TOUT  250000L // pause between event probes on a deadline (0.25ms)

#define FEED1_TIMEOUT 20 // ms to wait for the reply to the 1st request for work
#define FEED2_TIMEOUT 10 // ms to wait for the reply to the 2nd request for work
#define POLL_TIMEOUT  10 // ms to wait for the reply to a poll for work

#define millisleep(ms) do {						\
			 struct timespec ts = { 0, ms * 1000000 };	\
			 nanosleep(&ts, NULL);				\
		       } while (0)

#ifdef STATS_MSGS
static unsigned long _fd_msgs_received = 0, _fd_msgs_sent = 0;

static int _fd_MPI_Recv(void *b, int c, MPI_Datatype d, int s, int t,
			MPI_Comm w, MPI_Status *x)
{
  ++_fd_msgs_received;

  return MPI_Recv(b, c, d, s, t, w, x);
}
#define MPI_Recv _fd_MPI_Recv

static int _fd_MPI_Send(void *b, int c, MPI_Datatype d, int s, int t,
			MPI_Comm w)
{
  ++_fd_msgs_sent;

  return MPI_Send(b, c, d, s, t, w);
}
#define MPI_Send _fd_MPI_Send

static int _fd_MPI_Isend(void *b, int c, MPI_Datatype d, int s, int t,
			 MPI_Comm w, MPI_Request *r)
{
  ++_fd_msgs_sent;

  return MPI_Isend(b, c, d, s, t, w, r);
}
#define MPI_Isend _fd_MPI_Isend

static int _fd_MPI_Ssend(void *b, int c, MPI_Datatype d, int s, int t,
			 MPI_Comm w)
{
  ++_fd_msgs_sent;

  return MPI_Ssend(b, c, d, s, t, w);
}
#define MPI_Ssend _fd_MPI_Ssend

static int _fd_MPI_Issend(void *b, int c, MPI_Datatype d, int s, int t,
			  MPI_Comm w, MPI_Request *r)
{
  ++_fd_msgs_sent;

  return MPI_Issend(b, c, d, s, t, w, r);
}
#define MPI_Issend _fd_MPI_Issend

void _fd_statistics_msgs()
{
  extern int tid;

  fd__output("[%d] messages: %lu sent, %lu received\n", tid, _fd_msgs_sent,
	     _fd_msgs_received);
}
#else /* STATS_MSGS */
#define _fd_statistics_msgs() ((void) 0)
#endif /* STATS_MSGS */


static fd_int *_fd_copies[MAX_AGENTS];
static _fd_store _fd_agents_stores[MAX_AGENTS];
static _fd_store _fd_processes_stores[MAX_AGENTS]; // XXX: MAX_AGENTS!?
						   // only needed by process 0

// used to deliver the id of the agent
static int successful;

// used to signal the main thread that an agent has found a solution
// XXX: could be done through a (shared) variable?
static pthread_mutex_t success_mutex = PTHREAD_MUTEX_INITIALIZER;
static sem_t ready_semaphore, notify_semaphore;
// used when optimising, to release an agent after checking its solution
static sem_t resume_semaphore;
#if STEAL_WORK >= 2
// where agents will wait for more work
static pthread_mutex_t continue_mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t continue_cond = PTHREAD_COND_INITIALIZER;
#endif

// number of agents which will actually run (depends on the splitting)
static int agents_to_run;

extern unsigned long long _fd_count_solutions(fd_int[], int);

// used to accumulate the number of solutions found (when counting solutions)
static unsigned long long total_solutions = 0;

// record issued MPI requests, so they can be cleaned up before
// calling MPI_Finalize
// XXX: this list should be checked once in a while, otherwise it will
//      keep growing as the program runs
static fd_list pending_requests;

int tid;	// team ID // XXX: only used outside _fd_dsolve() for debugging

static void _fd_copy_problem(int n)
{
#ifndef PACK_PROBLEM
  int i, v;

  for (i = 0; i < n; ++i)
    {
      _fd_copies[i] = calloc(fd_variables_count, sizeof(fd_int)); // XXX: NULL?

      for (v = 0; v < fd_variables_count; ++v)
	_fd_copies[i][v] = _fd_var_copy(_fd_variables[v]);

      _fd_import_constraints(_fd_copies[i]);
    }
#else /* PACK_PROBLEM */
  int i;

  for (i = 0; i < n; ++i)
    _fd_copies[i] = _fd_variables; // XXX
#endif /* PACK_PROBLEM */
}

// XXX: turn the variables' domains into ``indexes'' into the store
static void _fd_despicable_hack(int n)
{
  store = _fd_agents_stores[n];
}

int _fd_agent(int n)
{
  if (!_fd_counting_solutions)
    {
      int result;
      struct timeval ti, tis, to, tos;

      fd__trace("[%d.%d] agent starting\n", tid, n);

      gettimeofday(&ti, NULL);

      _fd_despicable_hack(n);

      memcpy(_fd_variables, _fd_copies[n], fd_variables_count * sizeof(fd_int));

      // make sure agents release the mutexes they hold when cancelled
      pthread_cleanup_push((void (*)(void *)) pthread_mutex_unlock,
			   &success_mutex);
#if STEAL_WORK >= 2
      pthread_cleanup_push((void (*)(void *)) pthread_mutex_unlock,
			   &continue_mutex);
#endif

      do {
	gettimeofday(&tis, NULL);

	result = _fd_dsearch(_fd_copies[n], n);

	gettimeofday(&to, NULL);
	timersub(&to, &tis, &tos);
	timersub(&to, &ti, &to);
	fd__trace("[%d.%d] took %d.%06ds (%d.%06ds)\n", tid, n,
		  tos.tv_sec, tos.tv_usec, to.tv_sec, to.tv_usec);

	while (result == FD_OK)
	  {
	    fd__trace("[%d.%d] agent was successful\n", tid, n);

	    // ensure that only one agent signals its success
	    // XXX: only good (?) if only looking for the 1st solution
	    pthread_mutex_lock(&success_mutex);
	    fd__trace("[%d.%d] acquired success mutex\n", tid, n);

	    // tell the main thread that this agent is done
	    if (sem_wait(&ready_semaphore))
	      perror("sem_wait (ready)");

	    successful = n;

	    if (sem_post(&notify_semaphore))
	      perror("sem_post");

	    pthread_mutex_unlock(&success_mutex);
	    fd__trace("[%d.%d] released success mutex\n", tid, n);

	    if (!_fd_optimising)
	      break;

	    if (sem_wait(&resume_semaphore))
	      perror("sem_post (resume)");
	    fd__trace("[%d.%d] resuming, new bound is %d\n", tid, n, _fd_bound_value());

	    // find the next solution
	    gettimeofday(&tis, NULL);

	    result = _fd_dsearch_again(_fd_copies[n]);

	    gettimeofday(&to, NULL);
	    timersub(&to, &tis, &tos);
	    timersub(&to, &ti, &to);
	    fd__trace("[%d.%d] took %d.%06ds (%d.%06ds)\n", tid, n,
		      tos.tv_sec, tos.tv_usec, to.tv_sec, to.tv_usec);
	  }

	// XXX: only interested in the 1st solution, for now
	if (!_fd_optimising && result == FD_OK)
	  break;

	// eventually, the agent will fail to find a solution
	fd__trace("[%d.%d] agent was unsuccessful\n", tid, n);

	// tell the main thread that this agent is done
	if (sem_wait(&ready_semaphore))
	  perror("sem_wait (ready)");

	successful = -n - 1;	// negative agent number means failure

#if STEAL_WORK >= 2
	pthread_mutex_lock(&continue_mutex);
#endif

	if (sem_post(&notify_semaphore))
	  perror("sem_post");

#if STEAL_WORK >= 2
	do {
	  // wait for more work
	  fd__trace("[%d.%d] waiting for more work\n", tid, n);
	  pthread_cond_wait(&continue_cond, &continue_mutex);
	} while(n >= agents_to_run);

	if (pthread_mutex_unlock(&continue_mutex))
	  perror("pthread_mutex_unlock");
#endif
      } while (STEAL_WORK >= 2);

#if STEAL_WORK >= 2
      pthread_cleanup_pop(0);
#endif
      pthread_cleanup_pop(0);

      return result;
    }
  else /* _fd_counting_solutions != 0 */
    {
      unsigned long long solutions;
      struct timeval ti, tis, to, tos;

      fd__trace("[%d.%d] agent starting\n", tid, n);

      gettimeofday(&ti, NULL);

      _fd_despicable_hack(n);

      memcpy(_fd_variables, _fd_copies[n], fd_variables_count * sizeof(fd_int));

#if STEAL_WORK >= 2
      pthread_cleanup_push((void (*)(void *)) pthread_mutex_unlock,
			   &continue_mutex);
#endif

      do {
	gettimeofday(&tis, NULL);

	solutions = _fd_count_solutions(_fd_copies[n], n);

	gettimeofday(&to, NULL);
	timersub(&to, &tis, &tos);
	timersub(&to, &ti, &to);
	fd__trace("[%d.%d] took %d.%06ds (%d.%06ds)\n", tid, n,
		  tos.tv_sec, tos.tv_usec, to.tv_sec, to.tv_usec);

	fd__trace("[%d.%d] found %llu solutions\n", tid, n, solutions);

	// tell the main thread that this agent is done
	if (sem_wait(&ready_semaphore))
	  perror("sem_wait (ready)");

	successful = n;

	// and update the solutions' running total
	total_solutions += solutions;

#if STEAL_WORK >= 2
	pthread_mutex_lock(&continue_mutex);
#endif

	if (sem_post(&notify_semaphore))
	  perror("sem_post");

#if STEAL_WORK >= 2
	do {
	  // wait for more work
	  fd__trace("[%d.%d] waiting for more work\n", tid, n);
	  pthread_cond_wait(&continue_cond, &continue_mutex);
	} while (n >= agents_to_run);

	if (pthread_mutex_unlock(&continue_mutex))
	  perror("pthread_mutex_unlock");
#endif /* STEAL_WORK >= 2 */
      } while (STEAL_WORK >= 2);

#if STEAL_WORK >= 2
      pthread_cleanup_pop(0);
#endif

      return 0;
    }
}

static void clock_add(struct timespec *ts, long delta)
{
  clock_gettime(CLOCK_REALTIME, ts);

  ts->tv_nsec += delta;

  if (ts->tv_nsec >= 1000000000L)
    {
      ts->tv_sec += ts->tv_nsec / 1000000000L;
      ts->tv_nsec %= 1000000000L;
    }
}

/* send TO a dataless message (non-blocking) */
static void _fd_send(int to, int tag)
{
  MPI_Request mpi_request = MPI_REQUEST_NULL;

  if (MPI_Isend(NULL, 0, MPI_CHAR, to, tag, MPI_COMM_WORLD, &mpi_request))
    fd__fatal("MPI_Isend failed");
}

/* send TO a message with data (non-blocking) */
static void _fd_send_data(int to, int tag, void *buf, int n, MPI_Datatype type)
{
  MPI_Request mpi_request = MPI_REQUEST_NULL;

  if (MPI_Isend(buf, n, type, to, tag, MPI_COMM_WORLD, &mpi_request))
    fd__fatal("MPI_Isend failed");
}

#if STEAL_WORK >= 2
/* send TO a message with data and keep the record of the REQUEST */
static void _fd_ssend_data(int to, int tag, void *buf, int n, MPI_Datatype type,
			   MPI_Request *request)
{
  if (request)
    {
#ifndef MAD_MPI
      if (MPI_Issend(buf, n, type, to, tag, MPI_COMM_WORLD, request))
	fd__fatal("MPI_Issend failed");

#if 0
      {
	int mpi_flag;

	fd__debug("[%d] MPI_Test = %d\n", tid,
		  MPI_Test(request, &mpi_flag, MPI_STATUS_IGNORE));

	if (mpi_flag)
	  fd__debug("[%d] send completed\n", tid);
	else
	  fd__debug("[%d] send didn't complete yet\n", tid);
      }
#endif
#else /* MAD_MPI */
      // XXX: NMAD doesn't have MPI_Issend
      // XXX: no record of the sending is left
      if (MPI_Ssend(buf, n, type, to, tag, MPI_COMM_WORLD))
	fd__fatal("MPI_Send failed");

      *request = MPI_REQUEST_NULL;
#endif /* MAD_MPI */
    }
  else
    {
      MPI_Request mpi_request = MPI_REQUEST_NULL;

      if (MPI_Isend(buf, n, type, to, tag, MPI_COMM_WORLD, &mpi_request))
	fd__fatal("MPI_Isend failed");
    }
}
#endif /* STEAL_WORK >= 2 */

static void fd__send_empty_store(int to, int tag, _fd_store buffer,
				 MPI_Request *request)
{
  if (MPI_Isend(buffer, 0, MPI_DOMAIN_TYPE,
		to, tag, MPI_COMM_WORLD, request))
    fd__fatal("MPI_Isend failed");
}

static void _fd_send_store(int to, int tag, _fd_store buffer,
			   MPI_Request *request, bool synchronous)
{
  if (synchronous)
    {
      _fd_ssend_data(to, tag, buffer, fd_variables_count * DOMAIN_WORDS,
		     MPI_DOMAIN_TYPE, request);
    }
  else
    {
      if (MPI_Isend(buffer, fd_variables_count * DOMAIN_WORDS, MPI_DOMAIN_TYPE,
		    to, tag, MPI_COMM_WORLD, request))
	fd__fatal("MPI_Isend failed");
    }
}

static int _fd_recv_store(int from, int tag, _fd_store buffer)
{
  MPI_Status status;
  int count;

  if (MPI_Recv(buffer, fd_variables_count * DOMAIN_WORDS, MPI_DOMAIN_TYPE,
	       from, tag, MPI_COMM_WORLD, &status))
    fd__fatal("MPI_Recv failed");

  MPI_Get_count(&status, MPI_DOMAIN_TYPE, &count);

  return count;
}

static void _fd_broadcast(int msg, int processes, int from)
{
#if 0
  MPI_Request *mpi_request /* = MPI_REQUEST_NULL*/;
  int i;

  for (i = 0; i < processes; ++i)
    if (i != from)
      {
	mpi_request = malloc(sizeof(MPI_Request));

	if (MPI_Isend(NULL, 0, MPI_CHAR, i, msg, MPI_COMM_WORLD, mpi_request))
	  fd__fatal("MPI_Isend failed");

	fd_list_append(pending_requests, mpi_request);
      }
#else
  MPI_Request mpi_request = MPI_REQUEST_NULL;
  int i;

  for (i = 0; i < processes; ++i)
    if (i != from)
      if (MPI_Isend(NULL, 0, MPI_CHAR, i, msg, MPI_COMM_WORLD, &mpi_request))
	fd__fatal("MPI_Isend failed");
#endif
}

static void _fd_broadcast_data(int msg, void *buf, int n, MPI_Datatype type,
			       int processes, int from)
{
  int i;

  for (i = 0; i < processes; ++i)
    if (i != from)
      _fd_send_data(i, msg, buf, n, type);
}

#ifdef GET_SIGNAL
#include <signal.h>
void sig_handler(int sig)
{
  printf("******** caught signal %d ********\n", sig);
}

struct sigaction old_actions[_NSIG];
struct sigaction new_action = { sig_handler, 0, 0, NULL };
#endif

static bool _fd_probe_external(int tag, char *name, int *source, bool consume)
{
  MPI_Status mpi_status;
  int mpi_flag;
  int s;

  s = MPI_Iprobe(MPI_ANY_SOURCE, tag, MPI_COMM_WORLD, &mpi_flag, &mpi_status);

#if 0
  // XXX: what the heck is the return value supposed to mean?
  if (s)
    {
      char es[1024]; int _;
      fd__debug("MPI_Iprobe %s returned %d\n", name, s);
      MPI_Error_string(s, es, &_);
      fd__debug("%s\n", es);
    }
#endif

  if (mpi_flag)
    {
      *source = mpi_status.MPI_SOURCE;

      fd__trace("[%d] got %s from %d\n", tid, name, *source);

      // consume message
      if (consume)
	if (MPI_Recv(NULL, 0, MPI_CHAR, mpi_status.MPI_SOURCE, tag,
		     MPI_COMM_WORLD, &mpi_status))
	  fd__fatal("MPI_Recv failed");

      return true;
    }

  return false;
}

static int _fd_get_event(int running, int *source, int *timeout)
{
  struct timespec ts;
  double tin, tmout = 0;
  int event;

  fd__trace("[%d] getting next event\n", tid);

  if (timeout && *timeout)
    {
      tin = MPI_Wtime();
      tmout = tin + *timeout / 1000.0;	// timeout comes in milliseconds
    }

  for (;;)
    {
      // see if any of the agents has anything to say
      if (running > 0)
	{
	  if (sem_trywait(&notify_semaphore))
	    {
	      if (errno == EAGAIN)
		; //fd__trace("[%d] notify semaphore is busy\n", tid);
	      else
		perror("sem_trywait (notify)");
	    }
	  else
	    {
	      // got something from one of the agents
	      fd__trace("[%d] got something from an agent\n", tid);

	      if (!_fd_counting_solutions)
		{
		  int s = successful;

		  if (sem_post(&ready_semaphore))
		    perror("sem_post");

		  fd__trace("[%d] got an answer from %s%d\n", tid,
			    s == -1 ? "-" : "", s + (s < 0));

		  if (s >= 0)
		    {
		      *source = s;

		      event = EV_INT_SUCCESS;
		      break;
		    }

		  *source = -(s + 1);

		  return EV_INT_FAIL;
		}
	      else /* _fd_counting_solutions != 0 */
		{
		  *source = successful;

		  if (sem_post(&ready_semaphore))
		    perror("sem_post");

		  event = EV_INT_COUNT;
		  break;
		}
	    }
	}

#ifndef MAD_MPI
      {
	MPI_Status mpi_status;
	int mpi_flag;
	bool consume = true;
	char *name;

	MPI_Iprobe(MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, &mpi_flag,
		   &mpi_status);

	if (mpi_flag)
	  {
	    *source = mpi_status.MPI_SOURCE;

	    switch (mpi_status.MPI_TAG)
	      {
	      case FD_MSG_SOLUTION:
		name = "FD_MSG_SOLUTION";
		consume = false;
		event = EV_EXT_SUCCESS;
		break;
	      case FD_MSG_FAIL:
		name = "FD_MSG_FAIL";
		event = EV_EXT_FAIL;
		break;
	      case FD_MSG_DONE:
		name = "FD_MSG_DONE";
		event = EV_EXT_DONE;
		break;
	      case FD_MSG_BOUND:
		name = "FD_MSG_BOUND";
		consume = false;
		event = EV_EXT_BOUND;
		break;
	      case FD_MSG_COUNT:
		name = "FD_MSG_COUNT";
		consume = false;
		event = EV_EXT_COUNT;
		break;
	      case FD_MSG_STORE:
		name = "FD_MSG_STORE";
		consume = false;
		event = EV_EXT_STORE;
		break;
	      case FD_MSG_FEED_ME:
		name = "FD_MSG_FEED_ME";
		event = EV_EXT_REQ_WORK;
		break;
	      case FD_MSG_NO_WORK:
		name = "FD_MSG_NO_WORK";
		event = EV_EXT_NO_WORK;
		break;
	      case FD_MSG_NO_SHARE:
		name = "FD_MSG_NO_SHARE";
		event = EV_EXT_NO_SHARE;
		break;
	      case FD_MSG_SHARE:
		name = "FD_MSG_SHARE";
		event = EV_EXT_REQ_SHARE;
		break;
	      case FD_MSG_QUIT:
		name = "FD_MSG_QUIT";
		event = EV_EXT_QUIT;
		break;
	      case FD_MSG_READY:
		name = "FD_MSG_READY";
		event = EV_EXT_READY;
		break;
	      default:
		fd__debug("[%d] unknown tag %d\n", tid, mpi_status.MPI_TAG);
		fd__fatal("unknown tag in message");
	      }

	    fd__trace("[%d] got %s from %d\n", tid, name, *source);

	    // consume message
	    if (consume)
	      if (MPI_Recv(NULL, 0, MPI_CHAR, mpi_status.MPI_SOURCE,
			   mpi_status.MPI_TAG, MPI_COMM_WORLD, &mpi_status))
		fd__fatal("MPI_Recv failed");

	    break;
	  }
      }
#else /* MAD_MPI */
      // XXX: New Madeleine doesn't implement MPI_*probe(..., MPI_ANY_TAG, ...)

      if (_fd_probe_external(FD_MSG_SOLUTION, "FD_MSG_SOLUTION", source, false))
	{
	  event = EV_EXT_SUCCESS;
	  break;
	}

      if (_fd_probe_external(FD_MSG_BOUND, "FD_MSG_BOUND", source, false))
	{
	  event = EV_EXT_BOUND;
	  break;
	}

      if (_fd_probe_external(FD_MSG_QUIT, "FD_MSG_QUIT", source, true))
	{
	  event = EV_EXT_QUIT;
	  break;
	}

      if (_fd_probe_external(FD_MSG_NO_WORK, "FD_MSG_NO_WORK", source, true))
	{
	  event = EV_EXT_NO_WORK;
	  break;
	}

      if (_fd_probe_external(FD_MSG_STORE, "FD_MSG_STORE", source, false))
	{
	  event = EV_EXT_STORE;
	  break;
	}

      if (_fd_probe_external(FD_MSG_SHARE, "FD_MSG_SHARE", source, true))
	{
	  event = EV_EXT_REQ_SHARE;
	  break;
	}

      if (_fd_probe_external(FD_MSG_NO_SHARE, "FD_MSG_NO_SHARE", source, true))
	{
	  event = EV_EXT_NO_SHARE;
	  break;
	}

      /*
	must be checked for after FD_MSG_STORE: if the `supplier'
	sends an FD_MSG_STORE, then finishes its work and sends an
	FD_MSG_FEED_ME and the checks are reversed, the latter will be
	ignored (since this is not yet the supplier) and the solver
	will not halt (XXX: only if not polling?)
      */
      if (_fd_probe_external(FD_MSG_FEED_ME, "FD_MSG_FEED_ME", source, true))
	{
	  event = EV_EXT_REQ_WORK;
	  break;
	}

      if (_fd_probe_external(FD_MSG_FAIL, "FD_MSG_FAIL", source, true))
	{
	  event = EV_EXT_FAIL;
	  break;
	}

      // XXX: only processes with rank != 0 shall receive DONE
      if (_fd_probe_external(FD_MSG_DONE, "FD_MSG_DONE", source, true))
	{
	  event = EV_EXT_DONE;
	  break;
	}

      if (_fd_probe_external(FD_MSG_READY, "FD_MSG_READY", source, true))
	{
	  event = EV_EXT_READY;
	  break;
	}

      if (_fd_counting_solutions)
	// XXX: only the process with rank 0 shall receive COUNTs
	if (_fd_probe_external(FD_MSG_COUNT, "FD_MSG_COUNT", source, false))
	  {
	    event = EV_EXT_COUNT;
	    break;
	  }
#endif /* MAD_MPI */

      if (tmout && MPI_Wtime() >= tmout)
	{
	  *timeout = 0;

	  return EV_INT_TIMEOUT;
	}

      if (tmout)
	{
	  struct timespec ts = { 0, WAIT_TOUT };

	  nanosleep(&ts, NULL);
	}
      else
	{
	  struct timespec ts = { 0, WAIT_NORM };

	  nanosleep(&ts, NULL);
	}
    }

  if (tmout)
    {
      *timeout = (int) ((tmout - MPI_Wtime()) * 1000.0 + 0.5);
      if (*timeout < 1)
	*timeout = 1;
    }

  return event;
}

static void _fd_flush_events(int procno)
{
  MPI_Status mpi_status;
  int mpi_flag;
  int got_one;
  MPI_Request *mpi_request;

  // check the status of MPI pending requests
  while (mpi_request = fd_list_remove(pending_requests))
    {
      if (MPI_Test(mpi_request, &mpi_flag, MPI_STATUS_IGNORE))
	fd__debug("[%d] MPI_Test failed\n", procno);

      if (!mpi_flag)
	{
#if 0	// this is probably the solution just sent, not cancelling it
	  fd__trace("[%d] cancelling request\n", procno);

	  if (MPI_Cancel(mpi_request))
	    fd__debug("[%d] MPI_Cancel failed\n", procno);

	  fd__trace("[%d] waiting for cancellation completion\n", procno);

	  if (MPI_Wait(mpi_request, MPI_STATUS_IGNORE))
	    fd__debug("[%d] MPI_Wait failed\n", procno);
#else
	  fd__debug("[%d] found a not completed request\n", procno);
#endif
	}

      free(mpi_request);
    }

  fd_list_dispose(pending_requests);

#ifndef MAD_MPI
  // XXX: should be cancelled at the sending site?

  // consume pending incoming communications
  for (;;)
    {
      int source;
      char *name;
      MPI_Datatype type = MPI_CHAR;
      int count = 0;

      MPI_Iprobe(MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, &mpi_flag,
		 &mpi_status);

      if (!mpi_flag)	// nothing to do if no incoming message is found
	break;

      source = mpi_status.MPI_SOURCE;

      switch (mpi_status.MPI_TAG)
	{
	case FD_MSG_SOLUTION:
	  name = "FD_MSG_SOLUTION";
	  type = MPI_DOMAIN_TYPE;
	  count = fd_variables_count * DOMAIN_WORDS;
	  fd__error("[%d] dropping %s from %d\n", tid, name, source);
	  break;
	case FD_MSG_FAIL:
	  name = "FD_MSG_FAIL";
	  break;
	case FD_MSG_DONE:
	  name = "FD_MSG_DONE";
	  break;
	case FD_MSG_BOUND:
	  name = "FD_MSG_BOUND";
	  type = MPI_INT;
	  count = 1;
	  break;
	case FD_MSG_COUNT:
	  name = "FD_MSG_COUNT";
	  type = MPI_LONG_LONG;
	  count = 1;
	  fd__error("[%d] dropping %s from %d\n", tid, name, source);
	  break;
	case FD_MSG_STORE:
	  name = "FD_MSG_STORE";
	  type = MPI_DOMAIN_TYPE;
	  count = fd_variables_count * DOMAIN_WORDS;
	  fd__error("[%d] *** dropping %s from %d\n", tid, name, source);
	  break;
	case FD_MSG_FEED_ME:
	  name = "FD_MSG_FEED_ME";
	  break;
	case FD_MSG_NO_WORK:
	  name = "FD_MSG_NO_WORK";
	  break;
	case FD_MSG_NO_SHARE:
	  name = "FD_MSG_NO_SHARE";
	  break;
	case FD_MSG_SHARE:
	  name = "FD_MSG_SHARE";
	  break;
	case FD_MSG_QUIT:
	  name = "FD_MSG_QUIT";
	  break;
	case FD_MSG_READY:
	  name = "FD_MSG_READY";
	  break;
	default:
	  fd__debug("[%d] unknown tag %d\n", tid, mpi_status.MPI_TAG);
	  fd__fatal("unknown tag in message");
	}

      fd__debug("[%d] dropping %s from %d\n", tid, name, source);

      // consume message
      {
	char *buffer = alloca(fd_variables_count * sizeof(*store));

	if (MPI_Recv(buffer, count, type, source, mpi_status.MPI_TAG,
		     MPI_COMM_WORLD, MPI_STATUS_IGNORE))
	  fd__error("[%d] MPI_Recv failed (dropping %s)\n", tid, name);
      }
    }
#else /* MAD_MPI */
  // XXX: New Madeleine doesn't implement MPI_*probe(..., MPI_ANY_TAG, ...)
//#ifdef MAD_MPI
//  // XXX: otherwise, assume they will be cancelled at the sending site

  // consume pending incoming communications
  do
    {
      got_one = 0;

      MPI_Iprobe(MPI_ANY_SOURCE, FD_MSG_SOLUTION, MPI_COMM_WORLD, &mpi_flag, &mpi_status);

      if (mpi_flag)
	{
	  _fd_store null = alloca(fd_variables_count * sizeof(*null));

	  fd__trace("[%d] dropping FD_MSG_SOLUTION from %d\n", procno, mpi_status.MPI_SOURCE);

	  _fd_recv_store(mpi_status.MPI_SOURCE, FD_MSG_SOLUTION, null);

	  got_one++;
	}

      MPI_Iprobe(MPI_ANY_SOURCE, FD_MSG_FEED_ME, MPI_COMM_WORLD, &mpi_flag, &mpi_status);

      if (mpi_flag)
	{
	  fd__trace("[%d] dropping FD_MSG_FEED_ME from %d\n", procno, mpi_status.MPI_SOURCE);

	  // consume message
	  if (MPI_Recv(NULL, 0, MPI_CHAR, mpi_status.MPI_SOURCE, FD_MSG_FEED_ME,
		       MPI_COMM_WORLD, &mpi_status))
	    fd__fatal("MPI_Recv failed");

	  got_one++;
	}

      MPI_Iprobe(MPI_ANY_SOURCE, FD_MSG_NO_WORK, MPI_COMM_WORLD, &mpi_flag, &mpi_status);

      if (mpi_flag)
	{
	  fd__trace("[%d] dropping FD_MSG_NO_WORK from %d\n", procno, mpi_status.MPI_SOURCE);

	  // consume message
	  if (MPI_Recv(NULL, 0, MPI_CHAR, mpi_status.MPI_SOURCE, FD_MSG_NO_WORK,
		       MPI_COMM_WORLD, &mpi_status))
	    fd__fatal("MPI_Recv failed");

	  got_one++;
	}

      MPI_Iprobe(MPI_ANY_SOURCE, FD_MSG_STORE, MPI_COMM_WORLD, &mpi_flag, &mpi_status);

      if (mpi_flag)
	{
	  _fd_store null = alloca(fd_variables_count * sizeof(*null));

	  fd__trace("[%d] dropping FD_MSG_STORE from %d\n", procno, mpi_status.MPI_SOURCE);

	  _fd_recv_store(mpi_status.MPI_SOURCE, FD_MSG_STORE, null);

	  got_one++;
	}

      MPI_Iprobe(MPI_ANY_SOURCE, FD_MSG_FAIL, MPI_COMM_WORLD, &mpi_flag, &mpi_status);

      if (mpi_flag)
	{
	  fd__trace("[%d] dropping FD_MSG_FAIL from %d\n", procno, mpi_status.MPI_SOURCE);

	  // consume message
	  if (MPI_Recv(NULL, 0, MPI_CHAR, mpi_status.MPI_SOURCE, FD_MSG_FAIL,
		       MPI_COMM_WORLD, &mpi_status))
	    fd__fatal("MPI_Recv failed");

	  got_one++;
	}

      MPI_Iprobe(MPI_ANY_SOURCE, FD_MSG_DONE, MPI_COMM_WORLD, &mpi_flag, &mpi_status);

      if (mpi_flag)
	{
	  fd__trace("[%d] dropping FD_MSG_DONE from %d\n", procno, mpi_status.MPI_SOURCE);

	  // consume message
	  if (MPI_Recv(NULL, 0, MPI_CHAR, mpi_status.MPI_SOURCE, FD_MSG_DONE,
		       MPI_COMM_WORLD, &mpi_status))
	    fd__fatal("MPI_Recv failed");

	  got_one++;
	}

      MPI_Iprobe(MPI_ANY_SOURCE, FD_MSG_SHARE, MPI_COMM_WORLD, &mpi_flag, &mpi_status);

      if (mpi_flag)
	{
	  fd__trace("[%d] dropping FD_MSG_SHARE from %d\n", procno, mpi_status.MPI_SOURCE);

	  // consume message
	  if (MPI_Recv(NULL, 0, MPI_CHAR, mpi_status.MPI_SOURCE, FD_MSG_SHARE,
		       MPI_COMM_WORLD, &mpi_status))
	    fd__fatal("MPI_Recv failed");

	  got_one++;
	}

      MPI_Iprobe(MPI_ANY_SOURCE, FD_MSG_NO_SHARE, MPI_COMM_WORLD, &mpi_flag, &mpi_status);

      if (mpi_flag)
	{
	  fd__trace("[%d] dropping FD_MSG_NO_SHARE from %d\n", procno, mpi_status.MPI_SOURCE);

	  // consume message
	  if (MPI_Recv(NULL, 0, MPI_CHAR, mpi_status.MPI_SOURCE, FD_MSG_NO_SHARE,
		       MPI_COMM_WORLD, &mpi_status))
	    fd__fatal("MPI_Recv failed");

	  got_one++;
	}
    }
  while (got_one);
#endif /* MAD_MPI */
}

void _fd_cleanup_mpi_state(int process)
{
//  if (!_fd_counting_solutions)
    {
      // this rendez-vous ensures that no stop message is cancelled before
      // a still running process has a chance to see it
      // XXX: New Madeleine can't cope with messages en route
#if 0 //#ifndef MAD_MPI
      fd__trace("[%d] setting up barrier 1\n", process);
      if (MPI_Barrier(MPI_COMM_WORLD))
	fd__error("[%d] MPI_Barrier failed\n", process);
      else
	fd__trace("[%d] leaving barrier 1\n", process);
#endif

      _fd_flush_events(process);

#if 0
      fd__trace("[%d] setting up barrier 2\n", process);
      if (MPI_Barrier(MPI_COMM_WORLD))
	fd__error("[%d] MPI_Barrier failed\n", process);
      else
	fd__trace("[%d] leaving barrier 2\n", process);
#endif
    }

  _fd_statistics_msgs();

  fd__trace("[%d] calling MPI_Finalize\n", process);
  if (MPI_Finalize())
    fd__error("[%d] MPI_Finalize failed\n", process);
  else
    fd__trace("[%d] returned from MPI_Finalize\n", process);
}

static void _fd_exit_process(int process)
{
  _fd_cleanup_mpi_state(process);

  fd__trace("[%d] exiting\n", process);

  exit(0);
}

void _fd_search_space_sizes(_fd_store stores[], int n)
{
#ifdef TRACE
  double s;
  int i, v;

  // when there are no agents, just return
  if (*stores == NULL)
    return;

  for (i = 0; i < n; ++i)
    {
      s = 1;

      for (v = 0; v < fd__label_vars_count; ++v)
	s *= _fd_val_size(SVALUE(stores[i][fd__label_vars[v]->index]));

      fd__trace("[%d] search space %d size is %g\n", tid, i, s);
    }
#endif /* TRACE */
}

static void count_singletons(_fd_store store)
{
#ifdef TRACE
  int i, n;

  n = 0;

  for (i = 0; i < fd_variables_count; ++i)
    if (_fd_val_single(SVALUE(store[i]), NULL))
      ++n;

  fd__trace("[%d] store has %d/%d/%d singletons\n", tid, n,
	    fd__label_vars_count, fd_variables_count);
#endif
}

#if STEAL_WORK >= 2
static int _fd_restart_agents(int tid, int nagents)
{
  int parts;

  // split the store among the agents
  parts = fd__split_problem(nagents, _fd_agents_stores, fd__split_team_problem_f);

  if (parts < nagents)
    fd__debug("[%d] WARNING: store only good for %d agents (out of %d)\n",
	      tid, parts, nagents);

  agents_to_run = parts;

  // reset the pool
  _fd_init_store_depository(agents_to_run);

  // give the agents the go-ahead
  if (pthread_mutex_lock(&continue_mutex))
    perror("pthread_mutex_lock");
  pthread_cond_broadcast(&continue_cond);
  if (pthread_mutex_unlock(&continue_mutex))
    perror("pthread_mutex_unlock");

  fd__trace("[%d] told waiting agents to proceed\n", tid);

  return parts;
}

#ifndef ROUND_ROBIN_POLL

#define _fd_start_polling(x) _fd_poll_next(processes, x)

static int _fd_poll_next(int last, int skip)
{
  int poll;

  poll = last - 1;

  // skip over both self and the ex-supplier (if it is not process 0)
  while (poll == tid || poll == skip && skip != 0)
    --poll;

  if (poll >= 0)
    {
      // send a forceful request for work
      fd__trace("[%d] polling %d\n", tid, poll);
      _fd_send(poll, FD_MSG_SHARE);
    }

  return poll;
}
#else /* ROUND_ROBIN_POLL */

#define _fd_start_polling(x) _fd_poll_rr(true, processes, x)
#define _fd_poll_next(l, x) _fd_poll_rr(false, l, x)

static int _fd_poll_rr(bool starting, int np_or_last, int skip)
{
  static int processes = 0;
  static int tried, last_polled;
  int poll;
  int i;

  if (starting)
    {
      tried = 0;

      if (processes == 0)
	{
	  processes = np_or_last;
	  last_polled = tid;
	}
    }

  poll = (last_polled + 1) % processes;

  // skip over both the ex-supplier and self
  for (; tried < processes && (poll == tid || poll == skip); ++tried)
    poll = (poll + 1) % processes;

  if (tried == processes)
    return -1;

  // send a forceful request for work
  fd__trace("[%d] polling %d\n", tid, poll);
  _fd_send(poll, FD_MSG_SHARE);

  last_polled = poll;

  return poll;
}
#endif /* ROUND_ROBIN_POLL */

// try to share work with PEER, return true on success
// XXX: side-effect: when true is returned, the process will cease to
//      be the supplier
static bool _fd_share_work(int peer, MPI_Request *request, fd_list saved_stores,
			   bool polled)
{
  _fd_store saved;
  int mpi_flag;

  // test whether a previously sent store has yet to be received
  if (MPI_Test(request, &mpi_flag, MPI_STATUS_IGNORE))
    perror("MPI_Test");

  if (!mpi_flag)
    {
      // the previous send has not completed
      fd__trace("[%d] there's a store in transit to %d\n", tid, peer);

      /* despite MPI_Test() having failed, the store may already have
	 been received and processed; since process 0 must receive or
	 give an answer, otherwise there's a risk of deadlock, we'll
	 force one when it is involved */

      if (peer != 0 && tid != 0)
	return true;

      if (MPI_Wait(request, MPI_STATUS_IGNORE))
	perror("MPI_Wait");
    }

  // see if there's a saved store which can be sent
  if (saved = fd_list_remove(saved_stores))
    {
      memcpy(_fd_processes_stores[peer], saved,
	     fd_variables_count * sizeof(*saved));

      fd__trace("[%d] sending store (saved) to %d\n", tid, peer);
      _fd_send_store(peer, FD_MSG_STORE, _fd_processes_stores[peer], request,
		     true);

      free(saved);

      return true;
    }

  if (_fd_steal_store(_fd_processes_stores[peer], NULL, -1, 0))
    {
      fd__trace("[%d] sending store to %d\n", tid, peer);
      _fd_send_store(peer, FD_MSG_STORE, _fd_processes_stores[peer], request,
		     true);

      return true;
    }

  // tell the requester that there is no work available
  if (polled)
    {
      fd__trace("[%d] no work to share with %d\n", tid, peer);
      _fd_send(peer, FD_MSG_NO_SHARE);
    }
  else
    {
      fd__trace("[%d] can't find work requested by %d\n", tid, peer);
      _fd_send(peer, FD_MSG_NO_WORK);
    }

  return false;
}

/* Process PEER has polled last resort process 0 for work unsuccessfully,
   and (probably) has become idle. If all remaining processes are idle,
   stop them. */
#define mark_process_idle(peer)						     \
	do								     \
	  {								     \
	    int _p = peer;						     \
									     \
	    if (!process_idle[_p])					     \
	      {								     \
		/* record polling process as idle */			     \
		gettimeofday(&to, NULL); timersub(&to, &ti, &to);	     \
		fd__trace("[%d] %d idled after %d.%06ds\n", tid, _p,	     \
			   to.tv_sec, to.tv_usec);			     \
									     \
		process_idle[_p] = true;				     \
		if (++idle_processes == live_procs - 1)			     \
		  {							     \
		    fd__trace("[%d] sending 2nd quit\n", tid);		     \
									     \
		    _fd_broadcast(FD_MSG_QUIT, processes, tid);		     \
		  }							     \
	      }								     \
	  }								     \
	while (0)
#endif /* STEAL_WORK >= 2 */

int _fd_dsolve()
{
  pthread_t threads[MAX_AGENTS];
  int nagents = 4, started, running, peer;
  int status = FD_NOSOLUTION;
  int parts;
  int i;
  struct timeval ti, to, ts = { 0, 0 };
  char *s;
  int processes, live_procs;
#if STEAL_WORK >= 2
  bool *process_idle;		// processes who have polled process 0
  int idle_processes = 0;
  bool quitting = false;
  MPI_Request *stores_pending;	// to trace the reception of a store
  int supplier;
#endif
  MPI_Status mpi_status;
  int done, stopped;
  _fd_store solution;
  bool have_solution = false;
  int bound;

  unsigned long long external_solutions = 0; // other processes' solutions count

  fd_int *local_variables = _fd_variables;
  _fd_store local_store = store;

  gettimeofday(&ti, NULL);

  if (MPI_Comm_rank(MPI_COMM_WORLD, &tid))
    fd__fatal("MPI_Comm_rank failed\n");

  fd__trace("I'm MPI rank %d\n", tid);

  {
    char host[512];

    fd__trace("[%d] process %d at %s\n", tid, getpid(),
	      (gethostname(host, sizeof(host)), host));
  }

  if (MPI_Comm_size(MPI_COMM_WORLD, &processes))
    fd__fatal("MPI_Comm_size failed\n");

  fd__trace("[%d] There are %d of us\n", tid, processes);

  pending_requests = fd_list_new();

#if STEAL_WORK >= 2
  stores_pending = calloc(processes, sizeof(*stores_pending)); // XXX: NULL
  for (i = 0; i < processes; ++i)
    stores_pending[i] = MPI_REQUEST_NULL;

  // the last process is the one which can supply work to the others
  supplier = tid == processes - 1;

  process_idle = alloca(processes * sizeof(*process_idle));
  memset(process_idle, 0, processes * sizeof(*process_idle));
#endif

  if (fd__workers == -1 && (s = getenv("FDC_AGENTS")))
    fd__workers = atoi(s);

  if (fd__workers != -1)
    {
      nagents = fd__workers;

      if (nagents < 0)
	{
	  nagents = 0;
	  fd__info("[%d] using %d workers\n", tid, nagents);
	}
      else if (nagents > MAX_AGENTS)
	{
	  nagents = MAX_AGENTS;
	  fd__info("[%d] limiting workers to %d\n", tid, nagents);
	}
    }

  // allocate stores for all the processes, in case one needs to borrow
  // work from this one
  for (i = 0; i < processes; ++i)
    _fd_processes_stores[i] = calloc(fd_variables_count, sizeof(*store)); // XXX: NULL

  fd__setup_label_vars();

  if (tid == 0)
    {
      // code for the controlling process

      MPI_Request mpi_request = MPI_REQUEST_NULL;

#ifdef FILTER_DOMAINS
      // XXX: *must* revise as the splitting proceeds!
      if (_fd_filter_domains() == FD_NOSOLUTION)
	return FD_NOSOLUTION;
#endif

      // do an initial partition among all the processes
      // XXX: check if processes <= MAX_AGENTS ('cause of _fd_processes_stores)
      parts = fd__split_problem(processes, _fd_processes_stores, fd__split_problem_f);
      _fd_search_space_sizes(_fd_processes_stores, parts);
      if (parts < processes)
	fd__error("WARNING: work enough for %d processes only\n", parts);

      // now send the resulting parts to the other processes
      // XXX: could each have done what this did and now pick its share?
      for (i = 1; i < parts; ++i)
	_fd_send_store(i, FD_MSG_STORE, _fd_processes_stores[i], &mpi_request,
		       false);

      for (i = parts; i < processes; ++i)
	fd__send_empty_store(i, FD_MSG_STORE, _fd_processes_stores[i], &mpi_request);

      processes = parts;

      // install our part of the search space
      memcpy(store, _fd_processes_stores[0], fd_variables_count * sizeof(*store));
    }
  else
    {
      // code for the remaining processes

      fd__trace("[%d] waiting for initial store\n", tid);

      // retrieve the domains assigned to the process
      if (!_fd_recv_store(0, FD_MSG_STORE, store))
      	{
	  fd__debug("[%d] empty initial store\n", tid);

	  _fd_exit_process(tid);
	}

      fd__trace("[%d] received initial store\n", tid);
    }

#if FILTER_DOMAINS > 1
  // XXX: *must* revise as the splitting proceeds!
  if (_fd_filter_domains() == FD_NOSOLUTION)
    return FD_NOSOLUTION;	      // XXX: should try to obtain a new store
#endif

  // now, get on with the search
  _fd_copy_problem(nagents);

  // allocate space to keep a solution
  if (_fd_optimising)
    solution = calloc(fd_variables_count, sizeof(*solution)); // XXX: NULL?
  else
    solution = store;

  // create agents' stores
  for (i = 0; i < nagents; ++i)
    _fd_agents_stores[i] = calloc(fd_variables_count, sizeof(*store)); // XXX: NULL

  parts = fd__split_problem(nagents, _fd_agents_stores, fd__split_team_problem_f);

  if (parts < nagents)
    {
      fd__trace("[%d] reducing to %d agents!\n", tid, parts);

      nagents = parts;
    }

  // XXX: pretend there are stores for every agent (should always happen)
  agents_to_run = nagents;

  _fd_search_space_sizes(_fd_agents_stores, parts);

  gettimeofday(&to, NULL);
  timersub(&to, &ti, &to);
  fd__trace("[%d] setup took %d.%06ds\n", tid, to.tv_sec, to.tv_usec);

#if 0
  for (i = 0; i < nagents; ++i)
    _fd_cprint2(_fd_copies[i]);

  return FD_NOSOLUTION;
#endif

  if (nagents > 0)
    {
      int timeout = 0;
#if STEAL_WORK >= 2
      int timeouts;
      int ex_supplier, polling;
      fd_list saved_stores = fd_list_new();
      _fd_message waiting_on;
      bool ready = false;	// true means no pending communications
#ifdef ASK_EARLY
      bool waiting_for_work = false;
      bool getting_work_failed = false;
#endif
#endif

      _fd_init_store_depository(nagents);

      if (sem_init(&ready_semaphore, 0, 1))
	perror("sem_init (ready)");
      if (sem_init(&notify_semaphore, 0, 0))
	perror("sem_init (notify)");
      if (_fd_optimising && sem_init(&resume_semaphore, 0, 0))
	  perror("sem_init (resume)");

      for (i = started = 0; i < nagents; ++i)
	{
	  long l = i; // XXX: avoid a gcc warning

	  pthread_create(&threads[i], NULL, (void *) _fd_agent, (void *) l);

	  started++;

	  // don't start any more agents if one has already succeeded
	  if (!_fd_counting_solutions && !_fd_optimising)
	    {
	      if (pthread_mutex_trylock(&success_mutex) == 0)
		pthread_mutex_unlock(&success_mutex);
	      else
		break;
	    }
	}

      fd__trace("[%d] main thread waiting\n", tid);

      running = started;
      live_procs = processes;
      done = stopped = 0;

      do
	{
	  int event = _fd_get_event(running, &peer, &timeout);

	  switch (event)
	    {
#if STEAL_WORK >= 2
	    case EV_INT_TIMEOUT:
	      fd__trace("[%d] timed out waiting for event\n", tid);

	      switch (waiting_on)
		{
		case FD_MSG_ANY:
		  done = 1;
		  break;
		case FD_MSG_FEED_ME:
		  // no work sharing after having been told to stop
		  if (quitting || ready)
		    break;

		  if (timeouts++)
		    {
		      if (tid != 0 || live_procs > 1)
			{
			  polling = _fd_start_polling(ex_supplier = tid);

			  waiting_on = FD_MSG_SHARE;
			  if (polling == 0)
			    timeout = 0;	// last resort polling
			  else
			    timeout = POLL_TIMEOUT;
			}
		      else
#ifndef ASK_EARLY
			done = 1;
#else
			done = running == 0;

		      waiting_for_work = false;
		      getting_work_failed = true;
#endif
		    }
		  else
		    {
		      // resend request for work
		      fd__trace("[%d] re-asking for work\n", tid);
		      _fd_broadcast(FD_MSG_FEED_ME, processes, tid);

		      timeout = FEED2_TIMEOUT;
		    }
		  break;
		case FD_MSG_SHARE:
		  // no work sharing after having been told to stop
		  if (quitting || ready)
		    break;

		  // the last polled process must have stopped
		  // try the next one
		  if ((polling = _fd_poll_next(polling, ex_supplier)) >= 0)
		    {
		      if (polling == 0)
			timeout = 0;		// last resort polling
		      else
			timeout = POLL_TIMEOUT;	// reset timeout
		    }
		  else
		    {
		      // no additional work could be found

		      done = tid != 0 || live_procs == 1;

#ifdef ASK_EARLY
		      done = done && running == 0;
		      waiting_for_work = false;
		      getting_work_failed = true;
#endif

		      if (tid == 0)
			{
			  // XXX: ???
			  // become the supplier, to answer further requests
			  // for work, even if negatively
			  supplier = 1;
			}
		    }

		  break;
		default:
		  fd__debug("[%d] unknown timeout: %d\n", tid, waiting_on);
		  fd__fatal("unknown timeout reason");
		}

	      break;
#endif /* STEAL_WORK >= 2 */

	    /* first solution internal events (from the workers) */

	    case EV_INT_SUCCESS:
	      fd__trace("[%d] using agent %d's results\n", tid, peer);

	      assert(!_fd_counting_solutions);

	      if (_fd_optimising)
		{
		  // check and update bound
		  if (!_fd_bound_check_set(_fd_agents_stores[peer]))
		    {
		      fd__trace("[%d] invalid solution, releasing %d\n", tid, peer);
		      if (sem_post(&resume_semaphore))
			perror("sem_post (resume)");

		      break;
		    }

		  gettimeofday(&ts, NULL);

		  // save solution
		  memcpy(solution, _fd_agents_stores[peer],
			 fd_variables_count * sizeof(*solution));

		  // release agent
		  fd__trace("[%d] releasing agent %d\n", tid, peer);
		  if (sem_post(&resume_semaphore))
		    perror("sem_post (resume)");

		  fd__trace("[%d] bound updated to %d\n", tid, _fd_bound_value());

		  // broadcast new bound
		  bound = _fd_bound_value();
		  _fd_broadcast_data(FD_MSG_BOUND, &bound, 1, MPI_INT,
				     processes, tid);

		  status = FD_OK;
		  have_solution = true;

		  break;
		}

	      status = FD_OK;

	      memcpy(store, _fd_agents_stores[peer],
		     fd_variables_count * sizeof(*store));

	      done = 1;

	      break;
	    case EV_INT_FAIL:
	      fd__trace("[%d] agent %d found no solution\n", tid, peer);

	      assert(!_fd_counting_solutions);

	      running--;

#if STEAL_WORK < 2
	      done = running == 0 && (tid != 0 || live_procs == 1);
#else /* STEAL_WORK < 2 */
#ifdef ASK_EARLY
	      if (running == agents_to_run - 1)
		{
		  // the first agent halted and the other agents must
		  // be stopping soon; try to get work before they do

		  // no need if we already have a store saved up
		  if (fd_list_empty(saved_stores))
		    {
		      // ask the other processes for more work, unless this
		      // is the rank 0 process and all others have finished
		      if (tid != 0 || live_procs > 1)
			{
			  if (!supplier)
			    {
			      fd__trace("[%d] asking for work\n", tid);
			      _fd_broadcast(FD_MSG_FEED_ME, processes, tid);

			      waiting_on = FD_MSG_FEED_ME;
			      timeout = FEED1_TIMEOUT;
			      timeouts = 0;
			    }
			  else
			    {
			      // poll the other processes for work
			      polling = _fd_start_polling(ex_supplier = tid);

			      waiting_on = FD_MSG_SHARE;
			      if (polling == 0)
				timeout = 0;		// last resort polling
			      else
				timeout = POLL_TIMEOUT;	// set timeout
			    }

			  waiting_for_work = true;
			}
		    }
		}
#endif /* ASK_EARLY */

	      if (running == 0)
		{
		  _fd_store saved;

		  // see if there's some store saved up
		  if (saved = fd_list_remove(saved_stores))
		    {
		      fd__trace("[%d] using saved store\n", tid);

		      memcpy(store, saved,
			     fd_variables_count * sizeof(*saved));

		      count_singletons(store);

		      // restart the agents
		      running = _fd_restart_agents(tid, nagents);

		      free(saved);
		    }
#ifdef ASK_EARLY
		  else if (waiting_for_work)
		    {
		      // we're already trying to get work, let's
		      // wait and see how that fans out

		      break;
		    }
		  else if (getting_work_failed)
		    {
		      // if we already have tried to get work and
		      // failed, it's no use doing it again

		      done = tid != 0 || live_procs == 1;

		      break;
		    }
#endif /* ASK_EARLY */
		  else if (quitting || ready)
		    done = quitting && (tid != 0 || live_procs == 1);
		  else if (tid != 0 || live_procs > 1)
		    {
		      // there is at least one other process still running
		      if (!supplier)
			{
			  // ask the other processes for more work
			  fd__trace("[%d] asking for work\n", tid);
			  _fd_broadcast(FD_MSG_FEED_ME, processes, tid);

			  waiting_on = FD_MSG_FEED_ME;
			  timeout = FEED1_TIMEOUT;
			  timeouts = 0;
			}
		      else
			{
			  // poll the other processes for work
			  polling = _fd_start_polling(ex_supplier = tid);

			  waiting_on = FD_MSG_SHARE;
			  if (polling == 0)
			    timeout = 0;		// last resort polling
			  else
			    timeout = POLL_TIMEOUT;	// set timeout
			}

#ifdef ASK_EARLY
		      waiting_for_work = true;
#endif
		    }
		  else
		    done = 1;
		}
#endif /* STEAL_WORK < 2 */

	      break;

	    /* first solution external events (from the other processes) */

	    case EV_EXT_SUCCESS:
	      fd__trace("[%d] received a solution from %d\n", tid, peer);

	      assert(!_fd_counting_solutions);

	      _fd_recv_store(peer, FD_MSG_SOLUTION, store);

	      fd__trace("[%d] using process %d's results\n", tid, peer);

	      gettimeofday(&to, NULL);
	      timersub(&to, &ti, &to);
#if defined(STATS_PROCS) || defined(TRACE)
	      fd__debug("[%d] process %d took %d.%06ds\n", tid, peer, to.tv_sec, to.tv_usec);
#endif

	      if (_fd_optimising)
		{
		  // see if it is the better solution
		  if (!have_solution || _fd_better_solution(store, solution))
		    {
		      fd__trace("[%d] process %d solution is better\n", tid, peer);

		      memcpy(solution, store,
			     fd_variables_count * sizeof(*store));

		      status = FD_OK;
		      have_solution = true;
		      ts.tv_sec = 0;

		      // XXX: could update the current bound
		    }

		  live_procs--;
		  done = live_procs == 1 && running == 0;

		  break;
		}

	      status = FD_OK;

	      done = 1;

	      break;
	    case EV_EXT_FAIL:
	      fd__trace("[%d] process %d found no solution\n", tid, peer);

	      assert(!_fd_counting_solutions);

	      gettimeofday(&to, NULL);
	      timersub(&to, &ti, &to);
#if defined(STATS_PROCS) || defined(TRACE)
	      fd__debug("[%d] process %d took %d.%06ds\n", tid, peer, to.tv_sec, to.tv_usec);
#endif

	      live_procs--;

#if STEAL_WORK >= 2
	      // become the supplier, so further requests for work will
	      // be answered
	      // XXX: some requests for work may get lost
	      // XXX: side effect: this process *will* try to supply work
	      supplier = 1;
#endif

	      done = running == 0 && live_procs == 1;
#ifdef ASK_EARLY
	      done = done && !waiting_for_work;
#endif

	      break;
	    case EV_EXT_DONE:
	      fd__trace("[%d] process %d told me to stop\n", tid, peer);

	      assert(!_fd_counting_solutions);

	      done = stopped = 1;

	      break;

	    /* optimisation external events (from the other processes) */

	    case EV_EXT_BOUND:
	      fd__trace("[%d] received new bound from %d\n", tid, peer);

	      assert(!_fd_counting_solutions && _fd_optimising);

	      {
		int bound;

		if (MPI_Recv(&bound, 1, MPI_INT, peer, FD_MSG_BOUND,
			     MPI_COMM_WORLD, &mpi_status))
		  fd__fatal("MPI_Recv failed");

		// try to set new bound
		if (_fd_set_bound(bound))
		  {
		    fd__trace("[%d] new bound is %d\n", tid, bound);

		    // bound has been reset, invalidate previous solution
		    if (have_solution && !_fd_bound_check(solution))
		      {
			fd__trace("[%d] invalidating current solution\n", tid);

			status = FD_NOSOLUTION;
			have_solution = false;
		      }
		  }
		else
		  fd__trace("[%d] invalid bound %d\n", tid, bound);
	      }

	      break;

	    /* counting solutions internal events (from the workers) */

	    case EV_INT_COUNT:
	      fd__trace("[%d] agent %d is done\n", tid, peer);

	      assert(_fd_counting_solutions);

	      running--;

#if STEAL_WORK < 2
	      done = running == 0 && (tid != 0 || live_procs == 1);
#else /* STEAL_WORK < 2 */
#ifdef ASK_EARLY
	      if (running == agents_to_run - 1)
		{
		  // the first agent halted and the other agents must
		  // be stopping soon; try to get work before they do

		  // no need if we already have a store saved up
		  if (fd_list_empty(saved_stores))
		    {
		      // ask the other processes for more work, unless this
		      // is the rank 0 process and all others have finished
		      if (tid != 0 || live_procs > 1)
			{
			  if (!supplier)
			    {
			      fd__trace("[%d] asking for work\n", tid);
			      _fd_broadcast(FD_MSG_FEED_ME, processes, tid);

			      waiting_on = FD_MSG_FEED_ME;
			      timeout = FEED1_TIMEOUT;
			      timeouts = 0;
			    }
			  else
			    {
			      // poll the other processes for work
			      polling = _fd_start_polling(ex_supplier = tid);

			      waiting_on = FD_MSG_SHARE;
			      if (polling == 0)
				timeout = 0;		// last resort polling
			      else
				timeout = POLL_TIMEOUT;	// set timeout
			    }

			  waiting_for_work = true;
			}
		    }
		}
#endif /* ASK_EARLY */

	      if (running == 0)
		{
		  _fd_store saved;

		  // see if there's some store saved up
		  if (saved = fd_list_remove(saved_stores))
		    {
		      fd__trace("[%d] using saved store\n", tid);

		      memcpy(store, saved,
			     fd_variables_count * sizeof(*saved));

		      count_singletons(store);

		      // restart the agents
		      running = _fd_restart_agents(tid, nagents);

		      free(saved);
		    }
#ifdef ASK_EARLY
		  else if (waiting_for_work)
		    {
		      // we're already trying to get work, let's
		      // wait and see how that fans out

		      break;
		    }
		  else if (getting_work_failed)
		    {
		      // if we already have tried to get work and
		      // failed, it's no use doing it again

		      done = tid != 0 || live_procs == 1;

		      break;
		    }
#endif /* ASK_EARLY */
		  else if (quitting || ready)
		    done = quitting && (tid != 0 || live_procs == 1);
		  else if (tid != 0 || live_procs > 1)
		    {
		      // there is at least one other process still running
		      if (!supplier)
			{
			  // ask the other processes for more work
			  fd__trace("[%d] asking for work\n", tid);
			  _fd_broadcast(FD_MSG_FEED_ME, processes, tid);

			  waiting_on = FD_MSG_FEED_ME;
			  timeout = FEED1_TIMEOUT;
			  timeouts = 0;
			}
		      else
			{
			  // poll the other processes for work
			  polling = _fd_start_polling(ex_supplier = tid);

			  waiting_on = FD_MSG_SHARE;
			  if (polling == 0)
			    timeout = 0;		// last resort polling
			  else
			    timeout = POLL_TIMEOUT;	// set timeout
			}

#ifdef ASK_EARLY
		      waiting_for_work = true;
#endif
		    }
		  else
		    done = 1;
		}
#endif /* STEAL_WORK < 2 */

	      break;

	    /* counting solutions external events (from the other processes) */

	    case EV_EXT_COUNT:
	      fd__trace("[%d] receiving count from %d\n", tid, peer);

	      assert(_fd_counting_solutions);

	      {
		unsigned long long ull;

		if (MPI_Recv(&ull, 1, MPI_LONG_LONG, peer, FD_MSG_COUNT,
			     MPI_COMM_WORLD, &mpi_status))
		  fd__fatal("MPI_Recv failed");

		external_solutions += ull;

		fd__trace("[%d] process %d found %llu solutions\n", tid, peer, ull);
	      }

	      gettimeofday(&to, NULL);
	      timersub(&to, &ti, &to);
#if defined(STATS_PROCS) || defined(TRACE)
	      fd__debug("[%d] process %d took %d.%06ds\n", tid, peer, to.tv_sec, to.tv_usec);
#endif

	      live_procs--;

#if STEAL_WORK >= 2
	      // become the supplier, so further requests for work will
	      // be answered
	      // XXX: some requests for work may get lost
	      // XXX: side effect: this process *will* try to supply work
	      supplier = 1;
#endif

	      done = running == 0 && live_procs == 1;
#ifdef ASK_EARLY
	      done = done && !waiting_for_work;
#endif

	      break;
#if STEAL_WORK >= 2

	    /* work stealing events (external) */

	    case EV_EXT_STORE:
	      fd__trace("[%d] process %d sent a store\n", tid, peer);

if (waiting_on == FD_MSG_ANY) fd__trace("[%d] recovered store\n", tid);

	      if (running == 0)
		{
		  // retrieve the store
		  _fd_recv_store(peer, FD_MSG_STORE, store);

		  // restart the agents
		  running = _fd_restart_agents(tid, nagents);

		  count_singletons(store);
		}
	      else
		{
		  // save the store just received for later
		  _fd_store new = calloc(fd_variables_count, sizeof(*new)); // XXX: NULL

		  // retrieve the store
		  _fd_recv_store(peer, FD_MSG_STORE, new);

		  fd_list_append(saved_stores, new);
		}

	      // this process becomes the supplier of work
	      // XXX: between the moment the store was sent and now
	      //      some requests for work may have been lost
	      supplier = !ready || tid == 0;

	      // cancel timeout
	      timeout = 0;
	      waiting_on = FD_MSG_NONE;

#ifdef ASK_EARLY
	      waiting_for_work = false;
#endif

	      break;
	    case EV_EXT_REQ_WORK:
	      fd__trace("[%d] process %d wants more work\n", tid, peer);

	      if (supplier)
		{
		  if (quitting || ready)
		    {
		      _fd_send(peer, FD_MSG_NO_WORK);

		      break;
		    }

		  if (_fd_share_work(peer, stores_pending + peer,
				     saved_stores, false))
		    supplier = 0;
		}

	      break;
	    case EV_EXT_NO_WORK:
	      fd__trace("[%d] process %d can't supply work\n", tid, peer);

	      // no work sharing after having been told to stop
	      if (quitting || ready)
		break;

	      // if the message is not expected, just ignore it
#ifndef ASK_EARLY
	      if (running > 0 || waiting_on != FD_MSG_FEED_ME)
#else
	      if (!waiting_for_work || waiting_on != FD_MSG_FEED_ME)
#endif
		break;

	      // poll the other processes for work
	      if ((polling = _fd_start_polling(ex_supplier = peer)) >= 0)
		{
		  waiting_on = FD_MSG_SHARE;
		  if (polling == 0)
		    timeout = 0;		// last resort polling
		  else
		    timeout = POLL_TIMEOUT;	// set timeout
		}
	      else
		{
		  // no additional work could be found
		  if (tid != 0)
#ifndef ASK_EARLY
		    done = 1;
#else
		    done = running == 0;
#endif
		  else
		    {
		      // become the supplier, to answer further requests
		      // for work, even if negatively
		      supplier = 1;

		      done = running == 0 && live_procs == 1;
		    }

		  // cancel timeout
		  timeout = 0;

#ifdef ASK_EARLY
		  waiting_for_work = false;
		  getting_work_failed = true;
#endif
		}

	      break;
	    case EV_EXT_NO_SHARE:
	      fd__trace("[%d] process %d can't share work\n", tid, peer);

	      // no work sharing after having been told to stop
	      if (quitting || ready)
		break;

	      // if the message is not expected, just ignore it
#ifndef ASK_EARLY
	      if (running > 0 || waiting_on != FD_MSG_SHARE)
#else
	      if (!waiting_for_work || waiting_on != FD_MSG_SHARE)
#endif
		break;

	      if (peer != polling)
		{
		  fd__trace("[%d] polling %d but got answer from %d\n",
			    tid, polling, peer);

		  // belated answer, ignore
		  break;
		}

	      // try the next process
	      if ((polling = _fd_poll_next(polling, ex_supplier)) >= 0)
		{
		  if (polling == 0)
		    timeout = 0;		// last resort polling
		  else
		    timeout = POLL_TIMEOUT;	// reset timeout
		}
	      else
		{
		  // no additional work could be found
		  if (tid != 0)
#ifndef ASK_EARLY
		    {
		      quitting = true;
		      done = 1;
		    }
#else
		    done = running == 0;
#endif
		  else
		    {
		      // become the supplier, to answer further requests
		      // for work, even if negatively
		      supplier = 1;

		      done = running == 0 && live_procs == 1;
		    }

		  // cancel timeout
		  timeout = 0;

#ifdef ASK_EARLY
		  waiting_for_work = false;
		  getting_work_failed = true;
#endif
		}

	      break;
	    case EV_EXT_REQ_SHARE:
	      fd__trace("[%d] process %d really wants work\n", tid, peer);

	      // if we're idle, there's nothing to share
	      if (running == 0 || quitting || ready) // XXX: could be < nagents
		{
		  // notify the asking process that there really isn't
		  // work to be shared
		  fd__trace("[%d] no work to share with %d\n", tid, peer);
		  _fd_send(peer, FD_MSG_NO_SHARE);

		  break;
		}

	      // see if there is work to share
	      if (_fd_share_work(peer, stores_pending + peer, saved_stores,
				 true))
		supplier = 0;

	      break;
	    case EV_EXT_READY:
	      // peer has no work and no outgoing communications pending
	      fd__trace("[%d] %d is done\n", tid, peer);

	      assert(tid == 0);

	      mark_process_idle(peer);

	      // XXX: in mark_process_idle()
	      //fd__trace("[%d] sending 2nd quit\n", tid);
	      //_fd_broadcast(FD_MSG_QUIT, processes, tid);

	      //done = running == 0 && live_procs == 1;

	      // become the supplier, to answer further requests
	      // for work, even if negatively
	      supplier = 1;

	      break;
	    case EV_EXT_QUIT:
	      // possibilities for finding work have been exhausted
	      fd__trace("[%d] %d told me to stop\n", tid, peer);

	      assert(tid != 0);

	      quitting = true;

	      done = running == 0 && ready;

	      if (done)
		waiting_on = FD_MSG_NONE;

	      break;
#endif /* STEAL_WORK >= 2 */
	    default:
	      fd__debug("[%d] unknown event in main loop: %d\n", tid, event);
	      fd__fatal("unknown event in main loop");
	    }

#if STEAL_WORK >= 2
if (done) fd__trace("[%d] done (quitting = %d, ready = %d, timeout = %d)\n", tid, quitting, ready, timeout);

	  // if it is important that the full search space is explored,
	  // make an extra effort for receiving messages that may have
	  // been delayed
	  if (done && waiting_on != FD_MSG_ANY &&
	      (_fd_counting_solutions ||
	       _fd_optimising ||
	       status != FD_OK && !stopped))
	    {
	      fd__trace("[%d] checking for any event before leaving\n", tid);

	      done = 0;
	      waiting_on = FD_MSG_ANY;
	      timeout = 1;

	      continue;
	    }

#ifdef MPICH2
#  define _count count
#  define _cancelled cancelled
#elif OPEN_MPI && OMPI_MAJOR_VERSION == 1 && OMPI_MINOR_VERSION > 4
#  define _count _ucount
#endif

	  // try to ensure that no store sent to another process is
	  // lost, which may happen when the process it was addressed
	  // to stopped before receiving it
	  // this applies when optimising or counting all solutions,
	  // and when looking for the first solution and none has yet
	  // been found

	  if (done && !ready && (status != FD_OK && !stopped || _fd_optimising))
	    {
	      bool cancel_pending = false;

	      for (i = 0; i < processes; ++i)
		{
		  int tries = 3;
		  MPI_Status mpi_status = { 99, 99, 99, 99, 99 };
		  int mpi_flag;

		  if (MPI_Test(stores_pending + i, &mpi_flag, &mpi_status))
		    perror("MPI_Test");

		  if (!mpi_flag)	// request hasn't completed
		    {
		      fd__trace("[%d] recovering outstanding store to %d\n",
				tid, i);

		      // try cancelling the send (or the cancel?)
		      if (MPI_Cancel(stores_pending + i))
			perror("MPI_Cancel");

		      fd__trace("[%d] cancelled request\n", tid);

		      // allow the cancel some time to complete
		      do
			{
			  if (MPI_Test(stores_pending + i, &mpi_flag,
				       &mpi_status))
			    perror("MPI_Test");

			  if (mpi_flag)
			    break;

			  fd__trace("[%d] cancelling store to %d didn't complete\n",
				    tid, i);

			  if (--tries == 0)
			    break;

			  millisleep(5);
			}
		      while (!mpi_flag);

		      if (!mpi_flag)  // cancel didn't complete, give up for now
			{
			  fd__trace("[%d] giving up cancelling store to %i for now\n",
				    tid, i);

			  // if no store is cancelled, will have to wait
			  // for this cancellation to complete; in the
			  // meantime, will check the incoming messages
			  done = 0;
			  timeout = 1;
			  waiting_on = FD_MSG_ANY;

			  continue;
			}

		      fd__trace("[%d] status = (%d, %d, %d, %d, %d)\n", tid,
				mpi_status.MPI_SOURCE, mpi_status.MPI_TAG,
				mpi_status.MPI_ERROR, mpi_status._count,
				mpi_status._cancelled);

		      MPI_Test_cancelled(&mpi_status, &mpi_flag);

		      if (!mpi_flag)
			{
			  fd__trace("[%d] cancel did not succeed\n", tid);

			  continue;
			}

		      fd__trace("[%d] cancel succeeded\n", tid);
		    }
		  else
		    {
		      // this request may already have been cancelled
		      MPI_Test_cancelled(&mpi_status, &mpi_flag);

		      if (!mpi_flag)
			continue;	// the send has completed

		      fd__trace("[%d] previously cancelled store to %d\n",
				  tid, i);
		    }

		  assert(stores_pending[i] == MPI_REQUEST_NULL);

		  // recover the store
		  memcpy(store, _fd_processes_stores[i],
			 fd_variables_count * sizeof(*store));

		  count_singletons(store);

		  // and restart
		  running = _fd_restart_agents(tid, nagents);

		  done = 0;

		  // cancel timeout
		  timeout = 0;

		  break;
		}

	      if (done)	// no store was cancelled
		{
		  if (tid != 0)
		    {
		      fd__trace("[%d] ready to leave\n", tid);

		      _fd_send(0, FD_MSG_READY);

		      // cease to be the supplier
		      supplier = 0;
		    }

		  done = tid == 0 && live_procs == 1;
		  timeout = 0;
		  ready = true;
		  quitting = false;
		}
	    }

#ifdef MPICH2
#  undef _count
#  undef _cancelled
#elif OPEN_MPI && OMPI_MAJOR_VERSION == 1 && OMPI_MINOR_VERSION > 4
#  undef _count
#endif
#endif /* STEAL_WORK >= 2 */
	}
      while (!done);

      if (!_fd_counting_solutions)
	{
	  // send word that this process is done
	  if (tid == 0)
	    {
	      // stop the other processes
	      if (live_procs > 1)
		{
		  fd__trace("[%d] stopping all processes\n", tid);
		  _fd_broadcast(FD_MSG_DONE, processes, tid);
		}
	      else
		fd__trace("[%d] all other processes already stopped\n", tid);

	      if (_fd_optimising && status == FD_OK)
		{
		  assert(have_solution);

		  memcpy(store, solution, fd_variables_count * sizeof(*store));

		  if (ts.tv_sec)
		    {
		      timersub(&ts, &ti, &ts);
		      fd__trace("[%d] time to solution was %d.%06ds\n", tid,
				ts.tv_sec, ts.tv_usec);
		    }
		}
	    }
	  else if (!stopped)
	    {
	      MPI_Request mpi_request;

	      if (status == FD_OK)
		{
		  MPI_Request *mpi_request = malloc(sizeof(MPI_Request));

		  fd__trace("[%d] sending solution\n", tid);

		  if (_fd_optimising)
		    assert(have_solution);

		  _fd_send_store(0, FD_MSG_SOLUTION, solution, mpi_request,
				 false);

		  if (ts.tv_sec)
		    {
		      timersub(&ts, &ti, &ts);
		      fd__trace("[%d] time to solution was %d.%06ds\n", tid,
				ts.tv_sec, ts.tv_usec);
		    }

		  fd_list_append(pending_requests, mpi_request);
		}
	      else
		{
		  fd__trace("[%d] sending failure notice\n", tid);
		  _fd_send(0, FD_MSG_FAIL);
		}
	    }

	  // stop all the agents
	  fd__trace("[%d] stopping agents\n", tid);
	  for (i = 0; i < started; ++i)
	    {
	      int s;

	      if ((s = pthread_cancel(threads[i])) && s != ESRCH)
		fd__debug("[%d] error cancelling thread %d = %d\n", tid, i, s);
	    }

	  // make sure all agents have stopped before exiting
	  if (!_fd_optimising && (status == FD_OK || stopped))
	    for (i = 0; i < started; ++i)
	      {
		int s;

		fd__trace("[%d] joining agent %d\n", tid, i);
		if ((s = pthread_join(threads[i], NULL)) && s != ESRCH)
		  fd__debug("[%d] error joining thread %d = %d\n", tid, i, s);
	      }

#if STEAL_WORK >= 2
	  fd_list_dispose_deep(saved_stores, free);
#endif /* STEAL_WORK >= 2 */
	}
      else /* _fd_counting_solutions != 0 */
	{
	  // send solutions count to the rank 0 process, telling it that we
	  // are done
	  if (tid != 0)
	    _fd_send_data(0, FD_MSG_COUNT, &total_solutions, 1, MPI_LONG_LONG);

#if STEAL_WORK >= 2
	  // stop all the agents, which are waiting for work
	  fd__trace("[%d] stopping agents\n", tid);
	  for (i = 0; i < started; ++i)
	    {
	      int s;

	      if ((s = pthread_cancel(threads[i])) && s != ESRCH)
		fd__debug("[%d] error cancelling thread %d = %d\n", tid, i, s);
	    }

	  // make sure all agents have stopped before exiting
	  for (i = 0; i < started; ++i)
	    {
	      int s;

	      fd__trace("[%d] joining agent %d\n", tid, i);
	      if ((s = pthread_join(threads[i], NULL)) && s != ESRCH)
		fd__debug("[%d] error joining thread %d = %d\n", tid, i, s);
	    }

	  if (!fd_list_empty(saved_stores))
	    fd__fatal("saved_stores not empty on exit");

	  fd_list_dispose(saved_stores);
#endif /* STEAL_WORK >= 2 */
	}

      _fd_statistics_steal();
    }
  else /* agents == 0 */
    {
      _fd_agents_stores[0] = store;

      _fd_init_store_depository(1);

      if (!_fd_counting_solutions)
	status = _fd_dsearch(_fd_variables, 0);
      else
	total_solutions = _fd_count_solutions(_fd_variables, 0);

      store = _fd_agents_stores[0]; // restore its value so it can be freed
    }

  if (tid != 0)
    _fd_exit_process(tid);

  if (_fd_counting_solutions)
    {
      fd__output("found %llu solutions\n", total_solutions + external_solutions); // XXX

      status = FD_NOSOLUTION; // XXX
    }

  gettimeofday(&to, NULL);
  timersub(&to, &ti, &to);
#if defined(STATS_PROCS) || defined(TRACE)
  fd__debug("process %d took %d.%06ds\n", tid, to.tv_sec, to.tv_usec);
#endif

  return status;
}