Debian Codegraph @

packages/stokhos/src/sacado/kokkos/vector/Sacado_MP_ScalarTraitsImp.hpp

raw
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_MP_SCALAR_TRAITS_IMP_HPP
11 #define SACADO_MP_SCALAR_TRAITS_IMP_HPP
12
13 #include "Teuchos_ScalarTraits.hpp"
14 #include "Teuchos_SerializationTraits.hpp"
15 #include "Teuchos_SerializationTraitsHelpers.hpp"
16 #include "Teuchos_Assert.hpp"
17 #include "Teuchos_TestForException.hpp"
18 #include "Sacado_mpl_apply.hpp"
19
20 namespace Sacado {
21 namespace MP {
22
23 template <typename S, bool reduct_across_vector>
24 struct ScalarTraitsImp {};
25
26 // Implementation of Teuchos::ScalarTraits where reductions are taken
27 // across the components of MP::Vector. In this case magnitudeType is
28 // a scalar
29 template <typename S>
30 struct ScalarTraitsImp<S,true> {
31 typedef Sacado::MP::Vector<S> ScalarType;
32 typedef typename S::value_type value_type;
33 typedef typename S::ordinal_type ordinal_type;
34 typedef Teuchos::ScalarTraits<value_type> TVT;
35
36 typedef typename TVT::magnitudeType value_mag_type;
37 typedef typename TVT::halfPrecision value_half_type;
38 typedef typename TVT::doublePrecision value_double_type;
39
40 typedef typename Sacado::mpl::apply<S,ordinal_type,value_mag_type>::type storage_mag_type;
41 typedef typename Sacado::mpl::apply<S,ordinal_type,value_half_type>::type storage_half_type;
42 typedef typename Sacado::mpl::apply<S,ordinal_type,value_double_type>::type storage_double_type;
43
44 typedef value_mag_type magnitudeType;
45 typedef Sacado::MP::Vector<storage_half_type> halfPrecision;
46 typedef Sacado::MP::Vector<storage_double_type> doublePrecision;
47 typedef typename Teuchos::ScalarTraits<value_type>::coordinateType coordinateType;
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 ScalarType& a) {
75 magnitudeType m = magnitudeType(0.0);
76 const ordinal_type sz = a.size();
77 for (ordinal_type i=0; i<sz; ++i) {
78 value_mag_type t = TVT::magnitude(a.fastAccessCoeff(i));
79 m +=t*t;
80 }
81 return std::sqrt(m);
82 }
83
84 static ScalarType zero() { return ScalarType(0.0); }
85
86 static ScalarType one() { return ScalarType(1.0); }
87
88
89 static ScalarType conjugate(const ScalarType& x) {
90 int sz = x.size();
91 ScalarType y(sz, value_type(0.0));
92 for (int i=0; i<sz; i++)
93 y.fastAccessCoeff(i) = TVT::conjugate(x.fastAccessCoeff(i));
94 return y;
95 }
96
97
98 static magnitudeType real(const ScalarType& x) {
99 magnitudeType m = magnitudeType(0.0);
100 const ordinal_type sz = x.size();
101 for (ordinal_type i=0; i<sz; ++i) {
102 value_mag_type t = TVT::real(x.fastAccessCoeff(i));
103 m +=t*t;
104 }
105 return std::sqrt(m);
106 }
107
108
109 static magnitudeType imag(const ScalarType& x) {
110 magnitudeType m = magnitudeType(0.0);
111 const ordinal_type sz = x.size();
112 for (ordinal_type i=0; i<sz; ++i) {
113 value_mag_type t = TVT::imag(x.fastAccessCoeff(i));
114 m +=t*t;
115 }
116 return std::sqrt(m);
117 }
118
119 static value_type nan() { return TVT::nan(); }
120
121 static bool isnaninf(const ScalarType& x) {
122 for (int i=0; i<x.size(); i++)
123 if (TVT::isnaninf(x.fastAccessCoeff(i)))
124 return true;
125 return false;
126 }
127
128 static void seedrandom(unsigned int s) { TVT::seedrandom(s); }
129
130 static ScalarType random() { return ScalarType(TVT::random()); }
131
132 static const char * name() { return "Sacado::MP::Vector<>"; }
133
134 static ScalarType squareroot(const ScalarType& x) { return std::sqrt(x); }
135
136 static ScalarType pow(const ScalarType& x, const ScalarType& y) {
137 return std::pow(x,y);
138 }
139
140 static ScalarType log(const ScalarType& x) { return std::log(x); }
141
142 static ScalarType log10(const ScalarType& x) { return std::log10(x); }
143
144 }; // class ScalarTraitsImp<S,true>
145
146 // Implementation of Teuchos::ScalarTraits where reductions are not taken
147 // across the components of MP::Vector. In this case magnitudeType is
148 // an MP::Vector
149 template <typename S>
150 struct ScalarTraitsImp<S,false> {
151 typedef Sacado::MP::Vector<S> ScalarType;
152 typedef typename S::value_type value_type;
153 typedef typename S::ordinal_type ordinal_type;
154 typedef Teuchos::ScalarTraits<value_type> TVT;
155
156 typedef typename TVT::magnitudeType value_mag_type;
157 typedef typename TVT::halfPrecision value_half_type;
158 typedef typename TVT::doublePrecision value_double_type;
159
160 typedef typename Sacado::mpl::apply<S,ordinal_type,value_mag_type>::type storage_mag_type;
161 typedef typename Sacado::mpl::apply<S,ordinal_type,value_half_type>::type storage_half_type;
162 typedef typename Sacado::mpl::apply<S,ordinal_type,value_double_type>::type storage_double_type;
163
164 typedef Sacado::MP::Vector<storage_mag_type> magnitudeType;
165 typedef Sacado::MP::Vector<storage_half_type> halfPrecision;
166 typedef Sacado::MP::Vector<storage_double_type> doublePrecision;
167 typedef typename Teuchos::ScalarTraits<value_type>::coordinateType coordinateType;
168
169 static const bool isComplex = TVT::isComplex;
170 static const bool isOrdinal = TVT::isOrdinal;
171 static const bool isComparable = TVT::isComparable;
172 static const bool hasMachineParameters = TVT::hasMachineParameters;
173
174 static value_mag_type eps() { return TVT::eps(); }
175
176 static value_mag_type sfmin() { return TVT::sfmin(); }
177
178 static value_mag_type base() { return TVT::base(); }
179
180 static value_mag_type prec() { return TVT::prec(); }
181
182 static value_mag_type t() { return TVT::t(); }
183
184 static value_mag_type rnd() { return TVT::rnd(); }
185
186 static value_mag_type emin() { return TVT::emin(); }
187
188 static value_mag_type rmin() { return TVT::rmin(); }
189
190 static value_mag_type emax() { return TVT::emax(); }
191
192 static value_mag_type rmax() { return TVT::rmax(); }
193
194 static magnitudeType magnitude(const ScalarType& a) {
195 return std::fabs(a);
196 }
197
198 static ScalarType zero() { return ScalarType(0.0); }
199
200 static ScalarType one() { return ScalarType(1.0); }
201
202
203 static ScalarType conjugate(const ScalarType& x) {
204 int sz = x.size();
205 ScalarType y(sz, value_type(0.0));
206 for (int i=0; i<sz; i++)
207 y.fastAccessCoeff(i) = TVT::conjugate(x.fastAccessCoeff(i));
208 return y;
209 }
210
211 static ScalarType real(const ScalarType& x) {
212 int sz = x.size();
213 ScalarType y(sz, value_type(0.0));
214 for (int i=0; i<sz; i++)
215 y.fastAccessCoeff(i) = TVT::real(x.fastAccessCoeff(i));
216 return y;
217 }
218
219 static ScalarType imag(const ScalarType& x) {
220 int sz = x.size();
221 ScalarType y(sz, value_type(0.0));
222 for (int i=0; i<sz; i++)
223 y.fastAccessCoeff(i) = TVT::imag(x.fastAccessCoeff(i));
224 return y;
225 }
226
227 static value_type nan() { return TVT::nan(); }
228
229 static bool isnaninf(const ScalarType& x) {
230 for (int i=0; i<x.size(); i++)
231 if (TVT::isnaninf(x.fastAccessCoeff(i)))
232 return true;
233 return false;
234 }
235
236 static void seedrandom(unsigned int s) { TVT::seedrandom(s); }
237
238 static ScalarType random() { return ScalarType(TVT::random()); }
239
240 static const char * name() { return "Sacado::MP::Vector<>"; }
241
242 static ScalarType squareroot(const ScalarType& x) { return std::sqrt(x); }
243
244 static ScalarType pow(const ScalarType& x, const ScalarType& y) {
245 return std::pow(x,y);
246 }
247
248 static ScalarType log(const ScalarType& x) { return std::log(x); }
249
250 static ScalarType log10(const ScalarType& x) { return std::log10(x); }
251
252 }; // class ScalarTraitsImp<S,false>
253
254 //! Serialization implementation for all Vector types
255 template <typename Ordinal, typename VecType, typename Serializer>
256 struct SerializationImp {
257
258 private:
259
260 //! Value type
261 typedef typename Sacado::ValueType<VecType>::type ValueT;
262
263 //! How to serialize ints
264 typedef Teuchos::SerializationTraits<Ordinal,int> iSerT;
265
266 //! How to serialize ordinals
267 typedef Teuchos::SerializationTraits<Ordinal,Ordinal> oSerT;
268
269 public:
270
271 /// \brief Whether the type T supports direct serialization.
272 static const bool supportsDirectSerialization = false;
273
274 //! @name Indirect serialization functions (always defined and supported)
275 //@{
276
277 /** \brief Return the number of bytes for <tt>count</tt> objects. */
278 static Ordinal fromCountToIndirectBytes(const Serializer& vs,
279 const Ordinal count,
280 const VecType buffer[],
281 const Ordinal sz = 0) {
282 Ordinal bytes = 0;
283 VecType *x = NULL;
284 const VecType *cx;
285 for (Ordinal i=0; i<count; i++) {
286 int my_sz = buffer[i].size();
287 int tot_sz = sz;
288 if (sz == 0) tot_sz = my_sz;
289 Ordinal b1 = iSerT::fromCountToIndirectBytes(1, &tot_sz);
290 if (tot_sz != my_sz) {
291 if (x == NULL)
292 x = new VecType;
293 *x = buffer[i];
294 x->reset(tot_sz);
295 cx = x;
296 }
297 else
298 cx = &(buffer[i]);
299 Ordinal b2 = vs.fromCountToIndirectBytes(tot_sz, cx->coeff());
300 Ordinal b3 = oSerT::fromCountToIndirectBytes(1, &b2);
301 bytes += b1+b2+b3;
302 }
303 if (x != NULL)
304 delete x;
305 return bytes;
306 }
307
308 /** \brief Serialize to an indirect <tt>char[]</tt> buffer. */
309 static void serialize (const Serializer& vs,
310 const Ordinal count,
311 const VecType buffer[],
312 const Ordinal bytes,
313 char charBuffer[],
314 const Ordinal sz = 0) {
315 VecType *x = NULL;
316 const VecType *cx;
317 for (Ordinal i=0; i<count; i++) {
318 // First serialize size
319 int my_sz = buffer[i].size();
320 int tot_sz = sz;
321 if (sz == 0) tot_sz = my_sz;
322 Ordinal b1 = iSerT::fromCountToIndirectBytes(1, &tot_sz);
323 iSerT::serialize(1, &tot_sz, b1, charBuffer);
324 charBuffer += b1;
325
326 // Next serialize vector coefficients
327 if (tot_sz != my_sz) {
328 if (x == NULL)
329 x = new VecType;
330 *x = buffer[i];
331 x->reset(tot_sz);
332 cx = x;
333 }
334 else
335 cx = &(buffer[i]);
336 Ordinal b2 = vs.fromCountToIndirectBytes(tot_sz, cx->coeff());
337 Ordinal b3 = oSerT::fromCountToIndirectBytes(1, &b2);
338 oSerT::serialize(1, &b2, b3, charBuffer);
339 charBuffer += b3;
340 vs.serialize(tot_sz, cx->coeff(), b2, charBuffer);
341 charBuffer += b2;
342 }
343 if (x != NULL)
344 delete x;
345 }
346
347 /** \brief Return the number of objects for <tt>bytes</tt> of storage. */
348 static Ordinal fromIndirectBytesToCount(const Serializer& vs,
349 const Ordinal bytes,
350 const char charBuffer[],
351 const Ordinal sz = 0) {
352 Ordinal count = 0;
353 Ordinal bytes_used = 0;
354 while (bytes_used < bytes) {
355
356 // Bytes for size
357 Ordinal b1 = iSerT::fromCountToDirectBytes(1);
358 bytes_used += b1;
359 charBuffer += b1;
360
361 // Bytes for vector coefficients
362 Ordinal b3 = oSerT::fromCountToDirectBytes(1);
363 const Ordinal *b2 = oSerT::convertFromCharPtr(charBuffer);
364 bytes_used += b3;
365 charBuffer += b3;
366 bytes_used += *b2;
367 charBuffer += *b2;
368
369 ++count;
370 }
371 return count;
372 }
373
374 /** \brief Deserialize from an indirect <tt>char[]</tt> buffer. */
375 static void deserialize (const Serializer& vs,
376 const Ordinal bytes,
377 const char charBuffer[],
378 const Ordinal count,
379 VecType buffer[],
380 const Ordinal sz = 0) {
381 for (Ordinal i=0; i<count; i++) {
382
383 // Deserialize size
384 Ordinal b1 = iSerT::fromCountToDirectBytes(1);
385 const int *my_sz = iSerT::convertFromCharPtr(charBuffer);
386 charBuffer += b1;
387
388 // Create empty Vector object of given size
389 int tot_sz = sz;
390 if (sz == 0) tot_sz = *my_sz;
391 buffer[i] = VecType(tot_sz, ValueT(0.0));
392
393 // Deserialize vector coefficients
394 Ordinal b3 = oSerT::fromCountToDirectBytes(1);
395 const Ordinal *b2 = oSerT::convertFromCharPtr(charBuffer);
396 charBuffer += b3;
397 vs.deserialize(*b2, charBuffer, *my_sz, buffer[i].coeff());
398 charBuffer += *b2;
399 }
400
401 }
402 //@}
403
404 };
405
406 //! Implementation of Teuchos::SerializationTraits for all Vector types
407 template <typename Ordinal, typename VecType, bool is_static = false>
408 struct SerializationTraitsImp {
409
410 private:
411
412 //! Value type of Vec type
413 typedef typename Sacado::ValueType<VecType>::type ValueT;
414
415 //! Default serializer for values
416 typedef Teuchos::DefaultSerializer<Ordinal,ValueT> DS;
417
418 //! Default serializer type for values
419 typedef typename DS::DefaultSerializerType ValueSerializer;
420
421 //! Implementation
422 typedef SerializationImp<Ordinal,VecType,ValueSerializer> Imp;
423
424 public:
425
426 /// \brief Whether the type T supports direct serialization.
427 static const bool supportsDirectSerialization =
428 Imp::supportsDirectSerialization;
429
430 //! @name Indirect serialization functions (always defined and supported)
431 //@{
432
433 /** \brief Return the number of bytes for <tt>count</tt> objects. */
434 static Ordinal fromCountToIndirectBytes(const Ordinal count,
435 const VecType buffer[]) {
436 return Imp::fromCountToIndirectBytes(
437 DS::getDefaultSerializer(), count, buffer);
438 }
439
440 /** \brief Serialize to an indirect <tt>char[]</tt> buffer. */
441 static void serialize (const Ordinal count,
442 const VecType buffer[],
443 const Ordinal bytes,
444 char charBuffer[]) {
445 Imp::serialize(
446 DS::getDefaultSerializer(), count, buffer, bytes, charBuffer);
447 }
448
449 /** \brief Return the number of objects for <tt>bytes</tt> of storage. */
450 static Ordinal fromIndirectBytesToCount(const Ordinal bytes,
451 const char charBuffer[]) {
452 return Imp::fromIndirectBytesToCount(
453 DS::getDefaultSerializer(), bytes, charBuffer);
454 }
455
456 /** \brief Deserialize from an indirect <tt>char[]</tt> buffer. */
457 static void deserialize (const Ordinal bytes,
458 const char charBuffer[],
459 const Ordinal count,
460 VecType buffer[]) {
461 Imp::deserialize(
462 DS::getDefaultSerializer(), bytes, charBuffer, count, buffer);
463 }
464
465 //@}
466
467 };
468
469 //! Implementation of Teuchos::SerializationTraits for all static Vec types
470 template <typename Ordinal, typename VecType>
471 struct SerializationTraitsImp<Ordinal, VecType, true> {
472 typedef typename Sacado::ValueType<VecType>::type ValueT;
473 typedef Teuchos::SerializationTraits<Ordinal,ValueT> vSerT;
474 typedef Teuchos::DirectSerializationTraits<Ordinal,VecType> DSerT;
475 typedef Sacado::MP::SerializationTraitsImp<Ordinal,VecType> STI;
476
477 /// \brief Whether the type T supports direct serialization.
478 static const bool supportsDirectSerialization =
479 vSerT::supportsDirectSerialization;
480
481 //! @name Direct serialization functions (not defined if supportsDirectSerialization==false)
482 //@{
483
484 /** \brief Return the number of bytes for <tt>count</tt> objects. */
485 static Ordinal fromCountToDirectBytes(const Ordinal count) {
486 return DSerT::fromCountToDirectBytes(count);
487 }
488
489 /** \brief Convert the pointer type to <tt>char*</tt>. */
490 static char* convertToCharPtr( VecType* ptr ) {
491 return DSerT::convertToCharPtr(ptr);
492 }
493
494 /** \brief Convert the pointer type to <tt>const char*</tt>. */
495 static const char* convertToCharPtr( const VecType* ptr ) {
496 return DSerT::convertToCharPtr(ptr);
497 }
498
499 /** \brief Return the number of objects for <tt>bytes</tt> of storage. */
500 static Ordinal fromDirectBytesToCount(const Ordinal bytes) {
501 return DSerT::fromDirectBytesToCount(bytes);
502 }
503
504 /** \brief Convert the pointer type from <tt>char*</tt>. */
505 static VecType* convertFromCharPtr( char* ptr ) {
506 return DSerT::convertFromCharPtr(ptr);
507 }
508
509 /** \brief Convert the pointer type from <tt>char*</tt>. */
510 static const VecType* convertFromCharPtr( const char* ptr ) {
511 return DSerT::convertFromCharPtr(ptr);
512 }
513
514 //@}
515
516 //! @name Indirect serialization functions (always defined and supported)
517 //@{
518
519 /** \brief Return the number of bytes for <tt>count</tt> objects. */
520 static Ordinal fromCountToIndirectBytes(const Ordinal count,
521 const VecType buffer[]) {
522 if (supportsDirectSerialization)
523 return DSerT::fromCountToIndirectBytes(count, buffer);
524 else
525 return STI::fromCountToIndirectBytes(count, buffer);
526 }
527
528 /** \brief Serialize to an indirect <tt>char[]</tt> buffer. */
529 static void serialize (const Ordinal count,
530 const VecType buffer[],
531 const Ordinal bytes,
532 char charBuffer[]) {
533 if (supportsDirectSerialization)
534 return DSerT::serialize(count, buffer, bytes, charBuffer);
535 else
536 return STI::serialize(count, buffer, bytes, charBuffer);
537 }
538
539 /** \brief Return the number of objects for <tt>bytes</tt> of storage. */
540 static Ordinal fromIndirectBytesToCount(const Ordinal bytes,
541 const char charBuffer[]) {
542 if (supportsDirectSerialization)
543 return DSerT::fromIndirectBytesToCount(bytes, charBuffer);
544 else
545 return STI::fromIndirectBytesToCount(bytes, charBuffer);
546 }
547
548 /** \brief Deserialize from an indirect <tt>char[]</tt> buffer. */
549 static void deserialize (const Ordinal bytes,
550 const char charBuffer[],
551 const Ordinal count,
552 VecType buffer[]) {
553 if (supportsDirectSerialization)
554 return DSerT::deserialize(bytes, charBuffer, count, buffer);
555 else
556 return STI::deserialize(bytes, charBuffer, count, buffer);
557 }
558
559 //@}
560
561 };
562
563 //! An indirect serialization object for all Vector types
564 template <typename Ordinal, typename VecType, typename ValueSerializer>
565 class SerializerImp {
566
567 private:
568
569 //! Implementation
570 typedef SerializationImp<Ordinal,VecType,ValueSerializer> Imp;
571
572 //! Serializer for value types
573 Teuchos::RCP<const ValueSerializer> vs;
574
575 //! Specified number of derivative components;
576 Ordinal sz;
577
578 public:
579
580 //! Typename of value serializer
581 typedef ValueSerializer value_serializer_type;
582
583 /// \brief Whether we support direct serialization.
584 static const bool supportsDirectSerialization =
585 Imp::supportsDirectSerialization;
586
587 //! Constructor
588 SerializerImp(const Teuchos::RCP<const ValueSerializer>& vs_,
589 Ordinal sz_ = 0) :
590 vs(vs_), sz(sz_) {}
591
592 //! Return specified serializer size
593 Ordinal getSerializerSize() const { return sz; }
594
595 //! Get nested value serializer
596 Teuchos::RCP<const value_serializer_type> getValueSerializer() const {
597 return vs; }
598
599 //! @name Indirect serialization functions (always defined and supported)
600 //@{
601
602 /** \brief Return the number of bytes for <tt>count</tt> objects. */
603 Ordinal fromCountToIndirectBytes(const Ordinal count,
604 const VecType buffer[]) const {
605 return Imp::fromCountToIndirectBytes(*vs, count, buffer, sz);
606 }
607
608 /** \brief Serialize to an indirect <tt>char[]</tt> buffer. */
609 void serialize (const Ordinal count,
610 const VecType buffer[],
611 const Ordinal bytes,
612 char charBuffer[]) const {
613 Imp::serialize(*vs, count, buffer, bytes, charBuffer, sz);
614 }
615
616 /** \brief Return the number of objects for <tt>bytes</tt> of storage. */
617 Ordinal fromIndirectBytesToCount(const Ordinal bytes,
618 const char charBuffer[]) const {
619 return Imp::fromIndirectBytesToCount(*vs, bytes, charBuffer, sz);
620 }
621
622 /** \brief Deserialize from an indirect <tt>char[]</tt> buffer. */
623 void deserialize (const Ordinal bytes,
624 const char charBuffer[],
625 const Ordinal count,
626 VecType buffer[]) const {
627 return Imp::deserialize(*vs, bytes, charBuffer, count, buffer, sz);
628 }
629
630 //@}
631
632 };
633
634 }
635
636 }
637
638 #endif // SACADO_MP_SCALAR_TRAITS_IMP_HPP