Sacado Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Sacado_Tay_SerializationTraitsImp.hpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Sacado Package
4 //
5 // Copyright 2006 NTESS and the Sacado contributors.
6 // SPDX-License-Identifier: LGPL-2.1-or-later
7 // *****************************************************************************
8 // @HEADER
9 
10 #ifndef SACADO_TAY_SERIALIZATIONTRAITSIMP_HPP
11 #define SACADO_TAY_SERIALIZATIONTRAITSIMP_HPP
12 
13 #include "Sacado_ConfigDefs.h"
14 
15 #ifdef HAVE_SACADO_TEUCHOSCOMM
16 
19 #include "Teuchos_RCP.hpp"
20 
21 namespace Sacado {
22 
23  namespace Tay {
24 
26  template <typename Ordinal, typename TayType, typename Serializer>
27  struct SerializationImp {
28 
29  private:
30 
33 
36 
37  public:
38 
40  static const bool supportsDirectSerialization = false;
41 
43 
44 
46  static Ordinal fromCountToIndirectBytes(const Serializer& vs,
47  const Ordinal count,
48  const TayType buffer[],
49  const Ordinal sz = 0) {
50  Ordinal bytes = 0;
51  TayType *x = NULL;
52  const TayType *cx;
53  for (Ordinal i=0; i<count; i++) {
54  unsigned int my_sz = buffer[i].degree()+1;
55  unsigned int tot_sz = sz;
56  if (sz == 0) tot_sz = my_sz;
57  if (tot_sz != my_sz) {
58  x = new TayType(buffer[i]);
59  x->resize(tot_sz-1, true);
60  cx = x;
61  }
62  else
63  cx = &(buffer[i]);
64  Ordinal b1 = iSerT::fromCountToIndirectBytes(1, &tot_sz);
65  Ordinal b2 = vs.fromCountToIndirectBytes(
66  tot_sz, &(cx->fastAccessCoeff(0)));
67  Ordinal b3 = oSerT::fromCountToIndirectBytes(1, &b2);
68  bytes += b1+b2+b3;
69  if (x != NULL) {
70  delete x;
71  x = NULL;
72  }
73  }
74  return bytes;
75  }
76 
78  static void serialize (const Serializer& vs,
79  const Ordinal count,
80  const TayType buffer[],
81  const Ordinal bytes,
82  char charBuffer[],
83  const Ordinal sz = 0) {
84  TayType *x = NULL;
85  const TayType *cx;
86  for (Ordinal i=0; i<count; i++) {
87  // First serialize degree
88  unsigned int my_sz = buffer[i].degree()+1;
89  unsigned int tot_sz = sz;
90  if (sz == 0) tot_sz = my_sz;
91  Ordinal b1 = iSerT::fromCountToIndirectBytes(1, &tot_sz);
92  iSerT::serialize(1, &tot_sz, b1, charBuffer);
93  charBuffer += b1;
94 
95  // Next serialize taylor coefficients
96  if (tot_sz != my_sz) {
97  x = new TayType(buffer[i]);
98  x->resize(tot_sz-1, true);
99  cx = x;
100  }
101  else
102  cx = &(buffer[i]);
103  Ordinal b2 = vs.fromCountToIndirectBytes(
104  tot_sz, &(cx->fastAccessCoeff(0)));
105  Ordinal b3 = oSerT::fromCountToIndirectBytes(1, &b2);
106  oSerT::serialize(1, &b2, b3, charBuffer);
107  charBuffer += b3;
108  vs.serialize(tot_sz, &(cx->fastAccessCoeff(0)), b2, charBuffer);
109  charBuffer += b2;
110  if (x != NULL) {
111  delete x;
112  x = NULL;
113  }
114  }
115  }
116 
118  static Ordinal fromIndirectBytesToCount(const Serializer& vs,
119  const Ordinal bytes,
120  const char charBuffer[],
121  const Ordinal sz = 0) {
122  Ordinal count = 0;
123  Ordinal bytes_used = 0;
124  while (bytes_used < bytes) {
125 
126  // Bytes for degree
127  Ordinal b1 = iSerT::fromCountToDirectBytes(1);
128  bytes_used += b1;
129  charBuffer += b1;
130 
131  // Bytes for taylor coefficients
132  Ordinal b3 = oSerT::fromCountToDirectBytes(1);
133  const Ordinal *b2 = oSerT::convertFromCharPtr(charBuffer);
134  bytes_used += b3;
135  charBuffer += b3;
136  bytes_used += *b2;
137  charBuffer += *b2;
138 
139  ++count;
140  }
141  return count;
142  }
143 
145  static void deserialize (const Serializer& vs,
146  const Ordinal bytes,
147  const char charBuffer[],
148  const Ordinal count,
149  TayType buffer[],
150  const Ordinal sz = 0) {
151  for (Ordinal i=0; i<count; i++) {
152 
153  // Deserialize degree
154  Ordinal b1 = iSerT::fromCountToDirectBytes(1);
155  const unsigned int *my_sz = iSerT::convertFromCharPtr(charBuffer);
156  charBuffer += b1;
157 
158  // Create empty Taylor object of given size
159  unsigned int tot_sz = sz;
160  if (sz == 0) tot_sz = *my_sz;
161  buffer[i] = TayType(tot_sz-1, 0.0);
162 
163  // Deserialize taylor coefficients
164  Ordinal b3 = oSerT::fromCountToDirectBytes(1);
165  const Ordinal *b2 = oSerT::convertFromCharPtr(charBuffer);
166  charBuffer += b3;
167  vs.deserialize(*b2, charBuffer, *my_sz,
168  &(buffer[i].fastAccessCoeff(0)));
169  charBuffer += *b2;
170  }
171 
172  }
173 
175 
176  };
177 
179  template <typename Ordinal, typename TayType>
180  struct SerializationTraitsImp {
181 
182  private:
183 
185  typedef typename Sacado::ValueType<TayType>::type ValueT;
186 
189 
191  typedef typename DS::DefaultSerializerType ValueSerializer;
192 
194  typedef SerializationImp<Ordinal,TayType,ValueSerializer> Imp;
195 
196  public:
197 
199  static const bool supportsDirectSerialization =
200  Imp::supportsDirectSerialization;
201 
203 
204 
206  static Ordinal fromCountToIndirectBytes(const Ordinal count,
207  const TayType buffer[]) {
208  return Imp::fromCountToIndirectBytes(
209  DS::getDefaultSerializer(), count, buffer);
210  }
211 
213  static void serialize (const Ordinal count,
214  const TayType buffer[],
215  const Ordinal bytes,
216  char charBuffer[]) {
217  Imp::serialize(
218  DS::getDefaultSerializer(), count, buffer, bytes, charBuffer);
219  }
220 
222  static Ordinal fromIndirectBytesToCount(const Ordinal bytes,
223  const char charBuffer[]) {
224  return Imp::fromIndirectBytesToCount(
225  DS::getDefaultSerializer(), bytes, charBuffer);
226  }
227 
229  static void deserialize (const Ordinal bytes,
230  const char charBuffer[],
231  const Ordinal count,
232  TayType buffer[]) {
233  Imp::deserialize(
234  DS::getDefaultSerializer(), bytes, charBuffer, count, buffer);
235  }
236 
238 
239  };
240 
242  template <typename Ordinal, typename TayType, typename ValueSerializer>
243  class SerializerImp {
244 
245  private:
246 
248  typedef SerializationImp<Ordinal,TayType,ValueSerializer> Imp;
249 
252 
254  Ordinal sz;
255 
256  public:
257 
259  typedef ValueSerializer value_serializer_type;
260 
262  static const bool supportsDirectSerialization =
263  Imp::supportsDirectSerialization;
264 
266  SerializerImp(const Teuchos::RCP<const ValueSerializer>& vs_,
267  Ordinal sz_ = 0) :
268  vs(vs_), sz(sz_) {}
269 
271  Ordinal getSerializerSize() const { return sz; }
272 
274  Teuchos::RCP<const value_serializer_type> getValueSerializer() const {
275  return vs; }
276 
278 
279 
281  Ordinal fromCountToIndirectBytes(const Ordinal count,
282  const TayType buffer[]) const {
283  return Imp::fromCountToIndirectBytes(*vs, count, buffer, sz);
284  }
285 
287  void serialize (const Ordinal count,
288  const TayType buffer[],
289  const Ordinal bytes,
290  char charBuffer[]) const {
291  Imp::serialize(*vs, count, buffer, bytes, charBuffer, sz);
292  }
293 
295  Ordinal fromIndirectBytesToCount(const Ordinal bytes,
296  const char charBuffer[]) const {
297  return Imp::fromIndirectBytesToCount(*vs, bytes, charBuffer, sz);
298  }
299 
301  void deserialize (const Ordinal bytes,
302  const char charBuffer[],
303  const Ordinal count,
304  TayType buffer[]) const {
305  return Imp::deserialize(*vs, bytes, charBuffer, count, buffer, sz);
306  }
307 
309 
310  };
311 
312  } // namespace Tay
313 
314 } // namespace Sacado
315 
316 #endif // HAVE_SACADO_TEUCHOSCOMM
317 
318 #endif // SACADO_FAD_SERIALIZATIONTRAITSIMP_HPP
int * count
int Ordinal