1/*2 * Licensed to the Apache Software Foundation (ASF) under one3 * or more contributor license agreements. See the NOTICE file4 * distributed with this work for additional information5 * regarding copyright ownership. The ASF licenses this file6 * to you under the Apache License, Version 2.0 (the7 * "License"); you may not use this file except in compliance8 * with the License. You may obtain a copy of the License at9 *10 * http://www.apache.org/licenses/LICENSE-2.011 *12 * Unless required by applicable law or agreed to in writing, software13 * distributed under the License is distributed on an "AS IS" BASIS,14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.15 * See the License for the specific language governing permissions and16 * limitations under the License.17 */18package org.apache.giraph.block_app.test_setup.graphs;
1920import java.util.Random;
2122import org.apache.giraph.block_app.test_setup.NumericTestGraph;
23import org.apache.giraph.block_app.test_setup.TestGraphModifier;
24import org.apache.giraph.conf.FloatConfOption;
25import org.apache.giraph.conf.GiraphConfiguration;
26import org.apache.giraph.conf.IntConfOption;
27import org.apache.giraph.function.Supplier;
28import org.apache.hadoop.io.Writable;
29import org.apache.hadoop.io.WritableComparable;
3031/**32 * Creates synthetic graphs, that can have community structure.33 *34 * @param <I> Vertex id type35 * @param <V> Vertex value type36 * @param <E> Edge value type37 */38publicclass SyntheticGraphInit<I extends WritableComparable,
39 V extends Writable, E extends Writable>
40implements TestGraphModifier<I, V, E> {
41publicstaticfinalIntConfOption NUM_COMMUNITIES = newIntConfOption(
42"test.SyntheticGraphCreator.NUM_COMMUNITIES", -1, "");
43publicstaticfinalIntConfOption NUM_VERTICES = newIntConfOption(
44"test.SyntheticGraphCreator.NUM_VERTICES", -1, "");
45publicstaticfinalIntConfOption NUM_EDGES_PER_VERTEX = newIntConfOption(
46"test.SyntheticGraphCreator.NUM_EDGES_PER_VERTEX", -1, "");
47publicstaticfinalFloatConfOption ACTUAL_LOCALITY_RATIO =
48newFloatConfOption(
49"test.SyntheticGraphCreator.ACTUAL_LOCALITY_RATIO", -1, "");
5051protectedfinal Supplier<E> edgeSupplier;
5253publicSyntheticGraphInit(Supplier<E> edgeSupplier) {
54this.edgeSupplier = edgeSupplier;
55 }
5657publicSyntheticGraphInit() {
58this.edgeSupplier = null;
59 }
6061 @Override
62publicvoid modifyGraph(NumericTestGraph<I, V, E> graph) {
63GiraphConfiguration conf = graph.getConf();
64int numPartitions = NUM_COMMUNITIES.get(conf);
65int numVertices = NUM_VERTICES.get(conf);
66int numEdgesPerVertex = NUM_EDGES_PER_VERTEX.get(conf);
67int communitySize = numVertices / numPartitions;
68float actualLocalityRatio = ACTUAL_LOCALITY_RATIO.get(conf);
69 Random random = new Random(42);
70for (int i = 0; i < numVertices; ++i) {
71for (int e = 0; e < numEdgesPerVertex / 2; ++e) {
72boolean localEdge = random.nextFloat() < actualLocalityRatio;
73int community = i / communitySize;
74int j;
75do {
76if (localEdge) {
77 j = community * communitySize + random.nextInt(communitySize);
78 } else {
79 j = random.nextInt(numVertices);
80 }
81 } while (j == i);
82 graph.addSymmetricEdge(
83 i, j, edgeSupplier != null ? edgeSupplier.get() : null);
84 }
85 }
86 }
87 }