MADNESS  0.10.1
distributed_id.h
Go to the documentation of this file.
1 /*
2  This file is part of MADNESS.
3 
4  Copyright (C) 2013 Virginia Tech
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 #ifndef MADNESS_WORLD_DISTRIBUTED_ID_H__INCLUDED
33 #define MADNESS_WORLD_DISTRIBUTED_ID_H__INCLUDED
34 
35 /**
36  \file distributed_id.h
37  \brief \todo Description needed.
38  \ingroup parallel_runtime
39 */
40 
41 #include <madness/world/uniqueid.h>
43 #include <madness/world/print.h>
44 
45 namespace madness {
46 
47  /// Distributed ID which is used to identify objects.
48  typedef std::pair<uniqueidT, std::size_t> DistributedID;
49 
50  /// Distributed ID equality comparison operator.
51 
52  /// \param[in] left The first key to compare.
53  /// \param[in] right The second key to compare.
54  /// \return True when \c first and \c second of \c left and \c right are
55  /// equal; otherwise false.
56  inline bool operator==(const DistributedID& left, const DistributedID& right) {
57  return (left.first == right.first) && (left.second == right.second);
58  }
59 
60  /// Distributed ID inequality comparison operator.
61 
62  /// \param[in] left The first key to compare.
63  /// \param[in] right The second key to compare.
64  /// \return True when \c first or \c second of \c left and \c right are
65  /// not equal; otherwise false.
66  inline bool operator!=(const DistributedID& left, const DistributedID& right) {
67  return (left.first != right.first) || (left.second != right.second);
68  }
69 
70  /// Overload redirect to std::ostream to be discoverable via ADL
71  inline std::ostream& operator<<(std::ostream& os, const DistributedID& did) {
72  return madness::operators::operator<<(os, did);
73  }
74 
75  /// Key object that includes the process information.
76 
77  /// \tparam Key The base key type.
78  /// \tparam Tag A type to differentiate key types.
79  template <typename Key, typename Tag = void>
80  class ProcessKey {
81  private:
82  Key key_; ///< The base key type.
83  ProcessID proc_; ///< The process that generated the key.
84 
85  public:
86 
87  /// Default constructor.
88  ProcessKey() : key_(), proc_(-1) { }
89 
90  /// Constructor.
91 
92  /// \param[in] key The base key.
93  /// \param[in] proc The process that generated the key.
94  ProcessKey(const Key& key, const ProcessID proc) :
95  key_(key), proc_(proc)
96  { }
97 
98  /// Copy constructor.
99 
100  /// \param[in] other The key to be copied.
102  key_(other.key_), proc_(other.proc_)
103  { }
104 
105  /// Copy assignment operator.
106 
107  /// \param[in] other The key to be copied.
108  /// \return A reference to this object.
110  key_ = other.key_;
111  proc_ = other.proc_;
112  return *this;
113  }
114 
115  /// Base key accessor.
116 
117  /// \return The base key.
118  const Key& key() const { return key_; }
119 
120  /// Process ID accessor.
121 
122  /// \return The process ID.
123  ProcessID proc() const { return proc_; }
124 
125  /// Equality comparison.
126 
127  /// \param[in] other The key to be compared to this.
128  /// \return True when other key and other process are equal to that of
129  /// this key; otherwise false.
130  bool operator==(const ProcessKey<Key, Tag>& other) const {
131  return ((key_ == other.key_) && (proc_ == other.proc_));
132  }
133 
134  /// Inequality comparison.
135 
136  /// \param[in] other The key to be compared to this.
137  /// \return True when other key or other process are not equal to that
138  /// of this key; otherwise false.
139  bool operator!=(const ProcessKey<Key, Tag>& other) const {
140  return ((key_ != other.key_) || (proc_ != other.proc_));
141  }
142 
143  /// Serialize this key.
144 
145  /// \tparam Archive The archive type.
146  /// \param[in,out] ar The archive object that will serialize this object.
147  template <typename Archive>
148  void serialize(const Archive& ar) {
149  ar & key_ & proc_;
150  }
151 
152  /// Hashing function.
153 
154  /// \param[in] key The key to be hashed.
155  /// \return The hashed key value.
157  Hash<Key> hasher;
158  hashT seed = hasher(key.key_);
159  madness::detail::combine_hash(seed, key.proc_);
160  return seed;
161  }
162 
163  }; // class ProcessKey
164 
165  /// Overload redirect to std::ostream to be discoverable via ADL
166  template <typename Key, typename Tag>
167  inline std::ostream& operator<<(std::ostream& os, const ProcessKey<Key,Tag>& key) {
168  using madness::operators::operator<<;
169  return os << "{" << key.key() << "," << key.proc() << "}";
170  }
171 
172  /// Key object that uses a tag to differentiate keys.
173 
174  /// \tparam Key The base key type.
175  /// \tparam Tag A type to differentiate key types.
176  template <typename Key, typename Tag>
177  class TaggedKey {
178  private:
179  Key key_; ///< The base key type.
180 
181  public:
182 
183  /// Default constructor.
184  TaggedKey() : key_() { }
185 
186  /// Constructor.
187 
188  /// \param[in] key The base key.
189  TaggedKey(const Key& key) : key_(key) { }
190 
191  /// Copy constructor.
192 
193  /// \param[in] other The key to be copied.
194  TaggedKey(const TaggedKey<Key, Tag>& other) : key_(other.key_) { }
195 
196  /// Copy assignment operator.
197 
198  /// \param[in] other The key to be copied.
199  /// \return A reference to this object.
201  key_ = other.key_;
202  return *this;
203  }
204 
205  /// Base key accessor.
206 
207  /// \return The base key.
208  const Key& key() const { return key_; }
209 
210  /// Equality comparison.
211 
212  /// \param[in] other The key to be compared to this.
213  /// \return True when other key and other process are equal to that of
214  /// this key; otherwise false.
215  bool operator==(const TaggedKey<Key, Tag>& other) const {
216  return (key_ == other.key_);
217  }
218 
219  /// Inequality comparison.
220 
221  /// \param[in] other The key to be compared to this.
222  /// \return True when other key or other process are not equal to that
223  /// of this key; otherwise false.
224  bool operator!=(const TaggedKey<Key, Tag>& other) const {
225  return (key_ != other.key_);
226  }
227 
228  /// Serialize this key.
229 
230  /// \tparam Archive The archive type.
231  /// \param[in,out] ar The archive object that will serialize this object.
232  template <typename Archive>
233  void serialize(const Archive& ar) { ar & key_; }
234 
235  /// Hashing function.
236 
237  /// \param[in] key The key to be hashed.
238  /// \return The hashed key value.
240  Hash<Key> hasher;
241  return hasher(key.key_);
242  }
243 
244  }; // class TagKey
245 
246  /// Overload redirect to std::ostream to be discoverable via ADL
247  template <typename Key, typename Tag>
248  inline std::ostream& operator<<(std::ostream& os, const TaggedKey<Key,Tag>& key) {
249  using madness::operators::operator<<;
250  return os << "{" << key.key() << "}";
251  }
252 
253 } // namespace madness
254 
255 namespace std {
256 
257  /// Hash a \c DistributedID.
258 
259  /// \param[in] id The distributed ID to be hashed.
260  /// \return The hash value of \c id.
262  madness::hashT seed = madness::hash_value(id.first);
264 
265  return seed;
266  }
267 
268 } // namespace std
269 
270 #endif // MADNESS_WORLD_DISTRIBUTED_ID_H__INCLUDED
Key is the index for a node of the 2^NDIM-tree.
Definition: key.h:66
Key object that includes the process information.
Definition: distributed_id.h:80
void serialize(const Archive &ar)
Serialize this key.
Definition: distributed_id.h:148
friend hashT hash_value(const ProcessKey< Key, Tag > &key)
Hashing function.
Definition: distributed_id.h:156
const Key & key() const
Base key accessor.
Definition: distributed_id.h:118
Key key_
The base key type.
Definition: distributed_id.h:82
ProcessKey()
Default constructor.
Definition: distributed_id.h:88
ProcessKey(const ProcessKey< Key, Tag > &other)
Copy constructor.
Definition: distributed_id.h:101
ProcessID proc() const
Process ID accessor.
Definition: distributed_id.h:123
ProcessKey< Key, Tag > & operator=(const ProcessKey< Key, Tag > &other)
Copy assignment operator.
Definition: distributed_id.h:109
bool operator==(const ProcessKey< Key, Tag > &other) const
Equality comparison.
Definition: distributed_id.h:130
bool operator!=(const ProcessKey< Key, Tag > &other) const
Inequality comparison.
Definition: distributed_id.h:139
ProcessID proc_
The process that generated the key.
Definition: distributed_id.h:83
ProcessKey(const Key &key, const ProcessID proc)
Constructor.
Definition: distributed_id.h:94
Key object that uses a tag to differentiate keys.
Definition: distributed_id.h:177
bool operator!=(const TaggedKey< Key, Tag > &other) const
Inequality comparison.
Definition: distributed_id.h:224
const Key & key() const
Base key accessor.
Definition: distributed_id.h:208
TaggedKey()
Default constructor.
Definition: distributed_id.h:184
TaggedKey(const Key &key)
Constructor.
Definition: distributed_id.h:189
void serialize(const Archive &ar)
Serialize this key.
Definition: distributed_id.h:233
friend hashT hash_value(const TaggedKey< Key, Tag > &key)
Hashing function.
Definition: distributed_id.h:239
TaggedKey(const TaggedKey< Key, Tag > &other)
Copy constructor.
Definition: distributed_id.h:194
Key key_
The base key type.
Definition: distributed_id.h:179
TaggedKey< Key, Tag > & operator=(const TaggedKey< Key, Tag > &other)
Copy assignment operator.
Definition: distributed_id.h:200
bool operator==(const TaggedKey< Key, Tag > &other) const
Equality comparison.
Definition: distributed_id.h:215
void combine_hash(hashT &seed, hashT hash)
Internal use only.
Definition: worldhash.h:248
std::ostream & operator<<(std::ostream &s, const std::array< T, N > &a)
Output std::array to stream for human consumption.
Definition: array_addons.h:59
File holds all helper structures necessary for the CC_Operator and CC2 class.
Definition: DFParameters.h:10
std::pair< uniqueidT, std::size_t > DistributedID
Distributed ID which is used to identify objects.
Definition: distributed_id.h:48
bool operator!=(const ResponseParameters &p1, const ResponseParameters &p2)
Definition: response_parameters.cpp:17
std::ostream & operator<<(std::ostream &os, const particle< PDIM > &p)
Definition: lowrankfunction.h:397
bool operator==(const ResponseParameters &p1, const ResponseParameters &p2)
Definition: response_parameters.cpp:12
std::size_t hashT
The hash value type.
Definition: worldhash.h:145
madness::hashT hash_value(const std::array< T, N > &a)
Hash std::array with madness hash.
Definition: array_addons.h:78
Definition: mraimpl.h:50
madness::hashT hash_value(const madness::DistributedID &id)
Hash a DistributedID.
Definition: distributed_id.h:261
Defines simple templates for printing to std::cout "a la Python".
Hash functor.
Definition: worldhash.h:233
Defines types used by the parallel runtime.
int ProcessID
Used to clearly identify process number/rank.
Definition: worldtypes.h:43