Sacado Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ViewFactoryTests.cpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Sacado Package
4 //
5 // Copyright 2006 NTESS and the Sacado contributors.
6 // SPDX-License-Identifier: LGPL-2.1-or-later
7 // *****************************************************************************
8 // @HEADER
9 
13 
14 #include "Sacado.hpp"
15 
16 #include "Kokkos_ViewFactory.hpp"
17 
18 TEUCHOS_UNIT_TEST(view_factory, dyn_rank_views)
19 {
21  using Kokkos::View;
22  using Kokkos::DynRankView;
26 #ifndef SACADO_HAS_NEW_KOKKOS_VIEW_IMPL
27  using Kokkos::dimension_scalar;
28 #else
29  using Sacado::dimension_scalar;
30 #endif
31  using Kokkos::view_alloc;
32  using Kokkos::WithoutInitializing;
33  const unsigned derivative_dim_plus_one = 7;
34 
35 
36  // Test constructing a View pod using Kokkos::view_alloc with deduce_value_type
37  {
38  // Typedef View
39  typedef View<double**, Kokkos::DefaultExecutionSpace> view_type;
40 
41  // Create two rank 2 Views that will be used for deducing types and Fad dims
42  view_type v1("v1", 10, 4);
43  view_type v2("v2", 10, 4);
44 
45  // Get common type of the Views
46  // FIXME I have not seen any use of the return object of common_view_alloc_prop downstream that relies
47  // on its member types
48  #ifndef SACADO_HAS_NEW_KOKKOS_VIEW_IMPL
49  using CommonValueType = typename decltype( Kokkos::common_view_alloc_prop( v1, v2 ) )::value_type;
50  using ScalarArrayType = typename decltype( Kokkos::common_view_alloc_prop( v1, v2 ) )::scalar_array_type;
51  #else
52  using CommonValueType = double;
53  using ScalarArrayType = double;
54  #endif
55  // Create an instance of this returned type to pass to ViewCtorProp via view_alloc function
56  auto cvt_for_ctorprop = Kokkos::common_view_alloc_prop(v1, v2);
57 
58  // Create a view with the common type and the max fad_dim of the views passed to deduce_value_type
59  typedef View< CommonValueType** > ViewCommonType;
60  ViewCommonType vct1( Kokkos::view_alloc("vct1", cvt_for_ctorprop), 10, 4 ); // fad_dim deduced and comes from the cvt_for_ctorprop
61 
62  TEST_EQUALITY(vct1.extent(0), v1.extent(0));
63  TEST_EQUALITY(vct1.extent(1), v1.extent(1));
64  TEST_EQUALITY(vct1.extent(2), v1.extent(2));
65  TEST_EQUALITY( Kokkos::dimension_scalar(vct1), 0);
67  bool check_eq_scalar_double = std::is_same < double, ScalarArrayType >::value;
68  TEST_EQUALITY(check_eq_kokkos_type, true);
69  TEST_EQUALITY(check_eq_scalar_double, true);
70  }
71  // Test constructing a View of Fad using Kokkos::view_alloc with deduce_value_type
72  {
73  // Typedef View
74  typedef View<FadType**, Kokkos::DefaultExecutionSpace> view_type;
75 
76  // Create two rank 2 Views that will be used for deducing types and Fad dims
77  view_type v1("v1", 10, 4, derivative_dim_plus_one );
78  view_type v2("v2", 10, 4, derivative_dim_plus_one );
79 
80  // Get common type of the Views
81  // FIXME I have not seen any use of the return object of common_view_alloc_prop downstream that relies
82  // on its member types
83  #ifndef SACADO_HAS_NEW_KOKKOS_VIEW_IMPL
84  using CommonValueType = typename decltype( Kokkos::common_view_alloc_prop( v1, v2 ) )::value_type;
85  using ScalarArrayType = typename decltype( Kokkos::common_view_alloc_prop( v1, v2 ) )::scalar_array_type;
86  #else
88  using ScalarArrayType = typename Sacado::ValueType<CommonValueType>::type;
89  #endif
90  // Create an instance of this returned type to pass to ViewCtorProp via view_alloc function
91  auto cvt_for_ctorprop = Kokkos::common_view_alloc_prop(v1, v2);
92 
93  // Create a view with the common type and the max fad_dim of the views passed to deduce_value_type
94  typedef View< CommonValueType** > ViewCommonType;
95  ViewCommonType vct1( Kokkos::view_alloc("vct1", cvt_for_ctorprop), 10, 4 ); // fad_dim deduced and comes from the cvt_for_ctorprop
96 
97  TEST_EQUALITY(dimension_scalar(vct1), derivative_dim_plus_one);
98  TEST_EQUALITY(vct1.extent(0), v1.extent(0));
99  TEST_EQUALITY(vct1.extent(1), v1.extent(1));
100  TEST_EQUALITY(vct1.extent(2), v1.extent(2));
102  bool check_eq_fad_type = std::is_same < CommonValueType, FadType >::value;
103  bool check_eq_scalar_double = std::is_same < double, ScalarArrayType >::value;
104  TEST_EQUALITY(check_neq_kokkos_type, false);
105  TEST_EQUALITY(check_eq_fad_type, true);
106  TEST_EQUALITY(check_eq_scalar_double, true);
107  }
108  // Test constructing a View from mix of View and Viewof Fads using Kokkos::view_alloc with deduce_value_type
109  {
110  // Typedef View
111  typedef View<FadType**, Kokkos::DefaultExecutionSpace> view_of_fad_type;
112  typedef View<double**, Kokkos::DefaultExecutionSpace> view_of_pod_type;
113 
114  // Create two rank 2 Views that will be used for deducing types and Fad dims
115  view_of_fad_type v1("v1", 10, 4, derivative_dim_plus_one );
116  view_of_pod_type v2("v2", 10, 4);
117 
118  // Get common type of the Views
119  #ifndef SACADO_HAS_NEW_KOKKOS_VIEW_IMPL
120  using CommonValueType = typename decltype( Kokkos::common_view_alloc_prop( v1, v2 ) )::value_type;
121  using ScalarArrayType = typename decltype( Kokkos::common_view_alloc_prop( v1, v2 ) )::scalar_array_type;
122  #else
124  using ScalarArrayType = typename Sacado::ValueType<CommonValueType>::type;
125  #endif
126  // Create an instance of this returned type to pass to ViewCtorProp via view_alloc function
127  auto cvt_for_ctorprop = Kokkos::common_view_alloc_prop(v1, v2);
128  // Create a view with the common type and the max fad_dim of the views passed to deduce_value_type
129  typedef View< CommonValueType** > ViewCommonType;
130  ViewCommonType vct1( Kokkos::view_alloc("vct1", cvt_for_ctorprop), 10, 4 ); // fad_dim deduced and comes from the cvt_for_ctorprop
131 
132  TEST_EQUALITY(dimension_scalar(vct1), derivative_dim_plus_one);
133  TEST_EQUALITY(vct1.extent(0), v1.extent(0));
134  TEST_EQUALITY(vct1.extent(1), v1.extent(1));
135  TEST_EQUALITY(vct1.extent(2), v1.extent(2));
137  bool check_eq_fad_type = std::is_same < CommonValueType, FadType >::value;
138  bool check_eq_scalar_double = std::is_same < double, ScalarArrayType >::value;
139  TEST_EQUALITY(check_neq_kokkos_type, false);
140  TEST_EQUALITY(check_eq_fad_type, true);
141  TEST_EQUALITY(check_eq_scalar_double, true);
142  }
143  // Test constructing a DynRankView using Kokkos::view_alloc with deduce_value_type
144  {
145  // Typedef View
146  typedef DynRankView<FadType, Kokkos::DefaultExecutionSpace> view_type;
147 
148  // Create two rank 2 Views that will be used for deducing types and Fad dims
149  view_type v1("v1", 10, 4, derivative_dim_plus_one );
150  view_type v2("v2", 10, 4, derivative_dim_plus_one );
151 
152  // Get common type of the Views
153  #ifndef SACADO_HAS_NEW_KOKKOS_VIEW_IMPL
154  using CommonValueType = typename decltype( Kokkos::common_view_alloc_prop( v1, v2 ) )::value_type;
155  using ScalarArrayType = typename decltype( Kokkos::common_view_alloc_prop( v1, v2 ) )::scalar_array_type;
156  #else
158  using ScalarArrayType = typename Sacado::ValueType<CommonValueType>::type;
159  #endif
160  // Create an instance of this returned type to pass to ViewCtorProp via view_alloc function
161  auto cvt_for_ctorprop = Kokkos::common_view_alloc_prop(v1, v2);
162 
163  // Create a view with the common type and the max fad_dim of the views passed to deduce_value_type
164  typedef DynRankView< CommonValueType > ViewCommonType;
165  ViewCommonType vct1( Kokkos::view_alloc("vct1", cvt_for_ctorprop), 10, 4 ); // fad_dim deduced and comes from the cvt_for_ctorprop
166 
167  TEST_EQUALITY(dimension_scalar(vct1), derivative_dim_plus_one);
168  TEST_EQUALITY(vct1.extent(0), v1.extent(0));
169  TEST_EQUALITY(vct1.extent(1), v1.extent(1));
170  TEST_EQUALITY(vct1.extent(2), v1.extent(2));
171  TEST_EQUALITY(Kokkos::rank(vct1), 2);
173  bool check_eq_fad_type = std::is_same < CommonValueType, FadType >::value;
174  bool check_eq_scalar_double = std::is_same < double, ScalarArrayType >::value;
175  TEST_EQUALITY(check_neq_kokkos_type, false);
176  TEST_EQUALITY(check_eq_fad_type, true);
177  TEST_EQUALITY(check_eq_scalar_double, true);
178  }
179  // Test constructing a DynRankView from mix of DynRankView and DynRankView of Fads using Kokkos::view_alloc with deduce_value_type
180  {
181  // Typedef View
182  typedef DynRankView<FadType, Kokkos::DefaultExecutionSpace> view_of_fad_type;
183  typedef DynRankView<double, Kokkos::DefaultExecutionSpace> view_of_pod_type;
184 
185  // Create two rank 2 Views that will be used for deducing types and Fad dims
186  view_of_fad_type v1("v1", 10, 4, derivative_dim_plus_one );
187  view_of_pod_type v2("v2", 10, 4);
188 
189  // Get common type of the Views
190  #ifndef SACADO_HAS_NEW_KOKKOS_VIEW_IMPL
191  using CommonValueType = typename decltype( Kokkos::common_view_alloc_prop( v1, v2 ) )::value_type;
192  using ScalarArrayType = typename decltype( Kokkos::common_view_alloc_prop( v1, v2 ) )::scalar_array_type;
193  #else
195  using ScalarArrayType = typename Sacado::ValueType<CommonValueType>::type;
196  #endif
197  // Create an instance of this returned type to pass to ViewCtorProp via view_alloc function
198  auto cvt_for_ctorprop = Kokkos::common_view_alloc_prop(v1, v2);
199 
200  // Create a view with the common type and the max fad_dim of the views passed to deduce_value_type
201  typedef DynRankView< CommonValueType > ViewCommonType;
202  ViewCommonType vct1( Kokkos::view_alloc("vct1", cvt_for_ctorprop), 10, 4 ); // fad_dim deduced and comes from the cvt_for_ctorprop
203 
204  TEST_EQUALITY(dimension_scalar(vct1), derivative_dim_plus_one);
205  TEST_EQUALITY(vct1.extent(0), v1.extent(0));
206  TEST_EQUALITY(vct1.extent(1), v1.extent(1));
207  TEST_EQUALITY(vct1.extent(2), v1.extent(2));
208  TEST_EQUALITY(Kokkos::rank(vct1), 2);
210  bool check_eq_fad_type = std::is_same < CommonValueType, FadType >::value;
211  bool check_eq_scalar_double = std::is_same < double, ScalarArrayType >::value;
212  TEST_EQUALITY(check_neq_kokkos_type, false);
213  TEST_EQUALITY(check_eq_fad_type, true);
214  TEST_EQUALITY(check_eq_scalar_double, true);
215  }
216 
217 
218 
219 
220  // Test a DynRankView from a DynRankView
221  {
222  DynRankView<FadType> a("a",10,4,13,derivative_dim_plus_one);
223  TEST_EQUALITY(dimension_scalar(a),derivative_dim_plus_one);
224  TEST_EQUALITY(a.rank(),3);
225 
226  auto b = createDynRankView(a,"b",5,3,8);
227  TEST_EQUALITY(dimension_scalar(b),derivative_dim_plus_one);
228  TEST_EQUALITY(b.rank(),3);
229 
230  auto c = createDynRankView(a,view_alloc("c",WithoutInitializing),5,3,8);
231  TEST_EQUALITY(dimension_scalar(c),derivative_dim_plus_one);
232  TEST_EQUALITY(c.rank(),3);
233 
234  using d_type = Kokkos::DynRankView<FadType,Kokkos::LayoutRight>;
235  d_type d = createDynRankViewWithType<d_type>(a,"d",5,3,8);
236  TEST_EQUALITY(dimension_scalar(d),derivative_dim_plus_one);
237  TEST_EQUALITY(d.rank(),3);
238  }
239 
240  // Test a DynRankView from a View
241  {
242  View<FadType*> a("a",8,derivative_dim_plus_one);
243  TEST_EQUALITY(dimension_scalar(a),derivative_dim_plus_one);
244 
245  auto b = createDynRankView(a,"b",5,3,8);
246  TEST_EQUALITY(dimension_scalar(b),derivative_dim_plus_one);
247  TEST_EQUALITY(b.rank(),3);
248 
249  auto c = createDynRankView(a,view_alloc("c",WithoutInitializing),5,3,8);
250  TEST_EQUALITY(dimension_scalar(c),derivative_dim_plus_one);
251  TEST_EQUALITY(c.rank(),3);
252 
253  using d_type = Kokkos::DynRankView<FadType,Kokkos::LayoutRight>;
254  d_type d = createDynRankViewWithType<d_type>(a,"d",5,3,8);
255  TEST_EQUALITY(dimension_scalar(d),derivative_dim_plus_one);
256  TEST_EQUALITY(d.rank(),3);
257  }
258 
259  // Test a View from a View
260  {
261  View<FadType*> a("a",8,derivative_dim_plus_one);
262  TEST_EQUALITY(dimension_scalar(a),derivative_dim_plus_one);
263 
264  using b_type = Kokkos::View<FadType***>;
265  b_type b = createViewWithType<b_type>(a,"b",5,3,8);
266  TEST_EQUALITY(dimension_scalar(b),derivative_dim_plus_one);
267 
268  b_type c = createViewWithType<b_type>(a,view_alloc("c",WithoutInitializing),5,3,8);
269  TEST_EQUALITY(dimension_scalar(c),derivative_dim_plus_one);
270 
271  using d_type = Kokkos::View<FadType***,Kokkos::LayoutRight>;
272  d_type d = createViewWithType<d_type>(a,"d",5,3,8);
273  TEST_EQUALITY(dimension_scalar(d),derivative_dim_plus_one);
274  }
275 
276  // Test a View from a DynRankView
277  {
278  DynRankView<FadType> a("a",10,4,13,derivative_dim_plus_one);
279  TEST_EQUALITY(dimension_scalar(a),derivative_dim_plus_one);
280  TEST_EQUALITY(a.rank(),3);
281 
282  using b_type = Kokkos::View<FadType***>;
283  b_type b = createViewWithType<b_type>(a,"b",5,3,8);
284  TEST_EQUALITY(dimension_scalar(b),derivative_dim_plus_one);
285 
286  b_type c = createViewWithType<b_type>(a,view_alloc("c",WithoutInitializing),5,3,8);
287  TEST_EQUALITY(dimension_scalar(c),derivative_dim_plus_one);
288 
289  using d_type = Kokkos::View<FadType***,Kokkos::LayoutRight>;
290  d_type d = createViewWithType<d_type>(a,"d",5,3,8);
291  TEST_EQUALITY(dimension_scalar(d),derivative_dim_plus_one);
292  }
293 
294  // Test creation of a Fad DynRankView from a double DynRankView
295  {
296  DynRankView<double> a("a",10,4,13);
297  TEST_EQUALITY(dimension_scalar(a),0);
298  TEST_EQUALITY(a.rank(),3);
299 
300  using b_type = Kokkos::DynRankView<FadType,Kokkos::LayoutRight>;
301  b_type b = createDynRankViewWithType<b_type>(a,"b",5,3,8);
302  TEST_EQUALITY(dimension_scalar(b),1);
303  TEST_EQUALITY(b.rank(),3);
304  }
305 
306  // Test a double DynRankView from a double DynRankView
307  {
308  DynRankView<double> a("a",10,4,13);
309  TEST_EQUALITY(dimension_scalar(a),0);
310  TEST_EQUALITY(a.rank(),3);
311 
312  auto b = createDynRankView(a,"b",5,3,8);
313  TEST_EQUALITY(dimension_scalar(b),0);
314  TEST_EQUALITY(b.rank(),3);
315  }
316 
317  // Test double rank 0
318  {
319  DynRankView<double> a("a",10,4,13);
320  TEST_EQUALITY(dimension_scalar(a),0);
321  TEST_EQUALITY(a.rank(),3);
322 
323  auto b = createDynRankView(a,"b");
324  TEST_EQUALITY(dimension_scalar(b),0);
325  TEST_EQUALITY(b.rank(),0);
326  }
327 
328  // Test Fad rank 0
329  {
330  DynRankView<FadType> a("a",10,4,13,derivative_dim_plus_one);
331  TEST_EQUALITY(dimension_scalar(a),derivative_dim_plus_one);
332  TEST_EQUALITY(a.rank(),3);
333 
334  auto b = createDynRankView(a,"b");
335  TEST_EQUALITY(dimension_scalar(b),derivative_dim_plus_one);
336  TEST_EQUALITY(b.rank(),0);
337  }
338 
339  // Test unmanaged view of double
340  {
341  Kokkos::View<double*> a("a",5*3);
342  using b_type = Kokkos::View<double**,Kokkos::MemoryUnmanaged>;
343  b_type b = createViewWithType<b_type>(a,a.data(),5,3);
344  TEST_EQUALITY(b.extent(0),5);
345  TEST_EQUALITY(b.extent(1),3);
346  TEST_EQUALITY(dimension_scalar(b),0);
347  }
348 
349  // Test unmanaged view of Fad
350  {
351  Kokkos::View<FadType*> a("a",5*3,derivative_dim_plus_one);
352  using b_type = Kokkos::View<FadType**,Kokkos::MemoryUnmanaged>;
353  b_type b = createViewWithType<b_type>(a,a.data(),5,3);
354  TEST_EQUALITY(b.extent(0),5);
355  TEST_EQUALITY(b.extent(1),3);
356  TEST_EQUALITY(dimension_scalar(b),derivative_dim_plus_one);
357  }
358 
359  // Test LayoutStride view of double
360  {
361  Kokkos::DynRankView<double> a("a",10,13);
362  auto b = Kokkos::subview(a, std::make_pair(4,8), std::make_pair(5,11));
363  auto c = createDynRankView(b,"c",5,3);
364  using b_type = decltype(b);
365  using c_type = decltype(c);
366  using b_layout = typename b_type::array_layout;
367  using c_layout = typename c_type::array_layout;
368  using default_layout = typename b_type::device_type::execution_space::array_layout;
369  const bool is_b_layout_stride =
371  const bool is_c_default_layout =
373  TEST_EQUALITY(is_b_layout_stride,true);
374  TEST_EQUALITY(is_c_default_layout,true);
375  TEST_EQUALITY(c.rank(),2);
376  TEST_EQUALITY(c.extent(0),5);
377  TEST_EQUALITY(c.extent(1),3);
378  TEST_EQUALITY(dimension_scalar(b),0);
379  }
380 
381  // Test LayoutStride view of Fad
382  {
383  Kokkos::DynRankView<FadType> a("a",10,13,derivative_dim_plus_one);
384  auto b = Kokkos::subview(a, std::make_pair(4,8), std::make_pair(5,11));
385  auto c = createDynRankView(b,"c",5,3);
386  using b_type = decltype(b);
387  using c_type = decltype(c);
388  using b_layout = typename b_type::array_layout;
389  using c_layout = typename c_type::array_layout;
390  using default_layout = typename b_type::device_type::execution_space::array_layout;
391  const bool is_b_layout_stride =
393  const bool is_c_default_layout =
395  TEST_EQUALITY(is_b_layout_stride,true);
396  TEST_EQUALITY(is_c_default_layout,true);
397  TEST_EQUALITY(c.rank(),2);
398  TEST_EQUALITY(c.extent(0),5);
399  TEST_EQUALITY(c.extent(1),3);
400  TEST_EQUALITY(dimension_scalar(b),derivative_dim_plus_one);
401  }
402 
403 }
404 
405 int main( int argc, char* argv[] ) {
406  Teuchos::GlobalMPISession mpiSession(&argc, &argv);
407 
408  Kokkos::initialize();
409 
411 
412  Kokkos::finalize();
413 
414  return res;
415 }
KOKKOS_INLINE_FUNCTION auto subview(const View< D, Kokkos::LayoutContiguous< LayoutSrc, StrideSrc >, P...> &src, Args...args)
std::enable_if< is_view< InputViewType >::value||is_dyn_rank_view< InputViewType >::value, typename Impl::ResultDynRankView< InputViewType >::type >::type createDynRankView(const InputViewType &a, const CtorProp &prop, const Dims...dims)
Wrapper to simplify use of Sacado ViewFactory.
TEUCHOS_UNIT_TEST(Conversion, IsConvertible)
static int runUnitTestsFromMain(int argc, char *argv[])
expr expr1 expr1 expr1 c expr2 expr1 expr2 expr1 expr2 expr1 expr1 expr1 expr1 c expr2 expr1 expr2 expr1 expr2 expr1 expr1 expr1 expr1 c *expr2 expr1 expr2 expr1 expr2 expr1 expr1 expr1 expr1 c expr2 expr1 expr2 expr1 expr2 expr1 expr1 expr1 expr2 expr1 expr2 expr1 expr1 expr1 expr2 expr1 expr2 expr1 expr1 expr1 c
int main()
Definition: ad_example.cpp:171
std::enable_if< is_view< InputViewType >::value||is_dyn_rank_view< InputViewType >::value, ResultViewType >::type createDynRankViewWithType(const InputViewType &a, const CtorProp &prop, const Dims...dims)
Wrapper to simplify use of Sacado ViewFactory.
int value
std::enable_if< is_view< InputViewType >::value||is_dyn_rank_view< InputViewType >::value, ResultViewType >::type createViewWithType(const InputViewType &a, const CtorProp &prop, const Dims...dims)
Wrapper to simplify use of Sacado ViewFactory.
#define TEST_EQUALITY(v1, v2)
Base template specification for Promote.