/usr/include/viennacl/vector.hpp is in libviennacl-dev 1.7.1+dfsg1-2.
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 | #ifndef VIENNACL_VECTOR_HPP_
#define VIENNACL_VECTOR_HPP_
/* =========================================================================
Copyright (c) 2010-2016, Institute for Microelectronics,
Institute for Analysis and Scientific Computing,
TU Wien.
Portions of this software are copyright by UChicago Argonne, LLC.
-----------------
ViennaCL - The Vienna Computing Library
-----------------
Project Head: Karl Rupp rupp@iue.tuwien.ac.at
(A list of authors and contributors can be found in the manual)
License: MIT (X11), see file LICENSE in the base directory
============================================================================= */
/** @file viennacl/vector.hpp
@brief The vector type with operator-overloads and proxy classes is defined here.
Linear algebra operations such as norms and inner products are located in linalg/vector_operations.hpp
*/
#include "viennacl/forwards.h"
#include "viennacl/detail/vector_def.hpp"
#include "viennacl/backend/memory.hpp"
#include "viennacl/scalar.hpp"
#include "viennacl/tools/tools.hpp"
#include "viennacl/tools/entry_proxy.hpp"
#include "viennacl/linalg/detail/op_executor.hpp"
#include "viennacl/linalg/vector_operations.hpp"
#include "viennacl/meta/result_of.hpp"
#include "viennacl/context.hpp"
#include "viennacl/traits/handle.hpp"
namespace viennacl
{
//
// Vector expression
//
/** @brief An expression template class that represents a binary operation that yields a vector
*
* In contrast to full expression templates as introduced by Veldhuizen, ViennaCL does not allow nested expressions.
* The reason is that this requires automated GPU viennacl::ocl::kernel generation, which then has to be compiles just-in-time.
* For performance-critical applications, one better writes the appropriate viennacl::ocl::kernels by hand.
*
* Assumption: dim(LHS) >= dim(RHS), where dim(scalar) = 0, dim(vector) = 1 and dim(matrix = 2)
*
* @tparam LHS left hand side operand
* @tparam RHS right hand side operand
* @tparam OP the operator
*/
template<typename LHS, typename RHS, typename OP>
class vector_expression
{
typedef typename viennacl::result_of::reference_if_nonscalar<LHS>::type lhs_reference_type;
typedef typename viennacl::result_of::reference_if_nonscalar<RHS>::type rhs_reference_type;
public:
enum { alignment = 1 };
/** @brief Extracts the vector type from the two operands.
*/
typedef vcl_size_t size_type;
vector_expression(LHS & l, RHS & r) : lhs_(l), rhs_(r) {}
/** @brief Get left hand side operand
*/
lhs_reference_type lhs() const { return lhs_; }
/** @brief Get right hand side operand
*/
rhs_reference_type rhs() const { return rhs_; }
/** @brief Returns the size of the result vector */
size_type size() const { return viennacl::traits::size(*this); }
private:
/** @brief The left hand side operand */
lhs_reference_type lhs_;
/** @brief The right hand side operand */
rhs_reference_type rhs_;
};
/** @brief A STL-type const-iterator for vector elements. Elements can be accessed, but cannot be manipulated. VERY SLOW!!
*
* Every dereference operation initiates a transfer from the GPU to the CPU. The overhead of such a transfer is around 50us, so 20.000 dereferences take one second.
* This is four orders of magnitude slower than similar dereferences on the CPU. However, increments and comparisons of iterators is as fast as for CPU types.
* If you need a fast iterator, copy the whole vector to the CPU first and iterate over the CPU object, e.g.
* std::vector<float> temp;
* copy(gpu_vector, temp);
* for (std::vector<float>::const_iterator iter = temp.begin();
* iter != temp.end();
* ++iter)
* {
* //do something
* }
* Note that you may obtain inconsistent data if entries of gpu_vector are manipulated elsewhere in the meanwhile.
*
* @tparam NumericT The underlying floating point type (either float or double)
* @tparam AlignmentV Alignment of the underlying vector, @see vector
*/
template<class NumericT, unsigned int AlignmentV>
class const_vector_iterator
{
typedef const_vector_iterator<NumericT, AlignmentV> self_type;
public:
typedef scalar<NumericT> value_type;
typedef vcl_size_t size_type;
typedef vcl_ptrdiff_t difference_type;
typedef viennacl::backend::mem_handle handle_type;
//const_vector_iterator() {}
/** @brief Constructor
* @param vec The vector over which to iterate
* @param index The starting index of the iterator
* @param start First index of the element in the vector pointed to be the iterator (for vector_range and vector_slice)
* @param stride Stride for the support of vector_slice
*/
const_vector_iterator(vector_base<NumericT> const & vec,
size_type index,
size_type start = 0,
size_type stride = 1) : elements_(vec.handle()), index_(index), start_(start), stride_(stride) {}
/** @brief Constructor for vector-like treatment of arbitrary buffers
* @param elements The buffer over which to iterate
* @param index The starting index of the iterator
* @param start First index of the element in the vector pointed to be the iterator (for vector_range and vector_slice)
* @param stride Stride for the support of vector_slice
*/
const_vector_iterator(handle_type const & elements,
size_type index,
size_type start = 0,
size_type stride = 1) : elements_(elements), index_(index), start_(start), stride_(stride) {}
/** @brief Dereferences the iterator and returns the value of the element. For convenience only, performance is poor due to OpenCL overhead! */
value_type operator*(void) const
{
value_type result;
result = const_entry_proxy<NumericT>(start_ + index_ * stride(), elements_);
return result;
}
self_type operator++(void) { ++index_; return *this; }
self_type operator++(int) { self_type tmp = *this; ++(*this); return tmp; }
bool operator==(self_type const & other) const { return index_ == other.index_; }
bool operator!=(self_type const & other) const { return index_ != other.index_; }
// self_type & operator=(self_type const & other)
// {
// index_ = other._index;
// elements_ = other._elements;
// return *this;
// }
difference_type operator-(self_type const & other) const
{
assert( (other.start_ == start_) && (other.stride_ == stride_) && bool("Iterators are not from the same vector (proxy)!"));
return static_cast<difference_type>(index_) - static_cast<difference_type>(other.index_);
}
self_type operator+(difference_type diff) const { return self_type(elements_, size_type(difference_type(index_) + diff), start_, stride_); }
//vcl_size_t index() const { return index_; }
/** @brief Offset of the current element index with respect to the beginning of the buffer */
size_type offset() const { return start_ + index_ * stride(); }
/** @brief Index increment in the underlying buffer when incrementing the iterator to the next element */
size_type stride() const { return stride_; }
handle_type const & handle() const { return elements_; }
protected:
/** @brief The index of the entry the iterator is currently pointing to */
handle_type const & elements_;
size_type index_; //offset from the beginning of elements_
size_type start_;
size_type stride_;
};
/** @brief A STL-type iterator for vector elements. Elements can be accessed and manipulated. VERY SLOW!!
*
* Every dereference operation initiates a transfer from the GPU to the CPU. The overhead of such a transfer is around 50us, so 20.000 dereferences take one second.
* This is four orders of magnitude slower than similar dereferences on the CPU. However, increments and comparisons of iterators is as fast as for CPU types.
* If you need a fast iterator, copy the whole vector to the CPU first and iterate over the CPU object, e.g.
* std::vector<float> temp;
* copy(gpu_vector, temp);
* for (std::vector<float>::const_iterator iter = temp.begin();
* iter != temp.end();
* ++iter)
* {
* //do something
* }
* copy(temp, gpu_vector);
* Note that you may obtain inconsistent data if you manipulate entries of gpu_vector in the meanwhile.
*
* @tparam NumericT The underlying floating point type (either float or double)
* @tparam AlignmentV Alignment of the underlying vector, @see vector
*/
template<class NumericT, unsigned int AlignmentV>
class vector_iterator : public const_vector_iterator<NumericT, AlignmentV>
{
typedef const_vector_iterator<NumericT, AlignmentV> base_type;
typedef vector_iterator<NumericT, AlignmentV> self_type;
public:
typedef typename base_type::handle_type handle_type;
typedef typename base_type::size_type size_type;
typedef typename base_type::difference_type difference_type;
vector_iterator(handle_type const & elements,
size_type index,
size_type start = 0,
size_type stride = 1) : base_type(elements, index, start, stride), elements_(elements) {}
/** @brief Constructor
* @param vec The vector over which to iterate
* @param index The starting index of the iterator
* @param start Offset from the beginning of the underlying vector (for ranges and slices)
* @param stride Stride for slices
*/
vector_iterator(vector_base<NumericT> & vec,
size_type index,
size_type start = 0,
size_type stride = 1) : base_type(vec, index, start, stride), elements_(vec.handle()) {}
//vector_iterator(base_type const & b) : base_type(b) {}
entry_proxy<NumericT> operator*(void)
{
return entry_proxy<NumericT>(base_type::start_ + base_type::index_ * base_type::stride(), elements_);
}
difference_type operator-(self_type const & other) const { difference_type result = base_type::index_; return (result - static_cast<difference_type>(other.index_)); }
self_type operator+(difference_type diff) const { return self_type(elements_, static_cast<vcl_size_t>(static_cast<difference_type>(base_type::index_) + diff), base_type::start_, base_type::stride_); }
handle_type & handle() { return elements_; }
handle_type const & handle() const { return base_type::elements_; }
//operator base_type() const
//{
// return base_type(base_type::elements_, base_type::index_, base_type::start_, base_type::stride_);
//}
private:
handle_type elements_;
};
template<class NumericT, typename SizeT, typename DistanceT>
vector_base<NumericT, SizeT, DistanceT>::vector_base() : size_(0), start_(0), stride_(1), internal_size_(0) { /* Note: One must not call ::init() here because a vector might have been created globally before the backend has become available */ }
template<class NumericT, typename SizeT, typename DistanceT>
vector_base<NumericT, SizeT, DistanceT>::vector_base(viennacl::backend::mem_handle & h,
size_type vec_size, size_type vec_start, size_type vec_stride)
: size_(vec_size), start_(vec_start), stride_(vec_stride), internal_size_(vec_size), elements_(h) {}
template<class NumericT, typename SizeT, typename DistanceT>
vector_base<NumericT, SizeT, DistanceT>::vector_base(size_type vec_size, viennacl::context ctx)
: size_(vec_size), start_(0), stride_(1), internal_size_(viennacl::tools::align_to_multiple<size_type>(size_, dense_padding_size))
{
if (size_ > 0)
{
viennacl::backend::memory_create(elements_, sizeof(NumericT)*internal_size(), ctx);
clear();
}
}
// CUDA or host memory:
template<class NumericT, typename SizeT, typename DistanceT>
vector_base<NumericT, SizeT, DistanceT>::vector_base(NumericT * ptr_to_mem, viennacl::memory_types mem_type, size_type vec_size, vcl_size_t start, size_type stride)
: size_(vec_size), start_(start), stride_(stride), internal_size_(vec_size)
{
if (mem_type == viennacl::CUDA_MEMORY)
{
#ifdef VIENNACL_WITH_CUDA
elements_.switch_active_handle_id(viennacl::CUDA_MEMORY);
elements_.cuda_handle().reset(reinterpret_cast<char*>(ptr_to_mem));
elements_.cuda_handle().inc(); //prevents that the user-provided memory is deleted once the vector object is destroyed.
#else
throw cuda_not_available_exception();
#endif
}
else if (mem_type == viennacl::MAIN_MEMORY)
{
elements_.switch_active_handle_id(viennacl::MAIN_MEMORY);
elements_.ram_handle().reset(reinterpret_cast<char*>(ptr_to_mem));
elements_.ram_handle().inc(); //prevents that the user-provided memory is deleted once the vector object is destroyed.
}
elements_.raw_size(sizeof(NumericT) * vec_size);
}
#ifdef VIENNACL_WITH_OPENCL
template<class NumericT, typename SizeT, typename DistanceT>
vector_base<NumericT, SizeT, DistanceT>::vector_base(cl_mem existing_mem, size_type vec_size, size_type start, size_type stride, viennacl::context ctx)
: size_(vec_size), start_(start), stride_(stride), internal_size_(vec_size)
{
elements_.switch_active_handle_id(viennacl::OPENCL_MEMORY);
elements_.opencl_handle() = existing_mem;
elements_.opencl_handle().inc(); //prevents that the user-provided memory is deleted once the vector object is destroyed.
elements_.opencl_handle().context(ctx.opencl_context());
elements_.raw_size(sizeof(NumericT) * vec_size);
}
#endif
template<class NumericT, typename SizeT, typename DistanceT>
template<typename LHS, typename RHS, typename OP>
vector_base<NumericT, SizeT, DistanceT>::vector_base(vector_expression<const LHS, const RHS, OP> const & proxy)
: size_(viennacl::traits::size(proxy)), start_(0), stride_(1), internal_size_(viennacl::tools::align_to_multiple<size_type>(size_, dense_padding_size))
{
if (size_ > 0)
{
viennacl::backend::memory_create(elements_, sizeof(NumericT)*internal_size(), viennacl::traits::context(proxy));
clear();
}
self_type::operator=(proxy);
}
// Copy CTOR:
template<class NumericT, typename SizeT, typename DistanceT>
vector_base<NumericT, SizeT, DistanceT>::vector_base(const vector_base<NumericT, SizeT, DistanceT> & other) :
size_(other.size_), start_(0), stride_(1),
internal_size_(viennacl::tools::align_to_multiple<size_type>(other.size_, dense_padding_size))
{
elements_.switch_active_handle_id(viennacl::traits::active_handle_id(other));
if (internal_size() > 0)
{
viennacl::backend::memory_create(elements_, sizeof(NumericT)*internal_size(), viennacl::traits::context(other));
clear();
self_type::operator=(other);
}
}
// Conversion CTOR:
template<typename NumericT, typename SizeT, typename DistanceT>
template<typename OtherNumericT>
vector_base<NumericT, SizeT, DistanceT>::vector_base(const vector_base<OtherNumericT> & other) :
size_(other.size()), start_(0), stride_(1),
internal_size_(viennacl::tools::align_to_multiple<size_type>(other.size(), dense_padding_size))
{
elements_.switch_active_handle_id(viennacl::traits::active_handle_id(other));
if (internal_size() > 0)
{
viennacl::backend::memory_create(elements_, sizeof(NumericT)*internal_size(), viennacl::traits::context(other));
clear();
self_type::operator=(other);
}
}
template<class NumericT, typename SizeT, typename DistanceT>
vector_base<NumericT, SizeT, DistanceT> & vector_base<NumericT, SizeT, DistanceT>::operator=(const self_type & vec)
{
assert( ( (vec.size() == size()) || (size() == 0) )
&& bool("Incompatible vector sizes!"));
if (&vec==this)
return *this;
if (vec.size() > 0)
{
if (size_ == 0)
{
size_ = vec.size();
internal_size_ = viennacl::tools::align_to_multiple<size_type>(size_, dense_padding_size);
elements_.switch_active_handle_id(vec.handle().get_active_handle_id());
viennacl::backend::memory_create(elements_, sizeof(NumericT)*internal_size(), viennacl::traits::context(vec));
pad();
}
viennacl::linalg::av(*this,
vec, cpu_value_type(1.0), 1, false, false);
}
return *this;
}
/** @brief Implementation of the operation v1 = v2 @ alpha, where @ denotes either multiplication or division, and alpha is either a CPU or a GPU scalar
*
* @param proxy An expression template proxy class.
*/
template<class NumericT, typename SizeT, typename DistanceT>
template<typename LHS, typename RHS, typename OP>
vector_base<NumericT, SizeT, DistanceT> & vector_base<NumericT, SizeT, DistanceT>::operator=(const vector_expression<const LHS, const RHS, OP> & proxy)
{
assert( ( (viennacl::traits::size(proxy) == size()) || (size() == 0) )
&& bool("Incompatible vector sizes!"));
// initialize the necessary buffer
if (size() == 0)
{
size_ = viennacl::traits::size(proxy);
internal_size_ = viennacl::tools::align_to_multiple<size_type>(size_, dense_padding_size);
viennacl::backend::memory_create(elements_, sizeof(NumericT)*internal_size(), viennacl::traits::context(proxy));
pad();
}
linalg::detail::op_executor<self_type, op_assign, vector_expression<const LHS, const RHS, OP> >::apply(*this, proxy);
return *this;
}
// convert from vector with other numeric type
template<class NumericT, typename SizeT, typename DistanceT>
template<typename OtherNumericT>
vector_base<NumericT, SizeT, DistanceT> & vector_base<NumericT, SizeT, DistanceT>:: operator = (const vector_base<OtherNumericT> & v1)
{
assert( ( (v1.size() == size()) || (size() == 0) )
&& bool("Incompatible vector sizes!"));
if (size() == 0)
{
size_ = v1.size();
if (size_ > 0)
{
internal_size_ = viennacl::tools::align_to_multiple<size_type>(size_, dense_padding_size);
viennacl::backend::memory_create(elements_, sizeof(NumericT)*internal_size(), viennacl::traits::context(v1));
pad();
}
}
viennacl::linalg::convert(*this, v1);
return *this;
}
/** @brief Creates the vector from the supplied unit vector. */
template<class NumericT, typename SizeT, typename DistanceT>
vector_base<NumericT, SizeT, DistanceT> & vector_base<NumericT, SizeT, DistanceT>::operator = (unit_vector<NumericT> const & v)
{
assert( ( (v.size() == size()) || (size() == 0) )
&& bool("Incompatible vector sizes!"));
if (size() == 0)
{
size_ = v.size();
internal_size_ = viennacl::tools::align_to_multiple<size_type>(size_, dense_padding_size);
if (size_ > 0)
{
viennacl::backend::memory_create(elements_, sizeof(NumericT)*internal_size(), v.context());
clear();
}
}
else
viennacl::linalg::vector_assign(*this, NumericT(0));
if (size_ > 0)
this->operator()(v.index()) = NumericT(1);
return *this;
}
/** @brief Creates the vector from the supplied zero vector. */
template<class NumericT, typename SizeT, typename DistanceT>
vector_base<NumericT, SizeT, DistanceT> & vector_base<NumericT, SizeT, DistanceT>::operator = (zero_vector<NumericT> const & v)
{
assert( ( (v.size() == size()) || (size() == 0) )
&& bool("Incompatible vector sizes!"));
if (size() == 0)
{
size_ = v.size();
internal_size_ = viennacl::tools::align_to_multiple<size_type>(size_, dense_padding_size);
if (size_ > 0)
{
viennacl::backend::memory_create(elements_, sizeof(NumericT)*internal_size(), v.context());
clear();
}
}
else
viennacl::linalg::vector_assign(*this, NumericT(0));
return *this;
}
/** @brief Creates the vector from the supplied scalar vector. */
template<class NumericT, typename SizeT, typename DistanceT>
vector_base<NumericT, SizeT, DistanceT> & vector_base<NumericT, SizeT, DistanceT>::operator = (scalar_vector<NumericT> const & v)
{
assert( ( (v.size() == size()) || (size() == 0) )
&& bool("Incompatible vector sizes!"));
if (size() == 0)
{
size_ = v.size();
internal_size_ = viennacl::tools::align_to_multiple<size_type>(size_, dense_padding_size);
if (size_ > 0)
{
viennacl::backend::memory_create(elements_, sizeof(NumericT)*internal_size(), v.context());
pad();
}
}
if (size_ > 0)
viennacl::linalg::vector_assign(*this, v[0]);
return *this;
}
///////////////////////////// Matrix Vector interaction start ///////////////////////////////////
//Note: The following operator overloads are defined in matrix_operations.hpp, compressed_matrix_operations.hpp and coordinate_matrix_operations.hpp
//This is certainly not the nicest approach and will most likely by changed in the future, but it works :-)
//matrix<>
template<class NumericT, typename SizeT, typename DistanceT>
vector_base<NumericT, SizeT, DistanceT> & vector_base<NumericT, SizeT, DistanceT>::operator=(const viennacl::vector_expression< const matrix_base<NumericT>, const vector_base<NumericT>, viennacl::op_prod> & proxy)
{
assert(viennacl::traits::size1(proxy.lhs()) == size() && bool("Size check failed for v1 = A * v2: size1(A) != size(v1)"));
// check for the special case x = A * x
if (viennacl::traits::handle(proxy.rhs()) == viennacl::traits::handle(*this))
{
viennacl::vector<NumericT> result(viennacl::traits::size1(proxy.lhs()));
viennacl::linalg::prod_impl(proxy.lhs(), proxy.rhs(), result);
*this = result;
}
else
{
viennacl::linalg::prod_impl(proxy.lhs(), proxy.rhs(), *this);
}
return *this;
}
//transposed_matrix_proxy:
template<class NumericT, typename SizeT, typename DistanceT>
vector_base<NumericT, SizeT, DistanceT> & vector_base<NumericT, SizeT, DistanceT>::operator=(const vector_expression< const matrix_expression< const matrix_base<NumericT>, const matrix_base<NumericT>, op_trans >,
const vector_base<NumericT>,
op_prod> & proxy)
{
assert(viennacl::traits::size1(proxy.lhs()) == size() && bool("Size check failed in v1 = trans(A) * v2: size2(A) != size(v1)"));
// check for the special case x = trans(A) * x
if (viennacl::traits::handle(proxy.rhs()) == viennacl::traits::handle(*this))
{
viennacl::vector<NumericT> result(viennacl::traits::size1(proxy.lhs()));
viennacl::linalg::prod_impl(proxy.lhs(), proxy.rhs(), result);
*this = result;
}
else
{
viennacl::linalg::prod_impl(proxy.lhs(), proxy.rhs(), *this);
}
return *this;
}
///////////////////////////// Matrix Vector interaction end ///////////////////////////////////
//////////////////////////// Read-write access to an element of the vector start ///////////////////
//read-write access to an element of the vector
template<class NumericT, typename SizeT, typename DistanceT>
entry_proxy<NumericT> vector_base<NumericT, SizeT, DistanceT>::operator()(size_type index)
{
assert( (size() > 0) && bool("Cannot apply operator() to vector of size zero!"));
assert( index < size() && bool("Index out of bounds!") );
return entry_proxy<NumericT>(start_ + stride_ * index, elements_);
}
template<class NumericT, typename SizeT, typename DistanceT>
entry_proxy<NumericT> vector_base<NumericT, SizeT, DistanceT>::operator[](size_type index)
{
assert( (size() > 0) && bool("Cannot apply operator() to vector of size zero!"));
assert( index < size() && bool("Index out of bounds!") );
return entry_proxy<NumericT>(start_ + stride_ * index, elements_);
}
template<class NumericT, typename SizeT, typename DistanceT>
const_entry_proxy<NumericT> vector_base<NumericT, SizeT, DistanceT>::operator()(size_type index) const
{
assert( (size() > 0) && bool("Cannot apply operator() to vector of size zero!"));
assert( index < size() && bool("Index out of bounds!") );
return const_entry_proxy<NumericT>(start_ + stride_ * index, elements_);
}
template<class NumericT, typename SizeT, typename DistanceT>
const_entry_proxy<NumericT> vector_base<NumericT, SizeT, DistanceT>::operator[](size_type index) const
{
assert( (size() > 0) && bool("Cannot apply operator() to vector of size zero!"));
assert( index < size() && bool("Index out of bounds!") );
return const_entry_proxy<NumericT>(start_ + stride_ * index, elements_);
}
//////////////////////////// Read-write access to an element of the vector end ///////////////////
//
// Operator overloads with implicit conversion (thus cannot be made global without introducing additional headache)
//
template<class NumericT, typename SizeT, typename DistanceT>
vector_base<NumericT, SizeT, DistanceT> & vector_base<NumericT, SizeT, DistanceT>::operator += (const self_type & vec)
{
assert(vec.size() == size() && bool("Incompatible vector sizes!"));
if (size() > 0)
viennacl::linalg::avbv(*this,
*this, NumericT(1.0), 1, false, false,
vec, NumericT(1.0), 1, false, false);
return *this;
}
template<class NumericT, typename SizeT, typename DistanceT>
vector_base<NumericT, SizeT, DistanceT> & vector_base<NumericT, SizeT, DistanceT>::operator -= (const self_type & vec)
{
assert(vec.size() == size() && bool("Incompatible vector sizes!"));
if (size() > 0)
viennacl::linalg::avbv(*this,
*this, NumericT(1.0), 1, false, false,
vec, NumericT(-1.0), 1, false, false);
return *this;
}
/** @brief Scales a vector (or proxy) by a char (8-bit integer) value */
template<class NumericT, typename SizeT, typename DistanceT>
vector_base<NumericT, SizeT, DistanceT> & vector_base<NumericT, SizeT, DistanceT>::operator *= (char val)
{
if (size() > 0)
viennacl::linalg::av(*this,
*this, NumericT(val), 1, false, false);
return *this;
}
/** @brief Scales a vector (or proxy) by a short integer value */
template<class NumericT, typename SizeT, typename DistanceT>
vector_base<NumericT, SizeT, DistanceT> & vector_base<NumericT, SizeT, DistanceT>::operator *= (short val)
{
if (size() > 0)
viennacl::linalg::av(*this,
*this, NumericT(val), 1, false, false);
return *this;
}
/** @brief Scales a vector (or proxy) by an integer value */
template<class NumericT, typename SizeT, typename DistanceT>
vector_base<NumericT, SizeT, DistanceT> & vector_base<NumericT, SizeT, DistanceT>::operator *= (int val)
{
if (size() > 0)
viennacl::linalg::av(*this,
*this, NumericT(val), 1, false, false);
return *this;
}
/** @brief Scales a vector (or proxy) by a long integer value */
template<class NumericT, typename SizeT, typename DistanceT>
vector_base<NumericT, SizeT, DistanceT> & vector_base<NumericT, SizeT, DistanceT>::operator *= (long val)
{
if (size() > 0)
viennacl::linalg::av(*this,
*this, NumericT(val), 1, false, false);
return *this;
}
/** @brief Scales a vector (or proxy) by a single precision floating point value */
template<class NumericT, typename SizeT, typename DistanceT>
vector_base<NumericT, SizeT, DistanceT> & vector_base<NumericT, SizeT, DistanceT>::operator *= (float val)
{
if (size() > 0)
viennacl::linalg::av(*this,
*this, NumericT(val), 1, false, false);
return *this;
}
/** @brief Scales a vector (or proxy) by a double precision floating point value */
template<class NumericT, typename SizeT, typename DistanceT>
vector_base<NumericT, SizeT, DistanceT> & vector_base<NumericT, SizeT, DistanceT>::operator *= (double val)
{
if (size() > 0)
viennacl::linalg::av(*this,
*this, NumericT(val), 1, false, false);
return *this;
}
/** @brief Scales this vector by a char (8-bit) value */
template<class NumericT, typename SizeT, typename DistanceT>
vector_base<NumericT, SizeT, DistanceT> & vector_base<NumericT, SizeT, DistanceT>::operator /= (char val)
{
if (size() > 0)
viennacl::linalg::av(*this,
*this, NumericT(val), 1, true, false);
return *this;
}
/** @brief Scales this vector by a short integer value */
template<class NumericT, typename SizeT, typename DistanceT>
vector_base<NumericT, SizeT, DistanceT> & vector_base<NumericT, SizeT, DistanceT>::operator /= (short val)
{
if (size() > 0)
viennacl::linalg::av(*this,
*this, NumericT(val), 1, true, false);
return *this;
}
/** @brief Scales this vector by an integer value */
template<class NumericT, typename SizeT, typename DistanceT>
vector_base<NumericT, SizeT, DistanceT> & vector_base<NumericT, SizeT, DistanceT>::operator /= (int val)
{
if (size() > 0)
viennacl::linalg::av(*this,
*this, NumericT(val), 1, true, false);
return *this;
}
/** @brief Scales this vector by a long integer value */
template<class NumericT, typename SizeT, typename DistanceT>
vector_base<NumericT, SizeT, DistanceT> & vector_base<NumericT, SizeT, DistanceT>::operator /= (long val)
{
if (size() > 0)
viennacl::linalg::av(*this,
*this, NumericT(val), 1, true, false);
return *this;
}
/** @brief Scales this vector by a single precision floating point value */
template<class NumericT, typename SizeT, typename DistanceT>
vector_base<NumericT, SizeT, DistanceT> & vector_base<NumericT, SizeT, DistanceT>::operator /= (float val)
{
if (size() > 0)
viennacl::linalg::av(*this,
*this, NumericT(val), 1, true, false);
return *this;
}
/** @brief Scales this vector by a double precision floating point value */
template<class NumericT, typename SizeT, typename DistanceT>
vector_base<NumericT, SizeT, DistanceT> & vector_base<NumericT, SizeT, DistanceT>::operator /= (double val)
{
if (size() > 0)
viennacl::linalg::av(*this,
*this, NumericT(val), 1, true, false);
return *this;
}
/** @brief Scales the vector by a char (8-bit value) 'alpha' and returns an expression template */
template<class NumericT, typename SizeT, typename DistanceT>
vector_expression< const vector_base<NumericT, SizeT, DistanceT>, const NumericT, op_mult>
vector_base<NumericT, SizeT, DistanceT>::operator * (char value) const
{
return vector_expression< const self_type, const NumericT, op_mult>(*this, NumericT(value));
}
/** @brief Scales the vector by a short integer 'alpha' and returns an expression template */
template<class NumericT, typename SizeT, typename DistanceT>
vector_expression< const vector_base<NumericT, SizeT, DistanceT>, const NumericT, op_mult>
vector_base<NumericT, SizeT, DistanceT>::operator * (short value) const
{
return vector_expression< const self_type, const NumericT, op_mult>(*this, NumericT(value));
}
/** @brief Scales the vector by an integer 'alpha' and returns an expression template */
template<class NumericT, typename SizeT, typename DistanceT>
vector_expression< const vector_base<NumericT, SizeT, DistanceT>, const NumericT, op_mult>
vector_base<NumericT, SizeT, DistanceT>::operator * (int value) const
{
return vector_expression< const self_type, const NumericT, op_mult>(*this, NumericT(value));
}
/** @brief Scales the vector by a long integer 'alpha' and returns an expression template */
template<class NumericT, typename SizeT, typename DistanceT>
vector_expression< const vector_base<NumericT, SizeT, DistanceT>, const NumericT, op_mult>
vector_base<NumericT, SizeT, DistanceT>::operator * (long value) const
{
return vector_expression< const self_type, const NumericT, op_mult>(*this, NumericT(value));
}
/** @brief Scales the vector by a single precision floating point number 'alpha' and returns an expression template */
template<class NumericT, typename SizeT, typename DistanceT>
vector_expression< const vector_base<NumericT, SizeT, DistanceT>, const NumericT, op_mult>
vector_base<NumericT, SizeT, DistanceT>::operator * (float value) const
{
return vector_expression< const self_type, const NumericT, op_mult>(*this, NumericT(value));
}
/** @brief Scales the vector by a single precision floating point number 'alpha' and returns an expression template */
template<class NumericT, typename SizeT, typename DistanceT>
vector_expression< const vector_base<NumericT, SizeT, DistanceT>, const NumericT, op_mult>
vector_base<NumericT, SizeT, DistanceT>::operator * (double value) const
{
return vector_expression< const self_type, const NumericT, op_mult>(*this, NumericT(value));
}
/** @brief Scales the vector by a char (8-bit value) 'alpha' and returns an expression template */
template<class NumericT, typename SizeT, typename DistanceT>
vector_expression< const vector_base<NumericT, SizeT, DistanceT>, const NumericT, op_div>
vector_base<NumericT, SizeT, DistanceT>::operator / (char value) const
{
return vector_expression< const self_type, const NumericT, op_div>(*this, NumericT(value));
}
/** @brief Scales the vector by a short integer 'alpha' and returns an expression template */
template<class NumericT, typename SizeT, typename DistanceT>
vector_expression< const vector_base<NumericT, SizeT, DistanceT>, const NumericT, op_div>
vector_base<NumericT, SizeT, DistanceT>::operator / (short value) const
{
return vector_expression< const self_type, const NumericT, op_div>(*this, NumericT(value));
}
/** @brief Scales the vector by an integer 'alpha' and returns an expression template */
template<class NumericT, typename SizeT, typename DistanceT>
vector_expression< const vector_base<NumericT, SizeT, DistanceT>, const NumericT, op_div>
vector_base<NumericT, SizeT, DistanceT>::operator / (int value) const
{
return vector_expression< const self_type, const NumericT, op_div>(*this, NumericT(value));
}
/** @brief Scales the vector by a long integer 'alpha' and returns an expression template */
template<class NumericT, typename SizeT, typename DistanceT>
vector_expression< const vector_base<NumericT, SizeT, DistanceT>, const NumericT, op_div>
vector_base<NumericT, SizeT, DistanceT>::operator / (long value) const
{
return vector_expression< const self_type, const NumericT, op_div>(*this, NumericT(value));
}
/** @brief Scales the vector by a single precision floating point number 'alpha' and returns an expression template */
template<class NumericT, typename SizeT, typename DistanceT>
vector_expression< const vector_base<NumericT, SizeT, DistanceT>, const NumericT, op_div>
vector_base<NumericT, SizeT, DistanceT>::operator / (float value) const
{
return vector_expression< const self_type, const NumericT, op_div>(*this, NumericT(value));
}
/** @brief Scales the vector by a double precision floating point number 'alpha' and returns an expression template */
template<class NumericT, typename SizeT, typename DistanceT>
vector_expression< const vector_base<NumericT, SizeT, DistanceT>, const NumericT, op_div>
vector_base<NumericT, SizeT, DistanceT>::operator / (double value) const
{
return vector_expression< const self_type, const NumericT, op_div>(*this, NumericT(value));
}
/** @brief Sign flip for the vector. Emulated to be equivalent to -1.0 * vector */
template<class NumericT, typename SizeT, typename DistanceT>
vector_expression<const vector_base<NumericT, SizeT, DistanceT>, const NumericT, op_mult>
vector_base<NumericT, SizeT, DistanceT>::operator-() const
{
return vector_expression<const self_type, const NumericT, op_mult>(*this, NumericT(-1.0));
}
//
//// iterators:
//
/** @brief Returns an iterator pointing to the beginning of the vector (STL like)*/
template<class NumericT, typename SizeT, typename DistanceT>
typename vector_base<NumericT, SizeT, DistanceT>::iterator vector_base<NumericT, SizeT, DistanceT>::begin()
{
return iterator(*this, 0, start_, stride_);
}
/** @brief Returns an iterator pointing to the end of the vector (STL like)*/
template<class NumericT, typename SizeT, typename DistanceT>
typename vector_base<NumericT, SizeT, DistanceT>::iterator vector_base<NumericT, SizeT, DistanceT>::end()
{
return iterator(*this, size(), start_, stride_);
}
/** @brief Returns a const-iterator pointing to the beginning of the vector (STL like)*/
template<class NumericT, typename SizeT, typename DistanceT>
typename vector_base<NumericT, SizeT, DistanceT>::const_iterator vector_base<NumericT, SizeT, DistanceT>::begin() const
{
return const_iterator(*this, 0, start_, stride_);
}
template<class NumericT, typename SizeT, typename DistanceT>
typename vector_base<NumericT, SizeT, DistanceT>::const_iterator vector_base<NumericT, SizeT, DistanceT>::end() const
{
return const_iterator(*this, size(), start_, stride_);
}
template<class NumericT, typename SizeT, typename DistanceT>
vector_base<NumericT, SizeT, DistanceT> & vector_base<NumericT, SizeT, DistanceT>::swap(self_type & other)
{
viennacl::linalg::vector_swap(*this, other);
return *this;
}
template<class NumericT, typename SizeT, typename DistanceT>
void vector_base<NumericT, SizeT, DistanceT>::clear()
{
viennacl::linalg::vector_assign(*this, cpu_value_type(0.0), true);
}
template<class NumericT, typename SizeT, typename DistanceT>
vector_base<NumericT, SizeT, DistanceT> & vector_base<NumericT, SizeT, DistanceT>::fast_swap(self_type & other)
{
assert(this->size_ == other.size_ && bool("Vector size mismatch"));
this->elements_.swap(other.elements_);
return *this;
}
template<class NumericT, typename SizeT, typename DistanceT>
void vector_base<NumericT, SizeT, DistanceT>::pad()
{
if (internal_size() != size())
{
std::vector<NumericT> pad(internal_size() - size());
viennacl::backend::memory_write(elements_, sizeof(NumericT) * size(), sizeof(NumericT) * pad.size(), &(pad[0]));
}
}
template<class NumericT, typename SizeT, typename DistanceT>
void vector_base<NumericT, SizeT, DistanceT>::switch_memory_context(viennacl::context new_ctx)
{
viennacl::backend::switch_memory_context<NumericT>(elements_, new_ctx);
}
//TODO: Think about implementing the following public member functions
//void insert_element(unsigned int i, NumericT val){}
//void erase_element(unsigned int i){}
template<class NumericT, typename SizeT, typename DistanceT>
void vector_base<NumericT, SizeT, DistanceT>::resize(size_type new_size, bool preserve)
{
resize_impl(new_size, viennacl::traits::context(*this), preserve);
}
template<class NumericT, typename SizeT, typename DistanceT>
void vector_base<NumericT, SizeT, DistanceT>::resize(size_type new_size, viennacl::context ctx, bool preserve)
{
resize_impl(new_size, ctx, preserve);
}
template<class NumericT, typename SizeT, typename DistanceT>
void vector_base<NumericT, SizeT, DistanceT>::resize_impl(size_type new_size, viennacl::context ctx, bool preserve)
{
assert(new_size > 0 && bool("Positive size required when resizing vector!"));
if (new_size != size_)
{
vcl_size_t new_internal_size = viennacl::tools::align_to_multiple<vcl_size_t>(new_size, dense_padding_size);
std::vector<NumericT> temp(size_);
if (preserve && size_ > 0)
fast_copy(*this, temp);
temp.resize(new_size); //drop all entries above new_size
temp.resize(new_internal_size); //enlarge to fit new internal size
if (new_internal_size != internal_size())
{
viennacl::backend::memory_create(elements_, sizeof(NumericT)*new_internal_size, ctx, NULL);
}
fast_copy(temp, *this);
size_ = new_size;
internal_size_ = viennacl::tools::align_to_multiple<size_type>(size_, dense_padding_size);
pad();
}
}
template<class NumericT, unsigned int AlignmentV>
class vector : public vector_base<NumericT>
{
typedef vector<NumericT, AlignmentV> self_type;
typedef vector_base<NumericT> base_type;
public:
typedef typename base_type::size_type size_type;
typedef typename base_type::difference_type difference_type;
/** @brief Default constructor in order to be compatible with various containers.
*/
explicit vector() : base_type() { /* Note: One must not call ::init() here because the vector might have been created globally before the backend has become available */ }
/** @brief An explicit constructor for the vector, allocating the given amount of memory (plus a padding specified by 'AlignmentV')
*
* @param vec_size The length (i.e. size) of the vector.
*/
explicit vector(size_type vec_size) : base_type(vec_size) {}
explicit vector(size_type vec_size, viennacl::context ctx) : base_type(vec_size, ctx) {}
explicit vector(NumericT * ptr_to_mem, viennacl::memory_types mem_type, size_type vec_size, size_type start = 0, size_type stride = 1)
: base_type(ptr_to_mem, mem_type, vec_size, start, stride) {}
#ifdef VIENNACL_WITH_OPENCL
/** @brief Create a vector from existing OpenCL memory
*
* Note: The provided memory must take an eventual AlignmentV into account, i.e. existing_mem must be at least of size internal_size()!
* This is trivially the case with the default alignment, but should be considered when using vector<> with an alignment parameter not equal to 1.
*
* @param existing_mem An OpenCL handle representing the memory
* @param vec_size The size of the vector.
*/
explicit vector(cl_mem existing_mem, size_type vec_size, size_type start = 0, size_type stride = 1) : base_type(existing_mem, vec_size, start, stride) {}
/** @brief An explicit constructor for the vector, allocating the given amount of memory (plus a padding specified by 'AlignmentV') and the OpenCL context provided
*
* @param vec_size The length (i.e. size) of the vector.
* @param ctx The context
*/
explicit vector(size_type vec_size, viennacl::ocl::context const & ctx) : base_type(vec_size, ctx) {}
#endif
template<typename LHS, typename RHS, typename OP>
vector(vector_expression<const LHS, const RHS, OP> const & proxy) : base_type(proxy) {}
vector(const base_type & v) : base_type(v.size(), viennacl::traits::context(v))
{
if (v.size() > 0)
base_type::operator=(v);
}
vector(const self_type & v) : base_type(v.size(), viennacl::traits::context(v))
{
if (v.size() > 0)
base_type::operator=(v);
}
/** @brief Creates the vector from the supplied unit vector. */
vector(unit_vector<NumericT> const & v) : base_type(v.size())
{
if (v.size() > 0)
this->operator()(v.index()) = NumericT(1);;
}
/** @brief Creates the vector from the supplied zero vector. */
vector(zero_vector<NumericT> const & v) : base_type(v.size(), v.context())
{
if (v.size() > 0)
viennacl::linalg::vector_assign(*this, NumericT(0.0));
}
/** @brief Creates the vector from the supplied scalar vector. */
vector(scalar_vector<NumericT> const & v) : base_type(v.size(), v.context())
{
if (v.size() > 0)
viennacl::linalg::vector_assign(*this, v[0]);
}
// the following is used to circumvent an issue with Clang 3.0 when 'using base_type::operator=;' directly
template<typename T>
self_type & operator=(T const & other)
{
base_type::operator=(other);
return *this;
}
using base_type::operator+=;
using base_type::operator-=;
//enlarge or reduce allocated memory and set unused memory to zero
/** @brief Resizes the allocated memory for the vector. Pads the memory to be a multiple of 'AlignmentV'
*
* @param new_size The new size of the vector
* @param preserve If true, old entries of the vector are preserved, otherwise eventually discarded.
*/
void resize(size_type new_size, bool preserve = true)
{
base_type::resize(new_size, preserve);
}
void resize(size_type new_size, viennacl::context ctx, bool preserve = true)
{
base_type::resize(new_size, ctx, preserve);
}
/** @brief Swaps the handles of two vectors by swapping the OpenCL handles only, no data copy
*/
self_type & fast_swap(self_type & other)
{
base_type::fast_swap(other);
return *this;
}
void switch_memory_context(viennacl::context new_ctx)
{
base_type::switch_memory_context(new_ctx);
}
}; //vector
/** @brief Tuple class holding pointers to multiple vectors. Mainly used as a temporary object returned from viennacl::tie(). */
template<typename ScalarT>
class vector_tuple
{
typedef vector_base<ScalarT> VectorType;
public:
// 2 vectors
vector_tuple(VectorType const & v0, VectorType const & v1) : const_vectors_(2), non_const_vectors_()
{
const_vectors_[0] = &v0;
const_vectors_[1] = &v1;
}
vector_tuple(VectorType & v0, VectorType & v1) : const_vectors_(2), non_const_vectors_(2)
{
const_vectors_[0] = &v0; non_const_vectors_[0] = &v0;
const_vectors_[1] = &v1; non_const_vectors_[1] = &v1;
}
// 3 vectors
vector_tuple(VectorType const & v0, VectorType const & v1, VectorType const & v2) : const_vectors_(3), non_const_vectors_()
{
const_vectors_[0] = &v0;
const_vectors_[1] = &v1;
const_vectors_[2] = &v2;
}
vector_tuple(VectorType & v0, VectorType & v1, VectorType & v2) : const_vectors_(3), non_const_vectors_(3)
{
const_vectors_[0] = &v0; non_const_vectors_[0] = &v0;
const_vectors_[1] = &v1; non_const_vectors_[1] = &v1;
const_vectors_[2] = &v2; non_const_vectors_[2] = &v2;
}
// 4 vectors
vector_tuple(VectorType const & v0, VectorType const & v1, VectorType const & v2, VectorType const & v3) : const_vectors_(4), non_const_vectors_()
{
const_vectors_[0] = &v0;
const_vectors_[1] = &v1;
const_vectors_[2] = &v2;
const_vectors_[3] = &v3;
}
vector_tuple(VectorType & v0, VectorType & v1, VectorType & v2, VectorType & v3) : const_vectors_(4), non_const_vectors_(4)
{
const_vectors_[0] = &v0; non_const_vectors_[0] = &v0;
const_vectors_[1] = &v1; non_const_vectors_[1] = &v1;
const_vectors_[2] = &v2; non_const_vectors_[2] = &v2;
const_vectors_[3] = &v3; non_const_vectors_[3] = &v3;
}
// add more overloads here
// generic interface:
vector_tuple(std::vector<VectorType const *> const & vecs) : const_vectors_(vecs.size()), non_const_vectors_()
{
for (vcl_size_t i=0; i<vecs.size(); ++i)
const_vectors_[i] = vecs[i];
}
vector_tuple(std::vector<VectorType *> const & vecs) : const_vectors_(vecs.size()), non_const_vectors_(vecs.size())
{
for (vcl_size_t i=0; i<vecs.size(); ++i)
{
const_vectors_[i] = vecs[i];
non_const_vectors_[i] = vecs[i];
}
}
vcl_size_t size() const { return non_const_vectors_.size(); }
vcl_size_t const_size() const { return const_vectors_.size(); }
VectorType & at(vcl_size_t i) const { return *(non_const_vectors_.at(i)); }
VectorType const & const_at(vcl_size_t i) const { return *(const_vectors_.at(i)); }
private:
std::vector<VectorType const *> const_vectors_;
std::vector<VectorType *> non_const_vectors_;
};
// 2 args
template<typename ScalarT>
vector_tuple<ScalarT> tie(vector_base<ScalarT> const & v0, vector_base<ScalarT> const & v1) { return vector_tuple<ScalarT>(v0, v1); }
template<typename ScalarT>
vector_tuple<ScalarT> tie(vector_base<ScalarT> & v0, vector_base<ScalarT> & v1) { return vector_tuple<ScalarT>(v0, v1); }
// 3 args
template<typename ScalarT>
vector_tuple<ScalarT> tie(vector_base<ScalarT> const & v0, vector_base<ScalarT> const & v1, vector_base<ScalarT> const & v2) { return vector_tuple<ScalarT>(v0, v1, v2); }
template<typename ScalarT>
vector_tuple<ScalarT> tie(vector_base<ScalarT> & v0, vector_base<ScalarT> & v1, vector_base<ScalarT> & v2) { return vector_tuple<ScalarT>(v0, v1, v2); }
// 4 args
template<typename ScalarT>
vector_tuple<ScalarT> tie(vector_base<ScalarT> const & v0, vector_base<ScalarT> const & v1, vector_base<ScalarT> const & v2, vector_base<ScalarT> const & v3)
{
return vector_tuple<ScalarT>(v0, v1, v2, v3);
}
template<typename ScalarT>
vector_tuple<ScalarT> tie(vector_base<ScalarT> & v0, vector_base<ScalarT> & v1, vector_base<ScalarT> & v2, vector_base<ScalarT> & v3)
{
return vector_tuple<ScalarT>(v0, v1, v2, v3);
}
// 5 args
template<typename ScalarT>
vector_tuple<ScalarT> tie(vector_base<ScalarT> const & v0,
vector_base<ScalarT> const & v1,
vector_base<ScalarT> const & v2,
vector_base<ScalarT> const & v3,
vector_base<ScalarT> const & v4)
{
typedef vector_base<ScalarT> const * VectorPointerType;
std::vector<VectorPointerType> vec(5);
vec[0] = &v0;
vec[1] = &v1;
vec[2] = &v2;
vec[3] = &v3;
vec[4] = &v4;
return vector_tuple<ScalarT>(vec);
}
template<typename ScalarT>
vector_tuple<ScalarT> tie(vector_base<ScalarT> & v0,
vector_base<ScalarT> & v1,
vector_base<ScalarT> & v2,
vector_base<ScalarT> & v3,
vector_base<ScalarT> & v4)
{
typedef vector_base<ScalarT> * VectorPointerType;
std::vector<VectorPointerType> vec(5);
vec[0] = &v0;
vec[1] = &v1;
vec[2] = &v2;
vec[3] = &v3;
vec[4] = &v4;
return vector_tuple<ScalarT>(vec);
}
// TODO: Add more arguments to tie() here. Maybe use some preprocessor magic to accomplish this.
//
//////////////////// Copy from GPU to CPU //////////////////////////////////
//
/** @brief STL-like transfer of a GPU vector to the CPU. The cpu type is assumed to reside in a linear piece of memory, such as e.g. for std::vector.
*
* This method is faster than the plain copy() function, because entries are
* directly written to the cpu vector, starting with &(*cpu.begin()) However,
* keep in mind that the cpu type MUST represent a linear piece of
* memory, otherwise you will run into undefined behavior.
*
* @param gpu_begin GPU iterator pointing to the beginning of the gpu vector (STL-like)
* @param gpu_end GPU iterator pointing to the end of the vector (STL-like)
* @param cpu_begin Output iterator for the cpu vector. The cpu vector must be at least as long as the gpu vector!
*/
template<typename NumericT, unsigned int AlignmentV, typename CPU_ITERATOR>
void fast_copy(const const_vector_iterator<NumericT, AlignmentV> & gpu_begin,
const const_vector_iterator<NumericT, AlignmentV> & gpu_end,
CPU_ITERATOR cpu_begin )
{
if (gpu_begin != gpu_end)
{
if (gpu_begin.stride() == 1)
{
viennacl::backend::memory_read(gpu_begin.handle(),
sizeof(NumericT)*gpu_begin.offset(),
sizeof(NumericT)*gpu_begin.stride() * static_cast<vcl_size_t>(gpu_end - gpu_begin),
&(*cpu_begin));
}
else
{
vcl_size_t gpu_size = static_cast<vcl_size_t>(gpu_end - gpu_begin);
std::vector<NumericT> temp_buffer(gpu_begin.stride() * gpu_size);
viennacl::backend::memory_read(gpu_begin.handle(), sizeof(NumericT)*gpu_begin.offset(), sizeof(NumericT)*temp_buffer.size(), &(temp_buffer[0]));
for (vcl_size_t i=0; i<gpu_size; ++i)
{
(&(*cpu_begin))[i] = temp_buffer[i * gpu_begin.stride()];
}
}
}
}
/** @brief Transfer from a gpu vector to a cpu vector. Convenience wrapper for viennacl::linalg::fast_copy(gpu_vec.begin(), gpu_vec.end(), cpu_vec.begin());
*
* @param gpu_vec A gpu vector.
* @param cpu_vec The cpu vector. Type requirements: Output iterator pointing to entries linear in memory can be obtained via member function .begin()
*/
template<typename NumericT, typename CPUVECTOR>
void fast_copy(vector_base<NumericT> const & gpu_vec, CPUVECTOR & cpu_vec )
{
viennacl::fast_copy(gpu_vec.begin(), gpu_vec.end(), cpu_vec.begin());
}
/** @brief Asynchronous version of fast_copy(), copying data from device to host. The host iterator cpu_begin needs to reside in a linear piece of memory, such as e.g. for std::vector.
*
* This method allows for overlapping data transfer with host computation and returns immediately if the gpu vector has a unit-stride.
* In order to wait for the transfer to complete, use viennacl::backend::finish().
* Note that data pointed to by cpu_begin must not be modified prior to completion of the transfer.
*
* @param gpu_begin GPU iterator pointing to the beginning of the gpu vector (STL-like)
* @param gpu_end GPU iterator pointing to the end of the vector (STL-like)
* @param cpu_begin Output iterator for the cpu vector. The cpu vector must be at least as long as the gpu vector!
*/
template<typename NumericT, unsigned int AlignmentV, typename CPU_ITERATOR>
void async_copy(const const_vector_iterator<NumericT, AlignmentV> & gpu_begin,
const const_vector_iterator<NumericT, AlignmentV> & gpu_end,
CPU_ITERATOR cpu_begin )
{
if (gpu_begin != gpu_end)
{
if (gpu_begin.stride() == 1)
{
viennacl::backend::memory_read(gpu_begin.handle(),
sizeof(NumericT)*gpu_begin.offset(),
sizeof(NumericT)*gpu_begin.stride() * static_cast<vcl_size_t>(gpu_end - gpu_begin),
&(*cpu_begin),
true);
}
else // no async copy possible, so fall-back to fast_copy
fast_copy(gpu_begin, gpu_end, cpu_begin);
}
}
/** @brief Transfer from a gpu vector to a cpu vector. Convenience wrapper for viennacl::linalg::fast_copy(gpu_vec.begin(), gpu_vec.end(), cpu_vec.begin());
*
* @param gpu_vec A gpu vector.
* @param cpu_vec The cpu vector. Type requirements: Output iterator pointing to entries linear in memory can be obtained via member function .begin()
*/
template<typename NumericT, typename CPUVECTOR>
void async_copy(vector_base<NumericT> const & gpu_vec, CPUVECTOR & cpu_vec )
{
viennacl::async_copy(gpu_vec.begin(), gpu_vec.end(), cpu_vec.begin());
}
/** @brief STL-like transfer for the entries of a GPU vector to the CPU. The cpu type does not need to lie in a linear piece of memory.
*
* @param gpu_begin GPU constant iterator pointing to the beginning of the gpu vector (STL-like)
* @param gpu_end GPU constant iterator pointing to the end of the vector (STL-like)
* @param cpu_begin Output iterator for the cpu vector. The cpu vector must be at least as long as the gpu vector!
*/
template<typename NumericT, unsigned int AlignmentV, typename CPU_ITERATOR>
void copy(const const_vector_iterator<NumericT, AlignmentV> & gpu_begin,
const const_vector_iterator<NumericT, AlignmentV> & gpu_end,
CPU_ITERATOR cpu_begin )
{
assert(gpu_end - gpu_begin >= 0 && bool("Iterators incompatible"));
if (gpu_end - gpu_begin != 0)
{
std::vector<NumericT> temp_buffer(static_cast<vcl_size_t>(gpu_end - gpu_begin));
fast_copy(gpu_begin, gpu_end, temp_buffer.begin());
//now copy entries to cpu_vec:
std::copy(temp_buffer.begin(), temp_buffer.end(), cpu_begin);
}
}
/** @brief STL-like transfer for the entries of a GPU vector to the CPU. The cpu type does not need to lie in a linear piece of memory.
*
* @param gpu_begin GPU iterator pointing to the beginning of the gpu vector (STL-like)
* @param gpu_end GPU iterator pointing to the end of the vector (STL-like)
* @param cpu_begin Output iterator for the cpu vector. The cpu vector must be at least as long as the gpu vector!
*/
template<typename NumericT, unsigned int AlignmentV, typename CPU_ITERATOR>
void copy(const vector_iterator<NumericT, AlignmentV> & gpu_begin,
const vector_iterator<NumericT, AlignmentV> & gpu_end,
CPU_ITERATOR cpu_begin )
{
viennacl::copy(const_vector_iterator<NumericT, AlignmentV>(gpu_begin),
const_vector_iterator<NumericT, AlignmentV>(gpu_end),
cpu_begin);
}
/** @brief Transfer from a gpu vector to a cpu vector. Convenience wrapper for viennacl::linalg::copy(gpu_vec.begin(), gpu_vec.end(), cpu_vec.begin());
*
* @param gpu_vec A gpu vector
* @param cpu_vec The cpu vector. Type requirements: Output iterator can be obtained via member function .begin()
*/
template<typename NumericT, typename CPUVECTOR>
void copy(vector_base<NumericT> const & gpu_vec, CPUVECTOR & cpu_vec )
{
viennacl::copy(gpu_vec.begin(), gpu_vec.end(), cpu_vec.begin());
}
#ifdef VIENNACL_WITH_EIGEN
template<typename NumericT, unsigned int AlignmentV>
void copy(vector<NumericT, AlignmentV> const & gpu_vec,
Eigen::Matrix<NumericT, Eigen::Dynamic, 1> & eigen_vec)
{
viennacl::fast_copy(gpu_vec.begin(), gpu_vec.end(), &(eigen_vec[0]));
}
template<typename NumericT, unsigned int AlignmentV, int EigenMapTypeV, typename EigenStrideT>
void copy(vector<NumericT, AlignmentV> const & gpu_vec,
Eigen::Map<Eigen::Matrix<NumericT, Eigen::Dynamic, 1>, EigenMapTypeV, EigenStrideT> & eigen_vec)
{
viennacl::fast_copy(gpu_vec.begin(), gpu_vec.end(), &(eigen_vec[0]));
}
#endif
//
//////////////////// Copy from CPU to GPU //////////////////////////////////
//
/** @brief STL-like transfer of a CPU vector to the GPU. The cpu type is assumed to reside in a linear piece of memory, such as e.g. for std::vector.
*
* This method is faster than the plain copy() function, because entries are
* directly read from the cpu vector, starting with &(*cpu.begin()). However,
* keep in mind that the cpu type MUST represent a linear piece of
* memory, otherwise you will run into undefined behavior.
*
* @param cpu_begin CPU iterator pointing to the beginning of the cpu vector (STL-like)
* @param cpu_end CPU iterator pointing to the end of the vector (STL-like)
* @param gpu_begin Output iterator for the gpu vector. The gpu iterator must be incrementable (cpu_end - cpu_begin) times, otherwise the result is undefined.
*/
template<typename CPU_ITERATOR, typename NumericT, unsigned int AlignmentV>
void fast_copy(CPU_ITERATOR const & cpu_begin,
CPU_ITERATOR const & cpu_end,
vector_iterator<NumericT, AlignmentV> gpu_begin)
{
if (cpu_end - cpu_begin > 0)
{
if (gpu_begin.stride() == 1)
{
viennacl::backend::memory_write(gpu_begin.handle(),
sizeof(NumericT)*gpu_begin.offset(),
sizeof(NumericT)*gpu_begin.stride() * static_cast<vcl_size_t>(cpu_end - cpu_begin), &(*cpu_begin));
}
else //writing to slice:
{
vcl_size_t cpu_size = static_cast<vcl_size_t>(cpu_end - cpu_begin);
std::vector<NumericT> temp_buffer(gpu_begin.stride() * cpu_size);
viennacl::backend::memory_read(gpu_begin.handle(), sizeof(NumericT)*gpu_begin.offset(), sizeof(NumericT)*temp_buffer.size(), &(temp_buffer[0]));
for (vcl_size_t i=0; i<cpu_size; ++i)
temp_buffer[i * gpu_begin.stride()] = (&(*cpu_begin))[i];
viennacl::backend::memory_write(gpu_begin.handle(), sizeof(NumericT)*gpu_begin.offset(), sizeof(NumericT)*temp_buffer.size(), &(temp_buffer[0]));
}
}
}
/** @brief Transfer from a cpu vector to a gpu vector. Convenience wrapper for viennacl::linalg::fast_copy(cpu_vec.begin(), cpu_vec.end(), gpu_vec.begin());
*
* @param cpu_vec A cpu vector. Type requirements: Iterator can be obtained via member function .begin() and .end()
* @param gpu_vec The gpu vector.
*/
template<typename CPUVECTOR, typename NumericT>
void fast_copy(const CPUVECTOR & cpu_vec, vector_base<NumericT> & gpu_vec)
{
viennacl::fast_copy(cpu_vec.begin(), cpu_vec.end(), gpu_vec.begin());
}
/** @brief Asynchronous version of fast_copy(), copying data from host to device. The host iterator cpu_begin needs to reside in a linear piece of memory, such as e.g. for std::vector.
*
* This method allows for overlapping data transfer with host computation and returns immediately if the gpu vector has a unit-stride.
* In order to wait for the transfer to complete, use viennacl::backend::finish().
* Note that data pointed to by cpu_begin must not be modified prior to completion of the transfer.
*
* @param cpu_begin CPU iterator pointing to the beginning of the cpu vector (STL-like)
* @param cpu_end CPU iterator pointing to the end of the vector (STL-like)
* @param gpu_begin Output iterator for the gpu vector. The gpu iterator must be incrementable (cpu_end - cpu_begin) times, otherwise the result is undefined.
*/
template<typename CPU_ITERATOR, typename NumericT, unsigned int AlignmentV>
void async_copy(CPU_ITERATOR const & cpu_begin,
CPU_ITERATOR const & cpu_end,
vector_iterator<NumericT, AlignmentV> gpu_begin)
{
if (cpu_end - cpu_begin > 0)
{
if (gpu_begin.stride() == 1)
{
viennacl::backend::memory_write(gpu_begin.handle(),
sizeof(NumericT)*gpu_begin.offset(),
sizeof(NumericT)*gpu_begin.stride() * static_cast<vcl_size_t>(cpu_end - cpu_begin), &(*cpu_begin),
true);
}
else // fallback to blocking copy. There's nothing we can do to prevent this
fast_copy(cpu_begin, cpu_end, gpu_begin);
}
}
/** @brief Transfer from a cpu vector to a gpu vector. Convenience wrapper for viennacl::linalg::fast_copy(cpu_vec.begin(), cpu_vec.end(), gpu_vec.begin());
*
* @param cpu_vec A cpu vector. Type requirements: Iterator can be obtained via member function .begin() and .end()
* @param gpu_vec The gpu vector.
*/
template<typename CPUVECTOR, typename NumericT>
void async_copy(const CPUVECTOR & cpu_vec, vector_base<NumericT> & gpu_vec)
{
viennacl::async_copy(cpu_vec.begin(), cpu_vec.end(), gpu_vec.begin());
}
//from cpu to gpu. Safe assumption: cpu_vector does not necessarily occupy a linear memory segment, but is not larger than the allocated memory on the GPU
/** @brief STL-like transfer for the entries of a GPU vector to the CPU. The cpu type does not need to lie in a linear piece of memory.
*
* @param cpu_begin CPU iterator pointing to the beginning of the gpu vector (STL-like)
* @param cpu_end CPU iterator pointing to the end of the vector (STL-like)
* @param gpu_begin Output iterator for the gpu vector. The gpu vector must be at least as long as the cpu vector!
*/
template<typename NumericT, unsigned int AlignmentV, typename CPU_ITERATOR>
void copy(CPU_ITERATOR const & cpu_begin,
CPU_ITERATOR const & cpu_end,
vector_iterator<NumericT, AlignmentV> gpu_begin)
{
assert(cpu_end - cpu_begin > 0 && bool("Iterators incompatible"));
if (cpu_begin != cpu_end)
{
//we require that the size of the gpu_vector is larger or equal to the cpu-size
std::vector<NumericT> temp_buffer(static_cast<vcl_size_t>(cpu_end - cpu_begin));
std::copy(cpu_begin, cpu_end, temp_buffer.begin());
viennacl::fast_copy(temp_buffer.begin(), temp_buffer.end(), gpu_begin);
}
}
// for things like copy(std_vec.begin(), std_vec.end(), vcl_vec.begin() + 1);
/** @brief Transfer from a host vector object to a ViennaCL vector proxy. Requires the vector proxy to have the necessary size. Convenience wrapper for viennacl::linalg::copy(cpu_vec.begin(), cpu_vec.end(), gpu_vec.begin());
*
* @param cpu_vec A cpu vector. Type requirements: Iterator can be obtained via member function .begin() and .end()
* @param gpu_vec The gpu vector.
*/
template<typename HostVectorT, typename T>
void copy(HostVectorT const & cpu_vec, vector_base<T> & gpu_vec)
{
viennacl::copy(cpu_vec.begin(), cpu_vec.end(), gpu_vec.begin());
}
/** @brief Transfer from a host vector object to a ViennaCL vector. Resizes the ViennaCL vector if it has zero size. Convenience wrapper for viennacl::linalg::copy(cpu_vec.begin(), cpu_vec.end(), gpu_vec.begin());
*
* @param cpu_vec A host vector. Type requirements: Iterator can be obtained via member function .begin() and .end()
* @param gpu_vec The gpu (ViennaCL) vector.
*/
template<typename HostVectorT, typename T, unsigned int AlignmentV>
void copy(HostVectorT const & cpu_vec, vector<T, AlignmentV> & gpu_vec)
{
if (gpu_vec.size() == 0)
gpu_vec.resize(static_cast<vcl_size_t>(cpu_vec.end() - cpu_vec.begin()));
viennacl::copy(cpu_vec.begin(), cpu_vec.end(), gpu_vec.begin());
}
#ifdef VIENNACL_WITH_EIGEN
template<typename NumericT, unsigned int AlignmentV>
void copy(Eigen::Matrix<NumericT, Eigen::Dynamic, 1> const & eigen_vec,
vector<NumericT, AlignmentV> & gpu_vec)
{
viennacl::fast_copy(eigen_vec.data(), eigen_vec.data() + eigen_vec.size(), gpu_vec.begin());
}
template<typename NumericT, int EigenMapTypeV, typename EigenStrideT, unsigned int AlignmentV>
void copy(Eigen::Map<Eigen::Matrix<NumericT, Eigen::Dynamic, 1>, EigenMapTypeV, EigenStrideT> const & eigen_vec,
vector<NumericT, AlignmentV> & gpu_vec)
{
viennacl::fast_copy(eigen_vec.data(), eigen_vec.data() + eigen_vec.size(), gpu_vec.begin());
}
#endif
//
//////////////////// Copy from GPU to GPU //////////////////////////////////
//
/** @brief Copy (parts of a) GPU vector to another GPU vector
*
* @param gpu_src_begin GPU iterator pointing to the beginning of the gpu vector (STL-like)
* @param gpu_src_end GPU iterator pointing to the end of the vector (STL-like)
* @param gpu_dest_begin Output iterator for the gpu vector. The gpu_dest vector must be at least as long as the gpu_src vector!
*/
template<typename NumericT, unsigned int AlignmentV_SRC, unsigned int AlignmentV_DEST>
void copy(const_vector_iterator<NumericT, AlignmentV_SRC> const & gpu_src_begin,
const_vector_iterator<NumericT, AlignmentV_SRC> const & gpu_src_end,
vector_iterator<NumericT, AlignmentV_DEST> gpu_dest_begin)
{
assert(gpu_src_end - gpu_src_begin >= 0);
assert(gpu_src_begin.stride() == 1 && bool("ViennaCL ERROR: copy() for GPU->GPU not implemented for slices! Use operator= instead for the moment."));
if (gpu_src_begin.stride() == 1 && gpu_dest_begin.stride() == 1)
{
if (gpu_src_begin != gpu_src_end)
viennacl::backend::memory_copy(gpu_src_begin.handle(), gpu_dest_begin.handle(),
sizeof(NumericT) * gpu_src_begin.offset(),
sizeof(NumericT) * gpu_dest_begin.offset(),
sizeof(NumericT) * (gpu_src_end.offset() - gpu_src_begin.offset()));
}
else
{
assert( false && bool("not implemented yet"));
}
}
/** @brief Copy (parts of a) GPU vector to another GPU vector
*
* @param gpu_src_begin GPU iterator pointing to the beginning of the gpu vector (STL-like)
* @param gpu_src_end GPU iterator pointing to the end of the vector (STL-like)
* @param gpu_dest_begin Output iterator for the gpu vector. The gpu vector must be at least as long as the cpu vector!
*/
template<typename NumericT, unsigned int AlignmentV_SRC, unsigned int AlignmentV_DEST>
void copy(vector_iterator<NumericT, AlignmentV_SRC> const & gpu_src_begin,
vector_iterator<NumericT, AlignmentV_SRC> const & gpu_src_end,
vector_iterator<NumericT, AlignmentV_DEST> gpu_dest_begin)
{
viennacl::copy(static_cast<const_vector_iterator<NumericT, AlignmentV_SRC> >(gpu_src_begin),
static_cast<const_vector_iterator<NumericT, AlignmentV_SRC> >(gpu_src_end),
gpu_dest_begin);
}
/** @brief Transfer from a ViennaCL vector to another ViennaCL vector. Convenience wrapper for viennacl::linalg::copy(gpu_src_vec.begin(), gpu_src_vec.end(), gpu_dest_vec.begin());
*
* @param gpu_src_vec A gpu vector
* @param gpu_dest_vec The cpu vector. Type requirements: Output iterator can be obtained via member function .begin()
*/
template<typename NumericT, unsigned int AlignmentV_SRC, unsigned int AlignmentV_DEST>
void copy(vector<NumericT, AlignmentV_SRC> const & gpu_src_vec,
vector<NumericT, AlignmentV_DEST> & gpu_dest_vec )
{
viennacl::copy(gpu_src_vec.begin(), gpu_src_vec.end(), gpu_dest_vec.begin());
}
//global functions for handling vectors:
/** @brief Output stream. Output format is ublas compatible.
* @param os STL output stream
* @param val The vector that should be printed
*/
template<typename T>
std::ostream & operator<<(std::ostream & os, vector_base<T> const & val)
{
std::vector<T> tmp(val.size());
viennacl::copy(val.begin(), val.end(), tmp.begin());
os << "[" << val.size() << "](";
for (typename std::vector<T>::size_type i=0; i<val.size(); ++i)
{
if (i > 0)
os << ",";
os << tmp[i];
}
os << ")";
return os;
}
template<typename LHS, typename RHS, typename OP>
std::ostream & operator<<(std::ostream & os, vector_expression<LHS, RHS, OP> const & proxy)
{
typedef typename viennacl::result_of::cpu_value_type<typename LHS::value_type>::type ScalarType;
viennacl::vector<ScalarType> result = proxy;
os << result;
return os;
}
/** @brief Swaps the contents of two vectors, data is copied
*
* @param vec1 The first vector
* @param vec2 The second vector
*/
template<typename T>
void swap(vector_base<T> & vec1, vector_base<T> & vec2)
{
viennacl::linalg::vector_swap(vec1, vec2);
}
/** @brief Swaps the content of two vectors by swapping OpenCL handles only, NO data is copied
*
* @param v1 The first vector
* @param v2 The second vector
*/
template<typename NumericT, unsigned int AlignmentV>
vector<NumericT, AlignmentV> & fast_swap(vector<NumericT, AlignmentV> & v1,
vector<NumericT, AlignmentV> & v2)
{
return v1.fast_swap(v2);
}
//
//
////////// operations /////////////////////////////////////////////////////////////////////////////////
//
//
//
// operator *=
//
/** @brief Scales this vector by a GPU scalar value
*/
template<typename T, typename S1>
typename viennacl::enable_if< viennacl::is_any_scalar<S1>::value,
vector_base<T> &
>::type
operator *= (vector_base<T> & v1, S1 const & gpu_val)
{
bool flip_sign = viennacl::is_flip_sign_scalar<S1>::value;
if (v1.size() > 0)
viennacl::linalg::av(v1,
v1, gpu_val, 1, false, flip_sign);
return v1;
}
//
// operator /=
//
/** @brief Scales this vector by a GPU scalar value
*/
template<typename T, typename S1>
typename viennacl::enable_if< viennacl::is_any_scalar<S1>::value,
vector_base<T> &
>::type
operator /= (vector_base<T> & v1, S1 const & gpu_val)
{
bool flip_sign = viennacl::is_flip_sign_scalar<S1>::value;
if (v1.size() > 0)
viennacl::linalg::av(v1,
v1, gpu_val, 1, true, flip_sign);
return v1;
}
//
// operator +
//
/** @brief Operator overload for the addition of two vector expressions.
*
* @param proxy1 Left hand side vector expression
* @param proxy2 Right hand side vector expression
*/
template<typename LHS1, typename RHS1, typename OP1,
typename LHS2, typename RHS2, typename OP2>
vector_expression< const vector_expression< LHS1, RHS1, OP1>,
const vector_expression< LHS2, RHS2, OP2>,
viennacl::op_add>
operator + (vector_expression<LHS1, RHS1, OP1> const & proxy1,
vector_expression<LHS2, RHS2, OP2> const & proxy2)
{
assert(proxy1.size() == proxy2.size() && bool("Incompatible vector sizes!"));
return vector_expression< const vector_expression<LHS1, RHS1, OP1>,
const vector_expression<LHS2, RHS2, OP2>,
viennacl::op_add>(proxy1, proxy2);
}
/** @brief Operator overload for the addition of a vector expression with a vector or another vector expression. This is the default implementation for all cases that are too complex in order to be covered within a single kernel, hence a temporary vector is created.
*
* @param proxy Left hand side vector expression
* @param vec Right hand side vector (also -range and -slice is allowed)
*/
template<typename LHS, typename RHS, typename OP, typename T>
vector_expression< const vector_expression<LHS, RHS, OP>,
const vector_base<T>,
viennacl::op_add>
operator + (vector_expression<LHS, RHS, OP> const & proxy,
vector_base<T> const & vec)
{
assert(proxy.size() == vec.size() && bool("Incompatible vector sizes!"));
return vector_expression< const vector_expression<LHS, RHS, OP>,
const vector_base<T>,
viennacl::op_add>(proxy, vec);
}
/** @brief Operator overload for the addition of a vector with a vector expression. This is the default implementation for all cases that are too complex in order to be covered within a single kernel, hence a temporary vector is created.
*
* @param proxy Left hand side vector expression
* @param vec Right hand side vector (also -range and -slice is allowed)
*/
template<typename T, typename LHS, typename RHS, typename OP>
vector_expression< const vector_base<T>,
const vector_expression<LHS, RHS, OP>,
viennacl::op_add>
operator + (vector_base<T> const & vec,
vector_expression<LHS, RHS, OP> const & proxy)
{
assert(proxy.size() == vec.size() && bool("Incompatible vector sizes!"));
return vector_expression< const vector_base<T>,
const vector_expression<LHS, RHS, OP>,
viennacl::op_add>(vec, proxy);
}
/** @brief Returns an expression template object for adding up two vectors, i.e. v1 + v2
*/
template<typename T>
vector_expression< const vector_base<T>, const vector_base<T>, op_add>
operator + (const vector_base<T> & v1, const vector_base<T> & v2)
{
return vector_expression< const vector_base<T>, const vector_base<T>, op_add>(v1, v2);
}
//
// operator -
//
/** @brief Operator overload for the subtraction of two vector expressions.
*
* @param proxy1 Left hand side vector expression
* @param proxy2 Right hand side vector expression
*/
template<typename LHS1, typename RHS1, typename OP1,
typename LHS2, typename RHS2, typename OP2>
vector_expression< const vector_expression< LHS1, RHS1, OP1>,
const vector_expression< LHS2, RHS2, OP2>,
viennacl::op_sub>
operator - (vector_expression<LHS1, RHS1, OP1> const & proxy1,
vector_expression<LHS2, RHS2, OP2> const & proxy2)
{
assert(proxy1.size() == proxy2.size() && bool("Incompatible vector sizes!"));
return vector_expression< const vector_expression<LHS1, RHS1, OP1>,
const vector_expression<LHS2, RHS2, OP2>,
viennacl::op_sub>(proxy1, proxy2);
}
/** @brief Operator overload for the subtraction of a vector expression with a vector or another vector expression. This is the default implementation for all cases that are too complex in order to be covered within a single kernel, hence a temporary vector is created.
*
* @param proxy Left hand side vector expression
* @param vec Right hand side vector (also -range and -slice is allowed)
*/
template<typename LHS, typename RHS, typename OP, typename T>
vector_expression< const vector_expression<LHS, RHS, OP>,
const vector_base<T>,
viennacl::op_sub>
operator - (vector_expression<LHS, RHS, OP> const & proxy,
vector_base<T> const & vec)
{
assert(proxy.size() == vec.size() && bool("Incompatible vector sizes!"));
return vector_expression< const vector_expression<LHS, RHS, OP>,
const vector_base<T>,
viennacl::op_sub>(proxy, vec);
}
/** @brief Operator overload for the subtraction of a vector expression with a vector or another vector expression. This is the default implementation for all cases that are too complex in order to be covered within a single kernel, hence a temporary vector is created.
*
* @param proxy Left hand side vector expression
* @param vec Right hand side vector (also -range and -slice is allowed)
*/
template<typename T, typename LHS, typename RHS, typename OP>
vector_expression< const vector_base<T>,
const vector_expression<LHS, RHS, OP>,
viennacl::op_sub>
operator - (vector_base<T> const & vec,
vector_expression<LHS, RHS, OP> const & proxy)
{
assert(proxy.size() == vec.size() && bool("Incompatible vector sizes!"));
return vector_expression< const vector_base<T>,
const vector_expression<LHS, RHS, OP>,
viennacl::op_sub>(vec, proxy);
}
/** @brief Returns an expression template object for subtracting two vectors, i.e. v1 - v2
*/
template<typename T>
vector_expression< const vector_base<T>, const vector_base<T>, op_sub>
operator - (const vector_base<T> & v1, const vector_base<T> & v2)
{
return vector_expression< const vector_base<T>, const vector_base<T>, op_sub>(v1, v2);
}
//
// operator *
//
/** @brief Operator overload for the expression alpha * v1, where alpha is a host scalar (float or double) and v1 is a ViennaCL vector.
*
* @param value The host scalar (float or double)
* @param vec A ViennaCL vector
*/
template<typename S1, typename T>
typename viennacl::enable_if< viennacl::is_any_scalar<S1>::value,
vector_expression< const vector_base<T>, const S1, op_mult> >::type
operator * (S1 const & value, vector_base<T> const & vec)
{
return vector_expression< const vector_base<T>, const S1, op_mult>(vec, value);
}
/** @brief Operator overload for the expression alpha * v1, where alpha is a char
*
* @param value The host scalar (float or double)
* @param vec A ViennaCL vector
*/
template<typename T>
vector_expression< const vector_base<T>, const T, op_mult>
operator * (char value, vector_base<T> const & vec)
{
return vector_expression< const vector_base<T>, const T, op_mult>(vec, T(value));
}
/** @brief Operator overload for the expression alpha * v1, where alpha is a short
*
* @param value The host scalar (float or double)
* @param vec A ViennaCL vector
*/
template<typename T>
vector_expression< const vector_base<T>, const T, op_mult>
operator * (short value, vector_base<T> const & vec)
{
return vector_expression< const vector_base<T>, const T, op_mult>(vec, T(value));
}
/** @brief Operator overload for the expression alpha * v1, where alpha is a int
*
* @param value The host scalar (float or double)
* @param vec A ViennaCL vector
*/
template<typename T>
vector_expression< const vector_base<T>, const T, op_mult>
operator * (int value, vector_base<T> const & vec)
{
return vector_expression< const vector_base<T>, const T, op_mult>(vec, T(value));
}
/** @brief Operator overload for the expression alpha * v1, where alpha is a long
*
* @param value The host scalar (float or double)
* @param vec A ViennaCL vector
*/
template<typename T>
vector_expression< const vector_base<T>, const T, op_mult>
operator * (long value, vector_base<T> const & vec)
{
return vector_expression< const vector_base<T>, const T, op_mult>(vec, T(value));
}
/** @brief Operator overload for the expression alpha * v1, where alpha is a float
*
* @param value The host scalar (float or double)
* @param vec A ViennaCL vector
*/
template<typename T>
vector_expression< const vector_base<T>, const T, op_mult>
operator * (float value, vector_base<T> const & vec)
{
return vector_expression< const vector_base<T>, const T, op_mult>(vec, T(value));
}
/** @brief Operator overload for the expression alpha * v1, where alpha is a double
*
* @param value The host scalar (float or double)
* @param vec A ViennaCL vector
*/
template<typename T>
vector_expression< const vector_base<T>, const T, op_mult>
operator * (double value, vector_base<T> const & vec)
{
return vector_expression< const vector_base<T>, const T, op_mult>(vec, T(value));
}
/** @brief Operator overload for the expression alpha * v1, where alpha is a scalar expression and v1 is a ViennaCL vector.
*
* @param expr The scalar expression
* @param vec A ViennaCL vector
*/
template<typename LHS, typename RHS, typename OP, typename T>
vector_expression< const vector_base<T>, const scalar_expression<LHS, RHS, OP>, op_mult>
operator * (scalar_expression<LHS, RHS, OP> const & expr, vector_base<T> const & vec)
{
return vector_expression< const vector_base<T>, const scalar_expression<LHS, RHS, OP>, op_mult>(vec, expr);
}
/** @brief Scales the vector by a scalar 'alpha' and returns an expression template
*/
template<typename T, typename S1>
typename viennacl::enable_if< viennacl::is_any_scalar<S1>::value,
vector_expression< const vector_base<T>, const S1, op_mult> >::type
operator * (vector_base<T> const & vec, S1 const & value)
{
return vector_expression< const vector_base<T>, const S1, op_mult>(vec, value);
}
template<typename T>
vector_expression< const vector_base<T>, const T, op_mult>
operator * (vector_base<T> const & vec, T const & value)
{
return vector_expression< const vector_base<T>, const T, op_mult>(vec, value);
}
/** @brief Operator overload for the multiplication of a vector expression with a scalar from the right, e.g. (beta * vec1) * alpha. Here, beta * vec1 is wrapped into a vector_expression and then multiplied with alpha from the right.
*
* @param proxy Left hand side vector expression
* @param val Right hand side scalar
*/
template<typename LHS, typename RHS, typename OP, typename S1>
typename viennacl::enable_if< viennacl::is_any_scalar<S1>::value,
viennacl::vector_expression<const vector_expression<LHS, RHS, OP>, const S1, op_mult> >::type
operator * (vector_expression< LHS, RHS, OP> const & proxy,
S1 const & val)
{
return viennacl::vector_expression<const vector_expression<LHS, RHS, OP>, const S1, op_mult>(proxy, val);
}
/** @brief Operator overload for the multiplication of a vector expression with a ViennaCL scalar from the left, e.g. alpha * (beta * vec1). Here, beta * vec1 is wrapped into a vector_expression and then multiplied with alpha from the left.
*
* @param val Right hand side scalar
* @param proxy Left hand side vector expression
*/
template<typename S1, typename LHS, typename RHS, typename OP>
typename viennacl::enable_if< viennacl::is_any_scalar<S1>::value,
viennacl::vector_expression<const vector_expression<LHS, RHS, OP>, const S1, op_mult> >::type
operator * (S1 const & val,
vector_expression<LHS, RHS, OP> const & proxy)
{
return viennacl::vector_expression<const vector_expression<LHS, RHS, OP>, const S1, op_mult>(proxy, val);
}
//
// operator /
//
/** @brief Operator overload for the division of a vector expression by a scalar from the right, e.g. (beta * vec1) / alpha. Here, beta * vec1 is wrapped into a vector_expression and then divided by alpha.
*
* @param proxy Left hand side vector expression
* @param val Right hand side scalar
*/
template<typename S1, typename LHS, typename RHS, typename OP>
typename viennacl::enable_if< viennacl::is_any_scalar<S1>::value,
viennacl::vector_expression<const vector_expression<LHS, RHS, OP>, const S1, op_div> >::type
operator / (vector_expression< LHS, RHS, OP> const & proxy,
S1 const & val)
{
return viennacl::vector_expression<const vector_expression<LHS, RHS, OP>, const S1, op_div>(proxy, val);
}
/** @brief Returns an expression template for scaling the vector by a GPU scalar 'alpha'
*/
template<typename T, typename S1>
typename viennacl::enable_if< viennacl::is_any_scalar<S1>::value,
vector_expression< const vector_base<T>, const S1, op_div> >::type
operator / (vector_base<T> const & v1, S1 const & s1)
{
return vector_expression<const vector_base<T>, const S1, op_div>(v1, s1);
}
//
// Specify available operations:
//
/** \cond */
namespace linalg
{
namespace detail
{
// x = y
template<typename T>
struct op_executor<vector_base<T>, op_assign, vector_base<T> >
{
static void apply(vector_base<T> & lhs, vector_base<T> const & rhs)
{
viennacl::linalg::av(lhs, rhs, T(1), 1, false, false);
}
};
// x = inner_prod(z, {y0, y1, ...})
template<typename T>
struct op_executor<vector_base<T>, op_assign, vector_expression<const vector_base<T>, const vector_tuple<T>, op_inner_prod> >
{
static void apply(vector_base<T> & lhs, vector_expression<const vector_base<T>, const vector_tuple<T>, op_inner_prod> const & rhs)
{
viennacl::linalg::inner_prod_impl(rhs.lhs(), rhs.rhs(), lhs);
}
};
// x += y
template<typename T>
struct op_executor<vector_base<T>, op_inplace_add, vector_base<T> >
{
static void apply(vector_base<T> & lhs, vector_base<T> const & rhs)
{
viennacl::linalg::avbv(lhs, lhs, T(1), 1, false, false, rhs, T(1), 1, false, false);
}
};
// x -= y
template<typename T>
struct op_executor<vector_base<T>, op_inplace_sub, vector_base<T> >
{
static void apply(vector_base<T> & lhs, vector_base<T> const & rhs)
{
viennacl::linalg::avbv(lhs, lhs, T(1), 1, false, false, rhs, T(1), 1, false, true);
}
};
///////////// x OP y * alpha ////////////////////////
// x = alpha * y
template<typename T, typename ScalarType>
struct op_executor<vector_base<T>, op_assign, vector_expression<const vector_base<T>, const ScalarType, op_mult> >
{
// generic case: ScalarType is a scalar expression
template<typename LHS, typename RHS, typename OP>
static void apply(vector_base<T> & lhs, vector_expression<const vector_base<T>, const scalar_expression<LHS, RHS, OP>, op_mult> const & proxy)
{
T alpha = proxy.rhs();
viennacl::linalg::av(lhs, proxy.lhs(), alpha, 1, false, false);
}
static void apply(vector_base<T> & lhs, vector_expression<const vector_base<T>, const scalar<T>, op_mult> const & proxy)
{
viennacl::linalg::av(lhs, proxy.lhs(), proxy.rhs(), 1, false, false);
}
static void apply(vector_base<T> & lhs, vector_expression<const vector_base<T>, const T, op_mult> const & proxy)
{
viennacl::linalg::av(lhs, proxy.lhs(), proxy.rhs(), 1, false, false);
}
};
// x += alpha * y
template<typename T, typename ScalarType>
struct op_executor<vector_base<T>, op_inplace_add, vector_expression<const vector_base<T>, const ScalarType, op_mult> >
{
// generic case: ScalarType is a scalar expression
template<typename LHS, typename RHS, typename OP>
static void apply(vector_base<T> & lhs, vector_expression<const vector_base<T>, const scalar_expression<LHS, RHS, OP>, op_mult> const & proxy)
{
T alpha = proxy.rhs();
viennacl::linalg::avbv(lhs, lhs, T(1), 1, false, false, proxy.lhs(), alpha, 1, false, false);
}
static void apply(vector_base<T> & lhs, vector_expression<const vector_base<T>, const scalar<T>, op_mult> const & proxy)
{
viennacl::linalg::avbv(lhs, lhs, T(1), 1, false, false, proxy.lhs(), proxy.rhs(), 1, false, false);
}
static void apply(vector_base<T> & lhs, vector_expression<const vector_base<T>, const T, op_mult> const & proxy)
{
viennacl::linalg::avbv(lhs, lhs, T(1), 1, false, false, proxy.lhs(), proxy.rhs(), 1, false, false);
}
};
// x -= alpha * y
template<typename T, typename ScalarType>
struct op_executor<vector_base<T>, op_inplace_sub, vector_expression<const vector_base<T>, const ScalarType, op_mult> >
{
// generic case: ScalarType is a scalar expression
template<typename LHS, typename RHS, typename OP>
static void apply(vector_base<T> & lhs, vector_expression<const vector_base<T>, const scalar_expression<LHS, RHS, OP>, op_mult> const & proxy)
{
T alpha = proxy.rhs();
viennacl::linalg::avbv(lhs, lhs, T(1), 1, false, false, proxy.lhs(), alpha, 1, false, true);
}
static void apply(vector_base<T> & lhs, vector_expression<const vector_base<T>, const scalar<T>, op_mult> const & proxy)
{
viennacl::linalg::avbv(lhs, lhs, T(1), 1, false, false, proxy.lhs(), proxy.rhs(), 1, false, true);
}
static void apply(vector_base<T> & lhs, vector_expression<const vector_base<T>, const T, op_mult> const & proxy)
{
viennacl::linalg::avbv(lhs, lhs, T(1), 1, false, false, proxy.lhs(), proxy.rhs(), 1, false, true);
}
};
///////////// x OP vec_expr * alpha ////////////////////////
// x = alpha * vec_expr
template<typename T, typename LHS, typename RHS, typename OP, typename ScalarType>
struct op_executor<vector_base<T>, op_assign, vector_expression<const vector_expression<const LHS, const RHS, OP>, const ScalarType, op_mult> >
{
static void apply(vector_base<T> & lhs, vector_expression<const vector_expression<const LHS, const RHS, OP>, const ScalarType, op_mult> const & proxy)
{
vector<T> temp(proxy.lhs());
lhs = temp * proxy.rhs();
}
};
// x += alpha * vec_expr
template<typename T, typename LHS, typename RHS, typename OP, typename ScalarType>
struct op_executor<vector_base<T>, op_inplace_add, vector_expression<const vector_expression<const LHS, const RHS, OP>, const ScalarType, op_mult> >
{
static void apply(vector_base<T> & lhs, vector_expression<const vector_expression<const LHS, const RHS, OP>, const ScalarType, op_mult> const & proxy)
{
vector<T> temp(proxy.lhs());
lhs += temp * proxy.rhs();
}
};
// x -= alpha * vec_expr
template<typename T, typename LHS, typename RHS, typename OP, typename ScalarType>
struct op_executor<vector_base<T>, op_inplace_sub, vector_expression<const vector_expression<const LHS, const RHS, OP>, const ScalarType, op_mult> >
{
static void apply(vector_base<T> & lhs, vector_expression<const vector_expression<const LHS, const RHS, OP>, const ScalarType, op_mult> const & proxy)
{
vector<T> temp(proxy.lhs());
lhs -= temp * proxy.rhs();
}
};
///////////// x OP y / alpha ////////////////////////
// x = y / alpha
template<typename T, typename ScalarType>
struct op_executor<vector_base<T>, op_assign, vector_expression<const vector_base<T>, const ScalarType, op_div> >
{
static void apply(vector_base<T> & lhs, vector_expression<const vector_base<T>, const ScalarType, op_div> const & proxy)
{
viennacl::linalg::av(lhs, proxy.lhs(), proxy.rhs(), 1, true, false);
}
};
// x += y / alpha
template<typename T, typename ScalarType>
struct op_executor<vector_base<T>, op_inplace_add, vector_expression<const vector_base<T>, const ScalarType, op_div> >
{
static void apply(vector_base<T> & lhs, vector_expression<const vector_base<T>, const ScalarType, op_div> const & proxy)
{
viennacl::linalg::avbv(lhs, lhs, T(1), 1, false, false, proxy.lhs(), proxy.rhs(), 1, true, false);
}
};
// x -= y / alpha
template<typename T, typename ScalarType>
struct op_executor<vector_base<T>, op_inplace_sub, vector_expression<const vector_base<T>, const ScalarType, op_div> >
{
static void apply(vector_base<T> & lhs, vector_expression<const vector_base<T>, const ScalarType, op_div> const & proxy)
{
viennacl::linalg::avbv(lhs, lhs, T(1), 1, false, false, proxy.lhs(), proxy.rhs(), 1, true, true);
}
};
///////////// x OP vec_expr / alpha ////////////////////////
// x = vec_expr / alpha
template<typename T, typename LHS, typename RHS, typename OP, typename ScalarType>
struct op_executor<vector_base<T>, op_assign, vector_expression<const vector_expression<const LHS, const RHS, OP>, const ScalarType, op_div> >
{
static void apply(vector_base<T> & lhs, vector_expression<const vector_expression<const LHS, const RHS, OP>, const ScalarType, op_div> const & proxy)
{
vector<T> temp(proxy.lhs());
lhs = temp / proxy.rhs();
}
};
// x += vec_expr / alpha
template<typename T, typename LHS, typename RHS, typename OP, typename ScalarType>
struct op_executor<vector_base<T>, op_inplace_add, vector_expression<const vector_expression<const LHS, const RHS, OP>, const ScalarType, op_div> >
{
static void apply(vector_base<T> & lhs, vector_expression<const vector_expression<const LHS, const RHS, OP>, const ScalarType, op_div> const & proxy)
{
vector<T> temp(proxy.lhs());
lhs += temp / proxy.rhs();
}
};
// x -= vec_expr / alpha
template<typename T, typename LHS, typename RHS, typename OP, typename ScalarType>
struct op_executor<vector_base<T>, op_inplace_sub, vector_expression<const vector_expression<const LHS, const RHS, OP>, const ScalarType, op_div> >
{
static void apply(vector_base<T> & lhs, vector_expression<const vector_expression<const LHS, const RHS, OP>, const ScalarType, op_div> const & proxy)
{
vector<T> temp(proxy.lhs());
lhs -= temp / proxy.rhs();
}
};
// generic x = vec_expr1 + vec_expr2:
template<typename T, typename LHS, typename RHS>
struct op_executor<vector_base<T>, op_assign, vector_expression<const LHS, const RHS, op_add> >
{
// generic x = vec_expr1 + vec_expr2:
template<typename LHS1, typename RHS1>
static void apply(vector_base<T> & lhs, vector_expression<const LHS1, const RHS1, op_add> const & proxy)
{
bool op_aliasing_lhs = op_aliasing(lhs, proxy.lhs());
bool op_aliasing_rhs = op_aliasing(lhs, proxy.rhs());
if (op_aliasing_lhs || op_aliasing_rhs)
{
vector_base<T> temp(proxy.lhs());
op_executor<vector_base<T>, op_inplace_add, RHS>::apply(temp, proxy.rhs());
lhs = temp;
}
else
{
op_executor<vector_base<T>, op_assign, LHS>::apply(lhs, proxy.lhs());
op_executor<vector_base<T>, op_inplace_add, RHS>::apply(lhs, proxy.rhs());
}
}
// x = y + z
static void apply(vector_base<T> & lhs, vector_expression<const vector_base<T>, const vector_base<T>, op_add> const & proxy)
{
viennacl::linalg::avbv(lhs,
proxy.lhs(), T(1), 1, false, false,
proxy.rhs(), T(1), 1, false, false);
}
// x = alpha * y + z
static void apply(vector_base<T> & lhs, vector_expression<const vector_expression<const vector_base<T>, const T, op_mult>,
const vector_base<T>,
op_add> const & proxy)
{
viennacl::linalg::avbv(lhs,
proxy.lhs().lhs(), proxy.lhs().rhs(), 1, false, false,
proxy.rhs(), T(1), 1, false, false);
}
// x = y / alpha + z
static void apply(vector_base<T> & lhs, vector_expression<const vector_expression<const vector_base<T>, const T, op_div>,
const vector_base<T>,
op_add> const & proxy)
{
viennacl::linalg::avbv(lhs,
proxy.lhs().lhs(), proxy.lhs().rhs(), 1, true, false,
proxy.rhs(), T(1), 1, false, false);
}
// x = y + beta * z
static void apply(vector_base<T> & lhs, vector_expression<const vector_base<T>,
const vector_expression<const vector_base<T>, const T, op_mult>,
op_add> const & proxy)
{
viennacl::linalg::avbv(lhs,
proxy.lhs(), T(1), 1, false, false,
proxy.rhs().lhs(), proxy.rhs().rhs(), 1, false, false);
}
// x = y + z / beta
static void apply(vector_base<T> & lhs, vector_expression<const vector_base<T>,
const vector_expression<const vector_base<T>, const T, op_div>,
op_add> const & proxy)
{
viennacl::linalg::avbv(lhs,
proxy.lhs(), T(1), 1, false, false,
proxy.rhs().lhs(), proxy.rhs().rhs(), 1, true, false);
}
// x = alpha * y + beta * z
static void apply(vector_base<T> & lhs, vector_expression<const vector_expression<const vector_base<T>, const T, op_mult>,
const vector_expression<const vector_base<T>, const T, op_mult>,
op_add> const & proxy)
{
viennacl::linalg::avbv(lhs,
proxy.lhs().lhs(), proxy.lhs().rhs(), 1, false, false,
proxy.rhs().lhs(), proxy.rhs().rhs(), 1, false, false);
}
// x = alpha * y + z / beta
static void apply(vector_base<T> & lhs, vector_expression<const vector_expression<const vector_base<T>, const T, op_mult>,
const vector_expression<const vector_base<T>, const T, op_div>,
op_add> const & proxy)
{
viennacl::linalg::avbv(lhs,
proxy.lhs().lhs(), proxy.lhs().rhs(), 1, false, false,
proxy.rhs().lhs(), proxy.rhs().rhs(), 1, true, false);
}
// x = y / alpha + beta * z
static void apply(vector_base<T> & lhs, vector_expression<const vector_expression<const vector_base<T>, const T, op_div>,
const vector_expression<const vector_base<T>, const T, op_mult>,
op_add> const & proxy)
{
viennacl::linalg::avbv(lhs,
proxy.lhs().lhs(), proxy.lhs().rhs(), 1, true, false,
proxy.rhs().lhs(), proxy.rhs().rhs(), 1, false, false);
}
// x = y / alpha + z / beta
static void apply(vector_base<T> & lhs, vector_expression<const vector_expression<const vector_base<T>, const T, op_div>,
const vector_expression<const vector_base<T>, const T, op_div>,
op_add> const & proxy)
{
viennacl::linalg::avbv(lhs,
proxy.lhs().lhs(), proxy.lhs().rhs(), 1, true, false,
proxy.rhs().lhs(), proxy.rhs().rhs(), 1, true, false);
}
};
// generic x += vec_expr1 + vec_expr2:
template<typename T, typename LHS, typename RHS>
struct op_executor<vector_base<T>, op_inplace_add, vector_expression<const LHS, const RHS, op_add> >
{
// generic x += vec_expr1 + vec_expr2:
template<typename LHS1, typename RHS1>
static void apply(vector_base<T> & lhs, vector_expression<const LHS1, const RHS1, op_add> const & proxy)
{
bool op_aliasing_lhs = op_aliasing(lhs, proxy.lhs());
bool op_aliasing_rhs = op_aliasing(lhs, proxy.rhs());
if (op_aliasing_lhs || op_aliasing_rhs)
{
vector_base<T> temp(proxy.lhs());
op_executor<vector_base<T>, op_inplace_add, RHS>::apply(temp, proxy.rhs());
lhs += temp;
}
else
{
op_executor<vector_base<T>, op_inplace_add, LHS>::apply(lhs, proxy.lhs());
op_executor<vector_base<T>, op_inplace_add, RHS>::apply(lhs, proxy.rhs());
}
}
// x += y + z
static void apply(vector_base<T> & lhs, vector_expression<const vector_base<T>, const vector_base<T>, op_add> const & proxy)
{
viennacl::linalg::avbv_v(lhs,
proxy.lhs(), T(1), 1, false, false,
proxy.rhs(), T(1), 1, false, false);
}
// x += alpha * y + z
template<typename ScalarType>
static void apply(vector_base<T> & lhs, vector_expression<const vector_expression<const vector_base<T>, const ScalarType, op_mult>,
const vector_base<T>,
op_add> const & proxy)
{
viennacl::linalg::avbv_v(lhs,
proxy.lhs().lhs(), proxy.lhs().rhs(), 1, false, false,
proxy.rhs(), T(1), 1, false, false);
}
// x += y / alpha + z
template<typename ScalarType>
static void apply(vector_base<T> & lhs, vector_expression<const vector_expression<const vector_base<T>, const ScalarType, op_div>,
const vector_base<T>,
op_add> const & proxy)
{
viennacl::linalg::avbv_v(lhs,
proxy.lhs().lhs(), proxy.lhs().rhs(), 1, true, false,
proxy.rhs(), T(1), 1, false, false);
}
// x += y + beta * z
template<typename ScalarType>
static void apply(vector_base<T> & lhs, vector_expression<const vector_base<T>,
const vector_expression<const vector_base<T>, const ScalarType, op_mult>,
op_add> const & proxy)
{
viennacl::linalg::avbv_v(lhs,
proxy.lhs(), T(1), 1, false, false,
proxy.rhs().lhs(), proxy.rhs().rhs(), 1, false, false);
}
// x += y + z / beta
template<typename ScalarType>
static void apply(vector_base<T> & lhs, vector_expression<const vector_base<T>,
const vector_expression<const vector_base<T>, const ScalarType, op_div>,
op_add> const & proxy)
{
viennacl::linalg::avbv_v(lhs,
proxy.lhs(), T(1), 1, false, false,
proxy.rhs().lhs(), proxy.rhs().rhs(), 1, true, false);
}
// x += alpha * y + beta * z
template<typename ScalarType1, typename ScalarType2>
static void apply(vector_base<T> & lhs, vector_expression<const vector_expression<const vector_base<T>, const ScalarType1, op_mult>,
const vector_expression<const vector_base<T>, const ScalarType2, op_mult>,
op_add> const & proxy)
{
viennacl::linalg::avbv_v(lhs,
proxy.lhs().lhs(), proxy.lhs().rhs(), 1, false, false,
proxy.rhs().lhs(), proxy.rhs().rhs(), 1, false, false);
}
// x += alpha * y + z / beta
template<typename ScalarType1, typename ScalarType2>
static void apply(vector_base<T> & lhs, vector_expression<const vector_expression<const vector_base<T>, const ScalarType1, op_mult>,
const vector_expression<const vector_base<T>, const ScalarType2, op_div>,
op_add> const & proxy)
{
viennacl::linalg::avbv_v(lhs,
proxy.lhs().lhs(), proxy.lhs().rhs(), 1, false, false,
proxy.rhs().lhs(), proxy.rhs().rhs(), 1, true, false);
}
// x += y / alpha + beta * z
template<typename ScalarType1, typename ScalarType2>
static void apply(vector_base<T> & lhs, vector_expression<const vector_expression<const vector_base<T>, const ScalarType1, op_div>,
const vector_expression<const vector_base<T>, const ScalarType2, op_mult>,
op_add> const & proxy)
{
viennacl::linalg::avbv_v(lhs,
proxy.lhs().lhs(), proxy.lhs().rhs(), 1, true, false,
proxy.rhs().lhs(), proxy.rhs().rhs(), 1, false, false);
}
// x += y / alpha + z / beta
template<typename ScalarType1, typename ScalarType2>
static void apply(vector_base<T> & lhs, vector_expression<const vector_expression<const vector_base<T>, const ScalarType1, op_div>,
const vector_expression<const vector_base<T>, const ScalarType2, op_div>,
op_add> const & proxy)
{
viennacl::linalg::avbv_v(lhs,
proxy.lhs().lhs(), proxy.lhs().rhs(), 1, true, false,
proxy.rhs().lhs(), proxy.rhs().rhs(), 1, true, false);
}
};
// generic x -= vec_expr1 + vec_expr2:
template<typename T, typename LHS, typename RHS>
struct op_executor<vector_base<T>, op_inplace_sub, vector_expression<const LHS, const RHS, op_add> >
{
// generic x -= vec_expr1 + vec_expr2:
template<typename LHS1, typename RHS1>
static void apply(vector_base<T> & lhs, vector_expression<const LHS1, const RHS1, op_add> const & proxy)
{
bool op_aliasing_lhs = op_aliasing(lhs, proxy.lhs());
bool op_aliasing_rhs = op_aliasing(lhs, proxy.rhs());
if (op_aliasing_lhs || op_aliasing_rhs)
{
vector_base<T> temp(proxy.lhs());
op_executor<vector_base<T>, op_inplace_add, RHS>::apply(temp, proxy.rhs());
lhs -= temp;
}
else
{
op_executor<vector_base<T>, op_inplace_sub, LHS>::apply(lhs, proxy.lhs());
op_executor<vector_base<T>, op_inplace_sub, RHS>::apply(lhs, proxy.rhs());
}
}
// x -= y + z
static void apply(vector_base<T> & lhs, vector_expression<const vector_base<T>, const vector_base<T>, op_add> const & proxy)
{
viennacl::linalg::avbv_v(lhs,
proxy.lhs(), T(1), 1, false, true,
proxy.rhs(), T(1), 1, false, true);
}
// x -= alpha * y + z
template<typename ScalarType>
static void apply(vector_base<T> & lhs, vector_expression<const vector_expression<const vector_base<T>, const ScalarType, op_mult>,
const vector_base<T>,
op_add> const & proxy)
{
viennacl::linalg::avbv_v(lhs,
proxy.lhs().lhs(), proxy.lhs().rhs(), 1, false, true,
proxy.rhs(), T(1), 1, false, true);
}
// x -= y / alpha + z
template<typename ScalarType>
static void apply(vector_base<T> & lhs, vector_expression<const vector_expression<const vector_base<T>, const ScalarType, op_div>,
const vector_base<T>,
op_add> const & proxy)
{
viennacl::linalg::avbv_v(lhs,
proxy.lhs().lhs(), proxy.lhs().rhs(), 1, true, true,
proxy.rhs(), T(1), 1, false, true);
}
// x -= y + beta * z
template<typename ScalarType>
static void apply(vector_base<T> & lhs, vector_expression<const vector_base<T>,
const vector_expression<const vector_base<T>, const ScalarType, op_mult>,
op_add> const & proxy)
{
viennacl::linalg::avbv_v(lhs,
proxy.lhs(), T(1), 1, false, true,
proxy.rhs().lhs(), proxy.rhs().rhs(), 1, false, true);
}
// x -= y + z / beta
template<typename ScalarType>
static void apply(vector_base<T> & lhs, vector_expression<const vector_base<T>,
const vector_expression<const vector_base<T>, const ScalarType, op_div>,
op_add> const & proxy)
{
viennacl::linalg::avbv_v(lhs,
proxy.lhs(), T(1), 1, false, true,
proxy.rhs().lhs(), proxy.rhs().rhs(), 1, true, true);
}
// x -= alpha * y + beta * z
template<typename ScalarType1, typename ScalarType2>
static void apply(vector_base<T> & lhs, vector_expression<const vector_expression<const vector_base<T>, const ScalarType1, op_mult>,
const vector_expression<const vector_base<T>, const ScalarType2, op_mult>,
op_add> const & proxy)
{
viennacl::linalg::avbv_v(lhs,
proxy.lhs().lhs(), proxy.lhs().rhs(), 1, false, true,
proxy.rhs().lhs(), proxy.rhs().rhs(), 1, false, true);
}
// x -= alpha * y + z / beta
template<typename ScalarType1, typename ScalarType2>
static void apply(vector_base<T> & lhs, vector_expression<const vector_expression<const vector_base<T>, const ScalarType1, op_mult>,
const vector_expression<const vector_base<T>, const ScalarType2, op_div>,
op_add> const & proxy)
{
viennacl::linalg::avbv_v(lhs,
proxy.lhs().lhs(), proxy.lhs().rhs(), 1, false, true,
proxy.rhs().lhs(), proxy.rhs().rhs(), 1, true, true);
}
// x -= y / alpha + beta * z
template<typename ScalarType1, typename ScalarType2>
static void apply(vector_base<T> & lhs, vector_expression<const vector_expression<const vector_base<T>, const ScalarType1, op_div>,
const vector_expression<const vector_base<T>, const ScalarType2, op_mult>,
op_add> const & proxy)
{
viennacl::linalg::avbv_v(lhs,
proxy.lhs().lhs(), proxy.lhs().rhs(), 1, true, true,
proxy.rhs().lhs(), proxy.rhs().rhs(), 1, false, true);
}
// x -= y / alpha + z / beta
template<typename ScalarType1, typename ScalarType2>
static void apply(vector_base<T> & lhs, vector_expression<const vector_expression<const vector_base<T>, const ScalarType1, op_div>,
const vector_expression<const vector_base<T>, const ScalarType2, op_div>,
op_add> const & proxy)
{
viennacl::linalg::avbv_v(lhs,
proxy.lhs().lhs(), proxy.lhs().rhs(), 1, true, true,
proxy.rhs().lhs(), proxy.rhs().rhs(), 1, true, true);
}
};
///////////////////////
// generic x = vec_expr1 - vec_expr2:
template<typename T, typename LHS, typename RHS>
struct op_executor<vector_base<T>, op_assign, vector_expression<const LHS, const RHS, op_sub> >
{
// generic x = vec_expr1 - vec_expr2:
template<typename LHS1, typename RHS1>
static void apply(vector_base<T> & lhs, vector_expression<const LHS1, const RHS1, op_sub> const & proxy)
{
bool op_aliasing_lhs = op_aliasing(lhs, proxy.lhs());
bool op_aliasing_rhs = op_aliasing(lhs, proxy.rhs());
if (op_aliasing_lhs || op_aliasing_rhs)
{
vector_base<T> temp(proxy.lhs());
op_executor<vector_base<T>, op_inplace_sub, RHS>::apply(temp, proxy.rhs());
lhs = temp;
}
else
{
op_executor<vector_base<T>, op_assign, LHS>::apply(lhs, proxy.lhs());
op_executor<vector_base<T>, op_inplace_sub, RHS>::apply(lhs, proxy.rhs());
}
}
// x = y - z
static void apply(vector_base<T> & lhs, vector_expression<const vector_base<T>, const vector_base<T>, op_sub> const & proxy)
{
viennacl::linalg::avbv(lhs,
proxy.lhs(), T(1), 1, false, false,
proxy.rhs(), T(1), 1, false, true);
}
// x = alpha * y - z
template<typename ScalarType>
static void apply(vector_base<T> & lhs, vector_expression<const vector_expression<const vector_base<T>, const ScalarType, op_mult>,
const vector_base<T>,
op_sub> const & proxy)
{
viennacl::linalg::avbv(lhs,
proxy.lhs().lhs(), proxy.lhs().rhs(), 1, false, false,
proxy.rhs(), T(1), 1, false, true);
}
// x = y / alpha - z
template<typename ScalarType>
static void apply(vector_base<T> & lhs, vector_expression<const vector_expression<const vector_base<T>, const ScalarType, op_div>,
const vector_base<T>,
op_sub> const & proxy)
{
viennacl::linalg::avbv(lhs,
proxy.lhs().lhs(), proxy.lhs().rhs(), 1, true, false,
proxy.rhs(), T(1), 1, false, true);
}
// x = y - beta * z
template<typename ScalarType>
static void apply(vector_base<T> & lhs, vector_expression<const vector_base<T>,
const vector_expression<const vector_base<T>, const ScalarType, op_mult>,
op_sub> const & proxy)
{
viennacl::linalg::avbv(lhs,
proxy.lhs(), T(1), 1, false, false,
proxy.rhs().lhs(), proxy.rhs().rhs(), 1, false, true);
}
// x = y - z / beta
template<typename ScalarType>
static void apply(vector_base<T> & lhs, vector_expression<const vector_base<T>,
const vector_expression<const vector_base<T>, const ScalarType, op_div>,
op_sub> const & proxy)
{
viennacl::linalg::avbv(lhs,
proxy.lhs(), T(1), 1, false, false,
proxy.rhs().lhs(), proxy.rhs().rhs(), 1, true, true);
}
// x = alpha * y - beta * z
template<typename ScalarType1, typename ScalarType2>
static void apply(vector_base<T> & lhs, vector_expression<const vector_expression<const vector_base<T>, const ScalarType1, op_mult>,
const vector_expression<const vector_base<T>, const ScalarType2, op_mult>,
op_sub> const & proxy)
{
viennacl::linalg::avbv(lhs,
proxy.lhs().lhs(), proxy.lhs().rhs(), 1, false, false,
proxy.rhs().lhs(), proxy.rhs().rhs(), 1, false, true);
}
// x = alpha * y - z / beta
template<typename ScalarType1, typename ScalarType2>
static void apply(vector_base<T> & lhs, vector_expression<const vector_expression<const vector_base<T>, const ScalarType1, op_mult>,
const vector_expression<const vector_base<T>, const ScalarType2, op_div>,
op_sub> const & proxy)
{
viennacl::linalg::avbv(lhs,
proxy.lhs().lhs(), proxy.lhs().rhs(), 1, false, false,
proxy.rhs().lhs(), proxy.rhs().rhs(), 1, true, true);
}
// x = y / alpha - beta * z
template<typename ScalarType1, typename ScalarType2>
static void apply(vector_base<T> & lhs, vector_expression<const vector_expression<const vector_base<T>, const ScalarType1, op_div>,
const vector_expression<const vector_base<T>, const ScalarType2, op_mult>,
op_sub> const & proxy)
{
viennacl::linalg::avbv(lhs,
proxy.lhs().lhs(), proxy.lhs().rhs(), 1, true, false,
proxy.rhs().lhs(), proxy.rhs().rhs(), 1, false, true);
}
// x = y / alpha - z / beta
template<typename ScalarType1, typename ScalarType2>
static void apply(vector_base<T> & lhs, vector_expression<const vector_expression<const vector_base<T>, const ScalarType1, op_div>,
const vector_expression<const vector_base<T>, const ScalarType2, op_div>,
op_sub> const & proxy)
{
viennacl::linalg::avbv(lhs,
proxy.lhs().lhs(), proxy.lhs().rhs(), 1, true, false,
proxy.rhs().lhs(), proxy.rhs().rhs(), 1, true, true);
}
};
// generic x += vec_expr1 - vec_expr2:
template<typename T, typename LHS, typename RHS>
struct op_executor<vector_base<T>, op_inplace_add, vector_expression<const LHS, const RHS, op_sub> >
{
// generic x += vec_expr1 - vec_expr2:
template<typename LHS1, typename RHS1>
static void apply(vector_base<T> & lhs, vector_expression<const LHS1, const RHS1, op_sub> const & proxy)
{
bool op_aliasing_lhs = op_aliasing(lhs, proxy.lhs());
bool op_aliasing_rhs = op_aliasing(lhs, proxy.rhs());
if (op_aliasing_lhs || op_aliasing_rhs)
{
vector_base<T> temp(proxy.lhs());
op_executor<vector_base<T>, op_inplace_sub, RHS>::apply(temp, proxy.rhs());
lhs += temp;
}
else
{
op_executor<vector_base<T>, op_inplace_add, LHS>::apply(lhs, proxy.lhs());
op_executor<vector_base<T>, op_inplace_sub, RHS>::apply(lhs, proxy.rhs());
}
}
// x += y - z
static void apply(vector_base<T> & lhs, vector_expression<const vector_base<T>, const vector_base<T>, op_sub> const & proxy)
{
viennacl::linalg::avbv_v(lhs,
proxy.lhs(), T(1), 1, false, false,
proxy.rhs(), T(1), 1, false, true);
}
// x += alpha * y - z
template<typename ScalarType>
static void apply(vector_base<T> & lhs, vector_expression<const vector_expression<const vector_base<T>, const ScalarType, op_mult>,
const vector_base<T>,
op_sub> const & proxy)
{
viennacl::linalg::avbv_v(lhs,
proxy.lhs().lhs(), proxy.lhs().rhs(), 1, false, false,
proxy.rhs(), T(1), 1, false, true);
}
// x += y / alpha - z
template<typename ScalarType>
static void apply(vector_base<T> & lhs, vector_expression<const vector_expression<const vector_base<T>, const ScalarType, op_div>,
const vector_base<T>,
op_sub> const & proxy)
{
viennacl::linalg::avbv_v(lhs,
proxy.lhs().lhs(), proxy.lhs().rhs(), 1, true, false,
proxy.rhs(), T(1), 1, false, true);
}
// x += y - beta * z
template<typename ScalarType>
static void apply(vector_base<T> & lhs, vector_expression<const vector_base<T>,
const vector_expression<const vector_base<T>, const ScalarType, op_mult>,
op_sub> const & proxy)
{
viennacl::linalg::avbv_v(lhs,
proxy.lhs(), T(1), 1, false, false,
proxy.rhs().lhs(), proxy.rhs().rhs(), 1, false, true);
}
// x += y - z / beta
template<typename ScalarType>
static void apply(vector_base<T> & lhs, vector_expression<const vector_base<T>,
const vector_expression<const vector_base<T>, const ScalarType, op_div>,
op_sub> const & proxy)
{
viennacl::linalg::avbv_v(lhs,
proxy.lhs(), T(1), 1, false, false,
proxy.rhs().lhs(), proxy.rhs().rhs(), 1, true, true);
}
// x += alpha * y - beta * z
template<typename ScalarType1, typename ScalarType2>
static void apply(vector_base<T> & lhs, vector_expression<const vector_expression<const vector_base<T>, const ScalarType1, op_mult>,
const vector_expression<const vector_base<T>, const ScalarType2, op_mult>,
op_sub> const & proxy)
{
viennacl::linalg::avbv_v(lhs,
proxy.lhs().lhs(), proxy.lhs().rhs(), 1, false, false,
proxy.rhs().lhs(), proxy.rhs().rhs(), 1, false, true);
}
// x += alpha * y - z / beta
template<typename ScalarType1, typename ScalarType2>
static void apply(vector_base<T> & lhs, vector_expression<const vector_expression<const vector_base<T>, const ScalarType1, op_mult>,
const vector_expression<const vector_base<T>, const ScalarType2, op_div>,
op_sub> const & proxy)
{
viennacl::linalg::avbv_v(lhs,
proxy.lhs().lhs(), proxy.lhs().rhs(), 1, false, false,
proxy.rhs().lhs(), proxy.rhs().rhs(), 1, true, true);
}
// x += y / alpha - beta * z
template<typename ScalarType1, typename ScalarType2>
static void apply(vector_base<T> & lhs, vector_expression<const vector_expression<const vector_base<T>, const ScalarType1, op_div>,
const vector_expression<const vector_base<T>, const ScalarType2, op_mult>,
op_sub> const & proxy)
{
viennacl::linalg::avbv_v(lhs,
proxy.lhs().lhs(), proxy.lhs().rhs(), 1, true, false,
proxy.rhs().lhs(), proxy.rhs().rhs(), 1, false, true);
}
// x += y / alpha - z / beta
template<typename ScalarType1, typename ScalarType2>
static void apply(vector_base<T> & lhs, vector_expression<const vector_expression<const vector_base<T>, const ScalarType1, op_div>,
const vector_expression<const vector_base<T>, const ScalarType2, op_div>,
op_sub> const & proxy)
{
viennacl::linalg::avbv_v(lhs,
proxy.lhs().lhs(), proxy.lhs().rhs(), 1, true, false,
proxy.rhs().lhs(), proxy.rhs().rhs(), 1, true, true);
}
};
// generic x -= vec_expr1 - vec_expr2:
template<typename T, typename LHS, typename RHS>
struct op_executor<vector_base<T>, op_inplace_sub, vector_expression<const LHS, const RHS, op_sub> >
{
// generic x -= vec_expr1 - vec_expr2:
template<typename LHS1, typename RHS1>
static void apply(vector_base<T> & lhs, vector_expression<const LHS1, const RHS1, op_sub> const & proxy)
{
bool op_aliasing_lhs = op_aliasing(lhs, proxy.lhs());
bool op_aliasing_rhs = op_aliasing(lhs, proxy.rhs());
if (op_aliasing_lhs || op_aliasing_rhs)
{
vector_base<T> temp(proxy.lhs());
op_executor<vector_base<T>, op_inplace_sub, RHS>::apply(temp, proxy.rhs());
lhs -= temp;
}
else
{
op_executor<vector_base<T>, op_inplace_sub, LHS>::apply(lhs, proxy.lhs());
op_executor<vector_base<T>, op_inplace_add, RHS>::apply(lhs, proxy.rhs());
}
}
// x -= y - z
static void apply(vector_base<T> & lhs, vector_expression<const vector_base<T>, const vector_base<T>, op_sub> const & proxy)
{
viennacl::linalg::avbv_v(lhs,
proxy.lhs(), T(1), 1, false, true,
proxy.rhs(), T(1), 1, false, false);
}
// x -= alpha * y - z
template<typename ScalarType>
static void apply(vector_base<T> & lhs, vector_expression<const vector_expression<const vector_base<T>, const ScalarType, op_mult>,
const vector_base<T>,
op_sub> const & proxy)
{
viennacl::linalg::avbv_v(lhs,
proxy.lhs().lhs(), proxy.lhs().rhs(), 1, false, true,
proxy.rhs(), T(1), 1, false, false);
}
// x -= y / alpha - z
template<typename ScalarType>
static void apply(vector_base<T> & lhs, vector_expression<const vector_expression<const vector_base<T>, const ScalarType, op_div>,
const vector_base<T>,
op_sub> const & proxy)
{
viennacl::linalg::avbv_v(lhs,
proxy.lhs().lhs(), proxy.lhs().rhs(), 1, true, true,
proxy.rhs(), T(1), 1, false, false);
}
// x -= y - beta * z
template<typename ScalarType>
static void apply(vector_base<T> & lhs, vector_expression<const vector_base<T>,
const vector_expression<const vector_base<T>, const ScalarType, op_mult>,
op_sub> const & proxy)
{
viennacl::linalg::avbv_v(lhs,
proxy.lhs(), T(1), 1, false, true,
proxy.rhs().lhs(), proxy.rhs().rhs(), 1, false, false);
}
// x -= y - z / beta
template<typename ScalarType>
static void apply(vector_base<T> & lhs, vector_expression<const vector_base<T>,
const vector_expression<const vector_base<T>, const ScalarType, op_div>,
op_sub> const & proxy)
{
viennacl::linalg::avbv_v(lhs,
proxy.lhs(), T(1), 1, false, true,
proxy.rhs().lhs(), proxy.rhs().rhs(), 1, true, false);
}
// x -= alpha * y - beta * z
template<typename ScalarType1, typename ScalarType2>
static void apply(vector_base<T> & lhs, vector_expression<const vector_expression<const vector_base<T>, const ScalarType1, op_mult>,
const vector_expression<const vector_base<T>, const ScalarType2, op_mult>,
op_sub> const & proxy)
{
viennacl::linalg::avbv_v(lhs,
proxy.lhs().lhs(), proxy.lhs().rhs(), 1, false, true,
proxy.rhs().lhs(), proxy.rhs().rhs(), 1, false, false);
}
// x -= alpha * y - z / beta
template<typename ScalarType1, typename ScalarType2>
static void apply(vector_base<T> & lhs, vector_expression<const vector_expression<const vector_base<T>, const ScalarType1, op_mult>,
const vector_expression<const vector_base<T>, const ScalarType2, op_div>,
op_sub> const & proxy)
{
viennacl::linalg::avbv_v(lhs,
proxy.lhs().lhs(), proxy.lhs().rhs(), 1, false, true,
proxy.rhs().lhs(), proxy.rhs().rhs(), 1, true, false);
}
// x -= y / alpha - beta * z
template<typename ScalarType1, typename ScalarType2>
static void apply(vector_base<T> & lhs, vector_expression<const vector_expression<const vector_base<T>, const ScalarType1, op_div>,
const vector_expression<const vector_base<T>, const ScalarType2, op_mult>,
op_sub> const & proxy)
{
viennacl::linalg::avbv_v(lhs,
proxy.lhs().lhs(), proxy.lhs().rhs(), 1, true, true,
proxy.rhs().lhs(), proxy.rhs().rhs(), 1, false, false);
}
// x -= y / alpha - z / beta
template<typename ScalarType1, typename ScalarType2>
static void apply(vector_base<T> & lhs, vector_expression<const vector_expression<const vector_base<T>, const ScalarType1, op_div>,
const vector_expression<const vector_base<T>, const ScalarType2, op_div>,
op_sub> const & proxy)
{
viennacl::linalg::avbv_v(lhs,
proxy.lhs().lhs(), proxy.lhs().rhs(), 1, true, true,
proxy.rhs().lhs(), proxy.rhs().rhs(), 1, true, false);
}
};
//////////////////// Element-wise operations ////////////////////////////////////////
// generic x = vec_expr1 .* vec_expr2:
template<typename T, typename LHS, typename RHS, typename OP>
struct op_executor<vector_base<T>, op_assign, vector_expression<const LHS, const RHS, op_element_binary<OP> > >
{
// x = y .* z or x = y ./ z
static void apply(vector_base<T> & lhs, vector_expression<const vector_base<T>, const vector_base<T>, op_element_binary<OP> > const & proxy)
{
viennacl::linalg::element_op(lhs, proxy);
}
// x = y .* vec_expr or x = y ./ vec_expr
template<typename LHS2, typename RHS2, typename OP2>
static void apply(vector_base<T> & lhs, vector_expression<const vector_base<T>, const vector_expression<const LHS2, const RHS2, OP2>, op_element_binary<OP> > const & proxy)
{
vector<T> temp(proxy.rhs());
viennacl::linalg::element_op(lhs, viennacl::vector_expression<const vector_base<T>, const vector_base<T>, op_element_binary<OP> >(proxy.lhs(), temp));
}
// x = vec_expr .* z or x = vec_expr ./ z
template<typename LHS1, typename RHS1, typename OP1>
static void apply(vector_base<T> & lhs, vector_expression<const vector_expression<const LHS1, const RHS1, OP1>, const vector_base<T>, op_element_binary<OP> > const & proxy)
{
vector<T> temp(proxy.lhs());
viennacl::linalg::element_op(lhs, viennacl::vector_expression<const vector_base<T>, const vector_base<T>, op_element_binary<OP> >(temp, proxy.rhs()));
}
// x = vec_expr .* vec_expr or z = vec_expr .* vec_expr
template<typename LHS1, typename RHS1, typename OP1,
typename LHS2, typename RHS2, typename OP2>
static void apply(vector_base<T> & lhs, vector_expression<const vector_expression<const LHS1, const RHS1, OP1>,
const vector_expression<const LHS2, const RHS2, OP2>,
op_element_binary<OP> > const & proxy)
{
vector<T> temp1(proxy.lhs());
vector<T> temp2(proxy.rhs());
viennacl::linalg::element_op(lhs, viennacl::vector_expression<const vector_base<T>, const vector_base<T>, op_element_binary<OP> >(temp1, temp2));
}
};
// generic x += vec_expr1 .* vec_expr2:
template<typename T, typename LHS, typename RHS, typename OP>
struct op_executor<vector_base<T>, op_inplace_add, vector_expression<const LHS, const RHS, op_element_binary<OP> > >
{
// x += y .* z or x += y ./ z
static void apply(vector_base<T> & lhs, vector_expression<const vector_base<T>, const vector_base<T>, op_element_binary<OP> > const & proxy)
{
viennacl::vector<T> temp(proxy);
lhs += temp;
}
// x += y .* vec_expr or x += y ./ vec_expr
template<typename LHS2, typename RHS2, typename OP2>
static void apply(vector_base<T> & lhs, vector_expression<const vector_base<T>, const vector_expression<const LHS2, const RHS2, OP2>, op_element_binary<OP> > const & proxy)
{
vector<T> temp(proxy.rhs());
vector<T> temp2(temp.size());
viennacl::linalg::element_op(temp2, viennacl::vector_expression<const vector_base<T>, const vector_base<T>, op_element_binary<OP> >(proxy.lhs(), temp));
lhs += temp2;
}
// x += vec_expr .* z or x += vec_expr ./ z
template<typename LHS1, typename RHS1, typename OP1>
static void apply(vector_base<T> & lhs, vector_expression<const vector_expression<const LHS1, const RHS1, OP1>, const vector_base<T>, op_element_binary<OP> > const & proxy)
{
vector<T> temp(proxy.lhs());
vector<T> temp2(temp.size());
viennacl::linalg::element_op(temp2, viennacl::vector_expression<const vector_base<T>, const vector_base<T>, op_element_binary<OP> >(temp, proxy.rhs()));
lhs += temp2;
}
// x += vec_expr .* vec_expr or x += vec_expr ./ vec_expr
template<typename LHS1, typename RHS1, typename OP1,
typename LHS2, typename RHS2, typename OP2>
static void apply(vector_base<T> & lhs, vector_expression<const vector_expression<const LHS1, const RHS1, OP1>,
const vector_expression<const LHS2, const RHS2, OP2>,
op_element_binary<OP> > const & proxy)
{
vector<T> temp1(proxy.lhs());
vector<T> temp2(proxy.rhs());
vector<T> temp3(temp1.size());
viennacl::linalg::element_op(temp3, viennacl::vector_expression<const vector_base<T>, const vector_base<T>, op_element_binary<OP> >(temp1, temp2));
lhs += temp3;
}
};
// generic x -= vec_expr1 .* vec_expr2:
template<typename T, typename LHS, typename RHS, typename OP>
struct op_executor<vector_base<T>, op_inplace_sub, vector_expression<const LHS, const RHS, op_element_binary<OP> > >
{
// x -= y .* z or x -= y ./ z
static void apply(vector_base<T> & lhs, vector_expression<const vector_base<T>, const vector_base<T>, op_element_binary<OP> > const & proxy)
{
viennacl::vector<T> temp(proxy);
lhs -= temp;
}
// x -= y .* vec_expr or x -= y ./ vec_expr
template<typename LHS2, typename RHS2, typename OP2>
static void apply(vector_base<T> & lhs, vector_expression<const vector_base<T>, const vector_expression<const LHS2, const RHS2, OP2>, op_element_binary<OP> > const & proxy)
{
vector<T> temp(proxy.rhs());
vector<T> temp2(temp.size());
viennacl::linalg::element_op(temp2, viennacl::vector_expression<const vector_base<T>, const vector_base<T>, op_element_binary<OP> >(proxy.lhs(), temp));
lhs -= temp2;
}
// x -= vec_expr .* z or x -= vec_expr ./ z
template<typename LHS1, typename RHS1, typename OP1>
static void apply(vector_base<T> & lhs, vector_expression<const vector_expression<const LHS1, const RHS1, OP1>, const vector_base<T>, op_element_binary<OP> > const & proxy)
{
vector<T> temp(proxy.lhs());
vector<T> temp2(temp.size());
viennacl::linalg::element_op(temp2, viennacl::vector_expression<const vector_base<T>, const vector_base<T>, op_element_binary<OP> >(temp, proxy.rhs()));
lhs -= temp2;
}
// x -= vec_expr .* vec_expr or x -= vec_expr ./ vec_expr
template<typename LHS1, typename RHS1, typename OP1,
typename LHS2, typename RHS2, typename OP2>
static void apply(vector_base<T> & lhs, vector_expression<const vector_expression<const LHS1, const RHS1, OP1>,
const vector_expression<const LHS2, const RHS2, OP2>,
op_element_binary<OP> > const & proxy)
{
vector<T> temp1(proxy.lhs());
vector<T> temp2(proxy.rhs());
vector<T> temp3(temp1.size());
viennacl::linalg::element_op(temp3, viennacl::vector_expression<const vector_base<T>, const vector_base<T>, op_element_binary<OP> >(temp1, temp2));
lhs -= temp3;
}
};
//////////////// unary expressions
template<typename T, typename LHS, typename RHS, typename OP>
struct op_executor<vector_base<T>, op_assign, vector_expression<const LHS, const RHS, op_element_unary<OP> > >
{
// x = OP(y)
static void apply(vector_base<T> & lhs, vector_expression<const vector_base<T>, const vector_base<T>, op_element_unary<OP> > const & proxy)
{
viennacl::linalg::element_op(lhs, proxy);
}
// x = OP(vec_expr)
template<typename LHS2, typename RHS2, typename OP2>
static void apply(vector_base<T> & lhs, vector_expression<const vector_expression<const LHS2, const RHS2, OP2>,
const vector_expression<const LHS2, const RHS2, OP2>,
op_element_unary<OP> > const & proxy)
{
vector<T> temp(proxy.rhs());
viennacl::linalg::element_op(lhs, viennacl::vector_expression<const vector_base<T>, const vector_base<T>, op_element_unary<OP> >(temp, temp));
}
};
template<typename T, typename LHS, typename RHS, typename OP>
struct op_executor<vector_base<T>, op_inplace_add, vector_expression<const LHS, const RHS, op_element_unary<OP> > >
{
// x += OP(y)
static void apply(vector_base<T> & lhs, vector_expression<const vector_base<T>, const vector_base<T>, op_element_unary<OP> > const & proxy)
{
vector<T> temp(proxy);
lhs += temp;
}
// x += OP(vec_expr)
template<typename LHS2, typename RHS2, typename OP2>
static void apply(vector_base<T> & lhs, vector_expression<const vector_expression<const LHS2, const RHS2, OP2>,
const vector_expression<const LHS2, const RHS2, OP2>,
op_element_unary<OP> > const & proxy)
{
vector<T> temp(proxy.rhs());
viennacl::linalg::element_op(temp, viennacl::vector_expression<const vector_base<T>, const vector_base<T>, op_element_unary<OP> >(temp, temp)); // inplace operation is safe here
lhs += temp;
}
};
template<typename T, typename LHS, typename RHS, typename OP>
struct op_executor<vector_base<T>, op_inplace_sub, vector_expression<const LHS, const RHS, op_element_unary<OP> > >
{
// x -= OP(y)
static void apply(vector_base<T> & lhs, vector_expression<const vector_base<T>, const vector_base<T>, op_element_unary<OP> > const & proxy)
{
vector<T> temp(proxy);
lhs -= temp;
}
// x -= OP(vec_expr)
template<typename LHS2, typename RHS2, typename OP2>
static void apply(vector_base<T> & lhs, vector_expression<const vector_expression<const LHS2, const RHS2, OP2>,
const vector_expression<const LHS2, const RHS2, OP2>,
op_element_unary<OP> > const & proxy)
{
vector<T> temp(proxy.rhs());
viennacl::linalg::element_op(temp, viennacl::vector_expression<const vector_base<T>, const vector_base<T>, op_element_unary<OP> >(temp, temp)); // inplace operation is safe here
lhs -= temp;
}
};
//////////// Generic user-provided routines //////////////
template<typename T, typename UserMatrixT>
struct op_executor<vector_base<T>, op_assign, vector_expression<const UserMatrixT, const vector_base<T>, op_prod> >
{
static void apply(vector_base<T> & lhs, vector_expression<const UserMatrixT, const vector_base<T>, op_prod> const & rhs)
{
rhs.lhs().apply(rhs.rhs(), lhs);
}
};
} // namespace detail
} // namespace linalg
/** \endcond */
} // namespace viennacl
#endif
|