Galois
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
BasePolicies.h
Go to the documentation of this file.
1 /*
2  * This file belongs to the Galois project, a C++ library for exploiting
3  * parallelism. The code is being released under the terms of the 3-Clause BSD
4  * License (a copy is located in LICENSE.txt at the top-level directory).
5  *
6  * Copyright (C) 2019, The University of Texas at Austin. All rights reserved.
7  * UNIVERSITY EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES CONCERNING THIS
8  * SOFTWARE AND DOCUMENTATION, INCLUDING ANY WARRANTIES OF MERCHANTABILITY,
9  * FITNESS FOR ANY PARTICULAR PURPOSE, NON-INFRINGEMENT AND WARRANTIES OF
10  * PERFORMANCE, AND ANY WARRANTY THAT MIGHT OTHERWISE ARISE FROM COURSE OF
11  * DEALING OR USAGE OF TRADE. NO WARRANTY IS EITHER EXPRESS OR IMPLIED WITH
12  * RESPECT TO THE USE OF THE SOFTWARE OR DOCUMENTATION. Under no circumstances
13  * shall University be liable for incidental, special, indirect, direct or
14  * consequential damages or loss of profits, interruption of business, or
15  * related expenses which may arise from use of Software or Documentation,
16  * including but not limited to those resulting from defects in Software and/or
17  * Documentation, or loss or inaccuracy of data of any kind.
18  */
19 
27 #ifndef _GALOIS_CUSP_PSCAFFOLD_H_
28 #define _GALOIS_CUSP_PSCAFFOLD_H_
29 
30 namespace galois {
31 namespace graphs {
32 
38 protected:
39  uint32_t _hostID;
40  uint32_t _numHosts;
41  uint64_t _numNodes;
42  uint64_t _numEdges;
43  std::vector<std::pair<uint64_t, uint64_t>> _gid2host;
45 
46 public:
55  PartitioningScaffold(uint32_t hostID, uint32_t numHosts, uint64_t numNodes,
56  uint64_t numEdges)
57  : _hostID(hostID), _numHosts(numHosts), _numNodes(numNodes),
58  _numEdges(numEdges) {}
59 
65  void saveGIDToHost(std::vector<std::pair<uint64_t, uint64_t>>& gid2host) {
66  _gid2host = gid2host;
67  }
68 };
69 
75 public:
79  ReadMasterAssignment(uint32_t hostID, uint32_t numHosts, uint64_t numNodes,
80  uint64_t numEdges)
81  : PartitioningScaffold(hostID, numHosts, numNodes, numEdges) {}
82 
90  uint32_t retrieveMaster(uint32_t gid) const {
91  for (auto h = 0U; h < _numHosts; ++h) {
92  uint64_t start, end;
93  std::tie(start, end) = _gid2host[h];
94  if (gid >= start && gid < end) {
95  return h;
96  }
97  }
98  assert(false);
99  return _numHosts;
100  }
101 
102  // below all unused if not assigning masters in default manner, but must be
103  // defined or compiler complains
104 
109  bool masterAssignPhase() const { return false; }
113  void enterStage2() {}
114 
119  template <typename EdgeTy>
121  const std::vector<uint32_t>&,
122  std::unordered_map<uint64_t, uint32_t>&,
123  const std::vector<uint64_t>&,
124  std::vector<galois::CopyableAtomic<uint64_t>>&,
125  const std::vector<uint64_t>&,
126  std::vector<galois::CopyableAtomic<uint64_t>>&) {
127  return 0;
128  }
129 
133  void saveGID2HostInfo(std::unordered_map<uint64_t, uint32_t>&,
134  std::vector<uint32_t>&, uint64_t) {}
139  bool addMasterMapping(uint32_t, uint32_t) { return false; }
140 };
141 
148 protected:
149  char _status;
150  std::vector<uint32_t> _localNodeToMaster;
153  std::unordered_map<uint64_t, uint32_t> _gid2masters;
156  uint64_t _nodeOffset;
157 
163  unsigned getHostReader(uint64_t gid) const {
164  for (auto i = 0U; i < _numHosts; ++i) {
165  uint64_t start, end;
166  std::tie(start, end) = _gid2host[i];
167  if (gid >= start && gid < end) {
168  return i;
169  }
170  }
171  return -1;
172  }
173 
174 public:
176  CustomMasterAssignment(uint32_t hostID, uint32_t numHosts, uint64_t numNodes,
177  uint64_t numEdges)
178  : PartitioningScaffold(hostID, numHosts, numNodes, numEdges), _status(0) {
179  }
180 
189  uint32_t retrieveMaster(uint32_t gid) const {
190  if (_status != 0) {
191  // use map if not a locally read node, else use vector
192  if (getHostReader(gid) != _hostID) {
193  auto gidMasterIter = _gid2masters.find(gid);
194  // found in map
195  if (gidMasterIter != _gid2masters.end()) {
196  uint32_t mappedMaster = gidMasterIter->second;
197  // galois::gDebug("[", _hostID, "] ", gid, " found with master ",
198  // mappedMaster, "!");
199  // make sure host is in bounds
200  assert(mappedMaster < _numHosts);
201  return mappedMaster;
202  } else {
203  // NOT FOUND (not necessarily a bad thing, and required for
204  // some cases)
205  galois::gDebug("[", _hostID, "] ", gid, " not found!");
206  if (_status == 2) {
207  // die if we expect all gids to be mapped already (stage 2)
208  GALOIS_DIE("should not fail to find a GID after stage 2 "
209  "of master assignment phase");
210  }
211  return (uint32_t)-1;
212  }
213  } else {
214  // determine offset
215  uint32_t offsetIntoMap = gid - _nodeOffset;
216  assert(offsetIntoMap != (uint32_t)-1);
217  assert(offsetIntoMap < _localNodeToMaster.size());
218  return _localNodeToMaster[offsetIntoMap];
219  }
220  } else {
221  // stage 0 = this function shouldn't be called
222  GALOIS_DIE("master setup incomplete");
223  return (uint32_t)-1;
224  }
225  }
226 
236  void saveGID2HostInfo(std::unordered_map<uint64_t, uint32_t>& gid2offsets,
237  std::vector<uint32_t>& localNodeToMaster,
238  uint64_t nodeOffset) {
239 #ifndef NDEBUG
240  size_t originalSize = _gid2masters.size();
241 #endif
242 
243  for (auto i = gid2offsets.begin(); i != gid2offsets.end(); i++) {
244  assert(i->second < localNodeToMaster.size());
245  galois::gDebug("Map ", i->first, " to ", localNodeToMaster[i->second]);
246  _gid2masters[i->first] = localNodeToMaster[i->second];
247  }
248  assert(_gid2masters.size() == (originalSize + gid2offsets.size()));
249  // get memory back
250  gid2offsets.clear();
251 
252  size_t myLocalNodes = _gid2host[_hostID].second - _gid2host[_hostID].first;
253  assert((myLocalNodes + _gid2masters.size() - originalSize) ==
254  localNodeToMaster.size());
255  // copy over to this structure
256  _localNodeToMaster = std::move(localNodeToMaster);
257  assert(myLocalNodes <= _localNodeToMaster.size());
258 
259  // resize to fit only this host's read nodes
260  _localNodeToMaster.resize(myLocalNodes);
261  _nodeOffset = nodeOffset;
262 
263  // stage 1 setup complete
264  _status = 1;
265  }
266 
269  bool masterAssignPhase() const { return true; }
271  void enterStage2() { _status = 2; }
272 
281  template <typename EdgeTy>
283  const std::vector<uint32_t>&,
284  std::unordered_map<uint64_t, uint32_t>&,
285  const std::vector<uint64_t>&,
286  std::vector<galois::CopyableAtomic<uint64_t>>&,
287  const std::vector<uint64_t>&,
288  std::vector<galois::CopyableAtomic<uint64_t>>&) {
289  return (uint32_t)-1;
290  }
291 
300  bool addMasterMapping(uint32_t gid, uint32_t mappedMaster) {
301  assert(mappedMaster < _numHosts);
302  if (_status <= 1) {
303  auto offsetIntoMapIter = _gid2masters.find(gid);
304  if (offsetIntoMapIter == _gid2masters.end()) {
305  // NOT FOUND
306  galois::gDebug("[", _hostID, "] ", gid, " not found; mapping!");
307  _gid2masters[gid] = mappedMaster;
308  return true;
309  } else {
310  // already mapped
311  galois::gDebug("[", _hostID, "] ", gid, " already mapped with master ",
312  offsetIntoMapIter->second, "!");
313  assert(offsetIntoMapIter->second == mappedMaster);
314  return false;
315  }
316  } else {
317  GALOIS_DIE("unexpected status in add master mapping: ", _status);
318  return false;
319  }
320  }
321 };
322 
323 } // end namespace graphs
324 } // end namespace galois
325 
326 #endif
void saveGIDToHost(std::vector< std::pair< uint64_t, uint64_t >> &gid2host)
Save a provided map from host to nodes a host has read into this object.
Definition: BasePolicies.h:65
std::unordered_map< uint64_t, uint32_t > _gid2masters
Map GID to its master.
Definition: BasePolicies.h:153
bool addMasterMapping(uint32_t, uint32_t)
Technically doesn&#39;t nothing and should never be called because no master assignment phase...
Definition: BasePolicies.h:139
uint64_t _numEdges
number of edges in graph
Definition: BasePolicies.h:42
uint32_t _numHosts
total number of hosts
Definition: BasePolicies.h:40
Class that loads a portion of a Galois graph from disk directly into memory buffers for access...
Definition: BufferedGraph.h:49
Policies that use the read assignment of nodes as the masters.
Definition: BasePolicies.h:74
void saveGID2HostInfo(std::unordered_map< uint64_t, uint32_t > &gid2offsets, std::vector< uint32_t > &localNodeToMaster, uint64_t nodeOffset)
Given gid to master mapping info, save it into a local map.
Definition: BasePolicies.h:236
void gDebug(Args &&...GALOIS_USED_ONLY_IN_DEBUG(args))
Prints a debug string from a sequence of things; prints nothing if NDEBUG is defined.
Definition: gIO.h:72
uint32_t getMaster(uint32_t, galois::graphs::BufferedGraph< EdgeTy > &, const std::vector< uint32_t > &, std::unordered_map< uint64_t, uint32_t > &, const std::vector< uint64_t > &, std::vector< galois::CopyableAtomic< uint64_t >> &, const std::vector< uint64_t > &, std::vector< galois::CopyableAtomic< uint64_t >> &)
Does nothing because this policy doesn&#39;t have a master assignment phase.
Definition: BasePolicies.h:120
#define GALOIS_DIE(...)
Definition: gIO.h:96
uint32_t retrieveMaster(uint32_t gid) const
Retrieves a saved master mapping: does not fail if a GID mapping is not found but instead returns -1 ...
Definition: BasePolicies.h:189
Class that inherits from std::atomic to make it copyable by defining a copy constructor.
Definition: AtomicWrapper.h:40
uint32_t _hostID
host ID of owner of this object
Definition: BasePolicies.h:39
char _status
Specifies what phase of master assignment partitioner is on.
Definition: BasePolicies.h:149
uint32_t getMaster(uint32_t, galois::graphs::BufferedGraph< EdgeTy > &, const std::vector< uint32_t > &, std::unordered_map< uint64_t, uint32_t > &, const std::vector< uint64_t > &, std::vector< galois::CopyableAtomic< uint64_t >> &, const std::vector< uint64_t > &, std::vector< galois::CopyableAtomic< uint64_t >> &)
CuSP&#39;s &quot;getMaster&quot; function.
Definition: BasePolicies.h:282
uint32_t retrieveMaster(uint32_t gid) const
Returns the host ID of the host that read a particular node and its edges from disk.
Definition: BasePolicies.h:90
bool addMasterMapping(uint32_t gid, uint32_t mappedMaster)
Add a new master mapping to the local map: needs to be in stage 1.
Definition: BasePolicies.h:300
void enterStage2()
Does nothing as this policy doesn&#39;t have a master assignment phase.
Definition: BasePolicies.h:113
CustomMasterAssignment(uint32_t hostID, uint32_t numHosts, uint64_t numNodes, uint64_t numEdges)
Calls parent constructor to initialize common data.
Definition: BasePolicies.h:176
uint64_t _nodeOffset
This host&#39;s node offset (each host reads a distinct contiguous portion of graph.
Definition: BasePolicies.h:156
Policies that use a custom assignment of masters (from the user).
Definition: BasePolicies.h:147
std::vector< std::pair< uint64_t, uint64_t > > _gid2host
maps from host id to nodes that host as read from disk
Definition: BasePolicies.h:44
void enterStage2()
Shifts master assignment phase to stage 2.
Definition: BasePolicies.h:271
bool masterAssignPhase() const
Returns true as policies that inherit from this should define master assignment function.
Definition: BasePolicies.h:269
unsigned getHostReader(uint64_t gid) const
Return the reader of a particular node.
Definition: BasePolicies.h:163
std::vector< uint32_t > _localNodeToMaster
Metadata for determining where a node&#39;s master is.
Definition: BasePolicies.h:151
PartitioningScaffold(uint32_t hostID, uint32_t numHosts, uint64_t numNodes, uint64_t numEdges)
Constructor for Scaffold.
Definition: BasePolicies.h:55
void saveGID2HostInfo(std::unordered_map< uint64_t, uint32_t > &, std::vector< uint32_t > &, uint64_t)
No-op because no master assignment phase.
Definition: BasePolicies.h:133
uint64_t _numNodes
number of nodes in graph
Definition: BasePolicies.h:41
bool masterAssignPhase() const
Returns false as this partitioning policy doesn&#39;t have a master assignment phase. ...
Definition: BasePolicies.h:109
ReadMasterAssignment(uint32_t hostID, uint32_t numHosts, uint64_t numNodes, uint64_t numEdges)
Constructor simply calls parent constructor.
Definition: BasePolicies.h:79
Default fields and functions all CuSP partitioners use; this is a class to inherit from...
Definition: BasePolicies.h:37