Mesh Oriented datABase  (version 5.5.1)
An array-based unstructured mesh library
SharedSetData.cpp
Go to the documentation of this file.
1 /** \file SharedSetData.cpp
2  * \author Jason Kraftcheck
3  * \date 2011-06-23
4  */
5 
6 #include "moab/Interface.hpp"
7 #include "SharedSetData.hpp"
8 #include <cassert>
9 #include <cstdio>
10 #include <cstdlib>
11 
12 namespace moab
13 {
14 
15 SharedSetData::SharedSetData( Interface& moab, int pcID, unsigned rank ) : mb( moab ), sharedSetTag( 0 )
16 {
17  SharedSetTagData zero;
18 
19  // Zero out any padding bytes in SharedSetTagData (used for memory alignment)
20  // Otherwise, memcmp could lead to unexpected false negatives for comparison
21  memset( &zero, 0, sizeof( SharedSetTagData ) );
22 
23  zero.ownerRank = rank;
24  zero.ownerHandle = 0;
25  zero.sharingProcs = NULL;
26  // band-aid: make sure the tag is unique, to not interfere with different pcID
27  // maybe a better solution is needed
28  // pcID can be at most 64, it ranges from 0 to 63; problems appear at migrate mesh
29  std::ostringstream sharedTagUniqueName;
30  sharedTagUniqueName << "__sharedSetTag" << pcID;
31  ErrorCode rval = mb.tag_get_handle( sharedTagUniqueName.str().c_str(), sizeof( SharedSetTagData ), MB_TYPE_OPAQUE,
33  assert( MB_SUCCESS == rval );
34  if( MB_SUCCESS != rval )
35  {
36  fprintf( stderr, "Aborted from the constructor of SharedSetData.\n" );
37  abort();
38  }
39 }
40 
42 {
44 }
45 
46 ErrorCode SharedSetData::get_owning_procs( std::vector< unsigned >& ranks_out ) const
47 {
48  ranks_out.clear();
49  ranks_out.reserve( handleMap.size() );
50  for( RHMap::const_iterator i = handleMap.begin(); i != handleMap.end(); ++i )
51  ranks_out.push_back( i->first );
52  return MB_SUCCESS;
53 }
54 
55 ErrorCode SharedSetData::get_sharing_procs( EntityHandle entity_set, std::vector< unsigned >& ranks_out ) const
56 {
57  ErrorCode rval;
58  SharedSetTagData data;
59  rval = mb.tag_get_data( sharedSetTag, &entity_set, 1, &data );
60  if( MB_SUCCESS != rval ) return rval;
61 
62  ranks_out.clear();
63  if( data.sharingProcs ) ranks_out = *data.sharingProcs;
64  return MB_SUCCESS;
65 }
66 
68 {
69  // sets_out.clear();
70  // return mb.get_entities_by_type_and_tag( 0, MBENTITYSET, &sharedSetTag, 1, 0, sets_out );
71 
72  sets_out.clear();
73  for( RHMap::const_iterator i = handleMap.begin(); i != handleMap.end(); ++i )
74  append_local_handles( i->second, sets_out );
75  return MB_SUCCESS;
76 }
77 
78 ErrorCode SharedSetData::get_shared_sets( unsigned rank, Range& sets_out ) const
79 {
80  sets_out.clear();
81  // if (rank == myRank) {
82  // return mb.get_entities_by_type_and_tag( 0, MBENTITYSET,
83  // }
84  // else {
85  RHMap::const_iterator i = handleMap.find( rank );
86  if( i != handleMap.end() ) append_local_handles( i->second, sets_out );
87  return MB_SUCCESS;
88  // }
89 }
90 
91 ErrorCode SharedSetData::get_owner( EntityHandle entity_set, unsigned& rank_out, EntityHandle& remote_handle_out ) const
92 {
93  ErrorCode rval;
94  SharedSetTagData data;
95  rval = mb.tag_get_data( sharedSetTag, &entity_set, 1, &data );
96  if( MB_SUCCESS != rval ) return rval;
97 
98  if( !data.ownerHandle )
99  { // not shared
100  assert( !data.sharingProcs ); // really not shared
101  data.ownerHandle = entity_set;
102  }
103 
104  rank_out = data.ownerRank;
105  remote_handle_out = data.ownerHandle;
106  return MB_SUCCESS;
107 }
108 
110  EntityHandle remote_handle,
111  EntityHandle& local_handle ) const
112 {
113  RHMap::const_iterator i = handleMap.find( owner_rank );
114  assert( i != handleMap.end() );
115  if( i == handleMap.end() )
116  {
117  local_handle = ~(EntityHandle)0;
118  return MB_FAILURE;
119  }
120 
121  if( !i->second.find( remote_handle, local_handle ) )
122  {
123  assert( false );
124  local_handle = ~(EntityHandle)0;
125  return MB_FAILURE;
126  }
127 
128  return MB_SUCCESS;
129 }
130 
131 ErrorCode SharedSetData::set_owner( EntityHandle set, unsigned owner_rank, EntityHandle owner_handle )
132 {
133  ErrorCode rval;
134  SharedSetTagData data;
135  rval = mb.tag_get_data( sharedSetTag, &set, 1, &data );
136  if( MB_SUCCESS != rval ) return rval;
137 
138  if( data.ownerHandle )
139  {
140  RHMap::iterator i = handleMap.find( data.ownerRank );
141  if( i != handleMap.end() )
142  {
143  i->second.erase( data.ownerHandle, 1 );
144  }
145  }
146 
147  data.ownerRank = owner_rank;
148  data.ownerHandle = owner_handle;
149  rval = mb.tag_set_data( sharedSetTag, &set, 1, &data );
150  if( MB_SUCCESS != rval ) return rval;
151 
152  if( !handleMap[owner_rank].insert( owner_handle, set, 1 ).second )
153  {
154  assert( false );
155  return MB_FAILURE;
156  }
157 
158  return MB_SUCCESS;
159 }
160 
161 ErrorCode SharedSetData::set_sharing_procs( EntityHandle entity_set, std::vector< unsigned >& ranks )
162 {
163  std::sort( ranks.begin(), ranks.end() );
164  RProcMap::iterator it = procListMap.insert( ranks ).first;
165 
166  ErrorCode rval;
167  SharedSetTagData data;
168  rval = mb.tag_get_data( sharedSetTag, &entity_set, 1, &data );
169  if( MB_SUCCESS != rval ) return rval;
170 
171  data.sharingProcs = &*it;
172  rval = mb.tag_set_data( sharedSetTag, &entity_set, 1, &data );
173  if( MB_SUCCESS != rval ) return rval;
174 
175  return MB_SUCCESS;
176 }
177 
179 {
180  Range::iterator hint = range.begin();
181  for( ProcHandleMapType::const_iterator i = map.begin(); i != map.end(); ++i )
182  hint = range.insert( hint, i->value, i->value + i->count - 1 );
183 }
184 
185 } // namespace moab