MADNESS  version 0.9
bufar.h
Go to the documentation of this file.
1 /*
2  This file is part of MADNESS.
3 
4  Copyright (C) 2007,2010 Oak Ridge National Laboratory
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with this program; if not, write to the Free Software
18  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 
20  For more information please contact:
21 
22  Robert J. Harrison
23  Oak Ridge National Laboratory
24  One Bethel Valley Road
25  P.O. Box 2008, MS-6367
26 
27  email: harrisonrj@ornl.gov
28  tel: 865-241-3937
29  fax: 865-572-0680
30 
31 
32  $Id$
33 */
34 
35 
36 #ifndef MADNESS_WORLD_BUFAR_H__INCLUDED
37 #define MADNESS_WORLD_BUFAR_H__INCLUDED
38 
41 
42 
43 #include <madness/world/archive.h>
44 #include <madness/world/print.h>
45 #include <cstring>
46 
47 
48 namespace madness {
49  namespace archive {
50 
52 
59  private:
60  unsigned char * const ptr; // Buffer
61  const std::size_t nbyte; // Buffer size
62  mutable std::size_t i; // Current output location
63  bool countonly; // If true just count, don't copy
64  public:
66  : ptr(0), nbyte(0), i(0), countonly(true) {}
67 
68  BufferOutputArchive(void* ptr, std::size_t nbyte)
69  : ptr((unsigned char *) ptr), nbyte(nbyte), i(0), countonly(false) {}
70 
71  template <class T>
72  inline
74  store(const T* t, long n) const {
75  std::size_t m = n*sizeof(T);
76  if (countonly) {
77  i += m;
78  }
79  else if (i+m > nbyte) {
80  madness::print("BufferOutputArchive:ptr,nbyte,i,n,m,i+m:",(void *)ptr,nbyte,i,n,m,i+m);
81  MADNESS_ASSERT(i+m<=nbyte);
82  }
83  else {
84  memcpy(ptr+i, t, m);
85  i += m;
86  }
87  }
88 
89  void open(std::size_t /*hint*/) {}
90 
91  void close() {}
92 
93  void flush() {}
94 
95  bool count_only() const { return countonly; }
96 
97  inline std::size_t size() const {
98  return i;
99  };
100  };
101 
102 
104  template <typename A, typename B, typename C, typename D, typename E, typename F, typename G, typename H, typename I, typename J>
105  inline size_t bufar_size(const A& a, const B& b, const C& c, const D& d,
106  const E& e, const F& f, const G& g, const H& h,
107  const I& i, const J& j) {
108  BufferOutputArchive count;
109  count & a & b & c & d & e & f & g & h & i & j;
110  return count.size();
111  }
112 
113 
115  template <typename A, typename B, typename C, typename D, typename E, typename F, typename G, typename H, typename I>
116  inline size_t bufar_size(const A& a, const B& b, const C& c, const D& d,
117  const E& e, const F& f, const G& g, const H& h,
118  const I& i) {
119  BufferOutputArchive count;
120  count & a & b & c & d & e & f & g & h & i;
121  return count.size();
122  }
123 
124 
126  template <typename A, typename B, typename C, typename D, typename E, typename F, typename G, typename H>
127  inline size_t bufar_size(const A& a, const B& b, const C& c, const D& d,
128  const E& e, const F& f, const G& g, const H& h) {
129  BufferOutputArchive count;
130  count & a & b & c & d & e & f & g & h;
131  return count.size();
132  }
133 
134 
136  template <typename A, typename B, typename C, typename D, typename E, typename F, typename G>
137  inline size_t bufar_size(const A& a, const B& b, const C& c, const D& d,
138  const E& e, const F& f, const G& g) {
139  BufferOutputArchive count;
140  count & a & b & c & d & e & f & g;
141  return count.size();
142  }
143 
144 
146  template <typename A, typename B, typename C, typename D, typename E, typename F>
147  inline size_t bufar_size(const A& a, const B& b, const C& c, const D& d,
148  const E& e, const F& f) {
149  BufferOutputArchive count;
150  count & a & b & c & d & e & f;
151  return count.size();
152  }
153 
154 
156  template <typename A, typename B, typename C, typename D, typename E>
157  inline size_t bufar_size(const A& a, const B& b, const C& c, const D& d,
158  const E& e) {
159  BufferOutputArchive count;
160  count & a & b & c & d & e;
161  return count.size();
162  }
163 
164 
166  template <typename A, typename B, typename C, typename D>
167  inline size_t bufar_size(const A& a, const B& b, const C& c, const D& d) {
168  BufferOutputArchive count;
169  count & a & b & c & d;
170  return count.size();
171  }
172 
173 
175  template <typename A, typename B, typename C>
176  inline size_t bufar_size(const A& a, const B& b, const C& c) {
177  BufferOutputArchive count;
178  count & a & b & c;
179  return count.size();
180  }
181 
182 
184  template <typename A, typename B>
185  inline size_t bufar_size(const A& a, const B& b) {
186  BufferOutputArchive count;
187  count & a & b;
188  return count.size();
189  }
190 
191 
193  template <typename A>
194  inline size_t bufar_size(const A& a) {
195  BufferOutputArchive count;
196  count & a;
197  return count.size();
198  }
199 
200 
202 
207  private:
208  const unsigned char* const ptr;
209  const std::size_t nbyte;
210  mutable std::size_t i;
211 
212  public:
213  BufferInputArchive(const void* ptr, std::size_t nbyte)
214  : ptr((const unsigned char *) ptr), nbyte(nbyte), i(0) {};
215 
216  template <class T>
217  inline
219  load(T* t, long n) const {
220  std::size_t m = n*sizeof(T);
221  MADNESS_ASSERT(m+i <= nbyte);
222  memcpy((unsigned char*) t, ptr+i, m);
223  i += m;
224  }
225 
226  void open() {};
227 
228  void rewind() const {
229  i=0;
230  };
231 
232  std::size_t nbyte_avail() const {
233  return nbyte-i;
234  };
235 
236  void close() {}
237  };
238 
239 
240  // No type checking over Buffer stream for efficiency
241  template <class T>
243  static inline void preamble_store(const BufferOutputArchive& /*ar*/) {}
244  static inline void postamble_store(const BufferOutputArchive& /*ar*/) {}
245  };
246 
247  // No type checking over Buffer stream for efficiency
248  template <class T>
250  static inline void preamble_load(const BufferInputArchive& /*ar*/) {}
251  static inline void postamble_load(const BufferInputArchive& /*ar*/) {}
252  };
253  }
254 }
255 #endif // MADNESS_WORLD_BUFAR_H__INCLUDED
Tensor< double > B
Definition: tdse1d.cc:167
NDIM const Function< R, NDIM > & g
Definition: mra.h:2179
size_t bufar_size(const A &a, const B &b, const C &c, const D &d, const E &e, const F &f, const G &g, const H &h, const I &i, const J &j)
Convenience template computing the size of a buffer archive containing the arguments.
Definition: bufar.h:105
BufferInputArchive(const void *ptr, std::size_t nbyte)
Definition: bufar.h:213
static void preamble_load(const BufferInputArchive &)
Definition: bufar.h:250
static void postamble_load(const BufferInputArchive &)
Definition: bufar.h:251
Interface templates for the archives (serialization)
void open(std::size_t)
Definition: bufar.h:89
std::size_t nbyte_avail() const
Definition: bufar.h:232
NDIM & f
Definition: mra.h:2179
Default implementation of pre/postamble.
Definition: archive.h:641
std::size_t size() const
Definition: bufar.h:97
Wraps an archive around a memory buffer for output.
Definition: bufar.h:58
bool count_only() const
Definition: bufar.h:95
JLOOP2 NK jnz KLOOP2 mov C
Definition: mtxm_gen.h:10
pcomplex_operatorT G
Definition: tdse1d.cc:168
const T1 &f1 return GTEST_2_TUPLE_() T(f0, f1)
void rewind() const
Definition: bufar.h:228
Base class for output archives classes.
Definition: archive.h:583
static void preamble_store(const BufferOutputArchive &)
Definition: bufar.h:243
FLOAT a(int j, FLOAT z)
Definition: y1.cc:86
BufferOutputArchive()
Definition: bufar.h:65
Base class for input archives classes.
Definition: archive.h:576
static void postamble_store(const BufferOutputArchive &)
Definition: bufar.h:244
madness::enable_if< madness::is_serializable< T >, void >::type store(const T *t, long n) const
Definition: bufar.h:74
void close()
Definition: bufar.h:236
void open()
Definition: bufar.h:226
madness::enable_if< madness::is_serializable< T >, void >::type load(T *t, long n) const
Definition: bufar.h:219
const complexd I(0, 1)
const double m
Definition: gfit.cc:199
Wraps an archive around a memory buffer for input.
Definition: bufar.h:206
void close()
Definition: bufar.h:91
void flush()
Definition: bufar.h:93
void print(const A &a)
Print a single item to std::cout terminating with new line.
Definition: print.h:122
enable_if from Boost for conditionally instantiating templates based on type
Definition: enable_if.h:60
Holds machinery to set up Functions/FuncImpls using various Factories and Interfaces.
Definition: chem/atomutil.cc:45
const double c
Definition: gfit.cc:200
BufferOutputArchive(void *ptr, std::size_t nbyte)
Definition: bufar.h:68
FLOAT b(int j, FLOAT z)
Definition: y1.cc:79