Skip to content

Commit 1a7c71d

Browse files
committed
Merge pull request #4 from erikzenker/topic-randdis
New Mapping : Random
2 parents 234edc8 + 3b01c11 commit 1a7c71d

File tree

2 files changed

+94
-48
lines changed

2 files changed

+94
-48
lines changed

include/mapping.hpp

Lines changed: 93 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <algorithm> /* std::min */
44
#include <vector> /* std::vector */
55
#include <assert.h> /* assert */
6+
#include <stdlib.h> /* srand, rand */
67

78
/*******************************************************************************
89
*
@@ -12,74 +13,119 @@
1213

1314
namespace graybat {
1415

15-
namespace mapping {
16+
namespace mapping {
1617

1718

18-
struct Roundrobin {
19-
20-
template<typename T_Graph>
21-
std::vector<typename T_Graph::Vertex> operator()(const unsigned processID, const unsigned processCount, T_Graph &graph){
22-
typedef typename T_Graph::Vertex Vertex;
23-
19+
/**
20+
* Random distribution of vertices of the *graph* to the
21+
* the peers. All peers need to set the same random *seed*.
22+
* Thus, all peers have the same random base. Therefore
23+
* seeds that depend on varying parameters like time or
24+
* pid or not applicable here.
25+
*
26+
* @param seed static random seed for all peers
27+
* @return random set of vertices of the *graph*
28+
*
29+
*/
30+
struct Random {
2431

25-
// Distribute and announce vertices
26-
unsigned vertexCount = graph.getVertices().size();
27-
unsigned maxVertex = ceil((float)vertexCount / processCount);
32+
Random(int seed)
33+
: seed(seed){
2834

29-
std::vector<Vertex> myVertices;
30-
if(processID < processCount){
31-
for(unsigned i = 0; i < maxVertex; ++i){
32-
unsigned vertex_i = processID + (i * processCount);
33-
if(vertex_i >= vertexCount){
34-
break;
35-
}
36-
else {
37-
myVertices.push_back(graph.getVertices().at(vertex_i));
35+
}
36+
37+
template<typename T_Graph>
38+
std::vector<typename T_Graph::Vertex> operator()(const unsigned processID, const unsigned processCount, T_Graph &graph){
39+
40+
typedef typename T_Graph::Vertex Vertex;
41+
42+
srand(seed);
43+
std::vector<Vertex> myVertices;
44+
45+
for(Vertex v: graph.getVertices()){
46+
unsigned randomID = rand() % processCount;
47+
if(randomID == processID){
48+
myVertices.push_back(v);
3849
}
39-
50+
4051
}
52+
53+
54+
return myVertices;
4155
}
42-
return myVertices;
56+
57+
private:
58+
int seed;
59+
60+
};
61+
62+
63+
64+
struct Roundrobin {
65+
66+
template<typename T_Graph>
67+
std::vector<typename T_Graph::Vertex> operator()(const unsigned processID, const unsigned processCount, T_Graph &graph){
68+
typedef typename T_Graph::Vertex Vertex;
69+
70+
71+
// Distribute and announce vertices
72+
unsigned vertexCount = graph.getVertices().size();
73+
unsigned maxVertex = ceil((float)vertexCount / processCount);
74+
75+
std::vector<Vertex> myVertices;
76+
if(processID < processCount){
77+
for(unsigned i = 0; i < maxVertex; ++i){
78+
unsigned vertex_i = processID + (i * processCount);
79+
if(vertex_i >= vertexCount){
80+
break;
81+
}
82+
else {
83+
myVertices.push_back(graph.getVertices().at(vertex_i));
84+
}
4385

44-
}
86+
}
87+
}
88+
return myVertices;
89+
90+
}
4591

46-
};
92+
};
4793

48-
struct Consecutive {
94+
struct Consecutive {
4995

50-
template<typename T_Graph>
51-
std::vector<typename T_Graph::Vertex> operator()(const unsigned processID, const unsigned processCount, T_Graph &graph){
96+
template<typename T_Graph>
97+
std::vector<typename T_Graph::Vertex> operator()(const unsigned processID, const unsigned processCount, T_Graph &graph){
5298

53-
typedef typename T_Graph::Vertex Vertex;
99+
typedef typename T_Graph::Vertex Vertex;
54100

55-
unsigned vertexCount = graph.getVertices().size();
56-
unsigned vertexPerProcess = ceil((float)vertexCount / processCount);
101+
unsigned vertexCount = graph.getVertices().size();
102+
unsigned vertexPerProcess = ceil((float)vertexCount / processCount);
57103

58-
// More processes than vertices
59-
if(processID > vertexCount - 1){
60-
return std::vector<Vertex>(0);
61-
}
104+
// More processes than vertices
105+
if(processID > vertexCount - 1){
106+
return std::vector<Vertex>(0);
107+
}
62108

63-
unsigned minVertex = processID * vertexPerProcess;
64-
unsigned maxVertex = minVertex + vertexPerProcess;
109+
unsigned minVertex = processID * vertexPerProcess;
110+
unsigned maxVertex = minVertex + vertexPerProcess;
65111

66-
// Slice maxVertex of last process
67-
if(minVertex > vertexCount){
68-
return std::vector<Vertex>(0);
69-
}
112+
// Slice maxVertex of last process
113+
if(minVertex > vertexCount){
114+
return std::vector<Vertex>(0);
115+
}
70116

71-
maxVertex = std::min(maxVertex, vertexCount);
117+
maxVertex = std::min(maxVertex, vertexCount);
72118

73-
assert(minVertex <= maxVertex);
119+
assert(minVertex <= maxVertex);
74120

75-
std::vector<Vertex> vertices = graph.getVertices();
76-
std::vector<Vertex> myVertices(vertices.begin() + minVertex, vertices.begin() + maxVertex);
77-
return myVertices;
121+
std::vector<Vertex> vertices = graph.getVertices();
122+
std::vector<Vertex> myVertices(vertices.begin() + minVertex, vertices.begin() + maxVertex);
123+
return myVertices;
78124

79-
}
125+
}
80126

81-
};
127+
};
82128

83-
} /* mapping */
129+
} /* mapping */
84130

85131
} /* graybat */

src/gol.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ int gol(const unsigned nCells, const unsigned nTimeSteps ) {
112112
MyCave cave(graybat::pattern::GridDiagonal(height, width));
113113

114114
// Distribute vertices
115-
cave.distribute(graybat::mapping::Roundrobin());
115+
cave.distribute(graybat::mapping::Random(1234));
116116

117117
/***************************************************************************
118118
* Run Simulation

0 commit comments

Comments
 (0)