/usr/include/dune/common/parallel/remoteindices.hh is in libdune-common-dev 2.5.1-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | // -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
// vi: set et ts=4 sw=2 sts=2:
#ifndef DUNE_REMOTEINDICES_HH
#define DUNE_REMOTEINDICES_HH
#include "indexset.hh"
#include "plocalindex.hh"
#include <dune/common/exceptions.hh>
#include <dune/common/poolallocator.hh>
#include <dune/common/sllist.hh>
#include <dune/common/stdstreams.hh>
#include <map>
#include <set>
#include <utility>
#include <iostream>
#include <algorithm>
#include <iterator>
#if HAVE_MPI
#include "mpitraits.hh"
#include <mpi.h>
namespace Dune {
/** @addtogroup Common_Parallel
*
* @{
*/
/**
* @file
* @brief Classes describing a distributed indexset
* @author Markus Blatt
*/
//! \todo Please doc me!
template<typename TG, typename TA>
class MPITraits<IndexPair<TG,ParallelLocalIndex<TA> > >
{
public:
inline static MPI_Datatype getType();
private:
static MPI_Datatype type;
};
template<typename T, typename A>
class RemoteIndices;
template<typename T1, typename T2>
class RemoteIndex;
template<typename T>
class IndicesSyncer;
template<typename T1, typename T2>
std::ostream& operator<<(std::ostream& os, const RemoteIndex<T1,T2>& index);
template<typename T, typename A, bool mode>
class RemoteIndexListModifier;
/**
* @brief Information about an index residing on another processor.
*/
template<typename T1, typename T2>
class RemoteIndex
{
template<typename T>
friend class IndicesSyncer;
template<typename T, typename A, typename A1>
friend void repairLocalIndexPointers(std::map<int,SLList<std::pair<typename T::GlobalIndex, typename T::LocalIndex::Attribute>,A> >&,
RemoteIndices<T,A1>&,
const T&);
template<typename T, typename A, bool mode>
friend class RemoteIndexListModifier;
public:
/**
* @brief the type of the global index.
* This type has to provide at least a operator< for sorting.
*/
typedef T1 GlobalIndex;
/**
* @brief The type of the attributes.
* Normally this will be an enumeration like
* \code
* enum Attributes{owner, border, overlap}
* \endcode
* e.g. OwnerOverlapCopyAttributes.
*/
typedef T2 Attribute;
/**
* @brief The type of the index pair.
*/
typedef IndexPair<GlobalIndex,ParallelLocalIndex<Attribute> >
PairType;
/**
* @brief Get the attribute of the index on the remote process.
* @return The remote attribute.
*/
const Attribute attribute() const;
/**
* @brief Get the corresponding local index pair.
* @return The corresponding local index pair.
*/
const PairType& localIndexPair() const;
/**
* @brief Parameterless Constructor.
*/
RemoteIndex();
/**
* @brief Constructor.
* @param attribute The attribute of the index on the remote processor.
* @param local The corresponding local index.
*/
RemoteIndex(const T2& attribute,
const PairType* local);
/**
* @brief Constructor.
* Private as it should only be called from within Indexset.
* @param attribute The attribute of the index on the remote processor.
*/
RemoteIndex(const T2& attribute);
bool operator==(const RemoteIndex& ri) const;
bool operator!=(const RemoteIndex& ri) const;
private:
/** @brief The corresponding local index for this process. */
const PairType* localIndex_;
/** @brief The attribute of the index on the other process. */
char attribute_;
};
template<class T, class A>
std::ostream& operator<<(std::ostream& os, const RemoteIndices<T,A>& indices);
class InterfaceBuilder;
template<class T, class A>
class CollectiveIterator;
template<class T>
class IndicesSyncer;
// forward declaration needed for friend declaration.
template<typename T1, typename T2>
class OwnerOverlapCopyCommunication;
/**
* @brief The indices present on remote processes.
*
* To set up communication between the set of processes active in
* the communication every process needs to know which
* indices are also known to other processes and which attributes
* are attached to them on the remote side.
*
* This information is managed by this class. The information can either
* be computed automatically calling rebuild (which requires information
* to be sent in a ring) or set up by hand using the
* RemoteIndexListModifiers returned by function getModifier(int).
*
* @tparam T The type of the underlying index set.
* @tparam A The type of the allocator to use.
*/
template<class T, class A=std::allocator<RemoteIndex<typename T::GlobalIndex,
typename T::LocalIndex::Attribute> > >
class RemoteIndices
{
friend class InterfaceBuilder;
friend class IndicesSyncer<T>;
template<typename T1, typename A2, typename A1>
friend void repairLocalIndexPointers(std::map<int,SLList<std::pair<typename T1::GlobalIndex, typename T1::LocalIndex::Attribute>,A2> >&,
RemoteIndices<T1,A1>&,
const T1&);
template<class G, class T1, class T2>
friend void fillIndexSetHoles(const G& graph, Dune::OwnerOverlapCopyCommunication<T1,T2>& oocomm);
friend std::ostream& operator<<<>(std::ostream&, const RemoteIndices<T>&);
public:
/**
* @brief Type of the index set we use, e.g. ParallelLocalIndexSet.
*/
typedef T ParallelIndexSet;
/**
* @brief The type of the collective iterator over all remote indices. */
typedef CollectiveIterator<T,A> CollectiveIteratorT;
/**
* @brief The type of the global index.
*/
typedef typename ParallelIndexSet::GlobalIndex GlobalIndex;
/**
* @brief The type of the local index.
*/
typedef typename ParallelIndexSet::LocalIndex LocalIndex;
/**
* @brief The type of the attribute.
*/
typedef typename LocalIndex::Attribute Attribute;
/**
* @brief Type of the remote indices we manage.
*/
typedef Dune::RemoteIndex<GlobalIndex,Attribute> RemoteIndex;
/**
* @brief The type of the allocator for the remote index list.
*/
typedef typename A::template rebind<RemoteIndex>::other Allocator;
/** @brief The type of the remote index list. */
typedef Dune::SLList<RemoteIndex,Allocator>
RemoteIndexList;
/** @brief The type of the map from rank to remote index list. */
typedef std::map<int, std::pair<RemoteIndexList*,RemoteIndexList*> >
RemoteIndexMap;
typedef typename RemoteIndexMap::const_iterator const_iterator;
/**
* @brief Constructor.
* @param comm The communicator to use.
* @param source The indexset which represents the global to
* local mapping at the source of the communication
* @param destination The indexset to which the communication
* which represents the global to
* local mapping at the destination of the communication.
* May be the same as the source indexset.
* @param neighbours Optional: The neighbours the process shares indices with.
* If this parameter is omitted a ring communication with all indices will take
* place to calculate this information which is O(P).
* @param includeSelf If true, sending from indices of the processor to other
* indices on the same processor is enabled even if the same indexset is used
* on both the
* sending and receiving side.
*/
inline RemoteIndices(const ParallelIndexSet& source, const ParallelIndexSet& destination,
const MPI_Comm& comm, const std::vector<int>& neighbours=std::vector<int>(), bool includeSelf=false);
RemoteIndices();
/**
* @brief Tell whether sending from indices of the processor to other
* indices on the same processor is enabled even if the same indexset is
* used on both the sending and receiving side.
*
* @param includeSelf If true it is enabled.
*/
void setIncludeSelf(bool includeSelf);
/**
* @brief Set the index sets and communicator we work with.
*
* @warning All remote indices already setup will be deleted!
*
* @param comm The communicator to use.
* @param source The indexset which represents the global to
* local mapping at the source of the communication
* @param destination The indexset to which the communication
* which represents the global to
* local mapping at the destination of the communication.
* May be the same as the source indexset.
* @param neighbours Optional: The neighbours the process shares indices with.
* If this parameter is omitted a ring communication with all indices will take
* place to calculate this information which is O(P).
*/
void setIndexSets(const ParallelIndexSet& source, const ParallelIndexSet& destination,
const MPI_Comm& comm, const std::vector<int>& neighbours=std::vector<int>());
template<typename C>
void setNeighbours(const C& neighbours)
{
neighbourIds.clear();
neighbourIds.insert(neighbours.begin(), neighbours.end());
}
const std::set<int>& getNeighbours() const
{
return neighbourIds;
}
/**
* @brief Destructor.
*/
~RemoteIndices();
/**
* @brief Rebuilds the set of remote indices.
*
* This has to be called whenever the underlying index sets
* change.
*
* If the template parameter ignorePublic is true all indices will be treated
* as public.
*/
template<bool ignorePublic>
void rebuild();
bool operator==(const RemoteIndices& ri);
/**
* @brief Checks whether the remote indices are synced with
* the indexsets.
*
* If they are not synced the remote indices need to be rebuild.
* @return True if they are synced.
*/
inline bool isSynced() const;
/**
* @brief Get the mpi communicator used.
*/
inline MPI_Comm communicator() const;
/**
* @brief Get a modifier for a remote index list.
*
* Sometimes the user knows in advance which indices will be present
* on other processors, too. Then he can set them up using this modifier.
*
* @warning Use with care. If the remote index list is inconsistent
* after the modification the communication might result in a dead lock!
*
* @tparam mode If true the index set corresponding to the remote indices might get modified.
* Therefore the internal pointers to the indices need to be repaired.
* @tparam send If true the remote index information at the sending side will
* be modified, if false the receiving side.
*/
template<bool mode, bool send>
inline RemoteIndexListModifier<T,A,mode> getModifier(int process);
/**
* @brief Find an iterator over the remote index lists of a specific process.
* @param proc The identifier of the process.
* @return The iterator the remote index lists postioned at the process.
* If theres is no list for this process, the end iterator is returned.
*/
inline const_iterator find(int proc) const;
/**
* @brief Get an iterator over all remote index lists.
* @return The iterator over all remote index lists postioned at the first process.
*/
inline const_iterator begin() const;
/**
* @brief Get an iterator over all remote index lists.
* @return The iterator over all remote index lists postioned at the end.
*/
inline const_iterator end() const;
/**
* @brief Get an iterator for colletively iterating over the remote indices of all remote processes.
*/
template<bool send>
inline CollectiveIteratorT iterator() const;
/**
* @brief Free the index lists.
*/
inline void free();
/**
* @brief Get the number of processors we share indices with.
* @return The number of neighbours.
*/
inline int neighbours() const;
/** @brief Get the index set at the source. */
inline const ParallelIndexSet& sourceIndexSet() const;
/** @brief Get the index set at destination. */
inline const ParallelIndexSet& destinationIndexSet() const;
private:
/** copying is forbidden. */
RemoteIndices(const RemoteIndices&)
{}
/** @brief Index set used at the source of the communication. */
const ParallelIndexSet* source_;
/** @brief Index set used at the destination of the communication. */
const ParallelIndexSet* target_;
/** @brief The communicator to use.*/
MPI_Comm comm_;
/** @brief The neighbours we share indices with.
* If not empty this will speedup rebuild. */
std::set<int> neighbourIds;
/** @brief The communicator tag to use. */
const static int commTag_=333;
/**
* @brief The sequence number of the source index set when the remote indices
* where build.
*/
int sourceSeqNo_;
/**
* @brief The sequence number of the destination index set when the remote indices
* where build.
*/
int destSeqNo_;
/**
* @brief Whether the public flag was ignored during the build.
*/
bool publicIgnored;
/**
* @brief Whether the next build will be the first build ever.
*/
bool firstBuild;
/*
* @brief If true, sending from indices of the processor to other
* indices on the same processor is enabled even if the same indexset is used
* on both the
* sending and receiving side.
*/
bool includeSelf;
/** @brief The index pair type. */
typedef IndexPair<GlobalIndex, LocalIndex>
PairType;
/**
* @brief The remote indices.
*
* The key is the process id and the values are the pair of remote
* index lists, the first for receiving, the second for sending.
*/
RemoteIndexMap remoteIndices_;
/**
* @brief Build the remote mapping.
*
* If the template parameter ignorePublic is true all indices will be treated
* as public.
* @param includeSelf If true, sending from indices of the processor to other
* indices on the same processor is enabled even if the same indexset is used
* on both the
* sending and receiving side.
*/
template<bool ignorePublic>
inline void buildRemote(bool includeSelf);
/**
* @brief Count the number of public indices in an index set.
* @param indexSet The index set whose indices we count.
* @return the number of indices marked as public.
*/
inline int noPublic(const ParallelIndexSet& indexSet);
/**
* @brief Pack the indices to send if source_ and target_ are the same.
*
* If the template parameter ignorePublic is true all indices will be treated
* as public.
* @param myPairs Array to store references to the public indices in.
* @param p_out The output buffer to pack the entries to.
* @param type The mpi datatype for the pairs.
* @param bufferSize The size of the output buffer p_out.
* @param position The position to start packing.
*/
template<bool ignorePublic>
inline void packEntries(PairType** myPairs, const ParallelIndexSet& indexSet,
char* p_out, MPI_Datatype type, int bufferSize,
int* position, int n);
/**
* @brief unpacks the received indices and builds the remote index list.
*
* @param remote The list to add the indices to.
* @param remoteEntries The number of remote entries to unpack.
* @param local The local indices to check whether we know the remote
* indices.
* @param localEntries The number of local indices.
* @param type The mpi data type for unpacking.
* @param p_in The input buffer to unpack from.
* @param position The position in the buffer to start unpacking from.
* @param bufferSize The size of the input buffer.
*/
inline void unpackIndices(RemoteIndexList& remote, int remoteEntries,
PairType** local, int localEntries, char* p_in,
MPI_Datatype type, int* position, int bufferSize,
bool fromOurself);
inline void unpackIndices(RemoteIndexList& send, RemoteIndexList& receive,
int remoteEntries, PairType** localSource,
int localSourceEntries, PairType** localDest,
int localDestEntries, char* p_in,
MPI_Datatype type, int* position, int bufferSize);
void unpackCreateRemote(char* p_in, PairType** sourcePairs, PairType** DestPairs,
int remoteProc, int sourcePublish, int destPublish,
int bufferSize, bool sendTwo, bool fromOurSelf=false);
};
/** @} */
/**
* @brief Modifier for adding and/or deleting remote indices from
* the remote index list.
*
* In some cases all the information about the indices also present
* on remote process might already be known. In this case this
* information can be provided to the RemoteIndices via this modifier.
* This prevents the global communication needed by a call to
* RemoteIndices::rebuild.
*
* In some cases it might advisable to run IndicesSyncer::sync afterwards.
*
* @warning Use with care. If the indices are not consistent afterwards
* communication attempts might deadlock!
*/
template<class T, class A, bool mode>
class RemoteIndexListModifier
{
template<typename T1, typename A1>
friend class RemoteIndices;
public:
class InvalidPosition : public RangeError
{};
enum {
/**
* @brief If true the index set corresponding to the
* remote indices might get modified.
*
* If for example new indices are added to an index set
* all pointers of the remote indices to the local indices
* become invalid after ParallelIndexSet::endResize() was called.
*/
MODIFYINDEXSET=mode
};
/**
* @brief Type of the index set we use.
*/
typedef T ParallelIndexSet;
/**
* @brief The type of the global index.
*/
typedef typename ParallelIndexSet::GlobalIndex GlobalIndex;
/**
* @brief The type of the local index.
*/
typedef typename ParallelIndexSet::LocalIndex LocalIndex;
/**
* @brief The type of the attribute.
*/
typedef typename LocalIndex::Attribute Attribute;
/**
* @brief Type of the remote indices we manage.
*/
typedef Dune::RemoteIndex<GlobalIndex,Attribute> RemoteIndex;
/**
* @brief The type of the allocator for the remote index list.
*/
typedef A Allocator;
/** @brief The type of the remote index list. */
typedef Dune::SLList<RemoteIndex,Allocator>
RemoteIndexList;
/**
* @brief The type of the modifying iterator of the remote index list.
*/
typedef SLListModifyIterator<RemoteIndex,Allocator> ModifyIterator;
/**
* @brief The type of the remote index list iterator.
*/
typedef typename RemoteIndexList::const_iterator ConstIterator;
/**
* @brief Insert an index to the list.
*
* Moves to the position where the index fits and inserts it.
* After the insertion only indices with an bigger global index
* than the inserted can be inserted.
*
* This method is only available if MODIFYINDEXSET is false.
*
* @param index The index to insert.
* @exception InvalidPosition Thrown if the index at the current position or
* the one before has bigger global index than the one to be inserted.
*/
void insert(const RemoteIndex& index) throw(InvalidPosition);
/**
* @brief Insert an index to the list.
*
* Moves to the position where the index fits and inserts it.
* After the insertion only indices with an bigger global index
* than the inserted can be inserted.
*
* This method is only available if MODIFYINDEXSET is true.
*
* @param index The index to insert.
* @param global The global index of the remote index.
* @exception InvalidPosition Thrown if the index at the current position or
* the one before has bigger global index than the one to be inserted.
*/
void insert(const RemoteIndex& index, const GlobalIndex& global) throw(InvalidPosition);
/**
* @brief Remove a remote index.
* @param global The global index corresponding to the remote index.
* @return True If there was a corresponding remote index.
* @exception InvalidPostion If there was an insertion or deletion of
* a remote index corresponding to a bigger global index before.
*/
bool remove(const GlobalIndex& global) throw(InvalidPosition);
/**
* @brief Repair the pointers to the local index pairs.
*
* Due to adding new indices or/and deleting indices in the
* index set all pointers to the local index pair might become
* invalid during ParallelIndexSet::endResize().
* This method repairs them.
*
* @exception InvalidIndexSetState Thrown if the underlying
* index set is not in ParallelIndexSetState::GROUND mode (only when
* compiled with DUNE_ISTL_WITH_CHECKING!).
*/
void repairLocalIndexPointers() throw(InvalidIndexSetState);
RemoteIndexListModifier(const RemoteIndexListModifier&);
/**
* @brief Default constructor.
* @warning Object is not usable!
*/
RemoteIndexListModifier()
: glist_()
{}
private:
/**
* @brief Create a modifier for a remote index list.
* @param indexSet The set of indices the process knows.
* @param rList The list of remote indices to modify.
*/
RemoteIndexListModifier(const ParallelIndexSet& indexSet,
RemoteIndexList& rList);
typedef SLList<GlobalIndex,Allocator> GlobalList;
typedef typename GlobalList::ModifyIterator GlobalModifyIterator;
RemoteIndexList* rList_;
const ParallelIndexSet* indexSet_;
GlobalList glist_;
ModifyIterator iter_;
GlobalModifyIterator giter_;
ConstIterator end_;
bool first_;
GlobalIndex last_;
};
/**
* @brief A collective iterator for moving over the remote indices for
* all processes collectively.
*/
template<class T, class A>
class CollectiveIterator
{
/**
* @brief Type of the index set we use.
*/
typedef T ParallelIndexSet;
/**
* @brief The type of the global index.
*/
typedef typename ParallelIndexSet::GlobalIndex GlobalIndex;
/**
* @brief The type of the local index.
*/
typedef typename ParallelIndexSet::LocalIndex LocalIndex;
/**
* @brief The type of the attribute.
*/
typedef typename LocalIndex::Attribute Attribute;
/** @brief The remote index type */
typedef Dune::RemoteIndex<GlobalIndex,Attribute> RemoteIndex;
/** @brief The allocator of the remote indices. */
typedef typename A::template rebind<RemoteIndex>::other Allocator;
/** @brief The type of the remote index list. */
typedef Dune::SLList<RemoteIndex,Allocator> RemoteIndexList;
/** @brief The of map for storing the iterators. */
typedef std::map<int,std::pair<typename RemoteIndexList::const_iterator,
const typename RemoteIndexList::const_iterator> >
Map;
public:
/** @brief The type of the map from rank to remote index list. */
typedef std::map<int, std::pair<RemoteIndexList*,RemoteIndexList*> >
RemoteIndexMap;
/**
* @brief Constructor.
* @param map_ The map of the remote indices.
* @param send True if we want iterate over the remote indices used for sending.
*/
inline CollectiveIterator(const RemoteIndexMap& map_, bool send);
/**
* @brief Advances all underlying iterators.
*
* All iterators are advanced until they point to a remote index whose
* global id is bigger or equal to global.
* Iterators pointing to their end are removed.
* @param global The index we search for.
*/
inline void advance(const GlobalIndex& global);
/**
* @brief Advances all underlying iterators.
*
* All iterators are advanced until they point to a remote index whose
* global id is bigger or equal to global.
* Iterators pointing to their end are removed.
* @param global The index we search for.
* @param attribute The attribute we search for.
*/
inline void advance(const GlobalIndex& global, const Attribute& attribute);
CollectiveIterator& operator++();
/**
* @brief Checks whether there are still iterators in the map.
*/
inline bool empty();
/**
* @brief Iterator over the valid underlying iterators.
*
* An iterator is valid if it points to a remote index whose
* global id is equal to the one currently examined.
*/
class iterator
{
public:
typedef typename Map::iterator RealIterator;
typedef typename Map::iterator ConstRealIterator;
//! \todo Please doc me!
iterator(const RealIterator& iter, const ConstRealIterator& end, GlobalIndex& index)
: iter_(iter), end_(end), index_(index), hasAttribute(false)
{
// Move to the first valid entry
while(iter_!=end_ && iter_->second.first->localIndexPair().global()!=index_)
++iter_;
}
iterator(const RealIterator& iter, const ConstRealIterator& end, GlobalIndex index,
Attribute attribute)
: iter_(iter), end_(end), index_(index), attribute_(attribute), hasAttribute(true)
{
// Move to the first valid entry or the end
while(iter_!=end_ && (iter_->second.first->localIndexPair().global()!=index_
|| iter_->second.first->localIndexPair().local().attribute()!=attribute))
++iter_;
}
//! \todo Please doc me!
iterator(const iterator& other)
: iter_(other.iter_), end_(other.end_), index_(other.index_)
{ }
//! \todo Please doc me!
iterator& operator++()
{
++iter_;
// If entry is not valid move on
while(iter_!=end_ && (iter_->second.first->localIndexPair().global()!=index_ ||
(hasAttribute &&
iter_->second.first->localIndexPair().local().attribute()!=attribute_)))
++iter_;
assert(iter_==end_ ||
(iter_->second.first->localIndexPair().global()==index_));
assert(iter_==end_ || !hasAttribute ||
(iter_->second.first->localIndexPair().local().attribute()==attribute_));
return *this;
}
//! \todo Please doc me!
const RemoteIndex& operator*() const
{
return *(iter_->second.first);
}
//! \todo Please doc me!
int process() const
{
return iter_->first;
}
//! \todo Please doc me!
const RemoteIndex* operator->() const
{
return iter_->second.first.operator->();
}
//! \todo Please doc me!
bool operator==(const iterator& other)
{
return other.iter_==iter_;
}
//! \todo Please doc me!
bool operator!=(const iterator& other)
{
return other.iter_!=iter_;
}
private:
iterator();
RealIterator iter_;
RealIterator end_;
GlobalIndex index_;
Attribute attribute_;
bool hasAttribute;
};
iterator begin();
iterator end();
private:
Map map_;
GlobalIndex index_;
Attribute attribute_;
bool noattribute;
};
template<typename TG, typename TA>
MPI_Datatype MPITraits<IndexPair<TG,ParallelLocalIndex<TA> > >::getType()
{
if(type==MPI_DATATYPE_NULL) {
int length[2] = {1, 1};
MPI_Aint base;
MPI_Aint disp[2];
MPI_Datatype types[2] = {MPITraits<TG>::getType(),
MPITraits<ParallelLocalIndex<TA> >::getType()};
IndexPair<TG,ParallelLocalIndex<TA> > rep;
MPI_Get_address(&rep, &base); // lower bound of the datatype
MPI_Get_address(&(rep.global_), &disp[0]);
MPI_Get_address(&(rep.local_), &disp[1]);
for (MPI_Aint& d : disp)
d -= base;
MPI_Datatype tmp;
MPI_Type_create_struct(2, length, disp, types, &tmp);
MPI_Type_create_resized(tmp, 0, sizeof(IndexPair<TG,ParallelLocalIndex<TA> >), &type);
MPI_Type_commit(&type);
MPI_Type_free(&tmp);
}
return type;
}
template<typename TG, typename TA>
MPI_Datatype MPITraits<IndexPair<TG,ParallelLocalIndex<TA> > >::type=MPI_DATATYPE_NULL;
template<typename T1, typename T2>
RemoteIndex<T1,T2>::RemoteIndex(const T2& attribute, const PairType* local)
: localIndex_(local), attribute_(attribute)
{}
template<typename T1, typename T2>
RemoteIndex<T1,T2>::RemoteIndex(const T2& attribute)
: localIndex_(0), attribute_(attribute)
{}
template<typename T1, typename T2>
RemoteIndex<T1,T2>::RemoteIndex()
: localIndex_(0), attribute_()
{}
template<typename T1, typename T2>
inline bool RemoteIndex<T1,T2>::operator==(const RemoteIndex& ri) const
{
return localIndex_==ri.localIndex_ && attribute_==ri.attribute;
}
template<typename T1, typename T2>
inline bool RemoteIndex<T1,T2>::operator!=(const RemoteIndex& ri) const
{
return localIndex_!=ri.localIndex_ || attribute_!=ri.attribute_;
}
template<typename T1, typename T2>
inline const T2 RemoteIndex<T1,T2>::attribute() const
{
return T2(attribute_);
}
template<typename T1, typename T2>
inline const IndexPair<T1,ParallelLocalIndex<T2> >& RemoteIndex<T1,T2>::localIndexPair() const
{
return *localIndex_;
}
template<typename T, typename A>
inline RemoteIndices<T,A>::RemoteIndices(const ParallelIndexSet& source,
const ParallelIndexSet& destination,
const MPI_Comm& comm,
const std::vector<int>& neighbours,
bool includeSelf_)
: source_(&source), target_(&destination), comm_(comm),
sourceSeqNo_(-1), destSeqNo_(-1), publicIgnored(false), firstBuild(true),
includeSelf(includeSelf_)
{
setNeighbours(neighbours);
}
template<typename T, typename A>
void RemoteIndices<T,A>::setIncludeSelf(bool b)
{
includeSelf=b;
}
template<typename T, typename A>
RemoteIndices<T,A>::RemoteIndices()
: source_(0), target_(0), sourceSeqNo_(-1),
destSeqNo_(-1), publicIgnored(false), firstBuild(true),
includeSelf(false)
{}
template<class T, typename A>
void RemoteIndices<T,A>::setIndexSets(const ParallelIndexSet& source,
const ParallelIndexSet& destination,
const MPI_Comm& comm,
const std::vector<int>& neighbours)
{
free();
source_ = &source;
target_ = &destination;
comm_ = comm;
firstBuild = true;
setNeighbours(neighbours);
}
template<typename T, typename A>
const typename RemoteIndices<T,A>::ParallelIndexSet&
RemoteIndices<T,A>::sourceIndexSet() const
{
return *source_;
}
template<typename T, typename A>
const typename RemoteIndices<T,A>::ParallelIndexSet&
RemoteIndices<T,A>::destinationIndexSet() const
{
return *target_;
}
template<typename T, typename A>
RemoteIndices<T,A>::~RemoteIndices()
{
free();
}
template<typename T, typename A>
template<bool ignorePublic>
inline void RemoteIndices<T,A>::packEntries(IndexPair<GlobalIndex,LocalIndex>** pairs,
const ParallelIndexSet& indexSet,
char* p_out, MPI_Datatype type,
int bufferSize,
int *position, int n)
{
DUNE_UNUSED_PARAMETER(n);
// fill with own indices
typedef typename ParallelIndexSet::const_iterator const_iterator;
typedef IndexPair<GlobalIndex,LocalIndex> PairType;
const const_iterator end = indexSet.end();
//Now pack the source indices
int i=0;
for(const_iterator index = indexSet.begin(); index != end; ++index)
if(ignorePublic || index->local().isPublic()) {
MPI_Pack(const_cast<PairType*>(&(*index)), 1,
type,
p_out, bufferSize, position, comm_);
pairs[i++] = const_cast<PairType*>(&(*index));
}
assert(i==n);
}
template<typename T, typename A>
inline int RemoteIndices<T,A>::noPublic(const ParallelIndexSet& indexSet)
{
typedef typename ParallelIndexSet::const_iterator const_iterator;
int noPublic=0;
const const_iterator end=indexSet.end();
for(const_iterator index=indexSet.begin(); index!=end; ++index)
if(index->local().isPublic())
noPublic++;
return noPublic;
}
template<typename T, typename A>
inline void RemoteIndices<T,A>::unpackCreateRemote(char* p_in, PairType** sourcePairs,
PairType** destPairs, int remoteProc,
int sourcePublish, int destPublish,
int bufferSize, bool sendTwo,
bool fromOurSelf)
{
// unpack the number of indices we received
int noRemoteSource=-1, noRemoteDest=-1;
char twoIndexSets=0;
int position=0;
// Did we receive two index sets?
MPI_Unpack(p_in, bufferSize, &position, &twoIndexSets, 1, MPI_CHAR, comm_);
// The number of source indices received
MPI_Unpack(p_in, bufferSize, &position, &noRemoteSource, 1, MPI_INT, comm_);
// The number of destination indices received
MPI_Unpack(p_in, bufferSize, &position, &noRemoteDest, 1, MPI_INT, comm_);
// Indices for which we receive
RemoteIndexList* receive= new RemoteIndexList();
// Indices for which we send
RemoteIndexList* send=0;
MPI_Datatype type= MPITraits<PairType>::getType();
if(!twoIndexSets) {
if(sendTwo) {
send = new RemoteIndexList();
// Create both remote index sets simultaneously
unpackIndices(*send, *receive, noRemoteSource, sourcePairs, sourcePublish,
destPairs, destPublish, p_in, type, &position, bufferSize);
}else{
// we only need one list
unpackIndices(*receive, noRemoteSource, sourcePairs, sourcePublish,
p_in, type, &position, bufferSize, fromOurSelf);
send=receive;
}
}else{
int oldPos=position;
// Two index sets received
unpackIndices(*receive, noRemoteSource, destPairs, destPublish,
p_in, type, &position, bufferSize, fromOurSelf);
if(!sendTwo)
//unpack source entries again as destination entries
position=oldPos;
send = new RemoteIndexList();
unpackIndices(*send, noRemoteDest, sourcePairs, sourcePublish,
p_in, type, &position, bufferSize, fromOurSelf);
}
if(receive->empty() && send->empty()) {
if(send==receive) {
delete send;
}else{
delete send;
delete receive;
}
}else{
remoteIndices_.insert(std::make_pair(remoteProc,
std::make_pair(send,receive)));
}
}
template<typename T, typename A>
template<bool ignorePublic>
inline void RemoteIndices<T,A>::buildRemote(bool includeSelf_)
{
// Processor configuration
int rank, procs;
MPI_Comm_rank(comm_, &rank);
MPI_Comm_size(comm_, &procs);
// number of local indices to publish
// The indices of the destination will be send.
int sourcePublish, destPublish;
// Do we need to send two index sets?
char sendTwo = (source_ != target_);
if(procs==1 && !(sendTwo || includeSelf_))
// Nothing to communicate
return;
sourcePublish = (ignorePublic) ? source_->size() : noPublic(*source_);
if(sendTwo)
destPublish = (ignorePublic) ? target_->size() : noPublic(*target_);
else
// we only need to send one set of indices
destPublish = 0;
int maxPublish, publish=sourcePublish+destPublish;
// Calucate maximum number of indices send
MPI_Allreduce(&publish, &maxPublish, 1, MPI_INT, MPI_MAX, comm_);
// allocate buffers
typedef IndexPair<GlobalIndex,LocalIndex> PairType;
PairType** destPairs;
PairType** sourcePairs = new PairType*[sourcePublish>0 ? sourcePublish : 1];
if(sendTwo)
destPairs = new PairType*[destPublish>0 ? destPublish : 1];
else
destPairs=sourcePairs;
char** buffer = new char*[2];
int bufferSize;
int position=0;
int intSize;
int charSize;
// calculate buffer size
MPI_Datatype type = MPITraits<PairType>::getType();
MPI_Pack_size(maxPublish, type, comm_,
&bufferSize);
MPI_Pack_size(1, MPI_INT, comm_,
&intSize);
MPI_Pack_size(1, MPI_CHAR, comm_,
&charSize);
// Our message will contain the following:
// a bool whether two index sets where sent
// the size of the source and the dest indexset,
// then the source and destination indices
bufferSize += 2 * intSize + charSize;
if(bufferSize<=0) bufferSize=1;
buffer[0] = new char[bufferSize];
buffer[1] = new char[bufferSize];
// pack entries into buffer[0], p_out below!
MPI_Pack(&sendTwo, 1, MPI_CHAR, buffer[0], bufferSize, &position,
comm_);
// The number of indices we send for each index set
MPI_Pack(&sourcePublish, 1, MPI_INT, buffer[0], bufferSize, &position,
comm_);
MPI_Pack(&destPublish, 1, MPI_INT, buffer[0], bufferSize, &position,
comm_);
// Now pack the source indices and setup the destination pairs
packEntries<ignorePublic>(sourcePairs, *source_, buffer[0], type,
bufferSize, &position, sourcePublish);
// If necessary send the dest indices and setup the source pairs
if(sendTwo)
packEntries<ignorePublic>(destPairs, *target_, buffer[0], type,
bufferSize, &position, destPublish);
// Update remote indices for ourself
if(sendTwo|| includeSelf_)
unpackCreateRemote(buffer[0], sourcePairs, destPairs, rank, sourcePublish,
destPublish, bufferSize, sendTwo, includeSelf_);
neighbourIds.erase(rank);
if(neighbourIds.size()==0)
{
Dune::dvverb<<rank<<": Sending messages in a ring"<<std::endl;
// send messages in ring
for(int proc=1; proc<procs; proc++) {
// pointers to the current input and output buffers
char* p_out = buffer[1-(proc%2)];
char* p_in = buffer[proc%2];
MPI_Status status;
if(rank%2==0) {
MPI_Ssend(p_out, bufferSize, MPI_PACKED, (rank+1)%procs,
commTag_, comm_);
MPI_Recv(p_in, bufferSize, MPI_PACKED, (rank+procs-1)%procs,
commTag_, comm_, &status);
}else{
MPI_Recv(p_in, bufferSize, MPI_PACKED, (rank+procs-1)%procs,
commTag_, comm_, &status);
MPI_Ssend(p_out, bufferSize, MPI_PACKED, (rank+1)%procs,
commTag_, comm_);
}
// The process these indices are from
int remoteProc = (rank+procs-proc)%procs;
unpackCreateRemote(p_in, sourcePairs, destPairs, remoteProc, sourcePublish,
destPublish, bufferSize, sendTwo);
}
}
else
{
MPI_Request* requests=new MPI_Request[neighbourIds.size()];
MPI_Request* req=requests;
typedef typename std::set<int>::size_type size_type;
size_type noNeighbours=neighbourIds.size();
// setup sends
for(std::set<int>::iterator neighbour=neighbourIds.begin();
neighbour!= neighbourIds.end(); ++neighbour) {
// Only send the information to the neighbouring processors
MPI_Issend(buffer[0], position , MPI_PACKED, *neighbour, commTag_, comm_, req++);
}
//Test for received messages
for(size_type received=0; received <noNeighbours; ++received)
{
MPI_Status status;
// probe for next message
MPI_Probe(MPI_ANY_SOURCE, commTag_, comm_, &status);
int remoteProc=status.MPI_SOURCE;
int size;
MPI_Get_count(&status, MPI_PACKED, &size);
// receive message
MPI_Recv(buffer[1], size, MPI_PACKED, remoteProc,
commTag_, comm_, &status);
unpackCreateRemote(buffer[1], sourcePairs, destPairs, remoteProc, sourcePublish,
destPublish, bufferSize, sendTwo);
}
// wait for completion of pending requests
MPI_Status* statuses = new MPI_Status[neighbourIds.size()];
if(MPI_ERR_IN_STATUS==MPI_Waitall(neighbourIds.size(), requests, statuses)) {
for(size_type i=0; i < neighbourIds.size(); ++i)
if(statuses[i].MPI_ERROR!=MPI_SUCCESS) {
std::cerr<<rank<<": MPI_Error occurred while receiving message."<<std::endl;
MPI_Abort(comm_, 999);
}
}
delete[] requests;
delete[] statuses;
}
// delete allocated memory
if(destPairs!=sourcePairs)
delete[] destPairs;
delete[] sourcePairs;
delete[] buffer[0];
delete[] buffer[1];
delete[] buffer;
}
template<typename T, typename A>
inline void RemoteIndices<T,A>::unpackIndices(RemoteIndexList& remote,
int remoteEntries,
PairType** local,
int localEntries,
char* p_in,
MPI_Datatype type,
int* position,
int bufferSize,
bool fromOurSelf)
{
if(remoteEntries==0)
return;
PairType index(1);
MPI_Unpack(p_in, bufferSize, position, &index, 1,
type, comm_);
GlobalIndex oldGlobal=index.global();
int n_in=0, localIndex=0;
//Check if we know the global index
while(localIndex<localEntries) {
if(local[localIndex]->global()==index.global()) {
int oldLocalIndex=localIndex;
while(localIndex<localEntries &&
local[localIndex]->global()==index.global()) {
if(!fromOurSelf || index.local().attribute() !=
local[localIndex]->local().attribute())
// if index is from us it has to have a different attribute
remote.push_back(RemoteIndex(index.local().attribute(),
local[localIndex]));
localIndex++;
}
// unpack next remote index
if((++n_in) < remoteEntries) {
MPI_Unpack(p_in, bufferSize, position, &index, 1,
type, comm_);
if(index.global()==oldGlobal)
// Restart comparison for the same global indices
localIndex=oldLocalIndex;
else
oldGlobal=index.global();
}else{
// No more received indices
break;
}
continue;
}
if (local[localIndex]->global()<index.global()) {
// compare with next entry in our list
++localIndex;
}else{
// We do not know the index, unpack next
if((++n_in) < remoteEntries) {
MPI_Unpack(p_in, bufferSize, position, &index, 1,
type, comm_);
oldGlobal=index.global();
}else
// No more received indices
break;
}
}
// Unpack the other received indices without doing anything
while(++n_in < remoteEntries)
MPI_Unpack(p_in, bufferSize, position, &index, 1,
type, comm_);
}
template<typename T, typename A>
inline void RemoteIndices<T,A>::unpackIndices(RemoteIndexList& send,
RemoteIndexList& receive,
int remoteEntries,
PairType** localSource,
int localSourceEntries,
PairType** localDest,
int localDestEntries,
char* p_in,
MPI_Datatype type,
int* position,
int bufferSize)
{
int n_in=0, sourceIndex=0, destIndex=0;
//Check if we know the global index
while(n_in<remoteEntries && (sourceIndex<localSourceEntries || destIndex<localDestEntries)) {
// Unpack next index
PairType index;
MPI_Unpack(p_in, bufferSize, position, &index, 1,
type, comm_);
n_in++;
// Advance until global index in localSource and localDest are >= than the one in the unpacked index
while(sourceIndex<localSourceEntries && localSource[sourceIndex]->global()<index.global())
sourceIndex++;
while(destIndex<localDestEntries && localDest[destIndex]->global()<index.global())
destIndex++;
// Add a remote index if we found the global index.
if(sourceIndex<localSourceEntries && localSource[sourceIndex]->global()==index.global())
send.push_back(RemoteIndex(index.local().attribute(),
localSource[sourceIndex]));
if(destIndex < localDestEntries && localDest[destIndex]->global() == index.global())
receive.push_back(RemoteIndex(index.local().attribute(),
localDest[sourceIndex]));
}
}
template<typename T, typename A>
inline void RemoteIndices<T,A>::free()
{
typedef typename RemoteIndexMap::iterator Iterator;
Iterator lend = remoteIndices_.end();
for(Iterator lists=remoteIndices_.begin(); lists != lend; ++lists) {
if(lists->second.first==lists->second.second) {
// there is only one remote index list.
delete lists->second.first;
}else{
delete lists->second.first;
delete lists->second.second;
}
}
remoteIndices_.clear();
firstBuild=true;
}
template<typename T, typename A>
inline int RemoteIndices<T,A>::neighbours() const
{
return remoteIndices_.size();
}
template<typename T, typename A>
template<bool ignorePublic>
inline void RemoteIndices<T,A>::rebuild()
{
// Test whether a rebuild is Needed.
if(firstBuild ||
ignorePublic!=publicIgnored || !
isSynced()) {
free();
buildRemote<ignorePublic>(includeSelf);
sourceSeqNo_ = source_->seqNo();
destSeqNo_ = target_->seqNo();
firstBuild=false;
publicIgnored=ignorePublic;
}
}
template<typename T, typename A>
inline bool RemoteIndices<T,A>::isSynced() const
{
return sourceSeqNo_==source_->seqNo() && destSeqNo_ ==target_->seqNo();
}
template<typename T, typename A>
template<bool mode, bool send>
RemoteIndexListModifier<T,A,mode> RemoteIndices<T,A>::getModifier(int process)
{
// The user are on their own now!
// We assume they know what they are doing and just set the
// remote indices to synced status.
sourceSeqNo_ = source_->seqNo();
destSeqNo_ = target_->seqNo();
typename RemoteIndexMap::iterator found = remoteIndices_.find(process);
if(found == remoteIndices_.end())
{
if(source_ != target_)
found = remoteIndices_.insert(found, std::make_pair(process,
std::make_pair(new RemoteIndexList(),
new RemoteIndexList())));
else{
RemoteIndexList* rlist = new RemoteIndexList();
found = remoteIndices_.insert(found,
std::make_pair(process,
std::make_pair(rlist, rlist)));
}
}
firstBuild = false;
if(send)
return RemoteIndexListModifier<T,A,mode>(*source_, *(found->second.first));
else
return RemoteIndexListModifier<T,A,mode>(*target_, *(found->second.second));
}
template<typename T, typename A>
inline typename RemoteIndices<T,A>::const_iterator
RemoteIndices<T,A>::find(int proc) const
{
return remoteIndices_.find(proc);
}
template<typename T, typename A>
inline typename RemoteIndices<T,A>::const_iterator
RemoteIndices<T,A>::begin() const
{
return remoteIndices_.begin();
}
template<typename T, typename A>
inline typename RemoteIndices<T,A>::const_iterator
RemoteIndices<T,A>::end() const
{
return remoteIndices_.end();
}
template<typename T, typename A>
bool RemoteIndices<T,A>::operator==(const RemoteIndices& ri)
{
if(neighbours()!=ri.neighbours())
return false;
typedef RemoteIndexList RList;
typedef typename std::map<int,std::pair<RList*,RList*> >::const_iterator const_iterator;
const const_iterator rend = remoteIndices_.end();
for(const_iterator rindex = remoteIndices_.begin(), rindex1=ri.remoteIndices_.begin(); rindex!=rend; ++rindex, ++rindex1) {
if(rindex->first != rindex1->first)
return false;
if(*(rindex->second.first) != *(rindex1->second.first))
return false;
if(*(rindex->second.second) != *(rindex1->second.second))
return false;
}
return true;
}
template<class T, class A, bool mode>
RemoteIndexListModifier<T,A,mode>::RemoteIndexListModifier(const ParallelIndexSet& indexSet,
RemoteIndexList& rList)
: rList_(&rList), indexSet_(&indexSet), iter_(rList.beginModify()), end_(rList.end()), first_(true)
{
if(MODIFYINDEXSET) {
assert(indexSet_);
for(ConstIterator iter=iter_; iter != end_; ++iter)
glist_.push_back(iter->localIndexPair().global());
giter_ = glist_.beginModify();
}
}
template<typename T, typename A, bool mode>
RemoteIndexListModifier<T,A,mode>::RemoteIndexListModifier(const RemoteIndexListModifier<T,A,mode>& other)
: rList_(other.rList_), indexSet_(other.indexSet_),
glist_(other.glist_), iter_(other.iter_), giter_(other.giter_), end_(other.end_),
first_(other.first_), last_(other.last_)
{}
template<typename T, typename A, bool mode>
inline void RemoteIndexListModifier<T,A,mode>::repairLocalIndexPointers() throw(InvalidIndexSetState)
{
if(MODIFYINDEXSET) {
// repair pointers to local index set.
#ifdef DUNE_ISTL_WITH_CHECKING
if(indexSet_->state()!=GROUND)
DUNE_THROW(InvalidIndexSetState, "Index has to be in ground mode for repairing pointers to indices");
#endif
typedef typename ParallelIndexSet::const_iterator IndexIterator;
typedef typename GlobalList::const_iterator GlobalIterator;
typedef typename RemoteIndexList::iterator Iterator;
GlobalIterator giter = glist_.begin();
IndexIterator index = indexSet_->begin();
for(Iterator iter=rList_->begin(); iter != end_; ++iter) {
while(index->global()<*giter) {
++index;
#ifdef DUNE_ISTL_WITH_CHECKING
if(index == indexSet_->end())
DUNE_THROW(InvalidPosition, "No such global index in set!");
#endif
}
#ifdef DUNE_ISTL_WITH_CHECKING
if(index->global() != *giter)
DUNE_THROW(InvalidPosition, "No such global index in set!");
#endif
iter->localIndex_ = &(*index);
}
}
}
template<typename T, typename A, bool mode>
inline void RemoteIndexListModifier<T,A,mode>::insert(const RemoteIndex& index) throw(InvalidPosition)
{
static_assert(!mode,"Not allowed if the mode indicates that new indices"
"might be added to the underlying index set. Use "
"insert(const RemoteIndex&, const GlobalIndex&) instead");
#ifdef DUNE_ISTL_WITH_CHECKING
if(!first_ && index.localIndexPair().global()<last_)
DUNE_THROW(InvalidPosition, "Modifcation of remote indices have to occur with ascending global index.");
#endif
// Move to the correct position
while(iter_ != end_ && iter_->localIndexPair().global() < index.localIndexPair().global()) {
++iter_;
}
// No duplicate entries allowed
assert(iter_==end_ || iter_->localIndexPair().global() != index.localIndexPair().global());
iter_.insert(index);
last_ = index.localIndexPair().global();
first_ = false;
}
template<typename T, typename A, bool mode>
inline void RemoteIndexListModifier<T,A,mode>::insert(const RemoteIndex& index, const GlobalIndex& global) throw(InvalidPosition)
{
static_assert(mode,"Not allowed if the mode indicates that no new indices"
"might be added to the underlying index set. Use "
"insert(const RemoteIndex&) instead");
#ifdef DUNE_ISTL_WITH_CHECKING
if(!first_ && global<last_)
DUNE_THROW(InvalidPosition, "Modification of remote indices have to occur with ascending global index.");
#endif
// Move to the correct position
while(iter_ != end_ && *giter_ < global) {
++giter_;
++iter_;
}
// No duplicate entries allowed
assert(iter_->localIndexPair().global() != global);
iter_.insert(index);
giter_.insert(global);
last_ = global;
first_ = false;
}
template<typename T, typename A, bool mode>
bool RemoteIndexListModifier<T,A,mode>::remove(const GlobalIndex& global) throw(InvalidPosition)
{
#ifdef DUNE_ISTL_WITH_CHECKING
if(!first_ && global<last_)
DUNE_THROW(InvalidPosition, "Modifcation of remote indices have to occur with ascending global index.");
#endif
bool found= false;
if(MODIFYINDEXSET) {
// Move to the correct position
while(iter_!=end_ && *giter_< global) {
++giter_;
++iter_;
}
if(*giter_ == global) {
giter_.remove();
iter_.remove();
found=true;
}
}else{
while(iter_!=end_ && iter_->localIndexPair().global() < global)
++iter_;
if(iter_->localIndexPair().global()==global) {
iter_.remove();
found = true;
}
}
last_ = global;
first_ = false;
return found;
}
template<typename T, typename A>
template<bool send>
inline typename RemoteIndices<T,A>::CollectiveIteratorT RemoteIndices<T,A>::iterator() const
{
return CollectiveIterator<T,A>(remoteIndices_, send);
}
template<typename T, typename A>
inline MPI_Comm RemoteIndices<T,A>::communicator() const
{
return comm_;
}
template<typename T, typename A>
CollectiveIterator<T,A>::CollectiveIterator(const RemoteIndexMap& pmap, bool send)
{
typedef typename RemoteIndexMap::const_iterator const_iterator;
const const_iterator end=pmap.end();
for(const_iterator process=pmap.begin(); process != end; ++process) {
const RemoteIndexList* list = send ? process->second.first : process->second.second;
typedef typename RemoteIndexList::const_iterator iterator;
map_.insert(std::make_pair(process->first,
std::pair<iterator, const iterator>(list->begin(), list->end())));
}
}
template<typename T, typename A>
inline void CollectiveIterator<T,A>::advance(const GlobalIndex& index)
{
typedef typename Map::iterator iterator;
typedef typename Map::const_iterator const_iterator;
const const_iterator end = map_.end();
for(iterator iter = map_.begin(); iter != end;) {
// Step the iterator until we are >= index
typename RemoteIndexList::const_iterator current = iter->second.first;
typename RemoteIndexList::const_iterator rend = iter->second.second;
RemoteIndex remoteIndex;
if(current != rend)
remoteIndex = *current;
while(iter->second.first!=iter->second.second && iter->second.first->localIndexPair().global()<index)
++(iter->second.first);
// erase from the map if there are no more entries.
if(iter->second.first == iter->second.second)
map_.erase(iter++);
else{
++iter;
}
}
index_=index;
noattribute=true;
}
template<typename T, typename A>
inline void CollectiveIterator<T,A>::advance(const GlobalIndex& index,
const Attribute& attribute)
{
typedef typename Map::iterator iterator;
typedef typename Map::const_iterator const_iterator;
const const_iterator end = map_.end();
for(iterator iter = map_.begin(); iter != end;) {
// Step the iterator until we are >= index
typename RemoteIndexList::const_iterator current = iter->second.first;
typename RemoteIndexList::const_iterator rend = iter->second.second;
RemoteIndex remoteIndex;
if(current != rend)
remoteIndex = *current;
// Move to global index or bigger
while(iter->second.first!=iter->second.second && iter->second.first->localIndexPair().global()<index)
++(iter->second.first);
// move to attribute or bigger
while(iter->second.first!=iter->second.second
&& iter->second.first->localIndexPair().global()==index
&& iter->second.first->localIndexPair().local().attribute()<attribute)
++(iter->second.first);
// erase from the map if there are no more entries.
if(iter->second.first == iter->second.second)
map_.erase(iter++);
else{
++iter;
}
}
index_=index;
attribute_=attribute;
noattribute=false;
}
template<typename T, typename A>
inline CollectiveIterator<T,A>& CollectiveIterator<T,A>::operator++()
{
typedef typename Map::iterator iterator;
typedef typename Map::const_iterator const_iterator;
const const_iterator end = map_.end();
for(iterator iter = map_.begin(); iter != end;) {
// Step the iterator until we are >= index
typename RemoteIndexList::const_iterator current = iter->second.first;
typename RemoteIndexList::const_iterator rend = iter->second.second;
// move all iterators pointing to the current global index to next value
if(iter->second.first->localIndexPair().global()==index_ &&
(noattribute || iter->second.first->localIndexPair().local().attribute() == attribute_))
++(iter->second.first);
// erase from the map if there are no more entries.
if(iter->second.first == iter->second.second)
map_.erase(iter++);
else{
++iter;
}
}
return *this;
}
template<typename T, typename A>
inline bool CollectiveIterator<T,A>::empty()
{
return map_.empty();
}
template<typename T, typename A>
inline typename CollectiveIterator<T,A>::iterator
CollectiveIterator<T,A>::begin()
{
if(noattribute)
return iterator(map_.begin(), map_.end(), index_);
else
return iterator(map_.begin(), map_.end(), index_,
attribute_);
}
template<typename T, typename A>
inline typename CollectiveIterator<T,A>::iterator
CollectiveIterator<T,A>::end()
{
return iterator(map_.end(), map_.end(), index_);
}
template<typename TG, typename TA>
inline std::ostream& operator<<(std::ostream& os, const RemoteIndex<TG,TA>& index)
{
os<<"[global="<<index.localIndexPair().global()<<", remote attribute="<<index.attribute()<<" local attribute="<<index.localIndexPair().local().attribute()<<"]";
return os;
}
template<typename T, typename A>
inline std::ostream& operator<<(std::ostream& os, const RemoteIndices<T,A>& indices)
{
int rank;
MPI_Comm_rank(indices.comm_, &rank);
typedef typename RemoteIndices<T,A>::RemoteIndexList RList;
typedef typename std::map<int,std::pair<RList*,RList*> >::const_iterator const_iterator;
const const_iterator rend = indices.remoteIndices_.end();
for(const_iterator rindex = indices.remoteIndices_.begin(); rindex!=rend; ++rindex) {
os<<rank<<": Prozess "<<rindex->first<<":";
if(!rindex->second.first->empty()) {
os<<" send:";
const typename RList::const_iterator send= rindex->second.first->end();
for(typename RList::const_iterator index = rindex->second.first->begin();
index != send; ++index)
os<<*index<<" ";
os<<std::endl;
}
if(!rindex->second.second->empty()) {
os<<rank<<": Prozess "<<rindex->first<<": "<<"receive: ";
for(const auto& index : *(rindex->second.second))
os << index << " ";
}
os<<std::endl<<std::flush;
}
return os;
}
/** @} */
}
#endif
#endif
|