Mesh Oriented datABase  (version 5.4.1)
Array-based unstructured mesh datastructure
SharedSetData.cpp
Go to the documentation of this file.
00001 /** \file   SharedSetData.cpp
00002  *  \author Jason Kraftcheck
00003  *  \date   2011-06-23
00004  */
00005 
00006 #include "moab/Interface.hpp"
00007 #include "SharedSetData.hpp"
00008 #include <cassert>
00009 #include <cstdio>
00010 #include <cstdlib>
00011 
00012 namespace moab
00013 {
00014 
00015 SharedSetData::SharedSetData( Interface& moab, int pcID, unsigned rank ) : mb( moab ), sharedSetTag( 0 )
00016 {
00017     SharedSetTagData zero;
00018 
00019     // Zero out any padding bytes in SharedSetTagData (used for memory alignment)
00020     // Otherwise, memcmp could lead to unexpected false negatives for comparison
00021     memset( &zero, 0, sizeof( SharedSetTagData ) );
00022 
00023     zero.ownerRank    = rank;
00024     zero.ownerHandle  = 0;
00025     zero.sharingProcs = NULL;
00026     // band-aid: make sure the tag is unique, to not interfere with different pcID
00027     // maybe a better solution is needed
00028     // pcID can be at most 64, it ranges from 0 to 63; problems appear at migrate mesh
00029     std::ostringstream sharedTagUniqueName;
00030     sharedTagUniqueName << "__sharedSetTag" << pcID;
00031     ErrorCode rval = mb.tag_get_handle( sharedTagUniqueName.str().c_str(), sizeof( SharedSetTagData ), MB_TYPE_OPAQUE,
00032                                         sharedSetTag, MB_TAG_CREAT | MB_TAG_SPARSE, &zero );
00033     assert( MB_SUCCESS == rval );
00034     if( MB_SUCCESS != rval )
00035     {
00036         fprintf( stderr, "Aborted from the constructor of SharedSetData.\n" );
00037         abort();
00038     }
00039 }
00040 
00041 SharedSetData::~SharedSetData()
00042 {
00043     mb.tag_delete( sharedSetTag );
00044 }
00045 
00046 ErrorCode SharedSetData::get_owning_procs( std::vector< unsigned >& ranks_out ) const
00047 {
00048     ranks_out.clear();
00049     ranks_out.reserve( handleMap.size() );
00050     for( RHMap::const_iterator i = handleMap.begin(); i != handleMap.end(); ++i )
00051         ranks_out.push_back( i->first );
00052     return MB_SUCCESS;
00053 }
00054 
00055 ErrorCode SharedSetData::get_sharing_procs( EntityHandle entity_set, std::vector< unsigned >& ranks_out ) const
00056 {
00057     ErrorCode rval;
00058     SharedSetTagData data;
00059     rval = mb.tag_get_data( sharedSetTag, &entity_set, 1, &data );
00060     if( MB_SUCCESS != rval ) return rval;
00061 
00062     ranks_out.clear();
00063     if( data.sharingProcs ) ranks_out = *data.sharingProcs;
00064     return MB_SUCCESS;
00065 }
00066 
00067 ErrorCode SharedSetData::get_shared_sets( Range& sets_out ) const
00068 {
00069     //  sets_out.clear();
00070     //  return mb.get_entities_by_type_and_tag( 0, MBENTITYSET, &sharedSetTag, 1, 0, sets_out );
00071 
00072     sets_out.clear();
00073     for( RHMap::const_iterator i = handleMap.begin(); i != handleMap.end(); ++i )
00074         append_local_handles( i->second, sets_out );
00075     return MB_SUCCESS;
00076 }
00077 
00078 ErrorCode SharedSetData::get_shared_sets( unsigned rank, Range& sets_out ) const
00079 {
00080     sets_out.clear();
00081     //  if (rank == myRank) {
00082     //    return mb.get_entities_by_type_and_tag( 0, MBENTITYSET,
00083     //  }
00084     //  else {
00085     RHMap::const_iterator i = handleMap.find( rank );
00086     if( i != handleMap.end() ) append_local_handles( i->second, sets_out );
00087     return MB_SUCCESS;
00088     //  }
00089 }
00090 
00091 ErrorCode SharedSetData::get_owner( EntityHandle entity_set, unsigned& rank_out, EntityHandle& remote_handle_out ) const
00092 {
00093     ErrorCode rval;
00094     SharedSetTagData data;
00095     rval = mb.tag_get_data( sharedSetTag, &entity_set, 1, &data );
00096     if( MB_SUCCESS != rval ) return rval;
00097 
00098     if( !data.ownerHandle )
00099     {                                  // not shared
00100         assert( !data.sharingProcs );  // really not shared
00101         data.ownerHandle = entity_set;
00102     }
00103 
00104     rank_out          = data.ownerRank;
00105     remote_handle_out = data.ownerHandle;
00106     return MB_SUCCESS;
00107 }
00108 
00109 ErrorCode SharedSetData::get_local_handle( unsigned owner_rank,
00110                                            EntityHandle remote_handle,
00111                                            EntityHandle& local_handle ) const
00112 {
00113     RHMap::const_iterator i = handleMap.find( owner_rank );
00114     assert( i != handleMap.end() );
00115     if( i == handleMap.end() )
00116     {
00117         local_handle = ~(EntityHandle)0;
00118         return MB_FAILURE;
00119     }
00120 
00121     if( !i->second.find( remote_handle, local_handle ) )
00122     {
00123         assert( false );
00124         local_handle = ~(EntityHandle)0;
00125         return MB_FAILURE;
00126     }
00127 
00128     return MB_SUCCESS;
00129 }
00130 
00131 ErrorCode SharedSetData::set_owner( EntityHandle set, unsigned owner_rank, EntityHandle owner_handle )
00132 {
00133     ErrorCode rval;
00134     SharedSetTagData data;
00135     rval = mb.tag_get_data( sharedSetTag, &set, 1, &data );
00136     if( MB_SUCCESS != rval ) return rval;
00137 
00138     if( data.ownerHandle )
00139     {
00140         RHMap::iterator i = handleMap.find( data.ownerRank );
00141         if( i != handleMap.end() )
00142         {
00143             i->second.erase( data.ownerHandle, 1 );
00144         }
00145     }
00146 
00147     data.ownerRank   = owner_rank;
00148     data.ownerHandle = owner_handle;
00149     rval             = mb.tag_set_data( sharedSetTag, &set, 1, &data );
00150     if( MB_SUCCESS != rval ) return rval;
00151 
00152     if( !handleMap[owner_rank].insert( owner_handle, set, 1 ).second )
00153     {
00154         assert( false );
00155         return MB_FAILURE;
00156     }
00157 
00158     return MB_SUCCESS;
00159 }
00160 
00161 ErrorCode SharedSetData::set_sharing_procs( EntityHandle entity_set, std::vector< unsigned >& ranks )
00162 {
00163     std::sort( ranks.begin(), ranks.end() );
00164     RProcMap::iterator it = procListMap.insert( ranks ).first;
00165 
00166     ErrorCode rval;
00167     SharedSetTagData data;
00168     rval = mb.tag_get_data( sharedSetTag, &entity_set, 1, &data );
00169     if( MB_SUCCESS != rval ) return rval;
00170 
00171     data.sharingProcs = &*it;
00172     rval              = mb.tag_set_data( sharedSetTag, &entity_set, 1, &data );
00173     if( MB_SUCCESS != rval ) return rval;
00174 
00175     return MB_SUCCESS;
00176 }
00177 
00178 void SharedSetData::append_local_handles( const ProcHandleMapType& map, Range& range )
00179 {
00180     Range::iterator hint = range.begin();
00181     for( ProcHandleMapType::const_iterator i = map.begin(); i != map.end(); ++i )
00182         hint = range.insert( hint, i->value, i->value + i->count - 1 );
00183 }
00184 
00185 }  // namespace moab
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines