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 //
4 // Sacado Package
5 // Copyright (2006) Sandia Corporation
6 //
7 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
8 // the U.S. Government retains certain rights in this software.
9 //
10 // This library is free software; you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as
12 // published by the Free Software Foundation; either version 2.1 of the
13 // License, or (at your option) any later version.
14 //
15 // This library is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 // Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public
21 // License along with this library; if not, write to the Free Software
22 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
23 // USA
24 // Questions? Contact David M. Gay (dmgay@sandia.gov) or Eric T. Phipps
25 // (etphipp@sandia.gov).
26 //
27 // ***********************************************************************
28 // @HEADER
29 
30 #ifndef SACADO_TAY_SERIALIZATIONTRAITSIMP_HPP
31 #define SACADO_TAY_SERIALIZATIONTRAITSIMP_HPP
32 
33 #include "Sacado_ConfigDefs.h"
34 
35 #ifdef HAVE_SACADO_TEUCHOSCOMM
36 
39 #include "Teuchos_RCP.hpp"
40 
41 namespace Sacado {
42 
43  namespace Tay {
44 
46  template <typename Ordinal, typename TayType, typename Serializer>
47  struct SerializationImp {
48 
49  private:
50 
53 
56 
57  public:
58 
60  static const bool supportsDirectSerialization = false;
61 
63 
64 
66  static Ordinal fromCountToIndirectBytes(const Serializer& vs,
67  const Ordinal count,
68  const TayType buffer[],
69  const Ordinal sz = 0) {
70  Ordinal bytes = 0;
71  TayType *x = NULL;
72  const TayType *cx;
73  for (Ordinal i=0; i<count; i++) {
74  unsigned int my_sz = buffer[i].degree()+1;
75  unsigned int tot_sz = sz;
76  if (sz == 0) tot_sz = my_sz;
77  if (tot_sz != my_sz) {
78  x = new TayType(buffer[i]);
79  x->resize(tot_sz-1, true);
80  cx = x;
81  }
82  else
83  cx = &(buffer[i]);
84  Ordinal b1 = iSerT::fromCountToIndirectBytes(1, &tot_sz);
85  Ordinal b2 = vs.fromCountToIndirectBytes(
86  tot_sz, &(cx->fastAccessCoeff(0)));
87  Ordinal b3 = oSerT::fromCountToIndirectBytes(1, &b2);
88  bytes += b1+b2+b3;
89  if (x != NULL) {
90  delete x;
91  x = NULL;
92  }
93  }
94  return bytes;
95  }
96 
98  static void serialize (const Serializer& vs,
99  const Ordinal count,
100  const TayType buffer[],
101  const Ordinal bytes,
102  char charBuffer[],
103  const Ordinal sz = 0) {
104  TayType *x = NULL;
105  const TayType *cx;
106  for (Ordinal i=0; i<count; i++) {
107  // First serialize degree
108  unsigned int my_sz = buffer[i].degree()+1;
109  unsigned int tot_sz = sz;
110  if (sz == 0) tot_sz = my_sz;
111  Ordinal b1 = iSerT::fromCountToIndirectBytes(1, &tot_sz);
112  iSerT::serialize(1, &tot_sz, b1, charBuffer);
113  charBuffer += b1;
114 
115  // Next serialize taylor coefficients
116  if (tot_sz != my_sz) {
117  x = new TayType(buffer[i]);
118  x->resize(tot_sz-1, true);
119  cx = x;
120  }
121  else
122  cx = &(buffer[i]);
123  Ordinal b2 = vs.fromCountToIndirectBytes(
124  tot_sz, &(cx->fastAccessCoeff(0)));
125  Ordinal b3 = oSerT::fromCountToIndirectBytes(1, &b2);
126  oSerT::serialize(1, &b2, b3, charBuffer);
127  charBuffer += b3;
128  vs.serialize(tot_sz, &(cx->fastAccessCoeff(0)), b2, charBuffer);
129  charBuffer += b2;
130  if (x != NULL) {
131  delete x;
132  x = NULL;
133  }
134  }
135  }
136 
138  static Ordinal fromIndirectBytesToCount(const Serializer& vs,
139  const Ordinal bytes,
140  const char charBuffer[],
141  const Ordinal sz = 0) {
142  Ordinal count = 0;
143  Ordinal bytes_used = 0;
144  while (bytes_used < bytes) {
145 
146  // Bytes for degree
147  Ordinal b1 = iSerT::fromCountToDirectBytes(1);
148  bytes_used += b1;
149  charBuffer += b1;
150 
151  // Bytes for taylor coefficients
152  Ordinal b3 = oSerT::fromCountToDirectBytes(1);
153  const Ordinal *b2 = oSerT::convertFromCharPtr(charBuffer);
154  bytes_used += b3;
155  charBuffer += b3;
156  bytes_used += *b2;
157  charBuffer += *b2;
158 
159  ++count;
160  }
161  return count;
162  }
163 
165  static void deserialize (const Serializer& vs,
166  const Ordinal bytes,
167  const char charBuffer[],
168  const Ordinal count,
169  TayType buffer[],
170  const Ordinal sz = 0) {
171  for (Ordinal i=0; i<count; i++) {
172 
173  // Deserialize degree
174  Ordinal b1 = iSerT::fromCountToDirectBytes(1);
175  const unsigned int *my_sz = iSerT::convertFromCharPtr(charBuffer);
176  charBuffer += b1;
177 
178  // Create empty Taylor object of given size
179  unsigned int tot_sz = sz;
180  if (sz == 0) tot_sz = *my_sz;
181  buffer[i] = TayType(tot_sz-1, 0.0);
182 
183  // Deserialize taylor coefficients
184  Ordinal b3 = oSerT::fromCountToDirectBytes(1);
185  const Ordinal *b2 = oSerT::convertFromCharPtr(charBuffer);
186  charBuffer += b3;
187  vs.deserialize(*b2, charBuffer, *my_sz,
188  &(buffer[i].fastAccessCoeff(0)));
189  charBuffer += *b2;
190  }
191 
192  }
193 
195 
196  };
197 
199  template <typename Ordinal, typename TayType>
200  struct SerializationTraitsImp {
201 
202  private:
203 
205  typedef typename Sacado::ValueType<TayType>::type ValueT;
206 
209 
211  typedef typename DS::DefaultSerializerType ValueSerializer;
212 
214  typedef SerializationImp<Ordinal,TayType,ValueSerializer> Imp;
215 
216  public:
217 
219  static const bool supportsDirectSerialization =
220  Imp::supportsDirectSerialization;
221 
223 
224 
226  static Ordinal fromCountToIndirectBytes(const Ordinal count,
227  const TayType buffer[]) {
228  return Imp::fromCountToIndirectBytes(
229  DS::getDefaultSerializer(), count, buffer);
230  }
231 
233  static void serialize (const Ordinal count,
234  const TayType buffer[],
235  const Ordinal bytes,
236  char charBuffer[]) {
237  Imp::serialize(
238  DS::getDefaultSerializer(), count, buffer, bytes, charBuffer);
239  }
240 
242  static Ordinal fromIndirectBytesToCount(const Ordinal bytes,
243  const char charBuffer[]) {
244  return Imp::fromIndirectBytesToCount(
245  DS::getDefaultSerializer(), bytes, charBuffer);
246  }
247 
249  static void deserialize (const Ordinal bytes,
250  const char charBuffer[],
251  const Ordinal count,
252  TayType buffer[]) {
253  Imp::deserialize(
254  DS::getDefaultSerializer(), bytes, charBuffer, count, buffer);
255  }
256 
258 
259  };
260 
262  template <typename Ordinal, typename TayType, typename ValueSerializer>
263  class SerializerImp {
264 
265  private:
266 
268  typedef SerializationImp<Ordinal,TayType,ValueSerializer> Imp;
269 
272 
274  Ordinal sz;
275 
276  public:
277 
279  typedef ValueSerializer value_serializer_type;
280 
282  static const bool supportsDirectSerialization =
283  Imp::supportsDirectSerialization;
284 
286  SerializerImp(const Teuchos::RCP<const ValueSerializer>& vs_,
287  Ordinal sz_ = 0) :
288  vs(vs_), sz(sz_) {}
289 
291  Ordinal getSerializerSize() const { return sz; }
292 
294  Teuchos::RCP<const value_serializer_type> getValueSerializer() const {
295  return vs; }
296 
298 
299 
301  Ordinal fromCountToIndirectBytes(const Ordinal count,
302  const TayType buffer[]) const {
303  return Imp::fromCountToIndirectBytes(*vs, count, buffer, sz);
304  }
305 
307  void serialize (const Ordinal count,
308  const TayType buffer[],
309  const Ordinal bytes,
310  char charBuffer[]) const {
311  Imp::serialize(*vs, count, buffer, bytes, charBuffer, sz);
312  }
313 
315  Ordinal fromIndirectBytesToCount(const Ordinal bytes,
316  const char charBuffer[]) const {
317  return Imp::fromIndirectBytesToCount(*vs, bytes, charBuffer, sz);
318  }
319 
321  void deserialize (const Ordinal bytes,
322  const char charBuffer[],
323  const Ordinal count,
324  TayType buffer[]) const {
325  return Imp::deserialize(*vs, bytes, charBuffer, count, buffer, sz);
326  }
327 
329 
330  };
331 
332  } // namespace Tay
333 
334 } // namespace Sacado
335 
336 #endif // HAVE_SACADO_TEUCHOSCOMM
337 
338 #endif // SACADO_FAD_SERIALIZATIONTRAITSIMP_HPP
int Ordinal