RepastHPC  2.3.1
SharedContext.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  * SharedContext.h
36  *
37  * Created on: Jun 18, 2009
38  * Author: nick
39  */
40 
41 #ifndef SHAREDCONEXT_HPP_
42 #define SHAREDCONEXT_HPP_
43 
44 #include "Context.h"
45 #include "RepastErrors.h"
46 
47 #include <boost/mpi.hpp>
48 #include <exception>
49 
50 namespace repast {
51 
55 template<typename T>
56 struct IsLocalAgent {
57  int rank;
58  IsLocalAgent(int rankInCommunicator): rank(rankInCommunicator){ }
59 
60  bool operator()(const boost::shared_ptr<T>& ptr) {
61  return ptr->getId().currentRank() == rank;
62  }
63 
64 };
65 
69 template<typename T>
71  int rank;
72  bool local;
73  AgentStateFilter(int rankInCommunicator): rank(rankInCommunicator){
74  local = true;
75  }
76  AgentStateFilter(bool localFlag, int rankInCommunicator): rank(rankInCommunicator), local(localFlag) { }
77 
78  bool operator()(const boost::shared_ptr<T>& ptr) {
79  return (local ? ptr->getId().currentRank() == rank : ptr->getId().currentRank() != rank);
80  }
81 };
82 //
84 // * An instance of the AgentStateFilter that filters for local agents
85 // */
86 //template<typename T>
87 //struct LocalFilter: public AgentStateFilter<T>{
88 // LocalFilter(int rank): AgentStateFilter<T>(true, rank){}
89 //};
90 //
92 // * An instance of the AgentStateFilter that filters for nonlocal agents
93 // */
94 //template<typename T>
95 //struct NonLocalFilter: public AgentStateFilter<T>{
96 // NonLocalFilter(int rank): AgentStateFilter<T>(false, rank){}
97 //};
98 //
99 
103 void rpRemoveAgent(const AgentId& id);
104 
105 
114 template<typename T>
115 class SharedContext: public Context<T> {
116 
117 private:
118 
119  typedef typename boost::unordered_map<AgentId, int, HashId> RefMap;
120 
121  // holds reference count to foreign agents that are
122  // referenced by projections. If a projection removes an
123  // agent from a context, this should be checked to make sure
124  // no other projections hold a reference before actually deleting
125  // the agent.
126  RefMap projRefMap;
127  int _rank;
128 
129 public:
130 
131  // Create single instances for these and reuse them
132  IsLocalAgent<T> localPredicate;
133  AgentStateFilter<T> LOCAL_FILTER;
134  AgentStateFilter<T> NON_LOCAL_FILTER;
135 
136  // For more efficient 'push' during projection sync
137  std::vector<std::string> getAgentsToPushProjOrder;
138 
139  typedef typename boost::filter_iterator<IsLocalAgent<T> , typename Context<T>::const_iterator> const_local_iterator;
140 
141  typedef typename boost::filter_iterator<AgentStateFilter<T> , typename Context<T>::const_iterator> const_state_aware_iterator;
142  typedef typename boost::filter_iterator<AgentStateFilter<T> , typename Context<T>::const_bytype_iterator> const_state_aware_bytype_iterator;
143 
144  typedef typename Projection<T>::RADIUS RADIUS;
145 
146  SharedContext(boost::mpi::communicator* comm);
147  virtual ~SharedContext();
148 
156  const_local_iterator localBegin() const;
157 
163  const_local_iterator localEnd() const;
164 
172  void removeAgent(const AgentId id);
173 
181  void removeAgent(T* agent);
182 
190  void importedAgentRemoved(const AgentId& id);
191 
198  void incrementProjRefCount(const AgentId& id);
199 
205  void decrementProjRefCount(const AgentId& id);
206 
207  /*
208  * Used as an argument to the 'selectAgents' routines;
209  * cannot use an 'int' because doing so would mask
210  * the different versions of these routines
211  */
212  enum filterLocalFlag{
213  LOCAL = 1,
214  NON_LOCAL = 0
215  };
216 
217 
218  // Unhide these from the parent class so operation is transparent
219  using Context<T>::begin;
220  using Context<T>::end;
222  using Context<T>::byTypeEnd;
227  using Context<T>::size;
228 
234  const_state_aware_iterator begin(filterLocalFlag local);
235 
241  const_state_aware_iterator end(filterLocalFlag local);
242 
250  const_state_aware_bytype_iterator byTypeBegin(filterLocalFlag local, int type);
251 
259  const_state_aware_bytype_iterator byTypeEnd(filterLocalFlag local, int type);
260 
261 
272  template<typename filterStruct>
273  boost::filter_iterator<filterStruct, typename SharedContext<T>::const_state_aware_iterator> filteredBegin(filterLocalFlag local, filterStruct& fStruct);
274 
285  template<typename filterStruct>
286  boost::filter_iterator<filterStruct, typename SharedContext<T>::const_state_aware_iterator> filteredEnd(filterLocalFlag local, filterStruct& fStruct);
287 
288 
300  template<typename filterStruct>
301  boost::filter_iterator<filterStruct, typename SharedContext<T>::const_state_aware_bytype_iterator> byTypeFilteredBegin(filterLocalFlag local, int type, filterStruct& fStruct);
302 
314  template<typename filterStruct>
315  boost::filter_iterator<filterStruct, typename SharedContext<T>::const_state_aware_bytype_iterator> byTypeFilteredEnd(filterLocalFlag local, int type, filterStruct& fStruct);
316 
317 
318  // Select Methods
319 
320  // Unhide the parent class's versions
322 
323 
342  void selectAgents(filterLocalFlag localOrNonLocalOnly, std::set<T*>& selectedAgents, bool remove = false, int popSize = -1);
343 
362  void selectAgents(filterLocalFlag localOrNonLocalOnly, std::vector<T*>& selectedAgents, bool remove = false, int popSize = -1);
363 
388  void selectAgents(filterLocalFlag localOrNonLocalOnly, int count, std::set<T*>& selectedAgents, bool remove = false, int popSize = -1);
389 
414  void selectAgents(filterLocalFlag localOrNonLocalOnly, int count, std::vector<T*>& selectedAgents, bool remove = false, int popSize = -1);
415 
436  void selectAgents(filterLocalFlag localOrNonLocalOnly, std::set<T*>& selectedAgents, int type, bool remove = false, int popSize = -1);
437 
458  void selectAgents(filterLocalFlag localOrNonLocalOnly, std::vector<T*>& selectedAgents, int type, bool remove = false, int popSize = -1);
459 
485  void selectAgents(filterLocalFlag localOrNonLocalOnly, int count, std::set<T*>& selectedAgents, int type, bool remove = false, int popSize = -1);
486 
512  void selectAgents(filterLocalFlag localOrNonLocalOnly, int count, std::vector<T*>& selectedAgents, int type, bool remove = false, int popSize = -1);
513 
537  template<typename filterStruct>
538  void selectAgents(filterLocalFlag localOrNonLocalOnly, std::set<T*>& selectedAgents, filterStruct& filter, bool remove = false, int popSize = -1);
539 
563  template<typename filterStruct>
564  void selectAgents(filterLocalFlag localOrNonLocalOnly, std::vector<T*>& selectedAgents, filterStruct& filter, bool remove = false, int popSize = -1);
565 
594  template<typename filterStruct>
595  void selectAgents(filterLocalFlag localOrNonLocalOnly, int count, std::set<T*>& selectedAgents, filterStruct& filter, bool remove = false, int popSize = -1);
596 
625  template<typename filterStruct>
626  void selectAgents(filterLocalFlag localOrNonLocalOnly, int count, std::vector<T*>& selectedAgents, filterStruct& filter, bool remove = false, int popSize = -1);
627 
652  template<typename filterStruct>
653  void selectAgents(filterLocalFlag localOrNonLocalOnly, std::set<T*>& selectedAgents, int type, filterStruct& filter, bool remove = false, int popSize = -1);
654 
679  template<typename filterStruct>
680  void selectAgents(filterLocalFlag localOrNonLocalOnly, std::vector<T*>& selectedAgents, int type, filterStruct& filter, bool remove = false, int popSize = -1);
681 
711  template<typename filterStruct>
712  void selectAgents(filterLocalFlag localOrNonLocalOnly, int count, std::set<T*>& selectedAgents, int type, filterStruct& filter, bool remove = false, int popSize = -1);
713 
744  template<typename filterStruct>
745  void selectAgents(filterLocalFlag localOrNonLocalOnly, int count, std::vector<T*>& selectedAgents, int type, filterStruct& filter, bool remove = false, int popSize = -1);
746 
747 
748  // Beta
749 
755  bool keepsAgentsOnSyncProj();
756 
757  bool sendsSecondaryDataOnStatusExchange();
758 
759  void getProjInfoExchangePartners(std::set<int>& sends, std::set<int>& recvs);
760 
761  void getAgentStatusInfoExchangePartners(std::set<int>& sends, std::set<int>& recvs);
762 
767  void getRequiredAgents(std::set<AgentId>& agentsToTest, std::set<AgentId>& agentsToKeep, RADIUS radius = Projection<T>::PRIMARY);
768 
773  void getNonlocalAgentsToDrop(std::set<AgentId>& agentsToKeep, std::set<AgentId>& agentsToDrop, RADIUS radius = Projection<T>::PRIMARY);
774 
775  void getAgentsToPushToOtherProcesses(std::map<int, std::set<AgentId> >& agentsToPush);
776 
777  virtual void addProjection(Projection<T>* projection);
778 
779 };
780 
781 template<typename T>
782 SharedContext<T>::SharedContext(boost::mpi::communicator* comm) : Context<T> (), _rank(comm->rank()), localPredicate(comm->rank()),
783  LOCAL_FILTER(true, comm->rank()),
784  NON_LOCAL_FILTER(false, comm->rank()){
785 }
786 
787 template<typename T>
788 SharedContext<T>::~SharedContext() { }
789 
790 template<typename T>
792  removeAgent(agent->getId());
793 }
794 
795 template<typename T>
797  if (id.currentRank() != _rank) {
798  if (projRefMap.find(id) == projRefMap.end()) {
800  }
801  } else {
803  rpRemoveAgent(id);
804  }
805 }
806 
807 template <typename T>
809  projRefMap.erase(id);
811 }
812 
813 template<typename T>
815  if (id.currentRank() != _rank) {
816  RefMap::iterator iter = projRefMap.find(id);
817  if (iter == projRefMap.end()) {
818  projRefMap[id] = 1;
819  } else {
820  projRefMap[id] = ++(iter->second);
821  }
822  }
823 }
824 
825 template<typename T>
827  if (id.currentRank() == _rank) return;
828 
829  RefMap::iterator iter = projRefMap.find(id);
830  if (iter == projRefMap.end()) throw Repast_Error_31<AgentId>(id); // Id is not in the projection reference map
831 
832  int count = --(iter->second);
833  if (count == 0) projRefMap.erase(iter);
834  else projRefMap[id] = count;
835 
836 }
837 
838 // Local Agents Only
839 
840 template<typename T>
841 boost::filter_iterator<IsLocalAgent<T> , typename Context<T>::const_iterator> SharedContext<T>::localBegin() const {
842  return const_local_iterator(localPredicate, Context<T>::begin(), Context<T>::end());
843 }
844 
845 template<typename T>
846 boost::filter_iterator<IsLocalAgent<T> , typename Context<T>::const_iterator> SharedContext<T>::localEnd() const {
847  return const_local_iterator(localPredicate, Context<T>::end(), Context<T>::end());
848 }
849 
850 
851 // Iterator creation
852 
853 template<typename T>
854 boost::filter_iterator<AgentStateFilter<T> , typename Context<T>::const_iterator> SharedContext<T>::begin(filterLocalFlag local){
855  if(local) return const_state_aware_iterator(LOCAL_FILTER, Context<T>::begin(), Context<T>::end());
856  else return const_state_aware_iterator(NON_LOCAL_FILTER, Context<T>::begin(), Context<T>::end());
857 }
858 
859 template<typename T>
860 boost::filter_iterator<AgentStateFilter<T> , typename Context<T>::const_iterator> SharedContext<T>::end(filterLocalFlag local){
861  if(local) return const_state_aware_iterator(LOCAL_FILTER, Context<T>::end(), Context<T>::end());
862  else return const_state_aware_iterator(NON_LOCAL_FILTER, Context<T>::end(), Context<T>::end());
863 }
864 
865 template<typename T>
866 boost::filter_iterator<AgentStateFilter<T> , typename Context<T>::const_bytype_iterator> SharedContext<T>::byTypeBegin(filterLocalFlag local, int type){
867  if(local) return const_state_aware_bytype_iterator(LOCAL_FILTER, Context<T>::byTypeBegin(type), Context<T>::byTypeEnd(type));
868  else return const_state_aware_bytype_iterator(NON_LOCAL_FILTER, Context<T>::byTypeBegin(type), Context<T>::byTypeEnd(type));
869 }
870 
871 template<typename T>
872 boost::filter_iterator<AgentStateFilter<T> , typename Context<T>::const_bytype_iterator> SharedContext<T>::byTypeEnd(filterLocalFlag local, int type){
873  if(local) return const_state_aware_bytype_iterator(LOCAL_FILTER, Context<T>::byTypeEnd(type), Context<T>::byTypeEnd(type));
874  else return const_state_aware_bytype_iterator(NON_LOCAL_FILTER, Context<T>::byTypeEnd(type), Context<T>::byTypeEnd(type));
875 }
876 
877 template<typename T>
878 template<typename filterStruct>
879 boost::filter_iterator<filterStruct, typename SharedContext<T>::const_state_aware_iterator> SharedContext<T>::filteredBegin(filterLocalFlag local, filterStruct& fStruct){
880  return boost::filter_iterator<filterStruct , typename SharedContext<T>::const_state_aware_iterator> (fStruct, SharedContext<T>::begin(local), SharedContext<T>::end(local));
881 }
882 
883 template<typename T>
884 template<typename filterStruct>
885 boost::filter_iterator<filterStruct, typename SharedContext<T>::const_state_aware_iterator> SharedContext<T>::filteredEnd(filterLocalFlag local, filterStruct& fStruct){
886  return boost::filter_iterator<filterStruct , typename SharedContext<T>::const_state_aware_iterator> (fStruct, SharedContext<T>::end(local), SharedContext<T>::end(local));
887 }
888 
889 
890 template<typename T>
891 template<typename filterStruct>
892 boost::filter_iterator<filterStruct, typename SharedContext<T>::const_state_aware_bytype_iterator> SharedContext<T>::byTypeFilteredBegin(filterLocalFlag local, int type, filterStruct& fStruct){
893  return boost::filter_iterator<filterStruct , typename SharedContext<T>::const_state_aware_bytype_iterator> (fStruct, SharedContext<T>::byTypeBegin(local, type), SharedContext<T>::byTypeEnd(local, type));
894 }
895 
896 template<typename T>
897 template<typename filterStruct>
898 boost::filter_iterator<filterStruct, typename SharedContext<T>::const_state_aware_bytype_iterator> SharedContext<T>::byTypeFilteredEnd(filterLocalFlag local, int type, filterStruct& fStruct){
899  return boost::filter_iterator<filterStruct , typename SharedContext<T>::const_state_aware_bytype_iterator> (fStruct, SharedContext<T>::byTypeEnd(local, type), SharedContext<T>::byTypeEnd(local, type));
900 }
901 
902 
903 // Agent Selection
904 
905 template<typename T>
906 void SharedContext<T>::selectAgents(filterLocalFlag localOrNonLocalOnly, std::set<T*>& selectedAgents, bool remove, int popSize){
907  if(popSize <= -1) selectNElementsAtRandom(begin(localOrNonLocalOnly), end(localOrNonLocalOnly), size(), selectedAgents, remove);
908  else selectNElementsAtRandom(begin(localOrNonLocalOnly), popSize, size(), selectedAgents, remove);
909 }
910 
911 template<typename T>
912 void SharedContext<T>::selectAgents(filterLocalFlag localOrNonLocalOnly, std::vector<T*>& selectedAgents, bool remove, int popSize){
913  if(popSize <= -1) selectNElementsInRandomOrder(begin(localOrNonLocalOnly), end(localOrNonLocalOnly), size(), selectedAgents, remove);
914  else selectNElementsInRandomOrder(begin(localOrNonLocalOnly), popSize, size(), selectedAgents, remove);
915 }
916 
917 template<typename T>
918 void SharedContext<T>::selectAgents(filterLocalFlag localOrNonLocalOnly, int count, std::set<T*>& selectedAgents, bool remove, int popSize){
919  if(popSize <= -1) selectNElementsAtRandom(begin(localOrNonLocalOnly), end(localOrNonLocalOnly), count, selectedAgents, remove);
920  else selectNElementsAtRandom(begin(localOrNonLocalOnly), popSize, count, selectedAgents, remove);
921 }
922 
923 template<typename T>
924 void SharedContext<T>::selectAgents(filterLocalFlag localOrNonLocalOnly, int count, std::vector<T*>& selectedAgents, bool remove, int popSize){
925  if(popSize <= -1) selectNElementsInRandomOrder(begin(localOrNonLocalOnly), end(localOrNonLocalOnly), count, selectedAgents, remove);
926  else selectNElementsInRandomOrder(begin(localOrNonLocalOnly), popSize, count, selectedAgents, remove);
927 }
928 
929 template<typename T>
930 void SharedContext<T>::selectAgents(filterLocalFlag localOrNonLocalOnly, std::set<T*>& selectedAgents, int type, bool remove, int popSize){
931  if(popSize <= -1) selectNElementsAtRandom(byTypeBegin(localOrNonLocalOnly, type), byTypeEnd(localOrNonLocalOnly, type), size(), selectedAgents, remove);
932  else selectNElementsAtRandom(byTypeBegin(localOrNonLocalOnly, type), popSize, size(), selectedAgents, remove);
933 }
934 
935 template<typename T>
936 void SharedContext<T>::selectAgents(filterLocalFlag localOrNonLocalOnly, std::vector<T*>& selectedAgents, int type, bool remove, int popSize){
937  if(popSize <= -1) selectNElementsInRandomOrder(byTypeBegin(localOrNonLocalOnly, type), byTypeEnd(localOrNonLocalOnly, type), size(), selectedAgents, remove);
938  else selectNElementsInRandomOrder(byTypeBegin(localOrNonLocalOnly, type), popSize, size(), selectedAgents, remove);
939 }
940 
941 template<typename T>
942 void SharedContext<T>::selectAgents(filterLocalFlag localOrNonLocalOnly, int count, std::set<T*>& selectedAgents, int type, bool remove, int popSize){
943  if(popSize <= -1) selectNElementsAtRandom(byTypeBegin(localOrNonLocalOnly, type), byTypeEnd(localOrNonLocalOnly, type), count, selectedAgents, remove);
944  else selectNElementsAtRandom(byTypeBegin(localOrNonLocalOnly, type), popSize, count, selectedAgents, remove);
945 }
946 
947 template<typename T>
948 void SharedContext<T>::selectAgents(filterLocalFlag localOrNonLocalOnly, int count, std::vector<T*>& selectedAgents, int type, bool remove, int popSize){
949  if(popSize <= -1) selectNElementsInRandomOrder(byTypeBegin(localOrNonLocalOnly, type), byTypeEnd(localOrNonLocalOnly, type), count, selectedAgents, remove);
950  else selectNElementsInRandomOrder(byTypeBegin(localOrNonLocalOnly, type), popSize, count, selectedAgents, remove);
951 }
952 
953 template<typename T>
954 template<typename filterStruct>
955 void SharedContext<T>::selectAgents(filterLocalFlag localOrNonLocalOnly, std::set<T*>& selectedAgents, filterStruct& filter, bool remove, int popSize){
956  if(popSize <= -1) selectNElementsAtRandom(filteredBegin(localOrNonLocalOnly, filter), filteredEnd(localOrNonLocalOnly, filter), size(), selectedAgents, remove);
957  else selectNElementsAtRandom(filteredBegin(localOrNonLocalOnly, filter), popSize, size(), selectedAgents, remove);
958 }
959 
960 template<typename T>
961 template<typename filterStruct>
962 void SharedContext<T>::selectAgents(filterLocalFlag localOrNonLocalOnly, std::vector<T*>& selectedAgents, filterStruct& filter, bool remove, int popSize){
963  if(popSize <= -1) selectNElementsInRandomOrder(filteredBegin(localOrNonLocalOnly, filter), filteredEnd(localOrNonLocalOnly, filter), size(), selectedAgents, remove);
964  else selectNElementsInRandomOrder(filteredBegin(localOrNonLocalOnly, filter), popSize, size(), selectedAgents, remove);
965 }
966 
967 template<typename T>
968 template<typename filterStruct>
969 void SharedContext<T>::selectAgents(filterLocalFlag localOrNonLocalOnly, int count, std::set<T*>& selectedAgents, filterStruct& filter, bool remove, int popSize){
970  if(popSize <= -1) selectNElementsAtRandom(filteredBegin(localOrNonLocalOnly, filter), filteredEnd(localOrNonLocalOnly, filter), count, selectedAgents, remove);
971  else selectNElementsAtRandom(filteredBegin(localOrNonLocalOnly, filter), popSize, count, selectedAgents, remove);
972 }
973 
974 template<typename T>
975 template<typename filterStruct>
976 void SharedContext<T>::selectAgents(filterLocalFlag localOrNonLocalOnly, int count, std::vector<T*>& selectedAgents, filterStruct& filter, bool remove, int popSize){
977  if(popSize <= -1) selectNElementsInRandomOrder(filteredBegin(localOrNonLocalOnly, filter), filteredEnd(localOrNonLocalOnly, filter), count, selectedAgents, remove);
978  else selectNElementsInRandomOrder(filteredBegin(localOrNonLocalOnly, filter), popSize, count, selectedAgents, remove);
979 }
980 
981 template<typename T>
982 template<typename filterStruct>
983 void SharedContext<T>::selectAgents(filterLocalFlag localOrNonLocalOnly, std::set<T*>& selectedAgents, int type, filterStruct& filter, bool remove, int popSize){
984  if(popSize <= -1) selectNElementsAtRandom(byTypeFilteredBegin(localOrNonLocalOnly, type, filter), byTypeFilteredEnd(localOrNonLocalOnly, type, filter), size(), selectedAgents, remove);
985  else selectNElementsAtRandom(byTypeFilteredBegin(localOrNonLocalOnly, type, filter), popSize, size(), selectedAgents, remove);
986 }
987 
988 template<typename T>
989 template<typename filterStruct>
990 void SharedContext<T>::selectAgents(filterLocalFlag localOrNonLocalOnly, std::vector<T*>& selectedAgents, int type, filterStruct& filter, bool remove, int popSize){
991  if(popSize <= -1) selectNElementsInRandomOrder(byTypeFilteredBegin(localOrNonLocalOnly, type, filter), byTypeFilteredEnd(localOrNonLocalOnly, type, filter), size(), selectedAgents, remove);
992  else selectNElementsInRandomOrder(byTypeFilteredBegin(localOrNonLocalOnly, type, filter), popSize, size(), selectedAgents, remove);
993 }
994 
995 template<typename T>
996 template<typename filterStruct>
997 void SharedContext<T>::selectAgents(filterLocalFlag localOrNonLocalOnly, int count, std::set<T*>& selectedAgents, int type, filterStruct& filter, bool remove, int popSize){
998  if(popSize <= -1) selectNElementsAtRandom(byTypeFilteredBegin(localOrNonLocalOnly, type, filter), byTypeFilteredEnd(localOrNonLocalOnly, type, filter), count, selectedAgents, remove);
999  else selectNElementsAtRandom(byTypeFilteredBegin(localOrNonLocalOnly, type, filter), popSize, count, selectedAgents, remove);
1000 }
1001 
1002 template<typename T>
1003 template<typename filterStruct>
1004 void SharedContext<T>::selectAgents(filterLocalFlag localOrNonLocalOnly, int count, std::vector<T*>& selectedAgents, int type, filterStruct& filter, bool remove, int popSize){
1005  if(popSize <= -1) selectNElementsInRandomOrder(byTypeFilteredBegin(localOrNonLocalOnly, type, filter), byTypeFilteredEnd(localOrNonLocalOnly, type, filter), count, selectedAgents, remove);
1006  else selectNElementsInRandomOrder(byTypeFilteredBegin(localOrNonLocalOnly, type, filter), popSize, count, selectedAgents, remove);
1007 }
1008 
1009 
1010 
1011 
1012 // Beta
1013 
1014 template<typename T>
1016  typename std::vector<Projection<T> *>::iterator iter = Context<T>::projections.begin();
1017  typename std::vector<Projection<T> *>::iterator iterEnd = Context<T>::projections.end();
1018  while((iter != iterEnd)){
1019  if((*iter)->keepsAgentsOnSyncProj()) return true;
1020  iter++;
1021  }
1022  return false;
1023 }
1024 
1025 template<typename T>
1027  for(typename std::vector<Projection<T> *>::iterator iter = Context<T>::projections.begin(), iterEnd = Context<T>::projections.end(); iter != iterEnd; iter++){
1028  if((*iter)->sendsSecondaryAgentsOnStatusExchange()) return true;
1029  }
1030  return false;
1031 }
1032 
1033 template<typename T>
1034 void SharedContext<T>::getProjInfoExchangePartners(std::set<int>& sends, std::set<int>& recvs){
1035  for(typename std::vector<Projection<T> *>::iterator iter = Context<T>::projections.begin(), iterEnd = Context<T>::projections.end(); iter != iterEnd; iter++){
1036  (*iter)->getInfoExchangePartners(sends, recvs);
1037  }
1038 }
1039 
1040 
1041 template<typename T>
1042 void SharedContext<T>::getAgentStatusInfoExchangePartners(std::set<int>& sends, std::set<int>& recvs){
1043  for(typename std::vector<Projection<T> *>::iterator iter = Context<T>::projections.begin(), iterEnd = Context<T>::projections.end(); iter != iterEnd; iter++){
1044  (*iter)->getAgentStatusExchangePartners(sends, recvs);
1045  }
1046 }
1047 
1048 
1049 template<typename T>
1050 void SharedContext<T>::getRequiredAgents(std::set<AgentId>& agentsToTest, std::set<AgentId>& agentsToKeep, RADIUS radius){
1051  typename std::vector<Projection<T> *>::iterator iter = Context<T>::projections.begin();
1052  typename std::vector<Projection<T> *>::iterator iterEnd = Context<T>::projections.end();
1053  while((iter != iterEnd) && (agentsToTest.size() > 0)){
1054  (*iter)->getRequiredAgents(agentsToTest, agentsToKeep, radius);
1055  iter++;
1056  }
1057 }
1058 
1059 template<typename T>
1060 void SharedContext<T>::getNonlocalAgentsToDrop(std::set<AgentId>& agentsToKeep, std::set<AgentId>& agentsToDrop, RADIUS radius){
1061  if(agentsToKeep.size() > 0){
1062  const_state_aware_iterator iter = begin(NON_LOCAL), iterEnd = end(NON_LOCAL);
1063  std::set<AgentId>::iterator notFound = agentsToKeep.end();
1064  while(iter != iterEnd){
1065  AgentId id = (*iter)->getId();
1066  if(agentsToKeep.find(id) == notFound) agentsToDrop.insert((*iter)->getId());
1067  iter++;
1068  }
1069  }
1070  else{
1071  const_state_aware_iterator iter = begin(NON_LOCAL), iterEnd = end(NON_LOCAL);
1072  while(iter != iterEnd){
1073  agentsToDrop.insert((*iter)->getId());
1074  iter++;
1075  }
1076  }
1077  getRequiredAgents(agentsToDrop, agentsToKeep, radius);
1078 }
1079 
1080 template<typename T>
1081 void SharedContext<T>::getAgentsToPushToOtherProcesses(std::map<int, std::set<AgentId> >& agentsToPush){
1082  std::vector<AgentId> tmp;
1083  std::set<AgentId> agentsToTest;
1084  for(const_state_aware_iterator iter = begin(LOCAL), iterEnd = end(LOCAL); iter != iterEnd; ++iter){
1085  tmp.push_back((*iter)->getId());
1086  }
1087  agentsToTest.insert(tmp.begin(), tmp.end());
1088  for(typename std::vector<std::string>::iterator iter = getAgentsToPushProjOrder.begin(), iterEnd = getAgentsToPushProjOrder.end(); iter != iterEnd; iter++){
1089  Context<T>::getProjection(*iter)->getAgentsToPush(agentsToTest, agentsToPush);
1090  }
1091 }
1092 
1093 template<typename T>
1095  int sizeBefore = Context<T>::projections.size();
1096  Context<T>::addProjection(projection);
1097  if(sizeBefore != Context<T>::projections.size()) getAgentsToPushProjOrder.push_back(projection->name());
1098 }
1099 
1100 
1101 
1102 
1103 }
1104 #endif /* SHAREDCONEXT_HPP_ */
repast::SharedContext::incrementProjRefCount
void incrementProjRefCount(const AgentId &id)
Increments the projection reference count for the specified agent.
Definition: SharedContext.h:814
repast::SharedContext::importedAgentRemoved
void importedAgentRemoved(const AgentId &id)
Notifies this context that the specified non-local agent has been removed and this context should the...
Definition: SharedContext.h:808
repast::SharedContext::removeAgent
void removeAgent(const AgentId id)
Removes the specified agent from this context.
Definition: SharedContext.h:796
repast::Context
Collection of agents of type T with set semantics.
Definition: Context.h:82
repast::SharedContext::decrementProjRefCount
void decrementProjRefCount(const AgentId &id)
Decrements the projection reference count for the specified agent.
Definition: SharedContext.h:826
repast::SharedContext::filteredEnd
boost::filter_iterator< filterStruct, typename SharedContext< T >::const_state_aware_iterator > filteredEnd(filterLocalFlag local, filterStruct &fStruct)
Gets the end of an iterator that will iterate over only local or non-local agents meeting the criteri...
Definition: SharedContext.h:885
repast::Context::removeAgent
void removeAgent(const AgentId id)
Removes the specified agent from this context.
Definition: Context.h:908
repast::AgentStateFilter
Used in a filter iterator to filter on local or non-local agents only.
Definition: SharedContext.h:70
repast::Context::getProjection
Projection< T > * getProjection(const std::string &name)
Get the named Projection.
Definition: Context.h:851
repast::SharedContext::getNonlocalAgentsToDrop
void getNonlocalAgentsToDrop(std::set< AgentId > &agentsToKeep, std::set< AgentId > &agentsToDrop, RADIUS radius=Projection< T >::PRIMARY)
Given an initial set of agents that must be kept a priori, add any agents that must be kept due to pr...
Definition: SharedContext.h:1060
repast::AgentId
Agent identity information.
Definition: AgentId.h:60
repast::SharedContext::localEnd
const_local_iterator localEnd() const
Gets the end of an iterator over the local agents in this context.
Definition: SharedContext.h:846
repast::Projection
Abstract base class for all Projections.
Definition: Projection.h:125
repast::Context::addProjection
virtual void addProjection(Projection< T > *projection)
Adds the specified projection to this context.
Definition: Context.h:839
repast::SharedContext
Context implementation specialized for the parallel distributed simulation.
Definition: SharedContext.h:115
repast::SharedContext::keepsAgentsOnSyncProj
bool keepsAgentsOnSyncProj()
Returns true if any of the projections in this context will try to 'keep' non-local agents during a s...
Definition: SharedContext.h:1015
repast::IsLocalAgent
Used in a filter iterator to filter on local agents only.
Definition: SharedContext.h:56
repast::SharedContext::filteredBegin
boost::filter_iterator< filterStruct, typename SharedContext< T >::const_state_aware_iterator > filteredBegin(filterLocalFlag local, filterStruct &fStruct)
Gets the start of an iterator that will iterate over only local or non-local agents meeting the crite...
Definition: SharedContext.h:879
repast::SharedContext::getRequiredAgents
void getRequiredAgents(std::set< AgentId > &agentsToTest, std::set< AgentId > &agentsToKeep, RADIUS radius=Projection< T >::PRIMARY)
Given a set of agents to test, returns the set of those agents that must be kept in order to keep req...
Definition: SharedContext.h:1050
repast::Context::end
const_iterator end() const
Gets the end of an iterator over the agents in this context.
Definition: Context.h:192
repast::SharedContext::byTypeEnd
const_state_aware_bytype_iterator byTypeEnd(filterLocalFlag local, int type)
Gets the end of an iterator that will iterate over only local or non-local agents of a certain type (...
Definition: SharedContext.h:872
repast::Context::size
int size() const
Gets the size (number of agents) in this context.
Definition: Context.h:230
repast::SharedContext::byTypeBegin
const_state_aware_bytype_iterator byTypeBegin(filterLocalFlag local, int type)
Gets the start of an iterator that will iterate over only local or non-local agents of a certain type...
Definition: SharedContext.h:866
repast::SharedContext::localBegin
const_local_iterator localBegin() const
Gets the start of iterator over the local agents in this context.
Definition: SharedContext.h:841
repast::SharedContext::addProjection
virtual void addProjection(Projection< T > *projection)
Adds the specified projection to this context.
Definition: SharedContext.h:1094
repast::SharedContext::selectAgents
void selectAgents(filterLocalFlag localOrNonLocalOnly, std::set< T * > &selectedAgents, bool remove=false, int popSize=-1)
Gets a set of pointers to all local or non-local agents in this context.
Definition: SharedContext.h:906
repast::SharedContext::byTypeFilteredEnd
boost::filter_iterator< filterStruct, typename SharedContext< T >::const_state_aware_bytype_iterator > byTypeFilteredEnd(filterLocalFlag local, int type, filterStruct &fStruct)
Gets the end of an iterator that will iterate over only local or non-local agents of the specified ty...
Definition: SharedContext.h:898
repast::SharedContext::byTypeFilteredBegin
boost::filter_iterator< filterStruct, typename SharedContext< T >::const_state_aware_bytype_iterator > byTypeFilteredBegin(filterLocalFlag local, int type, filterStruct &fStruct)
Gets the start of an iterator that will iterate over only local or non-local agents of the specified ...
Definition: SharedContext.h:892
repast::Projection::name
const std::string name() const
Gets the name of this projection.
Definition: Projection.h:164
repast::Context::begin
const_iterator begin() const
Gets the start of iterator over the agents in this context.
Definition: Context.h:181