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 //
4 // Stokhos Package
5 // Copyright (2009) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact Eric T. Phipps (etphipp@sandia.gov).
38 //
39 // ***********************************************************************
40 // @HEADER
41 
45 
47 #include "Stokhos_Ensemble_Sizes.hpp"
48 #include "Sacado.hpp"
49 #include "Sacado_Fad_MP_Vector.hpp"
50 
51 // For computing DeviceConfig
52 #include "Kokkos_Core.hpp"
53 
54 //
55 // Tests various View< Fad< Sacado::MP::Vector<...> >,...> operations work
56 // as expected
57 //
58 
59 // Helper functions
60 
61 template <typename scalar, typename ordinal>
62 inline
63 scalar generate_vector_coefficient( const ordinal nFEM,
64  const ordinal nStoch,
65  const ordinal iColFEM,
66  const ordinal iStoch )
67 {
68  const scalar X_fem = 100.0 + scalar(iColFEM) / scalar(nFEM);
69  const scalar X_stoch = 1.0 + scalar(iStoch) / scalar(nStoch);
70  return X_fem + X_stoch;
71  //return 1.0;
72 }
73 
74 template <typename ViewType>
75 bool
76 checkVectorView(const ViewType& v,
77  Teuchos::FancyOStream& out) {
78  typedef ViewType view_type;
79  typedef typename view_type::size_type size_type;
80  typedef typename view_type::HostMirror host_view_type;
81  typedef typename host_view_type::array_type host_array_type;
83 
84  // Copy to host
85  host_view_type h_v = Kokkos::create_mirror_view(v);
86  Kokkos::deep_copy(h_v, v);
87  host_array_type h_a = h_v;
88 
89  size_type num_rows, num_cols;
90 
91  // For static, layout left, sacado dimension becomes first dimension
92  // instead of last
93  bool is_right = Kokkos::Impl::is_same< typename ViewType::array_layout,
94  Kokkos::LayoutRight >::value;
95  if (is_right || !view_type::is_contiguous) {
96  num_rows = h_a.extent(0);
97  num_cols = h_a.extent(1);
98  }
99  else {
100  num_rows = h_a.extent(1);
101  num_cols = h_a.extent(0);
102  }
103  bool success = true;
104  if (is_right || !view_type::is_contiguous) {
105  for (size_type i=0; i<num_rows; ++i) {
106  for (size_type j=0; j<num_cols; ++j) {
107  scalar_type val = h_a(i,j);
108  scalar_type val_expected =
109  generate_vector_coefficient<scalar_type>(
110  num_rows, num_cols, i, j);
111  TEUCHOS_TEST_EQUALITY(val, val_expected, out, success);
112  }
113  }
114  }
115  else {
116  for (size_type i=0; i<num_rows; ++i) {
117  for (size_type j=0; j<num_cols; ++j) {
118  scalar_type val = h_a(j,i);
119  scalar_type val_expected =
120  generate_vector_coefficient<scalar_type>(
121  num_rows, num_cols, i, j);
122  TEUCHOS_TEST_EQUALITY(val, val_expected, out, success);
123  }
124  }
125  }
126 
127  return success;
128 }
129 
130 template <typename ViewType>
131 bool
132 checkConstantFadVectorView(const ViewType& view,
133  const typename ViewType::value_type& v,
134  Teuchos::FancyOStream& out) {
135  typedef ViewType view_type;
136  typedef typename view_type::value_type fad_vector_type;
137  typedef typename fad_vector_type::value_type vector_type;
138  typedef typename vector_type::storage_type storage_type;
139  typedef typename view_type::size_type size_type;
140  typedef typename view_type::HostMirror host_view_type;
141 
142  // Copy to host
143  host_view_type h_view = Kokkos::create_mirror_view(view);
144  Kokkos::deep_copy(h_view, view);
145 
146  const size_type num_rows = h_view.extent(0);
147  const size_type num_fad = Kokkos::dimension_scalar(h_view)-1;
148  const size_type num_ensemble = storage_type::static_size;
149  bool success = true;
150  for (size_type i=0; i<num_rows; ++i) {
151  for (size_type k=0; k<num_ensemble; ++k) {
152  TEUCHOS_TEST_EQUALITY(h_view(i).val().coeff(k), v.val().coeff(k), out, success);
153  for (size_type j=0; j<num_fad; ++j) {
154  TEUCHOS_TEST_EQUALITY(h_view(i).dx(j).coeff(k), v.dx(j).coeff(k), out, success);
155  }
156  }
157  }
158 
159  return success;
160 }
161 
162 template <typename ViewType>
163 bool
164 checkConstantFadVectorView2(const ViewType& view,
165  const typename ViewType::value_type& v,
166  Teuchos::FancyOStream& out) {
167  typedef ViewType view_type;
168  typedef typename view_type::value_type fad_vector_type;
169  typedef typename fad_vector_type::value_type vector_type;
170  typedef typename vector_type::storage_type storage_type;
171  typedef typename view_type::size_type size_type;
172  typedef typename view_type::HostMirror host_view_type;
173 
174  // Copy to host
175  host_view_type h_view = Kokkos::create_mirror_view(view);
176  Kokkos::deep_copy(h_view, view);
177 
178  bool success = true;
179  const size_type num_fad = Kokkos::dimension_scalar(h_view)-1;
180  const size_type num_ensemble = storage_type::static_size;
181  for (size_type i0=0; i0<h_view.extent(0); ++i0) {
182  for (size_type i1=0; i1<h_view.extent(1); ++i1) {
183  for (size_type i2=0; i2<h_view.extent(2); ++i2) {
184  for (size_type i3=0; i3<h_view.extent(3); ++i3) {
185  for (size_type i4=0; i4<h_view.extent(4); ++i4) {
186  for (size_type i5=0; i5<h_view.extent(5); ++i5) {
187  for (size_type i6=0; i6<h_view.extent(6); ++i6) {
188  for (size_type k=0; k<num_ensemble; ++k)
189  TEUCHOS_TEST_EQUALITY(h_view.access(i0,i1,i2,i3,i4,i5,i6,0).val().coeff(k),
190  v.val().coeff(k), out, success);
191  for (size_type j=0; j<num_fad; ++j) {
192  for (size_type k=0; k<num_ensemble; ++k)
193  TEUCHOS_TEST_EQUALITY(h_view.access(i0,i1,i2,i3,i4,i5,i6,0).dx(j).coeff(k),
194  v.dx(j).coeff(k), out, success);
195  }
196  }}}}}}}
197 
198  return success;
199 }
200 
201 template <typename DataType, typename LayoutType, typename ExecutionSpace>
202 struct ApplyView {
203  typedef Kokkos::View<DataType,LayoutType,ExecutionSpace> type;
204 };
205 
206 struct NoLayout {};
207 template <typename DataType, typename ExecutionSpace>
208 struct ApplyView<DataType,NoLayout,ExecutionSpace> {
209  typedef Kokkos::View<DataType,ExecutionSpace> type;
210 };
211 
212 //
213 // Tests
214 //
215 
216 const int global_num_rows = 11;
217 const int global_ensemble_size = STOKHOS_DEFAULT_ENSEMBLE_SIZE;
218 const int global_fad_size = 5;
219 
220 TEUCHOS_UNIT_TEST_TEMPLATE_2_DECL( Kokkos_View_Fad_MP, Size, Scalar, Layout )
221 {
222  typedef typename Scalar::value_type Vector;
223  typedef typename Vector::execution_space Device;
224  typedef typename ApplyView<Scalar*,Layout,Device>::type ViewType;
225  typedef typename ViewType::size_type size_type;
226 
227  const size_type num_rows = global_num_rows;
228  const size_type num_cols = global_fad_size+1;
229  ViewType v("view", num_rows, num_cols);
230  TEUCHOS_TEST_EQUALITY(v.size(), num_rows, out, success);
231 }
232 
233 TEUCHOS_UNIT_TEST_TEMPLATE_2_DECL( Kokkos_View_Fad_MP, DeepCopy_ConstantScalar, Scalar, Layout )
234 {
235  typedef typename Scalar::value_type Vector;
236  typedef typename Vector::value_type BaseScalar;
237  typedef typename Vector::execution_space Device;
238  typedef typename ApplyView<Scalar*,Layout,Device>::type ViewType;
239  typedef typename ViewType::size_type size_type;
240 
241  const size_type num_rows = global_num_rows;
242  const size_type num_cols = global_fad_size+1;
243  ViewType v("view", num_rows, num_cols);
244  BaseScalar val = 1.2345;
245 
246  Kokkos::deep_copy( v, val );
247 
248  success = checkConstantFadVectorView(v, Scalar(val), out);
249 }
250 
251 TEUCHOS_UNIT_TEST_TEMPLATE_2_DECL( Kokkos_View_Fad_MP, Rank6, Scalar, Layout )
252 {
253  // Try and create a rank-6 view
254  typedef typename Scalar::value_type Vector;
255  typedef typename Vector::value_type BaseScalar;
256  typedef typename Vector::execution_space Device;
257  typedef typename ApplyView<Scalar******,Layout,Device>::type ViewType;
258 
259  ViewType v("view", 1, 2, 3, 4, 4, 3, global_fad_size+1);
260  BaseScalar val = 1.2345;
261 
262  Kokkos::deep_copy( v, val );
263 
264  success = checkConstantFadVectorView2(v, Scalar(val), out);
265 }
266 
267 #define VIEW_FAD_MP_VECTOR_TESTS_SCALAR_LAYOUT( SCALAR, LAYOUT ) \
268  TEUCHOS_UNIT_TEST_TEMPLATE_2_INSTANT( \
269  Kokkos_View_Fad_MP, Size, SCALAR, LAYOUT ) \
270  TEUCHOS_UNIT_TEST_TEMPLATE_2_INSTANT( \
271  Kokkos_View_Fad_MP, DeepCopy_ConstantScalar, SCALAR, LAYOUT ) \
272  TEUCHOS_UNIT_TEST_TEMPLATE_2_INSTANT( \
273  Kokkos_View_Fad_MP, Rank6, SCALAR, LAYOUT )
274 
275 #define VIEW_FAD_MP_VECTOR_TESTS_SCALAR( SCALAR ) \
276  using Kokkos::LayoutLeft; \
277  using Kokkos::LayoutRight; \
278  VIEW_FAD_MP_VECTOR_TESTS_SCALAR_LAYOUT(SCALAR, NoLayout) \
279  VIEW_FAD_MP_VECTOR_TESTS_SCALAR_LAYOUT(SCALAR, LayoutLeft) \
280  VIEW_FAD_MP_VECTOR_TESTS_SCALAR_LAYOUT(SCALAR, LayoutRight)
281 
282 #define VIEW_FAD_MP_VECTOR_TESTS_DEVICE( DEVICE ) \
283  typedef Stokhos::StaticFixedStorage<int,double,global_ensemble_size,DEVICE> SFS; \
284  typedef Sacado::MP::Vector< SFS > MP_SFS; \
285  typedef Sacado::Fad::DFad< MP_SFS > Fad_MP_SFS; \
286  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)