RepastHPC  2.3.1
SharedNetwork.h
1 /*
2  * Repast for High Performance Computing (Repast HPC)
3  *
4  * Copyright (c) 2010 Argonne National Laboratory
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with
8  * or without modification, are permitted provided that the following
9  * conditions are met:
10  *
11  * Redistributions of source code must retain the above copyright notice,
12  * this list of conditions and the following disclaimer.
13  *
14  * Redistributions in binary form must reproduce the above copyright notice,
15  * this list of conditions and the following disclaimer in the documentation
16  * and/or other materials provided with the distribution.
17  *
18  * Neither the name of the Argonne National Laboratory nor the names of its
19  * contributors may be used to endorse or promote products derived from
20  * this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
25  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE TRUSTEES OR
26  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
27  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
28  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
29  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
30  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
31  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
32  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  *
34  *
35  * SharedNetwork.h
36  *
37  * Created on: May 27, 2009
38  * Author: nick
39  */
40 
41 #ifndef SHAREDNETWORK_H_
42 #define SHAREDNETWORK_H_
43 
44 #include <map>
45 #include <utility>
46 #include <set>
47 
48 #include <boost/unordered_set.hpp>
49 #include <boost/serialization/vector.hpp>
50 #include <boost/serialization/utility.hpp>
51 #include <boost/lexical_cast.hpp>
52 #include <boost/mpi.hpp>
53 #include <boost/ptr_container/ptr_vector.hpp>
54 
55 #include "Graph.h"
56 #include "RepastProcess.h"
57 #include "logger.h"
58 #include "Utilities.h"
59 #include "SRManager.h"
60 #include "RepastErrors.h"
61 
62 
63 namespace repast {
64 
65 const int NET_SR_PAIR = 2000;
66 const int NET_RECV_PROC = 2001;
67 const int NET_EDGE_UPDATE = 2002;
68 const int NET_EXPORTERS_A = 2003;
69 const int NET_EXPORTERS_B = 2004;
70 const int NET_EXPORT_REQUESTS = 2005;
71 const int NET_EDGE_SYNC = 2006;
72 const int NET_EDGE_REMOVE_SYNC = 2007;
73 
74 
90 template<typename V, typename E, typename Ec, typename EcM>
91 class SharedNetwork: public Graph<V, E, Ec, EcM> {
92 private:
93 
94  template<typename Vertex, typename Edge, typename AgentContent, typename EdgeContent, typename EdgeManager, typename AgentCreator>
95  friend void createComplementaryEdges(SharedNetwork<Vertex, Edge, EdgeContent, EdgeManager>* net, SharedContext<Vertex>& context,
96  EdgeManager& edgeManager, AgentCreator& creator);
97 
98  template<typename Vertex, typename Edge, typename EdgeContent, typename EdgeManager>
99  friend void synchEdges(SharedNetwork<Vertex, Edge, EdgeContent, EdgeManager>*, EdgeManager&);
100 
101  boost::unordered_set<AgentId, HashId> fAgents;
102  std::vector<boost::shared_ptr<E> > sharedEdges;
103  int rank, worldSize;
104  std::map<int, int> senders;
105  // maps removed edges to the process to inform that the edges
106  // have been deleted
107  std::map<int, std::vector<std::pair<AgentId, AgentId> > > removedEdges;
108 
109 protected:
110 
111  virtual bool addAgent(boost::shared_ptr<V> agent);
112  virtual void removeAgent(V* agent);
113 
114  virtual void doAddEdge(boost::shared_ptr<E> edge);
115 
116 public:
117 
120 
128  SharedNetwork(std::string name, bool directed, EcM* edgeContentMgr);
129  virtual ~SharedNetwork() {
130  }
131 
138  void addSender(int rank);
139 
145  void removeSender(int rank);
146 
147  // doc inherited from Graphs
148  void removeEdge(V* source, V* target);
149 
155  void addEdge(boost::shared_ptr<E> edge);
156 
161  void synchRemovedEdges();
162 
169  virtual bool isMaster(E* e){
170  return (e->usesTargetAsMaster() ? e->target()->getId().currentRank() : e->source()->getId().currentRank()) == rank;
171  }
172 };
173 
174 /* Shared Network Definition */
175 
176 template<typename V, typename E, typename Ec, typename EcM>
177 SharedNetwork<V, E, Ec, EcM>::SharedNetwork(std::string name, bool directed, EcM* edgeContentMgr) :
178  Graph<V, E, Ec, EcM> (name, directed, edgeContentMgr) {
179  rank = RepastProcess::instance()->rank();
180  worldSize = RepastProcess::instance()->worldSize();
181 }
182 
183 template<typename V, typename E, typename Ec, typename EcM>
185  std::map<int, int>::iterator iter = senders.find(rank);
186  if (iter == senders.end()) {
187  senders[rank] = 1;
188  } else {
189  senders[rank] = senders[rank] + 1;
190  }
191 }
192 
193 template<typename V, typename E, typename Ec, typename EcM>
195  std::map<int, int>::iterator iter = senders.find(rank);
196  if (iter == senders.end()) throw Repast_Error_30(rank); // Cannot remove non-existent sender
197 
198  int val = iter->second;
199  if (val == 1)
200  senders.erase(rank);
201  else
202  senders[rank] = val - 1;
203 }
204 
205 template<typename V, typename E, typename Ec, typename EcM>
206 bool SharedNetwork<V, E, Ec, EcM>::addAgent(boost::shared_ptr<V> agent) {
207  AgentId id = agent->getId();
208  if (id.currentRank() != rank) {
209  // add to foreign ids
210  fAgents.insert(id);
211  }
212  return Graph<V, E, Ec, EcM>::addAgent(agent);
213 }
214 
215 template<typename V, typename E, typename Ec, typename EcM>
216 void SharedNetwork<V, E, Ec, EcM>::removeEdge(V* source, V* target) {
217  boost::shared_ptr<E> edge = Graph<V, E, Ec, EcM>::findEdge(source, target);
218  Graph<V, E, Ec, EcM>::removeEdge(source, target);
219 
220 }
221 
222 template<typename V, typename E, typename Ec, typename EcM>
224  AgentId id = agent->getId();
225  if (id.currentRank() != rank) {
226  fAgents.erase(id);
227  }
228  Graph<V, E, Ec, EcM>::removeAgent(agent);
229 }
230 template<typename V, typename E, typename Ec, typename EcM>
231 void SharedNetwork<V, E, Ec, EcM>::addEdge(boost::shared_ptr<E> edge) {
233 }
234 
235 template<typename V, typename E, typename Ec, typename EcM>
236 void SharedNetwork<V, E, Ec, EcM>::doAddEdge(boost::shared_ptr<E> edge) {
238 }
239 
240 }
241 
242 #endif /* SHAREDNETWORK_H_ */
repast::RepastProcess::rank
int rank() const
Gets the rank of this process.
Definition: RepastProcess.h:378
repast::Graph::findEdge
virtual boost::shared_ptr< E > findEdge(V *source, V *target)
Gets the edge between the source and target or 0 if no such edge is found.
Definition: Graph.h:465
repast::SharedNetwork::synchRemovedEdges
void synchRemovedEdges()
Synchronizes any removed edges that are have been copied across processes.
repast::SharedNetwork::addSender
void addSender(int rank)
NON USER API.
Definition: SharedNetwork.h:184
repast::SharedNetwork::removeSender
void removeSender(int rank)
NON USER API Decrements the count of edges that are sent from rank to this network.
Definition: SharedNetwork.h:194
repast::SharedNetwork::SharedNetwork
SharedNetwork(std::string name, bool directed, EcM *edgeContentMgr)
Creates a SharedNetwork with the specified name and whether or not the network is directed.
Definition: SharedNetwork.h:177
repast::AgentId
Agent identity information.
Definition: AgentId.h:60
repast::SharedNetwork::addEdge
void addEdge(boost::shared_ptr< E > edge)
Add an edge to this SharedNetwork.
Definition: SharedNetwork.h:231
repast::Graph::removeEdge
virtual void removeEdge(V *source, V *target)
Removes the edge between source and target from this Graph.
Definition: Graph.h:432
repast::SharedContext
Context implementation specialized for the parallel distributed simulation.
Definition: SharedContext.h:115
repast::RepastProcess::worldSize
int worldSize() const
Gets the number of processes in the world.
Definition: RepastProcess.h:385
repast::RepastProcess::instance
static RepastProcess * instance()
Gets this RepastProcess.
Definition: RepastProcess.cpp:126
repast::SharedNetwork::isMaster
virtual bool isMaster(E *e)
Returns true if this is a master link; will be a master link if its master node is local.
Definition: SharedNetwork.h:169
repast::SharedNetwork
Network implementation that can be shared across processes.
Definition: SharedNetwork.h:91
repast::Graph
Graph / Network implementation where agents are vertices in the graph.
Definition: Graph.h:72
repast::Projection< V >::name
const std::string name() const
Gets the name of this projection.
Definition: Projection.h:164
repast::SharedNetwork::removeEdge
void removeEdge(V *source, V *target)
Removes the edge between source and target from this Graph.
Definition: SharedNetwork.h:216