RepastHPC  2.3.1
SharedDiscreteSpace.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  * SharedDiscreteSpace.h
36  *
37  * Created on: Jul 20, 2010
38  * Author: nick
39  */
40 
41 #ifndef SHAREDDISCRETESPACE_H_
42 #define SHAREDDISCRETESPACE_H_
43 
44 #include <boost/mpi/communicator.hpp>
45 
46 #include "SharedBaseGrid.h"
47 
48 namespace repast {
49 
62 template<typename T, typename GPTransformer, typename Adder>
63 class SharedDiscreteSpace: public SharedBaseGrid<T, GPTransformer, Adder, int> {
64 
65 protected:
66  virtual void synchMoveTo(const AgentId& id, const Point<int>& pt);
67 
68 private:
69 
71 
72 public:
73  virtual ~SharedDiscreteSpace();
74  SharedDiscreteSpace(std::string name, GridDimensions gridDims, std::vector<int> processDims, int buffer, boost::mpi::communicator* communicator);
75 
76 // virtual void getAgentsToPush(std::set<AgentId>& agentsToTest, std::map<int, std::set<AgentId> >& agentsToPush);
77 
78 };
79 
80 template<typename T, typename GPTransformer, typename Adder>
82  std::vector<int> processDims, int buffer, boost::mpi::communicator* communicator) :
83  SharedBaseGrid<T, GPTransformer, Adder, int> (name, gridDims, processDims, buffer, communicator) {
84 }
85 
86 template<typename T, typename GPTransformer, typename Adder>
87 void SharedDiscreteSpace<T, GPTransformer, Adder>::synchMoveTo(const AgentId& id, const Point<int>& pt) {
88  //unlikely chance that agent could have
89  // moved and then "died" and so removed from sending context, in which case
90  // it would never get sent to this grid.
91  if (SharedBaseGridType::GridBaseType::contains(id)) {
92  SharedBaseGridType::GridBaseType::moveTo(id, pt.coords());
93  }
94 }
95 
96 
97 template<typename T, typename GPTransformer, typename Adder>
98 SharedDiscreteSpace<T, GPTransformer, Adder>::~SharedDiscreteSpace() {
99 }
100 
101 
102 //template<typename T, typename GPTransformer, typename Adder>
103 //void SharedDiscreteSpace<T, GPTransformer, Adder>::getAgentsToPush(std::set<AgentId>& agentsToTest, std::map<int, std::set<AgentId> >& agentsToPush){
104 //
105 // int buffer = SharedBaseGrid<T, GPTransformer, Adder, int>::_buffer;
106 // if(buffer == 0) return; // A buffer zone of zero means that no agents will be pushed.
107 //
108 // // The most efficient algorithm will vary, and
109 // // can be impacted by the number of agents vs. the
110 // // number of grid cells and the distribution of agents
111 // // within the space.
112 //
113 //
114 // // This implementation will assume that agents are distributed
115 // // evenly throughout the space and that the space is large
116 // // relative to the number of agents in it. and the buffer
117 // // zones comparatively small.
118 //
119 // // Consequently, the strategy will be to look through the
120 // // cells in the buffer zones, collecting all the
121 // // agents therein.
122 //
123 // // An increase in efficiency can be gained
124 // // by considering the overlapping areas of the buffer zones only
125 // // once (instead of three times)
126 //
127 //
128 // // In a general case, we might not want to do this, but
129 // // for the current configuration, we can make this (perhaps much) more efficient
130 // // this way:
131 // Point<double> localOrigin = SharedBaseGrid<T, GPTransformer, Adder, int>::localBounds.origin();
132 // Point<double> localExtent = SharedBaseGrid<T, GPTransformer, Adder, int>::localBounds.extents();
133 //
134 // int X1 = localOrigin.getX();
135 // int X2 = localOrigin.getX() + buffer;
136 // int X3 = localOrigin.getX() + localExtent.getX() - buffer;
137 // int X4 = localOrigin.getX() + localExtent.getX();
138 //
139 // int Y1 = localOrigin.getY();
140 // int Y2 = localOrigin.getY() + buffer;
141 // int Y3 = localOrigin.getY() + localExtent.getY() - buffer;
142 // int Y4 = localOrigin.getY() + localExtent.getY();
143 //
144 // Neighbor* neighbor;
145 //
146 // neighbor = SharedBaseGrid<T, GPTransformer, Adder, int>::nghs.neighbor(Neighbors::NW);
147 // int NW_rank = (neighbor == 0 ? -1 : neighbor->rank());
148 // neighbor = SharedBaseGrid<T, GPTransformer, Adder, int>::nghs.neighbor(Neighbors::N );
149 // int N_rank = (neighbor == 0 ? -1 : neighbor->rank());
150 // neighbor = SharedBaseGrid<T, GPTransformer, Adder, int>::nghs.neighbor(Neighbors::NE);
151 // int NE_rank = (neighbor == 0 ? -1 : neighbor->rank());
152 // neighbor = SharedBaseGrid<T, GPTransformer, Adder, int>::nghs.neighbor(Neighbors::E );
153 // int E_rank = (neighbor == 0 ? -1 : neighbor->rank());
154 // neighbor = SharedBaseGrid<T, GPTransformer, Adder, int>::nghs.neighbor(Neighbors::SE);
155 // int SE_rank = (neighbor == 0 ? -1 : neighbor->rank());
156 // neighbor = SharedBaseGrid<T, GPTransformer, Adder, int>::nghs.neighbor(Neighbors::S );
157 // int S_rank = (neighbor == 0 ? -1 : neighbor->rank());
158 // neighbor = SharedBaseGrid<T, GPTransformer, Adder, int>::nghs.neighbor(Neighbors::SW);
159 // int SW_rank = (neighbor == 0 ? -1 : neighbor->rank());
160 // neighbor = SharedBaseGrid<T, GPTransformer, Adder, int>::nghs.neighbor(Neighbors::W );
161 // int W_rank = (neighbor == 0 ? -1 : neighbor->rank());
162 //
163 //
164 // std::set<AgentId> NW_set;
165 // std::set<AgentId> N_set;
166 // std::set<AgentId> NE_set;
167 // std::set<AgentId> E_set;
168 // std::set<AgentId> SE_set;
169 // std::set<AgentId> S_set;
170 // std::set<AgentId> SW_set;
171 // std::set<AgentId> W_set;
172 //
173 //
174 // // NW
175 // for(int x = X1; x < X2; x++){
176 // for(int y = Y1; y < Y2; y++){
177 // Point<int> pt(x, y);
178 // std::vector<T*> out;
179 // SharedBaseGridType::GridBaseType::getObjectsAt(pt, out);
180 // for (size_t i = 0, n = out.size(); i < n; ++i) {
181 // AgentId id = out[i]->getId();
182 // agentsToTest.erase(id);
183 // NW_set.insert(id);
184 // }
185 // }
186 // }
187 // // N
188 // for(int x = X2; x < X3; x++){
189 // for(int y = Y1; y < Y2; y++){
190 // Point<int> pt(x, y);
191 // std::vector<T*> out;
192 // SharedBaseGridType::GridBaseType::getObjectsAt(pt, out);
193 // for (size_t i = 0, n = out.size(); i < n; ++i) {
194 // AgentId id = out[i]->getId();
195 // agentsToTest.erase(id);
196 // N_set.insert(id);
197 // }
198 // }
199 // }
200 // // NE
201 // for(int x = X3; x < X4; x++){
202 // for(int y = Y1; y < Y2; y++){
203 // Point<int> pt(x, y);
204 // std::vector<T*> out;
205 // SharedBaseGridType::GridBaseType::getObjectsAt(pt, out);
206 // for (size_t i = 0, n = out.size(); i < n; ++i) {
207 // AgentId id = out[i]->getId();
208 // agentsToTest.erase(id);
209 // NE_set.insert(id);
210 // }
211 // }
212 // }
213 // // E
214 // for(int x = X3; x < X4; x++){
215 // for(int y = Y2; y < Y3; y++){
216 // Point<int> pt(x, y);
217 // std::vector<T*> out;
218 // SharedBaseGridType::GridBaseType::getObjectsAt(pt, out);
219 // for (size_t i = 0, n = out.size(); i < n; ++i) {
220 // AgentId id = out[i]->getId();
221 // agentsToTest.erase(id);
222 // E_set.insert(id);
223 // }
224 // }
225 // }
226 // // SE
227 // for(int x = X3; x < X4; x++){
228 // for(int y = Y3; y < Y4; y++){
229 // Point<int> pt(x, y);
230 // std::vector<T*> out;
231 // SharedBaseGridType::GridBaseType::getObjectsAt(pt, out);
232 // for (size_t i = 0, n = out.size(); i < n; ++i) {
233 // AgentId id = out[i]->getId();
234 // agentsToTest.erase(id);
235 // SE_set.insert(id);
236 // }
237 // }
238 // }
239 // // S
240 // for(int x = X2; x < X3; x++){
241 // for(int y = Y3; y < Y4; y++){
242 // Point<int> pt(x, y);
243 // std::vector<T*> out;
244 // SharedBaseGridType::GridBaseType::getObjectsAt(pt, out);
245 // for (size_t i = 0, n = out.size(); i < n; ++i) {
246 // AgentId id = out[i]->getId();
247 // agentsToTest.erase(id);
248 // S_set.insert(id);
249 // }
250 // }
251 // }
252 // // SW
253 // for(int x = X1; x < X2; x++){
254 // for(int y = Y3; y < Y4; y++){
255 // Point<int> pt(x, y);
256 // std::vector<T*> out;
257 // SharedBaseGridType::GridBaseType::getObjectsAt(pt, out);
258 // for (size_t i = 0, n = out.size(); i < n; ++i) {
259 // AgentId id = out[i]->getId();
260 // agentsToTest.erase(id);
261 // SW_set.insert(id);
262 // }
263 // }
264 // }
265 // // W
266 // for(int x = X1; x < X2; x++){
267 // for(int y = Y2; y < Y3; y++){
268 // Point<int> pt(x, y);
269 // std::vector<T*> out;
270 // SharedBaseGridType::GridBaseType::getObjectsAt(pt, out);
271 // for (size_t i = 0, n = out.size(); i < n; ++i) {
272 // AgentId id = out[i]->getId();
273 // agentsToTest.erase(id);
274 // W_set.insert(id);
275 // }
276 // }
277 // }
278 //
279 // if(NW_set.size() > 0){
280 // if(W_rank > -1) agentsToPush[ W_rank].insert(NW_set.begin(), NW_set.end());
281 // if(NW_rank > -1) agentsToPush[NW_rank].insert(NW_set.begin(), NW_set.end());
282 // if(N_rank > -1) agentsToPush[ N_rank].insert(NW_set.begin(), NW_set.end());
283 // }
284 // if( N_set.size() > 0){
285 // if(N_rank > -1) agentsToPush[ N_rank].insert( N_set.begin(), N_set.end());
286 // }
287 // if(NE_set.size() > 0){
288 // if(N_rank > -1) agentsToPush[ N_rank].insert(NE_set.begin(), NE_set.end());
289 // if(NE_rank > -1) agentsToPush[NE_rank].insert(NE_set.begin(), NE_set.end());
290 // if(E_rank > -1) agentsToPush[ E_rank].insert(NE_set.begin(), NE_set.end());
291 // }
292 // if( E_set.size() > 0){
293 // if(E_rank > -1) agentsToPush[ E_rank].insert( E_set.begin(), E_set.end());
294 // }
295 // if(SE_set.size() > 0){
296 // if(E_rank > -1) agentsToPush[ E_rank].insert(SE_set.begin(), SE_set.end());
297 // if(SE_rank > -1) agentsToPush[SE_rank].insert(SE_set.begin(), SE_set.end());
298 // if(S_rank > -1) agentsToPush[ S_rank].insert(SE_set.begin(), SE_set.end());
299 // }
300 // if( S_set.size() > 0){
301 // if(S_rank > -1) agentsToPush[ S_rank].insert( S_set.begin(), S_set.end());
302 // }
303 // if(SW_set.size() > 0){
304 // if(S_rank > -1) agentsToPush[ S_rank].insert(SW_set.begin(), SW_set.end());
305 // if(SW_rank > -1) agentsToPush[SW_rank].insert(SW_set.begin(), SW_set.end());
306 // if(W_rank > -1) agentsToPush[ W_rank].insert(SW_set.begin(), SW_set.end());
307 // }
308 // if( W_set.size() > 0){
309 // if(W_rank > -1) agentsToPush[ W_rank].insert( W_set.begin(), W_set.end());
310 // }
311 //
312 //}
313 
314 
315 
316 
317 }
318 
319 #endif /* SHAREDDISCRETESPACE_H_ */
repast::SharedBaseGrid
Grid / Space implementation specialized for the distributed context.
Definition: SharedBaseGrid.h:168
repast::AgentId
Agent identity information.
Definition: AgentId.h:60
repast::GridDimensions
Basic structure for specifying grid dimenions.
Definition: GridDimensions.h:58
repast::SharedDiscreteSpace
Discrete matrix-like SharedBaseGrid implementation.
Definition: SharedDiscreteSpace.h:63
repast::Point< int >
repast::Projection::name
const std::string name() const
Gets the name of this projection.
Definition: Projection.h:164