42 #include "Teuchos_UnitTestHelpers.hpp" 50 #include "Kokkos_Core.hpp" 53 template<
typename IntType >
60 return k + N * (
j + N * i );
63 template <
typename ordinal >
66 std::vector< std::vector<ordinal> >& graph )
68 graph.resize( N * N * N, std::vector<ordinal>() );
72 for (
int i = 0; i < (
int) N; ++i ) {
73 for (
int j = 0;
j < (
int) N; ++
j ) {
74 for (
int k = 0; k < (
int) N; ++k ) {
78 graph[row].reserve(27);
80 for (
int ii = -1; ii < 2; ++ii ) {
81 for (
int jj = -1; jj < 2; ++jj ) {
82 for (
int kk = -1; kk < 2; ++kk ) {
83 if ( 0 <= i + ii && i + ii < (
int) N &&
84 0 <=
j + jj &&
j + jj < (
int) N &&
85 0 <= k + kk && k + kk < (
int) N ) {
88 graph[row].push_back(col);
91 total += graph[row].size();
97 template <
typename scalar,
typename ordinal>
100 const ordinal nStoch,
101 const ordinal iRowFEM,
102 const ordinal iColFEM,
103 const ordinal iStoch )
105 const scalar A_fem = ( 10.0 + scalar(iRowFEM) / scalar(nFEM) ) +
106 ( 5.0 + scalar(iColFEM) / scalar(nFEM) );
108 const scalar A_stoch = ( 1.0 + scalar(iStoch) / scalar(nStoch) );
110 return A_fem + A_stoch;
114 template <
typename scalar,
typename ordinal>
117 const ordinal nStoch,
118 const ordinal iColFEM,
119 const ordinal iStoch )
121 const scalar X_fem = 100.0 + scalar(iColFEM) / scalar(nFEM);
122 const scalar X_stoch = 1.0 + scalar(iStoch) / scalar(nStoch);
123 return X_fem + X_stoch;
129 template <>
struct ScalarTol<float> {
static float tol() {
return 1e-4; } };
133 template <
typename array_type,
typename scalar_type>
135 const array_type& y_exp,
138 Teuchos::FancyOStream& out)
140 typedef typename array_type::size_type size_type;
146 size_type num_rows =
y.extent(0);
147 size_type num_cols =
y.extent(1);
149 for (size_type i=0; i<num_rows; ++i) {
150 for (size_type
j=0;
j<num_cols; ++
j) {
154 out <<
"y_expected(" << i <<
"," <<
j <<
") - " 155 <<
"y(" << i <<
"," <<
j <<
") = " << hy_exp(i,
j)
156 <<
" - " << hy(i,
j) <<
" == " 157 << diff <<
" < " << tol <<
" : ";
163 success = success && s;
171 template <
typename MatrixType>
176 typedef typename MatrixType::StaticCrsGraphType matrix_graph_type;
177 typedef typename MatrixType::values_type matrix_values_type;
179 std::vector< std::vector<ordinal_type> > graph(nrow);
181 graph[i] = std::vector<ordinal_type>(1, i);
184 matrix_graph_type matrix_graph =
185 Kokkos::create_staticcrsgraph<matrix_graph_type>(
"graph", graph);
186 matrix_values_type matrix_values =
187 matrix_values_type(
"values", graph_length, mp_vector_size);
189 MatrixType matrix(
"matrix", nrow, matrix_values, matrix_graph);
198 template <
typename MatrixType>
209 KOKKOS_INLINE_FUNCTION
214 m_matrix.replaceValues(row, &col, 1, &
val,
false,
true);
218 static void apply(
const MatrixType matrix) {
224 static bool check(
const MatrixType matrix,
225 Teuchos::FancyOStream& out) {
226 typedef typename MatrixType::values_type matrix_values_type;
227 typename matrix_values_type::HostMirror host_matrix_values =
235 "matrix_values(row)",
239 success = success && s;
246 template <
typename MatrixType>
257 KOKKOS_INLINE_FUNCTION
262 m_matrix.sumIntoValues(row, &col, 1, &
val,
false,
true);
266 static void apply(
const MatrixType matrix) {
272 static bool check(
const MatrixType matrix,
273 Teuchos::FancyOStream& out) {
274 typedef typename MatrixType::values_type matrix_values_type;
275 typename matrix_values_type::HostMirror host_matrix_values =
283 "matrix_values(row)",
287 success = success && s;
295 template <
typename MatrixType>
306 KOKKOS_INLINE_FUNCTION
311 m_matrix.sumIntoValues(row, &col, 1, &
val,
false,
true);
315 static void apply(
const MatrixType matrix) {
321 static bool check(
const MatrixType matrix,
322 Teuchos::FancyOStream& out) {
323 typedef typename MatrixType::values_type matrix_values_type;
324 typename matrix_values_type::HostMirror host_matrix_values =
337 "matrix_values(row)",
341 success = success && s;
351 Kokkos_CrsMatrix_MP, ReplaceValues, MatrixScalar )
355 typedef KokkosSparse::CrsMatrix<MatrixScalar,Ordinal,Device> Matrix;
359 Matrix matrix = buildDiagonalMatrix<Matrix>(nrow,
VectorSize);
363 kernel::apply(matrix);
366 success = kernel::check(matrix, out);
370 Kokkos_CrsMatrix_MP, SumIntoValues, MatrixScalar )
374 typedef KokkosSparse::CrsMatrix<MatrixScalar,Ordinal,Device> Matrix;
378 Matrix matrix = buildDiagonalMatrix<Matrix>(nrow,
VectorSize);
382 kernel::apply(matrix);
385 success = kernel::check(matrix, out);
389 Kokkos_CrsMatrix_MP, SumIntoValuesAtomic, MatrixScalar )
393 typedef KokkosSparse::CrsMatrix<MatrixScalar,Ordinal,Device> Matrix;
397 Matrix matrix = buildDiagonalMatrix<Matrix>(nrow,
VectorSize);
401 kernel::apply(matrix);
404 success = kernel::check(matrix, out);
407 template <
typename VectorType,
typename Multiply>
410 KokkosSparse::DeviceConfig dev_config,
411 Multiply multiply_op,
412 Teuchos::FancyOStream& out)
418 typedef Kokkos::LayoutRight Layout;
419 typedef Kokkos::View< VectorType*, Layout, execution_space > block_vector_type;
420 typedef KokkosSparse::CrsMatrix< VectorType, ordinal_type, execution_space > block_matrix_type;
421 typedef typename block_matrix_type::StaticCrsGraphType matrix_graph_type;
422 typedef typename block_matrix_type::values_type matrix_values_type;
425 TEUCHOS_TEST_FOR_EXCEPTION(
426 storage_type::is_static && storage_type::static_size != stoch_length,
428 "Static storage size must equal ensemble size");
432 std::vector< std::vector<ordinal_type> > fem_graph;
445 block_vector_type
x =
446 block_vector_type(
"x", fem_length, stoch_length);
447 block_vector_type
y =
448 block_vector_type(
"y", fem_length, stoch_length);
454 typename block_vector_type::HostMirror::array_type hax = hx ;
455 typename block_vector_type::HostMirror::array_type hay = hy ;
457 for (
ordinal_type iRowFEM=0; iRowFEM<fem_length; ++iRowFEM) {
458 for (
ordinal_type iRowStoch=0; iRowStoch<stoch_length; ++iRowStoch) {
459 hax(iRowFEM,iRowStoch) =
460 generate_vector_coefficient<scalar_type>(
461 fem_length, stoch_length, iRowFEM, iRowStoch );
462 hay(iRowFEM,iRowStoch) = 0.0;
472 matrix_graph_type matrix_graph =
473 Kokkos::create_staticcrsgraph<matrix_graph_type>(
474 std::string(
"test crs graph"), fem_graph);
479 matrix_values_type matrix_values =
480 matrix_values_type(
"matrix", fem_graph_length, stoch_length);
481 block_matrix_type matrix(
482 "block_matrix", fem_length, matrix_values, matrix_graph);
483 matrix.dev_config = dev_config;
485 typename matrix_values_type::HostMirror hM =
488 typename matrix_values_type::HostMirror::array_type haM = hM ;
490 for (
ordinal_type iRowFEM=0, iEntryFEM=0; iRowFEM<fem_length; ++iRowFEM) {
491 const ordinal_type row_size = fem_graph[iRowFEM].size();
492 for (
ordinal_type iRowEntryFEM=0; iRowEntryFEM<row_size;
493 ++iRowEntryFEM, ++iEntryFEM) {
494 const ordinal_type iColFEM = fem_graph[iRowFEM][iRowEntryFEM];
498 generate_matrix_coefficient<scalar_type>(
499 fem_length, stoch_length, iRowFEM, iColFEM, k);
509 multiply_op( matrix,
x,
y );
514 typedef typename block_vector_type::array_type array_type;
515 array_type ay_expected =
516 array_type(
"ay_expected", fem_length, stoch_length);
517 typename array_type::HostMirror hay_expected =
519 for (
ordinal_type iRowFEM=0, iEntryFEM=0; iRowFEM<fem_length; ++iRowFEM) {
520 const ordinal_type row_size = fem_graph[iRowFEM].size();
521 for (
ordinal_type iRowEntryFEM=0; iRowEntryFEM<row_size;
522 ++iRowEntryFEM, ++iEntryFEM) {
523 const ordinal_type iColFEM = fem_graph[iRowFEM][iRowEntryFEM];
525 hay_expected(iRowFEM, k) +=
526 generate_matrix_coefficient<scalar_type>(
527 fem_length, stoch_length, iRowFEM, iColFEM, k) *
528 generate_vector_coefficient<scalar_type>(
529 fem_length, stoch_length, iColFEM, k );
538 typename block_vector_type::array_type ay =
y;
547 template <
typename Matrix,
typename InputVector,
typename OutputVector>
549 const InputVector&
x,
550 OutputVector&
y)
const {
555 template <
typename Tag>
560 template <
typename Matrix,
typename InputVector,
typename OutputVector>
562 const InputVector&
x,
563 OutputVector&
y)
const {
571 #define CRSMATRIX_MP_VECTOR_TESTS_MATRIXSCALAR( SCALAR ) \ 572 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( \ 573 Kokkos_CrsMatrix_MP, ReplaceValues, SCALAR ) \ 574 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( \ 575 Kokkos_CrsMatrix_MP, SumIntoValues, SCALAR ) \ 576 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( \ 577 Kokkos_CrsMatrix_MP, SumIntoValuesAtomic, SCALAR ) 579 #define CRSMATRIX_MP_VECTOR_TESTS_STORAGE( STORAGE ) \ 580 typedef Sacado::MP::Vector<STORAGE> MP_Vector_ ## STORAGE; \ 581 CRSMATRIX_MP_VECTOR_TESTS_MATRIXSCALAR( MP_Vector_ ## STORAGE ) 583 #define CRSMATRIX_MP_VECTOR_TESTS_ORDINAL_SCALAR_DEVICE( ORDINAL, SCALAR, DEVICE ) \ 584 typedef Stokhos::StaticFixedStorage<ORDINAL,SCALAR,VectorSize,DEVICE> SFS; \ 585 typedef Stokhos::DynamicStorage<ORDINAL,SCALAR,DEVICE> DS; \ 586 CRSMATRIX_MP_VECTOR_TESTS_STORAGE( SFS ) \ 587 CRSMATRIX_MP_VECTOR_TESTS_STORAGE( DS ) 589 #define CRSMATRIX_MP_VECTOR_TESTS_DEVICE( DEVICE ) \ 590 CRSMATRIX_MP_VECTOR_TESTS_ORDINAL_SCALAR_DEVICE( int, double, DEVICE )
MatrixType::execution_space execution_space
Stokhos::StandardStorage< int, double > storage_type
MatrixType::size_type size_type
const MatrixType m_matrix
MatrixType::value_type value_type
bool compareVecs(const VectorType1 &a1, const std::string &a1_name, const VectorType2 &a2, const std::string &a2_name, const ValueType &rel_tol, const ValueType &abs_tol, Teuchos::FancyOStream &out)
void operator()(const Matrix &A, const InputVector &x, OutputVector &y) const
static bool check(const MatrixType matrix, Teuchos::FancyOStream &out)
KOKKOS_INLINE_FUNCTION void operator()(const size_type i) const
Stokhos_MV_Multiply_Op< Stokhos::DefaultMultiply > DefaultMultiply
Kokkos::DefaultExecutionSpace execution_space
MatrixType::execution_space execution_space
MatrixType buildDiagonalMatrix(typename MatrixType::ordinal_type nrow, typename MatrixType::ordinal_type mp_vector_size)
MatrixType::ordinal_type ordinal_type
KOKKOS_INLINE_FUNCTION void operator()(const size_type i) const
static void apply(const MatrixType matrix)
MatrixType::value_type value_type
MatrixType::ordinal_type ordinal_type
ReplaceDiagonalValuesKernel(const MatrixType matrix)
MatrixType::size_type size_type
void multiply(const CrsMatrix< MatrixValue, Device, Layout > &A, const InputMultiVectorType &x, OutputMultiVectorType &y, const std::vector< OrdinalType > &col_indices, SingleColumnMultivectorMultiply)
scalar generate_vector_coefficient(const ordinal nFEM, const ordinal nStoch, const ordinal iColFEM, const ordinal iStoch)
static bool check(const MatrixType matrix, Teuchos::FancyOStream &out)
AddDiagonalValuesAtomicKernel(const MatrixType matrix)
const IndexType const IndexType const IndexType const IndexType const ValueType const ValueType * x
Kokkos_MV_Multiply_Op KokkosMultiply
MatrixType::execution_space execution_space
static void apply(const MatrixType matrix)
MatrixType::size_type size_type
static bool check(const MatrixType matrix, Teuchos::FancyOStream &out)
IntType map_fem_graph_coord(const IntType &N, const IntType &i, const IntType &j, const IntType &k)
KOKKOS_INLINE_FUNCTION void operator()(const size_type i) const
KOKKOS_INLINE_FUNCTION constexpr std::enable_if< is_view_uq_pce< View< T, P... > >::value, unsigned >::type dimension_scalar(const View< T, P... > &view)
Stokhos_MV_Multiply_Op(const Tag &tg=Tag())
void deep_copy(const Stokhos::CrsMatrix< ValueType, DstDevice, Layout > &dst, const Stokhos::CrsMatrix< ValueType, SrcDevice, Layout > &src)
KOKKOS_INLINE_FUNCTION PCE< Storage > abs(const PCE< Storage > &a)
ordinal generate_fem_graph(ordinal N, std::vector< std::vector< ordinal > > &graph)
const MatrixType m_matrix
TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL(Kokkos_CrsMatrix_MP, ReplaceValues, MatrixScalar)
AddDiagonalValuesKernel(const MatrixType matrix)
MatrixType::ordinal_type ordinal_type
scalar generate_matrix_coefficient(const ordinal nFEM, const ordinal nStoch, const ordinal iRowFEM, const ordinal iColFEM, const ordinal iStoch)
const MatrixType m_matrix
bool compare_rank_2_views(const array_type &y, const array_type &y_exp, const scalar_type rel_tol, const scalar_type abs_tol, Teuchos::FancyOStream &out)
static void apply(const MatrixType matrix)
const unsigned VectorSize
bool test_embedded_vector(const typename VectorType::ordinal_type nGrid, const typename VectorType::ordinal_type stoch_length, KokkosSparse::DeviceConfig dev_config, Multiply multiply_op, Teuchos::FancyOStream &out)
MatrixType::value_type value_type
const IndexType const IndexType const IndexType const IndexType const ValueType const ValueType ValueType * y
std::enable_if< Kokkos::is_view_uq_pce< Kokkos::View< InputType, InputP... > >::value &&Kokkos::is_view_uq_pce< Kokkos::View< OutputType, OutputP... > >::value >::type spmv(const char mode[], const AlphaType &a, const MatrixType &A, const Kokkos::View< InputType, InputP... > &x, const BetaType &b, const Kokkos::View< OutputType, OutputP... > &y, const RANK_ONE)
Stokhos::CrsMatrix< ValueType, Device, Layout >::HostMirror create_mirror_view(const Stokhos::CrsMatrix< ValueType, Device, Layout > &A)
void operator()(const Matrix &A, const InputVector &x, OutputVector &y) const