Sierra Toolkit  Version of the Day
EntityRepository.hpp
1 /*------------------------------------------------------------------------*/
2 /* Copyright 2010 Sandia Corporation. */
3 /* Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive */
4 /* license for use of this work by or on behalf of the U.S. Government. */
5 /* Export of this program may require a license from the */
6 /* United States Government. */
7 /*------------------------------------------------------------------------*/
8 
9 #ifndef stk_mesh_baseImpl_EntityRepository_hpp
10 #define stk_mesh_baseImpl_EntityRepository_hpp
11 
12 #include <stk_mesh/base/Trace.hpp>
13 
14 // We will use tr1 if we can (not on PGI or pathscale); otherwise, fall back to std map.
15 #if defined(__PGI) || defined(__PATHSCALE__)
16  #define STK_MESH_ENTITYREPOSITORY_MAP_TYPE_TR1 0
17 #else
18  #define STK_MESH_ENTITYREPOSITORY_MAP_TYPE_TR1 0
19 #endif
20 
21 #if STK_MESH_ENTITYREPOSITORY_MAP_TYPE_TR1
22  #include <tr1/unordered_map>
23 #else
24  #include <map>
25 #endif
26 
27 #include <stk_mesh/base/Entity.hpp>
28 
29 
30 #include <boost/pool/pool_alloc.hpp>
31 
32 namespace stk_classic {
33 namespace mesh {
34 namespace impl {
35 
36 class EntityRepository {
37 
38 #if STK_MESH_ENTITYREPOSITORY_MAP_TYPE_TR1
39  struct stk_entity_rep_hash : public std::unary_function< EntityKey, std::size_t >
40  {
41  inline std::size_t
42  operator()(const EntityKey& x) const
43  {
44  return (std::size_t)(x.raw_key());
45  }
46  };
47 
48  typedef std::tr1::unordered_map<EntityKey, Entity*, stk_entity_rep_hash, std::equal_to<EntityKey> > EntityMap;
49 #else
50  typedef std::map<EntityKey,Entity*> EntityMap;
51 #endif
52 
53  public:
54 
55  typedef EntityMap::const_iterator iterator;
56 
57  EntityRepository(bool use_pool)
58  : m_entities(), m_use_pool(use_pool) {}
59 
60  ~EntityRepository();
61 
62  Entity * get_entity( const EntityKey &key ) const;
63 
64  iterator begin() const { return m_entities.begin(); }
65  iterator end() const { return m_entities.end(); }
66 
67  void clean_changes();
68 
69  // Return a pair: the relevant entity, and whether it had to be created
70  // or not. If there was already an active entity, the second item in the
71  // will be false; otherwise it will be true (even if the Entity was present
72  // but marked as destroyed).
73  std::pair<Entity*,bool>
74  internal_create_entity( const EntityKey & key );
75 
79  void log_created_parallel_copy( Entity & e );
80 
87  inline void log_modified(Entity & e) const;
88 
89  inline void set_entity_owner_rank( Entity & e, unsigned owner_rank);
90  inline void set_entity_sync_count( Entity & e, size_t count);
91 
92  inline void comm_clear( Entity & e) const;
93  inline void comm_clear_ghosting( Entity & e) const;
94 
95  bool erase_ghosting( Entity & e, const Ghosting & ghosts) const;
96  bool erase_comm_info( Entity & e, const EntityCommInfo & comm_info) const;
97 
98  bool insert_comm_info( Entity & e, const EntityCommInfo & comm_info) const;
99 
100  void change_entity_bucket( Bucket & b, Entity & e, unsigned ordinal);
101  Bucket * get_entity_bucket ( Entity & e ) const;
102  void destroy_later( Entity & e, Bucket* nil_bucket );
103 
104  bool destroy_relation( Entity & e_from,
105  Entity & e_to,
106  const RelationIdentifier local_id);
107 
108  void declare_relation( Entity & e_from,
109  Entity & e_to,
110  const RelationIdentifier local_id,
111  unsigned sync_count );
112 
113  void update_entity_key(EntityKey key, Entity & entity);
114 
115  private:
116  void internal_expunge_entity( EntityMap::iterator i);
117 
118  Entity* internal_allocate_entity(EntityKey entity_key);
119  Entity* allocate_entity(bool use_pool);
120 
121  EntityMap m_entities;
122  bool m_use_pool;
123 
124  //disable copy constructor and assignment operator
125  EntityRepository(const EntityRepository &);
126  EntityRepository & operator =(const EntityRepository &);
127 };
128 
129 /*---------------------------------------------------------------*/
130 
131 void EntityRepository::set_entity_sync_count( Entity & e, size_t count)
132 {
133  TraceIfWatching("stk_classic::mesh::impl::EntityRepository::set_entity_sync_count", LOG_ENTITY, e.key());
134 
135  e.m_entityImpl.set_sync_count(count);
136 }
137 
138 void EntityRepository::set_entity_owner_rank( Entity & e, unsigned owner_rank)
139 {
140  TraceIfWatching("stk_classic::mesh::impl::EntityRepository::set_entity_owner_rank", LOG_ENTITY, e.key());
141  DiagIfWatching(LOG_ENTITY, e.key(), "new owner: " << owner_rank);
142 
143  bool changed = e.m_entityImpl.set_owner_rank(owner_rank);
144  if ( changed ) {
145  e.m_entityImpl.log_modified_and_propagate();
146  }
147 }
148 
149 void EntityRepository::comm_clear( Entity & e) const
150 {
151  TraceIfWatching("stk_classic::mesh::impl::EntityRepository::comm_clear", LOG_ENTITY, e.key());
152 
153  e.m_entityImpl.comm_clear();
154 }
155 
156 void EntityRepository::comm_clear_ghosting( Entity & e) const
157 {
158  TraceIfWatching("stk_classic::mesh::impl::EntityRepository::comm_clear_ghosting", LOG_ENTITY, e.key());
159 
160  e.m_entityImpl.comm_clear_ghosting();
161 }
162 
163 void EntityRepository::log_modified( Entity & e ) const
164 {
165  TraceIfWatching("stk_classic::mesh::impl::EntityRepository::log_modified", LOG_ENTITY, e.key());
166 
167  e.m_entityImpl.log_modified_and_propagate();
168 }
169 
170 } // namespace impl
171 } // namespace mesh
172 } // namespace stk_classic
173 
174 #endif // stk_mesh_baseImpl_EntityRepository_hpp
Sierra Toolkit.