10#ifndef BELOS_KOKKOS_DENSE_MAT_TRAITS_HPP
11#define BELOS_KOKKOS_DENSE_MAT_TRAITS_HPP
19#include "Teuchos_Assert.hpp"
20#include "Teuchos_RCP.hpp"
21#include "Teuchos_ScalarTraits.hpp"
22#include "Teuchos_BLAS.hpp"
23#include "Teuchos_LAPACK.hpp"
26#include "Kokkos_DualView.hpp"
27#include "Kokkos_Random.hpp"
28#include "KokkosKernels_ArithTraits.hpp"
29#include "KokkosBlas1_scal.hpp"
30#include "KokkosBlas1_axpby.hpp"
40 Kokkos::parallel_for(Kokkos::MDRangePolicy<
typename V::execution_space, Kokkos::Rank<2>>({0, 0}, {
dst.extent(0),
dst.extent(1)}),
43 dst(
i,
j) = KokkosKernels::ArithTraits<typename V::non_const_value_type>::conj(
src(
j,
i) );
48 template<
class Scalar,
class DM>
52 typedef typename KokkosKernels::ArithTraits<Scalar>::val_type
IST;
105 Teuchos::LAPACK<int,Scalar> lapack;
107 int M = DMT::GetNumRows(*
A_);
108 int N = DMT::GetNumCols(*
A_);
110 int LDA = DMT::GetStride(*
A_);
114 DMT::SyncDeviceToHost(*
A_);
136 for (
int j=0;
j<
N;
j++) {
139 for (
int i=0;
i<=
j;
i++) {
147 for (
int j=0;
j<
N;
j++) {
150 for (
int i=0;
i<
M;
i++) {
166 DMT::SyncHostToDevice(*
A_);
180 DMT::SyncDeviceToHost(*
X_);
184 DMT::Assign(*
X_, *
B_);
188 int M = DMT::GetNumRows(*
X_);
189 int NRHS = DMT::GetNumCols(*
X_);
190 int LDX = DMT::GetStride(*
X_);
201 for (
int i=0;
i<
M;
i++) {
210 int LDA = DMT::GetStride(*
A_);
212 Teuchos::LAPACK<int,Scalar> lapack;
218 lapack.GETRS(Teuchos::ETranspChar[
TRANS_],
M,
NRHS,
230 for (
int i=0;
i<
M;
i++) {
238 DMT::SyncHostToDevice(*
X_);
246 typedef typename Teuchos::ScalarTraits<Scalar>::magnitudeType MagnitudeType;
248 std::vector<int> IPIV_;
249 std::vector<MagnitudeType> R_, C_;
264 template<
class Scalar,
class... Properties>
268 typedef typename KokkosKernels::ArithTraits<Scalar>::val_type
IST;
278 return Teuchos::rcp(
new DM(
"BelosDenseCreate",0,0));
287 static Teuchos::RCP<DM>
293 return Teuchos::rcp(
new DM(Kokkos::view_alloc(Kokkos::WithoutInitializing,
"BelosDenseCreate2"),
numRows,
numCols));
301 static Teuchos::RCP<DM>
309 (Kokkos::view_alloc(Kokkos::WithoutInitializing,
"BelosDenseCreateCopy"),
dm.extent_int(1),
dm.extent_int(0)));
324 (Kokkos::view_alloc(Kokkos::WithoutInitializing,
"BelosDenseCreateCopy"),
dm.extent_int(0),
dm.extent_int(1)));
343 return reinterpret_cast<Scalar*
>(
dm.view_host().data());
356 return reinterpret_cast<Scalar const *
>(
dm.view_host().data());
361 static Teuchos::RCP<DM>
367 static Teuchos::RCP<const DM>
374 static Teuchos::RCP<DM>
381 (Kokkos::view_alloc(Kokkos::WithoutInitializing,
"BelosDenseSubViewCopy"),
numRows,
numCols));
397 return dm.extent_int(0);
402 return dm.extent_int(1);
430 Kokkos::deep_copy(
dm.view_device(), 0.0);
447 return reinterpret_cast<Scalar&
>((
dm.view_host())(
i,
j));
454 return reinterpret_cast<Scalar const &
>((
dm.view_host())(
i,
j));
466 if(
dm.need_sync_host()){
467 if(
dm.view_host().span_is_contiguous() &&
dm.view_device().span_is_contiguous()){
474 dm.clear_sync_state();
480 if(
dm.need_sync_device()){
481 if(
dm.view_host().span_is_contiguous() &&
dm.view_device().span_is_contiguous()){
489 dm.clear_sync_state();
501 KokkosBlas::axpy(1.0,
sourceDM.view_device(),
thisDM.view_device());
507 dm.clear_sync_state();
508 Kokkos::deep_copy(
dm.view_device(), value);
515 KokkosBlas::scal(
dm.view_device(), value,
dm.view_device());
524 dm.clear_sync_state();
525 Kokkos::fill_random(
dm.view_device(),
pool, -1,1);
536 using KAT = KokkosKernels::ArithTraits<IST>;
537 using mag_t =
typename KAT::mag_type;
541 Kokkos::parallel_reduce(Kokkos::MDRangePolicy<Kokkos::Rank<2>>({0, 0}, {
dm.extent(0),
dm.extent(1)}),
551 static typename Teuchos::ScalarTraits<Scalar>::magnitudeType
NormOne(
const DM&
dm) {
552 using KAT = KokkosKernels::ArithTraits<IST>;
553 using mag_t =
typename KAT::mag_type;
560 for(
int i = 0;
i <
dm.extent_int(0);
i++){
561 sum += KAT::abs((
dm.view_device())(
i,
j));
563 norm = Kokkos::max(norm,
sum);
564 }, Kokkos::Max<mag_t>(
max_sum));
572 static Teuchos::RCP<DenseSolver<Scalar, DM>>
575 Teuchos::RCP<DenseSolver<Scalar, DM>>
newSolver
static void Randomize(DM &dm)
Fill the Kokkos::DualView with random entries.
static int GetNumRows(const DM &dm)
Obtain the number of rows of dm.
static void SyncHostToDevice(DM &dm)
static Teuchos::RCP< DM > Subview(DM &source, int numRows, int numCols, int startRow=0, int startCol=0)
Returns an RCP to a Kokkos::DualView which has a subview of the given Kokkos::DualView.
static Teuchos::RCP< DM > SubviewCopy(const DM &source, int numRows, int numCols, int startRow=0, int startCol=0)
Returns a deep copy of the requested subview.
static Teuchos::RCP< DM > Create()
Creates a new empty Kokkos::DualView with no dimension.
static Teuchos::RCP< DenseSolver< Scalar, DM > > createDenseSolver()
Returns a dense solver object for the dense matrix.
static Teuchos::RCP< DM > Create(const int numRows, const int numCols, bool initZero=true)
Creates a new empty Kokkos::DualView containing and numRows rows and numCols columns....
static Scalar * GetRawHostPtr(DM &dm)
Returns a raw pointer to the (non-const) data on the host.
static Teuchos::RCP< DM > CreateCopy(const DM &dm, bool transpose=false)
Create a new copy DM, possibly transposed.
static int GetNumCols(const DM &dm)
Obtain the number of columns of dm.
static const Scalar & ValueConst(const DM &dm, const int i, const int j)
Access a const reference to the (i,j) entry of dm, e_i^T dm e_j.
static void Reshape(DM &dm, const int numRows, const int numCols, bool initZero=false)
KokkosKernels::ArithTraits< Scalar >::val_type IST
static Scalar & Value(DM &dm, const int i, const int j)
Access a reference to the (i,j) entry of dm, e_i^T dm e_j.
static void SyncDeviceToHost(DM &dm)
If an accelorator is in use, sync it to device on this call.
static Scalar const * GetConstRawHostPtr(const DM &dm)
Returns a raw pointer to const data on the host.
static int GetStride(const DM &dm)
Obtain the stride between the columns of dm.
static Teuchos::RCP< const DM > SubviewConst(const DM &source, int numRows, int numCols, int startRow=0, int startCol=0)
static void Assign(DM &dest, const DM &source)
Copies entries of source to dest (deep copy).
static Teuchos::ScalarTraits< Scalar >::magnitudeType NormOne(const DM &dm)
Returns the one-norm of the dense matrix.
static void PutScalar(DM &dm, Scalar value=Teuchos::ScalarTraits< Scalar >::zero())
Fill all entries with value. Value is zero if not specified.
static void Add(DM &thisDM, const DM &sourceDM)
Adds sourceDM to thisDM and returns answer in thisDM.
static void Scale(DM &dm, Scalar value)
Multiply all entries by a scalar. DM = value.*DM.
static Teuchos::ScalarTraits< Scalar >::magnitudeType NormFrobenius(const DM &dm)
Returns the Frobenius norm of the dense matrix.
Kokkos::DualView< IST **, Properties... > DM
Virtual base class which defines basic traits for the multi-vector type.
void setSPD(bool flag)
Set if dense matrix is symmetric positive definite.
void solveWithTransposeFlag(Teuchos::ETransp trans)
All subsequent function calls will work with the transpose-type set by this method (Teuchos::NO_TRANS...
virtual int setMatrix(const Teuchos::RCP< DM > &A)
Sets the pointers for coefficient matrix.
virtual int setVectors(const Teuchos::RCP< DM > &X, const Teuchos::RCP< DM > &B)
Sets the pointers for left and right hand side vector(s).
void factorWithEquilibration(bool flag)
Causes equilibration to be called just before the matrix factorization as part of the call to factor.
Full specialization of Belos::DenseMatSolver for Kokkos::DualView.
virtual ~KokkosDenseSolver()
KokkosDenseSolver destructor.
int solve()
Computes the solution X to AX = B for the this matrix and the B provided.
int factor()
Computes the in-place LU factorization of the matrix.
KokkosDenseSolver()
Default constructor; matrix should be set using setMatrix(), LHS and RHS set with setVectors().
KokkosKernels::ArithTraits< Scalar >::val_type IST
DenseMatTraits< Scalar, DM > DMT
Alternative run-time polymorphic interface for operators.
void kokkos_transpose(const V &dst, const V &src)
Helper function for copying Kokkos::DualView into conjugate Kokkos::DualView.