Sierra Toolkit  Version of the Day
EntityComm.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_EntityComm_hpp
10 #define stk_mesh_EntityComm_hpp
11 
12 //----------------------------------------------------------------------
13 
14 #include <iosfwd>
15 #include <vector>
16 
17 #include <stk_util/parallel/ParallelComm.hpp>
18 #include <stk_mesh/base/Types.hpp>
19 #include <stk_mesh/base/Ghosting.hpp>
20 
21 #include <boost/unordered_map.hpp>
22 
23 //----------------------------------------------------------------------
24 
25 namespace stk_classic {
26 namespace mesh {
27 
28 class EntityComm
29 {
30 public:
31  typedef boost::unordered_map<EntityKey, EntityCommInfoVector> map_type;
32 
33  PairIterEntityComm sharing( const EntityKey & key ) const;
34  PairIterEntityComm comm( const EntityKey & key ) const;
35  PairIterEntityComm comm( const EntityKey & key, const Ghosting & sub ) const;
36 
37  bool insert( const EntityKey & key, const EntityCommInfo & val );
38  bool erase( const EntityKey & key, const EntityCommInfo & val );
39  bool erase( const EntityKey & key, const Ghosting & ghost );
40  void comm_clear_ghosting(const EntityKey & key );
41  void comm_clear(const EntityKey & key );
42  void comm_swap(const EntityKey & key1, const EntityKey & key2);
43 
44 private:
45  map_type m_comm_map;
46 };
47 
48 inline PairIterEntityComm EntityComm::sharing( const EntityKey & key ) const
49 {
50  map_type::const_iterator it = m_comm_map.find(key);
51  if (it == m_comm_map.cend()) {
52  return PairIterEntityComm();
53  }
54  const EntityCommInfoVector & m_comm = it->second;
55 
56  EntityCommInfoVector::const_iterator i = m_comm.begin();
57  EntityCommInfoVector::const_iterator e = m_comm.end();
58 
59  e = std::lower_bound( i , e , EntityCommInfo(1, // ghost id, 1->aura
60  0 ) ); // proc
61 
62  // Contains everything up the first aura comm (IE, only contains shared comms)
63  return PairIterEntityComm( i , e );
64 }
65 
66 inline PairIterEntityComm EntityComm::comm( const EntityKey & key ) const
67 {
68  map_type::const_iterator it = m_comm_map.find(key);
69  if (it == m_comm_map.cend()) {
70  return PairIterEntityComm();
71  }
72  const EntityCommInfoVector & m_comm = it->second;
73  return PairIterEntityComm(m_comm);
74 }
75 
76 inline PairIterEntityComm EntityComm::comm( const EntityKey & key, const Ghosting & sub ) const
77 {
78  map_type::const_iterator it = m_comm_map.find(key);
79  if (it == m_comm_map.cend()) {
80  return PairIterEntityComm();
81  }
82  const EntityCommInfoVector & m_comm = it->second;
83 
84  const EntityCommInfo s_begin( sub.ordinal() , 0 );
85  const EntityCommInfo s_end( sub.ordinal() + 1 , 0 );
86 
87  EntityCommInfoVector::const_iterator i = m_comm.begin();
88  EntityCommInfoVector::const_iterator e = m_comm.end();
89 
90  i = std::lower_bound( i , e , s_begin );
91  e = std::lower_bound( i , e , s_end );
92 
93  return PairIterEntityComm( i , e );
94 }
95 
96 inline bool EntityComm::insert( const EntityKey & key, const EntityCommInfo & val )
97 {
98  TraceIfWatching("stk_classic::mesh::EntityComm::insert", LOG_ENTITY, key());
99  EntityCommInfoVector & m_comm = m_comm_map[key];
100 
101  std::vector< EntityCommInfo >::iterator i =
102  std::lower_bound( m_comm.begin() , m_comm.end() , val );
103 
104  const bool result = ((i == m_comm.end()) || (val != *i));
105 
106  if ( result ) {
107  m_comm.insert( i , val );
108  }
109 
110  return result ;
111 }
112 
113 inline bool EntityComm::erase( const EntityKey & key, const EntityCommInfo & val )
114 {
115  TraceIfWatching("stk_classic::mesh::EntityComm::erase(comm)", LOG_ENTITY, key());
116  EntityCommInfoVector & m_comm = m_comm_map[key];
117 
118  std::vector< EntityCommInfo >::iterator i =
119  std::lower_bound( m_comm.begin() , m_comm.end() , val );
120 
121  const bool result = ( (i != m_comm.end()) && (val == *i) ) ;
122 
123  if ( result ) {
124  m_comm.erase( i );
125  }
126 
127  return result ;
128 }
129 
130 inline bool EntityComm::erase( const EntityKey & key, const Ghosting & ghost )
131 {
132  TraceIfWatching("stk_classic::mesh::EntityComm::erase(ghost)", LOG_ENTITY, key());
133  EntityCommInfoVector & m_comm = m_comm_map[key];
134 
135  const EntityCommInfo s_begin( ghost.ordinal() , 0 );
136  const EntityCommInfo s_end( ghost.ordinal() + 1 , 0 );
137 
138  EntityCommInfoVector::iterator i = m_comm.begin();
139  EntityCommInfoVector::iterator e = m_comm.end();
140 
141  i = std::lower_bound( i , e , s_begin );
142  e = std::lower_bound( i , e , s_end );
143 
144  const bool result = i != e ;
145 
146  if ( result ) {
147  m_comm.erase( i , e );
148  }
149 
150  return result ;
151 }
152 
153 inline void EntityComm::comm_clear_ghosting(const EntityKey & key)
154 {
155  TraceIfWatching("stk_classic::mesh::EntityComm::comm_clear_ghosting", LOG_ENTITY, key());
156  EntityCommInfoVector & m_comm = m_comm_map[key];
157 
158  std::vector< EntityCommInfo >::iterator j = m_comm.begin();
159  while ( j != m_comm.end() && j->ghost_id == 0 ) { ++j ; }
160  m_comm.erase( j , m_comm.end() );
161 }
162 
163 inline void EntityComm::comm_clear(const EntityKey & key)
164 {
165  TraceIfWatching("stk_classic::mesh::EntityComm::comm_clear", LOG_ENTITY, key());
166  EntityCommInfoVector& commvec = m_comm_map[key];
167  commvec.clear();
168 }
169 
170 inline void EntityComm::comm_swap(const EntityKey & key1, const EntityKey & key2)
171 {
172  map_type::iterator it1 = m_comm_map.find(key1);
173  map_type::iterator it2 = m_comm_map.find(key2);
174 
175  if (it1 == m_comm_map.cend() && it2 == m_comm_map.cend()) {
176  return;
177  }
178 
179  EntityCommInfoVector & comm1 = it1 == m_comm_map.cend() ? m_comm_map[key1] : it1->second;
180  EntityCommInfoVector & comm2 = it2 == m_comm_map.cend() ? m_comm_map[key2] : it2->second;
181 
182  comm1.swap(comm2);
183 }
184 
186 bool in_shared( const Entity & entity );
187 
189 bool in_shared( const Entity & entity , unsigned proc );
190 
192 bool in_receive_ghost( const Entity & entity );
193 
195 bool in_receive_ghost( const Ghosting & ghost , const Entity & entity );
196 
198 bool in_send_ghost( const Entity & entity );
199 
201 bool in_send_ghost( const Entity & entity , unsigned proc );
202 
204 bool in_ghost( const Ghosting & ghost , const Entity & entity , unsigned p );
205 
209 bool in_owned_closure( const Entity & entity , unsigned proc );
210 
212 void comm_procs( const Entity & entity , std::vector<unsigned> & procs );
213 
215 void comm_procs( const Ghosting & ghost ,
216  const Entity & entity , std::vector<unsigned> & procs );
217 
218 //----------------------------------------------------------------------
219 
220 void pack_entity_info( CommBuffer & buf , const Entity & entity );
221 
222 void unpack_entity_info(
223  CommBuffer & buf,
224  const BulkData & mesh ,
225  EntityKey & key ,
226  unsigned & owner ,
227  PartVector & parts ,
228  std::vector<Relation> & relations );
229 
231 void pack_field_values( CommBuffer & , Entity & );
232 
234 bool unpack_field_values( CommBuffer & , Entity & , std::ostream & error_msg );
235 
236 } // namespace mesh
237 } // namespace stk_classic
238 
239 #endif
Sierra Toolkit.
std::vector< Part *> PartVector
Collections of parts are frequently maintained as a vector of Part pointers.
Definition: Types.hpp:31
PairIter< std::vector< EntityCommInfo >::const_iterator > PairIterEntityComm
Span of ( communication-subset-ordinal , process-rank ) pairs for the communication of an entity...
Definition: Types.hpp:128
bool insert(PartVector &v, Part &part)
Insert a part into a properly ordered collection of parts. Returns true if this is a new insertion...
Definition: Part.cpp:85