Sacado Package Browser (Single Doxygen Collection)  Version of the Day
ViewFactoryTests.cpp
Go to the documentation of this file.
1 // @HEADER
2 // ***********************************************************************
3 //
4 // Sacado Package
5 // Copyright (2006) Sandia Corporation
6 //
7 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
8 // the U.S. Government retains certain rights in this software.
9 //
10 // This library is free software; you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as
12 // published by the Free Software Foundation; either version 2.1 of the
13 // License, or (at your option) any later version.
14 //
15 // This library is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 // Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public
21 // License along with this library; if not, write to the Free Software
22 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
23 // USA
24 // Questions? Contact David M. Gay (dmgay@sandia.gov) or Eric T. Phipps
25 // (etphipp@sandia.gov).
26 //
27 // ***********************************************************************
28 // @HEADER
32 
33 #include "Sacado.hpp"
34 
35 #include "Kokkos_ViewFactory.hpp"
36 
37 TEUCHOS_UNIT_TEST(view_factory, dyn_rank_views)
38 {
40  using Kokkos::View;
41  using Kokkos::DynRankView;
46  using Kokkos::Experimental::view_alloc;
47  using Kokkos::Experimental::WithoutInitializing;
48  const unsigned derivative_dim_plus_one = 7;
49 
50  // Test a DynRankView from a DynRankView
51  {
52  DynRankView<FadType> a("a",10,4,13,derivative_dim_plus_one);
53  TEST_EQUALITY(dimension_scalar(a),derivative_dim_plus_one);
54  TEST_EQUALITY(a.rank(),3);
55 
56  auto b = createDynRankView(a,"b",5,3,8);
57  TEST_EQUALITY(dimension_scalar(b),derivative_dim_plus_one);
58  TEST_EQUALITY(b.rank(),3);
59 
60  auto c = createDynRankView(a,view_alloc("c",WithoutInitializing),5,3,8);
61  TEST_EQUALITY(dimension_scalar(c),derivative_dim_plus_one);
62  TEST_EQUALITY(c.rank(),3);
63 
64  using d_type = Kokkos::DynRankView<FadType,Kokkos::LayoutRight>;
65  d_type d = createDynRankViewWithType<d_type>(a,"d",5,3,8);
66  TEST_EQUALITY(dimension_scalar(d),derivative_dim_plus_one);
67  TEST_EQUALITY(d.rank(),3);
68  }
69 
70  // Test a DynRankView from a View
71  {
72  View<FadType*> a("a",8,derivative_dim_plus_one);
73  TEST_EQUALITY(dimension_scalar(a),derivative_dim_plus_one);
74 
75  auto b = createDynRankView(a,"b",5,3,8);
76  TEST_EQUALITY(dimension_scalar(b),derivative_dim_plus_one);
77  TEST_EQUALITY(b.rank(),3);
78 
79  auto c = createDynRankView(a,view_alloc("c",WithoutInitializing),5,3,8);
80  TEST_EQUALITY(dimension_scalar(c),derivative_dim_plus_one);
81  TEST_EQUALITY(c.rank(),3);
82 
83  using d_type = Kokkos::DynRankView<FadType,Kokkos::LayoutRight>;
84  d_type d = createDynRankViewWithType<d_type>(a,"d",5,3,8);
85  TEST_EQUALITY(dimension_scalar(d),derivative_dim_plus_one);
86  TEST_EQUALITY(d.rank(),3);
87  }
88 
89  // Test a View from a View
90  {
91  View<FadType*> a("a",8,derivative_dim_plus_one);
92  TEST_EQUALITY(dimension_scalar(a),derivative_dim_plus_one);
93 
94  using b_type = Kokkos::View<FadType***>;
95  b_type b = createViewWithType<b_type>(a,"b",5,3,8);
96  TEST_EQUALITY(dimension_scalar(b),derivative_dim_plus_one);
97 
98  b_type c = createViewWithType<b_type>(a,view_alloc("c",WithoutInitializing),5,3,8);
99  TEST_EQUALITY(dimension_scalar(c),derivative_dim_plus_one);
100 
101  using d_type = Kokkos::View<FadType***,Kokkos::LayoutRight>;
102  d_type d = createViewWithType<d_type>(a,"d",5,3,8);
103  TEST_EQUALITY(dimension_scalar(d),derivative_dim_plus_one);
104  }
105 
106  // Test a View from a DynRankView
107  {
108  DynRankView<FadType> a("a",10,4,13,derivative_dim_plus_one);
109  TEST_EQUALITY(dimension_scalar(a),derivative_dim_plus_one);
110  TEST_EQUALITY(a.rank(),3);
111 
112  using b_type = Kokkos::View<FadType***>;
113  b_type b = createViewWithType<b_type>(a,"b",5,3,8);
114  TEST_EQUALITY(dimension_scalar(b),derivative_dim_plus_one);
115 
116  b_type c = createViewWithType<b_type>(a,view_alloc("c",WithoutInitializing),5,3,8);
117  TEST_EQUALITY(dimension_scalar(c),derivative_dim_plus_one);
118 
119  using d_type = Kokkos::View<FadType***,Kokkos::LayoutRight>;
120  d_type d = createViewWithType<d_type>(a,"d",5,3,8);
121  TEST_EQUALITY(dimension_scalar(d),derivative_dim_plus_one);
122  }
123 
124  // Test creation of a Fad DynRankView from a double DynRankView
125  {
126  DynRankView<double> a("a",10,4,13);
128  TEST_EQUALITY(a.rank(),3);
129 
130  using b_type = Kokkos::DynRankView<FadType,Kokkos::LayoutRight>;
131  b_type b = createDynRankViewWithType<b_type>(a,"b",5,3,8);
133  TEST_EQUALITY(b.rank(),3);
134  }
135 
136  // Test a double DynRankView from a double DynRankView
137  {
138  DynRankView<double> a("a",10,4,13);
140  TEST_EQUALITY(a.rank(),3);
141 
142  auto b = createDynRankView(a,"b",5,3,8);
144  TEST_EQUALITY(b.rank(),3);
145  }
146 
147  // Test double rank 0
148  {
149  DynRankView<double> a("a",10,4,13);
151  TEST_EQUALITY(a.rank(),3);
152 
153  auto b = createDynRankView(a,"b");
155  TEST_EQUALITY(b.rank(),0);
156  }
157 
158  // Test Fad rank 0
159  {
160  DynRankView<FadType> a("a",10,4,13,derivative_dim_plus_one);
161  TEST_EQUALITY(dimension_scalar(a),derivative_dim_plus_one);
162  TEST_EQUALITY(a.rank(),3);
163 
164  auto b = createDynRankView(a,"b");
165  TEST_EQUALITY(dimension_scalar(b),derivative_dim_plus_one);
166  TEST_EQUALITY(b.rank(),0);
167  }
168 
169  // Test unmanaged view of double
170  {
171  Kokkos::View<double*> a("a",5*3);
172  using b_type = Kokkos::View<double**,Kokkos::MemoryUnmanaged>;
173  b_type b = createViewWithType<b_type>(a,a.data(),5,3);
174  TEST_EQUALITY(b.dimension_0(),5);
175  TEST_EQUALITY(b.dimension_1(),3);
177  }
178 
179  // Test unmanaged view of Fad
180  {
181  Kokkos::View<FadType*> a("a",5*3,derivative_dim_plus_one);
182  using b_type = Kokkos::View<FadType**,Kokkos::MemoryUnmanaged>;
183  b_type b = createViewWithType<b_type>(a,a.data(),5,3);
184  TEST_EQUALITY(b.dimension_0(),5);
185  TEST_EQUALITY(b.dimension_1(),3);
186  TEST_EQUALITY(dimension_scalar(b),derivative_dim_plus_one);
187  }
188 
189  // Test LayoutStride view of double
190  {
191  Kokkos::DynRankView<double> a("a",10,13);
192  auto b = Kokkos::subview(a, std::make_pair(4,8), std::make_pair(5,11));
193  auto c = createDynRankView(b,"c",5,3);
194  using b_type = decltype(b);
195  using c_type = decltype(c);
196  using b_layout = typename b_type::array_layout;
197  using c_layout = typename c_type::array_layout;
198  using default_layout = typename b_type::device_type::execution_space::array_layout;
199  const bool is_b_layout_stride =
200  std::is_same<b_layout,Kokkos::LayoutStride>::value;
201  const bool is_c_default_layout =
202  std::is_same<c_layout,default_layout>::value;
203  TEST_EQUALITY(is_b_layout_stride,true);
204  TEST_EQUALITY(is_c_default_layout,true);
205  TEST_EQUALITY(c.rank(),2);
206  TEST_EQUALITY(c.dimension_0(),5);
207  TEST_EQUALITY(c.dimension_1(),3);
209  }
210 
211  // Test LayoutStride view of Fad
212  {
213  Kokkos::DynRankView<FadType> a("a",10,13,derivative_dim_plus_one);
214  auto b = Kokkos::subview(a, std::make_pair(4,8), std::make_pair(5,11));
215  auto c = createDynRankView(b,"c",5,3);
216  using b_type = decltype(b);
217  using c_type = decltype(c);
218  using b_layout = typename b_type::array_layout;
219  using c_layout = typename c_type::array_layout;
220  using default_layout = typename b_type::device_type::execution_space::array_layout;
221  const bool is_b_layout_stride =
222  std::is_same<b_layout,Kokkos::LayoutStride>::value;
223  const bool is_c_default_layout =
224  std::is_same<c_layout,default_layout>::value;
225  TEST_EQUALITY(is_b_layout_stride,true);
226  TEST_EQUALITY(is_c_default_layout,true);
227  TEST_EQUALITY(c.rank(),2);
228  TEST_EQUALITY(c.dimension_0(),5);
229  TEST_EQUALITY(c.dimension_1(),3);
230  TEST_EQUALITY(dimension_scalar(b),derivative_dim_plus_one);
231  }
232 
233 }
234 
235 int main( int argc, char* argv[] ) {
236  Teuchos::GlobalMPISession mpiSession(&argc, &argv);
237 
238  Kokkos::initialize();
239 
241 
242  Kokkos::finalize();
243 
244  return res;
245 }
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.
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.
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(int argc, char *argv[])
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.
TEUCHOS_UNIT_TEST(view_factory, dyn_rank_views)
TEST_EQUALITY(rcp_dynamic_cast< const EnhancedNumberValidator< double > >(castedDep1->getValuesAndValidators().find("val1") ->second, true) ->getMax(), double1Vali->getMax())
unsigned dimension_scalar(const View &v, const ViewPack &... views)