1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
package org.apache.giraph.block_app.test_setup.graphs; |
19 | |
|
20 | |
import java.util.Random; |
21 | |
|
22 | |
import org.apache.giraph.block_app.test_setup.NumericTestGraph; |
23 | |
import org.apache.giraph.block_app.test_setup.TestGraphModifier; |
24 | |
import org.apache.giraph.conf.FloatConfOption; |
25 | |
import org.apache.giraph.conf.GiraphConfiguration; |
26 | |
import org.apache.giraph.conf.IntConfOption; |
27 | |
import org.apache.giraph.function.Supplier; |
28 | |
import org.apache.hadoop.io.Writable; |
29 | |
import org.apache.hadoop.io.WritableComparable; |
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
public class SyntheticGraphInit<I extends WritableComparable, |
39 | |
V extends Writable, E extends Writable> |
40 | |
implements TestGraphModifier<I, V, E> { |
41 | 0 | public static final IntConfOption NUM_COMMUNITIES = new IntConfOption( |
42 | |
"test.SyntheticGraphCreator.NUM_COMMUNITIES", -1, ""); |
43 | 0 | public static final IntConfOption NUM_VERTICES = new IntConfOption( |
44 | |
"test.SyntheticGraphCreator.NUM_VERTICES", -1, ""); |
45 | 0 | public static final IntConfOption NUM_EDGES_PER_VERTEX = new IntConfOption( |
46 | |
"test.SyntheticGraphCreator.NUM_EDGES_PER_VERTEX", -1, ""); |
47 | 0 | public static final FloatConfOption ACTUAL_LOCALITY_RATIO = |
48 | |
new FloatConfOption( |
49 | |
"test.SyntheticGraphCreator.ACTUAL_LOCALITY_RATIO", -1, ""); |
50 | |
|
51 | |
protected final Supplier<E> edgeSupplier; |
52 | |
|
53 | 0 | public SyntheticGraphInit(Supplier<E> edgeSupplier) { |
54 | 0 | this.edgeSupplier = edgeSupplier; |
55 | 0 | } |
56 | |
|
57 | 0 | public SyntheticGraphInit() { |
58 | 0 | this.edgeSupplier = null; |
59 | 0 | } |
60 | |
|
61 | |
@Override |
62 | |
public void modifyGraph(NumericTestGraph<I, V, E> graph) { |
63 | 0 | GiraphConfiguration conf = graph.getConf(); |
64 | 0 | int numPartitions = NUM_COMMUNITIES.get(conf); |
65 | 0 | int numVertices = NUM_VERTICES.get(conf); |
66 | 0 | int numEdgesPerVertex = NUM_EDGES_PER_VERTEX.get(conf); |
67 | 0 | int communitySize = numVertices / numPartitions; |
68 | 0 | float actualLocalityRatio = ACTUAL_LOCALITY_RATIO.get(conf); |
69 | 0 | Random random = new Random(42); |
70 | 0 | for (int i = 0; i < numVertices; ++i) { |
71 | 0 | for (int e = 0; e < numEdgesPerVertex / 2; ++e) { |
72 | 0 | boolean localEdge = random.nextFloat() < actualLocalityRatio; |
73 | 0 | int community = i / communitySize; |
74 | |
int j; |
75 | |
do { |
76 | 0 | if (localEdge) { |
77 | 0 | j = community * communitySize + random.nextInt(communitySize); |
78 | |
} else { |
79 | 0 | j = random.nextInt(numVertices); |
80 | |
} |
81 | 0 | } while (j == i); |
82 | 0 | graph.addSymmetricEdge( |
83 | 0 | i, j, edgeSupplier != null ? edgeSupplier.get() : null); |
84 | |
} |
85 | |
} |
86 | 0 | } |
87 | |
} |