/usr/include/deal.II/lac/trilinos_sparse_matrix.h is in libdeal.ii-dev 8.1.0-6ubuntu1.
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 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 | // ---------------------------------------------------------------------
// $Id: trilinos_sparse_matrix.h 31684 2013-11-16 12:46:15Z bangerth $
//
// Copyright (C) 2008 - 2013 by the deal.II authors
//
// This file is part of the deal.II library.
//
// The deal.II library is free software; you can use it, redistribute
// it, and/or modify it under the terms of the GNU Lesser General
// Public License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
// The full text of the license can be found in the file LICENSE at
// the top level of the deal.II distribution.
//
// ---------------------------------------------------------------------
#ifndef __deal2__trilinos_sparse_matrix_h
#define __deal2__trilinos_sparse_matrix_h
#include <deal.II/base/config.h>
#ifdef DEAL_II_WITH_TRILINOS
# include <deal.II/base/std_cxx1x/shared_ptr.h>
# include <deal.II/base/subscriptor.h>
# include <deal.II/base/index_set.h>
# include <deal.II/lac/full_matrix.h>
# include <deal.II/lac/exceptions.h>
# include <deal.II/lac/trilinos_vector.h>
# include <deal.II/lac/vector_view.h>
# include <vector>
# include <cmath>
# include <memory>
# define TrilinosScalar double
# include <Epetra_FECrsMatrix.h>
# include <Epetra_Map.h>
# include <Epetra_CrsGraph.h>
# include <Epetra_MultiVector.h>
# ifdef DEAL_II_WITH_MPI
# include <Epetra_MpiComm.h>
# include "mpi.h"
# else
# include "Epetra_SerialComm.h"
# endif
DEAL_II_NAMESPACE_OPEN
// forward declarations
template <typename MatrixType> class BlockMatrixBase;
template <typename number> class SparseMatrix;
class SparsityPattern;
namespace TrilinosWrappers
{
// forward declarations
class SparseMatrix;
class SparsityPattern;
/**
* Iterators for Trilinos matrices
*/
namespace SparseMatrixIterators
{
// forward declaration
template <bool Constness> class Iterator;
/**
* Exception
*/
DeclException0 (ExcBeyondEndOfMatrix);
/**
* Exception
*/
DeclException3 (ExcAccessToNonlocalRow,
std::size_t, std::size_t, std::size_t,
<< "You tried to access row " << arg1
<< " of a distributed sparsity pattern, "
<< " but only rows " << arg2 << " through " << arg3
<< " are stored locally and can be accessed.");
/**
* Handling of indices for both
* constant and non constant
* Accessor objects
*
* For a regular
* dealii::SparseMatrix, we would
* use an accessor for the sparsity
* pattern. For Trilinos matrices,
* this does not seem so simple,
* therefore, we write a little
* base class here.
*
* @author Guido Kanschat
* @date 2012
*/
class AccessorBase
{
public:
/**
* Declare the type for container size.
*/
typedef dealii::types::global_dof_index size_type;
/**
* Constructor.
*/
AccessorBase (SparseMatrix *matrix,
const size_type row,
const size_type index);
/**
* Row number of the element
* represented by this object.
*/
size_type row() const;
/**
* Index in row of the element
* represented by this object.
*/
size_type index() const;
/**
* Column number of the element
* represented by this object.
*/
size_type column() const;
protected:
/**
* Pointer to the matrix
* object. This object should
* be handled as a const
* pointer or non-const by the
* appropriate derived
* classes. In order to be able
* to implement both, it is not
* const here, so handle with
* care!
*/
mutable SparseMatrix *matrix;
/**
* Current row number.
*/
size_type a_row;
/**
* Current index in row.
*/
size_type a_index;
/**
* Discard the old row caches
* (they may still be used by
* other accessors) and
* generate new ones for the
* row pointed to presently by
* this accessor.
*/
void visit_present_row ();
/**
* Cache where we store the
* column indices of the
* present row. This is
* necessary, since Trilinos
* makes access to the elements
* of its matrices rather hard,
* and it is much more
* efficient to copy all column
* entries of a row once when
* we enter it than repeatedly
* asking Trilinos for
* individual ones. This also
* makes some sense since it is
* likely that we will access
* them sequentially anyway.
*
* In order to make copying of
* iterators/accessor of
* acceptable performance, we
* keep a shared pointer to
* these entries so that more
* than one accessor can access
* this data if necessary.
*/
std_cxx1x::shared_ptr<std::vector<size_type> > colnum_cache;
/**
* Cache for the values
* of this row.
*/
std_cxx1x::shared_ptr<std::vector<TrilinosScalar> > value_cache;
};
/**
* General template for sparse matrix accessors. The first template
* argument denotes the underlying numeric type, the second the
* constness of the matrix.
*
* The general template is not implemented, only the specializations
* for the two possible values of the second template
* argument. Therefore, the interface listed here only serves as a
* template provided since doxygen does not link the specializations.
*/
template <bool Constess>
class Accessor : public AccessorBase
{
/**
* Value of this matrix entry.
*/
TrilinosScalar value() const;
/**
* Value of this matrix entry.
*/
TrilinosScalar &value();
};
/**
* The specialization for a const Accessor.
*/
template<>
class Accessor<true> : public AccessorBase
{
public:
/**
* Typedef for the type (including
* constness) of the matrix to be
* used here.
*/
typedef const SparseMatrix MatrixType;
/**
* Constructor. Since we use
* accessors only for read
* access, a const matrix
* pointer is sufficient.
*/
Accessor (MatrixType *matrix,
const size_type row,
const size_type index);
/**
* Copy constructor to get from a
* const or non-const accessor to a const
* accessor.
*/
template <bool Other>
Accessor (const Accessor<Other> &a);
/**
* Value of this matrix entry.
*/
TrilinosScalar value() const;
private:
/**
* Make iterator class a
* friend.
*/
template <bool> friend class Iterator;
};
/**
* The specialization for a mutable Accessor.
*/
template<>
class Accessor<false> : public AccessorBase
{
class Reference
{
public:
/**
* Constructor.
*/
Reference (const Accessor<false> &accessor);
/**
* Conversion operator to the
* data type of the matrix.
*/
operator TrilinosScalar () const;
/**
* Set the element of the matrix
* we presently point to to @p n.
*/
const Reference &operator = (const TrilinosScalar n) const;
/**
* Add @p n to the element of the
* matrix we presently point to.
*/
const Reference &operator += (const TrilinosScalar n) const;
/**
* Subtract @p n from the element
* of the matrix we presently
* point to.
*/
const Reference &operator -= (const TrilinosScalar n) const;
/**
* Multiply the element of the
* matrix we presently point to
* by @p n.
*/
const Reference &operator *= (const TrilinosScalar n) const;
/**
* Divide the element of the
* matrix we presently point to
* by @p n.
*/
const Reference &operator /= (const TrilinosScalar n) const;
private:
/**
* Pointer to the accessor that
* denotes which element we
* presently point to.
*/
Accessor &accessor;
};
public:
/**
* Typedef for the type (including
* constness) of the matrix to be
* used here.
*/
typedef SparseMatrix MatrixType;
/**
* Constructor. Since we use
* accessors only for read
* access, a const matrix
* pointer is sufficient.
*/
Accessor (MatrixType *matrix,
const size_type row,
const size_type index);
/**
* Value of this matrix entry.
*/
Reference value() const;
private:
/**
* Make iterator class a
* friend.
*/
template <bool> friend class Iterator;
/**
* Make Reference object a
* friend.
*/
friend class Reference;
};
/**
* STL conforming iterator. This class acts as an iterator walking
* over the elements of Trilinos matrices. The implementation of this
* class is similar to the one for PETSc matrices.
*
* Note that Trilinos stores the elements within each row in ascending
* order. This is opposed to the deal.II sparse matrix style where the
* diagonal element (if it exists) is stored before all other values, and
* the PETSc sparse matrices, where one can't guarantee a certain order of
* the elements.
*
* @ingroup TrilinosWrappers
* @author Martin Kronbichler, Wolfgang Bangerth, 2008
*/
template <bool Constness>
class Iterator
{
public:
/**
* Declare type for container size.
*/
typedef dealii::types::global_dof_index size_type;
/**
* Typedef for the matrix type
* (including constness) we are to
* operate on.
*/
typedef typename Accessor<Constness>::MatrixType MatrixType;
/**
* Constructor. Create an
* iterator into the matrix @p
* matrix for the given row and
* the index within it.
*/
Iterator (MatrixType *matrix,
const size_type row,
const size_type index);
/**
* Copy constructor with
* optional change of constness.
*/
template <bool Other>
Iterator(const Iterator<Other> &other);
/**
* Prefix increment.
*/
Iterator<Constness> &operator++ ();
/**
* Postfix increment.
*/
Iterator<Constness> operator++ (int);
/**
* Dereferencing operator.
*/
const Accessor<Constness> &operator* () const;
/**
* Dereferencing operator.
*/
const Accessor<Constness> *operator-> () const;
/**
* Comparison. True, if both
* iterators point to the same
* matrix position.
*/
bool operator == (const Iterator<Constness> &) const;
/**
* Inverse of <tt>==</tt>.
*/
bool operator != (const Iterator<Constness> &) const;
/**
* Comparison operator. Result
* is true if either the first
* row number is smaller or if
* the row numbers are equal
* and the first index is
* smaller.
*/
bool operator < (const Iterator<Constness> &) const;
/**
* Comparison operator. The opposite of the previous operator
*/
bool operator > (const Iterator<Constness> &) const;
/**
* Exception
*/
DeclException2 (ExcInvalidIndexWithinRow,
size_type, size_type,
<< "Attempt to access element " << arg2
<< " of row " << arg1
<< " which doesn't have that many elements.");
private:
/**
* Store an object of the
* accessor class.
*/
Accessor<Constness> accessor;
template <bool Other> friend class Iterator;
};
}
/**
* This class implements a wrapper to use the Trilinos distributed
* sparse matrix class Epetra_FECrsMatrix. This is precisely the kind of
* matrix we deal with all the time - we most likely get it from some
* assembly process, where also entries not locally owned might need to
* be written and hence need to be forwarded to the owner process. This
* class is designed to be used in a distributed memory architecture
* with an MPI compiler on the bottom, but works equally well also for
* serial processes. The only requirement for this class to work is that
* Trilinos has been installed with the same compiler as is used for
* generating deal.II.
*
* The interface of this class is modeled after the existing
* SparseMatrix class in deal.II. It has almost the same member
* functions, and is often exchangable. However, since Trilinos only
* supports a single scalar type (double), it is not templated, and only
* works with doubles.
*
* Note that Trilinos only guarantees that operations do what you expect
* if the functions @p GlobalAssemble has been called after matrix
* assembly. Therefore, you need to call SparseMatrix::compress()
* before you actually use the matrix. This also calls @p FillComplete
* that compresses the storage format for sparse matrices by discarding
* unused elements. Trilinos allows to continue with assembling the
* matrix after calls to these functions, though.
*
* @ingroup TrilinosWrappers
* @ingroup Matrix1
* @author Martin Kronbichler, Wolfgang Bangerth, 2008, 2009
*/
class SparseMatrix : public Subscriptor
{
public:
/**
* Declare the type for container size.
*/
typedef dealii::types::global_dof_index size_type;
/**
* A structure that describes
* some of the traits of this
* class in terms of its run-time
* behavior. Some other classes
* (such as the block matrix
* classes) that take one or
* other of the matrix classes as
* its template parameters can
* tune their behavior based on
* the variables in this class.
*/
struct Traits
{
/**
* It is safe to elide additions
* of zeros to individual
* elements of this matrix.
*/
static const bool zero_addition_can_be_elided = true;
};
/**
* Declare a typedef for the
* iterator class.
*/
typedef SparseMatrixIterators::Iterator<false> iterator;
/**
* Declare a typedef for the
* const iterator class.
*/
typedef SparseMatrixIterators::Iterator<true> const_iterator;
/**
* Declare a typedef in analogy
* to all the other container
* classes.
*/
typedef TrilinosScalar value_type;
/**
* @name Constructors and initialization.
*/
//@{
/**
* Default constructor. Generates
* an empty (zero-size) matrix.
*/
SparseMatrix ();
/**
* Generate a matrix that is completely
* stored locally, having #m rows and
* #n columns.
*
* The number of columns entries per
* row is specified as the maximum
* number of entries argument.
*/
SparseMatrix (const size_type m,
const size_type n,
const unsigned int n_max_entries_per_row);
/**
* Generate a matrix that is completely
* stored locally, having #m rows and
* #n columns.
*
* The vector
* <tt>n_entries_per_row</tt>
* specifies the number of entries in
* each row.
*/
SparseMatrix (const size_type m,
const size_type n,
const std::vector<unsigned int> &n_entries_per_row);
/**
* Generate a matrix from a Trilinos
* sparsity pattern object.
*/
SparseMatrix (const SparsityPattern &InputSparsityPattern);
/**
* Copy constructor. Sets the
* calling matrix to be the same
* as the input matrix, i.e.,
* using the same sparsity
* pattern and entries.
*/
SparseMatrix (const SparseMatrix &InputMatrix);
/**
* Destructor. Made virtual so
* that one can use pointers to
* this class.
*/
virtual ~SparseMatrix ();
/**
* This function initializes the
* Trilinos matrix with a deal.II
* sparsity pattern, i.e. it makes
* the Trilinos Epetra matrix know
* the position of nonzero entries
* according to the sparsity
* pattern. This function is meant
* for use in serial programs, where
* there is no need to specify how
* the matrix is going to be
* distributed among different
* processors. This function works in
* %parallel, too, but it is
* recommended to manually specify
* the %parallel partioning of the
* matrix using an Epetra_Map. When
* run in %parallel, it is currently
* necessary that each processor
* holds the sparsity_pattern
* structure because each processor
* sets its rows.
*
* This is a collective operation
* that needs to be called on all
* processors in order to avoid a
* dead lock.
*/
template<typename SparsityType>
void reinit (const SparsityType &sparsity_pattern);
/**
* This function reinitializes the
* Trilinos sparse matrix from a
* (possibly distributed) Trilinos
* sparsity pattern.
*
* This is a collective operation
* that needs to be called on all
* processors in order to avoid a
* dead lock.
*/
void reinit (const SparsityPattern &sparsity_pattern);
/**
* This function copies the content
* in <tt>sparse_matrix</tt> to the
* calling matrix.
*
* This is a collective operation
* that needs to be called on all
* processors in order to avoid a
* dead lock.
*/
void reinit (const SparseMatrix &sparse_matrix);
/**
* This function initializes the
* Trilinos matrix using the deal.II
* sparse matrix and the entries
* stored therein. It uses a
* threshold to copy only elements
* with modulus larger than the
* threshold (so zeros in the deal.II
* matrix can be filtered away).
*
* The optional parameter
* <tt>copy_values</tt> decides
* whether only the sparsity
* structure of the input matrix
* should be used or the matrix
* entries should be copied, too.
*
* This is a collective operation
* that needs to be called on all
* processors in order to avoid a
* deadlock.
*
* @note If a different sparsity pattern is given in the last argument
* (i.e., one that differs from the one used in the sparse matrix given
* in the first argument), then the resulting Trilinos matrix will have
* the sparsity pattern so given. This of course also means that all
* entries in the given matrix that are not part of this separate
* sparsity pattern will in fact be dropped.
*/
template <typename number>
void reinit (const ::dealii::SparseMatrix<number> &dealii_sparse_matrix,
const double drop_tolerance=1e-13,
const bool copy_values=true,
const ::dealii::SparsityPattern *use_this_sparsity=0);
/**
* This reinit function takes as
* input a Trilinos Epetra_CrsMatrix
* and copies its sparsity
* pattern. If so requested, even the
* content (values) will be copied.
*/
void reinit (const Epetra_CrsMatrix &input_matrix,
const bool copy_values = true);
//@}
/**
* @name Constructors and initialization using an Epetra_Map description
*/
//@{
/**
* Constructor using an Epetra_Map to
* describe the %parallel
* partitioning. The parameter @p
* n_max_entries_per_row sets the
* number of nonzero entries in each
* row that will be allocated. Note
* that this number does not need to
* be exact, and it is even allowed
* that the actual matrix structure
* has more nonzero entries than
* specified in the
* constructor. However it is still
* advantageous to provide good
* estimates here since this will
* considerably increase the
* performance of the matrix
* setup. However, there is no effect
* in the performance of
* matrix-vector products, since
* Trilinos reorganizes the matrix
* memory prior to use (in the
* compress() step).
*/
SparseMatrix (const Epetra_Map ¶llel_partitioning,
const size_type n_max_entries_per_row = 0);
/**
* Same as before, but now set a
* value of nonzeros for each matrix
* row. Since we know the number of
* elements in the matrix exactly in
* this case, we can already allocate
* the right amount of memory, which
* makes the creation process
* including the insertion of nonzero
* elements by the respective
* SparseMatrix::reinit call
* considerably faster.
*/
SparseMatrix (const Epetra_Map ¶llel_partitioning,
const std::vector<unsigned int> &n_entries_per_row);
/**
* This constructor is similar to the
* one above, but it now takes two
* different Epetra maps for rows and
* columns. This interface is meant
* to be used for generating
* rectangular matrices, where one
* map describes the %parallel
* partitioning of the dofs
* associated with the matrix rows
* and the other one the partitioning
* of dofs in the matrix
* columns. Note that there is no
* real parallelism along the columns
* – the processor that owns a
* certain row always owns all the
* column elements, no matter how far
* they might be spread out. The
* second Epetra_Map is only used to
* specify the number of columns and
* for internal arrangements when
* doing matrix-vector products with
* vectors based on that column map.
*
* The integer input @p
* n_max_entries_per_row defines the
* number of columns entries per row
* that will be allocated.
*/
SparseMatrix (const Epetra_Map &row_parallel_partitioning,
const Epetra_Map &col_parallel_partitioning,
const size_type n_max_entries_per_row = 0);
/**
* This constructor is similar to the
* one above, but it now takes two
* different Epetra maps for rows and
* columns. This interface is meant
* to be used for generating
* rectangular matrices, where one
* map specifies the %parallel
* distribution of degrees of freedom
* associated with matrix rows and
* the second one specifies the
* %parallel distribution the dofs
* associated with columns in the
* matrix. The second map also
* provides information for the
* internal arrangement in matrix
* vector products (i.e., the
* distribution of vector this matrix
* is to be multiplied with), but is
* not used for the distribution of
* the columns – rather, all
* column elements of a row are
* stored on the same processor in
* any case. The vector
* <tt>n_entries_per_row</tt>
* specifies the number of entries in
* each row of the newly generated
* matrix.
*/
SparseMatrix (const Epetra_Map &row_parallel_partitioning,
const Epetra_Map &col_parallel_partitioning,
const std::vector<unsigned int> &n_entries_per_row);
/**
* This function is initializes the
* Trilinos Epetra matrix according to
* the specified sparsity_pattern, and
* also reassigns the matrix rows to
* different processes according to a
* user-supplied Epetra map. In
* programs following the style of the
* tutorial programs, this function
* (and the respective call for a
* rectangular matrix) are the natural
* way to initialize the matrix size,
* its distribution among the MPI
* processes (if run in %parallel) as
* well as the locatoin of non-zero
* elements. Trilinos stores the
* sparsity pattern internally, so it
* won't be needed any more after this
* call, in contrast to the deal.II own
* object. The optional argument @p
* exchange_data can be used for
* reinitialization with a sparsity
* pattern that is not fully
* constructed. This feature is only
* implemented for input sparsity
* patterns of type
* CompressedSimpleSparsityPattern. If
* the flag is not set, each processor
* just sets the elements in the
* sparsity pattern that belong to its
* rows.
*
* This is a collective operation
* that needs to be called on all
* processors in order to avoid a
* dead lock.
*/
template<typename SparsityType>
void reinit (const Epetra_Map ¶llel_partitioning,
const SparsityType &sparsity_pattern,
const bool exchange_data = false);
/**
* This function is similar to the
* other initialization function
* above, but now also reassigns the
* matrix rows and columns according
* to two user-supplied Epetra maps.
* To be used for rectangular
* matrices. The optional argument @p
* exchange_data can be used for
* reinitialization with a sparsity
* pattern that is not fully
* constructed. This feature is only
* implemented for input sparsity
* patterns of type
* CompressedSimpleSparsityPattern.
*
* This is a collective operation
* that needs to be called on all
* processors in order to avoid a
* dead lock.
*/
template<typename SparsityType>
void reinit (const Epetra_Map &row_parallel_partitioning,
const Epetra_Map &col_parallel_partitioning,
const SparsityType &sparsity_pattern,
const bool exchange_data = false);
/**
* This function initializes the
* Trilinos matrix using the deal.II
* sparse matrix and the entries
* stored therein. It uses a
* threshold to copy only elements
* with modulus larger than the
* threshold (so zeros in the deal.II
* matrix can be filtered away). In
* contrast to the other reinit
* function with deal.II sparse
* matrix argument, this function
* takes a %parallel partitioning
* specified by the user instead of
* internally generating it.
*
* The optional parameter
* <tt>copy_values</tt> decides
* whether only the sparsity
* structure of the input matrix
* should be used or the matrix
* entries should be copied, too.
*
* This is a collective operation
* that needs to be called on all
* processors in order to avoid a
* dead lock.
*/
template <typename number>
void reinit (const Epetra_Map ¶llel_partitioning,
const ::dealii::SparseMatrix<number> &dealii_sparse_matrix,
const double drop_tolerance=1e-13,
const bool copy_values=true,
const ::dealii::SparsityPattern *use_this_sparsity=0);
/**
* This function is similar to the
* other initialization function with
* deal.II sparse matrix input above,
* but now takes Epetra maps for both
* the rows and the columns of the
* matrix. Chosen for rectangular
* matrices.
*
* The optional parameter
* <tt>copy_values</tt> decides
* whether only the sparsity
* structure of the input matrix
* should be used or the matrix
* entries should be copied, too.
*
* This is a collective operation
* that needs to be called on all
* processors in order to avoid a
* dead lock.
*/
template <typename number>
void reinit (const Epetra_Map &row_parallel_partitioning,
const Epetra_Map &col_parallel_partitioning,
const ::dealii::SparseMatrix<number> &dealii_sparse_matrix,
const double drop_tolerance=1e-13,
const bool copy_values=true,
const ::dealii::SparsityPattern *use_this_sparsity=0);
//@}
/**
* @name Constructors and initialization using an IndexSet description
*/
//@{
/**
* Constructor using an IndexSet and
* an MPI communicator to describe
* the %parallel partitioning. The
* parameter @p n_max_entries_per_row
* sets the number of nonzero entries
* in each row that will be
* allocated. Note that this number
* does not need to be exact, and it
* is even allowed that the actual
* matrix structure has more nonzero
* entries than specified in the
* constructor. However it is still
* advantageous to provide good
* estimates here since this will
* considerably increase the
* performance of the matrix
* setup. However, there is no effect
* in the performance of
* matrix-vector products, since
* Trilinos reorganizes the matrix
* memory prior to use (in the
* compress() step).
*/
SparseMatrix (const IndexSet ¶llel_partitioning,
const MPI_Comm &communicator = MPI_COMM_WORLD,
const unsigned int n_max_entries_per_row = 0);
/**
* Same as before, but now set the
* number of nonzeros in each matrix
* row separately. Since we know the
* number of elements in the matrix
* exactly in this case, we can
* already allocate the right amount
* of memory, which makes the
* creation process including the
* insertion of nonzero elements by
* the respective
* SparseMatrix::reinit call
* considerably faster.
*/
SparseMatrix (const IndexSet ¶llel_partitioning,
const MPI_Comm &communicator,
const std::vector<unsigned int> &n_entries_per_row);
/**
* This constructor is similar to the
* one above, but it now takes two
* different IndexSet partitions for
* row and columns. This interface is
* meant to be used for generating
* rectangular matrices, where the
* first index set describes the
* %parallel partitioning of the
* degrees of freedom associated with
* the matrix rows and the second one
* the partitioning of the matrix
* columns. The second index set
* specifies the partitioning of the
* vectors this matrix is to be
* multiplied with, not the
* distribution of the elements that
* actually appear in the matrix.
*
* The parameter @p
* n_max_entries_per_row defines how
* much memory will be allocated for
* each row. This number does not
* need to be accurate, as the
* structure is reorganized in the
* compress() call.
*/
SparseMatrix (const IndexSet &row_parallel_partitioning,
const IndexSet &col_parallel_partitioning,
const MPI_Comm &communicator = MPI_COMM_WORLD,
const size_type n_max_entries_per_row = 0);
/**
* This constructor is similar to the
* one above, but it now takes two
* different Epetra maps for rows and
* columns. This interface is meant
* to be used for generating
* rectangular matrices, where one
* map specifies the %parallel
* distribution of degrees of freedom
* associated with matrix rows and
* the second one specifies the
* %parallel distribution the dofs
* associated with columns in the
* matrix. The second map also
* provides information for the
* internal arrangement in matrix
* vector products (i.e., the
* distribution of vector this matrix
* is to be multiplied with), but is
* not used for the distribution of
* the columns – rather, all
* column elements of a row are
* stored on the same processor in
* any case. The vector
* <tt>n_entries_per_row</tt>
* specifies the number of entries in
* each row of the newly generated
* matrix.
*/
SparseMatrix (const IndexSet &row_parallel_partitioning,
const IndexSet &col_parallel_partitioning,
const MPI_Comm &communicator,
const std::vector<unsigned int> &n_entries_per_row);
/**
* This function is initializes the
* Trilinos Epetra matrix according
* to the specified sparsity_pattern,
* and also reassigns the matrix rows
* to different processes according
* to a user-supplied index set and
* %parallel communicator. In
* programs following the style of
* the tutorial programs, this
* function (and the respective call
* for a rectangular matrix) are the
* natural way to initialize the
* matrix size, its distribution
* among the MPI processes (if run in
* %parallel) as well as the locatoin
* of non-zero elements. Trilinos
* stores the sparsity pattern
* internally, so it won't be needed
* any more after this call, in
* contrast to the deal.II own
* object. The optional argument @p
* exchange_data can be used for
* reinitialization with a sparsity
* pattern that is not fully
* constructed. This feature is only
* implemented for input sparsity
* patterns of type
* CompressedSimpleSparsityPattern. If
* the flag is not set, each
* processor just sets the elements
* in the sparsity pattern that
* belong to its rows.
*
* This is a collective operation
* that needs to be called on all
* processors in order to avoid a
* dead lock.
*/
template<typename SparsityType>
void reinit (const IndexSet ¶llel_partitioning,
const SparsityType &sparsity_pattern,
const MPI_Comm &communicator = MPI_COMM_WORLD,
const bool exchange_data = false);
/**
* This function is similar to the
* other initialization function
* above, but now also reassigns the
* matrix rows and columns according
* to two user-supplied index sets.
* To be used for rectangular
* matrices. The optional argument @p
* exchange_data can be used for
* reinitialization with a sparsity
* pattern that is not fully
* constructed. This feature is only
* implemented for input sparsity
* patterns of type
* CompressedSimpleSparsityPattern.
*
* This is a collective operation
* that needs to be called on all
* processors in order to avoid a
* dead lock.
*/
template<typename SparsityType>
void reinit (const IndexSet &row_parallel_partitioning,
const IndexSet &col_parallel_partitioning,
const SparsityType &sparsity_pattern,
const MPI_Comm &communicator = MPI_COMM_WORLD,
const bool exchange_data = false);
/**
* This function initializes the
* Trilinos matrix using the deal.II
* sparse matrix and the entries
* stored therein. It uses a
* threshold to copy only elements
* with modulus larger than the
* threshold (so zeros in the deal.II
* matrix can be filtered away). In
* contrast to the other reinit
* function with deal.II sparse
* matrix argument, this function
* takes a %parallel partitioning
* specified by the user instead of
* internally generating it.
*
* The optional parameter
* <tt>copy_values</tt> decides
* whether only the sparsity
* structure of the input matrix
* should be used or the matrix
* entries should be copied, too.
*
* This is a collective operation
* that needs to be called on all
* processors in order to avoid a
* dead lock.
*/
template <typename number>
void reinit (const IndexSet ¶llel_partitioning,
const ::dealii::SparseMatrix<number> &dealii_sparse_matrix,
const MPI_Comm &communicator = MPI_COMM_WORLD,
const double drop_tolerance=1e-13,
const bool copy_values=true,
const ::dealii::SparsityPattern *use_this_sparsity=0);
/**
* This function is similar to the
* other initialization function with
* deal.II sparse matrix input above,
* but now takes index sets for both
* the rows and the columns of the
* matrix. Chosen for rectangular
* matrices.
*
* The optional parameter
* <tt>copy_values</tt> decides
* whether only the sparsity
* structure of the input matrix
* should be used or the matrix
* entries should be copied, too.
*
* This is a collective operation
* that needs to be called on all
* processors in order to avoid a
* dead lock.
*/
template <typename number>
void reinit (const IndexSet &row_parallel_partitioning,
const IndexSet &col_parallel_partitioning,
const ::dealii::SparseMatrix<number> &dealii_sparse_matrix,
const MPI_Comm &communicator = MPI_COMM_WORLD,
const double drop_tolerance=1e-13,
const bool copy_values=true,
const ::dealii::SparsityPattern *use_this_sparsity=0);
//@}
/**
* @name Information on the matrix
*/
//@{
/**
* Return the number of rows in
* this matrix.
*/
size_type m () const;
/**
* Return the number of columns
* in this matrix.
*/
size_type n () const;
/**
* Return the local dimension
* of the matrix, i.e. the
* number of rows stored on the
* present MPI process. For
* sequential matrices, this
* number is the same as m(),
* but for %parallel matrices it
* may be smaller.
*
* To figure out which elements
* exactly are stored locally,
* use local_range().
*/
unsigned int local_size () const;
/**
* Return a pair of indices
* indicating which rows of
* this matrix are stored
* locally. The first number is
* the index of the first row
* stored, the second the index
* of the one past the last one
* that is stored locally. If
* this is a sequential matrix,
* then the result will be the
* pair (0,m()), otherwise it
* will be a pair (i,i+n),
* where
* <tt>n=local_size()</tt>.
*/
std::pair<size_type, size_type>
local_range () const;
/**
* Return whether @p index is
* in the local range or not,
* see also local_range().
*/
bool in_local_range (const size_type index) const;
/**
* Return the number of nonzero
* elements of this matrix.
*/
size_type n_nonzero_elements () const;
/**
* Number of entries in a
* specific row.
*/
unsigned int row_length (const size_type row) const;
/**
* Returns the state of the matrix,
* i.e., whether compress() needs to
* be called after an operation
* requiring data exchange. A call to
* compress() is also needed when the
* method set() has been called (even
* when working in serial).
*/
bool is_compressed () const;
/**
* Determine an estimate for the memory
* consumption (in bytes) of this
* object. Note that only the memory
* reserved on the current processor is
* returned in case this is called in
* an MPI-based program.
*/
size_type memory_consumption () const;
//@}
/**
* @name Modifying entries
*/
//@{
/**
* This operator assigns a scalar to
* a matrix. Since this does usually
* not make much sense (should we set
* all matrix entries to this value?
* Only the nonzero entries of the
* sparsity pattern?), this operation
* is only allowed if the actual
* value to be assigned is zero. This
* operator only exists to allow for
* the obvious notation
* <tt>matrix=0</tt>, which sets all
* elements of the matrix to zero,
* but keeps the sparsity pattern
* previously used.
*/
SparseMatrix &
operator = (const double d);
/**
* Release all memory and return to a
* state just like after having
* called the default constructor.
*
* This is a collective operation
* that needs to be called on all
* processors in order to avoid a
* dead lock.
*/
void clear ();
/**
* This command does two things:
* <ul>
* <li> If the matrix was initialized
* without a sparsity pattern,
* elements have been added manually
* using the set() command. When this
* process is completed, a call to
* compress() reorganizes the
* internal data structures (aparsity
* pattern) so that a fast access to
* data is possible in matrix-vector
* products.
* <li> If the matrix structure has
* already been fixed (either by
* initialization with a sparsity
* pattern or by calling compress()
* during the setup phase), this
* command does the %parallel
* exchange of data. This is
* necessary when we perform assembly
* on more than one (MPI) process,
* because then some non-local row
* data will accumulate on nodes that
* belong to the current's processor
* element, but are actually held by
* another. This command is usually
* called after all elements have
* been traversed.
* </ul>
*
* In both cases, this function
* compresses the data structures and
* allows the resulting matrix to be
* used in all other operations like
* matrix-vector products. This is a
* collective operation, i.e., it
* needs to be run on all processors
* when used in %parallel.
*
* See @ref GlossCompress "Compressing distributed objects"
* for more information.
*/
void compress (::dealii::VectorOperation::values operation);
/**
* @deprecated: use compress() with VectorOperation instead.
*/
void compress () DEAL_II_DEPRECATED;
/**
* Set the element (<i>i,j</i>)
* to @p value.
*
* This function is able to insert new
* elements into the matrix as long as
* compress() has not been called, so
* the sparsity pattern will be
* extended. When compress() is called
* for the first time, then this is no
* longer possible and an insertion of
* elements at positions which have not
* been initialized will throw an
* exception. Note that in case
* elements need to be inserted, it is
* mandatory that elements are inserted
* only once. Otherwise, the elements
* will actually be added in the end
* (since it is not possible to
* efficiently find values to the same
* entry before compress() has been
* called). In the case that an element
* is set more than once, initialize
* the matrix with a sparsity pattern
* first.
*/
void set (const size_type i,
const size_type j,
const TrilinosScalar value);
/**
* Set all elements given in a
* FullMatrix<double> into the sparse
* matrix locations given by
* <tt>indices</tt>. In other words,
* this function writes the elements
* in <tt>full_matrix</tt> into the
* calling matrix, using the
* local-to-global indexing specified
* by <tt>indices</tt> for both the
* rows and the columns of the
* matrix. This function assumes a
* quadratic sparse matrix and a
* quadratic full_matrix, the usual
* situation in FE calculations.
*
* This function is able to insert
* new elements into the matrix as
* long as compress() has not been
* called, so the sparsity pattern
* will be extended. When compress()
* is called for the first time, then
* this is no longer possible and an
* insertion of elements at positions
* which have not been initialized
* will throw an exception.
*
* The optional parameter
* <tt>elide_zero_values</tt> can be
* used to specify whether zero
* values should be inserted anyway
* or they should be filtered
* away. The default value is
* <tt>false</tt>, i.e., even zero
* values are inserted/replaced.
*/
void set (const std::vector<size_type> &indices,
const FullMatrix<TrilinosScalar> &full_matrix,
const bool elide_zero_values = false);
/**
* Same function as before, but now
* including the possibility to use
* rectangular full_matrices and
* different local-to-global indexing
* on rows and columns, respectively.
*/
void set (const std::vector<size_type> &row_indices,
const std::vector<size_type> &col_indices,
const FullMatrix<TrilinosScalar> &full_matrix,
const bool elide_zero_values = false);
/**
* Set several elements in the
* specified row of the matrix with
* column indices as given by
* <tt>col_indices</tt> to the
* respective value.
*
* This function is able to insert
* new elements into the matrix as
* long as compress() has not been
* called, so the sparsity pattern
* will be extended. When compress()
* is called for the first time, then
* this is no longer possible and an
* insertion of elements at positions
* which have not been initialized
* will throw an exception.
*
* The optional parameter
* <tt>elide_zero_values</tt> can be
* used to specify whether zero
* values should be inserted anyway
* or they should be filtered
* away. The default value is
* <tt>false</tt>, i.e., even zero
* values are inserted/replaced.
*/
void set (const size_type row,
const std::vector<size_type> &col_indices,
const std::vector<TrilinosScalar> &values,
const bool elide_zero_values = false);
/**
* Set several elements to values
* given by <tt>values</tt> in a
* given row in columns given by
* col_indices into the sparse
* matrix.
*
* This function is able to insert
* new elements into the matrix as
* long as compress() has not been
* called, so the sparsity pattern
* will be extended. When compress()
* is called for the first time, then
* this is no longer possible and an
* insertion of elements at positions
* which have not been initialized
* will throw an exception.
*
* The optional parameter
* <tt>elide_zero_values</tt> can be
* used to specify whether zero
* values should be inserted anyway
* or they should be filtered
* away. The default value is
* <tt>false</tt>, i.e., even zero
* values are inserted/replaced.
*/
void set (const size_type row,
const size_type n_cols,
const size_type *col_indices,
const TrilinosScalar *values,
const bool elide_zero_values = false);
/**
* Add @p value to the element
* (<i>i,j</i>).
*
* Just as the respective call in
* deal.II SparseMatrix<Number>
* class (but in contrast to the
* situation for PETSc based
* matrices), this function
* throws an exception if an
* entry does not exist in the
* sparsity pattern. Moreover, if
* <tt>value</tt> is not a finite
* number an exception is thrown.
*/
void add (const size_type i,
const size_type j,
const TrilinosScalar value);
/**
* Add all elements given in a
* FullMatrix<double> into sparse
* matrix locations given by
* <tt>indices</tt>. In other words,
* this function adds the elements in
* <tt>full_matrix</tt> to the
* respective entries in calling
* matrix, using the local-to-global
* indexing specified by
* <tt>indices</tt> for both the rows
* and the columns of the
* matrix. This function assumes a
* quadratic sparse matrix and a
* quadratic full_matrix, the usual
* situation in FE calculations.
*
* Just as the respective call in
* deal.II SparseMatrix<Number>
* class (but in contrast to the
* situation for PETSc based
* matrices), this function
* throws an exception if an
* entry does not exist in the
* sparsity pattern.
*
* The optional parameter
* <tt>elide_zero_values</tt> can be
* used to specify whether zero
* values should be added anyway or
* these should be filtered away and
* only non-zero data is added. The
* default value is <tt>true</tt>,
* i.e., zero values won't be added
* into the matrix.
*/
void add (const std::vector<size_type> &indices,
const FullMatrix<TrilinosScalar> &full_matrix,
const bool elide_zero_values = true);
/**
* Same function as before, but now
* including the possibility to use
* rectangular full_matrices and
* different local-to-global indexing
* on rows and columns, respectively.
*/
void add (const std::vector<size_type> &row_indices,
const std::vector<size_type> &col_indices,
const FullMatrix<TrilinosScalar> &full_matrix,
const bool elide_zero_values = true);
/**
* Set several elements in the
* specified row of the matrix with
* column indices as given by
* <tt>col_indices</tt> to the
* respective value.
*
* Just as the respective call in
* deal.II SparseMatrix<Number>
* class (but in contrast to the
* situation for PETSc based
* matrices), this function
* throws an exception if an
* entry does not exist in the
* sparsity pattern.
*
* The optional parameter
* <tt>elide_zero_values</tt> can be
* used to specify whether zero
* values should be added anyway or
* these should be filtered away and
* only non-zero data is added. The
* default value is <tt>true</tt>,
* i.e., zero values won't be added
* into the matrix.
*/
void add (const size_type row,
const std::vector<size_type> &col_indices,
const std::vector<TrilinosScalar> &values,
const bool elide_zero_values = true);
/**
* Add an array of values given by
* <tt>values</tt> in the given
* global matrix row at columns
* specified by col_indices in the
* sparse matrix.
*
* Just as the respective call in
* deal.II SparseMatrix<Number> class
* (but in contrast to the situation
* for PETSc based matrices), this
* function throws an exception if an
* entry does not exist in the
* sparsity pattern.
*
* The optional parameter
* <tt>elide_zero_values</tt> can be
* used to specify whether zero
* values should be added anyway or
* these should be filtered away and
* only non-zero data is added. The
* default value is <tt>true</tt>,
* i.e., zero values won't be added
* into the matrix.
*/
void add (const size_type row,
const size_type n_cols,
const size_type *col_indices,
const TrilinosScalar *values,
const bool elide_zero_values = true,
const bool col_indices_are_sorted = false);
/**
* Multiply the entire matrix
* by a fixed factor.
*/
SparseMatrix &operator *= (const TrilinosScalar factor);
/**
* Divide the entire matrix by
* a fixed factor.
*/
SparseMatrix &operator /= (const TrilinosScalar factor);
/**
* Copy the given (Trilinos) matrix
* (sparsity pattern and entries).
*/
void copy_from (const SparseMatrix &source);
/**
* Add <tt>matrix</tt> scaled by
* <tt>factor</tt> to this matrix,
* i.e. the matrix
* <tt>factor*matrix</tt> is added to
* <tt>this</tt>. If the sparsity
* pattern of the calling matrix does
* not contain all the elements in
* the sparsity pattern of the input
* matrix, this function will throw
* an exception.
*/
void add (const TrilinosScalar factor,
const SparseMatrix &matrix);
/**
* Remove all elements from
* this <tt>row</tt> by setting
* them to zero. The function
* does not modify the number
* of allocated nonzero
* entries, it only sets some
* entries to zero. It may drop
* them from the sparsity
* pattern, though (but retains
* the allocated memory in case
* new entries are again added
* later). Note that this is a
* global operation, so this
* needs to be done on all MPI
* processes.
*
* This operation is used in
* eliminating constraints
* (e.g. due to hanging nodes)
* and makes sure that we can
* write this modification to
* the matrix without having to
* read entries (such as the
* locations of non-zero
* elements) from it —
* without this operation,
* removing constraints on
* %parallel matrices is a
* rather complicated
* procedure.
*
* The second parameter can be
* used to set the diagonal
* entry of this row to a value
* different from zero. The
* default is to set it to
* zero.
*/
void clear_row (const size_type row,
const TrilinosScalar new_diag_value = 0);
/**
* Same as clear_row(), except
* that it works on a number of
* rows at once.
*
* The second parameter can be
* used to set the diagonal
* entries of all cleared rows
* to something different from
* zero. Note that all of these
* diagonal entries get the
* same value -- if you want
* different values for the
* diagonal entries, you have
* to set them by hand.
*/
void clear_rows (const std::vector<size_type> &rows,
const TrilinosScalar new_diag_value = 0);
/**
* Make an in-place transpose
* of a matrix.
*/
void transpose ();
//@}
/**
* @name Entry Access
*/
//@{
/**
* Return the value of the
* entry (<i>i,j</i>). This
* may be an expensive
* operation and you should
* always take care where to
* call this function. As in
* the deal.II sparse matrix
* class, we throw an exception
* if the respective entry
* doesn't exist in the
* sparsity pattern of this
* class, which is requested
* from Trilinos. Moreover, an
* exception will be thrown
* when the requested element
* is not saved on the calling
* process.
*/
TrilinosScalar operator () (const size_type i,
const size_type j) const;
/**
* Return the value of the
* matrix entry
* (<i>i,j</i>). If this entry
* does not exist in the
* sparsity pattern, then zero
* is returned. While this may
* be convenient in some cases,
* note that it is simple to
* write algorithms that are
* slow compared to an optimal
* solution, since the sparsity
* of the matrix is not used.
* On the other hand, if you
* want to be sure the entry
* exists, you should use
* operator() instead.
*
* The lack of error checking
* in this function can also
* yield surprising results if
* you have a parallel
* matrix. In that case, just
* because you get a zero
* result from this function
* does not mean that either
* the entry does not exist in
* the sparsity pattern or that
* it does but has a value of
* zero. Rather, it could also
* be that it simply isn't
* stored on the current
* processor; in that case, it
* may be stored on a different
* processor, and possibly so
* with a nonzero value.
*/
TrilinosScalar el (const size_type i,
const size_type j) const;
/**
* Return the main diagonal
* element in the <i>i</i>th
* row. This function throws an
* error if the matrix is not
* quadratic and it also throws
* an error if <i>(i,i)</i> is not
* element of the local matrix.
* See also the comment in
* trilinos_sparse_matrix.cc.
*/
TrilinosScalar diag_element (const size_type i) const;
//@}
/**
* @name Multiplications
*/
//@{
/**
* Matrix-vector multiplication: let <i>dst = M*src</i> with <i>M</i>
* being this matrix.
*
* Source and destination must not be the same vector.
*
* This function can be called with several different vector objects,
* namely TrilinosWrappers::Vector, TrilinosWrappers::MPI::Vector as well
* as deal.II's own vector classes Vector<double> and
* parallel::distributed::Vector<double>.
*
* Note that both vectors have to be distributed vectors generated using
* the same Map as was used for the matrix in case you work on a
* distributed memory architecture, using the interface in the
* TrilinosWrappers::VectorBase class (or one of the two derived classes
* Vector and MPI::Vector).
*
* In case of a localized Vector, this function will only work when
* running on one processor, since the matrix object is inherently
* distributed. Otherwise, and exception will be thrown.
*/
template<typename VectorType>
void vmult (VectorType &dst,
const VectorType &src) const;
/**
* Matrix-vector multiplication: let <i>dst = M<sup>T</sup>*src</i> with
* <i>M</i> being this matrix. This function does the same as vmult() but
* takes the transposed matrix.
*
* Source and destination must not be the same vector.
*
* This function can be called with several different vector objects,
* namely TrilinosWrappers::Vector, TrilinosWrappers::MPI::Vector as well
* as deal.II's own vector classes Vector<double> and
* parallel::distributed::Vector<double>.
*
* Note that both vectors have to be distributed vectors generated using
* the same Map as was used for the matrix in case you work on a
* distributed memory architecture, using the interface in the
* TrilinosWrappers::VectorBase class (or one of the two derived classes
* Vector and MPI::Vector).
*
* In case of a localized Vector, this function will only work when
* running on one processor, since the matrix object is inherently
* distributed. Otherwise, and exception will be thrown.
*/
template <typename VectorType>
void Tvmult (VectorType &dst,
const VectorType &src) const;
/**
* Adding matrix-vector multiplication. Add <i>M*src</i> on <i>dst</i>
* with <i>M</i> being this matrix.
*
* Source and destination must not be the same vector.
*
* This function can be called with several different vector objects,
* namely TrilinosWrappers::Vector, TrilinosWrappers::MPI::Vector as well
* as deal.II's own vector classes Vector<double> and
* parallel::distributed::Vector<double>.
*
* When using a vector of type TrilinosWrappers::MPI::Vector, both vectors
* have to be distributed vectors generated using the same Map as was used
* for the matrix rows and columns in case you work on a distributed
* memory architecture, using the interface in the
* TrilinosWrappers::VectorBase class.
*
* In case of a localized Vector (i.e., TrilinosWrappers::Vector or
* Vector<double>), this function will only work when running on one
* processor, since the matrix object is inherently
* distributed. Otherwise, and exception will be thrown.
*
*/
template<typename VectorType>
void vmult_add (VectorType &dst,
const VectorType &src) const;
/**
* Adding matrix-vector multiplication. Add <i>M<sup>T</sup>*src</i> to
* <i>dst</i> with <i>M</i> being this matrix. This function does the same
* as vmult_add() but takes the transposed matrix.
*
* Source and destination must not be the same vector.
*
* This function can be called with several different vector objects,
* namely TrilinosWrappers::Vector, TrilinosWrappers::MPI::Vector as well
* as deal.II's own vector classes Vector<double> and
* parallel::distributed::Vector<double>.
*
* When using a vector of type TrilinosWrappers::MPI::Vector, both vectors
* have to be distributed vectors generated using the same Map as was used
* for the matrix rows and columns in case you work on a distributed
* memory architecture, using the interface in the
* TrilinosWrappers::VectorBase class.
*
* In case of a localized Vector (i.e., TrilinosWrappers::Vector or
* Vector<double>), this function will only work when running on one
* processor, since the matrix object is inherently
* distributed. Otherwise, and exception will be thrown.
*/
template <typename VectorType>
void Tvmult_add (VectorType &dst,
const VectorType &src) const;
/**
* Return the square of the norm
* of the vector $v$ with respect
* to the norm induced by this
* matrix, i.e.,
* $\left(v,Mv\right)$. This is
* useful, e.g. in the finite
* element context, where the
* $L_2$ norm of a function
* equals the matrix norm with
* respect to the mass matrix of
* the vector representing the
* nodal values of the finite
* element function.
*
* Obviously, the matrix needs to
* be quadratic for this
* operation.
*
* The implementation of this
* function is not as efficient
* as the one in the @p
* SparseMatrix class used in
* deal.II (i.e. the original
* one, not the Trilinos wrapper
* class) since Trilinos doesn't
* support this operation and
* needs a temporary vector.
*
* Note that both vectors have to
* be distributed vectors
* generated using the same Map
* as was used for the matrix in
* case you work on a distributed
* memory architecture, using the
* interface in the
* TrilinosWrappers::VectorBase
* class (or one of the two
* derived classes Vector and
* MPI::Vector).
*
* In case of a localized Vector,
* this function will only work
* when running on one processor,
* since the matrix object is
* inherently
* distributed. Otherwise, and
* exception will be thrown.
*/
TrilinosScalar matrix_norm_square (const VectorBase &v) const;
/**
* Compute the matrix scalar
* product $\left(u,Mv\right)$.
*
* The implementation of this
* function is not as efficient
* as the one in the @p
* SparseMatrix class used in
* deal.II (i.e. the original
* one, not the Trilinos
* wrapper class) since
* Trilinos doesn't support
* this operation and needs a
* temporary vector.
*
* Note that both vectors have to
* be distributed vectors
* generated using the same Map
* as was used for the matrix in
* case you work on a distributed
* memory architecture, using the
* interface in the
* TrilinosWrappers::VectorBase
* class (or one of the two
* derived classes Vector and
* MPI::Vector).
*
* In case of a localized Vector,
* this function will only work
* when running on one processor,
* since the matrix object is
* inherently
* distributed. Otherwise, and
* exception will be thrown.
*/
TrilinosScalar matrix_scalar_product (const VectorBase &u,
const VectorBase &v) const;
/**
* Compute the residual of an
* equation <i>Mx=b</i>, where
* the residual is defined to
* be <i>r=b-Mx</i>. Write the
* residual into @p dst. The
* <i>l<sub>2</sub></i> norm of
* the residual vector is
* returned.
*
* Source <i>x</i> and
* destination <i>dst</i> must
* not be the same vector.
*
* Note that both vectors have to
* be distributed vectors
* generated using the same Map
* as was used for the matrix in
* case you work on a distributed
* memory architecture, using the
* interface in the
* TrilinosWrappers::VectorBase
* class (or one of the two
* derived classes Vector and
* MPI::Vector).
*
* In case of a localized Vector,
* this function will only work
* when running on one processor,
* since the matrix object is
* inherently
* distributed. Otherwise, and
* exception will be thrown.
*/
TrilinosScalar residual (VectorBase &dst,
const VectorBase &x,
const VectorBase &b) const;
/**
* Perform the matrix-matrix
* multiplication <tt>C = A * B</tt>,
* or, if an optional vector argument
* is given, <tt>C = A * diag(V) *
* B</tt>, where <tt>diag(V)</tt>
* defines a diagonal matrix with the
* vector entries.
*
* This function assumes that the
* calling matrix <tt>A</tt> and
* <tt>B</tt> have compatible
* sizes. The size of <tt>C</tt> will
* be set within this function.
*
* The content as well as the sparsity
* pattern of the matrix C will be
* changed by this function, so make
* sure that the sparsity pattern is
* not used somewhere else in your
* program. This is an expensive
* operation, so think twice before you
* use this function.
*/
void mmult (SparseMatrix &C,
const SparseMatrix &B,
const VectorBase &V = VectorBase()) const;
/**
* Perform the matrix-matrix
* multiplication with the transpose of
* <tt>this</tt>, i.e., <tt>C =
* A<sup>T</sup> * B</tt>, or, if an
* optional vector argument is given,
* <tt>C = A<sup>T</sup> * diag(V) *
* B</tt>, where <tt>diag(V)</tt>
* defines a diagonal matrix with the
* vector entries.
*
* This function assumes that the
* calling matrix <tt>A</tt> and
* <tt>B</tt> have compatible
* sizes. The size of <tt>C</tt> will
* be set within this function.
*
* The content as well as the sparsity
* pattern of the matrix C will be
* changed by this function, so make
* sure that the sparsity pattern is
* not used somewhere else in your
* program. This is an expensive
* operation, so think twice before you
* use this function.
*/
void Tmmult (SparseMatrix &C,
const SparseMatrix &B,
const VectorBase &V = VectorBase()) const;
//@}
/**
* @name Matrix norms
*/
//@{
/**
* Return the
* <i>l</i><sub>1</sub>-norm of
* the matrix, that is
* $|M|_1=
* \max_{\mathrm{all\ columns\ } j}
* \sum_{\mathrm{all\ rows\ } i}
* |M_{ij}|$, (max. sum
* of columns). This is the
* natural matrix norm that is
* compatible to the l1-norm for
* vectors, i.e. $|Mv|_1 \leq
* |M|_1 |v|_1$.
* (cf. Haemmerlin-Hoffmann:
* Numerische Mathematik)
*/
TrilinosScalar l1_norm () const;
/**
* Return the linfty-norm of the
* matrix, that is
* $|M|_\infty=\max_{\mathrm{all\
* rows\ } i}\sum_{\mathrm{all\
* columns\ } j} |M_{ij}|$,
* (max. sum of rows). This is
* the natural matrix norm that
* is compatible to the
* linfty-norm of vectors, i.e.
* $|Mv|_\infty \leq |M|_\infty
* |v|_\infty$.
* (cf. Haemmerlin-Hoffmann:
* Numerische Mathematik)
*/
TrilinosScalar linfty_norm () const;
/**
* Return the frobenius norm of
* the matrix, i.e. the square
* root of the sum of squares
* of all entries in the
* matrix.
*/
TrilinosScalar frobenius_norm () const;
//@}
/**
* @name Access to underlying Trilinos data
*/
//@{
/**
* Return a const reference to the
* underlying Trilinos
* Epetra_CrsMatrix data.
*/
const Epetra_CrsMatrix &trilinos_matrix () const;
/**
* Return a const reference to the
* underlying Trilinos
* Epetra_CrsGraph data that stores
* the sparsity pattern of the
* matrix.
*/
const Epetra_CrsGraph &trilinos_sparsity_pattern () const;
/**
* Return a const reference to the
* underlying Trilinos Epetra_Map
* that sets the partitioning of the
* domain space of this matrix, i.e.,
* the partitioning of the vectors
* this matrix has to be multiplied
* with.
*/
const Epetra_Map &domain_partitioner () const;
/**
* Return a const reference to the
* underlying Trilinos Epetra_Map
* that sets the partitioning of the
* range space of this matrix, i.e.,
* the partitioning of the vectors
* that are result from matrix-vector
* products.
*/
const Epetra_Map &range_partitioner () const;
/**
* Return a const reference to the
* underlying Trilinos Epetra_Map
* that sets the partitioning of the
* matrix rows. Equal to the
* partitioning of the range.
*/
const Epetra_Map &row_partitioner () const;
/**
* Return a const reference to the
* underlying Trilinos Epetra_Map
* that sets the partitioning of the
* matrix columns. This is in general
* not equal to the partitioner
* Epetra_Map for the domain because
* of overlap in the matrix.
*/
const Epetra_Map &col_partitioner () const;
//@}
/**
* @name Iterators
*/
//@{
/**
* STL-like iterator with the
* first entry.
*/
const_iterator begin () const;
/**
* Final iterator.
*/
const_iterator end () const;
/**
* STL-like iterator with the
* first entry of row @p r.
*
* Note that if the given row
* is empty, i.e. does not
* contain any nonzero entries,
* then the iterator returned
* by this function equals
* <tt>end(r)</tt>. Note also
* that the iterator may not be
* dereferencable in that case.
*/
const_iterator begin (const size_type r) const;
/**
* Final iterator of row
* <tt>r</tt>. It points to the
* first element past the end
* of line @p r, or past the
* end of the entire sparsity
* pattern.
*
* Note that the end iterator
* is not necessarily
* dereferencable. This is in
* particular the case if it is
* the end iterator for the
* last row of a matrix.
*/
const_iterator end (const size_type r) const;
/**
* STL-like iterator with the
* first entry.
*/
iterator begin ();
/**
* Final iterator.
*/
iterator end ();
/**
* STL-like iterator with the
* first entry of row @p r.
*
* Note that if the given row
* is empty, i.e. does not
* contain any nonzero entries,
* then the iterator returned
* by this function equals
* <tt>end(r)</tt>. Note also
* that the iterator may not be
* dereferencable in that case.
*/
iterator begin (const size_type r);
/**
* Final iterator of row
* <tt>r</tt>. It points to the
* first element past the end
* of line @p r, or past the
* end of the entire sparsity
* pattern.
*
* Note that the end iterator
* is not necessarily
* dereferencable. This is in
* particular the case if it is
* the end iterator for the
* last row of a matrix.
*/
iterator end (const size_type r);
//@}
/**
* @name Input/Output
*/
//@{
/**
* Abstract Trilinos object
* that helps view in ASCII
* other Trilinos
* objects. Currently this
* function is not
* implemented. TODO: Not
* implemented.
*/
void write_ascii ();
/**
* Print the matrix to the given
* stream, using the format
* <tt>(line,col) value</tt>, i.e. one
* nonzero entry of the matrix per
* line. The optional flag outputs the
* sparsity pattern in Trilinos style,
* where the data is sorted according
* to the processor number when printed
* to the stream, as well as a summary
* of the matrix like the global size.
*/
void print (std::ostream &out,
const bool write_extended_trilinos_info = false) const;
//@}
/** @addtogroup Exceptions
*
*/
//@{
/**
* Exception
*/
DeclException1 (ExcTrilinosError,
int,
<< "An error with error number " << arg1
<< " occurred while calling a Trilinos function");
/**
* Exception
*/
DeclException2 (ExcInvalidIndex,
size_type, size_type,
<< "The entry with index <" << arg1 << ',' << arg2
<< "> does not exist.");
/**
* Exception
*/
DeclException0 (ExcSourceEqualsDestination);
/**
* Exception
*/
DeclException0 (ExcMatrixNotCompressed);
/**
* Exception
*/
DeclException4 (ExcAccessToNonLocalElement,
size_type, size_type, size_type, size_type,
<< "You tried to access element (" << arg1
<< "/" << arg2 << ")"
<< " of a distributed matrix, but only rows "
<< arg3 << " through " << arg4
<< " are stored locally and can be accessed.");
/**
* Exception
*/
DeclException2 (ExcAccessToNonPresentElement,
size_type, size_type,
<< "You tried to access element (" << arg1
<< "/" << arg2 << ")"
<< " of a sparse matrix, but it appears to not"
<< " exist in the Trilinos sparsity pattern.");
//@}
protected:
/**
* For some matrix storage
* formats, in particular for the
* PETSc distributed blockmatrices,
* set and add operations on
* individual elements can not be
* freely mixed. Rather, one has
* to synchronize operations when
* one wants to switch from
* setting elements to adding to
* elements.
* BlockMatrixBase automatically
* synchronizes the access by
* calling this helper function
* for each block.
* This function ensures that the
* matrix is in a state that
* allows adding elements; if it
* previously already was in this
* state, the function does
* nothing.
*/
void prepare_add();
/**
* Same as prepare_add() but
* prepare the matrix for setting
* elements if the representation
* of elements in this class
* requires such an operation.
*/
void prepare_set();
private:
/**
* Pointer to the user-supplied
* Epetra Trilinos mapping of
* the matrix columns that
* assigns parts of the matrix
* to the individual processes.
*/
std_cxx1x::shared_ptr<Epetra_Map> column_space_map;
/**
* A sparse matrix object in
* Trilinos to be used for
* finite element based
* problems which allows for
* assembling into non-local
* elements. The actual type,
* a sparse matrix, is set in
* the constructor.
*/
std_cxx1x::shared_ptr<Epetra_FECrsMatrix> matrix;
/**
* Trilinos doesn't allow to mix
* additions to matrix entries and
* overwriting them (to make
* synchronisation of %parallel
* computations simpler). The way we
* do it is to, for each access
* operation, store whether it is an
* insertion or an addition. If the
* previous one was of different
* type, then we first have to flush
* the Trilinos buffers; otherwise,
* we can simply go on. Luckily,
* Trilinos has an object for this
* which does already all the
* %parallel communications in such a
* case, so we simply use their
* model, which stores whether the
* last operation was an addition or
* an insertion.
*/
Epetra_CombineMode last_action;
/**
* A boolean variable to hold
* information on whether the
* vector is compressed or not.
*/
bool compressed;
/**
* To allow calling protected
* prepare_add() and
* prepare_set().
*/
friend class BlockMatrixBase<SparseMatrix>;
};
// -------------------------- inline and template functions ----------------------
#ifndef DOXYGEN
namespace SparseMatrixIterators
{
inline
AccessorBase::AccessorBase(SparseMatrix *matrix, size_type row, size_type index)
:
matrix(matrix),
a_row(row),
a_index(index)
{
visit_present_row ();
}
inline
AccessorBase::size_type
AccessorBase::row() const
{
Assert (a_row < matrix->m(), ExcBeyondEndOfMatrix());
return a_row;
}
inline
AccessorBase::size_type
AccessorBase::column() const
{
Assert (a_row < matrix->m(), ExcBeyondEndOfMatrix());
return (*colnum_cache)[a_index];
}
inline
AccessorBase::size_type
AccessorBase::index() const
{
Assert (a_row < matrix->m(), ExcBeyondEndOfMatrix());
return a_index;
}
inline
Accessor<true>::Accessor (MatrixType *matrix,
const size_type row,
const size_type index)
:
AccessorBase(const_cast<SparseMatrix *>(matrix), row, index)
{}
template <bool Other>
inline
Accessor<true>::Accessor(const Accessor<Other> &other)
:
AccessorBase(other)
{}
inline
TrilinosScalar
Accessor<true>::value() const
{
Assert (a_row < matrix->m(), ExcBeyondEndOfMatrix());
return (*value_cache)[a_index];
}
inline
Accessor<false>::Reference::Reference (
const Accessor<false> &acc)
:
accessor(const_cast<Accessor<false>&>(acc))
{}
inline
Accessor<false>::Reference::operator TrilinosScalar () const
{
return (*accessor.value_cache)[accessor.a_index];
}
inline
const Accessor<false>::Reference &
Accessor<false>::Reference::operator = (const TrilinosScalar n) const
{
(*accessor.value_cache)[accessor.a_index] = n;
accessor.matrix->set(accessor.row(), accessor.column(),
static_cast<TrilinosScalar>(*this));
return *this;
}
inline
const Accessor<false>::Reference &
Accessor<false>::Reference::operator += (const TrilinosScalar n) const
{
(*accessor.value_cache)[accessor.a_index] += n;
accessor.matrix->set(accessor.row(), accessor.column(),
static_cast<TrilinosScalar>(*this));
return *this;
}
inline
const Accessor<false>::Reference &
Accessor<false>::Reference::operator -= (const TrilinosScalar n) const
{
(*accessor.value_cache)[accessor.a_index] -= n;
accessor.matrix->set(accessor.row(), accessor.column(),
static_cast<TrilinosScalar>(*this));
return *this;
}
inline
const Accessor<false>::Reference &
Accessor<false>::Reference::operator *= (const TrilinosScalar n) const
{
(*accessor.value_cache)[accessor.a_index] *= n;
accessor.matrix->set(accessor.row(), accessor.column(),
static_cast<TrilinosScalar>(*this));
return *this;
}
inline
const Accessor<false>::Reference &
Accessor<false>::Reference::operator /= (const TrilinosScalar n) const
{
(*accessor.value_cache)[accessor.a_index] /= n;
accessor.matrix->set(accessor.row(), accessor.column(),
static_cast<TrilinosScalar>(*this));
return *this;
}
inline
Accessor<false>::Accessor (MatrixType *matrix,
const size_type row,
const size_type index)
:
AccessorBase(matrix, row, index)
{}
inline
Accessor<false>::Reference
Accessor<false>::value() const
{
Assert (a_row < matrix->m(), ExcBeyondEndOfMatrix());
return Reference(*this);
}
template <bool Constness>
inline
Iterator<Constness>::Iterator(MatrixType *matrix,
const size_type row,
const size_type index)
:
accessor(matrix, row, index)
{}
template <bool Constness>
template <bool Other>
inline
Iterator<Constness>::Iterator(const Iterator<Other> &other)
:
accessor(other.accessor)
{}
template <bool Constness>
inline
Iterator<Constness> &
Iterator<Constness>::operator++ ()
{
Assert (accessor.a_row < accessor.matrix->m(), ExcIteratorPastEnd());
++accessor.a_index;
// If at end of line: do one
// step, then cycle until we
// find a row with a nonzero
// number of entries.
if (accessor.a_index >= accessor.colnum_cache->size())
{
accessor.a_index = 0;
++accessor.a_row;
while ((accessor.a_row < accessor.matrix->m())
&&
(accessor.matrix->row_length(accessor.a_row) == 0))
++accessor.a_row;
accessor.visit_present_row();
}
return *this;
}
template <bool Constness>
inline
Iterator<Constness>
Iterator<Constness>::operator++ (int)
{
const Iterator<Constness> old_state = *this;
++(*this);
return old_state;
}
template <bool Constness>
inline
const Accessor<Constness> &
Iterator<Constness>::operator* () const
{
return accessor;
}
template <bool Constness>
inline
const Accessor<Constness> *
Iterator<Constness>::operator-> () const
{
return &accessor;
}
template <bool Constness>
inline
bool
Iterator<Constness>::operator == (const Iterator<Constness> &other) const
{
return (accessor.a_row == other.accessor.a_row &&
accessor.a_index == other.accessor.a_index);
}
template <bool Constness>
inline
bool
Iterator<Constness>::operator != (const Iterator<Constness> &other) const
{
return ! (*this == other);
}
template <bool Constness>
inline
bool
Iterator<Constness>::operator < (const Iterator<Constness> &other) const
{
return (accessor.row() < other.accessor.row() ||
(accessor.row() == other.accessor.row() &&
accessor.index() < other.accessor.index()));
}
template <bool Constness>
inline
bool
Iterator<Constness>::operator > (const Iterator<Constness> &other) const
{
return (other < *this);
}
}
inline
SparseMatrix::const_iterator
SparseMatrix::begin() const
{
return const_iterator(this, 0, 0);
}
inline
SparseMatrix::const_iterator
SparseMatrix::end() const
{
return const_iterator(this, m(), 0);
}
inline
SparseMatrix::const_iterator
SparseMatrix::begin(const size_type r) const
{
Assert (r < m(), ExcIndexRange(r, 0, m()));
if (row_length(r) > 0)
return const_iterator(this, r, 0);
else
return end (r);
}
inline
SparseMatrix::const_iterator
SparseMatrix::end(const size_type r) const
{
Assert (r < m(), ExcIndexRange(r, 0, m()));
// place the iterator on the first entry
// past this line, or at the end of the
// matrix
for (size_type i=r+1; i<m(); ++i)
if (row_length(i) > 0)
return const_iterator(this, i, 0);
// if there is no such line, then take the
// end iterator of the matrix
return end();
}
inline
SparseMatrix::iterator
SparseMatrix::begin()
{
return iterator(this, 0, 0);
}
inline
SparseMatrix::iterator
SparseMatrix::end()
{
return iterator(this, m(), 0);
}
inline
SparseMatrix::iterator
SparseMatrix::begin(const size_type r)
{
Assert (r < m(), ExcIndexRange(r, 0, m()));
if (row_length(r) > 0)
return iterator(this, r, 0);
else
return end (r);
}
inline
SparseMatrix::iterator
SparseMatrix::end(const size_type r)
{
Assert (r < m(), ExcIndexRange(r, 0, m()));
// place the iterator on the first entry
// past this line, or at the end of the
// matrix
for (size_type i=r+1; i<m(); ++i)
if (row_length(i) > 0)
return iterator(this, i, 0);
// if there is no such line, then take the
// end iterator of the matrix
return end();
}
inline
bool
SparseMatrix::in_local_range (const size_type index) const
{
TrilinosWrappers::types::int_type begin, end;
#ifndef DEAL_II_USE_LARGE_INDEX_TYPE
begin = matrix->RowMap().MinMyGID();
end = matrix->RowMap().MaxMyGID()+1;
#else
begin = matrix->RowMap().MinMyGID();
end = matrix->RowMap().MaxMyGID()+1;
#endif
return ((index >= static_cast<size_type>(begin)) &&
(index < static_cast<size_type>(end)));
}
inline
bool
SparseMatrix::is_compressed () const
{
return compressed;
}
inline
void
SparseMatrix::compress (::dealii::VectorOperation::values operation)
{
Epetra_CombineMode mode = last_action;
if (last_action == Zero)
{
if ((operation==::dealii::VectorOperation::add) ||
(operation==::dealii::VectorOperation::unknown))
mode = Add;
else if (operation==::dealii::VectorOperation::insert)
mode = Insert;
}
else
{
Assert(
((last_action == Add) && (operation!=::dealii::VectorOperation::insert))
||
((last_action == Insert) && (operation!=::dealii::VectorOperation::add)),
ExcMessage("operation and argument to compress() do not match"));
}
// flush buffers
int ierr;
ierr = matrix->GlobalAssemble (*column_space_map, matrix->RowMap(),
true, mode);
AssertThrow (ierr == 0, ExcTrilinosError(ierr));
ierr = matrix->OptimizeStorage ();
AssertThrow (ierr == 0, ExcTrilinosError(ierr));
last_action = Zero;
compressed = true;
}
inline
void
SparseMatrix::compress ()
{
compress(::dealii::VectorOperation::unknown);
}
inline
SparseMatrix &
SparseMatrix::operator = (const double d)
{
Assert (d==0, ExcScalarAssignmentOnlyForZeroValue());
compress (::dealii::VectorOperation::unknown); // TODO: why do we do this? Should we not check for is_compressed?
const int ierr = matrix->PutScalar(d);
AssertThrow (ierr == 0, ExcTrilinosError(ierr));
return *this;
}
// Inline the set() and add()
// functions, since they will be
// called frequently, and the
// compiler can optimize away
// some unnecessary loops when
// the sizes are given at
// compile time.
inline
void
SparseMatrix::set (const size_type i,
const size_type j,
const TrilinosScalar value)
{
Assert (numbers::is_finite(value), ExcNumberNotFinite());
set (i, 1, &j, &value, false);
}
inline
void
SparseMatrix::set (const std::vector<size_type> &indices,
const FullMatrix<TrilinosScalar> &values,
const bool elide_zero_values)
{
Assert (indices.size() == values.m(),
ExcDimensionMismatch(indices.size(), values.m()));
Assert (values.m() == values.n(), ExcNotQuadratic());
for (size_type i=0; i<indices.size(); ++i)
set (indices[i], indices.size(), &indices[0], &values(i,0),
elide_zero_values);
}
inline
void
SparseMatrix::set (const std::vector<size_type> &row_indices,
const std::vector<size_type> &col_indices,
const FullMatrix<TrilinosScalar> &values,
const bool elide_zero_values)
{
Assert (row_indices.size() == values.m(),
ExcDimensionMismatch(row_indices.size(), values.m()));
Assert (col_indices.size() == values.n(),
ExcDimensionMismatch(col_indices.size(), values.n()));
for (size_type i=0; i<row_indices.size(); ++i)
set (row_indices[i], col_indices.size(), &col_indices[0], &values(i,0),
elide_zero_values);
}
inline
void
SparseMatrix::set (const size_type row,
const std::vector<size_type> &col_indices,
const std::vector<TrilinosScalar> &values,
const bool elide_zero_values)
{
Assert (col_indices.size() == values.size(),
ExcDimensionMismatch(col_indices.size(), values.size()));
set (row, col_indices.size(), &col_indices[0], &values[0],
elide_zero_values);
}
inline
void
SparseMatrix::set (const size_type row,
const size_type n_cols,
const size_type *col_indices,
const TrilinosScalar *values,
const bool elide_zero_values)
{
int ierr;
if (last_action == Add)
{
ierr = matrix->GlobalAssemble (*column_space_map, matrix->RowMap(),
true);
Assert (ierr == 0, ExcTrilinosError(ierr));
}
last_action = Insert;
TrilinosWrappers::types::int_type *col_index_ptr;
TrilinosScalar *col_value_ptr;
TrilinosWrappers::types::int_type n_columns;
TrilinosScalar short_val_array[100];
TrilinosWrappers::types::int_type short_index_array[100];
std::vector<TrilinosScalar> long_val_array;
std::vector<TrilinosWrappers::types::int_type> long_index_array;
// If we don't elide zeros, the pointers are already available... need to
// cast to non-const pointers as that is the format taken by Trilinos (but
// we will not modify const data)
if (elide_zero_values == false)
{
col_index_ptr = (TrilinosWrappers::types::int_type *)col_indices;
col_value_ptr = const_cast<TrilinosScalar *>(values);
n_columns = n_cols;
}
else
{
// Otherwise, extract nonzero values in each row and get the
// respective indices.
if (n_cols > 100)
{
long_val_array.resize(n_cols);
long_index_array.resize(n_cols);
col_index_ptr = &long_index_array[0];
col_value_ptr = &long_val_array[0];
}
else
{
col_index_ptr = &short_index_array[0];
col_value_ptr = &short_val_array[0];
}
n_columns = 0;
for (size_type j=0; j<n_cols; ++j)
{
const double value = values[j];
Assert (numbers::is_finite(value), ExcNumberNotFinite());
if (value != 0)
{
col_index_ptr[n_columns] = col_indices[j];
col_value_ptr[n_columns] = value;
n_columns++;
}
}
Assert(n_columns <= (TrilinosWrappers::types::int_type)n_cols, ExcInternalError());
}
// If the calling matrix owns the row to which we want to insert values,
// we can directly call the Epetra_CrsMatrix input function, which is much
// faster than the Epetra_FECrsMatrix function. We distinguish between two
// cases: the first one is when the matrix is not filled (i.e., it is
// possible to add new elements to the sparsity pattern), and the second
// one is when the pattern is already fixed. In the former case, we add
// the possibility to insert new values, and in the second we just replace
// data.
if (row_partitioner().MyGID(static_cast<TrilinosWrappers::types::int_type>(row)) == true)
{
if (matrix->Filled() == false)
{
ierr = matrix->Epetra_CrsMatrix::InsertGlobalValues(
static_cast<TrilinosWrappers::types::int_type>(row),
static_cast<int>(n_columns),const_cast<double *>(col_value_ptr),
col_index_ptr);
// When inserting elements, we do not want to create exceptions in
// the case when inserting non-local data (since that's what we
// want to do right now).
if (ierr > 0)
ierr = 0;
}
else
ierr = matrix->Epetra_CrsMatrix::ReplaceGlobalValues(row, n_columns,
col_value_ptr,
col_index_ptr);
}
else
{
// When we're at off-processor data, we have to stick with the
// standard Insert/ReplaceGlobalValues function. Nevertheless, the way
// we call it is the fastest one (any other will lead to repeated
// allocation and deallocation of memory in order to call the function
// we already use, which is very unefficient if writing one element at
// a time).
compressed = false;
if (matrix->Filled() == false)
{
ierr = matrix->InsertGlobalValues (1,
(TrilinosWrappers::types::int_type *)&row,
n_columns, col_index_ptr,
&col_value_ptr,
Epetra_FECrsMatrix::ROW_MAJOR);
if (ierr > 0)
ierr = 0;
}
else
ierr = matrix->ReplaceGlobalValues (1,
(TrilinosWrappers::types::int_type *)&row,
n_columns, col_index_ptr,
&col_value_ptr,
Epetra_FECrsMatrix::ROW_MAJOR);
}
Assert (ierr <= 0, ExcAccessToNonPresentElement(row, col_index_ptr[0]));
AssertThrow (ierr >= 0, ExcTrilinosError(ierr));
}
inline
void
SparseMatrix::add (const size_type i,
const size_type j,
const TrilinosScalar value)
{
Assert (numbers::is_finite(value), ExcNumberNotFinite());
if (value == 0)
{
// we have to do checkings on Insert/Add in any case to be consistent
// with the MPI communication model (see the comments in the
// documentation of TrilinosWrappers::Vector), but we can save some
// work if the addend is zero. However, these actions are done in case
// we pass on to the other function.
// TODO: fix this (do not run compress here, but fail)
if (last_action == Insert)
{
int ierr;
ierr = matrix->GlobalAssemble(*column_space_map,
row_partitioner(), false);
Assert (ierr == 0, ExcTrilinosError(ierr));
(void)ierr; // removes -Wunused-but-set-variable in optimized mode
}
last_action = Add;
return;
}
else
add (i, 1, &j, &value, false);
}
inline
void
SparseMatrix::add (const std::vector<size_type> &indices,
const FullMatrix<TrilinosScalar> &values,
const bool elide_zero_values)
{
Assert (indices.size() == values.m(),
ExcDimensionMismatch(indices.size(), values.m()));
Assert (values.m() == values.n(), ExcNotQuadratic());
for (size_type i=0; i<indices.size(); ++i)
add (indices[i], indices.size(), &indices[0], &values(i,0),
elide_zero_values);
}
inline
void
SparseMatrix::add (const std::vector<size_type> &row_indices,
const std::vector<size_type> &col_indices,
const FullMatrix<TrilinosScalar> &values,
const bool elide_zero_values)
{
Assert (row_indices.size() == values.m(),
ExcDimensionMismatch(row_indices.size(), values.m()));
Assert (col_indices.size() == values.n(),
ExcDimensionMismatch(col_indices.size(), values.n()));
for (size_type i=0; i<row_indices.size(); ++i)
add (row_indices[i], col_indices.size(), &col_indices[0],
&values(i,0), elide_zero_values);
}
inline
void
SparseMatrix::add (const size_type row,
const std::vector<size_type> &col_indices,
const std::vector<TrilinosScalar> &values,
const bool elide_zero_values)
{
Assert (col_indices.size() == values.size(),
ExcDimensionMismatch(col_indices.size(), values.size()));
add (row, col_indices.size(), &col_indices[0], &values[0],
elide_zero_values);
}
inline
void
SparseMatrix::add (const size_type row,
const size_type n_cols,
const size_type *col_indices,
const TrilinosScalar *values,
const bool elide_zero_values,
const bool /*col_indices_are_sorted*/)
{
int ierr;
if (last_action == Insert)
{
// TODO: this could lead to a dead lock when only one processor
// calls GlobalAssemble.
ierr = matrix->GlobalAssemble(*column_space_map,
row_partitioner(), false);
AssertThrow (ierr == 0, ExcTrilinosError(ierr));
}
last_action = Add;
TrilinosWrappers::types::int_type *col_index_ptr;
TrilinosScalar *col_value_ptr;
TrilinosWrappers::types::int_type n_columns;
double short_val_array[100];
TrilinosWrappers::types::int_type short_index_array[100];
std::vector<TrilinosScalar> long_val_array;
std::vector<TrilinosWrappers::types::int_type> long_index_array;
// If we don't elide zeros, the pointers are already available... need to
// cast to non-const pointers as that is the format taken by Trilinos (but
// we will not modify const data)
if (elide_zero_values == false)
{
col_index_ptr = (TrilinosWrappers::types::int_type *)col_indices;
col_value_ptr = const_cast<TrilinosScalar *>(values);
n_columns = n_cols;
#ifdef DEBUG
for (size_type j=0; j<n_cols; ++j)
Assert (numbers::is_finite(values[j]), ExcNumberNotFinite());
#endif
}
else
{
// Otherwise, extract nonzero values in each row and the corresponding
// index.
if (n_cols > 100)
{
long_val_array.resize(n_cols);
long_index_array.resize(n_cols);
col_index_ptr = &long_index_array[0];
col_value_ptr = &long_val_array[0];
}
else
{
col_index_ptr = &short_index_array[0];
col_value_ptr = &short_val_array[0];
}
n_columns = 0;
for (size_type j=0; j<n_cols; ++j)
{
const double value = values[j];
Assert (numbers::is_finite(value), ExcNumberNotFinite());
if (value != 0)
{
col_index_ptr[n_columns] = col_indices[j];
col_value_ptr[n_columns] = value;
n_columns++;
}
}
Assert(n_columns <= (TrilinosWrappers::types::int_type)n_cols, ExcInternalError());
}
// If the calling processor owns the row to which we want to add values, we
// can directly call the Epetra_CrsMatrix input function, which is much
// faster than the Epetra_FECrsMatrix function.
if (row_partitioner().MyGID(static_cast<TrilinosWrappers::types::int_type>(row)) == true)
{
ierr = matrix->Epetra_CrsMatrix::SumIntoGlobalValues(row, n_columns,
col_value_ptr,
col_index_ptr);
}
else
{
// When we're at off-processor data, we have to stick with the
// standard SumIntoGlobalValues function. Nevertheless, the way we
// call it is the fastest one (any other will lead to repeated
// allocation and deallocation of memory in order to call the function
// we already use, which is very inefficient if writing one element at
// a time).
compressed = false;
ierr = matrix->SumIntoGlobalValues (1,
(TrilinosWrappers::types::int_type *)&row, n_columns,
col_index_ptr,
&col_value_ptr,
Epetra_FECrsMatrix::ROW_MAJOR);
}
#ifdef DEBUG
if (ierr > 0)
{
std::cout << "------------------------------------------"
<< std::endl;
std::cout << "Got error " << ierr << " in row " << row
<< " of proc " << row_partitioner().Comm().MyPID()
<< " when trying to add the columns:" << std::endl;
for (TrilinosWrappers::types::int_type i=0; i<n_columns; ++i)
std::cout << col_index_ptr[i] << " ";
std::cout << std::endl << std::endl;
std::cout << "Matrix row has the following indices:" << std::endl;
int n_indices, *indices;
trilinos_sparsity_pattern().ExtractMyRowView(row_partitioner().LID(static_cast<TrilinosWrappers::types::int_type>(row)),
n_indices,
indices);
for (TrilinosWrappers::types::int_type i=0; i<n_indices; ++i)
std::cout << indices[i] << " ";
std::cout << std::endl << std::endl;
Assert (ierr <= 0,
ExcAccessToNonPresentElement(row, col_index_ptr[0]));
}
#endif
Assert (ierr >= 0, ExcTrilinosError(ierr));
}
// inline "simple" functions that are
// called frequently and do only involve
// a call to some Trilinos function.
inline
SparseMatrix::size_type
SparseMatrix::m () const
{
#ifndef DEAL_II_USE_LARGE_INDEX_TYPE
return matrix->NumGlobalRows();
#else
return matrix->NumGlobalRows64();
#endif
}
inline
SparseMatrix::size_type
SparseMatrix::n () const
{
#ifndef DEAL_II_USE_LARGE_INDEX_TYPE
return matrix->NumGlobalCols();
#else
return matrix->NumGlobalCols64();
#endif
}
inline
unsigned int
SparseMatrix::local_size () const
{
return matrix -> NumMyRows();
}
inline
std::pair<SparseMatrix::size_type, SparseMatrix::size_type>
SparseMatrix::local_range () const
{
size_type begin, end;
#ifndef DEAL_II_USE_LARGE_INDEX_TYPE
begin = matrix->RowMap().MinMyGID();
end = matrix->RowMap().MaxMyGID()+1;
#else
begin = matrix->RowMap().MinMyGID64();
end = matrix->RowMap().MaxMyGID64()+1;
#endif
return std::make_pair (begin, end);
}
inline
SparseMatrix::size_type
SparseMatrix::n_nonzero_elements () const
{
#ifndef DEAL_II_USE_LARGE_INDEX_TYPE
return matrix->NumGlobalNonzeros();
#else
return matrix->NumGlobalNonzeros64();
#endif
}
template <typename SparsityType>
inline
void SparseMatrix::reinit (const IndexSet ¶llel_partitioning,
const SparsityType &sparsity_pattern,
const MPI_Comm &communicator,
const bool exchange_data)
{
Epetra_Map map = parallel_partitioning.make_trilinos_map (communicator, false);
reinit (map, map, sparsity_pattern, exchange_data);
}
template <typename SparsityType>
inline
void SparseMatrix::reinit (const IndexSet &row_parallel_partitioning,
const IndexSet &col_parallel_partitioning,
const SparsityType &sparsity_pattern,
const MPI_Comm &communicator,
const bool exchange_data)
{
Epetra_Map row_map =
row_parallel_partitioning.make_trilinos_map (communicator, false);
Epetra_Map col_map =
col_parallel_partitioning.make_trilinos_map (communicator, false);
reinit (row_map, col_map, sparsity_pattern, exchange_data);
}
template <typename number>
inline
void SparseMatrix::reinit (const IndexSet ¶llel_partitioning,
const ::dealii::SparseMatrix<number> &sparse_matrix,
const MPI_Comm &communicator,
const double drop_tolerance,
const bool copy_values,
const ::dealii::SparsityPattern *use_this_sparsity)
{
Epetra_Map map = parallel_partitioning.make_trilinos_map (communicator, false);
reinit (map, map, sparse_matrix, drop_tolerance, copy_values,
use_this_sparsity);
}
template <typename number>
inline
void SparseMatrix::reinit (const IndexSet &row_parallel_partitioning,
const IndexSet &col_parallel_partitioning,
const ::dealii::SparseMatrix<number> &sparse_matrix,
const MPI_Comm &communicator,
const double drop_tolerance,
const bool copy_values,
const ::dealii::SparsityPattern *use_this_sparsity)
{
Epetra_Map row_map =
row_parallel_partitioning.make_trilinos_map (communicator, false);
Epetra_Map col_map =
col_parallel_partitioning.make_trilinos_map (communicator, false);
reinit (row_map, col_map, sparse_matrix, drop_tolerance, copy_values,
use_this_sparsity);
}
inline
TrilinosScalar
SparseMatrix::l1_norm () const
{
Assert (matrix->Filled(), ExcMatrixNotCompressed());
return matrix->NormOne();
}
inline
TrilinosScalar
SparseMatrix::linfty_norm () const
{
Assert (matrix->Filled(), ExcMatrixNotCompressed());
return matrix->NormInf();
}
inline
TrilinosScalar
SparseMatrix::frobenius_norm () const
{
Assert (matrix->Filled(), ExcMatrixNotCompressed());
return matrix->NormFrobenius();
}
inline
SparseMatrix &
SparseMatrix::operator *= (const TrilinosScalar a)
{
const int ierr = matrix->Scale (a);
Assert (ierr == 0, ExcTrilinosError(ierr));
(void)ierr; // removes -Wunused-variable in optimized mode
return *this;
}
inline
SparseMatrix &
SparseMatrix::operator /= (const TrilinosScalar a)
{
Assert (a !=0, ExcDivideByZero());
const TrilinosScalar factor = 1./a;
const int ierr = matrix->Scale (factor);
Assert (ierr == 0, ExcTrilinosError(ierr));
(void)ierr; // removes -Wunused-variable in optimized mode
return *this;
}
namespace internal
{
namespace SparseMatrix
{
template <typename VectorType>
inline
void check_vector_map_equality(const Epetra_CrsMatrix &,
const VectorType &,
const VectorType &)
{
}
inline
void check_vector_map_equality(const Epetra_CrsMatrix &m,
const TrilinosWrappers::MPI::Vector &in,
const TrilinosWrappers::MPI::Vector &out)
{
Assert (in.vector_partitioner().SameAs(m.DomainMap()) == true,
ExcMessage ("Column map of matrix does not fit with vector map!"));
Assert (out.vector_partitioner().SameAs(m.RangeMap()) == true,
ExcMessage ("Row map of matrix does not fit with vector map!"));
(void)m;
(void)in;
(void)out;
}
}
}
template <typename VectorType>
inline
void
SparseMatrix::vmult (VectorType &dst,
const VectorType &src) const
{
Assert (&src != &dst, ExcSourceEqualsDestination());
Assert (matrix->Filled(), ExcMatrixNotCompressed());
(void)src;
(void)dst;
internal::SparseMatrix::check_vector_map_equality(*matrix, src, dst);
const size_type dst_local_size = dst.end() - dst.begin();
AssertDimension (dst_local_size, static_cast<size_type>(matrix->RangeMap().NumMyElements()));
(void)dst_local_size;
const size_type src_local_size = src.end() - src.begin();
AssertDimension (src_local_size, static_cast<size_type>(matrix->DomainMap().NumMyElements()));
(void)src_local_size;
Epetra_MultiVector tril_dst (View, matrix->RangeMap(), dst.begin(),
matrix->DomainMap().NumMyPoints(), 1);
Epetra_MultiVector tril_src (View, matrix->DomainMap(),
const_cast<TrilinosScalar *>(src.begin()),
matrix->DomainMap().NumMyPoints(), 1);
const int ierr = matrix->Multiply (false, tril_src, tril_dst);
Assert (ierr == 0, ExcTrilinosError(ierr));
(void)ierr; // removes -Wunused-variable in optimized mode
}
template <typename VectorType>
inline
void
SparseMatrix::Tvmult (VectorType &dst,
const VectorType &src) const
{
Assert (&src != &dst, ExcSourceEqualsDestination());
Assert (matrix->Filled(), ExcMatrixNotCompressed());
internal::SparseMatrix::check_vector_map_equality(*matrix, dst, src);
const size_type dst_local_size = dst.end() - dst.begin();
AssertDimension (dst_local_size, static_cast<size_type>(matrix->DomainMap().NumMyElements()));
const size_type src_local_size = src.end() - src.begin();
AssertDimension (src_local_size, static_cast<size_type>(matrix->RangeMap().NumMyElements()));
Epetra_MultiVector tril_dst (View, matrix->DomainMap(), dst.begin(),
matrix->DomainMap().NumMyPoints(), 1);
Epetra_MultiVector tril_src (View, matrix->RangeMap(),
const_cast<double *>(src.begin()),
matrix->DomainMap().NumMyPoints(), 1);
const int ierr = matrix->Multiply (true, tril_src, tril_dst);
Assert (ierr == 0, ExcTrilinosError(ierr));
(void)ierr; // removes -Wunused-variable in optimized mode
}
template <typename VectorType>
inline
void
SparseMatrix::vmult_add (VectorType &dst,
const VectorType &src) const
{
Assert (&src != &dst, ExcSourceEqualsDestination());
// Reinit a temporary vector with fast argument set, which does not
// overwrite the content (to save time). However, the
// TrilinosWrappers::Vector classes do not support this, so create a
// deal.II local vector that has this fast setting. It will be accepted in
// vmult because it only checks the local size.
dealii::Vector<TrilinosScalar> temp_vector;
temp_vector.reinit(dst.end()-dst.begin(), true);
dealii::VectorView<TrilinosScalar> src_view(src.end()-src.begin(), src.begin());
dealii::VectorView<TrilinosScalar> dst_view(dst.end()-dst.begin(), dst.begin());
vmult (temp_vector, static_cast<const dealii::Vector<TrilinosScalar>&>(src_view));
if (dst_view.size() > 0)
dst_view += temp_vector;
}
template <typename VectorType>
inline
void
SparseMatrix::Tvmult_add (VectorType &dst,
const VectorType &src) const
{
Assert (&src != &dst, ExcSourceEqualsDestination());
// Reinit a temporary vector with fast argument set, which does not
// overwrite the content (to save time). However, the
// TrilinosWrappers::Vector classes do not support this, so create a
// deal.II local vector that has this fast setting. It will be accepted in
// vmult because it only checks the local size.
dealii::Vector<TrilinosScalar> temp_vector;
temp_vector.reinit(dst.end()-dst.begin(), true);
dealii::VectorView<TrilinosScalar> src_view(src.end()-src.begin(), src.begin());
dealii::VectorView<TrilinosScalar> dst_view(dst.end()-dst.begin(), dst.begin());
Tvmult (temp_vector, static_cast<const dealii::Vector<TrilinosScalar>&>(src_view));
if (dst_view.size() > 0)
dst_view += temp_vector;
}
inline
TrilinosScalar
SparseMatrix::matrix_norm_square (const VectorBase &v) const
{
Assert (row_partitioner().SameAs(domain_partitioner()),
ExcNotQuadratic());
VectorBase temp_vector;
temp_vector.reinit(v, true);
vmult (temp_vector, v);
return temp_vector*v;
}
inline
TrilinosScalar
SparseMatrix::matrix_scalar_product (const VectorBase &u,
const VectorBase &v) const
{
Assert (row_partitioner().SameAs(domain_partitioner()),
ExcNotQuadratic());
VectorBase temp_vector;
temp_vector.reinit(v, true);
vmult (temp_vector, v);
return u*temp_vector;
}
inline
TrilinosScalar
SparseMatrix::residual (VectorBase &dst,
const VectorBase &x,
const VectorBase &b) const
{
vmult (dst, x);
dst -= b;
dst *= -1.;
return dst.l2_norm();
}
inline
const Epetra_CrsMatrix &
SparseMatrix::trilinos_matrix () const
{
return static_cast<const Epetra_CrsMatrix &>(*matrix);
}
inline
const Epetra_CrsGraph &
SparseMatrix::trilinos_sparsity_pattern () const
{
return matrix->Graph();
}
inline
const Epetra_Map &
SparseMatrix::domain_partitioner () const
{
return matrix->DomainMap();
}
inline
const Epetra_Map &
SparseMatrix::range_partitioner () const
{
return matrix->RangeMap();
}
inline
const Epetra_Map &
SparseMatrix::row_partitioner () const
{
return matrix->RowMap();
}
inline
const Epetra_Map &
SparseMatrix::col_partitioner () const
{
return matrix->ColMap();
}
inline
void
SparseMatrix::prepare_add()
{
//nothing to do here
}
inline
void
SparseMatrix::prepare_set()
{
//nothing to do here
}
#endif // DOXYGEN
}
DEAL_II_NAMESPACE_CLOSE
#endif // DEAL_II_WITH_TRILINOS
/*----------------------- trilinos_sparse_matrix.h --------------------*/
#endif
/*----------------------- trilinos_sparse_matrix.h --------------------*/
|