/usr/include/trilinos/Kokkos_DefaultArithmetic.hpp is in libtrilinos-dev 10.4.0.dfsg-1ubuntu2.
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 | //@HEADER
// ************************************************************************
//
// Kokkos: Node API and Parallel Node Kernels
// Copyright (2009) Sandia Corporation
//
// Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
// license for use of this work by or on behalf of the U.S. Government.
//
// This library is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as
// published by the Free Software Foundation; either version 2.1 of the
// License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
// USA
// Questions? Contact Michael A. Heroux (maherou@sandia.gov)
//
// ************************************************************************
//@HEADER
#ifndef KOKKOS_DEFAULTARITHMETIC_H
#define KOKKOS_DEFAULTARITHMETIC_H
#include <Teuchos_BLAS_types.hpp>
#include <Teuchos_TestForException.hpp>
#include <Teuchos_TypeNameTraits.hpp>
#include <Teuchos_ArrayView.hpp>
#include <Teuchos_Tuple.hpp>
#include <stdexcept>
#include "Kokkos_MultiVector.hpp"
#include "Kokkos_MultiVectorKernelOps.hpp"
#include "Kokkos_NodeHelpers.hpp"
#ifdef HAVE_KOKKOS_TBB
#include "Kokkos_TBBNode.hpp"
#endif
#ifdef HAVE_KOKKOS_THREADPOOL
#include "Kokkos_TPINode.hpp"
#endif
#ifdef HAVE_KOKKOS_THRUST
#include "Kokkos_ThrustGPUNode.hpp"
#include "cublas.h"
#endif
#include "Kokkos_SerialNode.hpp"
#include <Teuchos_BLAS.hpp>
namespace Kokkos {
//! 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) {
TEST_FOR_EXCEPT(true);
}
};
template <typename Scalar>
struct NodeGEMM<Scalar,SerialNode> {
public:
static void GEMM(Teuchos::ETransp transA, Teuchos::ETransp transB, Scalar alpha, const MultiVector<Scalar,SerialNode> &A, const MultiVector<Scalar,SerialNode> &B, Scalar beta, MultiVector<Scalar,SerialNode> &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());
blas.GEMM(transA, transB, m, n, k, alpha, A.getValues().getRawPtr(), lda, B.getValues().getRawPtr(), ldb, beta, C.getValuesNonConst().getRawPtr(), ldc);
}
};
#ifdef HAVE_KOKKOS_TBB
template <typename Scalar>
struct NodeGEMM<Scalar,TBBNode> {
public:
static void GEMM(Teuchos::ETransp transA, Teuchos::ETransp transB, Scalar alpha, const MultiVector<Scalar,TBBNode> &A, const MultiVector<Scalar,TBBNode> &B, Scalar beta, MultiVector<Scalar,TBBNode> &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());
blas.GEMM(transA, transB, m, n, k, alpha, A.getValues().getRawPtr(), lda, B.getValues().getRawPtr(), ldb, beta, C.getValuesNonConst().getRawPtr(), ldc);
}
};
#endif
#ifdef HAVE_KOKKOS_THREADPOOL
template <typename Scalar>
struct NodeGEMM<Scalar,TPINode> {
public:
static void GEMM(Teuchos::ETransp transA, Teuchos::ETransp transB, Scalar alpha, const MultiVector<Scalar,TPINode> &A, const MultiVector<Scalar,TPINode> &B, Scalar beta, MultiVector<Scalar,TPINode> &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());
blas.GEMM(transA, transB, m, n, k, alpha, A.getValues().getRawPtr(), lda, B.getValues().getRawPtr(), ldb, beta, C.getValuesNonConst().getRawPtr(), ldc);
}
};
#endif
#ifdef HAVE_KOKKOS_THRUST
template <typename Scalar>
struct NodeGEMM<Scalar,ThrustGPUNode> {
public:
static void GEMM(Teuchos::ETransp transA, Teuchos::ETransp transB, Scalar alpha, const MultiVector<Scalar,ThrustGPUNode> &A, const MultiVector<Scalar,ThrustGPUNode> &B, Scalar beta, MultiVector<Scalar,ThrustGPUNode> &C) {
TEST_FOR_EXCEPTION(true, std::logic_error, "NodeGEMM: ThrustGPUNode has no support for GEMM operations over Scalar=" << Teuchos::typeName(alpha) << ".");
}
};
#ifdef HAVE_KOKKOS_CUDA_FLOAT
template <>
struct NodeGEMM<float,ThrustGPUNode> {
public:
static void GEMM(Teuchos::ETransp transA, Teuchos::ETransp transB, float alpha, const MultiVector<float,ThrustGPUNode> &A, const MultiVector<float,ThrustGPUNode> &B, float beta, MultiVector<float,ThrustGPUNode> &C) {
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());
const char char_transA = (transA == Teuchos::NO_TRANS ? 'N' : 'T'),
char_transB = (transB == Teuchos::NO_TRANS ? 'N' : 'T');
cublasSgemm(char_transA, char_transB, m, n, k, alpha, A.getValues().getRawPtr(), lda, B.getValues().getRawPtr(), ldb, beta, C.getValuesNonConst().getRawPtr(), ldc);
#ifdef HAVE_KOKKOS_DEBUG
cublasStatus info = cublasGetError();
TEST_FOR_EXCEPTION( info != CUBLAS_STATUS_SUCCESS, std::runtime_error, "cublasSgemm failed with status " << info << "." );
#endif
}
};
#endif
#ifdef HAVE_KOKKOS_CUDA_DOUBLE
template <>
struct NodeGEMM<double,ThrustGPUNode> {
public:
static void GEMM(Teuchos::ETransp transA, Teuchos::ETransp transB, double alpha, const MultiVector<double,ThrustGPUNode> &A, const MultiVector<double,ThrustGPUNode> &B, double beta, MultiVector<double,ThrustGPUNode> &C) {
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());
const char char_transA = (transA == Teuchos::NO_TRANS ? 'N' : 'T'),
char_transB = (transB == Teuchos::NO_TRANS ? 'N' : 'T');
cublasDgemm(char_transA, char_transB, m, n, k, alpha, A.getValues().getRawPtr(), lda, B.getValues().getRawPtr(), ldb, beta, C.getValuesNonConst().getRawPtr(), ldc);
#ifdef HAVE_KOKKOS_DEBUG
cublasStatus info = cublasGetError();
TEST_FOR_EXCEPTION( info != CUBLAS_STATUS_SUCCESS, std::runtime_error, "cublasDgemm failed with status " << info << "." );
#endif
}
};
#endif
#endif
//! Class DefaultArithmetic, unimplemented
template <class MV>
class DefaultArithmetic {
// nothing here
};
//! partial specialization of class DefaultArithmetic, for Kokkos::MultiVector<Scalar,Node>
template <class Scalar, class Node>
class DefaultArithmetic<MultiVector<Scalar,Node> > {
public:
//! Initialize multivector to constant value.
inline static void Init(MultiVector<Scalar,Node> &A, Scalar alpha) {
const size_t nR = A.getNumRows();
const size_t nC = A.getNumCols();
const size_t stride = A.getStride();
Teuchos::RCP<Node> node = A.getNode();
Teuchos::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;
}
}
}
//! Set MultiVector to the reciprocal of another: B(i,j) = 1/A(i,j)
inline 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();
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.");
Teuchos::RCP<Node> node = B.getNode();
Teuchos::ArrayRCP<const Scalar> Bdata = B.getValues();
Teuchos::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;
}
}
}
//! Set MultiVector to the scaled element-wise multiple of two others:
/** C(i,j) = scalarC * C(i,j) + scalarAB * B(i,j) * A(i,1) (A has only 1 column)
*/
inline 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();
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();
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 num-rows, B and C must have the same num-cols.");
Teuchos::RCP<Node> node = B.getNode();
Teuchos::ArrayRCP<Scalar> Cdata = C.getValuesNonConst();
Teuchos::ArrayRCP<const Scalar> Bdata = B.getValues();
Teuchos::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();
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;
}
}
//! Assign one MultiVector to another
inline 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();
TEST_FOR_EXCEPTION(nC != B.getNumCols() || nR != B.getNumRows(), std::runtime_error,
"DefaultArithmetic<" << Teuchos::typeName(A) << ">::Assign(A,B): A and B must have the same dimensions.");
if (nC*nR == 0) return;
Teuchos::RCP<Node> node = A.getNode();
Teuchos::ArrayRCP<const Scalar> Bdata = B.getValues();
Teuchos::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
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;
}
}
}
inline static void Dot(const MultiVector<Scalar,Node> &A, const MultiVector<Scalar,Node> &B, const Teuchos::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();
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.");
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;
}
Teuchos::RCP<Node> node = A.getNode();
Teuchos::ArrayRCP<const Scalar> Bdata = B.getValues(),
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;
}
}
inline 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();
TEST_FOR_EXCEPTION(nR != B.getNumRows(), std::runtime_error,
"DefaultArithmetic<" << Teuchos::typeName(A) << ">::Dot(A,B,dots): A and B must have the same number of rows.");
if (nR*nC == 0) {
return Teuchos::ScalarTraits<Scalar>::zero();
}
Teuchos::RCP<Node> node = A.getNode();
Teuchos::ArrayRCP<const Scalar> Bdata = B.getValues(0),
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);
}
inline 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);
}
inline 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();
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.");
Teuchos::RCP<Node> node = B.getNode();
Teuchos::ArrayRCP<const Scalar> Adata = A.getValues();
Teuchos::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();
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;
}
}
}
inline 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();
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.");
Teuchos::RCP<Node> node = B.getNode();
Teuchos::ArrayRCP<const Scalar> Adata = A.getValues(),
Bdata = B.getValues();
Teuchos::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();
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;
}
}
}
inline static void Norm1(const MultiVector<Scalar,Node> &A, const Teuchos::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();
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;
}
Teuchos::RCP<Node> node = A.getNode();
Teuchos::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;
}
}
inline static typename Teuchos::ScalarTraits<Scalar>::magnitudeType
Norm1(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();
}
Teuchos::RCP<Node> node = A.getNode();
Teuchos::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);
}
inline static void Sum(const MultiVector<Scalar,Node> &A, const Teuchos::ArrayView<Scalar> &sums) {
const size_t nR = A.getNumRows();
const size_t nC = A.getNumCols();
const size_t Astride = A.getStride();
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;
}
Teuchos::RCP<Node> node = A.getNode();
Teuchos::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;
}
}
inline 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();
}
Teuchos::RCP<Node> node = A.getNode();
Teuchos::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);
}
inline 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();
}
Teuchos::RCP<Node> node = A.getNode();
Teuchos::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);
}
inline static void NormInf(const MultiVector<Scalar,Node> &A, const Teuchos::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();
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;
}
Teuchos::RCP<Node> node = A.getNode();
Teuchos::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;
}
}
inline static void Norm2Squared(const MultiVector<Scalar,Node> &A, const Teuchos::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();
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;
}
Teuchos::RCP<Node> node = A.getNode();
Teuchos::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;
}
}
inline 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();
}
Teuchos::RCP<Node> node = A.getNode();
Teuchos::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);
}
inline 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();
}
Teuchos::RCP<Node> node = A.getNode();
Teuchos::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);
}
inline static void WeightedNorm(const MultiVector<Scalar,Node> &A, const MultiVector<Scalar,Node> &weightVector, const Teuchos::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();
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;
}
Teuchos::RCP<Node> node = A.getNode();
Teuchos::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;
}
}
}
inline 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;
Teuchos::RCP<Node> node = A.getNode();
Teuchos::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);
Teuchos::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 = Teuchos::null;
}
inline 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();
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;
Teuchos::RCP<Node> node = A.getNode();
Teuchos::ArrayRCP<const Scalar> Bdata = B.getValues();
Teuchos::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;
}
}
}
inline 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();
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.");
Teuchos::RCP<Node> node = B.getNode();
Teuchos::ArrayRCP<const Scalar> Adata = A.getValues();
Teuchos::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;
}
}
}
inline 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();
Teuchos::RCP<Node> node = A.getNode();
Teuchos::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;
}
}
}
inline static void initializeValues(MultiVector<Scalar,Node> &A,
size_t numRows, size_t numCols,
const Teuchos::ArrayRCP<Scalar> &values,
size_t stride) {
A.initializeValues(numRows,numCols,values,stride);
}
inline static Teuchos::ArrayRCP<const Scalar> getValues(const MultiVector<Scalar,Node> &A) {
return A.getValues();
}
inline static Teuchos::ArrayRCP<const Scalar> getValues(const MultiVector<Scalar,Node> &A, size_t j) {
return A.getValues(j);
}
inline static Teuchos::ArrayRCP<Scalar> getValuesNonConst(MultiVector<Scalar,Node> &A) {
return A.getValuesNonConst();
}
inline static Teuchos::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 Teuchos::RCP<Node> getNode(const MultiVector<Scalar,Node> &A) {
return A.getNode();
}
};
}
#endif
|