Stokhos Package Browser (Single Doxygen Collection) Version of the Day
Loading...
Searching...
No Matches
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
41#include "Teuchos_UnitTestHarness.hpp"
42#include "Teuchos_TestingHelpers.hpp"
43#include "Teuchos_UnitTestRepository.hpp"
44#include "Teuchos_GlobalMPISession.hpp"
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
53using Teuchos::RCP;
54using Teuchos::rcp;
55
56// Common setup for unit tests
57template <typename PCEType, typename FadType>
58struct UnitTestSetup {
59 RCP<const Stokhos::CompletePolynomialBasis<int,double> > basis;
60 RCP<Stokhos::Sparse3Tensor<int,double> > Cijk;
61
62 typedef typename PCEType::cijk_type kokkos_cijk_type;
64
65 typedef Teuchos::ValueTypeSerializer<int, PCEType> PCESerializerT;
66 RCP<PCESerializerT> pce_serializer;
67
68 typedef typename Sacado::mpl::apply<FadType,PCEType>::type FadPCEType;
69 typedef Teuchos::ValueTypeSerializer<int, FadPCEType> FadPCESerializerT;
70 RCP<FadPCESerializerT> fad_pce_serializer;
71 int sz;
72
74 const int d = 2;
75 const int p = 7;
76
77 // Create product basis
78 Teuchos::Array< RCP<const Stokhos::OneDOrthogPolyBasis<int,double> > > bases(d);
79 for (int i=0; i<d; i++)
80 bases[i] =
82 basis =
84
85 // Triple product tensor
86 typedef typename PCEType::execution_space execution_space;
87 Cijk = basis->computeTripleProductTensor();
89 Stokhos::create_product_tensor<execution_space>(*basis, *Cijk);
90
91 // Serializers
93 rcp(new PCESerializerT(
95 rcp(new Teuchos::ValueTypeSerializer<int,double>())));
97
98 sz = basis->size();
99 }
100};
101
102template <typename PCEType>
103bool testSerialization(const Teuchos::Array<PCEType>& x,
104 const std::string& tag,
105 Teuchos::FancyOStream& out) {
106
107 typedef int Ordinal;
108 typedef Teuchos::SerializationTraits<Ordinal,PCEType> SerT;
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
151template <typename PCEType, typename Serializer>
152bool testSerialization(Teuchos::Array<PCEType>& x,
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
202template <typename PCEType, typename Serializer>
203bool testNestedSerialization(Teuchos::Array<PCEType>& x,
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
260typedef Sacado::Fad::DFad<double> FadType;
261typedef Kokkos::DefaultExecutionSpace execution_space;
264
265Sacado::Random<double> rnd;
266TEUCHOS_UNIT_TEST( UQ_PCE_Serialization, Uniform ) {
267 UnitTestSetup<PCEType, FadType> setup;
268 int n = 7;
269 Teuchos::Array<PCEType> x(n);
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
282TEUCHOS_UNIT_TEST( UQ_PCE_Serialization, Empty ) {
283 UnitTestSetup<PCEType, FadType> setup;
284 int n = 7;
285 Teuchos::Array<PCEType> x(n);
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
296TEUCHOS_UNIT_TEST( UQ_PCE_Serialization, Mixed ) {
297 UnitTestSetup<PCEType, FadType> setup;
298 int n = 6;
299 int p[] = { 5, 0, 8, 8, 3, 0 };
300 Teuchos::Array<PCEType> x(n);
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
313TEUCHOS_UNIT_TEST( UQ_PCE_Serialization, FadPCEUniform ) {
314 UnitTestSetup<PCEType, FadType> setup;
315 typedef Sacado::mpl::apply<FadType,PCEType>::type FadPCEType;
316 int n = 7;
317 int p = 3;
318 Teuchos::Array<FadPCEType> x(n);
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 =
332 testNestedSerialization(x, *setup.fad_pce_serializer,
333 std::string("UQ::PCE") + " Nested Uniform", out);
334}
335TEUCHOS_UNIT_TEST( UQ_PCE_Serialization, FadPCEEmptyInner ) {
336 UnitTestSetup<PCEType, FadType> setup;
337 typedef Sacado::mpl::apply<FadType,PCEType>::type FadPCEType;
338 int n = 7;
339 int p = 3;
340 Teuchos::Array<FadPCEType> x(n);
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}
354TEUCHOS_UNIT_TEST( UQ_PCE_Serialization, FadPCEEmptyOuter ) {
355 UnitTestSetup<PCEType, FadType> setup;
356 typedef Sacado::mpl::apply<FadType,PCEType>::type FadPCEType;
357 int n = 7;
358 Teuchos::Array<FadPCEType> x(n);
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}
370TEUCHOS_UNIT_TEST( UQ_PCE_Serialization, FadPCEEmptyAll ) {
371 UnitTestSetup<PCEType, FadType> setup;
372 typedef Sacado::mpl::apply<FadType,PCEType>::type FadPCEType;
373 int n = 7;
374 Teuchos::Array<FadPCEType> x(n);
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}
expr1 expr1 expr1 expr2 expr1 expr1 c expr2 expr1 c fastAccessCoeff(j) - expr2.val(j)
expr expr expr expr fastAccessDx(i, j)) FAD_UNARYOP_MACRO(exp
UnitTestSetup< Kokkos::Cuda > setup
Kokkos::DefaultExecutionSpace execution_space
Stokhos::DynamicStorage< int, double, execution_space > storage_type
TEUCHOS_UNIT_TEST(UQ_PCE_Serialization, Uniform)
Sacado::Random< double > rnd
Sacado::UQ::PCE< storage_type > PCEType
bool testSerialization(const Teuchos::Array< PCEType > &x, const std::string &tag, Teuchos::FancyOStream &out)
Sacado::Fad::DFad< double > FadType
bool testNestedSerialization(Teuchos::Array< PCEType > &x, const Serializer &serializer, const std::string &tag, Teuchos::FancyOStream &out)
Multivariate orthogonal polynomial basis generated from a total-order complete-polynomial tensor prod...
Legendre polynomial basis.
ScalarType g(const Teuchos::Array< ScalarType > &x, const ScalarType &y)
ScalarType f(const Teuchos::Array< ScalarType > &x, double a, double b)
Teuchos::ValueTypeSerializer< int, PCEType > PCESerializerT
Sacado::mpl::apply< FadType, PCEType >::type FadPCEType
Teuchos::ValueTypeSerializer< int, FadPCEType > FadPCESerializerT
RCP< const Stokhos::CompletePolynomialBasis< int, double > > basis
RCP< Stokhos::Sparse3Tensor< int, double > > Cijk
RCP< PCESerializerT > pce_serializer
RCP< FadPCESerializerT > fad_pce_serializer