MOAB: Mesh Oriented datABase  (version 5.4.1)
ProcessSet.cpp
Go to the documentation of this file.
00001 #include "ProcessSet.hpp"
00002 
00003 #include <cassert>
00004 
00005 namespace moab
00006 {
00007 
00008 ProcessSet::ProcessSet()
00009 {
00010     this->clear();
00011 }
00012 
00013 ProcessSet::ProcessSet( const unsigned char* psetbits )
00014 {
00015     for( int i = 0; i < SHARED_PROC_BYTES; ++i )
00016         this->processes[i] = psetbits[i];
00017 }
00018 
00019 ProcessSet::~ProcessSet() {}
00020 
00021 void ProcessSet::unite( const ProcessSet& other )
00022 {
00023     for( int i = 0; i < SHARED_PROC_BYTES; ++i )
00024     {
00025         this->processes[i] |= other.processes[i];
00026     }
00027 }
00028 
00029 void ProcessSet::intersect( const ProcessSet& other )
00030 {
00031     for( int i = 0; i < SHARED_PROC_BYTES; ++i )
00032     {
00033         this->processes[i] &= other.processes[i];
00034     }
00035 }
00036 
00037 void ProcessSet::clear()
00038 {
00039     memset( this->processes, 0, SHARED_PROC_BYTES );
00040 }
00041 
00042 /**\brief Add a process to this process set.
00043  *
00044  * This does not verify that \a proc is within the range [0,MAX_SHARING_PROCS[ .
00045  * You are responsible for that.
00046  */
00047 void ProcessSet::set_process_member( int proc )
00048 {
00049     int byte    = proc / 8;
00050     int bitmask = 1 << ( proc % 8 );
00051     this->processes[byte] |= bitmask;
00052 }
00053 
00054 /**\brief Add each process in the input vector to this process set.
00055  *
00056  * This is a convenience routine that calls set_process_member() for each entry in the vector.
00057  * This does not verify that \a proc is within the range [0,MAX_SHARING_PROCS[ .
00058  * You are responsible for that.
00059  */
00060 void ProcessSet::set_process_members( const std::vector< int >& procs )
00061 {
00062     for( std::vector< int >::const_iterator it = procs.begin(); it != procs.end() && *it != -1; ++it )
00063     {
00064         this->set_process_member( *it );
00065     }
00066 }
00067 
00068 /**\brief Retrieve a vector containing processes in this set.
00069  *
00070  * @param [in] rank The rank of the local process. This integer will not be included in the output
00071  * list.
00072  * @param [out] procs The vector in which the list of sharing processes is listed.
00073  * @return   True when \a rank is the owning process and false otherwise.
00074  */
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
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines