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
|
/** @file mpi.c
*
* MPI dependent functions of parform
*
* This file contains all the functions for the parallel version of form3 that
* explicitly need to call mpi routines. This is the only file that really
* needs to be linked to the mpi-library!
*/
/* #[ License : */
/*
* Copyright (C) 1984-2026 J.A.M. Vermaseren
* When using this file you are requested to refer to the publication
* J.A.M.Vermaseren "New features of FORM" math-ph/0010025
* This is considered a matter of courtesy as the development was paid
* for by FOM the Dutch physics granting agency and we would like to
* be able to track its scientific use to convince FOM of its value
* for the community.
*
* This file is part of FORM.
*
* FORM is free software: you can redistribute it and/or modify it under the
* terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later
* version.
*
* FORM 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 FORM. If not, see <http://www.gnu.org/licenses/>.
*/
/* #] License : */
/*
#[ Includes and variables :
*/
#include <limits.h>
#include "form3.h"
#ifdef MPICH_PROFILING
# include "mpe.h"
#endif
#ifdef MPIDEBUGGING
#include "mpidbg.h"
#endif
/*[12oct2005 mt]:*/
/*
Today there was some cleanup, some stuff is moved into another place
in this file, and PF.packsize is removed and PF_packsize is used
instead. It is rather difficult to proper comment it, so not all these
changing are marked by "[12oct2005 mt]"
*/
#define PF_PACKSIZE 1600
/*
Size in bytes, will be initialized soon as
PF_packsize=PF_PACKSIZE/sizeof(int)*sizeof(int); for possible
future developing we prefer to do this initialization not here,
but in PF_LibInit:
*/
static int PF_packsize = 0;
static MPI_Status PF_status;
LONG PF_maxDollarChunkSize = 0; /*:[04oct2005 mt]*/
static int PF_ShortPackInit(void);
static int PF_longPackInit(void); /*:[12oct2005 mt]*/
/**
* A macro which exits the caller with a non-zero return value if \a err
* is not MPI_SUCCESS.
*
* @param err The return value of a MPI function to be checked.
*
* @remark The MPI standard defines MPI_SUCCESS == 0. Then (_tmp_err == 0) appears
* twice and we can expect the second evaluation will be eliminated by
* the compiler optimization.
*/
#define MPI_ERRCODE_CHECK(err) \
do { \
int _tmp_err = (err); \
if ( _tmp_err != MPI_SUCCESS ) return _tmp_err != 0 ? _tmp_err : -1; \
} while (0)
/*
#] Includes and variables :
#[ PF_RealTime :
*/
/**
* Returns the realtime in 1/100 sec. as a LONG.
*
* @param i the timer will be reset if i == 0.
* @return the real elapsed time in 1/100 second.
*/
LONG PF_RealTime(int i)
{
static double starttime;
if ( i == PF_RESET ) {
starttime = MPI_Wtime();
return((LONG)0);
}
return((LONG)( 100. * (MPI_Wtime() - starttime) ) );
}
/*
#] PF_RealTime :
#[ PF_LibInit :
*/
/**
* Performs all library dependent initializations.
*
* @param argcp pointer to the number of arguments.
* @param argvp pointer to the arguments.
* @return 0 if OK, nonzero on error.
*/
int PF_LibInit(int *argcp, char ***argvp)
{
int ret;
ret = MPI_Init(argcp,argvp);
if ( ret != MPI_SUCCESS ) return(ret);
ret = MPI_Comm_rank(PF_COMM,&PF.me);
if ( ret != MPI_SUCCESS ) return(ret);
ret = MPI_Comm_size(PF_COMM,&PF.numtasks);
if ( ret != MPI_SUCCESS ) return(ret);
/* Initialization of packed communications. */
PF_packsize = PF_PACKSIZE/sizeof(int)*sizeof(int);
if ( PF_ShortPackInit() ) return -1;
if ( PF_longPackInit() ) return -1;
{/*Block*/
int bytes, totalbytes=0;
/*
There is one problem with maximal possible packing: there is no API to
convert bytes to the record number. So, here we calculate the buffer
size needed for storing dollarvars:
LONG PF_maxDollarChunkSize is the size for the portion of the dollar
variable buffer suitable for broadcasting. This variable should be
visible from parallel.c
Evaluate PF_Pack(numterms,1,PF_INT):
*/
if ( ( ret = MPI_Pack_size(1,PF_INT,PF_COMM,&bytes) )!=MPI_SUCCESS )
return(ret);
totalbytes+=bytes;
/*
Evaluate PF_Pack( newsize,1,PF_LONG):
*/
if ( ( ret = MPI_Pack_size(1,PF_LONG,PF_COMM,&bytes) )!=MPI_SUCCESS )
return(ret);
totalbytes += bytes;
/*
Now available room is PF_packsize-totalbytes
*/
totalbytes = PF_packsize-totalbytes;
/*
Now totalbytes is the size of chunk in bytes.
Evaluate this size in number of records:
Rough estimate:
*/
PF_maxDollarChunkSize=totalbytes/sizeof(WORD);
/*
Go to the up limit:
*/
do {
if ( ( ret = MPI_Pack_size(
++PF_maxDollarChunkSize,PF_WORD,PF_COMM,&bytes) )!=MPI_SUCCESS )
return(ret);
} while ( bytes<totalbytes );
/*
Now the chunk size is too large
And now evaluate the exact value:
*/
do {
if ( ( ret = MPI_Pack_size(
--PF_maxDollarChunkSize,PF_WORD,PF_COMM,&bytes) )!=MPI_SUCCESS )
return(ret);
} while ( bytes>totalbytes );
/*
Now PF_maxDollarChunkSize is the size of chunk of PF_WORD fitting the
buffer <= (PF_packsize-PF_INT-PF_LONG)
*/
}/*Block*/
return(0);
}
/*
#] PF_LibInit :
#[ PF_LibTerminate :
*/
/**
* Exits mpi, when there is an error either indicated or happening,
* returnvalue is 1, else returnvalue is 0.
*
* @param error an error code.
* @return 0 if OK, nonzero on error.
*/
int PF_LibTerminate(int error)
{
DUMMYUSE(error);
return(MPI_Finalize());
}
/*
#] PF_LibTerminate :
#[ PF_Probe :
*/
/**
* Probes the next incoming message.
* If src == PF_ANY_SOURCE this operation is blocking,
* otherwise nonblocking.
*
* @param[in,out] src the source process number. In output, the process number of actual found source.
* @return the tag value of the next incoming message if found,
* 0 if a nonblocking probe (input src != PF_ANY_SOURCE) did not
* find any messages. A negative returned value indicates an error.
*/
int PF_Probe(int *src)
{
int ret, flag;
if ( *src == PF_ANY_SOURCE ) { /*Blocking call*/
ret = MPI_Probe(*src,MPI_ANY_TAG,PF_COMM,&PF_status);
flag = 1;
}
else { /*Non-blocking call*/
ret = MPI_Iprobe(*src,MPI_ANY_TAG,PF_COMM,&flag,&PF_status);
}
*src = PF_status.MPI_SOURCE;
if ( ret != MPI_SUCCESS ) { if ( ret > 0 ) ret *= -1; return(ret); }
if ( !flag ) return(0);
return(PF_status.MPI_TAG);
}
/*
#] PF_Probe :
#[ PF_ISendSbuf :
*/
/**
* Nonblocking send operation. It sends everything from \c buff to \c fill of the
* active buffer.
* Depending on \a tag it also can do waiting for other sends to finish or
* set the active buffer to the next one.
*
* @param to the destination process number.
* @param tag the message tag.
* @return 0 if OK, nonzero on error.
*/
int PF_ISendSbuf(int to, int tag)
{
PF_BUFFER *s = PF.sbuf;
int a = s->active;
int size = s->fill[a] - s->buff[a];
int r = 0;
static int finished;
s->fill[a] = s->buff[a];
if ( s->numbufs == 1 ) {
r = MPI_Ssend(s->buff[a],size,PF_WORD,MASTER,tag,PF_COMM);
if ( r != MPI_SUCCESS ) {
fprintf(stderr,"[%d|%d] PF_ISendSbuf: MPI_Ssend returns: %d \n",
PF.me,(int)AC.CModule,r);
fflush(stderr);
return(r);
}
return(0);
}
switch ( tag ) { /* things to do before sending */
case PF_TERM_MSGTAG:
if ( PF.sbuf->request[to] != MPI_REQUEST_NULL)
r = MPI_Wait(&PF.sbuf->request[to],&PF.sbuf->retstat[to]);
if ( r != MPI_SUCCESS ) return(r);
break;
default:
break;
}
r = MPI_Isend(s->buff[a],size,PF_WORD,to,tag,PF_COMM,&s->request[a]);
if ( r != MPI_SUCCESS ) return(r);
switch ( tag ) { /* things to do after initialising sending */
case PF_TERM_MSGTAG:
finished = 0;
break;
case PF_ENDSORT_MSGTAG:
if ( ++finished == PF.numtasks - 1 )
r = MPI_Waitall(s->numbufs,s->request,s->status);
if ( r != MPI_SUCCESS ) return(r);
break;
case PF_BUFFER_MSGTAG:
if ( ++s->active >= s->numbufs ) s->active = 0;
while ( s->request[s->active] != MPI_REQUEST_NULL ) {
r = MPI_Waitsome(s->numbufs,s->request,&size,s->index,s->retstat);
if ( r != MPI_SUCCESS ) return(r);
}
break;
case PF_ENDBUFFER_MSGTAG:
if ( ++s->active >= s->numbufs ) s->active = 0;
r = MPI_Waitall(s->numbufs,s->request,s->status);
if ( r != MPI_SUCCESS ) return(r);
break;
default:
return(-99);
break;
}
return(0);
}
/*
#] PF_ISendSbuf :
#[ PF_RecvWbuf :
*/
/**
* Blocking receive of a \c WORD buffer.
*
* @param[out] b the buffer to store the received data.
* @param[in,out] s the size of the buffer. The output value is the actual size of the received data.
* @param[in,out] src the source process number. The output value is the process number of actual source.
* @return the received message tag. A negative value indicates an error.
*/
int PF_RecvWbuf(WORD *b, LONG *s, int *src)
{
int i, r = 0;
r = MPI_Recv(b,(int)*s,PF_WORD,*src,PF_ANY_MSGTAG,PF_COMM,&PF_status);
if ( r != MPI_SUCCESS ) { if ( r > 0 ) r *= -1; return(r); }
r = MPI_Get_count(&PF_status,PF_WORD,&i);
if ( r != MPI_SUCCESS ) { if ( r > 0 ) r *= -1; return(r); }
*s = (LONG)i;
*src = PF_status.MPI_SOURCE;
return(PF_status.MPI_TAG);
}
/*
#] PF_RecvWbuf :
#[ PF_IRecvRbuf :
*/
/**
* Posts nonblocking receive for the active receive buffer.
* The buffer is filled from \c full to \c stop.
*
* @param r the \c PF_BUFFER struct for the nonblocking receive.
* @param bn the index of the cyclic buffer.
* @param from the source process number.
* @return 0 if OK, nonzero on error.
*/
int PF_IRecvRbuf(PF_BUFFER *r, int bn, int from)
{
int ret;
r->type[bn] = PF_WORD;
if ( r->numbufs == 1 ) {
r->tag[bn] = MPI_ANY_TAG;
r->from[bn] = from;
}
else {
ret = MPI_Irecv(r->full[bn],(int)(r->stop[bn] - r->full[bn]),PF_WORD,from,
MPI_ANY_TAG,PF_COMM,&r->request[bn]);
if (ret != MPI_SUCCESS) { if(ret > 0) ret *= -1; return(ret); }
}
return(0);
}
/*
#] PF_IRecvRbuf :
#[ PF_WaitRbuf :
*/
/**
* Waits for the buffer \a bn to finish a pending nonblocking
* receive. It returns the received tag and in <tt>*size</tt> the number of field
* received.
* If the receive is already finished, just return the flag and size,
* else wait for it to finish, but also check for other pending receives.
*
* @param r the \c PF_BUFFER struct for the pending nonblocking receive.
* @param bn the index of the cyclic buffer.
* @param[out] size the actual size of received data.
* @return the received message tag. A negative value indicates an error.
*/
int PF_WaitRbuf(PF_BUFFER *r, int bn, LONG *size)
{
int ret, rsize;
if ( r->numbufs == 1 ) {
*size = r->stop[bn] - r->full[bn];
ret = MPI_Recv(r->full[bn],(int)*size,r->type[bn],r->from[bn],r->tag[bn],
PF_COMM,&(r->status[bn]));
if ( ret != MPI_SUCCESS ) { if ( ret > 0 ) ret *= -1; return(ret); }
ret = MPI_Get_count(&(r->status[bn]),r->type[bn],&rsize);
if ( ret != MPI_SUCCESS ) { if ( ret > 0 ) ret *= -1; return(ret); }
if ( rsize > *size ) return(-99);
*size = (LONG)rsize;
}
else {
while ( r->request[bn] != MPI_REQUEST_NULL ) {
ret = MPI_Waitsome(r->numbufs,r->request,&rsize,r->index,r->retstat);
if ( ret != MPI_SUCCESS ) { if ( ret > 0 ) ret *= -1; return(ret); }
while ( --rsize >= 0 ) r->status[r->index[rsize]] = r->retstat[rsize];
}
ret = MPI_Get_count(&(r->status[bn]),r->type[bn],&rsize);
if ( ret != MPI_SUCCESS ) { if ( ret > 0 ) ret *= -1; return(ret); }
*size = (LONG)rsize;
}
return(r->status[bn].MPI_TAG);
}
/*
#] PF_WaitRbuf :
#[ PF_Bcast :
*/
/**
* Broadcasts a message from the master to slaves.
*
* @param[in,out] buffer the starting address of buffer. The contents in this buffer
* on the master will be transferred to those on the slaves.
* @param count the length of the buffer in bytes.
* @return 0 if OK, nonzero on error.
*/
int PF_Bcast(void *buffer, int count)
{
if ( MPI_Bcast(buffer,count,MPI_BYTE,MASTER,PF_COMM) != MPI_SUCCESS )
return(-1);
return(0);
}
/*
#] PF_Bcast :
#[ PF_RawSend :
*/
/**
* Sends \a l bytes from \a buf to \a dest.
* Returns 0 on success, or -1.
*
* @param dest the destination process number.
* @param buf the send buffer.
* @param l the size of data in the send buffer in bytes.
* @param tag the message tag.
* @return 0 if OK, nonzero on error.
*/
int PF_RawSend(int dest, void *buf, LONG l, int tag)
{
int ret=MPI_Ssend(buf,(int)l,MPI_BYTE,dest,tag,PF_COMM);
if ( ret != MPI_SUCCESS ) return(-1);
return(0);
}
/*
#] PF_RawSend :
#[ PF_RawRecv :
*/
/**
* Receives not more than \a thesize bytes from \a src,
* returns the actual number of received bytes, or -1 on failure.
*
* @param[in,out] src the source process number. In output, that of the actual received message.
* @param[out] buf the receive buffer.
* @param thesize the size of the receive buffer in bytes.
* @param[out] tag the message tag of the actual received message.
* @return the actual sizeof received data in bytes, or -1 on failure.
*/
LONG PF_RawRecv(int *src,void *buf,LONG thesize,int *tag)
{
MPI_Status stat;
int ret=MPI_Recv(buf,(int)thesize,MPI_BYTE,*src,MPI_ANY_TAG,PF_COMM,&stat);
if ( ret != MPI_SUCCESS ) return(-1);
if ( MPI_Get_count(&stat,MPI_BYTE,&ret) != MPI_SUCCESS ) return(-1);
*tag = stat.MPI_TAG;
*src = stat.MPI_SOURCE;
return(ret);
}
/*
#] PF_RawRecv :
#[ PF_RawProbe :
*/
/**
* Probes an incoming message.
*
* @param[in,out] src the source process number. In output, that of the actual received message.
* @param[in,out] tag the message tag. In output, that of the actual received message.
* @param[out] bytesize the size of incoming data in bytes.
* @return 0 if OK, nonzero on error.
*/
int PF_RawProbe(int *src, int *tag, int *bytesize)
{
MPI_Status stat;
int srcval = src != NULL ? *src : PF_ANY_SOURCE;
int tagval = tag != NULL ? *tag : PF_ANY_MSGTAG;
int ret = MPI_Probe(srcval, tagval, PF_COMM, &stat);
if ( ret != MPI_SUCCESS ) return -1;
if ( src != NULL ) *src = stat.MPI_SOURCE;
if ( tag != NULL ) *tag = stat.MPI_TAG;
if ( bytesize != NULL ) {
ret = MPI_Get_count(&stat, MPI_BYTE, bytesize);
if ( ret != MPI_SUCCESS ) return -1;
}
return 0;
}
/*
#] PF_RawProbe :
#[ The pack buffer :
#[ Variables :
*/
/*
* The pack buffer with the fixed size (= PF_packsize).
*/
static UBYTE *PF_packbuf = NULL;
static UBYTE *PF_packstop = NULL;
static int PF_packpos = 0;
/*
#] Variables :
#[ PF_ShortPackInit :
*/
/**
* Initializes buffers for "short" packed communications.
* PF_packsize must be set before calling this function.
*
* @return 0 if OK, nonzero on error.
*/
static int PF_ShortPackInit(void)
{
PF_packbuf = (UBYTE *)Malloc1(sizeof(UBYTE) * PF_packsize, "PF_ShortPackInit");
if ( PF_packbuf == NULL ) return -1;
PF_packstop = PF_packbuf + PF_packsize;
return 0;
}
/*
#] PF_ShortPackInit :
#[ PF_InitPackBuf :
*/
/**
* Initializes the pack buffer for the next communication.
*
* @return 0 if OK, nonzero on error.
*/
static inline int PF_InitPackBuf(void)
{
/*
This is definitely not the best place for allocating the
buffer! Moved to PF_LibInit():
if ( PF_packbuf == 0 ) {
PF_packbuf = (UBYTE *)Malloc1(sizeof(UBYTE)*PF.packsize,"PF_InitPackBuf");
if ( PF_packbuf == 0 ) return(-1);
PF_packstop = PF_packbuf + PF.packsize;
}
*/
PF_packpos = 0;
return(0);
}
/*
#] PF_InitPackBuf :
#[ PF_PrintPackBuf :
*/
/**
* Prints the contents in the pack buffer.
*
* @param s a message to be shown.
* @param size the length of the buffer to be shown.
* @return 0 if OK, nonzero on error.
*/
int PF_PrintPackBuf(char *s, int size)
{
#ifdef NOMESPRINTYET
/*
The use of printf should be discouraged. The results are flushed to
the output at unpredictable moments. We should use printf only
during startup when MesPrint doesn't have its buffers and output
channels initialized.
*/
int i;
printf("[%d] %s: ",PF.me,s);
for(i=0;i<size;i++) printf("%d ",PF_packbuf[i]);
printf("\n");
#else
MesPrint("[%d] %s: %a",PF.me,s,size,(WORD *)(PF_packbuf));
#endif
return(0);
}
/*
#] PF_PrintPackBuf :
#[ PF_PreparePack :
*/
/**
* Prepares for the next pack operations on the sender.
*
* @return 0 if OK, nonzero on error.
*/
int PF_PreparePack(void)
{
return PF_InitPackBuf();
}
/*
#] PF_PreparePack :
#[ PF_Pack :
*/
/**
* Adds data into the pack buffer.
*
* @param buffer the pointer to the buffer storing the data to be packed.
* @param count the number of elements in the buffer.
* @param type the data type of elements in the buffer.
* @return 0 if OK, nonzero on error.
*/
int PF_Pack(const void *buffer, size_t count, MPI_Datatype type)
{
int err, bytes;
if ( count > INT_MAX ) return -99;
err = MPI_Pack_size((int)count, type, PF_COMM, &bytes);
MPI_ERRCODE_CHECK(err);
if ( PF_packpos + bytes > PF_packstop - PF_packbuf ) return -99;
err = MPI_Pack((void *)buffer, (int)count, type, PF_packbuf, PF_packsize, &PF_packpos, PF_COMM);
MPI_ERRCODE_CHECK(err);
return 0;
}
/*
#] PF_Pack :
#[ PF_Unpack :
*/
/**
* Retrieves the next data in the pack buffer.
*
* @param[out] buffer the pointer to the buffer to store the unpacked data.
* @param count the number of elements of data to be received.
* @param type the data type of elements of data to be received.
* @return 0 if OK, nonzero on error.
*/
int PF_Unpack(void *buffer, size_t count, MPI_Datatype type)
{
int err;
if ( count > INT_MAX ) return -99;
err = MPI_Unpack(PF_packbuf, PF_packsize, &PF_packpos, buffer, (int)count, type, PF_COMM);
MPI_ERRCODE_CHECK(err);
return 0;
}
/*
#] PF_Unpack :
#[ PF_PackString :
*/
/**
* Packs a string \a str into the packed buffer PF_packbuf, including
* the trailing zero.
*
* The first element (PF_INT) is the length of the packed portion of
* the string. If the string does not fit to the buffer PF_packbuf,
* the function packs only the initial portion. It returns
* the number of packed bytes, so if (str[length-1]=='\0') then the whole
* string fits to the buffer, if not, then the rest (str+length) must be
* packed and send again. On error, the function returns the negative
* error code.
*
* One exception: the string "\0!\0" is used as an image of the NULL,
* so all 3 characters will be packed.
*
* @param str a string to be packed.
* @return the number of packed bytes, or a negative value on failure.
*/
int PF_PackString(const UBYTE *str)
{
int ret,buflength,bytes,length;
/*
length will be packed in the beginning.
Decrement buffer size by the length of the field "length":
*/
if ( ( ret = MPI_Pack_size(1,PF_INT,PF_COMM,&bytes) ) != MPI_SUCCESS )
return(ret);
buflength = PF_packsize - bytes;
/*
Calculate the string length (INCLUDING the trailing zero!):
*/
for ( length = 0; length < buflength; length++ ) {
if ( str[length] == '\0' ) {
length++; /* since the trailing zero must be accounted */
break;
}
}
/*
The string "\0!\0" is used as an image of the NULL.
*/
if ( ( str[0] == '\0' ) /* empty string */
&& ( str[1] == '!' ) /* Special case? */
&& ( str[2] == '\0' ) /* Yes, pass 3 initial symbols */
) length += 2; /* all 3 characters will be packed */
length++; /* Will be decremented in the following loop */
/*
The problem: packed size of byte may be not equal 1! So first, suppose
it is 1, and if this is not the case decrease the length of the string
until it fits the buffer:
*/
do {
if ( ( ret = MPI_Pack_size(--length,PF_BYTE,PF_COMM,&bytes) )
!= MPI_SUCCESS ) return(ret);
} while ( bytes > buflength );
/*
Note, now if str[length-1] == '\0' then the string fits to the buffer
(INCLUDING the trailing zero!);if not, the rest must be packed further!
Pack the length to PF_packbuf:
*/
if ( ( ret = MPI_Pack(&length,1,PF_INT,PF_packbuf,PF_packsize,
&PF_packpos,PF_COMM) ) != MPI_SUCCESS ) return(ret);
/*
Pack the string to PF_packbuf:
*/
if ( ( ret = MPI_Pack((UBYTE *)str,length,PF_BYTE,PF_packbuf,PF_packsize,
&PF_packpos,PF_COMM) ) != MPI_SUCCESS ) return(ret);
return(length);
}
/*
#] PF_PackString :
#[ PF_UnpackString :
*/
/**
* Unpacks a string to \a str from the packed buffer PF_packbuf, including
* the trailing zero.
*
* It returns the number of unpacked bytes, so if (str[length-1]=='\0')
* then the whole string was unpacked, if not, then the rest must be appended
* to (str+length). On error, the function returns the negative error code.
*
* @param[out] str the buffer to store the unpacked string
* @return the number of unpacked bytes, or a negative value on failure.
*/
int PF_UnpackString(UBYTE *str)
{
int ret,length;
/*
Unpack the length:
*/
if( (ret = MPI_Unpack(PF_packbuf,PF_packsize,&PF_packpos,
&length,1,PF_INT,PF_COMM))!= MPI_SUCCESS )
return(ret);
/*
Unpack the string:
*/
if ( ( ret = MPI_Unpack(PF_packbuf,PF_packsize,&PF_packpos,
str,length,PF_BYTE,PF_COMM) ) != MPI_SUCCESS ) return(ret);
/*
Now if str[length-1]=='\0' then the whole string
(INCLUDING the trailing zero!) was unpacked ;if not, the rest
must be unpacked to str+length.
*/
return(length);
}
/*
#] PF_UnpackString :
#[ PF_Send :
*/
/**
* Sends the contents in the pack buffer to the process specified by \a to.
*
* Example:
* @code
* if ( PF.me == SRC ) {
* PF_PreparePack();
* // Packing operations here...
* PF_Send(DEST, TAG);
* }
* else if ( PF.me == DEST ) {
* PF_Receive(SRC, TAG, &actual_src, &actual_tag);
* // Unpacking operations here...
* }
* @endcode
*
* @param to the destination process number.
* @param tag the message tag.
* @return 0 if OK, nonzero on error.
*/
int PF_Send(int to, int tag)
{
int err;
err = MPI_Ssend(PF_packbuf, PF_packpos, MPI_PACKED, to, tag, PF_COMM);
MPI_ERRCODE_CHECK(err);
return 0;
}
/*
#] PF_Send :
#[ PF_Receive :
*/
/**
* Receives data into the pack buffer from the process specified by \a src.
* This function allows &src == psrc or &tag == ptag.
* Either \a psrc or \a ptag can be NULL.
*
* See the example of PF_Send().
*
* @param src the source process number (can be PF_ANY_SOURCE).
* @param tag the source message tag (can be PF_ANY_TAG).
* @param[out] psrc the actual source process number of received message.
* @param[out] ptag the received message tag.
* @return 0 if OK, nonzero on error.
*/
int PF_Receive(int src, int tag, int *psrc, int *ptag)
{
int err;
MPI_Status status;
PF_InitPackBuf();
err = MPI_Recv(PF_packbuf, PF_packsize, MPI_PACKED, src, tag, PF_COMM, &status);
MPI_ERRCODE_CHECK(err);
if ( psrc ) *psrc = status.MPI_SOURCE;
if ( ptag ) *ptag = status.MPI_TAG;
return 0;
}
/*
#] PF_Receive :
#[ PF_Broadcast :
*/
/**
* Broadcasts the contents in the pack buffer on the master to those
* on the slaves.
*
* Example:
* @code
* if ( PF.me == MASTER ) {
* PF_PreparePack();
* // Packing operations here...
* }
* PF_Broadcast();
* if ( PF.me != MASTER ) {
* // Unpacking operations here...
* }
* @endcode
*
* @return 0 if OK, nonzero on error.
*/
int PF_Broadcast(void)
{
int err;
/*
* If PF_SHORTBROADCAST is defined, then the broadcasting will be performed in
* 2 steps. First, the size of the buffer will be broadcast, then the buffer of
* exactly used size. This should be faster with slow connections, but slower on
* SMP shmem MPI because of the latency.
*/
#ifdef PF_SHORTBROADCAST
int pos = PF_packpos;
#endif
if ( PF.me != MASTER ) {
err = PF_InitPackBuf();
if ( err ) return err;
}
#ifdef PF_SHORTBROADCAST
err = MPI_Bcast(&pos, 1, MPI_INT, MASTER, PF_COMM);
MPI_ERRCODE_CHECK(err);
err = MPI_Bcast(PF_packbuf, pos, MPI_PACKED, MASTER, PF_COMM);
#else
err = MPI_Bcast(PF_packbuf, PF_packsize, MPI_PACKED, MASTER, PF_COMM);
#endif
MPI_ERRCODE_CHECK(err);
return 0;
}
/*
#] PF_Broadcast :
#] The pack buffer :
#[ Long pack stuff :
#[ Explanations :
The problems here are:
1. We need to send/receive long dollar variables. For
preprocessor-defined dollarvars we used multiply
packing/broadcasting (see parallel.c:PF_BroadcastPreDollar())
since each variable must be broadcast immediately. For run-time
the changed dollar variables, collecting and broadcasting are
performed at the end of the module and all modified dollarvars
are transferred "at once", that is why the size of packed and
transferred buffers may be really very large.
2. There is some strange feature of MPI_Bcast() on Altix MPI
implementation, namely, sometimes it silently fails with big
buffers. For better performance, it would be useful to send one
big buffer instead of several small ones (since the latency is more
important than the bandwidth). That is why we need two different
sets of routines: for long point-to-point communication we collect
big re-allocatable buffer, the corresponding routines have the
prefix PF_longSingle, and for broadcasting we pack data into
several smaller buffers, the corresponding routines have the
prefix PF_longMulti.
Note, from portability reasons we cannot split large packed
buffer into small chunks, send them and collect back on the other
side, see "Advice to users" on page 180 MPI--The Complete Reference
Volume1, second edition.
OPTIMIZING:
We assume, for most communications, the single buffer of size
PF_packsize is enough.
How does it work:
For point-to-point, there is one big re-allocatable
buffer PF_longPackBuf with two integer positions: PF_longPackPos
and PF_longPackTop (due to re-allocatable character of the buffer,
it is better to use integers rather than pointers).
Each time of re-allocation, the size of the buffer
PF_longPackBuf is incremented by the same size of a "standard" chunk
PF_packsize.
For broadcasting there is one linked list (PF_longMultiRoot),
which contains either positions of a chunk of PF_longPackBuf, or
it's own buffer. This is done for better memory utilisation:
longSingle and longMulti are never used simultaneously.
When a new cell is needed for LongMulti packing, we increment
the counter PF_longPackN and just follow the list. If it is not
possible, we allocate the cell's own buffer and link it to the end
of the list PF_longMultiRoot.
When PF_longPackPos is reallocated, we link new chunks into
existing PF_longMultiRoot list before the first longMulti allocated
cell's own buffer. The pointer PF_longMultiLastChunk points to the last
cell of PF_longMultiRoot containing the pointer to the chunk of
PF_longPackBuf.
Initialization PF_longPackBuf is made by the function
PF_longSingleReset(). In the begin of the PF_longPackBuf it packs
the size of the last sent buffer. Upon sending, the program checks,
whether there was at list one re-allocation (PF_longPackN>1) .
If so, the sender first packs and sends small buffer
(PF_longPackSmallBuf) containing one integer number -- the
_negative_ new size of the send buffer. Getting the buffer, a
receiver unpacks one integer and checks whether it is <0 . If so,
the receiver will repeat receiving, but first it checks whether
it has enough buffer and increase it, if necessary.
Initialization PF_longMultiRoot is made by the function
PF_longMultiReset(). In the begin of the first chunk it packs
one integer -- the number 1. Upon sending, the program checks,
how many cells were packed (PF_longPackN). If more than 1, the
sender packs to the next cell the integer PF_longPackN, than
packs PF_longPackN pairs of integers -- the information about how many
times chunk on each cell was accessed by the packing procedure,
this information is contained by the nPacks field of the cell
structure, and how many non-complete items was at the end of this
chunk the structure field lastLen. Then the sender sends first
this auxiliary chunk.
The receiver unpacks the integer from obtained chunk and, if this
integer is more than 1, it gets more chunks, unpacking information
from the first auxiliary chunk into the corresponding nPacks
fields. Unpacking information from multiple chunks, the receiver
knows, when the chunk is expired and it must switch to the next cell,
successively decrementing corresponding nPacks field.
XXX: There are still some flaws:
PF_LongSingleSend/PF_LongSingleReceive may fail, for example, for data
transfers from the master to many slaves. Suppose that the master sends big
data to slaves, which needs an increase of the buffer of the receivers. For
the first data transfer, the master sends the new buffer size as the first
message, and then sends the data as the second message, because
PF_LongSinglePack records the increase of the buffer size on the master. For
the next time, however, the master sends the data without sending the new
buffer size, and then MPI_Recv fails due to the data overflow.
In parallel.c, they are used for the communication from slaves to the
master. In this case, this problem does not occur because the master always
has enough buffer.
The maximum size that PF_LongMultiBroadcast can broadcast is limited to
around 320kB because the current implementation tries to pack all
information of chained buffers into one buffer, whose size is PF_packsize
= 1600B.
#] Explanations :
#[ Variables :
*/
typedef struct longMultiStruct {
UBYTE *buffer; /* NULL if */
int bufpos; /* if >=0, PF_longPackBuf+bufpos is the chunk start */
int packpos; /* the current position */
int nPacks; /* How many times PF_longPack operates on this cell */
int lastLen; /* if > 0, the last packing didn't fit completely to this
chunk, only lastLen items was packed, the rest is in
the next cell. */
struct longMultiStruct *next; /* next linked cell, or NULL */
} PF_LONGMULTI;
static UBYTE *PF_longPackBuf = NULL;
static void *PF_longPackSmallBuf = NULL;
static int PF_longPackPos = 0;
static int PF_longPackTop = 0;
static PF_LONGMULTI *PF_longMultiRoot = NULL;
static PF_LONGMULTI *PF_longMultiTop = NULL;
static PF_LONGMULTI *PF_longMultiLastChunk = NULL;
static int PF_longPackN = 0;
/*
#] Variables :
#[ Long pack private functions :
#[ PF_longMultiNewCell :
*/
static inline int PF_longMultiNewCell(void)
{
/*
Allocate a new cell:
*/
PF_longMultiTop->next = (PF_LONGMULTI *)
Malloc1(sizeof(PF_LONGMULTI),"PF_longMultiCell");
if ( PF_longMultiTop->next == NULL ) return(-1);
/*
Allocate a private buffer:
*/
PF_longMultiTop->next->buffer=(UBYTE*)
Malloc1(sizeof(UBYTE)*PF_packsize,"PF_longMultiChunk");
if ( PF_longMultiTop->next->buffer == NULL ) return(-1);
/*
For the private buffer position is -1:
*/
PF_longMultiTop->next->bufpos = -1;
/*
This is the last cell in the chain:
*/
PF_longMultiTop->next->next = NULL;
/*
packpos and nPacks are not initialized!
*/
return(0);
}
/*
#] PF_longMultiNewCell :
#[ PF_longMultiPack2NextCell :
*/
static inline int PF_longMultiPack2NextCell(void)
{
/*
Is there a free cell in the chain?
*/
if ( PF_longMultiTop->next == NULL ) {
/*
No, allocate the new cell with a private buffer:
*/
if ( PF_longMultiNewCell() ) return(-1);
}
/*
Move to the next cell in the chain:
*/
PF_longMultiTop = PF_longMultiTop->next;
/*
if >=0, the cell buffer is the chunk of PF_longPackBuf, initialize it:
*/
if ( PF_longMultiTop->bufpos > -1 )
PF_longMultiTop->buffer = PF_longPackBuf+PF_longMultiTop->bufpos;
/*
else -- the cell has it's own private buffer.
Initialize the cell fields:
*/
PF_longMultiTop->nPacks = 0;
PF_longMultiTop->lastLen = 0;
PF_longMultiTop->packpos = 0;
return(0);
}
/*
#] PF_longMultiPack2NextCell :
#[ PF_longMultiNewChunkAdded :
*/
static inline int PF_longMultiNewChunkAdded(int n)
{
/*
Store the list tail:
*/
PF_LONGMULTI *MemCell = PF_longMultiLastChunk->next;
int pos = PF_longPackTop;
while ( n-- > 0 ) {
/*
Allocate a new cell:
*/
PF_longMultiLastChunk->next = (PF_LONGMULTI *)
Malloc1(sizeof(PF_LONGMULTI),"PF_longMultiCell");
if ( PF_longMultiLastChunk->next == NULL ) return(-1);
/*
Update the Last Chunk Pointer:
*/
PF_longMultiLastChunk = PF_longMultiLastChunk->next;
/*
Initialize the new cell:
*/
PF_longMultiLastChunk->bufpos = pos;
pos += PF_packsize;
PF_longMultiLastChunk->buffer = NULL;
PF_longMultiLastChunk->packpos = 0;
PF_longMultiLastChunk->nPacks = 0;
PF_longMultiLastChunk->lastLen = 0;
}
/*
Hitch the tail:
*/
PF_longMultiLastChunk->next = MemCell;
return(0);
}
/*
#] PF_longMultiNewChunkAdded :
#[ PF_longCopyChunk :
*/
static inline void PF_longCopyChunk(int *to, int *from, int n)
{
NCOPYI(to,from,n)
/* for ( ; n > 0; n-- ) *to++ = *from++; */
}
/*
#] PF_longCopyChunk :
#[ PF_longAddChunk :
The chunk must be increased by n*PF_packsize.
*/
static int PF_longAddChunk(int n, int mustRealloc)
{
UBYTE *newbuf;
if ( ( newbuf = (UBYTE*)Malloc1(sizeof(UBYTE)*(PF_longPackTop+n*PF_packsize),
"PF_longPackBuf") ) == NULL ) return(-1);
/*
Allocate and chain a new cell for longMulti:
*/
if ( PF_longMultiNewChunkAdded(n) ) return(-1);
/*
Copy the content to the new buffer:
*/
if ( mustRealloc ) {
PF_longCopyChunk((int*)newbuf,(int*)PF_longPackBuf,PF_longPackTop/sizeof(int));
}
/*
Note, PF_packsize is multiple by sizeof(int) by construction!
*/
PF_longPackTop += (n*PF_packsize);
/*
Free the old buffer and store the new one:
*/
M_free(PF_longPackBuf,"PF_longPackBuf");
PF_longPackBuf = newbuf;
/*
Count number of re-allocs:
*/
PF_longPackN += n;
return(0);
}
/*
#] PF_longAddChunk :
#[ PF_longMultiHowSplit :
"count" of "type" elements in an input buffer occupy "bytes" bytes.
We know from the algorithm, that it is too many. How to split
the buffer so that the head fits to rest of a storage buffer?*/
static inline int PF_longMultiHowSplit(int count, MPI_Datatype type, int bytes)
{
int ret, items, totalbytes;
if ( count < 2 ) return(0); /* Nothing to split */
/*
A rest of a storage buffer:
*/
totalbytes = PF_packsize - PF_longMultiTop->packpos;
/*
Rough estimate:
*/
items = (int)((double)totalbytes*count/bytes);
/*
Go to the up limit:
*/
do {
if ( ( ret = MPI_Pack_size(++items,type,PF_COMM,&bytes) )
!=MPI_SUCCESS ) return(ret);
} while ( bytes < totalbytes );
/*
Now the value of "items" is too large
And now evaluate the exact value:
*/
do {
if ( ( ret = MPI_Pack_size(--items,type,PF_COMM,&bytes) )
!=MPI_SUCCESS ) return(ret);
if ( items == 0 ) /* Nothing about MPI_Pack_size(0) == 0 in standards! */
return(0);
} while ( bytes > totalbytes );
return(items);
}
/*
#] PF_longMultiHowSplit :
#[ PF_longPackInit :
*/
static int PF_longPackInit(void)
{
int ret;
PF_longPackBuf = (UBYTE*)Malloc1(sizeof(UBYTE)*PF_packsize,"PF_longPackBuf");
if ( PF_longPackBuf == NULL ) return(-1);
/*
PF_longPackTop is not initialized yet, use in as a return value:
*/
ret = MPI_Pack_size(1,MPI_INT,PF_COMM,&PF_longPackTop);
if ( ret != MPI_SUCCESS ) return(ret);
PF_longPackSmallBuf =
(void*)Malloc1(sizeof(UBYTE)*PF_longPackTop,"PF_longPackSmallBuf");
PF_longPackTop = PF_packsize;
PF_longMultiRoot =
(PF_LONGMULTI *)Malloc1(sizeof(PF_LONGMULTI),"PF_longMultiRoot");
if ( PF_longMultiRoot == NULL ) return(-1);
PF_longMultiRoot->bufpos = 0;
PF_longMultiRoot->buffer = NULL;
PF_longMultiRoot->next = NULL;
PF_longMultiLastChunk = PF_longMultiRoot;
PF_longPackPos = 0;
PF_longMultiRoot->packpos = 0;
PF_longMultiTop = PF_longMultiRoot;
PF_longPackN = 1;
return(0);
}
/*
#] PF_longPackInit :
#[ PF_longMultiPreparePrefix :
*/
static inline int PF_longMultiPreparePrefix(void)
{
int ret;
PF_LONGMULTI *thePrefix;
int i = PF_longPackN;
/*
Here we have PF_longPackN>1!
New cell (at the list end) to create the auxiliary chunk:
*/
if ( PF_longMultiPack2NextCell() ) return(-1);
/*
Store the pointer to the chunk we will proceed:
*/
thePrefix = PF_longMultiTop;
/*
Pack PF_longPackN:
*/
ret = MPI_Pack(&(PF_longPackN),
1,
MPI_INT,
thePrefix->buffer,
PF_packsize,
&(thePrefix->packpos),
PF_COMM);
if ( ret != MPI_SUCCESS ) return(ret);
/*
And start from the beginning:
*/
for ( PF_longMultiTop = PF_longMultiRoot; i > 0; i-- ) {
/*
Pack number of Pack hits:
*/
ret = MPI_Pack(&(PF_longMultiTop->nPacks),
1,
MPI_INT,
thePrefix->buffer,
PF_packsize,
&(thePrefix->packpos),
PF_COMM);
/*
Pack the length of the last fit portion:
*/
ret |= MPI_Pack(&(PF_longMultiTop->lastLen),
1,
MPI_INT,
thePrefix->buffer,
PF_packsize,
&(thePrefix->packpos),
PF_COMM);
/*
Check the size -- not necessary, MPI_Pack did it.
*/
if ( ret != MPI_SUCCESS ) return(ret);
/*
Go to the next cell:
*/
PF_longMultiTop = PF_longMultiTop->next;
}
PF_longMultiTop = thePrefix;
/*
PF_longMultiTop is ready!
*/
return(0);
}
/*
#] PF_longMultiPreparePrefix :
#[ PF_longMultiProcessPrefix :
*/
static inline int PF_longMultiProcessPrefix(void)
{
int ret,i;
/*
We have PF_longPackN records packed in PF_longMultiRoot->buffer,
pairs nPacks and lastLen. Loop through PF_longPackN cells,
unpacking these integers into proper fields:
*/
for ( PF_longMultiTop = PF_longMultiRoot, i = 0; i < PF_longPackN; i++ ) {
/*
Go to th next cell, allocating, when necessary:
*/
if ( PF_longMultiPack2NextCell() ) return(-1);
/*
Unpack the number of Pack hits:
*/
ret = MPI_Unpack(PF_longMultiRoot->buffer,
PF_packsize,
&( PF_longMultiRoot->packpos),
&(PF_longMultiTop->nPacks),
1,
MPI_INT,
PF_COMM);
if ( ret != MPI_SUCCESS ) return(ret);
/*
Unpack the length of the last fit portion:
*/
ret = MPI_Unpack(PF_longMultiRoot->buffer,
PF_packsize,
&( PF_longMultiRoot->packpos),
&(PF_longMultiTop->lastLen),
1,
MPI_INT,
PF_COMM);
if ( ret != MPI_SUCCESS ) return(ret);
}
return(0);
}
/*
#] PF_longMultiProcessPrefix :
#[ PF_longSingleReset :
*/
/**
* Resets the "long single" pack buffer.
*
* @param is_sender if the current process is the sender, it must be true.
* Otherwise it must be false.
* @return 0 if OK, nonzero on error.
*/
static inline int PF_longSingleReset(int is_sender)
{
int ret;
PF_longPackPos=0;
if ( is_sender ) {
ret = MPI_Pack(&PF_longPackTop,1,MPI_INT,
PF_longPackBuf,PF_longPackTop,&PF_longPackPos,PF_COMM);
if ( ret != MPI_SUCCESS ) return(ret);
PF_longPackN = 1;
}
else {
PF_longPackN=0;
}
return(0);
}
/*
#] PF_longSingleReset :
#[ PF_longMultiReset :
*/
/**
* Resets the "long multi" pack buffer.
*
* @param is_sender if the current process is the sender, it must be true.
* Otherwise it must be false.
* @return 0 if OK, nonzero on error.
*/
static inline int PF_longMultiReset(int is_sender)
{
int ret = 0, theone = 1;
PF_longMultiRoot->packpos = 0;
if ( is_sender ) {
ret = MPI_Pack(&theone,1,MPI_INT,
PF_longPackBuf,PF_longPackTop,&(PF_longMultiRoot->packpos),PF_COMM);
PF_longPackN = 1;
}
else {
PF_longPackN = 0;
}
PF_longMultiRoot->nPacks = 0; /* The auxiliary field is not counted */
PF_longMultiRoot->lastLen = 0;
PF_longMultiTop = PF_longMultiRoot;
PF_longMultiRoot->buffer = PF_longPackBuf;
return ret;
}
/*
#] PF_longMultiReset :
#] Long pack private functions :
#[ PF_PrepareLongSinglePack :
*/
/**
* Prepares for the next long-single-pack operations on the sender.
*
* @return 0 if OK, nonzero on error.
*/
int PF_PrepareLongSinglePack(void)
{
return PF_longSingleReset(1);
}
/*
#] PF_PrepareLongSinglePack :
#[ PF_LongSinglePack :
*/
/**
* Adds data into the "long single" pack buffer.
*
* @param buffer the pointer to the buffer storing the data to be packed.
* @param count the number of elements in the buffer.
* @param type the data type of elements in the buffer.
* @return 0 if OK, nonzero on error.
*/
int PF_LongSinglePack(const void *buffer, size_t count, MPI_Datatype type)
{
int ret, bytes;
/* XXX: Limited by int size. */
if ( count > INT_MAX ) return -99;
ret = MPI_Pack_size((int)count,type,PF_COMM,&bytes);
if ( ret != MPI_SUCCESS ) return(ret);
while ( PF_longPackPos+bytes > PF_longPackTop ) {
if ( PF_longAddChunk(1, 1) ) return(-1);
}
/*
PF_longAddChunk(1, 1) means, the chunk must
be increased by 1 and re-allocated
*/
ret = MPI_Pack((void *)buffer,(int)count,type,
PF_longPackBuf,PF_longPackTop,&PF_longPackPos,PF_COMM);
if ( ret != MPI_SUCCESS ) return(ret);
return(0);
}
/*
#] PF_LongSinglePack :
#[ PF_LongSingleUnpack :
*/
/**
* Retrieves the next data in the "long single" pack buffer.
*
* @param[out] buffer the pointer to the buffer to store the unpacked data.
* @param count the number of elements of data to be received.
* @param type the data type of elements of data to be received.
* @return 0 if OK, nonzero on error.
*/
int PF_LongSingleUnpack(void *buffer, size_t count, MPI_Datatype type)
{
int ret;
/* XXX: Limited by int size. */
if ( count > INT_MAX ) return -99;
ret = MPI_Unpack(PF_longPackBuf,PF_longPackTop,&PF_longPackPos,
buffer,(int)count,type,PF_COMM);
if ( ret != MPI_SUCCESS ) return(ret);
return(0);
}
/*
#] PF_LongSingleUnpack :
#[ PF_LongSingleSend :
*/
/**
* Sends the contents in the "long single" pack buffer to the process
* specified by \a to.
*
* Example:
* @code
* if ( PF.me == SRC ) {
* PF_PrepareLongSinglePack();
* // Packing operations here...
* PF_LongSingleSend(DEST, TAG);
* }
* else if ( PF.me == DEST ) {
* PF_LongSingleReceive(SRC, TAG, &actual_src, &actual_tag);
* // Unpacking operations here...
* }
* @endcode
*
* @param to the destination process number.
* @param tag the message tag.
* @return 0 if OK, nonzero on error.
*/
int PF_LongSingleSend(int to, int tag)
{
int ret, pos = 0;
/*
Note, here we assume that this function couldn't be used
with to == PF_ANY_SOURCE!
*/
if ( PF_longPackN > 1 ) {
/* The buffer was incremented, pack send the new size first: */
int tmp = -PF_longPackTop;
/*
Negative value means there will be the second buffer
*/
ret = MPI_Pack(&tmp, 1,PF_INT,
PF_longPackSmallBuf,PF_longPackTop,&pos,PF_COMM);
if ( ret != MPI_SUCCESS ) return(ret);
ret = MPI_Ssend(PF_longPackSmallBuf,pos,MPI_PACKED,to,tag,PF_COMM);
if ( ret != MPI_SUCCESS ) return(ret);
}
ret = MPI_Ssend(PF_longPackBuf,PF_longPackPos,MPI_PACKED,to,tag,PF_COMM);
if ( ret != MPI_SUCCESS ) return(ret);
return(0);
}
/*
#] PF_LongSingleSend :
#[ PF_LongSingleReceive :
*/
/**
* Receives data into the "long single" pack buffer from the process
* specified by \a src.
* This function allows &src == psrc or &tag == ptag.
* Either \a psrc or \a ptag can be NULL.
*
* See the example of PF_LongSingleSend().
*
* @param src the source process number (can be PF_ANY_SOURCE).
* @param tag the source message tag (can be PF_ANY_TAG).
* @param[out] psrc the actual source process number of received message.
* @param[out] ptag the received message tag.
* @return 0 if OK, nonzero on error.
*/
int PF_LongSingleReceive(int src, int tag, int *psrc, int *ptag)
{
int ret, missed, oncemore;
MPI_Status status;
PF_longSingleReset(0);
do {
ret = MPI_Recv(PF_longPackBuf,PF_longPackTop,MPI_PACKED,src,tag,
PF_COMM,&status);
if ( ret != MPI_SUCCESS ) return(ret);
/*
The source and tag must be specified here for the case if
MPI_Recv is performed more than once:
*/
src = status.MPI_SOURCE;
tag = status.MPI_TAG;
if ( psrc ) *psrc = status.MPI_SOURCE;
if ( ptag ) *ptag = status.MPI_TAG;
/*
Now we got either small buffer with the new PF_longPackTop,
or just a regular chunk.
*/
ret = MPI_Unpack(PF_longPackBuf,PF_longPackTop,&PF_longPackPos,
&missed,1,MPI_INT,PF_COMM);
if ( ret != MPI_SUCCESS ) return(ret);
if ( missed < 0 ) { /* The small buffer was received. */
oncemore = 1; /* repeat receiving afterwards */
/* Reallocate the buffer and get the data */
missed = -missed;
/*
restore after unpacking small from buffer:
*/
PF_longPackPos = 0;
}
else {
oncemore = 0; /* That's all, no repetition */
}
if ( missed > PF_longPackTop ) {
/*
* The room must be increased. We need a re-allocation for the
* case that there is no repetition.
*/
if ( PF_longAddChunk( (missed-PF_longPackTop)/PF_packsize, !oncemore ) )
return(-1);
}
} while ( oncemore );
return(0);
}
/*
#] PF_LongSingleReceive :
#[ PF_PrepareLongMultiPack :
*/
/**
* Prepares for the next long-multi-pack operations on the sender.
*
* @return 0 if OK, nonzero on error.
*/
int PF_PrepareLongMultiPack(void)
{
return PF_longMultiReset(1);
}
/*
#] PF_PrepareLongMultiPack :
#[ PF_LongMultiPackImpl :
*/
/**
* Adds data into the "long multi" pack buffer.
*
* @param buffer the pointer to the buffer storing the data to be packed.
* @param count the number of elements in the buffer.
* @param eSize the byte size of each element of data.
* @param type the data type of elements in the buffer.
* @return 0 if OK, nonzero on error.
*/
int PF_LongMultiPackImpl(const void*buffer, size_t count, size_t eSize, MPI_Datatype type)
{
int ret, items;
/* XXX: Limited by int size. */
if ( count > INT_MAX ) return -99;
ret = MPI_Pack_size((int)count,type,PF_COMM,&items);
if ( ret != MPI_SUCCESS ) return(ret);
if ( PF_longMultiTop->packpos + items <= PF_packsize ) {
ret = MPI_Pack((void *)buffer,(int)count,type,PF_longMultiTop->buffer,
PF_packsize,&(PF_longMultiTop->packpos),PF_COMM);
if ( ret != MPI_SUCCESS ) return(ret);
PF_longMultiTop->nPacks++;
return(0);
}
/*
The data do not fit to the rest of the buffer.
There are two possibilities here: go to the next cell
immediately, or first try to pack some portion. The function
PF_longMultiHowSplit() returns the number of items could be
packed in the end of the current cell:
*/
if ( ( items = PF_longMultiHowSplit((int)count,type,items) ) < 0 ) return(items);
if ( items > 0 ) { /* store the head */
ret = MPI_Pack((void *)buffer,items,type,PF_longMultiTop->buffer,
PF_packsize,&(PF_longMultiTop->packpos),PF_COMM);
if ( ret != MPI_SUCCESS ) return(ret);
PF_longMultiTop->nPacks++;
PF_longMultiTop->lastLen = items;
}
/*
Now the rest should be packed to the new cell.
Slide to the new cell:
*/
if ( PF_longMultiPack2NextCell() ) return(-1);
PF_longPackN++;
/*
Pack the rest to the next cell:
*/
return(PF_LongMultiPackImpl((char *)buffer+items*eSize,count-items,eSize,type));
}
/*
#] PF_LongMultiPackImpl :
#[ PF_LongMultiUnpackImpl :
*/
/**
* Retrieves the next data in the "long multi" pack buffer.
*
* @param[out] buffer the pointer to the buffer to store the unpacked data.
* @param count the number of elements of data to be received.
* @param eSize the byte size of each element of data.
* @param type the data type of elements of data to be received.
* @return 0 if OK, nonzero on error.
*/
int PF_LongMultiUnpackImpl(void *buffer, size_t count, size_t eSize, MPI_Datatype type)
{
int ret;
/* XXX: Limited by int size. */
if ( count > INT_MAX ) return -99;
if ( PF_longPackN < 2 ) { /* Just unpack the buffer from the single cell */
ret = MPI_Unpack(
PF_longMultiTop->buffer,
PF_packsize,
&(PF_longMultiTop->packpos),
buffer,
count,type,PF_COMM);
if ( ret != MPI_SUCCESS ) return(ret);
return(0);
}
/*
More than one cell is in use.
*/
if ( ( PF_longMultiTop->nPacks > 1 ) /* the cell is not expired */
|| /* The last cell contains exactly required portion: */
( ( PF_longMultiTop->nPacks == 1 ) && ( PF_longMultiTop->lastLen == 0 ) )
) { /* Just unpack the buffer from the current cell */
ret = MPI_Unpack(
PF_longMultiTop->buffer,
PF_packsize,
&(PF_longMultiTop->packpos),
buffer,
count,type,PF_COMM);
if ( ret != MPI_SUCCESS ) return(ret);
(PF_longMultiTop->nPacks)--;
return(0);
}
if ( ( PF_longMultiTop->nPacks == 1 ) && ( PF_longMultiTop->lastLen != 0 ) ) {
/*
Unpack the head:
*/
ret = MPI_Unpack(
PF_longMultiTop->buffer,
PF_packsize,
&(PF_longMultiTop->packpos),
buffer,
PF_longMultiTop->lastLen,type,PF_COMM);
if ( ret != MPI_SUCCESS ) return(ret);
/*
Decrement the counter by read items:
*/
count -= PF_longMultiTop->lastLen;
if ( count <= 0 ) return(-1); /*Something is wrong! */
/*
Shift the output buffer position:
*/
buffer = (char *)buffer + PF_longMultiTop->lastLen * eSize;
(PF_longMultiTop->nPacks)--;
}
/*
Here PF_longMultiTop->nPacks == 0
*/
if ( ( PF_longMultiTop = PF_longMultiTop->next ) == NULL ) return(-1);
return(PF_LongMultiUnpackImpl(buffer,count,eSize,type));
}
/*
#] PF_LongMultiUnpackImpl :
#[ PF_LongMultiBroadcast :
*/
/**
* Broadcasts the contents in the "long multi" pack buffer on the master
* to those on the slaves.
*
* Example:
* @code
* if ( PF.me == MASTER ) {
* PF_PrepareLongMultiPack();
* // Packing operations here...
* }
* PF_LongMultiBroadcast();
* if ( PF.me != MASTER ) {
* // Unpacking operations here...
* }
* @endcode
*
* @return 0 if OK, nonzero on error.
*/
int PF_LongMultiBroadcast(void)
{
int ret, i;
if ( PF.me == MASTER ) {
/*
PF_longPackN is the number of packed chunks. If it is more
than 1, we have to pack a new one and send it first
*/
if ( PF_longPackN > 1 ) {
if ( PF_longMultiPreparePrefix() ) return(-1);
ret = MPI_Bcast((void*)PF_longMultiTop->buffer,
PF_packsize,MPI_PACKED,MASTER,PF_COMM);
if ( ret != MPI_SUCCESS ) return(ret);
/*
PF_longPackN was not incremented by PF_longMultiPreparePrefix()!
*/
}
/*
Now we start from the beginning:
*/
PF_longMultiTop = PF_longMultiRoot;
/*
Just broadcast all the chunks:
*/
for ( i = 0; i < PF_longPackN; i++ ) {
ret = MPI_Bcast((void*)PF_longMultiTop->buffer,
PF_packsize,MPI_PACKED,MASTER,PF_COMM);
if ( ret != MPI_SUCCESS ) return(ret);
PF_longMultiTop = PF_longMultiTop->next;
}
return(0);
}
/*
else - the slave
*/
PF_longMultiReset(0);
/*
Get the first chunk; it can be either the only data chunk, or
an auxiliary chunk, if the data do not fit the single chunk:
*/
ret = MPI_Bcast((void*)PF_longMultiRoot->buffer,
PF_packsize,MPI_PACKED,MASTER,PF_COMM);
if ( ret != MPI_SUCCESS ) return(ret);
ret = MPI_Unpack((void*)PF_longMultiRoot->buffer,
PF_packsize,
&(PF_longMultiRoot->packpos),
&PF_longPackN,1,MPI_INT,PF_COMM);
if ( ret != MPI_SUCCESS ) return(ret);
/*
Now in PF_longPackN we have the number of cells used
for broadcasting. If it is >1, then we have to allocate
enough cells, initialize them and receive all the chunks.
*/
if ( PF_longPackN < 2 ) /* That's all, the single chunk is received. */
return(0);
/*
Here we have to get PF_longPackN chunks. But, first,
initialize cells by info from the received auxiliary chunk.
*/
if ( PF_longMultiProcessPrefix() ) return(-1);
/*
Now we have free PF_longPackN cells, starting
from PF_longMultiRoot->next, with properly initialized
nPacks and lastLen fields. Get chunks:
*/
for ( PF_longMultiTop = PF_longMultiRoot->next, i = 0; i < PF_longPackN; i++ ) {
ret = MPI_Bcast((void*)PF_longMultiTop->buffer,
PF_packsize,MPI_PACKED,MASTER,PF_COMM);
if ( ret != MPI_SUCCESS ) return(ret);
if ( i == 0 ) { /* The first chunk, it contains extra "1". */
int tmp;
/*
Extract this 1 into tmp and forget about it.
*/
ret = MPI_Unpack((void*)PF_longMultiTop->buffer,
PF_packsize,
&(PF_longMultiTop->packpos),
&tmp,1,MPI_INT,PF_COMM);
if ( ret != MPI_SUCCESS ) return(ret);
}
PF_longMultiTop = PF_longMultiTop->next;
}
/*
multiUnPack starts with PF_longMultiTop, skip auxiliary chunk in
PF_longMultiRoot:
*/
PF_longMultiTop = PF_longMultiRoot->next;
return(0);
}
/*
#] PF_LongMultiBroadcast :
#] Long pack stuff :
*/
|