moab
ProcessSet.cpp
Go to the documentation of this file.
00001 #include "ProcessSet.hpp"
00002 
00003 #include <assert.h>
00004 
00005 namespace moab {
00006 
00007 ProcessSet::ProcessSet()
00008 {
00009   this->clear();
00010 }
00011 
00012 ProcessSet::ProcessSet( const unsigned char* psetbits )
00013 {
00014   for ( int i = 0; i < SHARED_PROC_BYTES; ++ i )
00015     this->processes[i] = psetbits[i];
00016 }
00017 
00018 ProcessSet::~ProcessSet()
00019 {
00020 }
00021 
00022 void ProcessSet::unite( const ProcessSet& other )
00023 {
00024   for ( int i = 0; i < SHARED_PROC_BYTES; ++ i )
00025     {
00026     this->processes[i] |= other.processes[i];
00027     }
00028 }
00029 
00030 void ProcessSet::intersect( const ProcessSet& other )
00031 {
00032   for ( int i = 0; i < SHARED_PROC_BYTES; ++ i )
00033     {
00034     this->processes[i] &= other.processes[i];
00035     }
00036 }
00037 
00038 void ProcessSet::clear()
00039 {
00040   memset( this->processes, 0, SHARED_PROC_BYTES );
00041 }
00042 
00048 void ProcessSet::set_process_member( int proc )
00049 {
00050   int byte = proc / 8;
00051   int bitmask = 1 << ( proc % 8 );
00052   this->processes[byte] |= bitmask;
00053 }
00054 
00061 void ProcessSet::set_process_members( const std::vector<int>& procs )
00062 {
00063   for ( std::vector<int>::const_iterator it = procs.begin(); it != procs.end() && *it != -1; ++ it )
00064     {
00065     this->set_process_member( *it );
00066     }
00067 }
00068 
00075 bool ProcessSet::get_process_members( int rank, std::vector<int>& procs )
00076 {
00077   int i = 0;
00078   assert( rank >= 0 );
00079   procs.clear();
00080   bool rank_owner = false;
00081   for ( int byte = 0; byte < SHARED_PROC_BYTES; ++ byte )
00082     {
00083     i = byte * 8;
00084     for ( unsigned char val = this->processes[byte]; val; ++ i, val >>= 1 )
00085       {
00086       if ( val & 0x1 )
00087         {
00088         if ( i != rank )
00089           {
00090           //std::cout << " " << i;
00091           procs.push_back( i );
00092           }
00093         else if ( ! procs.size() )
00094           {
00095           rank_owner = true;
00096           }
00097         }
00098       }
00099     }
00100   for ( i = procs.size(); i < MAX_SHARING_PROCS; ++ i )
00101     {
00102     procs.push_back( -1 ); // pad with invalid values
00103     }
00104   return rank_owner;
00105 }
00106 
00107 bool ProcessSet::is_process_member( int i ) const
00108 {
00109   int byte = i / 8;
00110   int bitmask = 1 << ( i % 8 );
00111   return ( this->processes[byte] & bitmask ) ? true : false;
00112 }
00113 
00114 const unsigned char* ProcessSet::data() const
00115 {
00116   return this->processes;
00117 }
00118 
00119 bool ProcessSet::operator < ( const ProcessSet& other ) const
00120 {
00121   for ( int i = 0; i < SHARED_PROC_BYTES; ++ i )
00122     {
00123     if ( this->processes[i] < other.processes[i] )
00124       return true;
00125     else if ( this->processes[i] > other.processes[i] )
00126       return false;
00127     }
00128   return false; // equality
00129 }
00130 
00131 std::ostream& operator << ( std::ostream& os, const ProcessSet& pset )
00132 {
00133   for ( int i = 0; i < MAX_SHARING_PROCS; ++ i )
00134     {
00135     os << ( pset.is_process_member( i ) ? "1" : "0" );
00136     }
00137   return os;
00138 }
00139 
00140 } // namespace moab
00141 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines