Stokhos Package Browser (Single Doxygen Collection)  Version of the Day
Sacado_MP_ScalarTraitsImp.hpp
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 
42 #ifndef SACADO_MP_SCALAR_TRAITS_IMP_HPP
43 #define SACADO_MP_SCALAR_TRAITS_IMP_HPP
44 
45 #ifdef HAVE_SACADO_TEUCHOS
46 
47 #include "Teuchos_ScalarTraits.hpp"
48 #include "Teuchos_SerializationTraits.hpp"
49 #include "Teuchos_SerializationTraitsHelpers.hpp"
50 #include "Teuchos_Assert.hpp"
51 #include "Teuchos_TestForException.hpp"
52 #include "Sacado_mpl_apply.hpp"
53 
54 namespace Sacado {
55  namespace MP {
56 
57  template <typename S, bool reduct_across_vector>
58  struct ScalarTraitsImp {};
59 
60  // Implementation of Teuchos::ScalarTraits where reductions are taken
61  // across the components of MP::Vector. In this case magnitudeType is
62  // a scalar
63  template <typename S>
64  struct ScalarTraitsImp<S,true> {
65  typedef Sacado::MP::Vector<S> ScalarType;
66  typedef typename S::value_type value_type;
67  typedef typename S::ordinal_type ordinal_type;
68  typedef Teuchos::ScalarTraits<value_type> TVT;
69 
70  typedef typename TVT::magnitudeType value_mag_type;
71  typedef typename TVT::halfPrecision value_half_type;
72  typedef typename TVT::doublePrecision value_double_type;
73 
74  typedef typename Sacado::mpl::apply<S,ordinal_type,value_mag_type>::type storage_mag_type;
75  typedef typename Sacado::mpl::apply<S,ordinal_type,value_half_type>::type storage_half_type;
76  typedef typename Sacado::mpl::apply<S,ordinal_type,value_double_type>::type storage_double_type;
77 
78  typedef value_mag_type magnitudeType;
79  typedef Sacado::MP::Vector<storage_half_type> halfPrecision;
80  typedef Sacado::MP::Vector<storage_double_type> doublePrecision;
81 
82  static const bool isComplex = TVT::isComplex;
83  static const bool isOrdinal = TVT::isOrdinal;
84  static const bool isComparable = TVT::isComparable;
85  static const bool hasMachineParameters = TVT::hasMachineParameters;
86 
87  static value_mag_type eps() { return TVT::eps(); }
88 
89  static value_mag_type sfmin() { return TVT::sfmin(); }
90 
91  static value_mag_type base() { return TVT::base(); }
92 
93  static value_mag_type prec() { return TVT::prec(); }
94 
95  static value_mag_type t() { return TVT::t(); }
96 
97  static value_mag_type rnd() { return TVT::rnd(); }
98 
99  static value_mag_type emin() { return TVT::emin(); }
100 
101  static value_mag_type rmin() { return TVT::rmin(); }
102 
103  static value_mag_type emax() { return TVT::emax(); }
104 
105  static value_mag_type rmax() { return TVT::rmax(); }
106 
107  static magnitudeType magnitude(const ScalarType& a) {
108  magnitudeType m = magnitudeType(0.0);
109  const ordinal_type sz = a.size();
110  for (ordinal_type i=0; i<sz; ++i) {
111  value_mag_type t = TVT::magnitude(a.fastAccessCoeff(i));
112  m +=t*t;
113  }
114  return std::sqrt(m);
115  }
116 
117  static ScalarType zero() { return ScalarType(0.0); }
118 
119  static ScalarType one() { return ScalarType(1.0); }
120 
121 
122  static ScalarType conjugate(const ScalarType& x) {
123  int sz = x.size();
124  ScalarType y(sz, value_type(0.0));
125  for (int i=0; i<sz; i++)
126  y.fastAccessCoeff(i) = TVT::conjugate(x.fastAccessCoeff(i));
127  return y;
128  }
129 
130 
131  static magnitudeType real(const ScalarType& x) {
132  magnitudeType m = magnitudeType(0.0);
133  const ordinal_type sz = x.size();
134  for (ordinal_type i=0; i<sz; ++i) {
135  value_mag_type t = TVT::real(x.fastAccessCoeff(i));
136  m +=t*t;
137  }
138  return std::sqrt(m);
139  }
140 
141 
142  static magnitudeType imag(const ScalarType& x) {
143  magnitudeType m = magnitudeType(0.0);
144  const ordinal_type sz = x.size();
145  for (ordinal_type i=0; i<sz; ++i) {
146  value_mag_type t = TVT::imag(x.fastAccessCoeff(i));
147  m +=t*t;
148  }
149  return std::sqrt(m);
150  }
151 
152  static value_type nan() { return TVT::nan(); }
153 
154  static bool isnaninf(const ScalarType& x) {
155  for (int i=0; i<x.size(); i++)
156  if (TVT::isnaninf(x.fastAccessCoeff(i)))
157  return true;
158  return false;
159  }
160 
161  static void seedrandom(unsigned int s) { TVT::seedrandom(s); }
162 
163  static ScalarType random() { return ScalarType(TVT::random()); }
164 
165  static const char * name() { return "Sacado::MP::Vector<>"; }
166 
167  static ScalarType squareroot(const ScalarType& x) { return std::sqrt(x); }
168 
169  static ScalarType pow(const ScalarType& x, const ScalarType& y) {
170  return std::pow(x,y);
171  }
172 
173  static ScalarType log(const ScalarType& x) { return std::log(x); }
174 
175  static ScalarType log10(const ScalarType& x) { return std::log10(x); }
176 
177  }; // class ScalarTraitsImp<S,true>
178 
179  // Implementation of Teuchos::ScalarTraits where reductions are not taken
180  // across the components of MP::Vector. In this case magnitudeType is
181  // an MP::Vector
182  template <typename S>
183  struct ScalarTraitsImp<S,false> {
184  typedef Sacado::MP::Vector<S> ScalarType;
185  typedef typename S::value_type value_type;
186  typedef typename S::ordinal_type ordinal_type;
187  typedef Teuchos::ScalarTraits<value_type> TVT;
188 
189  typedef typename TVT::magnitudeType value_mag_type;
190  typedef typename TVT::halfPrecision value_half_type;
191  typedef typename TVT::doublePrecision value_double_type;
192 
193  typedef typename Sacado::mpl::apply<S,ordinal_type,value_mag_type>::type storage_mag_type;
194  typedef typename Sacado::mpl::apply<S,ordinal_type,value_half_type>::type storage_half_type;
195  typedef typename Sacado::mpl::apply<S,ordinal_type,value_double_type>::type storage_double_type;
196 
197  typedef Sacado::MP::Vector<storage_mag_type> magnitudeType;
198  typedef Sacado::MP::Vector<storage_half_type> halfPrecision;
199  typedef Sacado::MP::Vector<storage_double_type> doublePrecision;
200 
201  static const bool isComplex = TVT::isComplex;
202  static const bool isOrdinal = TVT::isOrdinal;
203  static const bool isComparable = TVT::isComparable;
204  static const bool hasMachineParameters = TVT::hasMachineParameters;
205 
206  static value_mag_type eps() { return TVT::eps(); }
207 
208  static value_mag_type sfmin() { return TVT::sfmin(); }
209 
210  static value_mag_type base() { return TVT::base(); }
211 
212  static value_mag_type prec() { return TVT::prec(); }
213 
214  static value_mag_type t() { return TVT::t(); }
215 
216  static value_mag_type rnd() { return TVT::rnd(); }
217 
218  static value_mag_type emin() { return TVT::emin(); }
219 
220  static value_mag_type rmin() { return TVT::rmin(); }
221 
222  static value_mag_type emax() { return TVT::emax(); }
223 
224  static value_mag_type rmax() { return TVT::rmax(); }
225 
226  static magnitudeType magnitude(const ScalarType& a) {
227  return std::fabs(a);
228  }
229 
230  static ScalarType zero() { return ScalarType(0.0); }
231 
232  static ScalarType one() { return ScalarType(1.0); }
233 
234 
235  static ScalarType conjugate(const ScalarType& x) {
236  int sz = x.size();
237  ScalarType y(sz, value_type(0.0));
238  for (int i=0; i<sz; i++)
239  y.fastAccessCoeff(i) = TVT::conjugate(x.fastAccessCoeff(i));
240  return y;
241  }
242 
243  static ScalarType real(const ScalarType& x) {
244  int sz = x.size();
245  ScalarType y(sz, value_type(0.0));
246  for (int i=0; i<sz; i++)
247  y.fastAccessCoeff(i) = TVT::real(x.fastAccessCoeff(i));
248  return y;
249  }
250 
251  static ScalarType imag(const ScalarType& x) {
252  int sz = x.size();
253  ScalarType y(sz, value_type(0.0));
254  for (int i=0; i<sz; i++)
255  y.fastAccessCoeff(i) = TVT::imag(x.fastAccessCoeff(i));
256  return y;
257  }
258 
259  static value_type nan() { return TVT::nan(); }
260 
261  static bool isnaninf(const ScalarType& x) {
262  for (int i=0; i<x.size(); i++)
263  if (TVT::isnaninf(x.fastAccessCoeff(i)))
264  return true;
265  return false;
266  }
267 
268  static void seedrandom(unsigned int s) { TVT::seedrandom(s); }
269 
270  static ScalarType random() { return ScalarType(TVT::random()); }
271 
272  static const char * name() { return "Sacado::MP::Vector<>"; }
273 
274  static ScalarType squareroot(const ScalarType& x) { return std::sqrt(x); }
275 
276  static ScalarType pow(const ScalarType& x, const ScalarType& y) {
277  return std::pow(x,y);
278  }
279 
280  static ScalarType log(const ScalarType& x) { return std::log(x); }
281 
282  static ScalarType log10(const ScalarType& x) { return std::log10(x); }
283 
284  }; // class ScalarTraitsImp<S,false>
285 
287  template <typename Ordinal, typename VecType, typename Serializer>
288  struct SerializationImp {
289 
290  private:
291 
293  typedef typename Sacado::ValueType<VecType>::type ValueT;
294 
296  typedef Teuchos::SerializationTraits<Ordinal,int> iSerT;
297 
299  typedef Teuchos::SerializationTraits<Ordinal,Ordinal> oSerT;
300 
301  public:
302 
304  static const bool supportsDirectSerialization = false;
305 
307 
308 
310  static Ordinal fromCountToIndirectBytes(const Serializer& vs,
311  const Ordinal count,
312  const VecType buffer[],
313  const Ordinal sz = 0) {
314  Ordinal bytes = 0;
315  VecType *x = NULL;
316  const VecType *cx;
317  for (Ordinal i=0; i<count; i++) {
318  int my_sz = buffer[i].size();
319  int tot_sz = sz;
320  if (sz == 0) tot_sz = my_sz;
321  Ordinal b1 = iSerT::fromCountToIndirectBytes(1, &tot_sz);
322  if (tot_sz != my_sz) {
323  if (x == NULL)
324  x = new VecType;
325  *x = buffer[i];
326  x->reset(tot_sz);
327  cx = x;
328  }
329  else
330  cx = &(buffer[i]);
331  Ordinal b2 = vs.fromCountToIndirectBytes(tot_sz, cx->coeff());
332  Ordinal b3 = oSerT::fromCountToIndirectBytes(1, &b2);
333  bytes += b1+b2+b3;
334  }
335  if (x != NULL)
336  delete x;
337  return bytes;
338  }
339 
341  static void serialize (const Serializer& vs,
342  const Ordinal count,
343  const VecType buffer[],
344  const Ordinal bytes,
345  char charBuffer[],
346  const Ordinal sz = 0) {
347  VecType *x = NULL;
348  const VecType *cx;
349  for (Ordinal i=0; i<count; i++) {
350  // First serialize size
351  int my_sz = buffer[i].size();
352  int tot_sz = sz;
353  if (sz == 0) tot_sz = my_sz;
354  Ordinal b1 = iSerT::fromCountToIndirectBytes(1, &tot_sz);
355  iSerT::serialize(1, &tot_sz, b1, charBuffer);
356  charBuffer += b1;
357 
358  // Next serialize vector coefficients
359  if (tot_sz != my_sz) {
360  if (x == NULL)
361  x = new VecType;
362  *x = buffer[i];
363  x->reset(tot_sz);
364  cx = x;
365  }
366  else
367  cx = &(buffer[i]);
368  Ordinal b2 = vs.fromCountToIndirectBytes(tot_sz, cx->coeff());
369  Ordinal b3 = oSerT::fromCountToIndirectBytes(1, &b2);
370  oSerT::serialize(1, &b2, b3, charBuffer);
371  charBuffer += b3;
372  vs.serialize(tot_sz, cx->coeff(), b2, charBuffer);
373  charBuffer += b2;
374  }
375  if (x != NULL)
376  delete x;
377  }
378 
380  static Ordinal fromIndirectBytesToCount(const Serializer& vs,
381  const Ordinal bytes,
382  const char charBuffer[],
383  const Ordinal sz = 0) {
384  Ordinal count = 0;
385  Ordinal bytes_used = 0;
386  while (bytes_used < bytes) {
387 
388  // Bytes for size
389  Ordinal b1 = iSerT::fromCountToDirectBytes(1);
390  bytes_used += b1;
391  charBuffer += b1;
392 
393  // Bytes for vector coefficients
394  Ordinal b3 = oSerT::fromCountToDirectBytes(1);
395  const Ordinal *b2 = oSerT::convertFromCharPtr(charBuffer);
396  bytes_used += b3;
397  charBuffer += b3;
398  bytes_used += *b2;
399  charBuffer += *b2;
400 
401  ++count;
402  }
403  return count;
404  }
405 
407  static void deserialize (const Serializer& vs,
408  const Ordinal bytes,
409  const char charBuffer[],
410  const Ordinal count,
411  VecType buffer[],
412  const Ordinal sz = 0) {
413  for (Ordinal i=0; i<count; i++) {
414 
415  // Deserialize size
416  Ordinal b1 = iSerT::fromCountToDirectBytes(1);
417  const int *my_sz = iSerT::convertFromCharPtr(charBuffer);
418  charBuffer += b1;
419 
420  // Create empty Vector object of given size
421  int tot_sz = sz;
422  if (sz == 0) tot_sz = *my_sz;
423  buffer[i] = VecType(tot_sz, ValueT(0.0));
424 
425  // Deserialize vector coefficients
426  Ordinal b3 = oSerT::fromCountToDirectBytes(1);
427  const Ordinal *b2 = oSerT::convertFromCharPtr(charBuffer);
428  charBuffer += b3;
429  vs.deserialize(*b2, charBuffer, *my_sz, buffer[i].coeff());
430  charBuffer += *b2;
431  }
432 
433  }
435 
436  };
437 
439  template <typename Ordinal, typename VecType, bool is_static = false>
440  struct SerializationTraitsImp {
441 
442  private:
443 
445  typedef typename Sacado::ValueType<VecType>::type ValueT;
446 
448  typedef Teuchos::DefaultSerializer<Ordinal,ValueT> DS;
449 
451  typedef typename DS::DefaultSerializerType ValueSerializer;
452 
454  typedef SerializationImp<Ordinal,VecType,ValueSerializer> Imp;
455 
456  public:
457 
459  static const bool supportsDirectSerialization =
460  Imp::supportsDirectSerialization;
461 
463 
464 
466  static Ordinal fromCountToIndirectBytes(const Ordinal count,
467  const VecType buffer[]) {
468  return Imp::fromCountToIndirectBytes(
469  DS::getDefaultSerializer(), count, buffer);
470  }
471 
473  static void serialize (const Ordinal count,
474  const VecType buffer[],
475  const Ordinal bytes,
476  char charBuffer[]) {
477  Imp::serialize(
478  DS::getDefaultSerializer(), count, buffer, bytes, charBuffer);
479  }
480 
482  static Ordinal fromIndirectBytesToCount(const Ordinal bytes,
483  const char charBuffer[]) {
484  return Imp::fromIndirectBytesToCount(
485  DS::getDefaultSerializer(), bytes, charBuffer);
486  }
487 
489  static void deserialize (const Ordinal bytes,
490  const char charBuffer[],
491  const Ordinal count,
492  VecType buffer[]) {
493  Imp::deserialize(
494  DS::getDefaultSerializer(), bytes, charBuffer, count, buffer);
495  }
496 
498 
499  };
500 
502  template <typename Ordinal, typename VecType>
503  struct SerializationTraitsImp<Ordinal, VecType, true> {
504  typedef typename Sacado::ValueType<VecType>::type ValueT;
505  typedef Teuchos::SerializationTraits<Ordinal,ValueT> vSerT;
506  typedef Teuchos::DirectSerializationTraits<Ordinal,VecType> DSerT;
507  typedef Sacado::MP::SerializationTraitsImp<Ordinal,VecType> STI;
508 
510  static const bool supportsDirectSerialization =
511  vSerT::supportsDirectSerialization;
512 
514 
515 
517  static Ordinal fromCountToDirectBytes(const Ordinal count) {
518  return DSerT::fromCountToDirectBytes(count);
519  }
520 
522  static char* convertToCharPtr( VecType* ptr ) {
523  return DSerT::convertToCharPtr(ptr);
524  }
525 
527  static const char* convertToCharPtr( const VecType* ptr ) {
528  return DSerT::convertToCharPtr(ptr);
529  }
530 
532  static Ordinal fromDirectBytesToCount(const Ordinal bytes) {
533  return DSerT::fromDirectBytesToCount(bytes);
534  }
535 
537  static VecType* convertFromCharPtr( char* ptr ) {
538  return DSerT::convertFromCharPtr(ptr);
539  }
540 
542  static const VecType* convertFromCharPtr( const char* ptr ) {
543  return DSerT::convertFromCharPtr(ptr);
544  }
545 
547 
549 
550 
552  static Ordinal fromCountToIndirectBytes(const Ordinal count,
553  const VecType buffer[]) {
554  if (supportsDirectSerialization)
555  return DSerT::fromCountToIndirectBytes(count, buffer);
556  else
557  return STI::fromCountToIndirectBytes(count, buffer);
558  }
559 
561  static void serialize (const Ordinal count,
562  const VecType buffer[],
563  const Ordinal bytes,
564  char charBuffer[]) {
565  if (supportsDirectSerialization)
566  return DSerT::serialize(count, buffer, bytes, charBuffer);
567  else
568  return STI::serialize(count, buffer, bytes, charBuffer);
569  }
570 
572  static Ordinal fromIndirectBytesToCount(const Ordinal bytes,
573  const char charBuffer[]) {
574  if (supportsDirectSerialization)
575  return DSerT::fromIndirectBytesToCount(bytes, charBuffer);
576  else
577  return STI::fromIndirectBytesToCount(bytes, charBuffer);
578  }
579 
581  static void deserialize (const Ordinal bytes,
582  const char charBuffer[],
583  const Ordinal count,
584  VecType buffer[]) {
585  if (supportsDirectSerialization)
586  return DSerT::deserialize(bytes, charBuffer, count, buffer);
587  else
588  return STI::deserialize(bytes, charBuffer, count, buffer);
589  }
590 
592 
593  };
594 
596  template <typename Ordinal, typename VecType, typename ValueSerializer>
597  class SerializerImp {
598 
599  private:
600 
602  typedef SerializationImp<Ordinal,VecType,ValueSerializer> Imp;
603 
605  Teuchos::RCP<const ValueSerializer> vs;
606 
608  Ordinal sz;
609 
610  public:
611 
613  typedef ValueSerializer value_serializer_type;
614 
616  static const bool supportsDirectSerialization =
617  Imp::supportsDirectSerialization;
618 
620  SerializerImp(const Teuchos::RCP<const ValueSerializer>& vs_,
621  Ordinal sz_ = 0) :
622  vs(vs_), sz(sz_) {}
623 
625  Ordinal getSerializerSize() const { return sz; }
626 
628  Teuchos::RCP<const value_serializer_type> getValueSerializer() const {
629  return vs; }
630 
632 
633 
635  Ordinal fromCountToIndirectBytes(const Ordinal count,
636  const VecType buffer[]) const {
637  return Imp::fromCountToIndirectBytes(*vs, count, buffer, sz);
638  }
639 
641  void serialize (const Ordinal count,
642  const VecType buffer[],
643  const Ordinal bytes,
644  char charBuffer[]) const {
645  Imp::serialize(*vs, count, buffer, bytes, charBuffer, sz);
646  }
647 
649  Ordinal fromIndirectBytesToCount(const Ordinal bytes,
650  const char charBuffer[]) const {
651  return Imp::fromIndirectBytesToCount(*vs, bytes, charBuffer, sz);
652  }
653 
655  void deserialize (const Ordinal bytes,
656  const char charBuffer[],
657  const Ordinal count,
658  VecType buffer[]) const {
659  return Imp::deserialize(*vs, bytes, charBuffer, count, buffer, sz);
660  }
661 
663 
664  };
665 
666  }
667 
668 }
669 
670 #endif // HAVE_SACADO_TEUCHOS
671 
672 #endif // SACADO_MP_SCALAR_TRAITS_IMP_HPP
KOKKOS_INLINE_FUNCTION PCE< Storage > sqrt(const PCE< Storage > &a)
KOKKOS_INLINE_FUNCTION PCE< Storage > fabs(const PCE< Storage > &a)
KOKKOS_INLINE_FUNCTION PCE< Storage > pow(const PCE< Storage > &a, const PCE< Storage > &b)
const IndexType const IndexType const IndexType const IndexType const ValueType const ValueType * x
Definition: csr_vector.h:260
Sacado::Random< double > rnd
KOKKOS_INLINE_FUNCTION PCE< Storage > log(const PCE< Storage > &a)
KOKKOS_INLINE_FUNCTION PCE< Storage > log10(const PCE< Storage > &a)
const IndexType const IndexType const IndexType const IndexType const ValueType const ValueType ValueType * y
Definition: csr_vector.h:267