FEI  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
fei_LinearDecomposition.hpp
1 #ifndef _fei_LinearDecomposition_hpp_
2 #define _fei_LinearDecomposition_hpp_
3 
4 #include <fei_macros.hpp>
5 #include <fei_CommUtils.hpp>
6 
7 namespace fei {
8 
9 template<typename GlobalIDType>
10 class LinearDecomposition {
11 public:
12  LinearDecomposition(int localProc, int numProcs,
13  GlobalIDType lowest_global_id,
14  GlobalIDType highest_global_id);
15  ~LinearDecomposition() {}
16 
17  GlobalIDType first_locally_owned_global_id() const {return first_locally_owned_global;}
18  GlobalIDType last_locally_owned_global_id() const {return last_locally_owned_global;}
19 
20  GlobalIDType first_global_id() const {return first_global;}
21  GlobalIDType last_global_id() const {return last_global;}
22 
26  int which_proc(GlobalIDType id) const;
27 
28 private:
29  GlobalIDType first_global;
30  GlobalIDType last_global;
31  GlobalIDType first_locally_owned_global;
32  GlobalIDType last_locally_owned_global;
33  std::vector<GlobalIDType> proc_offsets;
34 };//class LinearDecomposition
35 
36 template<typename GlobalIDType>
37 LinearDecomposition<GlobalIDType>::LinearDecomposition(int localProc, int numProcs,
38  GlobalIDType lowest_global_id,
39  GlobalIDType highest_global_id)
40  : first_locally_owned_global(0),
41  last_locally_owned_global(0),
42  proc_offsets()
43 {
44  GlobalIDType num_global = highest_global_id - lowest_global_id + 1;
45  GlobalIDType num_local = num_global/numProcs;
46  GlobalIDType remainder = num_global%numProcs;
47 
48  //First have each entry in proc_offsets contain the number of local ids:
49  proc_offsets.assign(numProcs, num_local);
50  for(GlobalIDType i=0; i<remainder; ++i) {
51  ++proc_offsets[i];
52  }
53 
54  //Now convert proc_offsets so that proc_offsets[i] is the i-th proc's
55  //offset into the global space of ids:
56  GlobalIDType offset = 0;
57  for(size_t i=0; i<proc_offsets.size(); ++i) {
58  GlobalIDType tmp = proc_offsets[i];
59  proc_offsets[i] = offset;
60  offset += tmp;
61  }
62 
63  first_global = lowest_global_id;
64  last_global = highest_global_id;
65  first_locally_owned_global = lowest_global_id + proc_offsets[localProc];
66  last_locally_owned_global = highest_global_id + proc_offsets[localProc] + num_local - 1;
67 }
68 
69 template<typename GlobalIDType>
70 int LinearDecomposition<GlobalIDType>::which_proc(GlobalIDType id) const
71 {
72  if (id < first_global || id > last_global) return -1;
73 
74  for(size_t i=1; i<proc_offsets.size(); ++i) {
75  if (first_global+proc_offsets[i] > id) return i-1;
76  }
77 
78  int last_proc = proc_offsets.size() - 1;
79  return last_proc;
80 }
81 
82 }//namespace fei
83 
84 #endif
85 
int localProc(MPI_Comm comm)
int numProcs(MPI_Comm comm)