10#ifndef TPETRA_BLOCKVIEW_HPP
11#define TPETRA_BLOCKVIEW_HPP
21#include "TpetraCore_config.h"
22#include "Kokkos_ArithTraits.hpp"
23#include "Kokkos_Complex.hpp"
40template<
class ViewType1,
42 const int rank1 = ViewType1::rank>
60 static_assert (
static_cast<int> (ViewType1::rank) ==
static_cast<int> (ViewType2::rank),
61 "AbsMax: ViewType1 and ViewType2 must have the same rank.");
62 typedef typename std::remove_reference<
decltype (
Y(0,0)) >::type
STY;
63 static_assert(! std::is_const<STY>::value,
64 "AbsMax: The type of each entry of Y must be nonconst.");
65 typedef typename std::decay<
decltype (
X(0,0)) >::type
STX;
66 static_assert( std::is_same<STX, STY>::value,
67 "AbsMax: The type of each entry of X and Y must be the same.");
68 typedef Kokkos::ArithTraits<STY>
KAT;
70 const int numCols =
Y.extent (1);
72 for (
int j = 0;
j < numCols; ++
j) {
100 static_assert (
static_cast<int> (ViewType1::rank) ==
static_cast<int> (ViewType2::rank),
101 "AbsMax: ViewType1 and ViewType2 must have the same rank.");
103 typedef typename std::remove_reference<
decltype (
Y(0)) >::type
STY;
104 static_assert(! std::is_const<STY>::value,
105 "AbsMax: The type of each entry of Y must be nonconst.");
106 typedef typename std::remove_const<
typename std::remove_reference<
decltype (
X(0)) >::type>::type
STX;
107 static_assert( std::is_same<STX, STY>::value,
108 "AbsMax: The type of each entry of X and Y must be the same.");
109 typedef Kokkos::ArithTraits<STY>
KAT;
132template<
class ViewType1,
class ViewType2, const
int rank = ViewType1::rank>
136 static_assert (
static_cast<int> (ViewType1::rank) ==
static_cast<int> (ViewType2::rank),
137 "absMax: ViewType1 and ViewType2 must have the same rank.");
149 const int rank = ViewType::rank>
168#if defined(KOKKOS_ENABLE_PRAGMA_UNROLL)
202 using x_value_type =
typename std::decay<
decltype (*
x.data()) >::type;
205#if defined(KOKKOS_ENABLE_PRAGMA_UNROLL)
217template<
class ViewType,
219 class IndexType = int,
220 const bool is_contiguous =
false,
221 const int rank = ViewType::rank>
267#if defined(KOKKOS_ENABLE_PRAGMA_UNROLL)
280template<
class CoefficientType,
283 class IndexType = int,
284 const bool is_contiguous =
false,
285 const int rank = ViewType1::rank>
306 using Kokkos::ArithTraits;
307 static_assert (
static_cast<int> (ViewType1::rank) ==
static_cast<int> (ViewType2::rank),
308 "AXPY: x and y must have the same rank.");
332 using Kokkos::ArithTraits;
333 static_assert (
static_cast<int> (ViewType1::rank) ==
static_cast<int> (ViewType2::rank),
334 "AXPY: X and Y must have the same rank.");
358 using Kokkos::ArithTraits;
359 static_assert (
static_cast<int> (ViewType1::rank) ==
static_cast<int> (ViewType2::rank),
360 "AXPY: x and y must have the same rank.");
363 using x_value_type =
typename std::decay<
decltype (*
x.data()) >::type;
364 using y_value_type =
typename std::decay<
decltype (*
y.data()) >::type;
368#if defined(KOKKOS_ENABLE_PRAGMA_UNROLL)
381template<
class ViewType1,
383 class IndexType = int,
384 const bool is_contiguous =
false,
385 const int rank = ViewType1::rank>
437 using x_value_type =
typename std::decay<
decltype (*
x.data()) >::type;
438 using y_value_type =
typename std::decay<
decltype (*
y.data()) >::type;
442#if defined(KOKKOS_ENABLE_PRAGMA_UNROLL)
450template<
class VecType1,
454 class IndexType = int,
455 bool is_contiguous =
false,
456 class BlkLayoutType =
typename BlkType::array_layout>
458 static KOKKOS_INLINE_FUNCTION
void
459 run (
const CoeffType& alpha,
464 static_assert (VecType1::rank == 1,
"GEMV: VecType1 must have rank 1.");
465 static_assert (BlkType::rank == 2,
"GEMV: BlkType must have rank 2.");
466 static_assert (VecType2::rank == 1,
"GEMV: VecType2 must have rank 1.");
468 const IndexType numRows =
static_cast<IndexType
> (A.extent (0));
469 const IndexType numCols =
static_cast<IndexType
> (A.extent (1));
472 for (IndexType i = 0; i < numRows; ++i)
473 for (IndexType j = 0; j < numCols; ++j)
474 y(i) += alpha * A(i,j) * x(j);
478template<
class VecType1,
483struct GEMV<VecType1,BlkType,VecType2,CoeffType,IndexType,true,Kokkos::LayoutLeft> {
484 static KOKKOS_INLINE_FUNCTION
void
485 run (
const CoeffType& alpha,
490 static_assert (VecType1::rank == 1,
"GEMV: VecType1 must have rank 1.");
491 static_assert (BlkType::rank == 2,
"GEMV: BlkType must have rank 2.");
492 static_assert (VecType2::rank == 1,
"GEMV: VecType2 must have rank 1.");
494 using A_value_type =
typename std::decay<
decltype (A(0,0)) >::type;
495 using x_value_type =
typename std::decay<
decltype (x(0)) >::type;
496 using y_value_type =
typename std::decay<
decltype (y(0)) >::type;
498 const IndexType numRows =
static_cast<IndexType
> (A.extent (0));
499 const IndexType numCols =
static_cast<IndexType
> (A.extent (1));
501 const A_value_type *__restrict A_ptr(A.data());
const IndexType as1(A.stride(1));
502 const x_value_type *__restrict x_ptr(x.data());
503 y_value_type *__restrict y_ptr(y.data());
505#if defined(KOKKOS_ENABLE_PRAGMA_UNROLL)
508 for (IndexType j=0;j<numCols;++j) {
509 const x_value_type x_at_j = alpha*x_ptr[j];
510 const A_value_type *__restrict A_at_j = A_ptr + j*as1;
511#if defined(KOKKOS_ENABLE_PRAGMA_UNROLL)
514 for (IndexType i=0;i<numRows;++i)
515 y_ptr[i] += A_at_j[i] * x_at_j;
520template<
class VecType1,
525struct GEMV<VecType1,BlkType,VecType2,CoeffType,IndexType,true,Kokkos::LayoutRight> {
526 static KOKKOS_INLINE_FUNCTION
void
527 run (
const CoeffType& alpha,
532 static_assert (VecType1::rank == 1,
"GEMV: VecType1 must have rank 1.");
533 static_assert (BlkType::rank == 2,
"GEMV: BlkType must have rank 2.");
534 static_assert (VecType2::rank == 1,
"GEMV: VecType2 must have rank 1.");
536 using A_value_type =
typename std::decay<
decltype (A(0,0)) >::type;
537 using x_value_type =
typename std::decay<
decltype (x(0)) >::type;
538 using y_value_type =
typename std::decay<
decltype (y(0)) >::type;
540 const IndexType numRows =
static_cast<IndexType
> (A.extent (0));
541 const IndexType numCols =
static_cast<IndexType
> (A.extent (1));
543 const A_value_type *__restrict A_ptr(A.data());
const IndexType as0(A.stride(0));
544 const x_value_type *__restrict x_ptr(x.data());
545 y_value_type *__restrict y_ptr(y.data());
547#if defined(KOKKOS_ENABLE_PRAGMA_UNROLL)
550 for (IndexType i=0;i<numRows;++i) {
551 y_value_type y_at_i(0);
552 const auto A_at_i = A_ptr + i*as0;
553#if defined(KOKKOS_ENABLE_PRAGMA_UNROLL)
556 for (IndexType j=0;j<numCols;++j)
557 y_at_i += A_at_i[j] * x_ptr[j];
558 y_ptr[i] += alpha*y_at_i;
567template<
class ViewType,
568 class CoefficientType,
569 class IndexType = int,
570 const int rank = ViewType::rank>
571KOKKOS_INLINE_FUNCTION
void
574 using LayoutType =
typename ViewType::array_layout;
575 constexpr bool is_contiguous = (std::is_same<LayoutType,Kokkos::LayoutLeft>::value ||
576 std::is_same<LayoutType,Kokkos::LayoutRight>::value);
581template<
class ViewType,
583 class IndexType = int,
584 const int rank = ViewType::rank>
585KOKKOS_INLINE_FUNCTION
void
588 using LayoutType =
typename ViewType::array_layout;
589 constexpr bool is_contiguous = (std::is_same<LayoutType,Kokkos::LayoutLeft>::value ||
590 std::is_same<LayoutType,Kokkos::LayoutRight>::value);
599template<
class CoefficientType,
602 class IndexType = int,
603 const int rank = ViewType1::rank>
604KOKKOS_INLINE_FUNCTION
void
609 static_assert (
static_cast<int> (ViewType1::rank) ==
610 static_cast<int> (ViewType2::rank),
611 "AXPY: x and y must have the same rank.");
612 using LayoutType1 =
typename ViewType1::array_layout;
613 using LayoutType2 =
typename ViewType2::array_layout;
614 constexpr bool is_layout_same = std::is_same<LayoutType1,LayoutType2>::value;
615 constexpr bool is_x_contiguous = (std::is_same<LayoutType1,Kokkos::LayoutLeft>::value ||
616 std::is_same<LayoutType1,Kokkos::LayoutRight>::value);
629template<
class ViewType1,
631 class IndexType = int,
632 const int rank = ViewType1::rank>
633KOKKOS_INLINE_FUNCTION
void
636 static_assert (
static_cast<int> (ViewType1::rank) ==
637 static_cast<int> (ViewType2::rank),
638 "COPY: x and y must have the same rank.");
639 using LayoutType1 =
typename ViewType1::array_layout;
640 using LayoutType2 =
typename ViewType2::array_layout;
641 constexpr bool is_layout_same = std::is_same<LayoutType1,LayoutType2>::value;
642 constexpr bool is_x_contiguous = (std::is_same<LayoutType1,Kokkos::LayoutLeft>::value ||
643 std::is_same<LayoutType1,Kokkos::LayoutRight>::value);
659template<
class VecType1,
663 class IndexType =
int>
664KOKKOS_INLINE_FUNCTION
void
670 constexpr bool is_A_contiguous = (std::is_same<typename BlkType::array_layout, Kokkos::LayoutLeft>::value ||
671 std::is_same<typename BlkType::array_layout, Kokkos::LayoutRight>::value);
672 constexpr bool is_x_contiguous = (std::is_same<typename VecType1::array_layout, Kokkos::LayoutLeft>::value ||
673 std::is_same<typename VecType1::array_layout, Kokkos::LayoutRight>::value);
674 constexpr bool is_y_contiguous = (std::is_same<typename VecType2::array_layout, Kokkos::LayoutLeft>::value ||
675 std::is_same<typename VecType2::array_layout, Kokkos::LayoutRight>::value);
677 Impl::GEMV<VecType1, BlkType, VecType2, CoeffType, IndexType, is_contiguous>::run (
alpha,
A,
x,
y);
687template<
class ViewType1,
690 class CoefficientType,
691 class IndexType =
int>
692KOKKOS_INLINE_FUNCTION
void
702 static_assert (ViewType1::rank == 2,
"GEMM: A must have rank 2 (be a matrix).");
703 static_assert (ViewType2::rank == 2,
"GEMM: B must have rank 2 (be a matrix).");
704 static_assert (ViewType3::rank == 2,
"GEMM: C must have rank 2 (be a matrix).");
706 typedef typename std::remove_reference<
decltype (
A(0,0))>::type
Scalar;
707 typedef Kokkos::ArithTraits<Scalar> STS;
829template<
class LittleBlockType,
830 class LittleVectorType>
831KOKKOS_INLINE_FUNCTION
void
835 typedef typename std::decay<
decltype (
ipiv(0)) >::type
IndexType;
836 static_assert (std::is_integral<IndexType>::value,
837 "GETF2: The type of each entry of ipiv must be an integer type.");
838 typedef typename std::remove_reference<
decltype (
A(0,0))>::type
Scalar;
839 static_assert (! std::is_const<Scalar>::value,
840 "GETF2: A must not be a const View (or LittleBlock).");
841 static_assert (! std::is_const<std::remove_reference<
decltype (
ipiv(0))>>::value,
842 "GETF2: ipiv must not be a const View (or LittleBlock).");
843 static_assert (LittleBlockType::rank == 2,
"GETF2: A must have rank 2 (be a matrix).");
844 typedef Kokkos::ArithTraits<Scalar> STS;
867 if(STS::abs(
A(
i,
j)) > STS::abs(
A(
jp,
j))) {
910template<
class LittleBlockType,
911 class LittleIntVectorType,
912 class LittleScalarVectorType,
913 const int rank = LittleScalarVectorType::rank>
916 run (
const char mode[],
929 run (
const char mode[],
936 typedef typename std::decay<
decltype (
ipiv(0))>::type
IndexType;
939 static_assert (std::is_integral<IndexType>::value &&
940 std::is_signed<IndexType>::value,
941 "GETRS: The type of each entry of ipiv must be a signed integer.");
942 typedef typename std::decay<
decltype (
A(0,0))>::type
Scalar;
943 static_assert (! std::is_const<std::remove_reference<
decltype (
B(0))>>::value,
944 "GETRS: B must not be a const View (or LittleBlock).");
945 static_assert (LittleBlockType::rank == 2,
"GETRS: A must have rank 2 (be a matrix).");
946 static_assert (LittleIntVectorType::rank == 1,
"GETRS: ipiv must have rank 1.");
947 static_assert (LittleScalarVectorType::rank == 1,
"GETRS: For this specialization, B must have rank 1.");
949 typedef Kokkos::ArithTraits<Scalar> STS;
970 if(
mode[0] ==
'n' ||
mode[0] ==
'N') {
1002 else if(
mode[0] ==
't' ||
mode[0] ==
'T') {
1007 else if (
mode[0] ==
'c' ||
mode[0] ==
'C') {
1025 run (
const char mode[],
1032 typedef typename std::decay<
decltype (
ipiv(0)) >::type
IndexType;
1033 static_assert (std::is_integral<IndexType>::value,
1034 "GETRS: The type of each entry of ipiv must be an integer type.");
1035 static_assert (! std::is_const<std::remove_reference<
decltype (
B(0)) > >::value,
1036 "GETRS: B must not be a const View (or LittleBlock).");
1037 static_assert (LittleBlockType::rank == 2,
"GETRS: A must have rank 2 (be a matrix).");
1038 static_assert (LittleIntVectorType::rank == 1,
"GETRS: ipiv must have rank 1.");
1039 static_assert (LittleScalarVectorType::rank == 2,
"GETRS: For this specialization, B must have rank 2.");
1048 auto B_cur = Kokkos::subview (
B, Kokkos::ALL (),
rhs);
1091template<
class LittleBlockType,
1092 class LittleIntVectorType,
1093 class LittleScalarVectorType>
1094KOKKOS_INLINE_FUNCTION
void
1101 typedef typename std::decay<
decltype (
ipiv(0))>::type
IndexType;
1104 static_assert (std::is_integral<IndexType>::value &&
1105 std::is_signed<IndexType>::value,
1106 "GETRI: The type of each entry of ipiv must be a signed integer.");
1107 typedef typename std::remove_reference<
decltype (
A(0,0))>::type
Scalar;
1108 static_assert (! std::is_const<std::remove_reference<
decltype (
A(0,0))>>::value,
1109 "GETRI: A must not be a const View (or LittleBlock).");
1110 static_assert (! std::is_const<std::remove_reference<
decltype (
work(0))>>::value,
1111 "GETRI: work must not be a const View (or LittleBlock).");
1112 static_assert (LittleBlockType::rank == 2,
"GETRI: A must have rank 2 (be a matrix).");
1113 typedef Kokkos::ArithTraits<Scalar> STS;
1196template<
class LittleBlockType,
1197 class LittleVectorTypeX,
1198 class LittleVectorTypeY,
1199 class CoefficientType,
1200 class IndexType =
int>
1201KOKKOS_INLINE_FUNCTION
void
1202GEMV (
const char trans,
1203 const CoefficientType& alpha,
1204 const LittleBlockType& A,
1205 const LittleVectorTypeX& x,
1206 const CoefficientType& beta,
1207 const LittleVectorTypeY& y)
1213 typedef typename std::remove_reference<
decltype (y(0)) >::type y_value_type;
1214 const IndexType numRows =
static_cast<IndexType
> (A.extent (0));
1215 const IndexType numCols =
static_cast<IndexType
> (A.extent (1));
1219 for (IndexType i = 0; i < numRows; ++i) {
1224 for (IndexType i = 0; i < numRows; ++i) {
1225 y_value_type y_i = 0.0;
1226 for (IndexType j = 0; j < numCols; ++j) {
1227 y_i += A(i,j) * x(j);
1236 for (IndexType i = 0; i < numRows; ++i) {
1241 for (IndexType i = 0; i < numRows; ++i) {
1247 for (IndexType i = 0; i < numRows; ++i) {
1248 y_value_type y_i = beta * y(i);
1249 for (IndexType j = 0; j < numCols; ++j) {
1250 y_i += alpha * A(i,j) * x(j);
Struct that holds views of the contents of a CrsMatrix.
KOKKOS_INLINE_FUNCTION void absMax(const ViewType2 &Y, const ViewType1 &X)
Implementation of Tpetra's ABSMAX CombineMode for the small dense blocks in BlockCrsMatrix,...
Namespace Tpetra contains the class and methods constituting the Tpetra library.
KOKKOS_INLINE_FUNCTION void GEMV(const CoeffType &alpha, const BlkType &A, const VecType1 &x, const VecType2 &y)
y := y + alpha * A * x (dense matrix-vector multiply)
KOKKOS_INLINE_FUNCTION void GETF2(const LittleBlockType &A, const LittleVectorType &ipiv, int &info)
Computes A = P*L*U.
KOKKOS_INLINE_FUNCTION void GETRI(const LittleBlockType &A, const LittleIntVectorType &ipiv, const LittleScalarVectorType &work, int &info)
Compute inverse of A, using result of GETRF or GETF2.
KOKKOS_INLINE_FUNCTION void AXPY(const CoefficientType &alpha, const ViewType1 &x, const ViewType2 &y)
y := y + alpha * x (dense vector or matrix update)
KOKKOS_INLINE_FUNCTION void FILL(const ViewType &x, const InputType &val)
Set every entry of x to val.
KOKKOS_INLINE_FUNCTION void SCAL(const CoefficientType &alpha, const ViewType &x)
x := alpha*x, where x is either rank 1 (a vector) or rank 2 (a matrix).
KOKKOS_INLINE_FUNCTION void COPY(const ViewType1 &x, const ViewType2 &y)
Deep copy x into y, where x and y are either rank 1 (vectors) or rank 2 (matrices) with the same dime...
KOKKOS_INLINE_FUNCTION void GETRS(const char mode[], const LittleBlockType &A, const LittleIntVectorType &ipiv, const LittleScalarVectorType &B, int &info)
Solve the linear system(s) AX=B, using the result of GETRF or GETF2.
KOKKOS_INLINE_FUNCTION void GEMM(const char transA[], const char transB[], const CoefficientType &alpha, const ViewType1 &A, const ViewType2 &B, const CoefficientType &beta, const ViewType3 &C)
Small dense matrix-matrix multiply: C := alpha*A*B + beta*C
@ ZERO
Replace old values with zero.
static KOKKOS_INLINE_FUNCTION void run(const CoefficientType &alpha, const ViewType1 &x, const ViewType2 &y)
y := y + alpha*x (rank-1 x and y, i.e., vectors)
static KOKKOS_INLINE_FUNCTION void run(const CoefficientType &alpha, const ViewType1 &X, const ViewType2 &Y)
Y := Y + alpha*X (rank-2 X and Y, i.e., matrices)
Implementation of Tpetra::AXPY function.
static KOKKOS_INLINE_FUNCTION void run(const ViewType2 &Y, const ViewType1 &X)
(*this)(i) := max(abs((*this)(i)), abs(X(i))) for all i.
static KOKKOS_INLINE_FUNCTION void run(const ViewType2 &Y, const ViewType1 &X)
(*this)(i,j) := max(abs((*this)(i,j)), abs(X(i,j))) for all (i,j).
Implementation of Tpetra's ABSMAX CombineMode for the small dense blocks in BlockCrsMatrix,...
static KOKKOS_INLINE_FUNCTION void run(const ViewType1 &x, const ViewType2 &y)
y := x (rank-1 x and y, i.e., vectors)
static KOKKOS_INLINE_FUNCTION void run(const ViewType1 &X, const ViewType2 &Y)
Y := X (rank-2 X and Y, i.e., matrices)
Implementation of Tpetra::COPY function.
Implementation of Tpetra::FILL function.
Computes the solution to Ax=b.
static KOKKOS_INLINE_FUNCTION void run(const CoefficientType &alpha, const ViewType &x)
x := alpha*x (rank-1 x, i.e., a vector)
static KOKKOS_INLINE_FUNCTION void run(const CoefficientType &alpha, const ViewType &A)
A := alpha*A (rank-2 A, i.e., a matrix)
Implementation of Tpetra::SCAL function.