fei_LinearDecomposition.hpp

00001 #ifndef _fei_LinearDecomposition_hpp_
00002 #define _fei_LinearDecomposition_hpp_
00003 
00004 #include <fei_macros.hpp>
00005 #include <fei_CommUtils.hpp>
00006 
00007 namespace fei {
00008 
00009 template<typename GlobalIDType>
00010 class LinearDecomposition {
00011 public:
00012   LinearDecomposition(int localProc, int numProcs,
00013                       GlobalIDType lowest_global_id,
00014                       GlobalIDType highest_global_id);
00015   ~LinearDecomposition() {}
00016 
00017   GlobalIDType first_local_id() const {return first_local;}
00018   GlobalIDType last_local_id() const {return last_local;}
00019 
00020   GlobalIDType first_global_id() const {return first_global;}
00021   GlobalIDType last_global_id() const {return last_global;}
00022 
00026   int which_proc(GlobalIDType id) const;
00027 
00028 private:
00029   GlobalIDType first_global;
00030   GlobalIDType last_global;
00031   GlobalIDType first_local;
00032   GlobalIDType last_local;
00033   std::vector<GlobalIDType> proc_offsets;
00034 };//class LinearDecomposition
00035 
00036 template<typename GlobalIDType>
00037 LinearDecomposition<GlobalIDType>::LinearDecomposition(int localProc, int numProcs,
00038                                      GlobalIDType lowest_global_id,
00039                                      GlobalIDType highest_global_id)
00040  : first_local(0),
00041    last_local(0),
00042    proc_offsets()
00043 {
00044   GlobalIDType num_global = highest_global_id - lowest_global_id + 1;
00045   GlobalIDType num_local = num_global/numProcs;
00046   GlobalIDType remainder = num_global%numProcs;
00047 
00048   //First have each entry in proc_offsets contain the number of local ids:
00049   proc_offsets.assign(numProcs, num_local);
00050   for(GlobalIDType i=0; i<remainder; ++i) {
00051     ++proc_offsets[i];
00052   }
00053 
00054   //Now convert proc_offsets so that proc_offsets[i] is the i-th proc's
00055   //offset into the global space of ids:
00056   GlobalIDType offset = 0;
00057   for(size_t i=0; i<proc_offsets.size(); ++i) {
00058     GlobalIDType tmp = proc_offsets[i];
00059     proc_offsets[i] = offset;
00060     offset += tmp;
00061   }
00062 
00063   first_global = lowest_global_id;
00064   last_global = highest_global_id;
00065   first_local = lowest_global_id  + proc_offsets[localProc];
00066   last_local  = highest_global_id + proc_offsets[localProc] + num_local - 1;
00067 }
00068 
00069 template<typename GlobalIDType>
00070 int LinearDecomposition<GlobalIDType>::which_proc(GlobalIDType id) const
00071 {
00072   if (id < first_global || id > last_global) return -1;
00073 
00074   for(size_t i=1; i<proc_offsets.size(); ++i) {
00075     if (first_global+proc_offsets[i] > id) return i-1;
00076   }
00077 
00078   int last_proc = proc_offsets.size() - 1;
00079   return last_proc;
00080 }
00081 
00082 }//namespace fei
00083 
00084 #endif
00085 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends
Generated on Wed Apr 13 10:08:23 2011 for FEI by  doxygen 1.6.3