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 //
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
45 
46 #include "Teuchos_Array.hpp"
47 #include "Stokhos.hpp"
49 #include "Sacado_Fad_DFad.hpp"
50 #include "Sacado_mpl_apply.hpp"
51 #include "Sacado_Random.hpp"
52 
53 using Teuchos::RCP;
54 using Teuchos::rcp;
55 
56 // Common setup for unit tests
57 template <typename PCEType, typename FadType>
58 struct UnitTestSetup {
61 
62  typedef typename PCEType::cijk_type kokkos_cijk_type;
64 
67 
68  typedef typename Sacado::mpl::apply<FadType,PCEType>::type FadPCEType;
71  int sz;
72 
74  const int d = 2;
75  const int p = 7;
76 
77  // Create product basis
79  for (int i=0; i<d; i++)
80  bases[i] =
82  basis =
84 
85  // Triple product tensor
88  kokkos_cijk =
89  Stokhos::create_product_tensor<execution_space>(*basis, *Cijk);
90 
91  // Serializers
93  rcp(new PCESerializerT(
96  fad_pce_serializer = rcp(new FadPCESerializerT(pce_serializer, 8));
97 
98  sz = basis->size();
99  }
100 };
101 
102 template <typename PCEType>
104  const std::string& tag,
105  Teuchos::FancyOStream& out) {
106 
107  typedef int Ordinal;
109 
110  // Serialize
111  Ordinal count = x.size();
112  Ordinal bytes = SerT::fromCountToIndirectBytes(count, &x[0]);
113  char *charBuffer = new char[bytes];
114  SerT::serialize(count, &x[0], bytes, charBuffer);
115  Ordinal count2 = SerT::fromIndirectBytesToCount(bytes, charBuffer);
116 
117  // Check counts match
118  bool success = (count == count2);
119  out << tag << " serialize/deserialize count test";
120  if (success)
121  out << " passed";
122  else
123  out << " failed";
124  out << ": \n\tExpected: " << count << ", \n\tGot: " << count2 << "."
125  << std::endl;
126 
127  // Deserialize
128  Teuchos::Array<PCEType> x2(count2);
129  for (Ordinal i=0; i<count2; i++)
130  x2[i].reset(x[i].cijk());
131  SerT::deserialize(bytes, charBuffer, count2, &x2[0]);
132 
133  delete [] charBuffer;
134 
135  // Check coefficients match
136  for (Ordinal i=0; i<count; i++) {
137  bool success2 = Sacado::IsEqual<PCEType>::eval(x[i], x2[i]);
138  out << tag << " serialize/deserialize pce test " << i;
139  if (success2)
140  out << " passed";
141  else
142  out << " failed";
143  out << ": \n\tExpected: " << x[i] << ", \n\tGot: " << x2[i]
144  << "." << std::endl;
145  success = success && success2;
146  }
147 
148  return success;
149 }
150 
151 template <typename PCEType, typename Serializer>
153  const Serializer& serializer,
154  const std::string& tag,
155  Teuchos::FancyOStream& out) {
156 
157  typedef int Ordinal;
158 
159  // Serialize
160  Ordinal count = x.size();
161  Ordinal bytes = serializer.fromCountToIndirectBytes(count, &x[0]);
162  char *charBuffer = new char[bytes];
163  serializer.serialize(count, &x[0], bytes, charBuffer);
164 
165  // Reset x to given cijk
166  for (Ordinal i=0; i<count; i++)
167  x[i].reset(serializer.getSerializerCijk());
168 
169  // Deserialize
170  Ordinal count2 = serializer.fromIndirectBytesToCount(bytes, charBuffer);
171  Teuchos::Array<PCEType> x2(count2);
172  serializer.deserialize(bytes, charBuffer, count2, &x2[0]);
173 
174  delete [] charBuffer;
175 
176  // Check counts match
177  bool success = (count == count2);
178  out << tag << " serialize/deserialize count test";
179  if (success)
180  out << " passed";
181  else
182  out << " failed";
183  out << ": \n\tExpected: " << count << ", \n\tGot: " << count2 << "."
184  << std::endl;
185 
186  // Check coefficients match
187  for (Ordinal i=0; i<count; i++) {
188  bool success2 = Sacado::IsEqual<PCEType>::eval(x[i], x2[i]);
189  out << tag << " serialize/deserialize pce test " << i;
190  if (success2)
191  out << " passed";
192  else
193  out << " failed";
194  out << ": \n\tExpected: " << x[i] << ", \n\tGot: " << x2[i]
195  << "." << std::endl;
196  success = success && success2;
197  }
198 
199  return success;
200 }
201 
202 template <typename PCEType, typename Serializer>
204  const Serializer& serializer,
205  const std::string& tag,
206  Teuchos::FancyOStream& out) {
207 
208  typedef int Ordinal;
209 
210  // Serialize
211  Ordinal count = x.size();
212  Ordinal bytes = serializer.fromCountToIndirectBytes(count, &x[0]);
213  char *charBuffer = new char[bytes];
214  serializer.serialize(count, &x[0], bytes, charBuffer);
215 
216  // Reset x to given cijk
217  Ordinal sz = serializer.getSerializerSize();
218  typedef typename Serializer::value_serializer_type VST;
219  RCP<const VST> vs = serializer.getValueSerializer();
220  for (Ordinal i=0; i<count; i++) {
221  x[i].expand(sz);
222  for (Ordinal j=0; j<sz; j++)
223  x[i].fastAccessDx(j).reset(vs->getSerializerCijk());
224  x[i].val().reset(vs->getSerializerCijk());
225  }
226 
227  // Deserialize
228  Ordinal count2 = serializer.fromIndirectBytesToCount(bytes, charBuffer);
229  Teuchos::Array<PCEType> x2(count2);
230  serializer.deserialize(bytes, charBuffer, count2, &x2[0]);
231 
232  delete [] charBuffer;
233 
234  // Check counts match
235  bool success = (count == count2);
236  out << tag << " serialize/deserialize count test";
237  if (success)
238  out << " passed";
239  else
240  out << " failed";
241  out << ": \n\tExpected: " << count << ", \n\tGot: " << count2 << "."
242  << std::endl;
243 
244  // Check coefficients match
245  for (Ordinal i=0; i<count; i++) {
246  bool success2 = Sacado::IsEqual<PCEType>::eval(x[i], x2[i]);
247  out << tag << " serialize/deserialize pce test " << i;
248  if (success2)
249  out << " passed";
250  else
251  out << " failed";
252  out << ": \n\tExpected: " << x[i] << ", \n\tGot: " << x2[i]
253  << "." << std::endl;
254  success = success && success2;
255  }
256 
257  return success;
258 }
259 
260 typedef Sacado::Fad::DFad<double> FadType;
261 typedef Kokkos::DefaultExecutionSpace execution_space;
264 
265 Sacado::Random<double> rnd;
266 TEUCHOS_UNIT_TEST( UQ_PCE_Serialization, Uniform ) {
268  int n = 7;
270  for (int i=0; i<n; i++) {
271  x[i].reset(setup.kokkos_cijk);
272  for (int j=0; j<setup.sz; j++)
273  x[i].fastAccessCoeff(j) = rnd.number();
274  }
275  bool success1 = testSerialization(
276  x, std::string("UQ::PCE") + " Uniform", out);
277  bool success2 = testSerialization(
278  x, *setup.pce_serializer, std::string("UQ::PCE") + " Uniform PTS", out);
279  success = success1 && success2;
280 }
281 
282 TEUCHOS_UNIT_TEST( UQ_PCE_Serialization, Empty ) {
284  int n = 7;
286  for (int i=0; i<n; i++) {
287  x[i] = rnd.number();
288  }
289  bool success1 = testSerialization(
290  x, std::string("UQ::PCE") + " Empty", out);
291  bool success2 = testSerialization(
292  x, *setup.pce_serializer, std::string("UQ::PCE") + " Empty PTS", out);
293  success = success1 && success2;
294 }
295 
296 TEUCHOS_UNIT_TEST( UQ_PCE_Serialization, Mixed ) {
298  int n = 6;
299  int p[] = { 5, 0, 8, 8, 3, 0 };
301  for (int i=0; i<n; i++) {
302  x[i].reset(setup.kokkos_cijk, p[i]);
303  for (int j=0; j<p[i]; j++)
304  x[i].fastAccessCoeff(j) = rnd.number();
305  }
306  bool success1 = testSerialization(
307  x, std::string("UQ::PCE") + " Mixed", out);
308  bool success2 = testSerialization(
309  x, *setup.pce_serializer, std::string("UQ::PCE") + " Mixed PTS", out);
310  success = success1 && success2;
311 }
312 
313 TEUCHOS_UNIT_TEST( UQ_PCE_Serialization, FadPCEUniform ) {
315  typedef Sacado::mpl::apply<FadType,PCEType>::type FadPCEType;
316  int n = 7;
317  int p = 3;
319  for (int i=0; i<n; i++) {
320  PCEType f(setup.kokkos_cijk);
321  for (int k=0; k<setup.sz; k++)
322  f.fastAccessCoeff(k) = rnd.number();
323  x[i] = FadPCEType(p, f);
324  for (int j=0; j<p; j++) {
325  PCEType g(setup.kokkos_cijk);
326  for (int k=0; k<setup.sz; k++)
327  g.fastAccessCoeff(k) = rnd.number();
328  x[i].fastAccessDx(j) = g;
329  }
330  }
331  success =
333  std::string("UQ::PCE") + " Nested Uniform", out);
334 }
335 TEUCHOS_UNIT_TEST( UQ_PCE_Serialization, FadPCEEmptyInner ) {
337  typedef Sacado::mpl::apply<FadType,PCEType>::type FadPCEType;
338  int n = 7;
339  int p = 3;
341  for (int i=0; i<n; i++) {
342  PCEType f(setup.kokkos_cijk);
343  for (int k=0; k<setup.sz; k++)
344  f.fastAccessCoeff(k) = rnd.number();
345  x[i] = FadPCEType(p, f);
346  for (int j=0; j<p; j++)
347  x[i].fastAccessDx(j) = rnd.number();
348  }
349  success =
351  x, *setup.fad_pce_serializer,
352  std::string("UQ::PCE") + " Nested Empty Inner", out);
353 }
354 TEUCHOS_UNIT_TEST( UQ_PCE_Serialization, FadPCEEmptyOuter ) {
356  typedef Sacado::mpl::apply<FadType,PCEType>::type FadPCEType;
357  int n = 7;
359  for (int i=0; i<n; i++) {
360  PCEType f(setup.kokkos_cijk);
361  for (int k=0; k<setup.sz; k++)
362  f.fastAccessCoeff(k) = rnd.number();
363  x[i] = FadPCEType(f);
364  }
365  success =
367  x, *setup.fad_pce_serializer,
368  std::string("UQ::PCE") + " Nested Empty Outer", out);
369 }
370 TEUCHOS_UNIT_TEST( UQ_PCE_Serialization, FadPCEEmptyAll ) {
372  typedef Sacado::mpl::apply<FadType,PCEType>::type FadPCEType;
373  int n = 7;
375  for (int i=0; i<n; i++) {
376  x[i] = rnd.number();
377  }
378  success =
380  x, *setup.fad_pce_serializer,
381  std::string("UQ::PCE") + " Nested Empty All", out);
382 }
383 
384 int main( int argc, char* argv[] ) {
385  Teuchos::GlobalMPISession mpiSession(&argc, &argv);
386 
387  Kokkos::initialize();
388 // Kokkos::HostSpace::execution_space::initialize();
389 // if (!Kokkos::DefaultExecutionSpace::is_initialized())
390 // Kokkos::DefaultExecutionSpace::initialize();
391 
393 
394  Kokkos::finalize();
395 // Kokkos::HostSpace::execution_space::finalize();
396 // if (Kokkos::DefaultExecutionSpace::is_initialized())
397 // Kokkos::DefaultExecutionSpace::finalize();
398 
399  return res;
400 }
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
static int runUnitTestsFromMain(int argc, char *argv[])
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)
int main(int argc, char **argv)
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