DIY 3.0 data-parallel out-of-core C++ library

- Main Page
- Modules
- Namespaces
- Classes
- Files
- Examples

- File List

All Classes Namespaces Functions Typedefs Groups Pages
assigner.hpp
1 #ifndef DIY_ASSIGNER_HPP
2 #define DIY_ASSIGNER_HPP
3
4 #include <vector>
5
6 namespace diy
7 {
8 // Derived types should define
9 // int rank(int gid) const
10 // that converts a global block id to a rank that it's assigned to.
11 class Assigner
12 {
13 public:
18 Assigner(int size,
19 int nblocks
20 ):
21 size_(size), nblocks_(nblocks) {}
22
24 int size() const { return size_; }
26 int nblocks() const { return nblocks_; }
28 void set_nblocks(int nblocks) { nblocks_ = nblocks; }
30 virtual void local_gids(int rank, std::vector<int>& gids) const =0;
32 virtual int rank(int gid) const =0;
33
34 private:
35 int size_; // total number of ranks
36 int nblocks_; // total number of blocks
37 };
38
39 class ContiguousAssigner: public Assigner
40 {
41 public:
46 ContiguousAssigner(int size,
47 int nblocks
48 ):
49 Assigner(size, nblocks) {}
50
51 using Assigner::size;
52 using Assigner::nblocks;
53
54 int rank(int gid) const override
55 {
56 int div = nblocks() / size();
57 int mod = nblocks() % size();
58 int r = gid / (div + 1);
59 if (r < mod)
60 {
61 return r;
62 } else
63 {
64 return mod + (gid - (div + 1)*mod)/div;
65 }
66 }
67 inline
68 void local_gids(int rank, std::vector<int>& gids) const override;
69 };
70
71 class RoundRobinAssigner: public Assigner
72 {
73 public:
78 RoundRobinAssigner(int size,
79 int nblocks
80 ):
81 Assigner(size, nblocks) {}
82
83 using Assigner::size;
84 using Assigner::nblocks;
85
86 int rank(int gid) const override { return gid % size(); }
87 inline
88 void local_gids(int rank, std::vector<int>& gids) const override;
89 };
90 }
91
92 void
93 diy::ContiguousAssigner::
94 local_gids(int rank, std::vector<int>& gids) const
95 {
96 int div = nblocks() / size();
97 int mod = nblocks() % size();
98
99 int from, to;
100 if (rank < mod)
101 from = rank * (div + 1);
102 else
103 from = mod * (div + 1) + (rank - mod) * div;
104
105 if (rank + 1 < mod)
106 to = (rank + 1) * (div + 1);
107 else
108 to = mod * (div + 1) + (rank + 1 - mod) * div;
109
110 for (int gid = from; gid < to; ++gid)
111 gids.push_back(gid);
112 }
113
114 void
115 diy::RoundRobinAssigner::
116 local_gids(int rank, std::vector<int>& gids) const
117 {
118 int cur = rank;
119 while (cur < nblocks())
120 {
121 gids.push_back(cur);
122 cur += size();
123 }
124 }
125
126 #endif
diy::RoundRobinAssigner::local_gids
void local_gids(int rank, std::vector< int > &gids) const override
gets the local gids for a given process rank
Definition: assigner.hpp:116
diy::ContiguousAssigner
Definition: assigner.hpp:39
diy::RoundRobinAssigner
Definition: assigner.hpp:71
diy::Assigner::nblocks
int nblocks() const
returns the total number of global blocks
Definition: assigner.hpp:26
diy::Assigner::set_nblocks
void set_nblocks(int nblocks)
sets the total number of global blocks
Definition: assigner.hpp:28
diy::Assigner::rank
virtual int rank(int gid) const =0
returns the process rank of the block with global id gid (need not be local)
diy::Assigner::size
int size() const
returns the total number of process ranks
Definition: assigner.hpp:24
diy::Assigner
Definition: assigner.hpp:11
diy::ContiguousAssigner::rank
int rank(int gid) const override
returns the process rank of the block with global id gid (need not be local)
Definition: assigner.hpp:54
diy::RoundRobinAssigner::rank
int rank(int gid) const override
returns the process rank of the block with global id gid (need not be local)
Definition: assigner.hpp:86
diy::ContiguousAssigner::ContiguousAssigner
ContiguousAssigner(int size, int nblocks)
Assigns blocks to processes in contiguous gid (block global id) order.
Definition: assigner.hpp:46
diy::ContiguousAssigner::local_gids
void local_gids(int rank, std::vector< int > &gids) const override
gets the local gids for a given process rank
Definition: assigner.hpp:94
diy::Assigner::local_gids
virtual void local_gids(int rank, std::vector< int > &gids) const =0
gets the local gids for a given process rank
diy::RoundRobinAssigner::RoundRobinAssigner
RoundRobinAssigner(int size, int nblocks)
Assigns blocks to processes in cyclic or round-robin gid (block global id) order. ...
Definition: assigner.hpp:78
diy::Assigner::Assigner
Assigner(int size, int nblocks)
Manages how blocks are assigned to processes.
Definition: assigner.hpp:18