Stokhos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Stokhos_KokkosViewFadMPVectorUnitTest.hpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Stokhos Package
4 //
5 // Copyright 2009 NTESS and the Stokhos contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
13 
14 #include "Kokkos_View_Fad_Fwd.hpp"
16 #include "Stokhos_Ensemble_Sizes.hpp"
17 #include "Sacado.hpp"
18 #include "Sacado_Fad_MP_Vector.hpp"
19 
20 // For computing DeviceConfig
21 #include "Kokkos_Core.hpp"
22 
23 //
24 // Tests various View< Fad< Sacado::MP::Vector<...> >,...> operations work
25 // as expected
26 //
27 
28 // Helper functions
29 
30 template <typename scalar, typename ordinal>
31 inline
32 scalar generate_vector_coefficient( const ordinal nFEM,
33  const ordinal nStoch,
34  const ordinal iColFEM,
35  const ordinal iStoch )
36 {
37  const scalar X_fem = 100.0 + scalar(iColFEM) / scalar(nFEM);
38  const scalar X_stoch = 1.0 + scalar(iStoch) / scalar(nStoch);
39  return X_fem + X_stoch;
40  //return 1.0;
41 }
42 
43 template <typename ViewType>
44 bool
45 checkVectorView(const ViewType& v,
46  Teuchos::FancyOStream& out) {
47  typedef ViewType view_type;
48  typedef typename view_type::size_type size_type;
49  typedef typename view_type::HostMirror host_view_type;
50  typedef typename host_view_type::array_type host_array_type;
52 
53  // Copy to host
54  host_view_type h_v = Kokkos::create_mirror_view(v);
55  Kokkos::deep_copy(h_v, v);
56  host_array_type h_a = h_v;
57 
58  size_type num_rows, num_cols;
59 
60  // For static, layout left, sacado dimension becomes first dimension
61  // instead of last
62  bool is_right = std::is_same< typename ViewType::array_layout,
63  Kokkos::LayoutRight >::value;
64  if (is_right || !view_type::is_contiguous) {
65  num_rows = h_a.extent(0);
66  num_cols = h_a.extent(1);
67  }
68  else {
69  num_rows = h_a.extent(1);
70  num_cols = h_a.extent(0);
71  }
72  bool success = true;
73  if (is_right || !view_type::is_contiguous) {
74  for (size_type i=0; i<num_rows; ++i) {
75  for (size_type j=0; j<num_cols; ++j) {
76  scalar_type val = h_a(i,j);
77  scalar_type val_expected =
78  generate_vector_coefficient<scalar_type>(
79  num_rows, num_cols, i, j);
80  TEUCHOS_TEST_EQUALITY(val, val_expected, out, success);
81  }
82  }
83  }
84  else {
85  for (size_type i=0; i<num_rows; ++i) {
86  for (size_type j=0; j<num_cols; ++j) {
87  scalar_type val = h_a(j,i);
88  scalar_type val_expected =
89  generate_vector_coefficient<scalar_type>(
90  num_rows, num_cols, i, j);
91  TEUCHOS_TEST_EQUALITY(val, val_expected, out, success);
92  }
93  }
94  }
95 
96  return success;
97 }
98 
99 template <typename ViewType>
100 bool
101 checkConstantFadVectorView(const ViewType& view,
102  const typename ViewType::value_type& v,
103  Teuchos::FancyOStream& out) {
104  typedef ViewType view_type;
105  typedef typename view_type::value_type fad_vector_type;
106  typedef typename fad_vector_type::value_type vector_type;
107  typedef typename vector_type::storage_type storage_type;
108  typedef typename view_type::size_type size_type;
109  typedef typename view_type::HostMirror host_view_type;
110 
111  // Copy to host
112  host_view_type h_view = Kokkos::create_mirror_view(view);
113  Kokkos::deep_copy(h_view, view);
114 
115  const size_type num_rows = h_view.extent(0);
116  const size_type num_fad = Kokkos::dimension_scalar(h_view)-1;
117  const size_type num_ensemble = storage_type::static_size;
118  bool success = true;
119  for (size_type i=0; i<num_rows; ++i) {
120  for (size_type k=0; k<num_ensemble; ++k) {
121  TEUCHOS_TEST_EQUALITY(h_view(i).val().coeff(k), v.val().coeff(k), out, success);
122  for (size_type j=0; j<num_fad; ++j) {
123  TEUCHOS_TEST_EQUALITY(h_view(i).dx(j).coeff(k), v.dx(j).coeff(k), out, success);
124  }
125  }
126  }
127 
128  return success;
129 }
130 
131 template <typename ViewType>
132 bool
133 checkConstantFadVectorView2(const ViewType& view,
134  const typename ViewType::value_type& v,
135  Teuchos::FancyOStream& out) {
136  typedef ViewType view_type;
137  typedef typename view_type::value_type fad_vector_type;
138  typedef typename fad_vector_type::value_type vector_type;
139  typedef typename vector_type::storage_type storage_type;
140  typedef typename view_type::size_type size_type;
141  typedef typename view_type::HostMirror host_view_type;
142 
143  // Copy to host
144  host_view_type h_view = Kokkos::create_mirror_view(view);
145  Kokkos::deep_copy(h_view, view);
146 
147  bool success = true;
148  const size_type num_fad = Kokkos::dimension_scalar(h_view)-1;
149  const size_type num_ensemble = storage_type::static_size;
150  for (size_type i0=0; i0<h_view.extent(0); ++i0) {
151  for (size_type i1=0; i1<h_view.extent(1); ++i1) {
152  for (size_type i2=0; i2<h_view.extent(2); ++i2) {
153  for (size_type i3=0; i3<h_view.extent(3); ++i3) {
154  for (size_type i4=0; i4<h_view.extent(4); ++i4) {
155  for (size_type i5=0; i5<h_view.extent(5); ++i5) {
156  for (size_type i6=0; i6<h_view.extent(6); ++i6) {
157  for (size_type k=0; k<num_ensemble; ++k)
158  TEUCHOS_TEST_EQUALITY(h_view.access(i0,i1,i2,i3,i4,i5,i6,0).val().coeff(k),
159  v.val().coeff(k), out, success);
160  for (size_type j=0; j<num_fad; ++j) {
161  for (size_type k=0; k<num_ensemble; ++k)
162  TEUCHOS_TEST_EQUALITY(h_view.access(i0,i1,i2,i3,i4,i5,i6,0).dx(j).coeff(k),
163  v.dx(j).coeff(k), out, success);
164  }
165  }}}}}}}
166 
167  return success;
168 }
169 
170 template <typename DataType, typename LayoutType, typename ExecutionSpace>
171 struct ApplyView {
172  typedef Kokkos::View<DataType,LayoutType,ExecutionSpace> type;
173 };
174 
175 struct NoLayout {};
176 template <typename DataType, typename ExecutionSpace>
177 struct ApplyView<DataType,NoLayout,ExecutionSpace> {
178  typedef Kokkos::View<DataType,ExecutionSpace> type;
179 };
180 
181 //
182 // Tests
183 //
184 
185 const int global_num_rows = 11;
186 const int global_ensemble_size = STOKHOS_DEFAULT_ENSEMBLE_SIZE;
187 const int global_fad_size = 5;
188 
189 TEUCHOS_UNIT_TEST_TEMPLATE_2_DECL( Kokkos_View_Fad_MP, Size, Scalar, Layout )
190 {
191  typedef typename Scalar::value_type Vector;
192  typedef typename Vector::execution_space Device;
193  typedef typename ApplyView<Scalar*,Layout,Device>::type ViewType;
194  typedef typename ViewType::size_type size_type;
195 
196  const size_type num_rows = global_num_rows;
197  const size_type num_cols = global_fad_size+1;
198  ViewType v("view", num_rows, num_cols);
199  TEUCHOS_TEST_EQUALITY(v.size(), num_rows, out, success);
200 }
201 
202 TEUCHOS_UNIT_TEST_TEMPLATE_2_DECL( Kokkos_View_Fad_MP, DeepCopy_ConstantScalar, Scalar, Layout )
203 {
204  typedef typename Scalar::value_type Vector;
205  typedef typename Vector::value_type BaseScalar;
206  typedef typename Vector::execution_space Device;
207  typedef typename ApplyView<Scalar*,Layout,Device>::type ViewType;
208  typedef typename ViewType::size_type size_type;
209 
210  const size_type num_rows = global_num_rows;
211  const size_type num_cols = global_fad_size+1;
212  ViewType v("view", num_rows, num_cols);
213  BaseScalar val = 1.2345;
214 
215  Kokkos::deep_copy( v, val );
216 
217  success = checkConstantFadVectorView(v, Scalar(val), out);
218 }
219 
220 TEUCHOS_UNIT_TEST_TEMPLATE_2_DECL( Kokkos_View_Fad_MP, Rank6, Scalar, Layout )
221 {
222  // Try and create a rank-6 view
223  typedef typename Scalar::value_type Vector;
224  typedef typename Vector::value_type BaseScalar;
225  typedef typename Vector::execution_space Device;
226  typedef typename ApplyView<Scalar******,Layout,Device>::type ViewType;
227 
228  ViewType v("view", 1, 2, 3, 4, 4, 3, global_fad_size+1);
229  BaseScalar val = 1.2345;
230 
231  Kokkos::deep_copy( v, val );
232 
233  success = checkConstantFadVectorView2(v, Scalar(val), out);
234 }
235 
236 #define VIEW_FAD_MP_VECTOR_TESTS_SCALAR_LAYOUT( SCALAR, LAYOUT ) \
237  TEUCHOS_UNIT_TEST_TEMPLATE_2_INSTANT( \
238  Kokkos_View_Fad_MP, Size, SCALAR, LAYOUT ) \
239  TEUCHOS_UNIT_TEST_TEMPLATE_2_INSTANT( \
240  Kokkos_View_Fad_MP, DeepCopy_ConstantScalar, SCALAR, LAYOUT ) \
241  TEUCHOS_UNIT_TEST_TEMPLATE_2_INSTANT( \
242  Kokkos_View_Fad_MP, Rank6, SCALAR, LAYOUT )
243 
244 #define VIEW_FAD_MP_VECTOR_TESTS_SCALAR( SCALAR ) \
245  using Kokkos::LayoutLeft; \
246  using Kokkos::LayoutRight; \
247  VIEW_FAD_MP_VECTOR_TESTS_SCALAR_LAYOUT(SCALAR, NoLayout) \
248  VIEW_FAD_MP_VECTOR_TESTS_SCALAR_LAYOUT(SCALAR, LayoutLeft) \
249  VIEW_FAD_MP_VECTOR_TESTS_SCALAR_LAYOUT(SCALAR, LayoutRight)
250 
251 #define VIEW_FAD_MP_VECTOR_TESTS_DEVICE( DEVICE ) \
252  typedef Stokhos::StaticFixedStorage<int,double,global_ensemble_size,DEVICE> SFS; \
253  typedef Sacado::MP::Vector< SFS > MP_SFS; \
254  typedef Sacado::Fad::DFad< MP_SFS > Fad_MP_SFS; \
255  VIEW_FAD_MP_VECTOR_TESTS_SCALAR( Fad_MP_SFS )
Stokhos::StandardStorage< int, double > storage_type
Kokkos::DefaultExecutionSpace execution_space
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)
bool checkConstantFadVectorView2(const ViewType &view, const typename ViewType::value_type &v, Teuchos::FancyOStream &out)
bool checkVectorView(const ViewType &v, Teuchos::FancyOStream &out)
TEUCHOS_UNIT_TEST_TEMPLATE_2_DECL(Kokkos_SG_SpMv, CrsProductTensorCijk, Scalar, Device)
void deep_copy(const Stokhos::CrsMatrix< ValueType, DstDevice, Layout > &dst, const Stokhos::CrsMatrix< ValueType, SrcDevice, Layout > &src)
expr expr expr dx(i, j)
expr val()
#define TEUCHOS_TEST_EQUALITY(v1, v2, out, success)
SparseArrayIterator< index_iterator, value_iterator >::value_reference value(const SparseArrayIterator< index_iterator, value_iterator > &it)
bool checkConstantFadVectorView(const ViewType &view, const typename ViewType::value_type &v, Teuchos::FancyOStream &out)
Kokkos::View< DataType, LayoutType, ExecutionSpace > type
Stokhos::CrsMatrix< ValueType, Device, Layout >::HostMirror create_mirror_view(const Stokhos::CrsMatrix< ValueType, Device, Layout > &A)