14 #include "Stokhos_Ensemble_Sizes.hpp"
19 #include "Kokkos_Core.hpp"
22 template<
typename IntType >
29 return k + N * ( j + N * i );
32 template <
typename ordinal >
35 std::vector< std::vector<ordinal> >& graph )
37 graph.resize( N * N * N, std::vector<ordinal>() );
41 for (
int i = 0; i < (int) N; ++i ) {
42 for (
int j = 0;
j < (int) N; ++
j ) {
43 for (
int k = 0; k < (int) N; ++k ) {
47 graph[row].reserve(27);
49 for (
int ii = -1; ii < 2; ++ii ) {
50 for (
int jj = -1; jj < 2; ++jj ) {
51 for (
int kk = -1; kk < 2; ++kk ) {
52 if ( 0 <= i + ii && i + ii < (
int) N &&
53 0 <=
j + jj &&
j + jj < (
int) N &&
54 0 <= k + kk && k + kk < (
int) N ) {
57 graph[row].push_back(col);
60 total += graph[row].size();
66 template <
typename scalar,
typename ordinal>
70 const ordinal iRowFEM,
71 const ordinal iColFEM,
72 const ordinal iStoch )
74 const scalar A_fem = ( 10.0 + scalar(iRowFEM) / scalar(nFEM) ) +
75 ( 5.0 + scalar(iColFEM) / scalar(nFEM) );
77 const scalar A_stoch = ( 1.0 + scalar(iStoch) / scalar(nStoch) );
79 return A_fem + A_stoch;
83 template <
typename scalar,
typename ordinal>
87 const ordinal iColFEM,
88 const ordinal iStoch )
90 const scalar X_fem = 100.0 + scalar(iColFEM) / scalar(nFEM);
91 const scalar X_stoch = 1.0 + scalar(iStoch) / scalar(nStoch);
92 return X_fem + X_stoch;
98 template <>
struct ScalarTol<float> {
static float tol() {
return 1e-4; } };
102 template <
typename array_type,
typename scalar_type>
104 const array_type& y_exp,
109 typedef typename array_type::size_type size_type;
115 size_type num_rows = y.extent(0);
116 size_type num_cols = y.extent(1);
118 for (size_type i=0; i<num_rows; ++i) {
119 for (size_type
j=0;
j<num_cols; ++
j) {
123 out <<
"y_expected(" << i <<
"," <<
j <<
") - "
124 <<
"y(" << i <<
"," <<
j <<
") = " << hy_exp(i,
j)
125 <<
" - " << hy(i,
j) <<
" == "
126 << diff <<
" < " << tol <<
" : ";
132 success = success && s;
140 template <
typename MatrixType>
145 typedef typename MatrixType::StaticCrsGraphType matrix_graph_type;
146 typedef typename MatrixType::values_type matrix_values_type;
148 std::vector< std::vector<ordinal_type> > graph(nrow);
149 for (ordinal_type i=0; i<nrow; ++i)
150 graph[i] = std::vector<ordinal_type>(1, i);
151 ordinal_type graph_length = nrow;
153 matrix_graph_type matrix_graph =
154 Kokkos::create_staticcrsgraph<matrix_graph_type>(
"graph", graph);
155 matrix_values_type matrix_values =
156 matrix_values_type(
"values", graph_length, mp_vector_size);
158 MatrixType matrix(
"matrix", nrow, matrix_values, matrix_graph);
167 template <
typename MatrixType>
178 KOKKOS_INLINE_FUNCTION
183 m_matrix.replaceValues(row, &col, 1, &val,
false,
true);
187 static void apply(
const MatrixType matrix) {
193 static bool check(
const MatrixType matrix,
195 typedef typename MatrixType::values_type matrix_values_type;
196 typename matrix_values_type::HostMirror host_matrix_values =
204 "matrix_values(row)",
208 success = success && s;
215 template <
typename MatrixType>
226 KOKKOS_INLINE_FUNCTION
231 m_matrix.sumIntoValues(row, &col, 1, &val,
false,
true);
235 static void apply(
const MatrixType matrix) {
241 static bool check(
const MatrixType matrix,
243 typedef typename MatrixType::values_type matrix_values_type;
244 typename matrix_values_type::HostMirror host_matrix_values =
252 "matrix_values(row)",
256 success = success && s;
264 template <
typename MatrixType>
275 KOKKOS_INLINE_FUNCTION
280 m_matrix.sumIntoValues(row, &col, 1, &val,
false,
true);
284 static void apply(
const MatrixType matrix) {
290 static bool check(
const MatrixType matrix,
292 typedef typename MatrixType::values_type matrix_values_type;
293 typename matrix_values_type::HostMirror host_matrix_values =
302 val =
value_type( vec_size, nrow*(nrow-1)/2 );
306 "matrix_values(row)",
310 success = success && s;
319 Kokkos_CrsMatrix_MP, ReplaceValues, MatrixScalar )
323 typedef Kokkos::Device<execution_space, typename execution_space::memory_space> Device;
324 typedef KokkosSparse::CrsMatrix<MatrixScalar,Ordinal,Device> Matrix;
328 Matrix matrix = buildDiagonalMatrix<Matrix>(nrow,
VectorSize);
332 kernel::apply(matrix);
335 success = kernel::check(matrix, out);
339 Kokkos_CrsMatrix_MP, SumIntoValues, MatrixScalar )
343 typedef Kokkos::Device<execution_space, typename execution_space::memory_space> Device;
344 typedef KokkosSparse::CrsMatrix<MatrixScalar,Ordinal,Device> Matrix;
348 Matrix matrix = buildDiagonalMatrix<Matrix>(nrow,
VectorSize);
352 kernel::apply(matrix);
355 success = kernel::check(matrix, out);
359 Kokkos_CrsMatrix_MP, SumIntoValuesAtomic, MatrixScalar )
363 typedef Kokkos::Device<execution_space, typename execution_space::memory_space> Device;
364 typedef KokkosSparse::CrsMatrix<MatrixScalar,Ordinal,Device> Matrix;
368 Matrix matrix = buildDiagonalMatrix<Matrix>(nrow,
VectorSize);
372 kernel::apply(matrix);
375 success = kernel::check(matrix, out);
382 template <
class ViewType,
class OrdinalType,
size_t I >
384 static ViewType
create_view(
const std::string & name,
const OrdinalType & fem_length,
const OrdinalType & stoch_length ) {
385 return ViewType(name, fem_length, stoch_length);
389 template <
class ViewType,
class OrdinalType>
391 static ViewType
create_view(
const std::string & name,
const OrdinalType & fem_length,
const OrdinalType & stoch_length ) {
393 return ViewType(name, fem_length);
397 template <
class ViewType,
class OrdinalType>
399 static ViewType
create_view(
const std::string & name,
const OrdinalType & fem_length,
const OrdinalType & stoch_length ) {
402 return ViewType(name);
407 template <
typename VectorType,
typename Multiply>
410 KokkosSparse::DeviceConfig dev_config,
411 Multiply multiply_op,
418 typedef Kokkos::Device<execution_space, typename execution_space::memory_space> device_type;
419 typedef Kokkos::LayoutRight Layout;
420 typedef Kokkos::View< VectorType*, Layout, execution_space > block_vector_type;
421 typedef KokkosSparse::CrsMatrix< VectorType, ordinal_type, device_type > block_matrix_type;
422 typedef typename block_matrix_type::StaticCrsGraphType matrix_graph_type;
423 typedef typename block_matrix_type::values_type matrix_values_type;
427 storage_type::is_static && storage_type::static_size != stoch_length,
429 "Static storage size must equal ensemble size");
432 ordinal_type fem_length = nGrid * nGrid * nGrid;
433 std::vector< std::vector<ordinal_type> > fem_graph;
446 block_vector_type x =
447 block_vector_type(
"x", fem_length, stoch_length);
448 block_vector_type y =
449 block_vector_type(
"y", fem_length, stoch_length);
455 typename block_vector_type::HostMirror::array_type hax = hx ;
456 typename block_vector_type::HostMirror::array_type hay = hy ;
458 for (ordinal_type iRowFEM=0; iRowFEM<fem_length; ++iRowFEM) {
459 for (ordinal_type iRowStoch=0; iRowStoch<stoch_length; ++iRowStoch) {
460 hax(iRowFEM,iRowStoch) =
461 generate_vector_coefficient<scalar_type>(
462 fem_length, stoch_length, iRowFEM, iRowStoch );
463 hay(iRowFEM,iRowStoch) = 0.0;
473 matrix_graph_type matrix_graph =
474 Kokkos::create_staticcrsgraph<matrix_graph_type>(
475 std::string(
"test crs graph"), fem_graph);
480 matrix_values_type matrix_values =
481 matrix_values_type(
"matrix", fem_graph_length, stoch_length);
482 block_matrix_type matrix(
483 "block_matrix", fem_length, matrix_values, matrix_graph);
484 matrix.dev_config = dev_config;
486 typename matrix_values_type::HostMirror hM =
489 typename matrix_values_type::HostMirror::array_type haM = hM ;
491 for (ordinal_type iRowFEM=0, iEntryFEM=0; iRowFEM<fem_length; ++iRowFEM) {
492 const ordinal_type row_size = fem_graph[iRowFEM].size();
493 for (ordinal_type iRowEntryFEM=0; iRowEntryFEM<row_size;
494 ++iRowEntryFEM, ++iEntryFEM) {
495 const ordinal_type iColFEM = fem_graph[iRowFEM][iRowEntryFEM];
497 for (ordinal_type k=0; k<stoch_length; ++k) {
499 generate_matrix_coefficient<scalar_type>(
500 fem_length, stoch_length, iRowFEM, iColFEM, k);
510 multiply_op( matrix, x, y );
515 typedef typename block_vector_type::array_type array_type;
516 #ifdef KOKKOS_ENABLE_DEPRECATED_CODE
517 array_type ay_expected =
518 array_type(
"ay_expected", fem_length, stoch_length);
520 array_type ay_expected =
523 typename array_type::HostMirror hay_expected =
525 for (ordinal_type iRowFEM=0, iEntryFEM=0; iRowFEM<fem_length; ++iRowFEM) {
526 const ordinal_type row_size = fem_graph[iRowFEM].size();
527 for (ordinal_type iRowEntryFEM=0; iRowEntryFEM<row_size;
528 ++iRowEntryFEM, ++iEntryFEM) {
529 const ordinal_type iColFEM = fem_graph[iRowFEM][iRowEntryFEM];
530 for (ordinal_type k=0; k<stoch_length; ++k) {
531 hay_expected(iRowFEM, k) +=
532 generate_matrix_coefficient<scalar_type>(
533 fem_length, stoch_length, iRowFEM, iColFEM, k) *
534 generate_vector_coefficient<scalar_type>(
535 fem_length, stoch_length, iColFEM, k );
544 typename block_vector_type::array_type ay = y;
553 template <
typename Matrix,
typename InputVector,
typename OutputVector>
555 const InputVector& x,
556 OutputVector& y)
const {
561 template <
typename Tag>
566 template <
typename Matrix,
typename InputVector,
typename OutputVector>
568 const InputVector& x,
569 OutputVector& y)
const {
577 #define CRSMATRIX_MP_VECTOR_TESTS_MATRIXSCALAR( SCALAR ) \
578 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( \
579 Kokkos_CrsMatrix_MP, ReplaceValues, SCALAR ) \
580 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( \
581 Kokkos_CrsMatrix_MP, SumIntoValues, SCALAR ) \
582 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( \
583 Kokkos_CrsMatrix_MP, SumIntoValuesAtomic, SCALAR )
585 #define CRSMATRIX_MP_VECTOR_TESTS_STORAGE( STORAGE ) \
586 typedef Sacado::MP::Vector<STORAGE> MP_Vector_ ## STORAGE; \
587 CRSMATRIX_MP_VECTOR_TESTS_MATRIXSCALAR( MP_Vector_ ## STORAGE )
589 #define CRSMATRIX_MP_VECTOR_TESTS_ORDINAL_SCALAR_DEVICE( ORDINAL, SCALAR, DEVICE ) \
590 typedef Stokhos::StaticFixedStorage<ORDINAL,SCALAR,VectorSize,DEVICE> SFS; \
591 typedef Stokhos::DynamicStorage<ORDINAL,SCALAR,DEVICE> DS; \
592 CRSMATRIX_MP_VECTOR_TESTS_STORAGE( SFS ) \
593 CRSMATRIX_MP_VECTOR_TESTS_STORAGE( DS )
595 #define CRSMATRIX_MP_VECTOR_TESTS_DEVICE( DEVICE ) \
596 CRSMATRIX_MP_VECTOR_TESTS_ORDINAL_SCALAR_DEVICE( int, double, DEVICE )
KOKKOS_INLINE_FUNCTION void operator()(const size_type i) const
MatrixType::execution_space execution_space
KOKKOS_INLINE_FUNCTION void operator()(const size_type i) const
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)
ordinal generate_fem_graph(ordinal N, std::vector< std::vector< ordinal > > &graph)
static bool check(const MatrixType matrix, Teuchos::FancyOStream &out)
Stokhos_MV_Multiply_Op< Stokhos::DefaultMultiply > DefaultMultiply
Kokkos::DefaultExecutionSpace execution_space
MatrixType::execution_space execution_space
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
MatrixType buildDiagonalMatrix(typename MatrixType::ordinal_type nrow, typename MatrixType::ordinal_type mp_vector_size)
MatrixType::ordinal_type ordinal_type
static void apply(const MatrixType matrix)
scalar generate_vector_coefficient(const ordinal nFEM, const ordinal nStoch, const ordinal iColFEM, const ordinal iStoch)
KOKKOS_INLINE_FUNCTION constexpr std::enable_if< is_view_uq_pce< View< T, P...> >::value, unsigned >::type dimension_scalar(const View< T, P...> &view)
IntType map_fem_graph_coord(const IntType &N, const IntType &i, const IntType &j, const IntType &k)
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)
static bool check(const MatrixType matrix, Teuchos::FancyOStream &out)
AddDiagonalValuesAtomicKernel(const MatrixType matrix)
Kokkos_MV_Multiply_Op KokkosMultiply
MatrixType::execution_space execution_space
void operator()(const Matrix &A, const InputVector &x, OutputVector &y) const
static void apply(const MatrixType matrix)
MatrixType::size_type size_type
static bool check(const MatrixType matrix, Teuchos::FancyOStream &out)
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)
static ViewType create_view(const std::string &name, const OrdinalType &fem_length, const OrdinalType &stoch_length)
static ViewType create_view(const std::string &name, const OrdinalType &fem_length, const OrdinalType &stoch_length)
static ViewType create_view(const std::string &name, const OrdinalType &fem_length, const OrdinalType &stoch_length)
scalar generate_matrix_coefficient(const ordinal nFEM, const ordinal nStoch, const ordinal iRowFEM, const ordinal iColFEM, const ordinal iStoch)
const MatrixType m_matrix
KOKKOS_INLINE_FUNCTION void operator()(const size_type i) const
TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL(Kokkos_CrsMatrix_MP, ReplaceValues, MatrixScalar)
AddDiagonalValuesKernel(const MatrixType matrix)
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(KokkosKernels::Experimental::Controls, 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)
MatrixType::ordinal_type ordinal_type
const MatrixType m_matrix
void operator()(const Matrix &A, const InputVector &x, OutputVector &y) const
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
Stokhos::CrsMatrix< ValueType, Device, Layout >::HostMirror create_mirror_view(const Stokhos::CrsMatrix< ValueType, Device, Layout > &A)