41 #ifndef SHAREDBASEGRID_H_
42 #define SHAREDBASEGRID_H_
51 #include "GridComponents.h"
52 #include "MultipleOccupancy.h"
53 #include "RepastProcess.h"
55 #include "SRManager.h"
56 #include "RepastErrors.h"
57 #include "RelativeLocation.h"
58 #include "CartesianTopology.h"
77 int rank()
const {
return _rank; }
89 friend std::ostream& operator<<(std::ostream& os,
const Neighbors& nghs);
93 std::vector<Neighbor*> nghs;
128 void getNeighborRanks(std::set<int>& ranks) {
129 for(std::vector<Neighbor*>::iterator iter = nghs.begin(), iterEnd=nghs.end(); iter != iterEnd; ++iter) {
132 ranks.insert((*iter)->rank());
137 Neighbor* getNeighborByIndex(
int index){
138 if(index < 0|| index >= nghs.size())
return 0;
144 std::ostream& operator<<(std::ostream& os,
const Neighbors& nghs);
167 template<
typename T,
typename GPTransformer,
typename Adder,
typename GPType>
179 std::vector<AgentId> buffered;
186 boost::mpi::communicator* comm;
231 virtual bool moveTo(
const AgentId&
id,
const std::vector<GPType>& newLocation);
236 virtual void removeAgent(T* agent);
240 virtual void getRequiredAgents(std::set<AgentId>& agentsToTest, std::set<AgentId>& agentsRequired){ }
242 virtual void getAgentsToPush(std::set<AgentId>& agentsToTest, std::map<
int, std::set<AgentId> >& agentsToPush);
246 nghs->getNeighborRanks(psToSendTo);
247 nghs->getNeighborRanks(psToReceiveFrom);
251 nghs->getNeighborRanks(psToSendTo);
252 nghs->getNeighborRanks(psToReceiveFrom);
259 template<
typename T,
typename GPTransformer,
typename Adder,
typename GPType>
261 int> processDims,
int buffer, boost::mpi::communicator* communicator) :
262 GridBaseType(name, gridDims), _buffer(buffer), comm(communicator), globalBounds(gridDims) {
264 int dimCount = gridDims.dimensionCount();
265 if (processDims.size() != dimCount)
266 throw Repast_Error_50<GridDimensions>(dimCount, gridDims, processDims.size());
269 bool periodic = GridBaseType::gpTransformer.isPeriodic();
271 cartTopology = RepastProcess::instance()->getCartesianTopology(processDims, periodic);
273 std::vector<int> coords;
274 cartTopology->getCoordinates(rank, coords);
276 localBounds = cartTopology->getDimensions(rank, gridDims);
277 GridBaseType::adder.init(localBounds,
this);
286 int rankOfNeighbor = cartTopology->getRank(coords, currentVal);
287 if(rankOfNeighbor != rank && rankOfNeighbor != MPI_PROC_NULL){
288 Neighbor* ngh =
new Neighbor(rankOfNeighbor, cartTopology->getDimensions(rankOfNeighbor, gridDims));
289 nghs->addNeighbor(ngh, relLoc);
291 }
while(relLoc.increment());
295 template<
typename T,
typename GPTransformer,
typename Adder,
typename GPType>
343 template<
typename T,
typename GPTransformer,
typename Adder,
typename GPType>
344 void SharedBaseGrid<T, GPTransformer, Adder, GPType>::balance() {
345 int r = comm->rank();
346 typename GridBaseType::LocationMapConstIter iterEnd = GridBaseType::locationsEnd();
347 for (
typename GridBaseType::LocationMapConstIter iter = GridBaseType::locationsBegin(); iter != iterEnd; ++iter) {
348 AgentId
id = iter->second->ptr->getId();
349 if(
id.currentRank() == r){
350 Point<GPType> loc = iter->second->point;
351 if(!localBounds.contains(loc)){
352 Neighbor* ngh = nghs->findNeighbor(loc.coords());
353 RepastProcess::instance()->moveAgent(
id, ngh->rank());
359 template<
typename T,
typename GPTransformer,
typename Adder,
typename GPType>
364 template<
typename T,
typename GPTransformer,
typename Adder,
typename GPType>
366 return GridBaseType::moveTo(
id, newLocation);
369 template<
typename T,
typename GPTransformer,
typename Adder,
typename GPType>
371 GridBaseType::removeAgent(agent);
377 template<
typename T,
typename GPTransformer,
typename Adder,
typename GPType>
380 if(_buffer == 0)
return;
382 int numDims = localBounds.dimensionCount();
389 std::vector<double> unbufferedOrigin;
390 std::vector<double> unbufferedExtents;
392 for(
int i = 0; i < numDims; i++){
395 unbufferedOrigin.push_back(localBounds.origin(i) + (hasLeft ? _buffer : 0));
396 unbufferedExtents.push_back(localBounds.extents(i) - (hasLeft ? _buffer : 0) - (hasRight ? _buffer : 0));
407 int* outRanks =
new int[numOutgoing];
410 std::vector<double> bufferOrigin;
411 std::vector<double> bufferExtents;
414 for(
int i = 0; i < numDims; i++){
418 bufferOrigin.push_back(localBounds.origin(i));
419 bufferExtents.push_back(localBounds.extents(i));
423 bufferOrigin.push_back(localBounds.origin(i));
426 bufferOrigin.push_back(localBounds.origin(i) + localBounds.extents(i) - _buffer);
428 bufferExtents.push_back(_buffer);
437 outRanks[index] =nghs->getNeighborByIndex(index)->rank();
444 }
while(relLoc.increment());
448 int r = comm->rank();
449 std::set<AgentId>::iterator idIter = agentsToTest.begin();
450 while(idIter != agentsToTest.end()){
453 if(
id.currentRank() == r){
454 std::vector<GPType> locationVector;
455 GridBaseType::getLocation(
id, locationVector);
457 if(!unbuffered.contains(loc)){
458 for(
int i = 0; i < numOutgoing; i++){
459 if ((outgoing[i] != NULL) && (outgoing[i]->contains(loc))) {
460 agentsToPush[outRanks[i]].insert(
id);
467 std::set<AgentId>::iterator tmp = idIter;
469 agentsToTest.erase(tmp);
480 template<
typename T,
typename GPTransformer,
typename Adder,
typename GPType>
483 synchMoveTo(spip->id, spip->data);