MADNESS  version 0.9
mpiar.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_MPIAR_H__INCLUDED
37 #define MADNESS_WORLD_MPIAR_H__INCLUDED
38 
39 #include <madness/world/archive.h>
40 #include <madness/world/worldfwd.h>
41 #include <madness/world/vecar.h>
42 
43 namespace madness {
44  namespace archive {
45 
47  mutable World* world;
48  ProcessID dest;
49  int tag;
50  public:
51  MPIRawOutputArchive(World& world, const ProcessID& dest, int tag=SafeMPI::MPIAR_TAG)
52  : world(&world), dest(dest), tag(tag) {};
53 
54  template <class T>
55  inline
56  typename madness::enable_if< std::is_fundamental<T>, void >::type
57  store(const T* t, long n) const {
58  world->mpi.Send(t, n, dest, tag);
59  }
60  };
61 
63  mutable World* world;
64  ProcessID src;
65  int tag;
66  public:
67  MPIRawInputArchive(World& world, const ProcessID& src, int tag=SafeMPI::MPIAR_TAG)
68  : world(&world), src(src), tag(tag) {};
69 
70  template <class T>
71  inline
72  typename madness::enable_if< std::is_fundamental<T>, void >::type
73  load(T* t, long n) const {
74  world->mpi.Recv(t, n, src, tag);
75  }
76  };
77 
78 
80  mutable World* world;
81  ProcessID dest;
82  int tag;
83  const std::size_t bufsize;
84  mutable std::vector<unsigned char> v;
86  public:
87  MPIOutputArchive(World& world, const ProcessID& dest, int tag=SafeMPI::MPIAR_TAG)
88  : world(&world), dest(dest), tag(tag), bufsize(1024*1024), v(), var(v) {
89  v.reserve(2*bufsize);
90  };
91 
92  template <class T>
93  inline
94  typename madness::enable_if< std::is_fundamental<T>, void >::type
95  store(const T* t, long n) const {
96  if (v.size() > bufsize) flush();
97  var.store(t,n);
98  if (v.size() > bufsize) flush();
99  }
100 
101  void flush() const {
102  if (v.size()) {
103  world->mpi.Send(v.size(), dest, tag);
104  world->mpi.Send(&v[0], v.size(), dest, tag);
105  v.clear();
106  if (v.capacity() < 2*bufsize) v.reserve(2*bufsize); // ?? why ??
107  }
108  };
109 
110  void close() {
111  flush();
112  };
113 
115  close();
116  };
117  };
118 
120  mutable World* world;
121  ProcessID src;
122  int tag;
123  mutable std::vector<unsigned char> v;
125  public:
126  MPIInputArchive(World& world, const ProcessID& src, int tag=SafeMPI::MPIAR_TAG)
127  : world(&world), src(src), tag(tag), v(), var(v) {};
128 
129  template <class T>
130  inline
131  typename madness::enable_if< std::is_fundamental<T>, void >::type
132  load(T* t, long n) const {
133  if (!var.nbyte_avail()) {
134  var.rewind();
135  std::size_t m;
136  world->mpi.Recv(m, src, tag);
137  v.resize(m);
138  world->mpi.Recv(&v[0], m, src, tag);
139  }
140  var.load(t,n);
141  }
142  };
143 
144  // No type checking over MPI stream for efficiency
145  template <class T>
147  static void preamble_store(const MPIRawOutputArchive& ar) {};
148  static inline void postamble_store(const MPIRawOutputArchive& ar) {};
149  };
150 
151  // No type checking over MPI stream for efficiency
152  template <class T>
154  static inline void preamble_load(const MPIRawInputArchive& ar) {};
155  static inline void postamble_load(const MPIRawInputArchive& ar) {};
156  };
157 
158  // No type checking over MPI stream for efficiency
159  template <class T>
161  static void preamble_store(const MPIOutputArchive& ar) {};
162  static inline void postamble_store(const MPIOutputArchive& ar) {};
163  };
164 
165  // No type checking over MPI stream for efficiency
166  template <class T>
168  static inline void preamble_load(const MPIInputArchive& ar) {};
169  static inline void postamble_load(const MPIInputArchive& ar) {};
170  };
171 
172 
173  }
174 }
175 #endif // MADNESS_WORLD_MPIAR_H__INCLUDED
void close()
Definition: mpiar.h:110
void flush() const
Definition: mpiar.h:101
madness::enable_if< std::is_fundamental< T >, void >::type load(T *t, long n) const
Definition: mpiar.h:132
void Recv(T *buf, long lenbuf, int src, int tag) const
Receive data of up to lenbuf elements from process dest.
Definition: worldmpi.h:313
WorldMpiInterface & mpi
MPI interface.
Definition: worldfwd.h:459
Implements archive wrapping an STL vector.
madness::enable_if< std::is_fundamental< T >, void >::type load(T *t, long n) const
Definition: mpiar.h:73
static void preamble_store(const MPIRawOutputArchive &ar)
Definition: mpiar.h:147
MPIInputArchive(World &world, const ProcessID &src, int tag=SafeMPI::MPIAR_TAG)
Definition: mpiar.h:126
madness::enable_if< std::is_fundamental< T >, void >::type store(const T *t, long n) const
Definition: mpiar.h:95
Interface templates for the archives (serialization)
static void postamble_store(const MPIRawOutputArchive &ar)
Definition: mpiar.h:148
~MPIOutputArchive()
Definition: mpiar.h:114
Default implementation of pre/postamble.
Definition: archive.h:641
static void postamble_load(const MPIInputArchive &ar)
Definition: mpiar.h:169
std::size_t nbyte_avail() const
Definition: vecar.h:103
Wraps an archive around an STL vector for output.
Definition: vecar.h:54
Wraps an archive around an STL vector for input.
Definition: vecar.h:81
madness::enable_if< std::is_fundamental< T >, void >::type store(const T *t, long n) const
Definition: mpiar.h:57
madness::enable_if< madness::is_serializable< T >, void >::type store(const T *t, long n) const
Definition: vecar.h:64
const T1 &f1 return GTEST_2_TUPLE_() T(f0, f1)
Base class for output archives classes.
Definition: archive.h:583
static void preamble_load(const MPIRawInputArchive &ar)
Definition: mpiar.h:154
void rewind() const
Definition: vecar.h:99
static void postamble_store(const MPIOutputArchive &ar)
Definition: mpiar.h:162
Definition: mpiar.h:119
Base class for input archives classes.
Definition: archive.h:576
A parallel world with full functionality wrapping an MPI communicator.
Definition: worldfwd.h:416
int ProcessID
Used to clearly identify process number/rank.
Definition: worldtypes.h:37
const double m
Definition: gfit.cc:199
madness::enable_if< madness::is_serializable< T >, void >::type load(T *t, long n) const
Definition: vecar.h:90
MPIOutputArchive(World &world, const ProcessID &dest, int tag=SafeMPI::MPIAR_TAG)
Definition: mpiar.h:87
static void postamble_load(const MPIRawInputArchive &ar)
Definition: mpiar.h:155
MPIRawInputArchive(World &world, const ProcessID &src, int tag=SafeMPI::MPIAR_TAG)
Definition: mpiar.h:67
Implements World.
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
void Send(const T *buf, long lenbuf, int dest, int tag=SafeMPI::DEFAULT_SEND_RECV_TAG) const
Send array of lenbuf elements to process dest.
Definition: worldmpi.h:296
static void preamble_load(const MPIInputArchive &ar)
Definition: mpiar.h:168
static void preamble_store(const MPIOutputArchive &ar)
Definition: mpiar.h:161
MPIRawOutputArchive(World &world, const ProcessID &dest, int tag=SafeMPI::MPIAR_TAG)
Definition: mpiar.h:51