| 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" |
| 14 |
#include "Teuchos_SerializationTraits.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 |
|
| 26 |
//! Implementation for Teuchos::ScalarTraits for all PCE types |
| 27 |
template <typename PCEType> |
| 28 |
struct PCEScalarTraitsImp { |
| 29 |
typedef typename PCEType::storage_type storage_type; |
| 30 |
typedef typename storage_type::value_type value_type; |
| 31 |
typedef typename storage_type::ordinal_type ordinal_type; |
| 32 |
typedef Teuchos::ScalarTraits<value_type> TVT; |
| 33 |
|
| 34 |
typedef typename TVT::magnitudeType value_mag_type; |
| 35 |
typedef typename TVT::halfPrecision value_half_type; |
| 36 |
typedef typename TVT::doublePrecision value_double_type; |
| 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 |
|
| 42 |
typedef value_mag_type magnitudeType; |
| 43 |
typedef typename Sacado::mpl::apply<PCEType, storage_half_type>::type halfPrecision; |
| 44 |
typedef typename Sacado::mpl::apply<PCEType, storage_double_type>::type doublePrecision; |
| 45 |
typedef typename TVT::coordinateType coordinateType; |
| 46 |
|
| 47 |
typedef value_type innerProductType; |
| 48 |
|
| 49 |
static const bool isComplex = TVT::isComplex; |
| 50 |
static const bool isOrdinal = TVT::isOrdinal; |
| 51 |
static const bool isComparable = TVT::isComparable; |
| 52 |
static const bool hasMachineParameters = TVT::hasMachineParameters; |
| 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) { |
| 94 |
magnitudeType m = magnitudeType(0.0); |
| 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 |
|
| 141 |
//! Implementation for Teuchos::ValueTypeConversionTraits for all PCE types |
| 142 |
template <typename TypeTo, typename PCEType> |
| 143 |
struct PCEValueTypeConversionTraitsImp { |
| 144 |
typedef typename Sacado::ValueType<PCEType>::type ValueT; |
| 145 |
typedef Teuchos::ValueTypeConversionTraits<TypeTo,ValueT> VTCT; |
| 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 |
|
| 154 |
//! Implementation of Teuchos::SerializationTraits for all PCE types |
| 155 |
template <typename Ordinal, typename PCEType> |
| 156 |
class PCESerializationTraitsImp { |
| 157 |
typedef typename Sacado::ValueType<PCEType>::type ValueT; |
| 158 |
typedef Teuchos::SerializationTraits<Ordinal,ValueT> vSerT; |
| 159 |
typedef Teuchos::SerializationTraits<Ordinal,int> iSerT; |
| 160 |
typedef Teuchos::SerializationTraits<Ordinal,Ordinal> oSerT; |
| 161 |
|
| 162 |
public: |
| 163 |
|
| 164 |
/// \brief Whether the type T supports direct serialization. |
| 165 |
static const bool supportsDirectSerialization = false; |
| 166 |
|
| 167 |
//! @name Indirect serialization functions (always defined and supported) |
| 168 |
//@{ |
| 169 |
|
| 170 |
/** \brief Return the number of bytes for <tt>count</tt> objects. */ |
| 171 |
static Ordinal fromCountToIndirectBytes(const Ordinal count, |
| 172 |
const PCEType buffer[]) { |
| 173 |
Ordinal bytes = 0; |
| 174 |
for (Ordinal i=0; i<count; i++) { |
| 175 |
int sz = buffer[i].size(); |
| 176 |
Ordinal b1 = iSerT::fromCountToIndirectBytes(1, &sz); |
| 177 |
Ordinal b2 = vSerT::fromCountToIndirectBytes(sz, buffer[i].coeff()); |
| 178 |
Ordinal b3 = oSerT::fromCountToIndirectBytes(1, &b2); |
| 179 |
bytes += b1+b2+b3; |
| 180 |
} |
| 181 |
return bytes; |
| 182 |
} |
| 183 |
|
| 184 |
/** \brief Serialize to an indirect <tt>char[]</tt> buffer. */ |
| 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(); |
| 192 |
Ordinal b1 = iSerT::fromCountToIndirectBytes(1, &sz); |
| 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()); |
| 198 |
Ordinal b3 = oSerT::fromCountToIndirectBytes(1, &b2); |
| 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 |
|
| 206 |
/** \brief Return the number of objects for <tt>bytes</tt> of storage. */ |
| 207 |
static Ordinal fromIndirectBytesToCount(const Ordinal bytes, |
| 208 |
const char charBuffer[]) { |
| 209 |
Ordinal count = 0; |
| 210 |
Ordinal bytes_used = 0; |
| 211 |
while (bytes_used < bytes) { |
| 212 |
|
| 213 |
// Bytes for size |
| 214 |
Ordinal b1 = iSerT::fromCountToDirectBytes(1); |
| 215 |
bytes_used += b1; |
| 216 |
charBuffer += b1; |
| 217 |
|
| 218 |
// Bytes for PCE coefficients |
| 219 |
Ordinal b3 = oSerT::fromCountToDirectBytes(1); |
| 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 |
|
| 231 |
/** \brief Deserialize from an indirect <tt>char[]</tt> buffer. */ |
| 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 |
| 239 |
Ordinal b1 = iSerT::fromCountToDirectBytes(1); |
| 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 |
| 251 |
Ordinal b3 = oSerT::fromCountToDirectBytes(1); |
| 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 |
|
| 260 |
//@} |
| 261 |
|
| 262 |
}; |
| 263 |
|
| 264 |
|
| 265 |
//! Serializer object for all PCE types |
| 266 |
template <typename Ordinal, typename PCEType, typename ValueSerializer> |
| 267 |
class PCESerializerImp { |
| 268 |
|
| 269 |
public: |
| 270 |
|
| 271 |
//! Typename of value serializer |
| 272 |
typedef ValueSerializer value_serializer_type; |
| 273 |
|
| 274 |
//! Typename of cijk |
| 275 |
typedef typename PCEType::cijk_type cijk_type; |
| 276 |
|
| 277 |
|
| 278 |
protected: |
| 279 |
typedef typename Sacado::ValueType<PCEType>::type ValueT; |
| 280 |
typedef Teuchos::SerializationTraits<Ordinal,int> iSerT; |
| 281 |
typedef Teuchos::SerializationTraits<Ordinal,Ordinal> oSerT; |
| 282 |
|
| 283 |
cijk_type cijk; |
| 284 |
Teuchos::RCP<const ValueSerializer> vs; |
| 285 |
int sz; |
| 286 |
|
| 287 |
public: |
| 288 |
|
| 289 |
/// \brief Whether the type T supports direct serialization. |
| 290 |
static const bool supportsDirectSerialization = false; |
| 291 |
|
| 292 |
PCESerializerImp(const cijk_type& cijk_, |
| 293 |
const Teuchos::RCP<const ValueSerializer>& vs_) : |
| 294 |
cijk(cijk_), vs(vs_), sz(cijk.dimension()) {} |
| 295 |
|
| 296 |
//! Return specified serializer size |
| 297 |
cijk_type getSerializerCijk() const { return cijk; } |
| 298 |
|
| 299 |
//! Get nested value serializer |
| 300 |
Teuchos::RCP<const value_serializer_type> getValueSerializer() const { |
| 301 |
return vs; } |
| 302 |
|
| 303 |
//! @name Indirect serialization functions (always defined and supported) |
| 304 |
//@{ |
| 305 |
|
| 306 |
/** \brief Return the number of bytes for <tt>count</tt> objects. */ |
| 307 |
Ordinal fromCountToIndirectBytes(const Ordinal count, |
| 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]); |
| 323 |
Ordinal b1 = iSerT::fromCountToIndirectBytes(1, &sz); |
| 324 |
Ordinal b2 = vs->fromCountToIndirectBytes(sz, cx->coeff()); |
| 325 |
Ordinal b3 = oSerT::fromCountToIndirectBytes(1, &b2); |
| 326 |
bytes += b1+b2+b3; |
| 327 |
} |
| 328 |
if (x != NULL) |
| 329 |
delete x; |
| 330 |
return bytes; |
| 331 |
} |
| 332 |
|
| 333 |
/** \brief Serialize to an indirect <tt>char[]</tt> buffer. */ |
| 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]); |
| 352 |
Ordinal b1 = iSerT::fromCountToIndirectBytes(1, &sz); |
| 353 |
iSerT::serialize(1, &sz, b1, charBuffer); |
| 354 |
charBuffer += b1; |
| 355 |
|
| 356 |
// Next serialize PCE coefficients |
| 357 |
Ordinal b2 = vs->fromCountToIndirectBytes(sz, cx->coeff()); |
| 358 |
Ordinal b3 = oSerT::fromCountToIndirectBytes(1, &b2); |
| 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 |
|
| 368 |
/** \brief Return the number of objects for <tt>bytes</tt> of storage. */ |
| 369 |
Ordinal fromIndirectBytesToCount(const Ordinal bytes, |
| 370 |
const char charBuffer[]) const { |
| 371 |
Ordinal count = 0; |
| 372 |
Ordinal bytes_used = 0; |
| 373 |
while (bytes_used < bytes) { |
| 374 |
|
| 375 |
// Bytes for size |
| 376 |
Ordinal b1 = iSerT::fromCountToDirectBytes(1); |
| 377 |
bytes_used += b1; |
| 378 |
charBuffer += b1; |
| 379 |
|
| 380 |
// Bytes for PCE coefficients |
| 381 |
Ordinal b3 = oSerT::fromCountToDirectBytes(1); |
| 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 |
|
| 393 |
/** \brief Deserialize from an indirect <tt>char[]</tt> buffer. */ |
| 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 |
| 401 |
Ordinal b1 = iSerT::fromCountToDirectBytes(1); |
| 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 |
| 409 |
Ordinal b3 = oSerT::fromCountToDirectBytes(1); |
| 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 |
|
| 418 |
//@} |
| 419 |
|
| 420 |
}; |
| 421 |
|
| 422 |
} // namespace UQ |
| 423 |
|
| 424 |
} // namespace Sacado |
| 425 |
|
| 426 |
#endif // SACADO_FAD_SCALARTRAITSIMP_HPP |