|
3 | 3 | #include <algorithm> /* std::min */ |
4 | 4 | #include <vector> /* std::vector */ |
5 | 5 | #include <assert.h> /* assert */ |
| 6 | +#include <stdlib.h> /* srand, rand */ |
6 | 7 |
|
7 | 8 | /******************************************************************************* |
8 | 9 | * |
|
12 | 13 |
|
13 | 14 | namespace graybat { |
14 | 15 |
|
15 | | - namespace mapping { |
| 16 | + namespace mapping { |
16 | 17 |
|
17 | 18 |
|
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 { |
24 | 31 |
|
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){ |
28 | 34 |
|
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); |
38 | 49 | } |
39 | | - |
| 50 | + |
40 | 51 | } |
| 52 | + |
| 53 | + |
| 54 | + return myVertices; |
41 | 55 | } |
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 | + } |
43 | 85 |
|
44 | | - } |
| 86 | + } |
| 87 | + } |
| 88 | + return myVertices; |
| 89 | + |
| 90 | + } |
45 | 91 |
|
46 | | - }; |
| 92 | + }; |
47 | 93 |
|
48 | | - struct Consecutive { |
| 94 | + struct Consecutive { |
49 | 95 |
|
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){ |
52 | 98 |
|
53 | | - typedef typename T_Graph::Vertex Vertex; |
| 99 | + typedef typename T_Graph::Vertex Vertex; |
54 | 100 |
|
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); |
57 | 103 |
|
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 | + } |
62 | 108 |
|
63 | | - unsigned minVertex = processID * vertexPerProcess; |
64 | | - unsigned maxVertex = minVertex + vertexPerProcess; |
| 109 | + unsigned minVertex = processID * vertexPerProcess; |
| 110 | + unsigned maxVertex = minVertex + vertexPerProcess; |
65 | 111 |
|
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 | + } |
70 | 116 |
|
71 | | - maxVertex = std::min(maxVertex, vertexCount); |
| 117 | + maxVertex = std::min(maxVertex, vertexCount); |
72 | 118 |
|
73 | | - assert(minVertex <= maxVertex); |
| 119 | + assert(minVertex <= maxVertex); |
74 | 120 |
|
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; |
78 | 124 |
|
79 | | - } |
| 125 | + } |
80 | 126 |
|
81 | | - }; |
| 127 | + }; |
82 | 128 |
|
83 | | - } /* mapping */ |
| 129 | + } /* mapping */ |
84 | 130 |
|
85 | 131 | } /* graybat */ |
0 commit comments