Sacado Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MoveTests.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 
14 
15 #include "Sacado_No_Kokkos.hpp"
16 
17 #if SACADO_ENABLE_NEW_DESIGN
18 #include "Sacado_Fad_Exp_DVFad.hpp"
20 
21 // Size used for all Fad types
22 const int global_fad_size = 10;
23 
24 //
25 // Move constructor tests
26 //
27 
28 TEUCHOS_UNIT_TEST( MoveConstructorTests, SFad )
29 {
30  typedef double value_type;
32  success = true;
33 
34  // Initialize AD type
35  ad_type x(global_fad_size, value_type(1.5));
36  for (int i=0; i<global_fad_size; ++i)
37  x.fastAccessDx(i) = value_type(2.0+i);
38 
39  // Move x into y
40  ad_type y = std::move(x);
41 
42  // Check y is correct
44  TEST_EQUALITY_CONST( y.val(), value_type(1.5) );
45  for (int i=0; i<global_fad_size; ++i) {
46  TEST_EQUALITY_CONST( y.dx(i), value_type(2.0+i) );
47  }
48 
49  // Check x is correct
51  TEST_EQUALITY_CONST( x.val(), value_type(1.5) );
52  TEST_INEQUALITY( x.dx(), y.dx() );
53  for (int i=0; i<global_fad_size; ++i) {
54  TEST_EQUALITY_CONST( x.dx(i), value_type(2.0+i) );
55  }
56 }
57 
58 TEUCHOS_UNIT_TEST( MoveConstructorTests, SLFad )
59 {
60  typedef double value_type;
62  success = true;
63 
64  // Initialize AD type
65  ad_type x(global_fad_size, value_type(1.5));
66  for (int i=0; i<global_fad_size; ++i)
67  x.fastAccessDx(i) = value_type(2.0+i);
68 
69  // Move x into y
70  ad_type y = std::move(x);
71 
72  // Check y is correct
74  TEST_EQUALITY_CONST( y.val(), value_type(1.5) );
75  for (int i=0; i<global_fad_size; ++i) {
76  TEST_EQUALITY_CONST( y.dx(i), value_type(2.0+i) );
77  }
78 
79  // Check x is correct
81  TEST_EQUALITY_CONST( x.val(), value_type(1.5) );
82  TEST_INEQUALITY( x.dx(), y.dx() );
83  for (int i=0; i<global_fad_size; ++i) {
84  TEST_EQUALITY_CONST( x.dx(i), value_type(2.0+i) );
85  }
86 }
87 
88 TEUCHOS_UNIT_TEST( MoveConstructorTests, DFad )
89 {
90  typedef double value_type;
91  typedef Sacado::Fad::Exp::DFad<value_type> ad_type;
92  success = true;
93 
94  // Initialize AD type
95  ad_type x(global_fad_size, value_type(1.5));
96  for (int i=0; i<global_fad_size; ++i)
97  x.fastAccessDx(i) = value_type(2.0+i);
98 
99  // Move x into y
100  ad_type y = std::move(x);
101 
102  // Check y is correct
104  TEST_EQUALITY_CONST( y.val(), value_type(1.5) );
105  for (int i=0; i<global_fad_size; ++i) {
106  TEST_EQUALITY_CONST( y.dx(i), value_type(2.0+i) );
107  }
108 
109  // Check x is correct
110  value_type *null_ptr = nullptr;
111  TEST_EQUALITY_CONST( x.size(), 0 );
112  TEST_EQUALITY_CONST( x.val(), value_type(1.5) );
113  TEST_EQUALITY( x.dx(), null_ptr );
114 }
115 
116 TEUCHOS_UNIT_TEST( MoveConstructorTests, DVFad_Owned )
117 {
118  typedef double value_type;
119  typedef Sacado::Fad::Exp::DVFad<value_type> ad_type;
120  success = true;
121 
122  // Initialize AD type
123  ad_type x(global_fad_size, value_type(1.5));
124  for (int i=0; i<global_fad_size; ++i)
125  x.fastAccessDx(i) = value_type(2.0+i);
126 
127  // Move x into y
128  ad_type y = std::move(x);
129 
130  // Check y is correct
132  TEST_EQUALITY_CONST( y.val(), value_type(1.5) );
133  for (int i=0; i<global_fad_size; ++i) {
134  TEST_EQUALITY_CONST( y.dx(i), value_type(2.0+i) );
135  }
136 
137  // Check x is correct
139  TEST_EQUALITY_CONST( x.val(), value_type(1.5) );
140  TEST_INEQUALITY( x.dx(), y.dx() );
141  for (int i=0; i<global_fad_size; ++i) {
142  TEST_EQUALITY_CONST( x.dx(i), value_type(2.0+i) );
143  }
144 }
145 
146 TEUCHOS_UNIT_TEST( MoveConstructorTests, DVFad_Unowned )
147 {
148  typedef double value_type;
149  typedef Sacado::Fad::Exp::DVFad<value_type> ad_type;
150  success = true;
151 
152  // Initialize AD type
153  value_type x_val = value_type(1.5);
154  std::vector<value_type> x_dx(global_fad_size);
155  for (int i=0; i<global_fad_size; ++i)
156  x_dx[i] = value_type(2.0+i);
157  ad_type x(global_fad_size, &x_val, x_dx.data(), 1, false);
158 
159  // Move x into y
160  ad_type y = std::move(x);
161 
162  // Check y is correct
164  TEST_EQUALITY_CONST( y.val(), value_type(1.5) );
165  for (int i=0; i<global_fad_size; ++i) {
166  TEST_EQUALITY_CONST( y.dx(i), value_type(2.0+i) );
167  }
168 
169  // Check x is correct
171  TEST_EQUALITY_CONST( x.val(), value_type(1.5) );
172  TEST_INEQUALITY( x.dx(), y.dx() );
173  for (int i=0; i<global_fad_size; ++i) {
174  TEST_EQUALITY_CONST( x.dx(i), value_type(2.0+i) );
175  }
176 }
177 
178 TEUCHOS_UNIT_TEST( MoveConstructorTests, ViewFad )
179 {
180  typedef double value_type;
181  typedef Sacado::Fad::Exp::DFad<value_type> dfad_type;
183  success = true;
184 
185  // Initialize AD type
186  value_type x_val = value_type(1.5);
187  std::vector<value_type> x_dx(global_fad_size);
188  for (int i=0; i<global_fad_size; ++i)
189  x_dx[i] = value_type(2.0+i);
190  ad_type x(x_dx.data(), &x_val, global_fad_size, 1);
191 
192  // Move x into y
193  ad_type y = std::move(x);
194 
195  // Check y is correct
197  TEST_EQUALITY_CONST( y.val(), value_type(1.5) );
198  TEST_EQUALITY( y.dx(), x_dx.data() );
199  for (int i=0; i<global_fad_size; ++i) {
200  TEST_EQUALITY_CONST( y.dx(i), value_type(2.0+i) );
201  }
202 
203  // Check x is correct
205  TEST_EQUALITY_CONST( x.val(), value_type(1.5) );
206  TEST_EQUALITY( x.dx(), x_dx.data() );
207  for (int i=0; i<global_fad_size; ++i) {
208  TEST_EQUALITY_CONST( x.dx(i), value_type(2.0+i) );
209  }
210 }
211 
212 //
213 // Move assignment tests
214 //
215 
216 TEUCHOS_UNIT_TEST( MoveAssignmentTests, SFad )
217 {
218  typedef double value_type;
220  success = true;
221 
222  // Initialize AD type
223  ad_type x(global_fad_size, value_type(1.5));
224  for (int i=0; i<global_fad_size; ++i)
225  x.fastAccessDx(i) = value_type(2.0+i);
226 
227  // Move x into y
228  ad_type y(0.5);
229  y = std::move(x);
230 
231  // Check y is correct
233  TEST_EQUALITY_CONST( y.val(), value_type(1.5) );
234  for (int i=0; i<global_fad_size; ++i) {
235  TEST_EQUALITY_CONST( y.dx(i), value_type(2.0+i) );
236  }
237 
238  // Check x is correct
240  TEST_EQUALITY_CONST( x.val(), value_type(1.5) );
241  TEST_INEQUALITY( x.dx(), y.dx() );
242  for (int i=0; i<global_fad_size; ++i) {
243  TEST_EQUALITY_CONST( x.dx(i), value_type(2.0+i) );
244  }
245 }
246 
247 TEUCHOS_UNIT_TEST( MoveAssignmentTests, SLFad )
248 {
249  typedef double value_type;
251  success = true;
252 
253  // Initialize AD type
254  ad_type x(global_fad_size, value_type(1.5));
255  for (int i=0; i<global_fad_size; ++i)
256  x.fastAccessDx(i) = value_type(2.0+i);
257 
258  // Move x into y
259  ad_type y(0.5);
260  y = std::move(x);
261 
262  // Check y is correct
264  TEST_EQUALITY_CONST( y.val(), value_type(1.5) );
265  for (int i=0; i<global_fad_size; ++i) {
266  TEST_EQUALITY_CONST( y.dx(i), value_type(2.0+i) );
267  }
268 
269  // Check x is correct
271  TEST_EQUALITY_CONST( x.val(), value_type(1.5) );
272  TEST_INEQUALITY( x.dx(), y.dx() );
273  for (int i=0; i<global_fad_size; ++i) {
274  TEST_EQUALITY_CONST( x.dx(i), value_type(2.0+i) );
275  }
276 }
277 
278 TEUCHOS_UNIT_TEST( MoveAssignmentTests, DFad )
279 {
280  typedef double value_type;
281  typedef Sacado::Fad::Exp::DFad<value_type> ad_type;
282  success = true;
283 
284  // Initialize AD type
285  ad_type x(global_fad_size, value_type(1.5));
286  for (int i=0; i<global_fad_size; ++i)
287  x.fastAccessDx(i) = value_type(2.0+i);
288 
289  // Move x into y
290  ad_type y(0.5);
291  y = std::move(x);
292 
293  // Check y is correct
295  TEST_EQUALITY_CONST( y.val(), value_type(1.5) );
296  for (int i=0; i<global_fad_size; ++i) {
297  TEST_EQUALITY_CONST( y.dx(i), value_type(2.0+i) );
298  }
299 
300  // Check x is correct
301  value_type *null_ptr = nullptr;
302  TEST_EQUALITY_CONST( x.size(), 0 );
303  TEST_EQUALITY_CONST( x.val(), value_type(1.5) );
304  TEST_EQUALITY( x.dx(), null_ptr );
305 }
306 
307 TEUCHOS_UNIT_TEST( MoveAssignmentTests, DVFad_Owned )
308 {
309  typedef double value_type;
310  typedef Sacado::Fad::Exp::DVFad<value_type> ad_type;
311  success = true;
312 
313  // Initialize AD type
314  ad_type x(global_fad_size, value_type(1.5));
315  for (int i=0; i<global_fad_size; ++i)
316  x.fastAccessDx(i) = value_type(2.0+i);
317 
318  // Move x into y
319  ad_type y(0.5);
320  y = std::move(x);
321 
322  // Check y is correct
324  TEST_EQUALITY_CONST( y.val(), value_type(1.5) );
325  for (int i=0; i<global_fad_size; ++i) {
326  TEST_EQUALITY_CONST( y.dx(i), value_type(2.0+i) );
327  }
328 
329  // Check x is correct
331  TEST_EQUALITY_CONST( x.val(), value_type(1.5) );
332  TEST_INEQUALITY( x.dx(), y.dx() );
333  for (int i=0; i<global_fad_size; ++i) {
334  TEST_EQUALITY_CONST( x.dx(i), value_type(2.0+i) );
335  }
336 }
337 
338 TEUCHOS_UNIT_TEST( MoveAssignmentTests, DVFad_Unowned )
339 {
340  typedef double value_type;
341  typedef Sacado::Fad::Exp::DVFad<value_type> ad_type;
342  success = true;
343 
344  // Initialize AD type
345  value_type x_val = value_type(1.5);
346  std::vector<value_type> x_dx(global_fad_size);
347  for (int i=0; i<global_fad_size; ++i)
348  x_dx[i] = value_type(2.0+i);
349  ad_type x(global_fad_size, &x_val, x_dx.data(), 1, false);
350 
351  // Move x into y
352  ad_type y(0.5);
353  y = std::move(x);
354 
355  // Check y is correct
357  TEST_EQUALITY_CONST( y.val(), value_type(1.5) );
358  for (int i=0; i<global_fad_size; ++i) {
359  TEST_EQUALITY_CONST( y.dx(i), value_type(2.0+i) );
360  }
361 
362  // Check x is correct
364  TEST_EQUALITY_CONST( x.val(), value_type(1.5) );
365  TEST_INEQUALITY( x.dx(), y.dx() );
366  for (int i=0; i<global_fad_size; ++i) {
367  TEST_EQUALITY_CONST( x.dx(i), value_type(2.0+i) );
368  }
369 }
370 
371 TEUCHOS_UNIT_TEST( MoveAssignmentTests, ViewFad )
372 {
373  typedef double value_type;
374  typedef Sacado::Fad::Exp::DFad<value_type> dfad_type;
376  success = true;
377 
378  // Initialize AD type
379  value_type x_val = value_type(1.5);
380  std::vector<value_type> x_dx(global_fad_size);
381  for (int i=0; i<global_fad_size; ++i)
382  x_dx[i] = value_type(2.0+i);
383  ad_type x(x_dx.data(), &x_val, global_fad_size, 1);
384 
385  // Move x into y
386  value_type y_val = value_type(0.5);
387  std::vector<value_type> y_dx(global_fad_size);
388  for (int i=0; i<global_fad_size; ++i)
389  y_dx[i] = value_type(20.0+i);
390  ad_type y(y_dx.data(), &y_val, global_fad_size, 1);
391  y = std::move(x);
392 
393  // Check y is correct
395  TEST_EQUALITY_CONST( y.val(), value_type(1.5) );
396  TEST_EQUALITY( y.dx(), y_dx.data() );
397  for (int i=0; i<global_fad_size; ++i) {
398  TEST_EQUALITY_CONST( y.dx(i), value_type(2.0+i) );
399  }
400 
401  // Check x is correct
403  TEST_EQUALITY_CONST( x.val(), value_type(1.5) );
404  TEST_EQUALITY( x.dx(), x_dx.data() );
405  for (int i=0; i<global_fad_size; ++i) {
406  TEST_EQUALITY_CONST( x.dx(i), value_type(2.0+i) );
407  }
408 }
409 
410 #endif
411 
412 int main( int argc, char* argv[] ) {
413  Teuchos::GlobalMPISession mpiSession(&argc, &argv);
415 }
#define TEST_INEQUALITY(v1, v2)
#define TEST_EQUALITY_CONST(v1, v2)
const int global_fad_size
GeneralFad< StaticStorage< T, Num > > SLFad
TEUCHOS_UNIT_TEST(Conversion, IsConvertible)
static int runUnitTestsFromMain(int argc, char *argv[])
GeneralFad< DynamicStorage< T > > DFad
Forward-mode AD class templated on the storage for the derivative array.
int main()
Definition: ad_example.cpp:171
#define TEST_EQUALITY(v1, v2)
GeneralFad< ViewStorage< T, static_length, static_stride, U > > ViewFad
GeneralFad< StaticFixedStorage< T, Num > > SFad
const double y