Stokhos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Stokhos_SacadoUQPCESerializationTests.cpp
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 
14 
15 #include "Teuchos_Array.hpp"
16 #include "Stokhos.hpp"
18 #include "Sacado_Fad_DFad.hpp"
19 #include "Sacado_mpl_apply.hpp"
20 #include "Sacado_Random.hpp"
21 
22 using Teuchos::RCP;
23 using Teuchos::rcp;
24 
25 // Common setup for unit tests
26 template <typename PCEType, typename FadType>
27 struct UnitTestSetup {
30 
31  typedef typename PCEType::cijk_type kokkos_cijk_type;
33 
36 
37  typedef typename Sacado::mpl::apply<FadType,PCEType>::type FadPCEType;
40  int sz;
41 
43  const int d = 2;
44  const int p = 7;
45 
46  // Create product basis
48  for (int i=0; i<d; i++)
49  bases[i] =
51  basis =
53 
54  // Triple product tensor
57  kokkos_cijk =
58  Stokhos::create_product_tensor<execution_space>(*basis, *Cijk);
59 
60  // Serializers
62  rcp(new PCESerializerT(
65  fad_pce_serializer = rcp(new FadPCESerializerT(pce_serializer, 8));
66 
67  sz = basis->size();
68  }
69 };
70 
71 template <typename PCEType>
73  const std::string& tag,
74  Teuchos::FancyOStream& out) {
75 
76  typedef int Ordinal;
78 
79  // Serialize
80  Ordinal count = x.size();
81  Ordinal bytes = SerT::fromCountToIndirectBytes(count, &x[0]);
82  char *charBuffer = new char[bytes];
83  SerT::serialize(count, &x[0], bytes, charBuffer);
84  Ordinal count2 = SerT::fromIndirectBytesToCount(bytes, charBuffer);
85 
86  // Check counts match
87  bool success = (count == count2);
88  out << tag << " serialize/deserialize count test";
89  if (success)
90  out << " passed";
91  else
92  out << " failed";
93  out << ": \n\tExpected: " << count << ", \n\tGot: " << count2 << "."
94  << std::endl;
95 
96  // Deserialize
97  Teuchos::Array<PCEType> x2(count2);
98  for (Ordinal i=0; i<count2; i++)
99  x2[i].reset(x[i].cijk());
100  SerT::deserialize(bytes, charBuffer, count2, &x2[0]);
101 
102  delete [] charBuffer;
103 
104  // Check coefficients match
105  for (Ordinal i=0; i<count; i++) {
106  bool success2 = Sacado::IsEqual<PCEType>::eval(x[i], x2[i]);
107  out << tag << " serialize/deserialize pce test " << i;
108  if (success2)
109  out << " passed";
110  else
111  out << " failed";
112  out << ": \n\tExpected: " << x[i] << ", \n\tGot: " << x2[i]
113  << "." << std::endl;
114  success = success && success2;
115  }
116 
117  return success;
118 }
119 
120 template <typename PCEType, typename Serializer>
122  const Serializer& serializer,
123  const std::string& tag,
124  Teuchos::FancyOStream& out) {
125 
126  typedef int Ordinal;
127 
128  // Serialize
129  Ordinal count = x.size();
130  Ordinal bytes = serializer.fromCountToIndirectBytes(count, &x[0]);
131  char *charBuffer = new char[bytes];
132  serializer.serialize(count, &x[0], bytes, charBuffer);
133 
134  // Reset x to given cijk
135  for (Ordinal i=0; i<count; i++)
136  x[i].reset(serializer.getSerializerCijk());
137 
138  // Deserialize
139  Ordinal count2 = serializer.fromIndirectBytesToCount(bytes, charBuffer);
140  Teuchos::Array<PCEType> x2(count2);
141  serializer.deserialize(bytes, charBuffer, count2, &x2[0]);
142 
143  delete [] charBuffer;
144 
145  // Check counts match
146  bool success = (count == count2);
147  out << tag << " serialize/deserialize count test";
148  if (success)
149  out << " passed";
150  else
151  out << " failed";
152  out << ": \n\tExpected: " << count << ", \n\tGot: " << count2 << "."
153  << std::endl;
154 
155  // Check coefficients match
156  for (Ordinal i=0; i<count; i++) {
157  bool success2 = Sacado::IsEqual<PCEType>::eval(x[i], x2[i]);
158  out << tag << " serialize/deserialize pce test " << i;
159  if (success2)
160  out << " passed";
161  else
162  out << " failed";
163  out << ": \n\tExpected: " << x[i] << ", \n\tGot: " << x2[i]
164  << "." << std::endl;
165  success = success && success2;
166  }
167 
168  return success;
169 }
170 
171 template <typename PCEType, typename Serializer>
173  const Serializer& serializer,
174  const std::string& tag,
175  Teuchos::FancyOStream& out) {
176 
177  typedef int Ordinal;
178 
179  // Serialize
180  Ordinal count = x.size();
181  Ordinal bytes = serializer.fromCountToIndirectBytes(count, &x[0]);
182  char *charBuffer = new char[bytes];
183  serializer.serialize(count, &x[0], bytes, charBuffer);
184 
185  // Reset x to given cijk
186  Ordinal sz = serializer.getSerializerSize();
187  typedef typename Serializer::value_serializer_type VST;
188  RCP<const VST> vs = serializer.getValueSerializer();
189  for (Ordinal i=0; i<count; i++) {
190  x[i].expand(sz);
191  for (Ordinal j=0; j<sz; j++)
192  x[i].fastAccessDx(j).reset(vs->getSerializerCijk());
193  x[i].val().reset(vs->getSerializerCijk());
194  }
195 
196  // Deserialize
197  Ordinal count2 = serializer.fromIndirectBytesToCount(bytes, charBuffer);
198  Teuchos::Array<PCEType> x2(count2);
199  serializer.deserialize(bytes, charBuffer, count2, &x2[0]);
200 
201  delete [] charBuffer;
202 
203  // Check counts match
204  bool success = (count == count2);
205  out << tag << " serialize/deserialize count test";
206  if (success)
207  out << " passed";
208  else
209  out << " failed";
210  out << ": \n\tExpected: " << count << ", \n\tGot: " << count2 << "."
211  << std::endl;
212 
213  // Check coefficients match
214  for (Ordinal i=0; i<count; i++) {
215  bool success2 = Sacado::IsEqual<PCEType>::eval(x[i], x2[i]);
216  out << tag << " serialize/deserialize pce test " << i;
217  if (success2)
218  out << " passed";
219  else
220  out << " failed";
221  out << ": \n\tExpected: " << x[i] << ", \n\tGot: " << x2[i]
222  << "." << std::endl;
223  success = success && success2;
224  }
225 
226  return success;
227 }
228 
229 typedef Sacado::Fad::DFad<double> FadType;
230 typedef Kokkos::DefaultExecutionSpace execution_space;
233 
234 Sacado::Random<double> rnd;
235 TEUCHOS_UNIT_TEST( UQ_PCE_Serialization, Uniform ) {
237  int n = 7;
239  for (int i=0; i<n; i++) {
240  x[i].reset(setup.kokkos_cijk);
241  for (int j=0; j<setup.sz; j++)
242  x[i].fastAccessCoeff(j) = rnd.number();
243  }
244  bool success1 = testSerialization(
245  x, std::string("UQ::PCE") + " Uniform", out);
246  bool success2 = testSerialization(
247  x, *setup.pce_serializer, std::string("UQ::PCE") + " Uniform PTS", out);
248  success = success1 && success2;
249 }
250 
251 TEUCHOS_UNIT_TEST( UQ_PCE_Serialization, Empty ) {
253  int n = 7;
255  for (int i=0; i<n; i++) {
256  x[i] = rnd.number();
257  }
258  bool success1 = testSerialization(
259  x, std::string("UQ::PCE") + " Empty", out);
260  bool success2 = testSerialization(
261  x, *setup.pce_serializer, std::string("UQ::PCE") + " Empty PTS", out);
262  success = success1 && success2;
263 }
264 
265 TEUCHOS_UNIT_TEST( UQ_PCE_Serialization, Mixed ) {
267  int n = 6;
268  int p[] = { 5, 0, 8, 8, 3, 0 };
270  for (int i=0; i<n; i++) {
271  x[i].reset(setup.kokkos_cijk, p[i]);
272  for (int j=0; j<p[i]; j++)
273  x[i].fastAccessCoeff(j) = rnd.number();
274  }
275  bool success1 = testSerialization(
276  x, std::string("UQ::PCE") + " Mixed", out);
277  bool success2 = testSerialization(
278  x, *setup.pce_serializer, std::string("UQ::PCE") + " Mixed PTS", out);
279  success = success1 && success2;
280 }
281 
282 TEUCHOS_UNIT_TEST( UQ_PCE_Serialization, FadPCEUniform ) {
284  typedef Sacado::mpl::apply<FadType,PCEType>::type FadPCEType;
285  int n = 7;
286  int p = 3;
288  for (int i=0; i<n; i++) {
289  PCEType f(setup.kokkos_cijk);
290  for (int k=0; k<setup.sz; k++)
291  f.fastAccessCoeff(k) = rnd.number();
292  x[i] = FadPCEType(p, f);
293  for (int j=0; j<p; j++) {
294  PCEType g(setup.kokkos_cijk);
295  for (int k=0; k<setup.sz; k++)
296  g.fastAccessCoeff(k) = rnd.number();
297  x[i].fastAccessDx(j) = g;
298  }
299  }
300  success =
302  std::string("UQ::PCE") + " Nested Uniform", out);
303 }
304 TEUCHOS_UNIT_TEST( UQ_PCE_Serialization, FadPCEEmptyInner ) {
306  typedef Sacado::mpl::apply<FadType,PCEType>::type FadPCEType;
307  int n = 7;
308  int p = 3;
310  for (int i=0; i<n; i++) {
311  PCEType f(setup.kokkos_cijk);
312  for (int k=0; k<setup.sz; k++)
313  f.fastAccessCoeff(k) = rnd.number();
314  x[i] = FadPCEType(p, f);
315  for (int j=0; j<p; j++)
316  x[i].fastAccessDx(j) = rnd.number();
317  }
318  success =
320  x, *setup.fad_pce_serializer,
321  std::string("UQ::PCE") + " Nested Empty Inner", out);
322 }
323 TEUCHOS_UNIT_TEST( UQ_PCE_Serialization, FadPCEEmptyOuter ) {
325  typedef Sacado::mpl::apply<FadType,PCEType>::type FadPCEType;
326  int n = 7;
328  for (int i=0; i<n; i++) {
329  PCEType f(setup.kokkos_cijk);
330  for (int k=0; k<setup.sz; k++)
331  f.fastAccessCoeff(k) = rnd.number();
332  x[i] = FadPCEType(f);
333  }
334  success =
336  x, *setup.fad_pce_serializer,
337  std::string("UQ::PCE") + " Nested Empty Outer", out);
338 }
339 TEUCHOS_UNIT_TEST( UQ_PCE_Serialization, FadPCEEmptyAll ) {
341  typedef Sacado::mpl::apply<FadType,PCEType>::type FadPCEType;
342  int n = 7;
344  for (int i=0; i<n; i++) {
345  x[i] = rnd.number();
346  }
347  success =
349  x, *setup.fad_pce_serializer,
350  std::string("UQ::PCE") + " Nested Empty All", out);
351 }
Stokhos::StandardStorage< int, double > storage_type
RCP< FadPCESerializerT > fad_pce_serializer
Kokkos::DefaultExecutionSpace execution_space
RCP< const Stokhos::CompletePolynomialBasis< int, double > > basis
RCP< PCESerializerT > pce_serializer
Teuchos::ValueTypeSerializer< int, FadPCEType > FadPCESerializerT
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Sacado::Fad::DFad< double > FadType
expr expr expr expr fastAccessDx(i, j)) FAD_UNARYOP_MACRO(exp
virtual Teuchos::RCP< Stokhos::Sparse3Tensor< ordinal_type, value_type > > computeTripleProductTensor() const
Compute triple product tensor.
bool testSerialization(const Teuchos::Array< VecType > &x, const std::string &tag, Teuchos::FancyOStream &out)
expr1 expr1 expr1 expr2 expr1 expr1 c expr2 expr1 c fastAccessCoeff(j)-expr2.val(j)
Sacado::Random< double > rnd
KOKKOS_INLINE_FUNCTION constexpr std::enable_if< is_view_uq_pce< view_type >::value, typename CijkType< view_type >::type >::type cijk(const view_type &view)
Sacado::mpl::apply< FadType, PCEType >::type FadPCEType
Legendre polynomial basis.
bool testNestedSerialization(Teuchos::Array< VecType > &x, const Serializer &serializer, const std::string &tag, Teuchos::FancyOStream &out)
Sacado::UQ::PCE< storage_type > PCEType
ScalarType f(const Teuchos::Array< ScalarType > &x, double a, double b)
size_type size() const
TEUCHOS_UNIT_TEST(tAdaptivityManager, test_interface)
int n
ScalarType g(const Teuchos::Array< ScalarType > &x, const ScalarType &y)
RCP< Stokhos::Sparse3Tensor< int, double > > Cijk
Teuchos::ValueTypeSerializer< int, PCEType > PCESerializerT