Stokhos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Sacado_UQ_PCE_ScalarTraitsImp.hpp
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 
10 #ifndef SACADO_UQ_PCE_SCALARTRAITSIMP_HPP
11 #define SACADO_UQ_PCE_SCALARTRAITSIMP_HPP
12 
13 #include "Teuchos_ScalarTraits.hpp"
15 #include "Teuchos_RCP.hpp"
16 #include "Teuchos_Assert.hpp"
17 #include "Sacado_mpl_apply.hpp"
18 #include "Teuchos_as.hpp"
19 
20 #include <iterator>
21 
22 namespace Sacado {
23 
24  namespace UQ {
25 
27  template <typename PCEType>
33 
37 
38  typedef typename Sacado::mpl::apply<storage_type,ordinal_type,value_mag_type>::type storage_mag_type;
39  typedef typename Sacado::mpl::apply<storage_type,ordinal_type,value_half_type>::type storage_half_type;
40  typedef typename Sacado::mpl::apply<storage_type,ordinal_type,value_double_type>::type storage_double_type;
41 
43  typedef typename Sacado::mpl::apply<PCEType, storage_half_type>::type halfPrecision;
44  typedef typename Sacado::mpl::apply<PCEType, storage_double_type>::type doublePrecision;
46 
48 
49  static const bool isComplex = TVT::isComplex;
50  static const bool isOrdinal = TVT::isOrdinal;
51  static const bool isComparable = TVT::isComparable;
53 
54  static value_mag_type eps() { return TVT::eps(); }
55 
56  static value_mag_type sfmin() { return TVT::sfmin(); }
57 
58  static value_mag_type base() { return TVT::base(); }
59 
60  static value_mag_type prec() { return TVT::prec(); }
61 
62  static value_mag_type t() { return TVT::t(); }
63 
64  static value_mag_type rnd() { return TVT::rnd(); }
65 
66  static value_mag_type emin() { return TVT::emin(); }
67 
68  static value_mag_type rmin() { return TVT::rmin(); }
69 
70  static value_mag_type emax() { return TVT::emax(); }
71 
72  static value_mag_type rmax() { return TVT::rmax(); }
73 
74  static magnitudeType magnitude(const PCEType& a) {
75  return a.two_norm();
76  }
77 
78  static innerProductType innerProduct(const PCEType& a, const PCEType& b) {
79  return a.inner_product(b);
80  }
81 
82  static PCEType zero() { return PCEType(0.0); }
83 
84  static PCEType one() { return PCEType(1.0); }
85 
86  static PCEType conjugate(const PCEType& x) {
87  PCEType y = x;
88  y.copyForWrite();
89  y.val() = TVT::conjugate(x.val());
90  return y;
91  }
92 
93  static magnitudeType real(const PCEType& x) {
95  const ordinal_type sz = x.size();
96  for (ordinal_type i=0; i<sz; ++i) {
97  value_mag_type t = TVT::real(x.fastAccessCoeff(i));
98  m +=t*t;
99  }
100  return std::sqrt(m);
101  }
102 
103  static magnitudeType imag(const PCEType& x) {
104  magnitudeType m = magnitudeType(0.0);
105  const ordinal_type sz = x.size();
106  for (ordinal_type i=0; i<sz; ++i) {
107  value_mag_type t = TVT::imag(x.fastAccessCoeff(i));
108  m +=t*t;
109  }
110  return std::sqrt(m);
111  }
112 
113 
114  static value_type nan() { return TVT::nan(); }
115 
116  static bool isnaninf(const PCEType& x) {
117  for (int i=0; i<x.size(); i++)
118  if (TVT::isnaninf(x.fastAccessCoeff(i)))
119  return true;
120  return false;
121  }
122 
123  static void seedrandom(unsigned int s) { TVT::seedrandom(s); }
124 
125  static PCEType random() { return PCEType(TVT::random()); }
126 
127  static const char * name() { return "Sacado::UQ::PCE<>"; }
128 
129  static PCEType squareroot(const PCEType& x) { return std::sqrt(x); }
130 
131  static PCEType pow(const PCEType& x, const PCEType& y) {
132  return std::pow(x,y);
133  }
134 
135  static PCEType log(const PCEType& x) { return std::log(x); }
136 
137  static PCEType log10(const PCEType& x) { return std::log10(x); }
138 
139  }; // class PCEScalarTraitsImp
140 
142  template <typename TypeTo, typename PCEType>
144  typedef typename Sacado::ValueType<PCEType>::type ValueT;
146  static TypeTo convert( const PCEType t ) {
147  return VTCT::convert(t.val());
148  }
149  static TypeTo safeConvert( const PCEType t ) {
150  return VTCT::safeConvert(t.val());
151  }
152  };
153 
155  template <typename Ordinal, typename PCEType>
157  typedef typename Sacado::ValueType<PCEType>::type ValueT;
161 
162  public:
163 
165  static const bool supportsDirectSerialization = false;
166 
168 
169 
172  const PCEType buffer[]) {
173  Ordinal bytes = 0;
174  for (Ordinal i=0; i<count; i++) {
175  int sz = buffer[i].size();
177  Ordinal b2 = vSerT::fromCountToIndirectBytes(sz, buffer[i].coeff());
179  bytes += b1+b2+b3;
180  }
181  return bytes;
182  }
183 
185  static void serialize (const Ordinal count,
186  const PCEType buffer[],
187  const Ordinal bytes,
188  char charBuffer[]) {
189  for (Ordinal i=0; i<count; i++) {
190  // First serialize size
191  int sz = buffer[i].size();
193  iSerT::serialize(1, &sz, b1, charBuffer);
194  charBuffer += b1;
195 
196  // Next serialize PCE coefficients
197  Ordinal b2 = vSerT::fromCountToIndirectBytes(sz, buffer[i].coeff());
199  oSerT::serialize(1, &b2, b3, charBuffer);
200  charBuffer += b3;
201  vSerT::serialize(sz, buffer[i].coeff(), b2, charBuffer);
202  charBuffer += b2;
203  }
204  }
205 
208  const char charBuffer[]) {
209  Ordinal count = 0;
210  Ordinal bytes_used = 0;
211  while (bytes_used < bytes) {
212 
213  // Bytes for size
215  bytes_used += b1;
216  charBuffer += b1;
217 
218  // Bytes for PCE coefficients
220  const Ordinal *b2 = oSerT::convertFromCharPtr(charBuffer);
221  bytes_used += b3;
222  charBuffer += b3;
223  bytes_used += *b2;
224  charBuffer += *b2;
225 
226  ++count;
227  }
228  return count;
229  }
230 
232  static void deserialize (const Ordinal bytes,
233  const char charBuffer[],
234  const Ordinal count,
235  PCEType buffer[]) {
236  for (Ordinal i=0; i<count; i++) {
237 
238  // Deserialize size
240  const int *sz = iSerT::convertFromCharPtr(charBuffer);
241  charBuffer += b1;
242 
243  // Make sure PCE object is ready to receive values
244  // We assume it has already been initialized with the proper
245  // cijk object
246  if (buffer[i].size() != *sz)
247  buffer[i].reset(buffer[i].cijk(), *sz);
248  buffer[i].copyForWrite();
249 
250  // Deserialize PCE coefficients
252  const Ordinal *b2 = oSerT::convertFromCharPtr(charBuffer);
253  charBuffer += b3;
254  vSerT::deserialize(*b2, charBuffer, *sz, buffer[i].coeff());
255  charBuffer += *b2;
256  }
257 
258  }
259 
261 
262  };
263 
264 
266  template <typename Ordinal, typename PCEType, typename ValueSerializer>
268 
269  public:
270 
272  typedef ValueSerializer value_serializer_type;
273 
275  typedef typename PCEType::cijk_type cijk_type;
276 
277 
278  protected:
279  typedef typename Sacado::ValueType<PCEType>::type ValueT;
282 
285  int sz;
286 
287  public:
288 
290  static const bool supportsDirectSerialization = false;
291 
294  cijk(cijk_), vs(vs_), sz(cijk.dimension()) {}
295 
297  cijk_type getSerializerCijk() const { return cijk; }
298 
301  return vs; }
302 
304 
305 
308  const PCEType buffer[]) const {
309  Ordinal bytes = 0;
310  PCEType *x = NULL;
311  const PCEType *cx;
312  for (Ordinal i=0; i<count; i++) {
313  int my_sz = buffer[i].size();
314  if (sz != my_sz) {
315  if (x == NULL)
316  x = new PCEType;
317  *x = buffer[i];
318  x->reset(cijk);
319  cx = x;
320  }
321  else
322  cx = &(buffer[i]);
324  Ordinal b2 = vs->fromCountToIndirectBytes(sz, cx->coeff());
326  bytes += b1+b2+b3;
327  }
328  if (x != NULL)
329  delete x;
330  return bytes;
331  }
332 
334  void serialize (const Ordinal count,
335  const PCEType buffer[],
336  const Ordinal bytes,
337  char charBuffer[]) const {
338  PCEType *x = NULL;
339  const PCEType *cx;
340  for (Ordinal i=0; i<count; i++) {
341  // First serialize size
342  int my_sz = buffer[i].size();
343  if (sz != my_sz) {
344  if (x == NULL)
345  x = new PCEType(cijk);
346  *x = buffer[i];
347  x->reset(cijk);
348  cx = x;
349  }
350  else
351  cx = &(buffer[i]);
353  iSerT::serialize(1, &sz, b1, charBuffer);
354  charBuffer += b1;
355 
356  // Next serialize PCE coefficients
357  Ordinal b2 = vs->fromCountToIndirectBytes(sz, cx->coeff());
359  oSerT::serialize(1, &b2, b3, charBuffer);
360  charBuffer += b3;
361  vs->serialize(sz, cx->coeff(), b2, charBuffer);
362  charBuffer += b2;
363  }
364  if (x != NULL)
365  delete x;
366  }
367 
370  const char charBuffer[]) const {
371  Ordinal count = 0;
372  Ordinal bytes_used = 0;
373  while (bytes_used < bytes) {
374 
375  // Bytes for size
377  bytes_used += b1;
378  charBuffer += b1;
379 
380  // Bytes for PCE coefficients
382  const Ordinal *b2 = oSerT::convertFromCharPtr(charBuffer);
383  bytes_used += b3;
384  charBuffer += b3;
385  bytes_used += *b2;
386  charBuffer += *b2;
387 
388  ++count;
389  }
390  return count;
391  }
392 
394  void deserialize (const Ordinal bytes,
395  const char charBuffer[],
396  const Ordinal count,
397  PCEType buffer[]) const {
398  for (Ordinal i=0; i<count; i++) {
399 
400  // Deserialize size
402  const int *my_sz = iSerT::convertFromCharPtr(charBuffer);
403  charBuffer += b1;
404 
405  // Create empty PCE object of given size
406  buffer[i].reset(cijk);
407 
408  // Deserialize PCE coefficients
410  const Ordinal *b2 = oSerT::convertFromCharPtr(charBuffer);
411  charBuffer += b3;
412  vs->deserialize(*b2, charBuffer, *my_sz, buffer[i].coeff());
413  charBuffer += *b2;
414  }
415 
416  }
417 
419 
420  };
421 
422  } // namespace UQ
423 
424 } // namespace Sacado
425 
426 #endif // SACADO_FAD_SCALARTRAITSIMP_HPP
KOKKOS_INLINE_FUNCTION PCE< Storage > sqrt(const PCE< Storage > &a)
static Ordinal fromIndirectBytesToCount(const Ordinal bytes, const char charBuffer[])
Return the number of objects for bytes of storage.
static void serialize(const Ordinal count, const intbuffer[], const Ordinal bytes, char charBuffer[])
Sacado::mpl::apply< storage_type, ordinal_type, value_half_type >::type storage_half_type
Stokhos::StandardStorage< int, double > storage_type
static Ordinal fromCountToIndirectBytes(const Ordinal count, const intbuffer[])
static Ordinal fromCountToDirectBytes(const Ordinal count)
static void serialize(const Ordinal count, const PCEType buffer[], const Ordinal bytes, char charBuffer[])
Serialize to an indirect char[] buffer.
Teuchos::SerializationTraits< Ordinal, Ordinal > oSerT
Serializer object for all PCE types.
cijk_type getSerializerCijk() const
Return specified serializer size.
Implementation for Teuchos::ScalarTraits for all PCE types.
static PCEType log10(const PCEType &x)
KOKKOS_INLINE_FUNCTION PCE< Storage > pow(const PCE< Storage > &a, const PCE< Storage > &b)
PCESerializerImp(const cijk_type &cijk_, const Teuchos::RCP< const ValueSerializer > &vs_)
static TypeTo safeConvert(const TypeFrom t)
static Ordinal fromCountToIndirectBytes(const Ordinal count, const T buffer[])
Implementation for Teuchos::ValueTypeConversionTraits for all PCE types.
static magnitudeType real(const PCEType &x)
Sacado::mpl::apply< PCEType, storage_half_type >::type halfPrecision
static TypeTo convert(const TypeFrom t)
Teuchos::SerializationTraits< Ordinal, int > iSerT
static magnitudeType real(value_typea)
static void deserialize(const Ordinal bytes, const char charBuffer[], const Ordinal count, T buffer[])
Ordinal fromIndirectBytesToCount(const Ordinal bytes, const char charBuffer[]) const
Return the number of objects for bytes of storage.
static PCEType conjugate(const PCEType &x)
Teuchos::ValueTypeConversionTraits< TypeTo, ValueT > VTCT
static const bool hasMachineParameters
static magnitudeType sfmin()
static magnitudeType imag(const PCEType &x)
Sacado::mpl::apply< storage_type, ordinal_type, value_double_type >::type storage_double_type
static value_type conjugate(value_typea)
PCEType::cijk_type cijk_type
Typename of cijk.
ValueSerializer value_serializer_type
Typename of value serializer.
static PCEType squareroot(const PCEType &x)
static PCEType log(const PCEType &x)
static innerProductType innerProduct(const PCEType &a, const PCEType &b)
static T * convertFromCharPtr(char *ptr)
Sacado::ValueType< PCEType >::type ValueT
Teuchos::RCP< const value_serializer_type > getValueSerializer() const
Get nested value serializer.
void serialize(const Ordinal count, const PCEType buffer[], const Ordinal bytes, char charBuffer[]) const
Serialize to an indirect char[] buffer.
Sacado::mpl::apply< storage_type, ordinal_type, value_mag_type >::type storage_mag_type
static void seedrandom(unsigned int s)
Teuchos::ScalarTraits< value_type > TVT
static magnitudeType imag(value_typea)
static bool isnaninf(const value_type &x)
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)
static const bool supportsDirectSerialization
Whether the type T supports direct serialization.
Teuchos::SerializationTraits< Ordinal, Ordinal > oSerT
static const bool supportsDirectSerialization
Whether the type T supports direct serialization.
Sacado::UQ::PCE< storage_type > PCEType
Teuchos::SerializationTraits< Ordinal, ValueT > vSerT
static PCEType pow(const PCEType &x, const PCEType &y)
Teuchos::SerializationTraits< Ordinal, int > iSerT
static Ordinal fromCountToIndirectBytes(const Ordinal count, const PCEType buffer[])
Return the number of bytes for count objects.
Implementation of Teuchos::SerializationTraits for all PCE types.
void deserialize(const Ordinal bytes, const char charBuffer[], const Ordinal count, PCEType buffer[]) const
Deserialize from an indirect char[] buffer.
Ordinal fromCountToIndirectBytes(const Ordinal count, const PCEType buffer[]) const
Return the number of bytes for count objects.
KOKKOS_INLINE_FUNCTION PCE< Storage > log(const PCE< Storage > &a)
Sacado::mpl::apply< PCEType, storage_double_type >::type doublePrecision
KOKKOS_INLINE_FUNCTION PCE< Storage > log10(const PCE< Storage > &a)
static void deserialize(const Ordinal bytes, const char charBuffer[], const Ordinal count, PCEType buffer[])
Deserialize from an indirect char[] buffer.
static void serialize(const Ordinal count, const T buffer[], const Ordinal bytes, char charBuffer[])
static Ordinal fromCountToDirectBytes(const Ordinal count)
Teuchos::RCP< const ValueSerializer > vs
static magnitudeType magnitude(const PCEType &a)