Sacado Package Browser (Single Doxygen Collection) Version of the Day
Loading...
Searching...
No Matches
Sacado_Fad_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_FAD_SERIALIZATIONTRAITSIMP_HPP
31#define SACADO_FAD_SERIALIZATIONTRAITSIMP_HPP
32
33#include "Sacado_ConfigDefs.h"
34
35#ifdef HAVE_SACADO_TEUCHOSCOMM
36
37#include "Teuchos_SerializationTraits.hpp"
38#include "Teuchos_SerializationTraitsHelpers.hpp"
39#include "Teuchos_RCP.hpp"
40
41namespace Sacado {
42
43 namespace Fad {
44
46 template <typename Ordinal, typename FadType, typename Serializer>
47 struct SerializationImp {
48
49 private:
50
52 typedef Teuchos::SerializationTraits<Ordinal,int> iSerT;
53
55 typedef Teuchos::SerializationTraits<Ordinal,Ordinal> oSerT;
56
58 typedef typename Sacado::ValueType<FadType>::type value_type;
59
60 public:
61
63 static const bool supportsDirectSerialization = false;
64
66
67
70 const Ordinal count,
71 const FadType buffer[],
72 const Ordinal sz = 0) {
73 Ordinal bytes = 0;
74 FadType *x = NULL;
75 const FadType *cx;
76 for (Ordinal i=0; i<count; i++) {
77 int my_sz = buffer[i].size();
78 int tot_sz = sz;
79 if (sz == 0) tot_sz = my_sz;
80 Ordinal b1 = iSerT::fromCountToIndirectBytes(1, &tot_sz);
81 Ordinal b2 = vs.fromCountToIndirectBytes(1, &(buffer[i].val()));
82 Ordinal b3 = oSerT::fromCountToIndirectBytes(1, &b2);
83 Ordinal b4;
84 if (tot_sz != my_sz) {
85 if (x == NULL)
86 x = new FadType(tot_sz, 0.0);
87 *x = buffer[i];
88 x->expand(tot_sz);
89 cx = x;
90 }
91 else
92 cx = &(buffer[i]);
93 b4 = vs.fromCountToIndirectBytes(tot_sz, cx->dx());
94 Ordinal b5 = oSerT::fromCountToIndirectBytes(1, &b4);
95 bytes += b1+b2+b3+b4+b5;
96 }
97 if (x != NULL)
98 delete x;
99 return bytes;
100 }
101
103 static void serialize (const Serializer& vs,
104 const Ordinal count,
105 const FadType buffer[],
106 const Ordinal bytes,
107 char charBuffer[],
108 const Ordinal sz = 0) {
109 FadType *x = NULL;
110 const FadType *cx;
111 for (Ordinal i=0; i<count; i++) {
112 // First serialize size
113 int my_sz = buffer[i].size();
114 int tot_sz = sz;
115 if (sz == 0) tot_sz = my_sz;
116 Ordinal b1 = iSerT::fromCountToIndirectBytes(1, &tot_sz);
117 iSerT::serialize(1, &tot_sz, b1, charBuffer);
118 charBuffer += b1;
119
120 // Next serialize value
121 Ordinal b2 = vs.fromCountToIndirectBytes(1, &(buffer[i].val()));
122 Ordinal b3 = oSerT::fromCountToIndirectBytes(1, &b2);
123 oSerT::serialize(1, &b2, b3, charBuffer);
124 charBuffer += b3;
125 vs.serialize(1, &(buffer[i].val()), b2, charBuffer);
126 charBuffer += b2;
127
128 // Next serialize derivative array
129 Ordinal b4;
130 if (tot_sz != my_sz) {
131 if (x == NULL)
132 x = new FadType(tot_sz, 0.0);
133 *x = buffer[i];
134 x->expand(tot_sz);
135 cx = x;
136 }
137 else
138 cx = &(buffer[i]);
139 b4 = vs.fromCountToIndirectBytes(tot_sz, cx->dx());
140 Ordinal b5 = oSerT::fromCountToIndirectBytes(1, &b4);
141 oSerT::serialize(1, &b4, b5, charBuffer);
142 charBuffer += b5;
143 vs.serialize(tot_sz, cx->dx(), b4, charBuffer);
144 charBuffer += b4;
145 }
146 if (x != NULL)
147 delete x;
148 }
149
152 const Ordinal bytes,
153 const char charBuffer[],
154 const Ordinal sz = 0) {
155 Ordinal count = 0;
157 while (bytes_used < bytes) {
158
159 // Bytes for size
160 Ordinal b1 = iSerT::fromCountToDirectBytes(1);
161 bytes_used += b1;
162 charBuffer += b1;
163
164 // Bytes for value
165 Ordinal b3 = oSerT::fromCountToDirectBytes(1);
166 const Ordinal *b2 = oSerT::convertFromCharPtr(charBuffer);
167 bytes_used += b3;
168 charBuffer += b3;
169 bytes_used += *b2;
170 charBuffer += *b2;
171
172 // Bytes for derivative array
173 Ordinal b5 = oSerT::fromCountToDirectBytes(1);
174 const Ordinal *b4 = oSerT::convertFromCharPtr(charBuffer);
175 bytes_used += b5;
176 charBuffer += b5;
177 bytes_used += *b4;
178 charBuffer += *b4;
179
180 ++count;
181 }
182 return count;
183 }
184
186 static void deserialize (const Serializer& vs,
187 const Ordinal bytes,
188 const char charBuffer[],
189 const Ordinal count,
190 FadType buffer[],
191 const Ordinal sz = 0) {
192 for (Ordinal i=0; i<count; i++) {
193
194 // Deserialize size
195 Ordinal b1 = iSerT::fromCountToDirectBytes(1);
196 const int *my_sz = iSerT::convertFromCharPtr(charBuffer);
197 charBuffer += b1;
198
199 // Create empty Fad object of given size
200 int tot_sz = sz;
201 if (sz == 0) tot_sz = *my_sz;
202 buffer[i] = FadType(tot_sz, 0.0);
203
204 // Deserialize value
205 Ordinal b3 = oSerT::fromCountToDirectBytes(1);
206 const Ordinal *b2 = oSerT::convertFromCharPtr(charBuffer);
207 charBuffer += b3;
208 vs.deserialize(*b2, charBuffer, 1, &(buffer[i].val()));
209 charBuffer += *b2;
210
211 // Deserialize derivative array
212 Ordinal b5 = oSerT::fromCountToDirectBytes(1);
213 const Ordinal *b4 = oSerT::convertFromCharPtr(charBuffer);
214 charBuffer += b5;
215 vs.deserialize(*b4, charBuffer, *my_sz,
216 &(buffer[i].fastAccessDx(0)));
217 charBuffer += *b4;
218 }
219
220 }
221
223
224 };
225
227 template <typename Ordinal, typename FadType>
228 struct SerializationTraitsImp {
229
230 private:
231
234
236 typedef Teuchos::DefaultSerializer<Ordinal,ValueT> DS;
237
239 typedef typename DS::DefaultSerializerType ValueSerializer;
240
243
244 public:
245
247 static const bool supportsDirectSerialization =
248 Imp::supportsDirectSerialization;
249
251
252
255 const FadType buffer[]) {
256 return Imp::fromCountToIndirectBytes(
257 DS::getDefaultSerializer(), count, buffer);
258 }
259
261 static void serialize (const Ordinal count,
262 const FadType buffer[],
263 const Ordinal bytes,
264 char charBuffer[]) {
265 Imp::serialize(
266 DS::getDefaultSerializer(), count, buffer, bytes, charBuffer);
267 }
268
271 const char charBuffer[]) {
272 return Imp::fromIndirectBytesToCount(
273 DS::getDefaultSerializer(), bytes, charBuffer);
274 }
275
277 static void deserialize (const Ordinal bytes,
278 const char charBuffer[],
279 const Ordinal count,
280 FadType buffer[]) {
281 Imp::deserialize(
282 DS::getDefaultSerializer(), bytes, charBuffer, count, buffer);
283 }
284
286
287 };
288
290 template <typename Ordinal, typename FadType>
291 struct StaticSerializationTraitsImp {
293 typedef Teuchos::SerializationTraits<Ordinal,ValueT> vSerT;
294 typedef Teuchos::DirectSerializationTraits<Ordinal,FadType> DSerT;
296
298 static const bool supportsDirectSerialization =
299 vSerT::supportsDirectSerialization;
300
302
303
306 return DSerT::fromCountToDirectBytes(count);
307 }
308
310 static char* convertToCharPtr( FadType* ptr ) {
311 return DSerT::convertToCharPtr(ptr);
312 }
313
315 static const char* convertToCharPtr( const FadType* ptr ) {
316 return DSerT::convertToCharPtr(ptr);
317 }
318
321 return DSerT::fromDirectBytesToCount(bytes);
322 }
323
325 static FadType* convertFromCharPtr( char* ptr ) {
326 return DSerT::convertFromCharPtr(ptr);
327 }
328
330 static const FadType* convertFromCharPtr( const char* ptr ) {
331 return DSerT::convertFromCharPtr(ptr);
332 }
333
335
337
338
341 const FadType buffer[]) {
343 return DSerT::fromCountToIndirectBytes(count, buffer);
344 else
345 return STI::fromCountToIndirectBytes(count, buffer);
346 }
347
349 static void serialize (const Ordinal count,
350 const FadType buffer[],
351 const Ordinal bytes,
352 char charBuffer[]) {
354 return DSerT::serialize(count, buffer, bytes, charBuffer);
355 else
356 return STI::serialize(count, buffer, bytes, charBuffer);
357 }
358
361 const char charBuffer[]) {
363 return DSerT::fromIndirectBytesToCount(bytes, charBuffer);
364 else
365 return STI::fromIndirectBytesToCount(bytes, charBuffer);
366 }
367
369 static void deserialize (const Ordinal bytes,
370 const char charBuffer[],
371 const Ordinal count,
372 FadType buffer[]) {
374 return DSerT::deserialize(bytes, charBuffer, count, buffer);
375 else
376 return STI::deserialize(bytes, charBuffer, count, buffer);
377 }
378
380
381 };
382
384 template <typename Ordinal, typename FadType, typename ValueSerializer>
385 class SerializerImp {
386
387 private:
388
391
393 Teuchos::RCP<const ValueSerializer> vs;
394
396 Ordinal sz;
397
398 public:
399
402
404 static const bool supportsDirectSerialization =
405 Imp::supportsDirectSerialization;
406
408 SerializerImp(const Teuchos::RCP<const ValueSerializer>& vs_,
409 Ordinal sz_ = 0) :
410 vs(vs_), sz(sz_) {}
411
413 Ordinal getSerializerSize() const { return sz; }
414
416 Teuchos::RCP<const value_serializer_type> getValueSerializer() const {
417 return vs; }
418
420
421
424 const FadType buffer[]) const {
425 return Imp::fromCountToIndirectBytes(*vs, count, buffer, sz);
426 }
427
429 void serialize (const Ordinal count,
430 const FadType buffer[],
431 const Ordinal bytes,
432 char charBuffer[]) const {
433 Imp::serialize(*vs, count, buffer, bytes, charBuffer, sz);
434 }
435
438 const char charBuffer[]) const {
439 return Imp::fromIndirectBytesToCount(*vs, bytes, charBuffer, sz);
440 }
441
443 void deserialize (const Ordinal bytes,
444 const char charBuffer[],
445 const Ordinal count,
446 FadType buffer[]) const {
447 return Imp::deserialize(*vs, bytes, charBuffer, count, buffer, sz);
448 }
449
451
452 };
453
454 } // namespace Fad
455
456} // namespace Sacado
457
458#endif // HAVE_SACADO_TEUCHOSCOMM
459
460#endif // SACADO_FAD_SERIALIZATIONTRAITSIMP_HPP
int Ordinal
expr expr expr fastAccessDx(i)) FAD_UNARYOP_MACRO(exp
expr val()
Sacado::Fad::DFad< double > FadType
Fad specializations for Teuchos::BLAS wrappers.
BLAS(bool use_default_impl=true, bool use_dynamic=true, OrdinalType static_workspace_size=0)
Default constructor.
int * count