/usr/include/trilinos/Kokkos_DefaultArithmetic.hpp is in libtrilinos-tpetra-dev 12.4.2-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 | //@HEADER
// ************************************************************************
//
// Kokkos: Node API and Parallel Node Kernels
// Copyright (2008) Sandia Corporation
//
// Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
// the U.S. Government retains certain rights in this software.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// 1. Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// 3. Neither the name of the Corporation nor the names of the
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// Questions? Contact Michael A. Heroux (maherou@sandia.gov)
//
// ************************************************************************
//@HEADER
#ifndef KOKKOS_DEFAULTARITHMETIC_H
#define KOKKOS_DEFAULTARITHMETIC_H
/// \file Kokkos_DefaultArithmetic.hpp
/// \brief Traits class for local multivector operations.
#include "Kokkos_DefaultNode.hpp"
#include "Kokkos_NodeHelpers.hpp" // ReadyBufferHelper
#include "Kokkos_MultiVector.hpp"
#include "Kokkos_MultiVectorKernelOps.hpp"
#include "Teuchos_ArrayView.hpp"
#include "Teuchos_Assert.hpp"
#include "Teuchos_BLAS.hpp"
#include "Teuchos_BLAS_types.hpp"
#include "Teuchos_Tuple.hpp"
#include "Teuchos_TypeNameTraits.hpp"
#include <stdexcept>
namespace KokkosClassic {
// Class for providing GEMM for a particular Node
template <typename Scalar, typename Node>
struct NodeGEMM {
public:
static void GEMM(Teuchos::ETransp transA, Teuchos::ETransp transB, Scalar alpha, const MultiVector<Scalar,Node> &A, const MultiVector<Scalar,Node> &B, Scalar beta, MultiVector<Scalar,Node> &C) {
Teuchos::BLAS<int,Scalar> blas;
const int m = Teuchos::as<int>(C.getNumRows()),
n = Teuchos::as<int>(C.getNumCols()),
k = (transA == Teuchos::NO_TRANS ? A.getNumCols() : A.getNumRows()),
lda = Teuchos::as<int>(A.getStride()),
ldb = Teuchos::as<int>(B.getStride()),
ldc = Teuchos::as<int>(C.getStride());
// For some BLAS implementations (i.e. MKL), GEMM when B has one column
// is signficantly less efficient
if (n == 1 && transB == Teuchos::NO_TRANS)
blas.GEMV(transA, A.getNumRows(), A.getNumCols(), alpha, A.getValues().getRawPtr(), lda, B.getValues().getRawPtr(), Teuchos::as<int>(1), beta, C.getValuesNonConst().getRawPtr(), Teuchos::as<int>(1));
else
blas.GEMM(transA, transB, m, n, k, alpha, A.getValues().getRawPtr(), lda, B.getValues().getRawPtr(), ldb, beta, C.getValuesNonConst().getRawPtr(), ldc);
}
};
/// \class DefaultArithmeticBase
/// \brief Base class for DefaultArithmetic; not for users of the latter.
///
/// \tparam MV The local multivector type. We provide a
/// specialization for MultiVector.
template <class MV>
class DefaultArithmeticBase {
public:
/// \brief Compute the matrix-matrix product <tt>C = alpha*Op(A)*Op(B) + beta*C</tt>.
///
/// \c Op(A) may be either \c A, its transpose, or its conjugate
/// transpose, depending on \c transA. Likewise, \c Op(B) may be
/// either \c B, its transpose, or its conjugate transpose,
/// depending on \c transB.
static void
GEMM (MV &C, Teuchos::ETransp transA, Teuchos::ETransp transB,
typename MV::ScalarType alpha, const MV &A,
const MV &B, typename MV::ScalarType beta);
//! Fill \c A with uniform random numbers.
static void Random (MV& A);
/// \brief Tell \c A about its dimensions, and give it a pointer to its data.
///
/// \param A [out] The multivector to tell about its dimensions and data.
/// \param numRows [in] Number of rows in A.
/// \param numCols [in] Number of columns in A.
/// \param values [in] Pointer to A's data. This is a matrix
/// stored in column-major format.
/// \param stride [in] Stride between columns of the matrix.
///
/// \pre <tt>stride >= numRows</tt>
static void
initializeValues (MV &A, size_t numRows, size_t numCols,
const ArrayRCP<typename MV::ScalarType> &values,
size_t stride);
/// Tell \c A about its dimensions and original dimensions, and
/// give it a pointer to its data.
///
/// Use this version of initializeValues when the MV to initialize
/// is actually a view of another MV. Keeping the original
/// dimensions lets you do error checking correctly, especially
/// when going from a subset view (a view of a subset of rows) to
/// a superset of the subset. This is an important case for
/// Tpetra, e.g., when making domain Map vectors that are actually
/// views of column Map vectors, then getting the original column
/// Map vector back. (This makes things like Import and local
/// Gauss-Seidel more efficient.)
///
/// \param A [out] The multivector to tell about its dimensions and data.
/// \param numRows [in] Number of rows in A.
/// \param numCols [in] Number of columns in A.
/// \param values [in] Pointer to A's data. This is a matrix
/// stored in column-major format.
/// \param stride [in] Stride between columns of the matrix.
/// \param origNumRows [in] Original number of rows in the
/// multivector (of which A is to be a view).
/// \param origNumCols [in] Original number of rows in the
/// multivector (of which A is to be a view).
///
/// \pre <tt>stride >= numRows</tt>
/// \pre <tt>stride >= origNumRows</tt>
static void
initializeValues (MV &A, size_t numRows, size_t numCols,
const ArrayRCP<typename MV::ScalarType> &values,
size_t stride,
size_t origNumRows,
size_t origNumCols);
//! Get a const pointer to A's data; the same pointer set by initializeValues().
static ArrayRCP<const typename MV::ScalarType> getValues (const MV &A);
//! Get a const pointer to the data of column \c j of \c A.
static ArrayRCP<const typename MV::ScalarType>
getValues (const MV &A, size_t j);
//! Get a nonconst pointer to A's data; the same pointer set by initializeValues().
static ArrayRCP<typename MV::ScalarType> getValuesNonConst (MV &A);
//! Get a nonconst pointer to the data of column \c j of \c A.
static ArrayRCP<typename MV::ScalarType>
getValuesNonConst (const MV &A, size_t j);
//! The number of rows in \c A.
static size_t getNumRows (const MV &A);
//! The number of columns in \c A.
static size_t getNumCols (const MV &A);
//! The (column) stride of \c A.
static size_t getStride (const MV &A);
/// \brief "Original" number of rows (of the multivector of
/// which A is a view).
///
/// If A is <i>not</i> a view of another multivector, then this
/// method just returns the number of rows.
static size_t getOrigNumRows (const MV &A);
/// \brief "Original" number of columns (of the multivector of
/// which A is a view).
///
/// If A is <i>not</i> a view of another multivector, then this
/// method just returns the number of columns.
static size_t getOrigNumCols (const MV &A);
//! The Kokkos Node instance with which \c A was created.
static RCP<typename MV::NodeType> getNode (const MV &A);
};
/// \class DefaultArithmetic
/// \brief Traits class providing a generic arithmetic interface for local multivectors.
///
/// \tparam MV The local multivector type. We provide a
/// specialization for MultiVector.
template <class MV>
class DefaultArithmetic : public DefaultArithmeticBase<MV> {
public:
//! Initialize all entries of \c A to the given constant value \c alpha.
static void Init (MV& A, typename MV::ScalarType alpha);
//! Set A to the reciprocal of B: <tt>B(i,j) = 1/A(i,j)</tt>.
static void Recip (MV& A, const MV& B);
/// \brief A threshold, in-place variant of Recip().
///
/// For each element A(i,j) of A, set A(i,j) = 1/A(i,j) if the
/// magnitude of A(i,j) is greater than or equal to the magnitude
/// of minDiagVal. Otherwise, set A(i,j) to minDiagVal.
static void
ReciprocalThreshold (MV& A, typename MV::ScalarType& minDiagVal);
/// \brief Set C to the scaled element-wise multiple of A and B.
///
/// <tt>C(i,j) = scalarC * C(i,j) + scalarAB * B(i,j) * A(i,1)</tt>,
/// where the input multivector A has only 1 column. If scalarC
/// is zero, this method ignores the initial contents of C, even
/// if there are NaN entries.
static void
ElemMult (MV& C,
typename MV::ScalarType scalarC,
typename MV::ScalarType scalarAB,
const MV& A,
const MV& B);
/// \brief Assign B to A: <tt>A(i,j) = B(i,j)</tt>.
///
/// If A and B point to the same data, then this function skips
/// the assignment entirely.
static void Assign (MV& A, const MV& B);
/// \brief Assign the given columns of B to A.
///
/// This assigns <tt>A(i, j) = B(i, whichVectors[j])</tt>
/// for i in <tt>[0, getNumRows(A)]</tt> and
/// j in <tt>[0, getNumCols(A)]</tt>.
///
/// If for any j, <tt>A(0,j)</tt> and
/// <tt>B(0, whichVectors[j])</tt> point to the same data,
/// then this function skips the assignment for that column.
static void Assign (MV& A, const MV& B, const ArrayView<const size_t>& whichVectors);
//! Compute the inner products of corresponding columns of A and B.
static void Dot (const MV& A, const MV& B, const ArrayView<typename MV::ScalarType> &dots);
//! Compute the inner product of A and B (assuming each has only one column).
static typename MV::ScalarType Dot (const MV& A, const MV& B);
/// \brief Compute <tt>B = alpha * A + beta * B</tt>.
///
/// If beta is zero, overwrite B, even if it contains NaN entries.
static void
GESUM (MV& B, typename MV::ScalarType alpha, const MV& A, typename MV::ScalarType beta);
/// \brief Compute <tt>C = alpha * A + beta * B + gamma * C</tt>.
///
/// If gamma is zero, overwrite C, even if it contains NaN entries.
static void
GESUM (MV &C, typename MV::ScalarType alpha, const MV &A,
typename MV::ScalarType beta, const MV &B, typename MV::ScalarType gamma);
//! Compute the one-norm of each column of \c A.
static void Norm1 (const MV &A, const ArrayView<typename Teuchos::ScalarTraits<typename MV::ScalarType>::magnitudeType> &norms);
//! Compute the one-norm of (the first column of) \c A.
static typename Teuchos::ScalarTraits<typename MV::ScalarType>::magnitudeType Norm1 (const MV &A);
//! Compute the sum of each column of \c A.
static void Sum (const MV &A, const ArrayView<typename MV::ScalarType> &sums);
//! Compute the sum of (the first column of) \c A.
static typename MV::ScalarType Sum (const MV& A);
//! Compute the infinity norm (element of maximum magnitude) of each column of \c A.
static void NormInf (const MV& A, const ArrayView<typename Teuchos::ScalarTraits<typename MV::ScalarType>::magnitudeType> &norms);
//! Compute the infinity norm (element of maximum magnitude) of (the first column of) \c A.
static typename Teuchos::ScalarTraits<typename MV::ScalarType>::magnitudeType
NormInf (const MV& A);
//! Compute the square of the 2-norm of each column of \c A.
static void Norm2Squared (const MV &A, const ArrayView<typename Teuchos::ScalarTraits<typename MV::ScalarType>::magnitudeType> &norms);
//! Compute the square of the 2-norm of (the first column of) \c A.
static typename Teuchos::ScalarTraits<typename MV::ScalarType>::magnitudeType
Norm2Squared (const MV& A);
//! Compute the norm of (the first column of) \c A, weighted by the given vector of weights.
static typename Teuchos::ScalarTraits<typename MV::ScalarType>::magnitudeType
WeightedNorm (const MV &A, const MV &weightVector);
//! Compute the norm of each column of \c A, weighted by the given vector of weights.
static void WeightedNorm (const MV &A, const MV &weightVector, const ArrayView<typename Teuchos::ScalarTraits<typename MV::ScalarType>::magnitudeType> &norms);
//! Compute <tt>A = abs(B)</tt>, elementwise.
static void Abs (MV &A, const MV &B);
//! Compute <tt>B = alpha * A</tt>.
static void Scale (MV &B, typename MV::ScalarType alpha, const MV &A);
//! Scale \c A in place by \c alpha: <tt>A = alpha * A</tt>.
static void Scale (MV &A, typename MV::ScalarType alpha);
};
/// \brief Partial specialization of DefaultArithmeticBase for MultiVector<Scalar,Node>.
///
/// \tparam Scalar The type of entries of the multivector.
/// \tparam The Kokkos Node type.
template <class Scalar, class Node>
class DefaultArithmeticBase<MultiVector<Scalar,Node> > {
public:
static void
GEMM (MultiVector<Scalar,Node> &C,
Teuchos::ETransp transA,
Teuchos::ETransp transB,
Scalar alpha,
const MultiVector<Scalar,Node> &A,
const MultiVector<Scalar,Node> &B,
Scalar beta)
{
NodeGEMM<Scalar,Node>::GEMM(transA, transB, alpha, A, B, beta, C);
}
static void Random(MultiVector<Scalar,Node> &A) {
// TODO: consider adding rand() functionality to node
// in the meantime, just generate random numbers via Teuchos and then copy to node
typedef Teuchos::ScalarTraits<Scalar> SCT;
const size_t stride = A.getStride();
const size_t nR = A.getNumRows();
const size_t nC = A.getNumCols();
if (nR*nC == 0) return;
RCP<Node> node = A.getNode();
ArrayRCP<Scalar> Adata = A.getValuesNonConst();
// we'll overwrite all data covered by the multivector, but not off-stride data
// therefore, we are write-only only in the case that stride=nR
ReadWriteOption rw = (stride == nR ? WriteOnly : ReadWrite);
ArrayRCP<Scalar> mvdata = node->template viewBufferNonConst<Scalar>(rw,stride*(nC-1)+nR,Adata);
for (size_t j=0; j<nC; ++j) {
for (size_t i=0; i<nR; ++i) {
mvdata[j*stride + i] = SCT::random();
}
}
mvdata = null;
}
inline static void
initializeValues (MultiVector<Scalar,Node> &A,
size_t numRows, size_t numCols,
const ArrayRCP<Scalar> &values,
size_t stride)
{
A.initializeValues(numRows,numCols,values,stride);
}
inline static void
initializeValues (MultiVector<Scalar,Node> &A,
size_t numRows,
size_t numCols,
const ArrayRCP<Scalar> &values,
size_t stride,
size_t origNumRows,
size_t origNumCols)
{
A.initializeValues(numRows,numCols,values,stride,origNumRows,origNumCols);
}
inline static ArrayRCP<const Scalar> getValues(const MultiVector<Scalar,Node> &A) {
return A.getValues();
}
inline static ArrayRCP<const Scalar> getValues(const MultiVector<Scalar,Node> &A, size_t j) {
return A.getValues(j);
}
inline static ArrayRCP<Scalar> getValuesNonConst(MultiVector<Scalar,Node> &A) {
return A.getValuesNonConst();
}
inline static ArrayRCP<Scalar> getValuesNonConst(MultiVector<Scalar,Node> &A, size_t j) {
return A.getValuesNonConst(j);
}
inline static size_t getNumRows(const MultiVector<Scalar,Node> &A) {
return A.getNumRows();
}
inline static size_t getNumCols(const MultiVector<Scalar,Node> &A) {
return A.getNumCols();
}
inline static size_t getStride(const MultiVector<Scalar,Node> &A) {
return A.getStride();
}
inline static size_t getOrigNumRows (const MultiVector<Scalar,Node> &A) {
return A.getOrigNumRows ();
}
inline static size_t getOrigNumCols (const MultiVector<Scalar,Node> &A) {
return A.getOrigNumCols ();
}
inline static RCP<Node> getNode(const MultiVector<Scalar,Node> &A) {
return A.getNode();
}
};
/// \brief Partial specialization of DefaultArithmetic for MultiVector<Scalar,Node>.
///
/// Tpetra::MultiVector uses this as a traits class for
/// KokkosClassic::MultiVector, to implement all of its computational
/// kernels.
///
/// \tparam Scalar The type of entries of the multivector.
/// \tparam The Kokkos Node type.
template <class Scalar, class Node>
class DefaultArithmetic<MultiVector<Scalar, Node> > :
public DefaultArithmeticBase<MultiVector<Scalar, Node> > {
public:
static void Init (MultiVector<Scalar,Node> &A, Scalar alpha) {
const size_t nR = A.getNumRows();
const size_t nC = A.getNumCols();
if (nR*nC == 0) return;
const size_t stride = A.getStride();
RCP<Node> node = A.getNode();
ArrayRCP<Scalar> data = A.getValuesNonConst();
// prepare buffers
ReadyBufferHelper<Node> rbh(node);
rbh.begin();
rbh.template addNonConstBuffer<Scalar>(data);
rbh.end();
// prepare op
InitOp<Scalar> wdp;
wdp.alpha = alpha;
if (stride == nR) {
// one kernel invocation for whole multivector
wdp.x = data(0,nR*nC).getRawPtr();
node->template parallel_for<InitOp<Scalar> >(0,nR*nC,wdp);
}
else {
// one kernel invocation for each column
for (size_t j=0; j<nC; ++j) {
wdp.x = data(0,nR).getRawPtr();
node->template parallel_for<InitOp<Scalar> >(0,nR,wdp);
data += stride;
}
}
}
static void
Recip (MultiVector<Scalar,Node> &A,
const MultiVector<Scalar,Node> &B)
{
const size_t nR = A.getNumRows();
const size_t nC = A.getNumCols();
const size_t Astride = A.getStride();
const size_t Bstride = B.getStride();
TEUCHOS_TEST_FOR_EXCEPTION(
nC != B.getNumCols() || nR != B.getNumRows(),
std::runtime_error,
"DefaultArithmetic<" << Teuchos::typeName(A) << ">::Recip(A,B): "
"A and B must have the same dimensions.");
RCP<Node> node = B.getNode();
ArrayRCP<const Scalar> Bdata = B.getValues();
ArrayRCP<Scalar> Adata = A.getValuesNonConst();
// prepare buffers
ReadyBufferHelper<Node> rbh(node);
rbh.begin();
rbh.template addConstBuffer<Scalar>(Bdata);
rbh.template addNonConstBuffer<Scalar>(Adata);
rbh.end();
RecipOp<Scalar> wdp;
if (A.getStride() == nR && B.getStride() == nR) {
// one kernel invocation for whole multivector
wdp.scale = Bdata(0,nR*nC).getRawPtr();
wdp.x = Adata(0,nR*nC).getRawPtr();
node->template parallel_for<RecipOp<Scalar> >(0,nR*nC,wdp);
}
else {
// one kernel invocation for each column
for (size_t j=0; j<nC; ++j) {
wdp.x = Adata(0,nR).getRawPtr();
wdp.scale = Bdata(0,nR).getRawPtr();
node->template parallel_for<RecipOp<Scalar> >(0,nR,wdp);
Adata += Astride;
Bdata += Bstride;
}
}
}
static void
ReciprocalThreshold (MultiVector<Scalar,Node>& A,
const Scalar& minDiagVal)
{
const size_t numRows = A.getNumRows ();
const size_t numCols = A.getNumCols ();
const size_t stride = A.getStride ();
ArrayRCP<Scalar> A_data = A.getValuesNonConst ();
Scalar* const A_ptr = A_data.getRawPtr ();
RCP<Node> node = A.getNode ();
ReadyBufferHelper<Node> rbh (node);
rbh.begin();
rbh.template addNonConstBuffer<Scalar> (A_data);
rbh.end();
if (stride == numRows) {
// One kernel invocation for all columns of the multivector.
typedef ReciprocalThresholdOp<Scalar> op_type;
op_type wdp (A_ptr, minDiagVal);
node->template parallel_for<op_type> (0, numRows*numCols, wdp);
}
else {
// One kernel invocation for each column of the multivector.
for (size_t j = 0; j < numCols; ++j) {
typedef ReciprocalThresholdOp<Scalar> op_type;
Scalar* const A_j = A_ptr + j * stride;
op_type wdp (A_j, minDiagVal);
node->template parallel_for<op_type> (0, numRows, wdp);
}
}
}
static void
ElemMult (MultiVector<Scalar,Node> &C,
Scalar scalarC,
Scalar scalarAB,
const MultiVector<Scalar,Node> &A,
const MultiVector<Scalar,Node> &B)
{
const size_t nR_A = A.getNumRows();
const size_t nC_A = A.getNumCols();
TEUCHOS_TEST_FOR_EXCEPTION(
nC_A != 1, std::runtime_error,
"DefaultArithmetic<" << Teuchos::typeName(A)
<< ">::ElemMult(C,sC,sAB,A,B): A must have just 1 column.");
const size_t Cstride = C.getStride();
const size_t Bstride = B.getStride();
const size_t nC_C = C.getNumCols();
const size_t nR_C = C.getNumRows();
TEUCHOS_TEST_FOR_EXCEPTION(
nC_C != B.getNumCols() || nR_A != B.getNumRows() || nR_C != B.getNumRows(),
std::runtime_error,
"DefaultArithmetic<" << Teuchos::typeName(A) << ">::ElemMult"
"(C,sC,sAB,A,B): A, B and C must have the same number of rows, "
"and B and C must have the same number of columns.");
RCP<Node> node = B.getNode();
ArrayRCP<Scalar> Cdata = C.getValuesNonConst();
ArrayRCP<const Scalar> Bdata = B.getValues();
ArrayRCP<const Scalar> Adata = A.getValues();
// prepare buffers
ReadyBufferHelper<Node> rbh(node);
rbh.begin();
rbh.template addNonConstBuffer<Scalar>(Cdata);
rbh.template addConstBuffer<Scalar>(Bdata);
rbh.template addConstBuffer<Scalar>(Adata);
rbh.end();
if (scalarC == Teuchos::ScalarTraits<Scalar>::zero ()) {
MVElemMultOverwriteOp<Scalar> wdp;
wdp.scalarYZ = scalarAB;
// one kernel invocation for each column
for (size_t j=0; j<nC_C; ++j) {
wdp.x = Cdata(0,nR_C).getRawPtr();
wdp.y = Adata(0,nR_C).getRawPtr();
wdp.z = Bdata(0,nR_C).getRawPtr();
node->template parallel_for<MVElemMultOverwriteOp<Scalar> >(0,nR_C,wdp);
Cdata += Cstride;
Bdata += Bstride;
}
}
else {
MVElemMultOp<Scalar> wdp;
wdp.scalarX = scalarC;
wdp.scalarYZ = scalarAB;
// one kernel invocation for each column
for (size_t j=0; j<nC_C; ++j) {
wdp.x = Cdata(0,nR_C).getRawPtr();
wdp.y = Adata(0,nR_C).getRawPtr();
wdp.z = Bdata(0,nR_C).getRawPtr();
node->template parallel_for<MVElemMultOp<Scalar> >(0,nR_C,wdp);
Cdata += Cstride;
Bdata += Bstride;
}
}
}
static void
Assign (MultiVector<Scalar,Node> &A,
const MultiVector<Scalar,Node> &B)
{
const size_t nR = A.getNumRows();
const size_t nC = A.getNumCols();
const size_t Astride = A.getStride();
const size_t Bstride = B.getStride();
TEUCHOS_TEST_FOR_EXCEPTION(
nC != B.getNumCols() || nR != B.getNumRows(),
std::runtime_error,
"DefaultArithmetic<" << Teuchos::typeName(A) << ">::Assign(A,B): "
"The MultiVectors A and B do not have the same dimensions. "
"A is " << nR << " x " << nC << ", but B is "
<< B.getNumRows() << " x " << B.getNumCols() << ".");
if (nC*nR == 0) {
return; // Nothing to do!
}
RCP<Node> node = A.getNode();
ArrayRCP<const Scalar> Bdata = B.getValues();
ArrayRCP<Scalar> Adata = A.getValuesNonConst();
// If A and B are the same pointer, just return without doing
// anything. This can make the implementation of
// Tpetra::MultiVector::copyAndPermute more concise.
if (Adata.getRawPtr () == Bdata.getRawPtr ()) {
return;
}
// prepare buffers
ReadyBufferHelper<Node> rbh(node);
rbh.begin();
rbh.template addConstBuffer<Scalar>(Bdata);
rbh.template addNonConstBuffer<Scalar>(Adata);
rbh.end();
// prepare op
AssignOp<Scalar> wdp;
if (Astride == nR && Bstride == nR) {
// one kernel invocation for whole multivector assignment
wdp.x = Adata(0,nR*nC).getRawPtr();
wdp.y = Bdata(0,nR*nC).getRawPtr();
node->template parallel_for<AssignOp<Scalar> >(0,nR*nC,wdp);
}
else {
// one kernel invocation for each column
for (size_t j=0; j<nC; ++j) {
wdp.x = Adata(0,nR).getRawPtr();
wdp.y = Bdata(0,nR).getRawPtr();
node->template parallel_for<AssignOp<Scalar> >(0,nR,wdp);
Adata += Astride;
Bdata += Bstride;
}
}
}
static void
Assign (MultiVector<Scalar,Node>& A,
const MultiVector<Scalar,Node>& B,
const ArrayView<const size_t>& whichVectors)
{
const size_t nR = A.getNumRows();
const size_t nC = A.getNumCols();
const size_t numColsToCopy = static_cast<size_t> (whichVectors.size ());
TEUCHOS_TEST_FOR_EXCEPTION(
nR != B.getNumRows() || numColsToCopy > nC,
std::runtime_error,
"DefaultArithmetic<" << Teuchos::typeName(A) << ">::Assign(A,B,"
"whichVectors): The KokkosClassic::MultiVector inputs A and B(whichVectors) "
"do not have compatible dimensions. "
"A is " << nR << " x " << nC << ", but B has "
<< B.getNumRows() << ", and there are " << numColsToCopy
<< " columns of B to copy into A.");
if (nR == 0 || numColsToCopy == 0) {
return; // Nothing to do!
}
RCP<Node> node = A.getNode();
// Make sure that the buffers don't go out of scope until the
// kernels are done.
ReadyBufferHelper<Node> rbh (node);
rbh.begin();
rbh.template addNonConstBuffer<Scalar> (A.getValuesNonConst ());
rbh.template addConstBuffer<Scalar> (B.getValues ());
rbh.end();
AssignOp<Scalar> wdp; // Reuse the struct for each loop iteration.
// One kernel invocation for each column of B to copy.
for (size_t j = 0; j < numColsToCopy; ++j) {
wdp.x = A.getValuesNonConst (j).getRawPtr ();
wdp.y = B.getValues (whichVectors[j]).getRawPtr ();
// Skip columns that alias one another.
if (wdp.x != wdp.y) {
node->template parallel_for<AssignOp<Scalar> > (0, nR, wdp);
}
}
}
static void
Dot (const MultiVector<Scalar,Node> &A,
const MultiVector<Scalar,Node> &B,
const ArrayView<Scalar> &dots)
{
const size_t nR = A.getNumRows();
const size_t nC = A.getNumCols();
const size_t Astride = A.getStride();
const size_t Bstride = B.getStride();
TEUCHOS_TEST_FOR_EXCEPTION(
nC != B.getNumCols () || nR != B.getNumRows (), std::runtime_error,
"DefaultArithmetic<" << Teuchos::typeName (A) << ">::Dot(A,B,dots): "
"A and B must have the same dimensions.");
TEUCHOS_TEST_FOR_EXCEPTION(
nC > Teuchos::as<size_t> (dots.size ()), std::runtime_error,
"DefaultArithmetic<" << Teuchos::typeName (A) << ">::Dot(A,B,dots): "
"dots must have length as large as number of columns of A and B.");
if (nR*nC == 0) {
std::fill (dots.begin(), dots.begin() + nC,
Teuchos::ScalarTraits<Scalar>::zero ());
return;
}
RCP<Node> node = A.getNode();
ArrayRCP<const Scalar> Bdata = B.getValues();
ArrayRCP<const Scalar> Adata = A.getValues();
// prepare buffers
ReadyBufferHelper<Node> rbh(node);
rbh.begin();
rbh.template addConstBuffer<Scalar>(Bdata);
rbh.template addConstBuffer<Scalar>(Adata);
rbh.end();
DotOp2<Scalar> op;
for (size_t j=0; j<nC; ++j) {
op.x = Adata(0,nR).getRawPtr();
op.y = Bdata(0,nR).getRawPtr();
dots[j] = node->parallel_reduce(0,nR,op);
Adata += Astride;
Bdata += Bstride;
}
}
static Scalar
Dot (const MultiVector<Scalar,Node> &A,
const MultiVector<Scalar,Node> &B)
{
const size_t nR = A.getNumRows();
const size_t nC = A.getNumCols();
TEUCHOS_TEST_FOR_EXCEPTION(
nR != B.getNumRows(), std::runtime_error,
"DefaultArithmetic<" << Teuchos::typeName(A) << ">::Dot(A,B): "
"A and B must have the same number of rows.");
if (nR*nC == 0) {
return Teuchos::ScalarTraits<Scalar>::zero ();
}
RCP<Node> node = A.getNode();
ArrayRCP<const Scalar> Bdata = B.getValues(0);
ArrayRCP<const Scalar> Adata = A.getValues(0);
// prepare buffers
ReadyBufferHelper<Node> rbh(node);
rbh.begin();
rbh.template addConstBuffer<Scalar>(Bdata);
rbh.template addConstBuffer<Scalar>(Adata);
rbh.end();
DotOp2<Scalar> op;
op.x = Adata(0,nR).getRawPtr();
op.y = Bdata(0,nR).getRawPtr();
return node->parallel_reduce(0,nR,op);
}
static void
GESUM (MultiVector<Scalar,Node> &B,
Scalar alpha,
const MultiVector<Scalar,Node> &A,
Scalar beta)
{
const size_t nR = A.getNumRows();
const size_t nC = A.getNumCols();
const size_t Astride = A.getStride();
const size_t Bstride = B.getStride();
TEUCHOS_TEST_FOR_EXCEPTION(
nC != B.getNumCols() || nR != B.getNumRows(), std::runtime_error,
"DefaultArithmetic<" << Teuchos::typeName(A) << ">::GESUM(B,alpha,A,beta): "
"A and B must have the same dimensions.");
RCP<Node> node = B.getNode();
ArrayRCP<const Scalar> Adata = A.getValues();
ArrayRCP<Scalar> Bdata = B.getValuesNonConst();
// prepare buffers
ReadyBufferHelper<Node> rbh(node);
rbh.begin();
rbh.template addConstBuffer<Scalar>(Adata);
rbh.template addNonConstBuffer<Scalar>(Bdata);
rbh.end();
// mfh 07 Mar 2013: Special case for beta == 0, to overwrite B
// unconditionally, regardless of NaN entries.
if (beta == Teuchos::ScalarTraits<Scalar>::zero ()) {
GESUMZeroBetaOp<Scalar> wdp;
wdp.alpha = alpha;
if (Astride == nR && Bstride == nR) {
// one kernel invocation for whole multivector
wdp.y = Bdata(0,nR*nC).getRawPtr();
wdp.x = Adata(0,nR*nC).getRawPtr();
node->template parallel_for<GESUMZeroBetaOp<Scalar> >(0,nR*nC,wdp);
}
else {
// one kernel invocation for each column
for (size_t j=0; j<nC; ++j) {
wdp.y = Bdata(0,nR).getRawPtr();
wdp.x = Adata(0,nR).getRawPtr();
node->template parallel_for<GESUMZeroBetaOp<Scalar> >(0,nR,wdp);
Adata += Astride;
Bdata += Bstride;
}
}
}
else {
GESUMOp<Scalar> wdp;
wdp.alpha = alpha;
wdp.beta = beta;
if (Astride == nR && Bstride == nR) {
// one kernel invocation for whole multivector
wdp.y = Bdata(0,nR*nC).getRawPtr();
wdp.x = Adata(0,nR*nC).getRawPtr();
node->template parallel_for<GESUMOp<Scalar> >(0,nR*nC,wdp);
}
else {
// one kernel invocation for each column
for (size_t j=0; j<nC; ++j) {
wdp.y = Bdata(0,nR).getRawPtr();
wdp.x = Adata(0,nR).getRawPtr();
node->template parallel_for<GESUMOp<Scalar> >(0,nR,wdp);
Adata += Astride;
Bdata += Bstride;
}
}
}
}
static void
GESUM (MultiVector<Scalar,Node> &C,
Scalar alpha,
const MultiVector<Scalar,Node> &A,
Scalar beta,
const MultiVector<Scalar,Node> &B,
Scalar gamma)
{
const size_t nR = A.getNumRows();
const size_t nC = A.getNumCols();
const size_t Astride = A.getStride();
const size_t Bstride = B.getStride();
const size_t Cstride = C.getStride();
TEUCHOS_TEST_FOR_EXCEPTION(
nC != B.getNumCols() || nR != B.getNumRows(),
std::runtime_error,
"DefaultArithmetic<" << Teuchos::typeName(A) << ">::GESUM"
"(C,alpha,A,beta,B,gamma): A and B must have the same dimensions.");
RCP<Node> node = B.getNode();
ArrayRCP<const Scalar> Adata = A.getValues();
ArrayRCP<const Scalar> Bdata = B.getValues();
ArrayRCP<Scalar> Cdata = C.getValuesNonConst();
// prepare buffers
ReadyBufferHelper<Node> rbh(node);
rbh.begin();
rbh.template addConstBuffer<Scalar>(Adata);
rbh.template addConstBuffer<Scalar>(Bdata);
rbh.template addNonConstBuffer<Scalar>(Cdata);
rbh.end();
// mfh 07 Mar 2013: Special case for gamma == 0, to overwrite C
// unconditionally, regardless of NaN entries.
if (gamma == Teuchos::ScalarTraits<Scalar>::zero ()) {
GESUMZeroGammaOp3<Scalar> wdp;
wdp.alpha = alpha;
wdp.beta = beta;
if (Astride == nR && Bstride == nR && Cstride == nR) {
// one kernel invocation for whole multivector
wdp.z = Cdata(0,nR*nC).getRawPtr();
wdp.y = Bdata(0,nR*nC).getRawPtr();
wdp.x = Adata(0,nR*nC).getRawPtr();
node->template parallel_for<GESUMZeroGammaOp3<Scalar> >(0,nR*nC,wdp);
}
else {
// one kernel invocation for each column
for (size_t j=0; j<nC; ++j) {
wdp.z = Cdata(0,nR).getRawPtr();
wdp.y = Bdata(0,nR).getRawPtr();
wdp.x = Adata(0,nR).getRawPtr();
node->template parallel_for<GESUMZeroGammaOp3<Scalar> >(0,nR,wdp);
Adata += Astride;
Bdata += Bstride;
Cdata += Cstride;
}
}
}
else {
GESUMOp3<Scalar> wdp;
wdp.alpha = alpha;
wdp.beta = beta;
wdp.gamma = gamma;
if (Astride == nR && Bstride == nR && Cstride == nR) {
// one kernel invocation for whole multivector
wdp.z = Cdata(0,nR*nC).getRawPtr();
wdp.y = Bdata(0,nR*nC).getRawPtr();
wdp.x = Adata(0,nR*nC).getRawPtr();
node->template parallel_for<GESUMOp3<Scalar> >(0,nR*nC,wdp);
}
else {
// one kernel invocation for each column
for (size_t j=0; j<nC; ++j) {
wdp.z = Cdata(0,nR).getRawPtr();
wdp.y = Bdata(0,nR).getRawPtr();
wdp.x = Adata(0,nR).getRawPtr();
node->template parallel_for<GESUMOp3<Scalar> >(0,nR,wdp);
Adata += Astride;
Bdata += Bstride;
Cdata += Cstride;
}
}
}
}
static void
Norm1 (const MultiVector<Scalar,Node> &A,
const ArrayView<typename Teuchos::ScalarTraits<Scalar>::magnitudeType> &norms)
{
typedef Teuchos::ScalarTraits<Scalar> STS;
typedef typename STS::magnitudeType magnitude_type;
typedef Teuchos::ScalarTraits<magnitude_type> STM;
const size_t nR = A.getNumRows();
const size_t nC = A.getNumCols();
const size_t Astride = A.getStride();
TEUCHOS_TEST_FOR_EXCEPTION(
nC > Teuchos::as<size_t>(norms.size()),
std::runtime_error,
"DefaultArithmetic<" << Teuchos::typeName(A) << ">::Norm1(A,norms): "
"norms must have length as large as number of columns of A.");
if (nR*nC == 0) {
std::fill (norms.begin(), norms.begin() + nC, STM::zero ());
return;
}
RCP<Node> node = A.getNode();
ArrayRCP<const Scalar> Adata = A.getValues();
// prepare buffers
ReadyBufferHelper<Node> rbh(node);
rbh.begin();
rbh.template addConstBuffer<Scalar>(Adata);
rbh.end();
SumAbsOp<Scalar> op;
for (size_t j=0; j<nC; ++j) {
op.x = Adata(0,nR).getRawPtr();
norms[j] = node->parallel_reduce(0,nR,op);
Adata += Astride;
}
}
static typename Teuchos::ScalarTraits<Scalar>::magnitudeType
Norm1 (const MultiVector<Scalar,Node> &A)
{
typedef Teuchos::ScalarTraits<Scalar> STS;
typedef typename STS::magnitudeType magnitude_type;
typedef Teuchos::ScalarTraits<magnitude_type> STM;
const size_t nR = A.getNumRows();
const size_t nC = A.getNumCols();
if (nR*nC == 0) {
return STM::zero ();
}
RCP<Node> node = A.getNode();
ArrayRCP<const Scalar> Adata = A.getValues(0);
// prepare buffers
ReadyBufferHelper<Node> rbh(node);
rbh.begin();
rbh.template addConstBuffer<Scalar>(Adata);
rbh.end();
SumAbsOp<Scalar> op;
op.x = Adata(0,nR).getRawPtr();
return node->parallel_reduce(0,nR,op);
}
static void
Sum (const MultiVector<Scalar,Node> &A,
const ArrayView<Scalar> &sums)
{
const size_t nR = A.getNumRows();
const size_t nC = A.getNumCols();
const size_t Astride = A.getStride();
TEUCHOS_TEST_FOR_EXCEPTION(
nC > (size_t)sums.size(),
std::runtime_error,
"DefaultArithmetic<" << Teuchos::typeName(A) << ">::Sum(A,sums): "
"sums must have length as large as number of columns of A.");
if (nR*nC == 0) {
std::fill( sums.begin(), sums.begin() + nC, Teuchos::ScalarTraits<Scalar>::zero() );
return;
}
RCP<Node> node = A.getNode();
ArrayRCP<const Scalar> Adata = A.getValues();
// prepare buffers
ReadyBufferHelper<Node> rbh(node);
rbh.begin();
rbh.template addConstBuffer<Scalar>(Adata);
rbh.end();
SumOp<Scalar> op;
for (size_t j=0; j<nC; ++j) {
op.x = Adata(0,nR).getRawPtr();
sums[j] = node->parallel_reduce(0,nR,op);
Adata += Astride;
}
}
static Scalar Sum(const MultiVector<Scalar,Node> &A) {
const size_t nR = A.getNumRows();
const size_t nC = A.getNumCols();
if (nR*nC == 0) {
return Teuchos::ScalarTraits<Scalar>::zero();
}
RCP<Node> node = A.getNode();
ArrayRCP<const Scalar> Adata = A.getValues(0);
// prepare buffers
ReadyBufferHelper<Node> rbh(node);
rbh.begin();
rbh.template addConstBuffer<Scalar>(Adata);
rbh.end();
SumOp<Scalar> op;
op.x = Adata(0,nR).getRawPtr();
return node->parallel_reduce(0,nR,op);
}
static typename Teuchos::ScalarTraits<Scalar>::magnitudeType NormInf(const MultiVector<Scalar,Node> &A) {
const size_t nR = A.getNumRows();
const size_t nC = A.getNumCols();
if (nR*nC == 0) {
return Teuchos::ScalarTraits<typename Teuchos::ScalarTraits<Scalar>::magnitudeType>::zero();
}
RCP<Node> node = A.getNode();
ArrayRCP<const Scalar> Adata = A.getValues(0);
// prepare buffers
ReadyBufferHelper<Node> rbh(node);
rbh.begin();
rbh.template addConstBuffer<Scalar>(Adata);
rbh.end();
MaxAbsOp<Scalar> op;
op.x = Adata(0,nR).getRawPtr();
return node->parallel_reduce(0,nR,op);
}
static void NormInf(const MultiVector<Scalar,Node> &A, const ArrayView<typename Teuchos::ScalarTraits<Scalar>::magnitudeType> &norms) {
const size_t nR = A.getNumRows();
const size_t nC = A.getNumCols();
const size_t Astride = A.getStride();
TEUCHOS_TEST_FOR_EXCEPTION(nC > Teuchos::as<size_t>(norms.size()), std::runtime_error,
"DefaultArithmetic<" << Teuchos::typeName(A) << ">::NormInf(A,norms): norms must have length as large as number of columns of A.");
if (nR*nC == 0) {
std::fill( norms.begin(), norms.begin() + nC, Teuchos::ScalarTraits<typename Teuchos::ScalarTraits<Scalar>::magnitudeType>::zero() );
return;
}
RCP<Node> node = A.getNode();
ArrayRCP<const Scalar> Adata = A.getValues();
// prepare buffers
ReadyBufferHelper<Node> rbh(node);
rbh.begin();
rbh.template addConstBuffer<Scalar>(Adata);
rbh.end();
MaxAbsOp<Scalar> op;
for (size_t j=0; j<nC; ++j) {
op.x = Adata(0,nR).getRawPtr();
norms[j] = node->parallel_reduce(0,nR,op);
Adata += Astride;
}
}
static void Norm2Squared(const MultiVector<Scalar,Node> &A, const ArrayView<typename Teuchos::ScalarTraits<Scalar>::magnitudeType> &norms) {
const size_t nR = A.getNumRows();
const size_t nC = A.getNumCols();
const size_t Astride = A.getStride();
TEUCHOS_TEST_FOR_EXCEPTION(nC > Teuchos::as<size_t>(norms.size()), std::runtime_error,
"DefaultArithmetic<" << Teuchos::typeName(A) << ">::Norm2Squared(A,norms): norms must have length as large as number of columns of A.");
if (nR*nC == 0) {
std::fill( norms.begin(), norms.begin() + nC, Teuchos::ScalarTraits<typename Teuchos::ScalarTraits<Scalar>::magnitudeType>::zero() );
return;
}
RCP<Node> node = A.getNode();
ArrayRCP<const Scalar> Adata = A.getValues();
// prepare buffers
ReadyBufferHelper<Node> rbh(node);
rbh.begin();
rbh.template addConstBuffer<Scalar>(Adata);
rbh.end();
DotOp1<Scalar> op;
for (size_t j=0; j<nC; ++j) {
op.x = Adata(0,nR).getRawPtr();
norms[j] = node->parallel_reduce(0,nR,op);
Adata += Astride;
}
}
static typename Teuchos::ScalarTraits<Scalar>::magnitudeType
Norm2Squared(const MultiVector<Scalar,Node> &A) {
const size_t nR = A.getNumRows();
if (nR == 0) {
return Teuchos::ScalarTraits<typename Teuchos::ScalarTraits<Scalar>::magnitudeType>::zero();
}
RCP<Node> node = A.getNode();
ArrayRCP<const Scalar> Adata = A.getValues(0);
// prepare buffers
ReadyBufferHelper<Node> rbh(node);
rbh.begin();
rbh.template addConstBuffer<Scalar>(Adata);
rbh.end();
DotOp1<Scalar> op;
op.x = Adata(0,nR).getRawPtr();
return node->parallel_reduce(0,nR,op);
}
static typename Teuchos::ScalarTraits<Scalar>::magnitudeType
WeightedNorm(const MultiVector<Scalar,Node> &A, const MultiVector<Scalar,Node> &weightVector) {
const size_t nR = A.getNumRows();
const size_t nC = A.getNumCols();
if (nR*nC == 0) {
return Teuchos::ScalarTraits<typename Teuchos::ScalarTraits<Scalar>::magnitudeType>::zero();
}
RCP<Node> node = A.getNode();
ArrayRCP<const Scalar> Adata = A.getValues(0),
Wdata = weightVector.getValues(0);
// prepare buffers
ReadyBufferHelper<Node> rbh(node);
rbh.begin();
rbh.template addConstBuffer<Scalar>(Adata);
rbh.template addConstBuffer<Scalar>(Wdata);
rbh.end();
WeightNormOp<Scalar> op;
op.x = Adata(0,nR).getRawPtr();
op.w = Wdata(0,nR).getRawPtr();
return node->parallel_reduce(0,nR,op);
}
static void WeightedNorm(const MultiVector<Scalar,Node> &A, const MultiVector<Scalar,Node> &weightVector, const ArrayView<typename Teuchos::ScalarTraits<Scalar>::magnitudeType> &norms) {
const size_t nR = A.getNumRows();
const size_t nC = A.getNumCols();
const size_t Astride = A.getStride(),
Wstride = weightVector.getStride();
TEUCHOS_TEST_FOR_EXCEPTION(nC > Teuchos::as<size_t>(norms.size()), std::runtime_error,
"DefaultArithmetic<" << Teuchos::typeName(A) << ">::Norm1(A,norms): norms must have length as large as number of columns of A.");
if (nR*nC == 0) {
std::fill( norms.begin(), norms.begin() + nC, Teuchos::ScalarTraits<typename Teuchos::ScalarTraits<Scalar>::magnitudeType>::zero() );
return;
}
RCP<Node> node = A.getNode();
ArrayRCP<const Scalar> Adata = A.getValues(),
Wdata = weightVector.getValues();
const bool OneW = (weightVector.getNumCols() == 1);
// prepare buffers
ReadyBufferHelper<Node> rbh(node);
rbh.begin();
rbh.template addConstBuffer<Scalar>(Adata);
rbh.template addConstBuffer<Scalar>(Wdata);
rbh.end();
WeightNormOp<Scalar> op;
if (OneW) {
op.w = Wdata(0,nR).getRawPtr();
for (size_t j=0; j<nC; ++j) {
op.x = Adata(0,nR).getRawPtr();
norms[j] = node->parallel_reduce(0,nR,op);
Adata += Astride;
}
}
else {
for (size_t j=0; j<nC; ++j) {
op.x = Adata(0,nR).getRawPtr();
op.w = Wdata(0,nR).getRawPtr();
norms[j] = node->parallel_reduce(0,nR,op);
Adata += Astride;
Wdata += Wstride;
}
}
}
static void Abs(MultiVector<Scalar,Node> &A, const MultiVector<Scalar,Node> &B) {
const size_t nR = A.getNumRows();
const size_t nC = A.getNumCols();
const size_t Astride = A.getStride();
const size_t Bstride = B.getStride();
TEUCHOS_TEST_FOR_EXCEPTION(nC != B.getNumCols() || nR != B.getNumRows(), std::runtime_error,
"DefaultArithmetic<" << Teuchos::typeName(A) << ">::Abs(A,B): A and B must have the same dimensions.");
if (nC*nR == 0) return;
RCP<Node> node = A.getNode();
ArrayRCP<const Scalar> Bdata = B.getValues();
ArrayRCP<Scalar> Adata = A.getValuesNonConst();
// prepare buffers
ReadyBufferHelper<Node> rbh(node);
rbh.begin();
rbh.template addConstBuffer<Scalar>(Bdata);
rbh.template addNonConstBuffer<Scalar>(Adata);
rbh.end();
// prepare op
AbsOp<Scalar> wdp;
if (Astride == nR && Bstride == nR) {
// one kernel invocation for whole multivector assignment
wdp.x = Adata(0,nR*nC).getRawPtr();
wdp.y = Bdata(0,nR*nC).getRawPtr();
node->template parallel_for<AbsOp<Scalar> >(0,nR*nC,wdp);
}
else {
// one kernel invocation for each column
for (size_t j=0; j<nC; ++j) {
wdp.x = Adata(0,nR).getRawPtr();
wdp.y = Bdata(0,nR).getRawPtr();
node->template parallel_for<AbsOp<Scalar> >(0,nR,wdp);
Adata += Astride;
Bdata += Bstride;
}
}
}
static void Scale(MultiVector<Scalar,Node> &B, Scalar alpha, const MultiVector<Scalar,Node> &A) {
const size_t nR = A.getNumRows();
const size_t nC = A.getNumCols();
const size_t Astride = A.getStride();
const size_t Bstride = B.getStride();
TEUCHOS_TEST_FOR_EXCEPTION(nC != B.getNumCols() || nR != B.getNumRows(), std::runtime_error,
"DefaultArithmetic<" << Teuchos::typeName(A) << ">::Scale(B,alpha,A): A and B must have the same dimensions.");
RCP<Node> node = B.getNode();
ArrayRCP<const Scalar> Adata = A.getValues();
ArrayRCP<Scalar> Bdata = B.getValuesNonConst();
// prepare buffers
ReadyBufferHelper<Node> rbh(node);
rbh.begin();
rbh.template addConstBuffer<Scalar>(Adata);
rbh.template addNonConstBuffer<Scalar>(Bdata);
rbh.end();
MVScaleOp<Scalar> wdp;
wdp.alpha = alpha;
if (Astride == nR && Bstride == nR) {
// one kernel invocation for whole multivector
wdp.x = Bdata(0,nR*nC).getRawPtr();
wdp.y = Adata(0,nR*nC).getRawPtr();
node->template parallel_for<MVScaleOp<Scalar> >(0,nR*nC,wdp);
}
else {
// one kernel invocation for each column
for (size_t j=0; j<nC; ++j) {
wdp.x = Bdata(0,nR).getRawPtr();
wdp.y = Adata(0,nR).getRawPtr();
node->template parallel_for<MVScaleOp<Scalar> >(0,nR,wdp);
Adata += Astride;
Bdata += Bstride;
}
}
}
static void Scale(MultiVector<Scalar,Node> &A, Scalar alpha) {
const size_t nR = A.getNumRows();
const size_t nC = A.getNumCols();
const size_t stride = A.getStride();
RCP<Node> node = A.getNode();
ArrayRCP<Scalar> data = A.getValuesNonConst();
// prepare buffers
ReadyBufferHelper<Node> rbh(node);
rbh.begin();
rbh.template addNonConstBuffer<Scalar>(data);
rbh.end();
// prepare op
SingleScaleOp<Scalar> wdp;
wdp.alpha = alpha;
if (stride == nR) {
// one kernel invocation for whole multivector
wdp.x = data(0,nR*nC).getRawPtr();
node->template parallel_for<SingleScaleOp<Scalar> >(0,nR*nC,wdp);
}
else {
// one kernel invocation for each column
for (size_t j=0; j<nC; ++j) {
wdp.x = data(0,nR).getRawPtr();
node->template parallel_for<SingleScaleOp<Scalar> >(0,nR,wdp);
data += stride;
}
}
}
};
} // namespace KokkosClassic
#endif
|