1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
package org.apache.giraph.examples; |
20 | |
|
21 | |
import org.apache.giraph.graph.BasicComputation; |
22 | |
import org.apache.giraph.edge.EdgeFactory; |
23 | |
import org.apache.giraph.graph.Vertex; |
24 | |
import org.apache.giraph.worker.WorkerContext; |
25 | |
import org.apache.hadoop.io.DoubleWritable; |
26 | |
import org.apache.hadoop.io.FloatWritable; |
27 | |
import org.apache.hadoop.io.LongWritable; |
28 | |
import org.apache.log4j.Logger; |
29 | |
|
30 | |
import java.io.IOException; |
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | 0 | public class SimpleMutateGraphComputation extends BasicComputation< |
36 | |
LongWritable, DoubleWritable, FloatWritable, DoubleWritable> { |
37 | |
|
38 | 0 | private static Logger LOG = |
39 | 0 | Logger.getLogger(SimpleMutateGraphComputation.class); |
40 | |
|
41 | 0 | private long maxRanges = 100; |
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
private long rangeVertexIdStart(int range) { |
52 | 0 | return (Long.MAX_VALUE / maxRanges) * range; |
53 | |
} |
54 | |
|
55 | |
@Override |
56 | |
public void compute( |
57 | |
Vertex<LongWritable, DoubleWritable, FloatWritable> vertex, |
58 | |
Iterable<DoubleWritable> messages) throws IOException { |
59 | 0 | SimpleMutateGraphVertexWorkerContext workerContext = getWorkerContext(); |
60 | 0 | if (getSuperstep() == 0) { |
61 | 0 | LOG.debug("Reached superstep " + getSuperstep()); |
62 | 0 | } else if (getSuperstep() == 1) { |
63 | |
|
64 | |
|
65 | 0 | LongWritable destVertexId = |
66 | 0 | new LongWritable(rangeVertexIdStart(1) + vertex.getId().get()); |
67 | 0 | sendMessage(destVertexId, new DoubleWritable(0.0)); |
68 | 0 | } else if (getSuperstep() == 2) { |
69 | 0 | LOG.debug("Reached superstep " + getSuperstep()); |
70 | 0 | } else if (getSuperstep() == 3) { |
71 | 0 | long vertexCount = workerContext.getVertexCount(); |
72 | 0 | if (vertexCount * 2 != getTotalNumVertices()) { |
73 | 0 | throw new IllegalStateException( |
74 | 0 | "Impossible to have " + getTotalNumVertices() + |
75 | |
" vertices when should have " + vertexCount * 2 + |
76 | 0 | " on superstep " + getSuperstep()); |
77 | |
} |
78 | 0 | long edgeCount = workerContext.getEdgeCount(); |
79 | 0 | if (edgeCount != getTotalNumEdges()) { |
80 | 0 | throw new IllegalStateException( |
81 | 0 | "Impossible to have " + getTotalNumEdges() + |
82 | |
" edges when should have " + edgeCount + |
83 | 0 | " on superstep " + getSuperstep()); |
84 | |
} |
85 | |
|
86 | 0 | LongWritable vertexIndex = |
87 | 0 | new LongWritable(rangeVertexIdStart(3) + vertex.getId().get()); |
88 | 0 | addVertexRequest(vertexIndex, new DoubleWritable(0.0)); |
89 | |
|
90 | 0 | addEdgeRequest(vertexIndex, |
91 | 0 | EdgeFactory.create(vertex.getId(), new FloatWritable(0.0f))); |
92 | 0 | } else if (getSuperstep() == 4) { |
93 | 0 | LOG.debug("Reached superstep " + getSuperstep()); |
94 | 0 | } else if (getSuperstep() == 5) { |
95 | 0 | long vertexCount = workerContext.getVertexCount(); |
96 | 0 | if (vertexCount * 2 != getTotalNumVertices()) { |
97 | 0 | throw new IllegalStateException( |
98 | 0 | "Impossible to have " + getTotalNumVertices() + |
99 | |
" when should have " + vertexCount * 2 + |
100 | 0 | " on superstep " + getSuperstep()); |
101 | |
} |
102 | 0 | long edgeCount = workerContext.getEdgeCount(); |
103 | 0 | if (edgeCount + vertexCount != getTotalNumEdges()) { |
104 | 0 | throw new IllegalStateException( |
105 | 0 | "Impossible to have " + getTotalNumEdges() + |
106 | |
" edges when should have " + edgeCount + vertexCount + |
107 | 0 | " on superstep " + getSuperstep()); |
108 | |
} |
109 | |
|
110 | 0 | LongWritable vertexIndex = |
111 | 0 | new LongWritable(rangeVertexIdStart(3) + vertex.getId().get()); |
112 | 0 | workerContext.increaseEdgesRemoved(); |
113 | 0 | removeEdgesRequest(vertexIndex, vertex.getId()); |
114 | 0 | } else if (getSuperstep() == 6) { |
115 | |
|
116 | 0 | if (vertex.getId().compareTo( |
117 | 0 | new LongWritable(rangeVertexIdStart(3))) >= 0) { |
118 | 0 | removeVertexRequest(vertex.getId()); |
119 | |
} |
120 | 0 | } else if (getSuperstep() == 7) { |
121 | 0 | long origEdgeCount = workerContext.getOrigEdgeCount(); |
122 | 0 | if (origEdgeCount != getTotalNumEdges()) { |
123 | 0 | throw new IllegalStateException( |
124 | 0 | "Impossible to have " + getTotalNumEdges() + |
125 | |
" edges when should have " + origEdgeCount + |
126 | 0 | " on superstep " + getSuperstep()); |
127 | |
} |
128 | 0 | } else if (getSuperstep() == 8) { |
129 | 0 | long vertexCount = workerContext.getVertexCount(); |
130 | 0 | if (vertexCount / 2 != getTotalNumVertices()) { |
131 | 0 | throw new IllegalStateException( |
132 | 0 | "Impossible to have " + getTotalNumVertices() + |
133 | |
" vertices when should have " + vertexCount / 2 + |
134 | 0 | " on superstep " + getSuperstep()); |
135 | |
} |
136 | 0 | } else if (getSuperstep() == 9) { |
137 | |
|
138 | |
|
139 | 0 | if (vertex.getId().compareTo( |
140 | 0 | new LongWritable(rangeVertexIdStart(1))) >= 0) { |
141 | |
|
142 | 0 | removeVertexRequest(vertex.getId()); |
143 | |
} else { |
144 | |
|
145 | |
|
146 | 0 | sendMessage( |
147 | 0 | new LongWritable(rangeVertexIdStart(1) + vertex.getId().get()), |
148 | |
new DoubleWritable(0.0)); |
149 | |
} |
150 | 0 | } else if (getSuperstep() == 10) { |
151 | 0 | LOG.debug("Reached superstep " + getSuperstep()); |
152 | 0 | } else if (getSuperstep() == 11) { |
153 | 0 | long vertexCount = workerContext.getVertexCount(); |
154 | 0 | if (vertexCount / 2 != getTotalNumVertices()) { |
155 | 0 | throw new IllegalStateException( |
156 | 0 | "Impossible to have " + getTotalNumVertices() + |
157 | |
" vertices when should have " + vertexCount / 2 + |
158 | 0 | " on superstep " + getSuperstep()); |
159 | |
} |
160 | 0 | } else { |
161 | 0 | vertex.voteToHalt(); |
162 | |
} |
163 | 0 | } |
164 | |
|
165 | |
|
166 | |
|
167 | |
|
168 | 0 | public static class SimpleMutateGraphVertexWorkerContext |
169 | |
extends WorkerContext { |
170 | |
|
171 | |
private long vertexCount; |
172 | |
|
173 | |
private long edgeCount; |
174 | |
|
175 | |
private long origEdgeCount; |
176 | |
|
177 | 0 | private int edgesRemoved = 0; |
178 | |
|
179 | |
@Override |
180 | |
public void preApplication() |
181 | 0 | throws InstantiationException, IllegalAccessException { } |
182 | |
|
183 | |
@Override |
184 | 0 | public void postApplication() { } |
185 | |
|
186 | |
@Override |
187 | 0 | public void preSuperstep() { } |
188 | |
|
189 | |
@Override |
190 | |
public void postSuperstep() { |
191 | 0 | vertexCount = getTotalNumVertices(); |
192 | 0 | edgeCount = getTotalNumEdges(); |
193 | 0 | if (getSuperstep() == 1) { |
194 | 0 | origEdgeCount = edgeCount; |
195 | |
} |
196 | 0 | LOG.info("Got " + vertexCount + " vertices, " + |
197 | |
edgeCount + " edges on superstep " + |
198 | 0 | getSuperstep()); |
199 | 0 | LOG.info("Removed " + edgesRemoved); |
200 | 0 | edgesRemoved = 0; |
201 | 0 | } |
202 | |
|
203 | |
public long getVertexCount() { |
204 | 0 | return vertexCount; |
205 | |
} |
206 | |
|
207 | |
public long getEdgeCount() { |
208 | 0 | return edgeCount; |
209 | |
} |
210 | |
|
211 | |
public long getOrigEdgeCount() { |
212 | 0 | return origEdgeCount; |
213 | |
} |
214 | |
|
215 | |
|
216 | |
|
217 | |
|
218 | |
public void increaseEdgesRemoved() { |
219 | 0 | this.edgesRemoved++; |
220 | 0 | } |
221 | |
} |
222 | |
} |