MADNESS  version 0.9
worldptr.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 #ifndef MADNESS_WORLD_WORLDPTR_H__INCLUDED
36 #define MADNESS_WORLD_WORLDPTR_H__INCLUDED
37 
38 #include <madness/world/worldexc.h> // for MADNESS_ASSERT
39 #include <madness/world/worldtypes.h> // for ProcessID
40 #include <madness/world/archive.h> // for wrap_opaque
41 #include <madness/world/worldfwd.h>
42 #include <algorithm> // for std::swap
43 #include <iostream> // for std::iostream
44 
45 namespace madness {
46 
47  namespace detail {
48 
49  template<typename U>
50  struct ptr_traits {
51  typedef U & reference;
52  };
53 
54  template<>
55  struct ptr_traits<void> {
56  typedef void reference;
57  };
58 
60 
64  template <typename T>
65  class WorldPtr {
66  public:
67  typedef unsigned long worldidT;
68 
69  private:
70  World* world_;
71  worldidT worldid_;
72  ProcessID rank_;
73  T* pointer_;
74 
75  template<typename>
76  friend class WorldPtr;
77 
79 
84  ProcessID local_rank() const { return (world_ != NULL ? world_->rank() : -2); }
85 
86  public:
87 
88  typedef T* pointer;
90 
92 
97  world_(NULL),
98  worldid_(0),
99  rank_(-1),
100  pointer_(NULL)
101  { }
102 
104 
109  WorldPtr(World& w, T* p) :
110  world_(&w),
111  worldid_(w.id() + 1),
112  rank_(w.rank()),
113  pointer_(p)
114  { }
115 
117 
120  WorldPtr(const WorldPtr<T>& other) :
121  world_(other.world_),
122  worldid_(other.worldid_),
123  rank_(other.rank_),
124  pointer_(other.pointer_)
125  { }
126 
128 
134  template <typename U>
135  WorldPtr(const WorldPtr<U>& other) :
136  world_(other.world_),
137  worldid_(other.worldid_),
138  rank_(other.rank_),
139  pointer_(other.pointer_)
140  { }
141 
143 
148  world_ = other.world_;
149  worldid_ = other.worldid_;
150  rank_ = other.rank_;
151  pointer_ = other.pointer_;
152 
153  return *this;
154  }
155 
157 
164  template <typename U>
166  world_ = other.world_;
167  worldid_ = other.worldid_;
168  rank_ = other.rank_;
169  pointer_ = other.pointer_;
170 
171  return *this;
172  }
173 
175 
179  bool is_local() const { return local_rank() == rank_; }
180 
182 
185  bool has_owner() const { return (rank_ != -1) && (world_ != NULL); }
186 
188 
195  pointer get() const {
196  // It is not safe to access this pointer remotely unless null.
197  MADNESS_ASSERT(is_local());
198  return pointer_;
199  }
200 
202 
207  reference operator*() const {
208  // It is not safe to access a NULL pointer with this operator.
209  MADNESS_ASSERT(pointer_ != NULL);
210  // It is not safe to access this pointer remotely.
211  MADNESS_ASSERT(is_local());
212  return *pointer_;
213  }
214 
216 
221  pointer operator->() const {
222  // It is not safe to access a NULL pointer with this operator.
223  MADNESS_ASSERT(pointer_ != NULL);
224  // It is not safe to access this pointer remotely.
225  MADNESS_ASSERT(is_local());
226  return pointer_;
227  }
228 
230 
233  operator bool () const { return pointer_; }
234 
236 
239  bool operator ! () const { return !pointer_; }
240 
242 
247  template <typename U>
248  bool operator==(const WorldPtr<U>& other) const {
249  return (pointer_ == other.pointer_) && (rank_ == other.rank_)
250  && (worldid_ == other.worldid_);
251  }
252 
254 
259  template <typename U>
260  bool operator!=(const WorldPtr<U>& other) const {
261  return (pointer_ != other.pointer_) || (rank_ != other.rank_)
262  || (worldid_ != other.worldid_);
263  }
264 
266 
273  template <typename U>
274  bool operator<(const WorldPtr<U>& other) const {
275  return (worldid_ < other.worldid_) ||
276  ((worldid_ == worldid_) && ((rank_ < other.rank_) ||
277  ((rank_ == other.rank_) && (pointer_ < other.pointer_))));
278  }
279 
281 
285  World& get_world() const {
286  MADNESS_ASSERT(world_ != NULL);
287  return *world_;
288  }
289 
291 
294  worldidT get_worldid() const { return worldid_ - 1; }
295 
297 
301  ProcessID owner() const { return rank_; }
302 
304 
308  template <typename U>
309  void swap(WorldPtr<U>& other) {
310  std::swap(world_, other.world_);
311  std::swap(worldid_, other.worldid_);
312  std::swap(rank_, other.rank_);
313  std::swap(pointer_, other.pointer_);
314  }
315 
317 
321  template <class Archive>
322  inline void load_internal_(const Archive& ar) {
323  ar & worldid_ & rank_ & archive::wrap_opaque(pointer_);
324  world_ = (worldid_ != 0 ? World::world_from_id(get_worldid()) : NULL);
325  }
326 
327  template <class Archive>
328  inline void store_internal_(const Archive& ar) const {
329  ar & worldid_ & rank_ & archive::wrap_opaque(pointer_);
330  }
331 
332  friend std::ostream& operator<<(std::ostream& out, const WorldPtr<T>& p) {
333  out << "WorldPointer(ptr=" << p.pointer_ << ", rank=";
334  if(p.rank_ >= 0)
335  out << p.rank_;
336  else
337  out << "none";
338  out << ", worldid=";
339  if(p.worldid_ != 0)
340  out << (p.worldid_ - 1);
341  else
342  out << "none";
343  out << ")";
344  return out;
345  }
346  }; // class WorldPtr
347 
349 
353  //template <typename T, typename U>
354  //void swap(WorldPtr<T>& l, WorldPtr<U>& r) {
355  template <typename T>
356  void swap(WorldPtr<T>& l, WorldPtr<T>& r) {
357  l.swap(r);
358  }
359 
360  template <typename T, typename U>
361  bool operator>(const WorldPtr<T>& left, const WorldPtr<U>& right) {
362  return right < left;
363  }
364 
365  template <typename T, typename U>
366  bool operator<=(const WorldPtr<T>& left, const WorldPtr<U>& right) {
367  return !(right < left);
368  }
369 
370  template <typename T, typename U>
371  bool operator>=(const WorldPtr<T>& left, const WorldPtr<U>& right) {
372  return !(left < right);
373  }
374 
375 
376 
377  } // namespace detail
378 
379  namespace archive {
380  template <typename, typename>
381  struct ArchiveLoadImpl;
382 
383  template <typename, typename>
384  struct ArchiveStoreImpl;
385 
386  template <typename Archive, typename T>
387  struct ArchiveLoadImpl<Archive, detail::WorldPtr<T> > {
388  static inline void load(const Archive& ar, detail::WorldPtr<T>& p) {
389  p.load_internal_(ar);
390  }
391  };
392 
393  template <typename Archive, typename T>
394  struct ArchiveStoreImpl<Archive, detail::WorldPtr<T> > {
395  static inline void store(const Archive& ar, const detail::WorldPtr<T>& p) {
396  p.store_internal_(ar);
397  }
398  };
399 
400  } // namespace archive
401 } // namespace madness
402 #endif // MADNESS_WORLD_WORLDPTR_H__INCLUDED
static void load(const Archive &ar, detail::WorldPtr< T > &p)
Definition: worldptr.h:388
void store_internal_(const Archive &ar) const
Definition: worldptr.h:328
WorldPtr(const WorldPtr< U > &other)
Copy conversion constructor.
Definition: worldptr.h:135
Interface templates for the archives (serialization)
void swap(WorldPtr< T > &l, WorldPtr< T > &r)
Swap the content of l with r.
Definition: worldptr.h:356
Definition: worldptr.h:50
Default store of a thingy via serialize(ar,t)
Definition: archive.h:708
static void store(const Archive &ar, const detail::WorldPtr< T > &p)
Definition: worldptr.h:395
U & reference
Definition: worldptr.h:51
WorldPtr(World &w, T *p)
World pointer constructor.
Definition: worldptr.h:109
static World * world_from_id(unsigned long id)
Convert world id to world pointer.
Definition: worldfwd.h:628
unsigned long worldidT
World ID type.
Definition: worldptr.h:67
ProcessID owner() const
Rank accessor.
Definition: worldptr.h:301
T * pointer
Definition: worldptr.h:88
worldidT get_worldid() const
World ID accessor.
Definition: worldptr.h:294
bool operator>(const WorldPtr< T > &left, const WorldPtr< U > &right)
Definition: worldptr.h:361
const T1 &f1 return GTEST_2_TUPLE_() T(f0, f1)
ptr_traits< T >::reference reference
Definition: worldptr.h:89
World & get_world() const
World accessor.
Definition: worldptr.h:285
pointer operator->() const
Pointer arrow operator.
Definition: worldptr.h:221
bool operator!() const
Boolean conversion operator.
Definition: worldptr.h:239
Default load of a thingy via serialize(ar,t)
Definition: archive.h:718
bool operator>=(const WorldPtr< T > &left, const WorldPtr< U > &right)
Definition: worldptr.h:371
WorldPtr()
Default constructor.
Definition: worldptr.h:96
bool operator!=(const WorldPtr< U > &other) const
Inequality comparison operator.
Definition: worldptr.h:260
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
ProcessID rank() const
Returns the process rank in this world (same as MPI_Comm_rank()))
Definition: worldfwd.h:526
archive_array< unsigned char > wrap_opaque(const T *, unsigned int)
Factory function to wrap pointer to contiguous data as opaque (uchar) archive_array.
Definition: archive.h:827
bool has_owner() const
Check that the world pointer has an owner.
Definition: worldptr.h:185
WorldPtr(const WorldPtr< T > &other)
Copy constructor.
Definition: worldptr.h:120
A global pointer address, valid anywhere in the world.
Definition: worldptr.h:65
Implements MadnessException.
Implements World.
WorldPtr< T > & operator=(const WorldPtr< U > &other)
Copy conversion assignment operator.
Definition: worldptr.h:165
Holds machinery to set up Functions/FuncImpls using various Factories and Interfaces.
Definition: chem/atomutil.cc:45
reference operator*() const
Dereference operator.
Definition: worldptr.h:207
void swap(mpfr::mpreal &x, mpfr::mpreal &y)
Definition: mpreal.h:3069
void reference
Definition: worldptr.h:56
bool operator==(const WorldPtr< U > &other) const
Equality comparison operator.
Definition: worldptr.h:248
void load_internal_(const Archive &ar)
Serialize/deserialize the world pointer.
Definition: worldptr.h:322
WorldPtr< T > & operator=(const WorldPtr< T > &other)
Copy assignment operator.
Definition: worldptr.h:147
bool is_local() const
Check that the world pointer references a local pointer.
Definition: worldptr.h:179
void swap(WorldPtr< U > &other)
Swap the content of this with other.
Definition: worldptr.h:309