MOAB: Mesh Oriented datABase  (version 5.3.1)
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, EntityHandle remote_handle,
00110                                            EntityHandle& local_handle ) const
00111 {
00112     RHMap::const_iterator i = handleMap.find( owner_rank );
00113     assert( i != handleMap.end() );
00114     if( i == handleMap.end() )
00115     {
00116         local_handle = ~(EntityHandle)0;
00117         return MB_FAILURE;
00118     }
00119 
00120     if( !i->second.find( remote_handle, local_handle ) )
00121     {
00122         assert( false );
00123         local_handle = ~(EntityHandle)0;
00124         return MB_FAILURE;
00125     }
00126 
00127     return MB_SUCCESS;
00128 }
00129 
00130 ErrorCode SharedSetData::set_owner( EntityHandle set, unsigned owner_rank, EntityHandle owner_handle )
00131 {
00132     ErrorCode rval;
00133     SharedSetTagData data;
00134     rval = mb.tag_get_data( sharedSetTag, &set, 1, &data );
00135     if( MB_SUCCESS != rval ) return rval;
00136 
00137     if( data.ownerHandle )
00138     {
00139         RHMap::iterator i = handleMap.find( data.ownerRank );
00140         if( i != handleMap.end() ) { i->second.erase( data.ownerHandle, 1 ); }
00141     }
00142 
00143     data.ownerRank   = owner_rank;
00144     data.ownerHandle = owner_handle;
00145     rval             = mb.tag_set_data( sharedSetTag, &set, 1, &data );
00146     if( MB_SUCCESS != rval ) return rval;
00147 
00148     if( !handleMap[owner_rank].insert( owner_handle, set, 1 ).second )
00149     {
00150         assert( false );
00151         return MB_FAILURE;
00152     }
00153 
00154     return MB_SUCCESS;
00155 }
00156 
00157 ErrorCode SharedSetData::set_sharing_procs( EntityHandle entity_set, std::vector< unsigned >& ranks )
00158 {
00159     std::sort( ranks.begin(), ranks.end() );
00160     RProcMap::iterator it = procListMap.insert( ranks ).first;
00161 
00162     ErrorCode rval;
00163     SharedSetTagData data;
00164     rval = mb.tag_get_data( sharedSetTag, &entity_set, 1, &data );
00165     if( MB_SUCCESS != rval ) return rval;
00166 
00167     data.sharingProcs = &*it;
00168     rval              = mb.tag_set_data( sharedSetTag, &entity_set, 1, &data );
00169     if( MB_SUCCESS != rval ) return rval;
00170 
00171     return MB_SUCCESS;
00172 }
00173 
00174 void SharedSetData::append_local_handles( const ProcHandleMapType& map, Range& range )
00175 {
00176     Range::iterator hint = range.begin();
00177     for( ProcHandleMapType::const_iterator i = map.begin(); i != map.end(); ++i )
00178         hint = range.insert( hint, i->value, i->value + i->count - 1 );
00179 }
00180 
00181 }  // namespace moab
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines