/usr/include/dune/istl/bcrsmatrix.hh is in libdune-istl-dev 2.4.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 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 | // -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
// vi: set et ts=4 sw=2 sts=2:
#ifndef DUNE_ISTL_BCRSMATRIX_HH
#define DUNE_ISTL_BCRSMATRIX_HH
#include <cmath>
#include <complex>
#include <set>
#include <iostream>
#include <algorithm>
#include <numeric>
#include <vector>
#include <map>
#include "istlexception.hh"
#include "bvector.hh"
#include "matrixutils.hh"
#include <dune/common/stdstreams.hh>
#include <dune/common/iteratorfacades.hh>
#include <dune/common/typetraits.hh>
#include <dune/common/ftraits.hh>
#include <dune/common/nullptr.hh>
/*! \file
* \brief Implementation of the BCRSMatrix class
*/
namespace Dune {
/**
* @defgroup ISTL_SPMV Sparse Matrix and Vector classes
* @ingroup ISTL
* @brief Matrix and Vector classes that support a block recursive
* structure capable of representing the natural structure from Finite
* Element discretisations.
*
*
* The interface of our matrices is designed according to what they
* represent from a mathematical point of view. The vector classes are
* representations of vector spaces:
*
* - FieldVector represents a vector space \f$V=K^n\f$ where the field \f$K\f$
* is represented by a numeric type (e.g. double, float, complex). \f$n\f$
* is known at compile time.
* - BlockVector represents a vector space \f$V=W\times W \times W \times\cdots\times W\f$
* where W is itself a vector space.
* - VariableBlockVector represents a vector space having a two-level
* block structure of the form
* \f$V=B^{n_1}\times B^{n_2}\times\ldots \times B^{n_m}\f$, i.e. it is constructed
* from \f$m\f$ vector spaces, \f$i=1,\ldots,m\f$.
*
* The matrix classes represent linear maps \f$A: V \mapsto W\f$
* from vector space \f$V\f$ to vector space \f$W\f$ the recursive block
* structure of the matrix rows and columns immediately follows
* from the recursive block structure of the vectors representing
* the domain and range of the mapping, respectively:
* - FieldMatrix represents a linear map \f$M: V_1 \to V_2\f$ where
* \f$V_1=K^n\f$ and \f$V_2=K^m\f$ are vector spaces over the same field represented by a numerix type.
* - BCRSMatrix represents a linear map \f$M: V_1 \to V_2\f$ where
* \f$V_1=W\times W \times W \times\cdots\times W\f$ and \f$V_2=W\times W \times W \times\cdots\times W\f$
* where W is itself a vector space.
* - VariableBCRSMatrix is not yet implemented.
*/
/**
@addtogroup ISTL_SPMV
@{
*/
template<typename M>
struct MatrixDimension;
//! Statistics about compression achieved in implicit mode.
/**
* To enable the user to tune parameters of the implicit build mode of a
* Dune::BCRSMatrix manually, some statistics are exported upon during
* the compression step.
*
*/
template<typename size_type>
struct CompressionStatistics
{
//! average number of non-zeroes per row.
double avg;
//! maximum number of non-zeroes per row.
size_type maximum;
//! total number of elements written to the overflow area during construction.
size_type overflow_total;
//! fraction of wasted memory resulting from non-used overflow area.
/**
* mem_ratio is equal to `nonzeros()/(# allocated matrix entries)`.
*/
double mem_ratio;
};
//! A wrapper for uniform access to the BCRSMatrix during and after the build stage in implicit build mode.
/**
* The implicit build mode of Dune::BCRSMatrix handles matrices differently during
* assembly and afterwards. Using this class, one can wrap a BCRSMatrix to allow
* use with code that is not written for the new build mode specifically. The wrapper
* forwards any calls to operator[][] to the entry() method.The assembly code
* does not even necessarily need to know that the underlying matrix is sparse.
* Dune::AMG uses this to reassemble an existing matrix without code duplication.
* The compress() method of Dune::BCRSMatrix still has to be called from outside
* this wrapper after the pattern assembly is finished.
*
* \tparam M_ the matrix type
*/
template<class M_>
class ImplicitMatrixBuilder
{
public:
//! The underlying matrix.
typedef M_ Matrix;
//! The block_type of the underlying matrix.
typedef typename Matrix::block_type block_type;
//! The size_type of the underlying matrix.
typedef typename Matrix::size_type size_type;
//! Proxy row object for entry access.
/**
* During matrix construction, there are no fully functional rows available
* yet, so we instead hand out a simple proxy which only allows accessing
* individual entries using operator[].
*/
class row_object
{
public:
//! Returns entry in column j.
block_type& operator[](size_type j) const
{
return _m.entry(_i,j);
}
#ifndef DOXYGEN
row_object(Matrix& m, size_type i)
: _m(m)
, _i(i)
{}
#endif
private:
Matrix& _m;
size_type _i;
};
//! Creates an ImplicitMatrixBuilder for matrix m.
/**
* \note You can only pass a completely set up matrix to this constructor:
* All of setBuildMode(), setImplicitBuildModeParameters() and setSize()
* must have been called with the correct values.
*
*/
ImplicitMatrixBuilder(Matrix& m)
: _m(m)
{
if (m.buildMode() != Matrix::implicit)
DUNE_THROW(BCRSMatrixError,"You can only create an ImplicitBuilder for a matrix in implicit build mode");
if (m.buildStage() != Matrix::building)
DUNE_THROW(BCRSMatrixError,"You can only create an ImplicitBuilder for a matrix with set size that has not been compressed() yet");
}
//! Sets up matrix m for implicit construction using the given parameters and creates an ImplicitBmatrixuilder for it.
/**
* Using this constructor, you can perform the necessary matrix setup and the creation
* of the ImplicitMatrixBuilder in a single step. The matrix must still be in the build stage
* notAllocated, otherwise a BCRSMatrixError will be thrown. For a detailed explanation
* of the matrix parameters, see BCRSMatrix.
*
* \param m the matrix to be built
* \param rows the number of matrix rows
* \param cols the number of matrix columns
* \param avg_cols_per_row the average number of non-zero columns per row
* \param overflow_fraction the amount of overflow to reserve in the matrix
*
* \sa BCRSMatrix
*/
ImplicitMatrixBuilder(Matrix& m, size_type rows, size_type cols, size_type avg_cols_per_row, double overflow_fraction)
: _m(m)
{
if (m.buildStage() != Matrix::notAllocated)
DUNE_THROW(BCRSMatrixError,"You can only set up a matrix for this ImplicitBuilder if it has no memory allocated yet");
m.setBuildMode(Matrix::implicit);
m.setImplicitBuildModeParameters(avg_cols_per_row,overflow_fraction);
m.setSize(rows,cols);
}
//! Returns a proxy for entries in row i.
row_object operator[](size_type i) const
{
return row_object(_m,i);
}
//! The number of rows in the matrix.
size_type N() const
{
return _m.N();
}
//! The number of columns in the matrix.
size_type M() const
{
return _m.M();
}
private:
Matrix& _m;
};
/**
\brief A sparse block matrix with compressed row storage
Implements a block compressed row storage scheme. The block
type B can be any type implementing the matrix interface.
Different ways to build up a compressed row
storage matrix are supported:
1. Row-wise scheme
2. Random scheme
3. implicit scheme
Error checking: no error checking is provided normally.
Setting the compile time switch DUNE_ISTL_WITH_CHECKING
enables error checking.
Details:
1. Row-wise scheme
Rows are built up in sequential order. Size of the row and
the column indices are defined. A row can be used as soon as it
is initialized. With respect to memory there are two variants of
this scheme: (a) number of non-zeroes known in advance (application
finite difference schemes), (b) number of non-zeroes not known
in advance (application: Sparse LU, ILU(n)).
\code
#include<dune/common/fmatrix.hh>
#include<dune/istl/bcrsmatrix.hh>
...
typedef FieldMatrix<double,2,2> M;
// third parameter is an optional upper bound for the number
// of nonzeros. If given the matrix will use one array for all values
// as opposed to one for each row.
BCRSMatrix<M> B(4,4,12,BCRSMatrix<M>::row_wise);
typedef BCRSMatrix<M>::CreateIterator Iter;
for(Iter row=B.createbegin(); row!=B.createend(); ++row){
// Add nonzeros for left neighbour, diagonal and right neighbour
if(row.index()>0)
row.insert(row.index()-1);
row.insert(row.index());
if(row.index()<B.N()-1)
row.insert(row.index()+1);
}
// Now the sparsity pattern is fully set up and we can add values
B[0][0]=2;
...
\endcode
2. Random scheme
For general finite element implementations the number of rows n
is known, the number of non-zeroes might also be known (e.g.
\#edges + \#nodes for P1) but the size of a row and the indices of a row
can not be defined in sequential order.
\code
#include<dune/common/fmatrix.hh>
#include<dune/istl/bcrsmatrix.hh>
...
typedef FieldMatrix<double,2,2> M;
BCRSMatrix<M> B(4,4,BCRSMatrix<M>::random);
// initially set row size for each row
B.setrowsize(0,1);
B.setrowsize(3,4);
B.setrowsize(2,1);
B.setrowsize(1,1);
// increase row size for row 2
B.incrementrowsize(2)
// finalize row setup phase
B.endrowsizes();
// add column entries to rows
B.addindex(0,0);
B.addindex(3,1);
B.addindex(2,2);
B.addindex(1,1);
B.addindex(2,0);
B.addindex(3,2);
B.addindex(3,0);
B.addindex(3,3);
// finalize column setup phase
B.endindices();
// set entries using the random access operator
B[0][0] = 1;
B[1][1] = 2;
B[2][0] = 3;
B[2][2] = 4;
B[3][1] = 5;
B[3][2] = 6;
B[3][0] = 7;
B[3][3] = 8;
\endcode
3. implicit scheme
With the above Random Scheme, the sparsity pattern has to be determined
and stored before the matrix is assembled. This leads to increased memory
usage and computation time. Often, one has good a priori
knowledge about the number of entries a row contains on average. `implicit`
mode tries to make use of that knowledge by allocating memory based on
that average. Entries in rows with more non-zeroes than the average value
are written to an overflow area during the initial assembly phase, up to a
specified maximum number of overflow entries that must not be exceeded.
After all indices are added a compression step optimizes the matrix and
integrates any entries from the overflow area into the standard BCRS storage
scheme.
To use this mode use the following methods:
Construct the matrix via
- BCRSMatrix(size_type _n, size_type _m, size_type _avg, double _overflowsize, BuildMode bm)
- void setSize(size_type rows, size_type columns, size_type nnz=0) after setting
the buildmode to implicit and the compression parameters via setImplicitBuildModeParameters(size_type _avg, double _overflow)
Here, the parameter `_avg` denotes the average number of matrix entries per row, while
`_overflowsize` reserves `_n * _overflowsize * _avg` entries in the overflow area.
\warning If you exceed this number of overflow entries during the assembly phase, matrix
construction fails and an exception will be thrown!
Start filling your matrix by calling entry(size_type row, size_type col),
which returns the corresponding matrix entry, creating it on the fly if
it did not exist yet. Please note that this method may be slightly slower than
accessing entries via `matrix[row][col]` after the initial assembly because
of the additional overhead of searching the overflow area.
The matrix pattern is created by implicitly by simply accessing nonzero entries
during the initial matrix assembly.
After the entry-method has been called for each nonzero matrix entry at least once,
you can call compress() to reorganize the data into the standard BCRS data layout,
which sets the matrix state to `built`. No matrix entries may be added after
this step. compress() returns a value of type Dune::CompressionStatistics, which
you can inspect to tune the construction parameters `_avg` and `_overflowsize`.
Use of copy constructor, assignment operator and matrix vector arithmetics
are not supported until the matrix is fully built.
In the following sample code, an array with 28 entries will be reserved
\code
#include<dune/common/fmatrix.hh>
#include<dune/istl/bcrsmatrix.hh>
typedef Dune::BCRSMatrix<Dune::FieldMatrix<double,1,1> > M;
M m(10, 10, 2, 0.4, M::implicit);
//fill in some arbitrary entries, even operations on these would be possible,
//you get a reference to the entry! the order of these statements is irrelevant!
m.entry(0,0) = 0.;
m.entry(8,0) = 0.;
m.entry(1,8) = 0.; m.entry(1,0) = 0.; m.entry(1,5) = 0.;
m.entry(2,0) = 0.;
m.entry(3,5) = 0.; m.entry(3,0) = 0.; m.entry(3,8) = 0.;
m.entry(4,0) = 0.;
m.entry(9,0) = 0.; m.entry(9,5) = 0.; m.entry(9,8) = 0.;
m.entry(5,0) = 0.; m.entry(5,5) = 0.; m.entry(5,8) = 0.;
m.entry(6,0) = 0.;
m.entry(7,0) = 0.; m.entry(7,5) = 0.; m.entry(7,8) = 0.;
// internally the index array now looks like this (second row are the row pointers):
// xxxxxxxx0x800x500x050x050x05
// ........|.|.|.|.|.|.|.|.|.|.
// and the overflow area contains (1,5,0.0), (3,8,0.0), (5,8,0.0), (7,8,0.0), (9,8,0.0)
// the data array has similar structure.
//finish building by compressing the array
Dune::CompressionStatistics<M::size_type> stats = m.compress();
// internally the index array now looks like this:
// 00580058005800580058xxxxxxxx
// ||..||..||..||..||..........
\endcode
*/
template<class B, class A=std::allocator<B> >
class BCRSMatrix
{
friend struct MatrixDimension<BCRSMatrix>;
public:
enum BuildStage {
/** @brief Matrix is not built at all, no memory has been allocated, build mode and size can still be set. */
notbuilt=0,
/** @brief Matrix is not built at all, no memory has been allocated, build mode and size can still be set. */
notAllocated=0,
/** @brief Matrix is currently being built, some memory has been allocated, build mode and size are fixed. */
building=1,
/** @brief The row sizes of the matrix are known.
*
* Only used in random mode.
*/
rowSizesBuilt=2,
/** @brief The matrix structure is fully built. */
built=3
};
//===== type definitions and constants
//! export the type representing the field
typedef typename B::field_type field_type;
//! export the type representing the components
typedef B block_type;
//! export the allocator type
typedef A allocator_type;
//! implement row_type with compressed vector
typedef CompressedBlockVectorWindow<B,A> row_type;
//! The type for the index access and the size
typedef typename A::size_type size_type;
//! The type for the statistics object returned by compress()
typedef ::Dune::CompressionStatistics<size_type> CompressionStatistics;
//! increment block level counter
enum {
//! The number of blocklevels the matrix contains.
blocklevel = B::blocklevel+1
};
//! we support two modes
enum BuildMode {
/**
* @brief Build in a row-wise manner.
*
* Rows are built up in sequential order. Size of the row and
* the column indices are defined. A row can be used as soon as it
* is initialized. With respect to memory there are two variants of
* this scheme: (a) number of non-zeroes known in advance (application
* finite difference schemes), (b) number of non-zeroes not known
* in advance (application: Sparse LU, ILU(n)).
*/
row_wise,
/**
* @brief Build entries randomly.
*
* For general finite element implementations the number of rows n
* is known, the number of non-zeroes might also be known (e.g.
* \#edges + \#nodes for P1) but the size of a row and the indices of a row
* can not be defined in sequential order.
*/
random,
/**
* @brief Build entries randomly with an educated guess on entries per row.
*
* Allows random order generation as in random mode, but row sizes do not need
* to be given first. Instead an average number of non-zeroes per row is passed
* to the constructor. Matrix setup is finished with compress(), full data access
* during build stage is possible.
*/
implicit,
/**
* @brief Build mode not set!
*/
unknown
};
//===== random access interface to rows of the matrix
//! random access to the rows
row_type& operator[] (size_type i)
{
#ifdef DUNE_ISTL_WITH_CHECKING
if (build_mode == implicit && ready != built)
DUNE_THROW(BCRSMatrixError,"You cannot use operator[] in implicit build mode before calling compress()");
if (r==0) DUNE_THROW(BCRSMatrixError,"row not initialized yet");
if (i>=n) DUNE_THROW(BCRSMatrixError,"index out of range");
#endif
return r[i];
}
//! same for read only access
const row_type& operator[] (size_type i) const
{
#ifdef DUNE_ISTL_WITH_CHECKING
if (build_mode == implicit && ready != built)
DUNE_THROW(BCRSMatrixError,"You cannot use operator[] in implicit build mode before calling compress()");
if (built!=ready) DUNE_THROW(BCRSMatrixError,"row not initialized yet");
if (i>=n) DUNE_THROW(BCRSMatrixError,"index out of range");
#endif
return r[i];
}
//===== iterator interface to rows of the matrix
//! %Iterator access to matrix rows
template<class T>
class RealRowIterator
: public RandomAccessIteratorFacade<RealRowIterator<T>, T>
{
public:
//! \brief The unqualified value type
typedef typename remove_const<T>::type ValueType;
friend class RandomAccessIteratorFacade<RealRowIterator<const ValueType>, const ValueType>;
friend class RandomAccessIteratorFacade<RealRowIterator<ValueType>, ValueType>;
friend class RealRowIterator<const ValueType>;
friend class RealRowIterator<ValueType>;
//! constructor
RealRowIterator (row_type* _p, size_type _i)
: p(_p), i(_i)
{}
//! empty constructor, use with care!
RealRowIterator ()
: p(0), i(0)
{}
RealRowIterator(const RealRowIterator<ValueType>& it)
: p(it.p), i(it.i)
{}
//! return index
size_type index () const
{
return i;
}
std::ptrdiff_t distanceTo(const RealRowIterator<ValueType>& other) const
{
assert(other.p==p);
return (other.i-i);
}
std::ptrdiff_t distanceTo(const RealRowIterator<const ValueType>& other) const
{
assert(other.p==p);
return (other.i-i);
}
//! equality
bool equals (const RealRowIterator<ValueType>& other) const
{
assert(other.p==p);
return i==other.i;
}
//! equality
bool equals (const RealRowIterator<const ValueType>& other) const
{
assert(other.p==p);
return i==other.i;
}
private:
//! prefix increment
void increment()
{
++i;
}
//! prefix decrement
void decrement()
{
--i;
}
void advance(std::ptrdiff_t diff)
{
i+=diff;
}
T& elementAt(std::ptrdiff_t diff) const
{
return p[i+diff];
}
//! dereferencing
row_type& dereference () const
{
return p[i];
}
row_type* p;
size_type i;
};
//! The iterator over the (mutable matrix rows
typedef RealRowIterator<row_type> iterator;
typedef RealRowIterator<row_type> Iterator;
//! Get iterator to first row
Iterator begin ()
{
return Iterator(r,0);
}
//! Get iterator to one beyond last row
Iterator end ()
{
return Iterator(r,n);
}
//! @returns an iterator that is positioned before
//! the end iterator of the rows, i.e. at the last row.
Iterator beforeEnd ()
{
return Iterator(r,n-1);
}
//! @returns an iterator that is positioned before
//! the first row of the matrix.
Iterator beforeBegin ()
{
return Iterator(r,-1);
}
//! rename the iterators for easier access
typedef Iterator RowIterator;
/** \brief Iterator for the entries of each row */
typedef typename row_type::Iterator ColIterator;
//! The const iterator over the matrix rows
typedef RealRowIterator<const row_type> const_iterator;
typedef RealRowIterator<const row_type> ConstIterator;
//! Get const iterator to first row
ConstIterator begin () const
{
return ConstIterator(r,0);
}
//! Get const iterator to one beyond last row
ConstIterator end () const
{
return ConstIterator(r,n);
}
//! @returns an iterator that is positioned before
//! the end iterator of the rows. i.e. at the last row.
ConstIterator beforeEnd() const
{
return ConstIterator(r,n-1);
}
//! @returns an iterator that is positioned before
//! the first row of the matrix.
ConstIterator beforeBegin () const
{
return ConstIterator(r,-1);
}
//! rename the const row iterator for easier access
typedef ConstIterator ConstRowIterator;
//! Const iterator to the entries of a row
typedef typename row_type::ConstIterator ConstColIterator;
//===== constructors & resizers
// we use a negative overflowsize to indicate that the implicit
// mode parameters have not been set yet
//! an empty matrix
BCRSMatrix ()
: build_mode(unknown), ready(notAllocated), n(0), m(0), nnz_(0),
allocationSize(0), r(0), a(0),
avg(0), overflowsize(-1.0)
{}
//! matrix with known number of nonzeroes
BCRSMatrix (size_type _n, size_type _m, size_type _nnz, BuildMode bm)
: build_mode(bm), ready(notAllocated), n(0), m(0), nnz_(0),
allocationSize(0), r(0), a(0),
avg(0), overflowsize(-1.0)
{
allocate(_n, _m, _nnz,true,false);
}
//! matrix with unknown number of nonzeroes
BCRSMatrix (size_type _n, size_type _m, BuildMode bm)
: build_mode(bm), ready(notAllocated), n(0), m(0), nnz_(0),
allocationSize(0), r(0), a(0),
avg(0), overflowsize(-1.0)
{
allocate(_n, _m,0,true,false);
}
//! \brief construct matrix with a known average number of entries per row
/**
* Constructs a matrix in implicit buildmode.
*
* @param _n number of rows of the matrix
* @param _m number of columns of the matrix
* @param _avg expected average number of entries per row
* @param _overflowsize fraction of _n*_avg which is expected to be
* needed for elements that exceed _avg entries per row.
*
*/
BCRSMatrix (size_type _n, size_type _m, size_type _avg, double _overflowsize, BuildMode bm)
: build_mode(bm), ready(notAllocated), n(0), m(0), nnz_(0),
allocationSize(0), r(0), a(0),
avg(_avg), overflowsize(_overflowsize)
{
if (bm != implicit)
DUNE_THROW(BCRSMatrixError,"Only call this constructor when using the implicit build mode");
// Prevent user from setting a negative overflowsize:
// 1) It doesn't make sense
// 2) We use a negative overflow value to indicate that the parameters
// have not been set yet
if (_overflowsize < 0.0)
DUNE_THROW(BCRSMatrixError,"You cannot set a negative overflow fraction");
implicit_allocate(_n,_m);
}
/**
* @brief copy constructor
*
* Does a deep copy as expected.
*/
BCRSMatrix (const BCRSMatrix& Mat)
: build_mode(Mat.build_mode), ready(notAllocated), n(0), m(0), nnz_(0),
allocationSize(0), r(0), a(0),
avg(Mat.avg), overflowsize(Mat.overflowsize)
{
if (!(Mat.ready == notAllocated || Mat.ready == built))
DUNE_THROW(InvalidStateException,"BCRSMatrix can only be copy-constructed when source matrix is completely empty (size not set) or fully built)");
// deep copy in global array
size_type _nnz = Mat.nnz_;
// in case of row-wise allocation
if (_nnz<=0)
{
_nnz = 0;
for (size_type i=0; i<Mat.n; i++)
_nnz += Mat.r[i].getsize();
}
j_ = Mat.j_; // enable column index sharing, release array in case of row-wise allocation
allocate(Mat.n, Mat.m, _nnz, true, true);
// build window structure
copyWindowStructure(Mat);
}
//! destructor
~BCRSMatrix ()
{
deallocate();
}
/**
* @brief Sets the build mode of the matrix
* @param bm The build mode to use.
*/
void setBuildMode(BuildMode bm)
{
if (ready == notAllocated)
{
build_mode = bm;
return;
}
if (ready == building && (build_mode == unknown || build_mode == random || build_mode == row_wise) && (bm == row_wise || bm == random))
build_mode = bm;
else
DUNE_THROW(InvalidStateException, "Matrix structure cannot be changed at this stage anymore (ready == "<<ready<<").");
}
/**
* @brief Set the size of the matrix.
*
* Sets the number of rows and columns of the matrix and allocates
* the memory needed for the storage of the matrix entries.
*
* @warning After calling this methods on an already allocated (and probably
* setup matrix) results in all the structure and data being deleted. I.~e.
* one has to setup the matrix again.
*
* @param rows The number of rows the matrix should contain.
* @param columns the number of columns the matrix should contain.
* @param nnz The number of nonzero entries the matrix should hold (if omitted
* defaults to 0). Must be omitted in implicit mode.
*/
void setSize(size_type rows, size_type columns, size_type nnz=0)
{
// deallocate already setup memory
deallocate();
if (build_mode == implicit)
{
if (nnz>0)
DUNE_THROW(Dune::BCRSMatrixError,"number of non-zeroes may not be set in implicit mode, use setImplicitBuildModeParameters() instead");
// implicit allocates differently
implicit_allocate(rows,columns);
}
else
{
// allocate matrix memory
allocate(rows, columns, nnz, true, false);
}
}
/** @brief Set parameters needed for creation in implicit build mode.
*
* Use this method before setSize() to define storage behaviour of a matrix
* in implicit build mode
* @param _avg expected average number of entries per row
* @param _overflowsize fraction of _n*_avg which is expected to be
* needed for elements that exceed _avg entries per row.
*/
void setImplicitBuildModeParameters(size_type _avg, double _overflow)
{
// Prevent user from setting a negative overflowsize:
// 1) It doesn't make sense
// 2) We use a negative overflow value to indicate that the parameters
// have not been set yet
if (_overflow < 0.0)
DUNE_THROW(BCRSMatrixError,"You cannot set a negative overflow fraction");
// make sure the parameters aren't changed after memory has been allocated
if (ready != notAllocated)
DUNE_THROW(InvalidStateException,"You cannot modify build mode parameters at this stage anymore");
avg = _avg;
overflowsize = _overflow;
}
/**
* @brief assignment
*
* Frees and reallocates space.
* Both sparsity pattern and values are set from Mat.
*/
BCRSMatrix& operator= (const BCRSMatrix& Mat)
{
// return immediately when self-assignment
if (&Mat==this) return *this;
if (!((ready == notAllocated || ready == built) && (Mat.ready == notAllocated || Mat.ready == built)))
DUNE_THROW(InvalidStateException,"BCRSMatrix can only be copied when both target and source are empty or fully built)");
// make it simple: ALWAYS throw away memory for a and j_
// and deallocate rows only if n != Mat.n
deallocate(n!=Mat.n);
// reallocate the rows if required
if (n>0 && n!=Mat.n) {
// free rows
for(row_type *riter=r+(n-1), *rend=r-1; riter!=rend; --riter)
rowAllocator_.destroy(riter);
rowAllocator_.deallocate(r,n);
}
nnz_ = Mat.nnz_;
if (nnz_ <= 0)
{
for (size_type i=0; i<Mat.n; i++)
nnz_ += Mat.r[i].getsize();
}
// allocate a, share j_
j_ = Mat.j_;
allocate(Mat.n, Mat.m, nnz_, n!=Mat.n, true);
// build window structure
copyWindowStructure(Mat);
return *this;
}
//! Assignment from a scalar
BCRSMatrix& operator= (const field_type& k)
{
if (!(ready == notAllocated || ready == built))
DUNE_THROW(InvalidStateException,"Scalar assignment only works on fully built BCRSMatrix)");
for (size_type i=0; i<n; i++) r[i] = k;
return *this;
}
//===== row-wise creation interface
//! %Iterator class for sequential creation of blocks
class CreateIterator
{
public:
//! constructor
CreateIterator (BCRSMatrix& _Mat, size_type _i)
: Mat(_Mat), i(_i), nnz(0), current_row(nullptr, Mat.j_.get(), 0)
{
if (Mat.build_mode == unknown && Mat.ready == building)
{
Mat.build_mode = row_wise;
}
if (i==0 && Mat.ready != building)
DUNE_THROW(BCRSMatrixError,"creation only allowed for uninitialized matrix");
if(Mat.build_mode!=row_wise)
DUNE_THROW(BCRSMatrixError,"creation only allowed if row wise allocation was requested in the constructor");
}
//! prefix increment
CreateIterator& operator++()
{
// this should only be called if matrix is in creation
if (Mat.ready != building)
DUNE_THROW(BCRSMatrixError,"matrix already built up");
// row i is defined through the pattern
// get memory for the row and initialize the j_ array
// this depends on the allocation mode
// compute size of the row
size_type s = pattern.size();
if(s>0) {
// update number of nonzeroes including this row
nnz += s;
// alloc memory / set window
if (Mat.nnz_ > 0)
{
// memory is allocated in one long array
// check if that memory is sufficient
if (nnz > Mat.nnz_)
DUNE_THROW(BCRSMatrixError,"allocated nnz too small");
// set row i
Mat.r[i].set(s,nullptr,current_row.getindexptr());
current_row.setindexptr(current_row.getindexptr()+s);
}else{
// memory is allocated individually per row
// allocate and set row i
B* b = Mat.allocator_.allocate(s);
// use placement new to call constructor that allocates
// additional memory.
new (b) B[s];
size_type* j = Mat.sizeAllocator_.allocate(s);
Mat.r[i].set(s,b,j);
}
}else
// setup empty row
Mat.r[i].set(0,0,0);
// initialize the j array for row i from pattern
size_type k=0;
size_type *j = Mat.r[i].getindexptr();
for (typename PatternType::const_iterator it=pattern.begin(); it!=pattern.end(); ++it)
j[k++] = *it;
// now go to next row
i++;
pattern.clear();
// check if this was last row
if (i==Mat.n)
{
Mat.ready = built;
if(Mat.nnz_ > 0)
{
// Set nnz to the exact number of nonzero blocks inserted
// as some methods rely on it
Mat.nnz_ = nnz;
// allocate data array
Mat.allocateData();
Mat.setDataPointers();
}
}
// done
return *this;
}
//! inequality
bool operator!= (const CreateIterator& it) const
{
return (i!=it.i) || (&Mat!=&it.Mat);
}
//! equality
bool operator== (const CreateIterator& it) const
{
return (i==it.i) && (&Mat==&it.Mat);
}
//! dereferencing
size_type index () const
{
return i;
}
//! put column index in row
void insert (size_type j)
{
pattern.insert(j);
}
//! return true if column index is in row
bool contains (size_type j)
{
if (pattern.find(j)!=pattern.end())
return true;
else
return false;
}
/**
* @brief Get the current row size.
* @return The number of indices already
* inserted for the current row.
*/
size_type size() const
{
return pattern.size();
}
private:
BCRSMatrix& Mat; // the matrix we are defining
size_type i; // current row to be defined
size_type nnz; // count total number of nonzeros
typedef std::set<size_type,std::less<size_type> > PatternType;
PatternType pattern; // used to compile entries in a row
row_type current_row; // row pointing to the current row to setup
};
//! allow CreateIterator to access internal data
friend class CreateIterator;
//! get initial create iterator
CreateIterator createbegin ()
{
return CreateIterator(*this,0);
}
//! get create iterator pointing to one after the last block
CreateIterator createend ()
{
return CreateIterator(*this,n);
}
//===== random creation interface
//! set number of indices in row i to s
void setrowsize (size_type i, size_type s)
{
if (build_mode!=random)
DUNE_THROW(BCRSMatrixError,"requires random build mode");
if (ready != building)
DUNE_THROW(BCRSMatrixError,"matrix row sizes already built up");
r[i].setsize(s);
}
//! get current number of indices in row i
size_type getrowsize (size_type i) const
{
#ifdef DUNE_ISTL_WITH_CHECKING
if (r==0) DUNE_THROW(BCRSMatrixError,"row not initialized yet");
if (i>=n) DUNE_THROW(BCRSMatrixError,"index out of range");
#endif
return r[i].getsize();
}
//! increment size of row i by s (1 by default)
void incrementrowsize (size_type i, size_type s = 1)
{
if (build_mode!=random)
DUNE_THROW(BCRSMatrixError,"requires random build mode");
if (ready != building)
DUNE_THROW(BCRSMatrixError,"matrix row sizes already built up");
r[i].setsize(r[i].getsize()+s);
}
//! indicate that size of all rows is defined
void endrowsizes ()
{
if (build_mode!=random)
DUNE_THROW(BCRSMatrixError,"requires random build mode");
if (ready != building)
DUNE_THROW(BCRSMatrixError,"matrix row sizes already built up");
// compute total size, check positivity
size_type total=0;
for (size_type i=0; i<n; i++)
{
total += r[i].getsize();
}
if(nnz_ == 0)
// allocate/check memory
allocate(n,m,total,false,false);
else if(nnz_ < total)
DUNE_THROW(BCRSMatrixError,"Specified number of nonzeros ("<<nnz_<<") not "
<<"sufficient for calculated nonzeros ("<<total<<"! ");
// set the window pointers correctly
setColumnPointers(begin());
// initialize j_ array with m (an invalid column index)
// this indicates an unused entry
for (size_type k=0; k<nnz_; k++)
j_.get()[k] = m;
ready = rowSizesBuilt;
}
//! \brief add index (row,col) to the matrix
/*!
This method can only be used when building the BCRSMatrix
in random mode.
addindex adds a new column entry to the row. If this column
entry already exists, nothing is done.
Don't call addindex after the setup phase is finished
(after endindices is called).
*/
void addindex (size_type row, size_type col)
{
if (build_mode!=random)
DUNE_THROW(BCRSMatrixError,"requires random build mode");
if (ready==built)
DUNE_THROW(BCRSMatrixError,"matrix already built up");
if (ready==building)
DUNE_THROW(BCRSMatrixError,"matrix row sizes not built up yet");
if (ready==notAllocated)
DUNE_THROW(BCRSMatrixError,"matrix size not set and no memory allocated yet");
if (col >= m)
DUNE_THROW(BCRSMatrixError,"column index exceeds matrix size");
// get row range
size_type* const first = r[row].getindexptr();
size_type* const last = first + r[row].getsize();
// find correct insertion position for new column index
size_type* pos = std::lower_bound(first,last,col);
// check if index is already in row
if (pos!=last && *pos == col) return;
// find end of already inserted column indices
size_type* end = std::lower_bound(pos,last,m);
if (end==last)
DUNE_THROW(BCRSMatrixError,"row is too small");
// insert new column index at correct position
std::copy_backward(pos,end,end+1);
*pos = col;
}
//! Set all column indices for row from the given iterator range.
/**
* The iterator range has to be of the same length as the previously set row size.
* The entries in the iterator range do not have to be in any particular order, but
* must not contain duplicate values.
*
* Calling this method overwrites any previously set column indices!
*/
template<typename It>
void setIndices(size_type row, It begin, It end)
{
size_type row_size = r[row].size();
size_type* col_begin = r[row].getindexptr();
size_type* col_end;
// consistency check between allocated row size and number of passed column indices
if ((col_end = std::copy(begin,end,r[row].getindexptr())) != col_begin + row_size)
DUNE_THROW(BCRSMatrixError,"Given size of row " << row
<< " (" << row_size
<< ") does not match number of passed entries (" << (col_end - col_begin) << ")");
std::sort(col_begin,col_end);
}
//! indicate that all indices are defined, check consistency
void endindices ()
{
if (build_mode!=random)
DUNE_THROW(BCRSMatrixError,"requires random build mode");
if (ready==built)
DUNE_THROW(BCRSMatrixError,"matrix already built up");
if (ready==building)
DUNE_THROW(BCRSMatrixError,"row sizes are not built up yet");
if (ready==notAllocated)
DUNE_THROW(BCRSMatrixError,"matrix size not set and no memory allocated yet");
// check if there are undefined indices
RowIterator endi=end();
for (RowIterator i=begin(); i!=endi; ++i)
{
ColIterator endj = (*i).end();
for (ColIterator j=(*i).begin(); j!=endj; ++j) {
if (j.index() >= m) {
dwarn << "WARNING: size of row "<< i.index()<<" is "<<j.offset()<<". But was specified as being "<< (*i).end().offset()
<<". This means you are wasting valuable space and creating additional cache misses!"<<std::endl;
r[i.index()].setsize(j.offset());
break;
}
}
}
allocateData();
setDataPointers();
// if not, set matrix to built
ready = built;
}
//===== implicit creation interface
//! Returns reference to entry (row,col) of the matrix.
/**
* This method can only be used when the matrix is in implicit
* building mode.
*
* A reference to entry (row, col) of the matrix is returned.
* If entry (row, col) is accessed for the first time, it is created
* on the fly.
*
* This method can only be used while building the matrix,
* after compression operator[] gives a much better performance.
*/
B& entry(size_type row, size_type col)
{
#ifdef DUNE_ISTL_WITH_CHECKING
if (build_mode!=implicit)
DUNE_THROW(BCRSMatrixError,"requires implicit build mode");
if (ready==built)
DUNE_THROW(BCRSMatrixError,"matrix already built up, use operator[] for entry access now");
if (ready==notAllocated)
DUNE_THROW(BCRSMatrixError,"matrix size not set and no memory allocated yet");
if (ready!=building)
DUNE_THROW(InvalidStateException,"You may only use entry() during the 'building' stage");
if (row >= n)
DUNE_THROW(BCRSMatrixError,"row index exceeds matrix size");
if (col >= m)
DUNE_THROW(BCRSMatrixError,"column index exceeds matrix size");
#endif
size_type* begin = r[row].getindexptr();
size_type* end = begin + r[row].getsize();
size_type* pos = std::find(begin, end, col);
//treat the case that there was a match in the array
if (pos != end)
if (*pos == col)
{
std::ptrdiff_t offset = pos - r[row].getindexptr();
B* aptr = r[row].getptr() + offset;
return *aptr;
}
//determine whether overflow has to be taken into account or not
if (r[row].getsize() == avg)
return overflow[std::make_pair(row,col)];
else
{
//modify index array
*end = col;
//do simulatenous operations on data array a
std::ptrdiff_t offset = end - r[row].getindexptr();
B* apos = r[row].getptr() + offset;
//increase rowsize
r[row].setsize(r[row].getsize()+1);
//return reference to the newly created entry
return *apos;
}
}
//! Finishes the buildstage in implicit mode.
/**
* Performs compression of index and data arrays with linear
* complexity in the number of nonzeroes.
*
* After calling this method, the matrix is in the built state
* and no more entries can be added.
*
* \returns An object with some statistics about the compression for
* future optimization.
*/
CompressionStatistics compress()
{
if (build_mode!=implicit)
DUNE_THROW(BCRSMatrixError,"requires implicit build mode");
if (ready==built)
DUNE_THROW(BCRSMatrixError,"matrix already built up, no more need for compression");
if (ready==notAllocated)
DUNE_THROW(BCRSMatrixError,"matrix size not set and no memory allocated yet");
if (ready!=building)
DUNE_THROW(InvalidStateException,"You may only call compress() at the end of the 'building' stage");
//calculate statistics
CompressionStatistics stats;
stats.overflow_total = overflow.size();
stats.maximum = 0;
//get insertion iterators pointing to one before start (for later use of ++it)
size_type* jiit = j_.get();
B* aiit = a;
//get iterator to the smallest overflow element
typename OverflowType::iterator oit = overflow.begin();
//store a copy of index pointers on which to perform sortation
std::vector<size_type*> perm;
//iterate over all rows and copy elements into their position in the compressed array
for (size_type i=0; i<n; i++)
{
//get old pointers into a and j and size without overflow changes
size_type* begin = r[i].getindexptr();
//B* apos = r[i].getptr();
size_type size = r[i].getsize();
perm.resize(size);
typename std::vector<size_type*>::iterator it = perm.begin();
for (size_type* iit = begin; iit < begin + size; ++iit, ++it)
*it = iit;
//sort permutation array
std::sort(perm.begin(),perm.end(),PointerCompare<size_type>());
//change row window pointer to their new positions
r[i].setindexptr(jiit);
r[i].setptr(aiit);
for (it = perm.begin(); it != perm.end(); ++it)
{
//check whether there are elements in the overflow area which take precedence
while ((oit!=overflow.end()) && (oit->first < std::make_pair(i,**it)))
{
//check whether there is enough memory to write to
if (jiit > begin)
DUNE_THROW(Dune::ImplicitModeOverflowExhausted,
"Allocated memory for BCRSMatrix exhausted during compress()!"
"Please increase either the average number of entries per row or the overflow fraction."
);
//copy an element from the overflow area to the insertion position in a and j
*jiit = oit->first.second;
++jiit;
*aiit = oit->second;
++aiit;
++oit;
r[i].setsize(r[i].getsize()+1);
}
//check whether there is enough memory to write to
if (jiit > begin)
DUNE_THROW(Dune::ImplicitModeOverflowExhausted,
"Allocated memory for BCRSMatrix exhausted during compress()!"
"Please increase either the average number of entries per row or the overflow fraction."
);
//copy element from array
*jiit = **it;
++jiit;
B* apos = *it - j_.get() + a;
*aiit = *apos;
++aiit;
}
//copy remaining elements from the overflow area
while ((oit!=overflow.end()) && (oit->first.first == i))
{
//check whether there is enough memory to write to
if (jiit > begin)
DUNE_THROW(Dune::ImplicitModeOverflowExhausted,
"Allocated memory for BCRSMatrix exhausted during compress()!"
"Please increase either the average number of entries per row or the overflow fraction."
);
//copy and element from the overflow area to the insertion position in a and j
*jiit = oit->first.second;
++jiit;
*aiit = oit->second;
++aiit;
++oit;
r[i].setsize(r[i].getsize()+1);
}
// update maximum row size
if (r[i].getsize()>stats.maximum)
stats.maximum = r[i].getsize();
}
// overflow area may be cleared
overflow.clear();
//determine average number of entries and memory usage
std::ptrdiff_t diff = (r[n-1].getindexptr() + r[n-1].getsize() - j_.get());
nnz_ = diff;
stats.avg = (double) (nnz_) / (double) n;
stats.mem_ratio = (double) (nnz_) / (double) allocationSize;
//matrix is now built
ready = built;
return stats;
}
//===== vector space arithmetic
//! vector space multiplication with scalar
BCRSMatrix& operator*= (const field_type& k)
{
#ifdef DUNE_ISTL_WITH_CHECKING
if (ready != built)
DUNE_THROW(BCRSMatrixError,"You can only call arithmetic operations on fully built BCRSMatrix instances");
#endif
if (nnz_ > 0)
{
// process 1D array
for (size_type i=0; i<nnz_; i++)
a[i] *= k;
}
else
{
RowIterator endi=end();
for (RowIterator i=begin(); i!=endi; ++i)
{
ColIterator endj = (*i).end();
for (ColIterator j=(*i).begin(); j!=endj; ++j)
(*j) *= k;
}
}
return *this;
}
//! vector space division by scalar
BCRSMatrix& operator/= (const field_type& k)
{
#ifdef DUNE_ISTL_WITH_CHECKING
if (ready != built)
DUNE_THROW(BCRSMatrixError,"You can only call arithmetic operations on fully built BCRSMatrix instances");
#endif
if (nnz_ > 0)
{
// process 1D array
for (size_type i=0; i<nnz_; i++)
a[i] /= k;
}
else
{
RowIterator endi=end();
for (RowIterator i=begin(); i!=endi; ++i)
{
ColIterator endj = (*i).end();
for (ColIterator j=(*i).begin(); j!=endj; ++j)
(*j) /= k;
}
}
return *this;
}
/*! \brief Add the entries of another matrix to this one.
*
* \param b The matrix to add to this one. Its sparsity pattern
* has to be subset of the sparsity pattern of this matrix.
*/
BCRSMatrix& operator+= (const BCRSMatrix& b)
{
#ifdef DUNE_ISTL_WITH_CHECKING
if (ready != built || b.ready != built)
DUNE_THROW(BCRSMatrixError,"You can only call arithmetic operations on fully built BCRSMatrix instances");
if(N()!=b.N() || M() != b.M())
DUNE_THROW(RangeError, "Matrix sizes do not match!");
#endif
RowIterator endi=end();
ConstRowIterator j=b.begin();
for (RowIterator i=begin(); i!=endi; ++i, ++j) {
i->operator+=(*j);
}
return *this;
}
/*! \brief Substract the entries of another matrix to this one.
*
* \param b The matrix to add to this one. Its sparsity pattern
* has to be subset of the sparsity pattern of this matrix.
*/
BCRSMatrix& operator-= (const BCRSMatrix& b)
{
#ifdef DUNE_ISTL_WITH_CHECKING
if (ready != built || b.ready != built)
DUNE_THROW(BCRSMatrixError,"You can only call arithmetic operations on fully built BCRSMatrix instances");
if(N()!=b.N() || M() != b.M())
DUNE_THROW(RangeError, "Matrix sizes do not match!");
#endif
RowIterator endi=end();
ConstRowIterator j=b.begin();
for (RowIterator i=begin(); i!=endi; ++i, ++j) {
i->operator-=(*j);
}
return *this;
}
/*! \brief Add the scaled entries of another matrix to this one.
*
* Matrix axpy operation: *this += alpha * b
*
* \param alpha Scaling factor.
* \param b The matrix to add to this one. Its sparsity pattern has to
* be subset of the sparsity pattern of this matrix.
*/
BCRSMatrix& axpy(field_type alpha, const BCRSMatrix& b)
{
#ifdef DUNE_ISTL_WITH_CHECKING
if (ready != built || b.ready != built)
DUNE_THROW(BCRSMatrixError,"You can only call arithmetic operations on fully built BCRSMatrix instances");
if(N()!=b.N() || M() != b.M())
DUNE_THROW(RangeError, "Matrix sizes do not match!");
#endif
RowIterator endi=end();
ConstRowIterator j=b.begin();
for(RowIterator i=begin(); i!=endi; ++i, ++j)
i->axpy(alpha, *j);
return *this;
}
//===== linear maps
//! y = A x
template<class X, class Y>
void mv (const X& x, Y& y) const
{
#ifdef DUNE_ISTL_WITH_CHECKING
if (ready != built)
DUNE_THROW(BCRSMatrixError,"You can only call arithmetic operations on fully built BCRSMatrix instances");
if (x.N()!=M()) DUNE_THROW(BCRSMatrixError,
"Size mismatch: M: " << N() << "x" << M() << " x: " << x.N());
if (y.N()!=N()) DUNE_THROW(BCRSMatrixError,
"Size mismatch: M: " << N() << "x" << M() << " y: " << y.N());
#endif
ConstRowIterator endi=end();
for (ConstRowIterator i=begin(); i!=endi; ++i)
{
y[i.index()]=0;
ConstColIterator endj = (*i).end();
for (ConstColIterator j=(*i).begin(); j!=endj; ++j)
(*j).umv(x[j.index()],y[i.index()]);
}
}
//! y += A x
template<class X, class Y>
void umv (const X& x, Y& y) const
{
#ifdef DUNE_ISTL_WITH_CHECKING
if (ready != built)
DUNE_THROW(BCRSMatrixError,"You can only call arithmetic operations on fully built BCRSMatrix instances");
if (x.N()!=M()) DUNE_THROW(BCRSMatrixError,"index out of range");
if (y.N()!=N()) DUNE_THROW(BCRSMatrixError,"index out of range");
#endif
ConstRowIterator endi=end();
for (ConstRowIterator i=begin(); i!=endi; ++i)
{
ConstColIterator endj = (*i).end();
for (ConstColIterator j=(*i).begin(); j!=endj; ++j)
(*j).umv(x[j.index()],y[i.index()]);
}
}
//! y -= A x
template<class X, class Y>
void mmv (const X& x, Y& y) const
{
#ifdef DUNE_ISTL_WITH_CHECKING
if (ready != built)
DUNE_THROW(BCRSMatrixError,"You can only call arithmetic operations on fully built BCRSMatrix instances");
if (x.N()!=M()) DUNE_THROW(BCRSMatrixError,"index out of range");
if (y.N()!=N()) DUNE_THROW(BCRSMatrixError,"index out of range");
#endif
ConstRowIterator endi=end();
for (ConstRowIterator i=begin(); i!=endi; ++i)
{
ConstColIterator endj = (*i).end();
for (ConstColIterator j=(*i).begin(); j!=endj; ++j)
(*j).mmv(x[j.index()],y[i.index()]);
}
}
//! y += alpha A x
template<typename F, class X, class Y>
void usmv (F&& alpha, const X& x, Y& y) const
{
#ifdef DUNE_ISTL_WITH_CHECKING
if (ready != built)
DUNE_THROW(BCRSMatrixError,"You can only call arithmetic operations on fully built BCRSMatrix instances");
if (x.N()!=M()) DUNE_THROW(BCRSMatrixError,"index out of range");
if (y.N()!=N()) DUNE_THROW(BCRSMatrixError,"index out of range");
#endif
ConstRowIterator endi=end();
for (ConstRowIterator i=begin(); i!=endi; ++i)
{
ConstColIterator endj = (*i).end();
for (ConstColIterator j=(*i).begin(); j!=endj; ++j)
(*j).usmv(alpha,x[j.index()],y[i.index()]);
}
}
//! y = A^T x
template<class X, class Y>
void mtv (const X& x, Y& y) const
{
#ifdef DUNE_ISTL_WITH_CHECKING
if (ready != built)
DUNE_THROW(BCRSMatrixError,"You can only call arithmetic operations on fully built BCRSMatrix instances");
if (x.N()!=N()) DUNE_THROW(BCRSMatrixError,"index out of range");
if (y.N()!=M()) DUNE_THROW(BCRSMatrixError,"index out of range");
#endif
for(size_type i=0; i<y.N(); ++i)
y[i]=0;
umtv(x,y);
}
//! y += A^T x
template<class X, class Y>
void umtv (const X& x, Y& y) const
{
#ifdef DUNE_ISTL_WITH_CHECKING
if (ready != built)
DUNE_THROW(BCRSMatrixError,"You can only call arithmetic operations on fully built BCRSMatrix instances");
if (x.N()!=N()) DUNE_THROW(BCRSMatrixError,"index out of range");
if (y.N()!=M()) DUNE_THROW(BCRSMatrixError,"index out of range");
#endif
ConstRowIterator endi=end();
for (ConstRowIterator i=begin(); i!=endi; ++i)
{
ConstColIterator endj = (*i).end();
for (ConstColIterator j=(*i).begin(); j!=endj; ++j)
(*j).umtv(x[i.index()],y[j.index()]);
}
}
//! y -= A^T x
template<class X, class Y>
void mmtv (const X& x, Y& y) const
{
#ifdef DUNE_ISTL_WITH_CHECKING
if (x.N()!=N()) DUNE_THROW(BCRSMatrixError,"index out of range");
if (y.N()!=M()) DUNE_THROW(BCRSMatrixError,"index out of range");
#endif
ConstRowIterator endi=end();
for (ConstRowIterator i=begin(); i!=endi; ++i)
{
ConstColIterator endj = (*i).end();
for (ConstColIterator j=(*i).begin(); j!=endj; ++j)
(*j).mmtv(x[i.index()],y[j.index()]);
}
}
//! y += alpha A^T x
template<class X, class Y>
void usmtv (const field_type& alpha, const X& x, Y& y) const
{
#ifdef DUNE_ISTL_WITH_CHECKING
if (ready != built)
DUNE_THROW(BCRSMatrixError,"You can only call arithmetic operations on fully built BCRSMatrix instances");
if (x.N()!=N()) DUNE_THROW(BCRSMatrixError,"index out of range");
if (y.N()!=M()) DUNE_THROW(BCRSMatrixError,"index out of range");
#endif
ConstRowIterator endi=end();
for (ConstRowIterator i=begin(); i!=endi; ++i)
{
ConstColIterator endj = (*i).end();
for (ConstColIterator j=(*i).begin(); j!=endj; ++j)
(*j).usmtv(alpha,x[i.index()],y[j.index()]);
}
}
//! y += A^H x
template<class X, class Y>
void umhv (const X& x, Y& y) const
{
#ifdef DUNE_ISTL_WITH_CHECKING
if (ready != built)
DUNE_THROW(BCRSMatrixError,"You can only call arithmetic operations on fully built BCRSMatrix instances");
if (x.N()!=N()) DUNE_THROW(BCRSMatrixError,"index out of range");
if (y.N()!=M()) DUNE_THROW(BCRSMatrixError,"index out of range");
#endif
ConstRowIterator endi=end();
for (ConstRowIterator i=begin(); i!=endi; ++i)
{
ConstColIterator endj = (*i).end();
for (ConstColIterator j=(*i).begin(); j!=endj; ++j)
(*j).umhv(x[i.index()],y[j.index()]);
}
}
//! y -= A^H x
template<class X, class Y>
void mmhv (const X& x, Y& y) const
{
#ifdef DUNE_ISTL_WITH_CHECKING
if (ready != built)
DUNE_THROW(BCRSMatrixError,"You can only call arithmetic operations on fully built BCRSMatrix instances");
if (x.N()!=N()) DUNE_THROW(BCRSMatrixError,"index out of range");
if (y.N()!=M()) DUNE_THROW(BCRSMatrixError,"index out of range");
#endif
ConstRowIterator endi=end();
for (ConstRowIterator i=begin(); i!=endi; ++i)
{
ConstColIterator endj = (*i).end();
for (ConstColIterator j=(*i).begin(); j!=endj; ++j)
(*j).mmhv(x[i.index()],y[j.index()]);
}
}
//! y += alpha A^H x
template<class X, class Y>
void usmhv (const field_type& alpha, const X& x, Y& y) const
{
#ifdef DUNE_ISTL_WITH_CHECKING
if (ready != built)
DUNE_THROW(BCRSMatrixError,"You can only call arithmetic operations on fully built BCRSMatrix instances");
if (x.N()!=N()) DUNE_THROW(BCRSMatrixError,"index out of range");
if (y.N()!=M()) DUNE_THROW(BCRSMatrixError,"index out of range");
#endif
ConstRowIterator endi=end();
for (ConstRowIterator i=begin(); i!=endi; ++i)
{
ConstColIterator endj = (*i).end();
for (ConstColIterator j=(*i).begin(); j!=endj; ++j)
(*j).usmhv(alpha,x[i.index()],y[j.index()]);
}
}
//===== norms
//! square of frobenius norm, need for block recursion
typename FieldTraits<field_type>::real_type frobenius_norm2 () const
{
#ifdef DUNE_ISTL_WITH_CHECKING
if (ready != built)
DUNE_THROW(BCRSMatrixError,"You can only call arithmetic operations on fully built BCRSMatrix instances");
#endif
double sum=0;
ConstRowIterator endi=end();
for (ConstRowIterator i=begin(); i!=endi; ++i)
{
ConstColIterator endj = (*i).end();
for (ConstColIterator j=(*i).begin(); j!=endj; ++j)
sum += (*j).frobenius_norm2();
}
return sum;
}
//! frobenius norm: sqrt(sum over squared values of entries)
typename FieldTraits<field_type>::real_type frobenius_norm () const
{
return sqrt(frobenius_norm2());
}
//! infinity norm (row sum norm, how to generalize for blocks?)
typename FieldTraits<field_type>::real_type infinity_norm () const
{
if (ready != built)
DUNE_THROW(BCRSMatrixError,"You can only call arithmetic operations on fully built BCRSMatrix instances");
double max=0;
ConstRowIterator endi=end();
for (ConstRowIterator i=begin(); i!=endi; ++i)
{
double sum=0;
ConstColIterator endj = (*i).end();
for (ConstColIterator j=(*i).begin(); j!=endj; ++j)
sum += (*j).infinity_norm();
max = std::max(max,sum);
}
return max;
}
//! simplified infinity norm (uses Manhattan norm for complex values)
typename FieldTraits<field_type>::real_type infinity_norm_real () const
{
#ifdef DUNE_ISTL_WITH_CHECKING
if (ready != built)
DUNE_THROW(BCRSMatrixError,"You can only call arithmetic operations on fully built BCRSMatrix instances");
#endif
double max=0;
ConstRowIterator endi=end();
for (ConstRowIterator i=begin(); i!=endi; ++i)
{
double sum=0;
ConstColIterator endj = (*i).end();
for (ConstColIterator j=(*i).begin(); j!=endj; ++j)
sum += (*j).infinity_norm_real();
max = std::max(max,sum);
}
return max;
}
//===== sizes
//! number of rows (counted in blocks)
size_type N () const
{
return n;
}
//! number of columns (counted in blocks)
size_type M () const
{
return m;
}
//! number of blocks that are stored (the number of blocks that possibly are nonzero)
size_type nonzeroes () const
{
return nnz_;
}
//! The current build stage of the matrix.
BuildStage buildStage() const
{
return ready;
}
//! The currently selected build mode of the matrix.
BuildMode buildMode() const
{
return build_mode;
}
//===== query
//! return true if (i,j) is in pattern
bool exists (size_type i, size_type j) const
{
#ifdef DUNE_ISTL_WITH_CHECKING
if (i<0 || i>=n) DUNE_THROW(BCRSMatrixError,"row index out of range");
if (j<0 || j>=m) DUNE_THROW(BCRSMatrixError,"column index out of range");
#endif
if (r[i].size() && r[i].find(j)!=r[i].end())
return true;
else
return false;
}
protected:
// state information
BuildMode build_mode; // row wise or whole matrix
BuildStage ready; // indicate the stage the matrix building is in
// The allocator used for memory management
typename A::template rebind<B>::other allocator_;
typename A::template rebind<row_type>::other rowAllocator_;
typename A::template rebind<size_type>::other sizeAllocator_;
// size of the matrix
size_type n; // number of rows
size_type m; // number of columns
size_type nnz_; // number of nonzeroes contained in the matrix
size_type allocationSize; //allocated size of a and j arrays, except in implicit mode: nnz_==allocationsSize
// zero means that memory is allocated separately for each row.
// the rows are dynamically allocated
row_type* r; // [n] the individual rows having pointers into a,j arrays
// dynamically allocated memory
B* a; // [allocationSize] non-zero entries of the matrix in row-wise ordering
// If a single array of column indices is used, it can be shared
// between different matrices with the same sparsity pattern
std::shared_ptr<size_type> j_; // [allocationSize] column indices of entries
// additional data is needed in implicit buildmode
size_type avg;
double overflowsize;
typedef std::map<std::pair<size_type,size_type>, B> OverflowType;
OverflowType overflow;
void setWindowPointers(ConstRowIterator row)
{
row_type current_row(a,j_.get(),0); // Pointers to current row data
for (size_type i=0; i<n; i++, ++row) {
// set row i
size_type s = row->getsize();
if (s>0) {
// setup pointers and size
r[i].set(s,current_row.getptr(), current_row.getindexptr());
// update pointer for next row
current_row.setptr(current_row.getptr()+s);
current_row.setindexptr(current_row.getindexptr()+s);
} else{
// empty row
r[i].set(0,0,0);
}
}
}
//! Copy row sizes from iterator range starting at row and set column index pointers for all rows.
/**
* This method does not modify the data pointers, as those are set only
* after building the pattern (to allow for a delayed allocation).
*/
void setColumnPointers(ConstRowIterator row)
{
size_type* jptr = j_.get();
for (size_type i=0; i<n; ++i, ++row) {
// set row i
size_type s = row->getsize();
if (s>0) {
// setup pointers and size
r[i].setsize(s);
r[i].setindexptr(jptr);
} else{
// empty row
r[i].set(0,0,0);
}
// advance position in global array
jptr += s;
}
}
//! Set data pointers for all rows.
/**
* This method assumes that column pointers and row sizes have been correctly set up
* by a prior call to setColumnPointers().
*/
void setDataPointers()
{
B* aptr = a;
for (size_type i=0; i<n; ++i) {
// set row i
if (r[i].getsize() > 0) {
// setup pointers and size
r[i].setptr(aptr);
} else{
// empty row
r[i].set(0,0,0);
}
// advance position in global array
aptr += r[i].getsize();
}
}
//! \brief Copy the window structure from another matrix
void copyWindowStructure(const BCRSMatrix& Mat)
{
setWindowPointers(Mat.begin());
// copy data
for (size_type i=0; i<n; i++) r[i] = Mat.r[i];
// finish off
build_mode = row_wise; // dummy
ready = built;
}
/**
* @brief deallocate memory of the matrix.
* @param deallocateRows Whether we have to deallocate the row pointers, too.
* If false they will not be touched. (Defaults to true).
*/
void deallocate(bool deallocateRows=true)
{
if (notAllocated)
return;
if (allocationSize>0)
{
// a,j_ have been allocated as one long vector
j_.reset();
if (a)
{
for(B *aiter=a+(allocationSize-1), *aend=a-1; aiter!=aend; --aiter)
allocator_.destroy(aiter);
allocator_.deallocate(a,allocationSize);
a = nullptr;
}
}
else if (r)
{
// check if memory for rows have been allocated individually
for (size_type i=0; i<n; i++)
if (r[i].getsize()>0)
{
for (B *col=r[i].getptr()+(r[i].getsize()-1),
*colend = r[i].getptr()-1; col!=colend; --col) {
allocator_.destroy(col);
}
sizeAllocator_.deallocate(r[i].getindexptr(),1);
allocator_.deallocate(r[i].getptr(),1);
// clear out row data in case we don't want to deallocate the rows
// otherwise we might run into a double free problem here later
r[i].set(0,nullptr,nullptr);
}
}
// deallocate the rows
if (n>0 && deallocateRows && r) {
for(row_type *riter=r+(n-1), *rend=r-1; riter!=rend; --riter)
rowAllocator_.destroy(riter);
rowAllocator_.deallocate(r,n);
r = nullptr;
}
// Mark matrix as not built at all.
ready=notAllocated;
}
/** \brief Class used by shared_ptr to deallocate memory using the proper allocator */
class Deallocator
{
typename A::template rebind<size_type>::other& sizeAllocator_;
public:
Deallocator(typename A::template rebind<size_type>::other& sizeAllocator)
: sizeAllocator_(sizeAllocator)
{}
void operator()(size_type* p) { sizeAllocator_.deallocate(p,1); }
};
/**
* @brief Allocate memory for the matrix structure
*
* Sets the number of rows and columns of the matrix and allocates
* the memory needed for the storage of the matrix entries.
*
* @warning After calling this methods on an already allocated (and probably
* setup matrix) results in all the structure and data being lost. Please
* call deallocate() before calling allocate in this case.
*
* @param row The number of rows the matrix should contain.
* @param columns the number of columns the matrix should contain.
* @param allocationSize_ The number of nonzero entries the matrix should hold (if omitted
* defaults to 0).
* @param allocateRow Whether we have to allocate the row pointers, too. (Defaults to
* true)
*/
void allocate(size_type rows, size_type columns, size_type allocationSize_, bool allocateRows, bool allocate_data)
{
// Store size
n = rows;
m = columns;
nnz_ = allocationSize_;
allocationSize = allocationSize_;
// allocate rows
if(allocateRows) {
if (n>0) {
if (r)
DUNE_THROW(InvalidStateException,"Rows have already been allocated, cannot allocate a second time");
r = rowAllocator_.allocate(rows);
}else{
r = 0;
}
}
// allocate a and j_ array
if (allocate_data)
allocateData();
if (allocationSize>0) {
// allocate column indices only if not yet present (enable sharing)
if (!j_.get())
j_.reset(sizeAllocator_.allocate(allocationSize),Deallocator(sizeAllocator_));
}else{
j_.reset();
for(row_type* ri=r; ri!=r+rows; ++ri)
rowAllocator_.construct(ri, row_type());
}
// Mark the matrix as not built.
ready = building;
}
void allocateData()
{
if (a)
DUNE_THROW(InvalidStateException,"Cannot allocate data array (already allocated)");
if (allocationSize>0) {
a = allocator_.allocate(allocationSize);
// use placement new to call constructor that allocates
// additional memory.
new (a) B[allocationSize];
} else {
a = nullptr;
}
}
/** @brief organizes allocation implicit mode
* calculates correct array size to be allocated and sets the
* the window pointers to their correct positions for insertion.
* internally uses allocate() for the real allocation.
*/
void implicit_allocate(size_type _n, size_type _m)
{
if (build_mode != implicit)
DUNE_THROW(InvalidStateException,"implicit_allocate() may only be called in implicit build mode");
if (ready != notAllocated)
DUNE_THROW(InvalidStateException,"memory has already been allocated");
// check to make sure the user has actually set the parameters
if (overflowsize < 0)
DUNE_THROW(InvalidStateException,"You have to set the implicit build mode parameters before starting to build the matrix");
//calculate size of overflow region, add buffer for row sort!
size_type osize = (size_type) (_n*avg)*overflowsize + 4*avg;
allocationSize = _n*avg + osize;
allocate(_n, _m, allocationSize,true,true);
//set row pointers correctly
size_type* jptr = j_.get() + osize;
B* aptr = a + osize;
for (size_type i=0; i<n; i++)
{
r[i].set(0,aptr,jptr);
jptr = jptr + avg;
aptr = aptr + avg;
}
ready = building;
}
};
/** @} end documentation */
} // end namespace
#endif
|