Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
PseudoRandomIntNullLocalEdgesHelper |
|
| 2.0;2 |
1 | /* | |
2 | * Licensed to the Apache Software Foundation (ASF) under one | |
3 | * or more contributor license agreements. See the NOTICE file | |
4 | * distributed with this work for additional information | |
5 | * regarding copyright ownership. The ASF licenses this file | |
6 | * to you under the Apache License, Version 2.0 (the | |
7 | * "License"); you may not use this file except in compliance | |
8 | * with the License. You may obtain a copy of the License at | |
9 | * | |
10 | * http://www.apache.org/licenses/LICENSE-2.0 | |
11 | * | |
12 | * Unless required by applicable law or agreed to in writing, software | |
13 | * 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 and | |
16 | * limitations under the License. | |
17 | */ | |
18 | ||
19 | package org.apache.giraph.io.formats; | |
20 | ||
21 | import org.apache.giraph.conf.ImmutableClassesGiraphConfiguration; | |
22 | import org.apache.giraph.partition.PartitionUtils; | |
23 | import org.apache.giraph.worker.WorkerInfo; | |
24 | ||
25 | import java.util.Collections; | |
26 | import java.util.List; | |
27 | import java.util.Random; | |
28 | ||
29 | /** | |
30 | * Helper class to generate pseudo-random local edges. | |
31 | * Like {@link PseudoRandomLocalEdgesHelper}, but for graphs where vertex ids | |
32 | * are integers. | |
33 | */ | |
34 | public class PseudoRandomIntNullLocalEdgesHelper { | |
35 | /** Minimum ratio of partition-local edges. */ | |
36 | private float minLocalEdgesRatio; | |
37 | /** Total number of vertices. */ | |
38 | private int numVertices; | |
39 | /** Total number of partitions. */ | |
40 | private int numPartitions; | |
41 | /** Average partition size. */ | |
42 | private int partitionSize; | |
43 | ||
44 | /** | |
45 | * Constructor. | |
46 | * | |
47 | * @param numVertices Total number of vertices. | |
48 | * @param conf Configuration. | |
49 | */ | |
50 | public PseudoRandomIntNullLocalEdgesHelper(int numVertices, | |
51 | 0 | ImmutableClassesGiraphConfiguration conf) { |
52 | 0 | this.minLocalEdgesRatio = conf.getFloat( |
53 | PseudoRandomInputFormatConstants.LOCAL_EDGES_MIN_RATIO, | |
54 | PseudoRandomInputFormatConstants.LOCAL_EDGES_MIN_RATIO_DEFAULT); | |
55 | 0 | this.numVertices = numVertices; |
56 | 0 | int numWorkers = conf.getMaxWorkers(); |
57 | 0 | List<WorkerInfo> workerInfos = Collections.nCopies(numWorkers, |
58 | new WorkerInfo()); | |
59 | 0 | numPartitions = PartitionUtils.computePartitionCount( |
60 | 0 | workerInfos.size(), conf); |
61 | 0 | partitionSize = numVertices / numPartitions; |
62 | 0 | } |
63 | ||
64 | /** | |
65 | * Generate a destination vertex id for the given source vertex, | |
66 | * using the desired configuration for edge locality and the provided | |
67 | * pseudo-random generator. | |
68 | * | |
69 | * @param sourceVertexId Source vertex id. | |
70 | * @param rand Pseudo-random generator. | |
71 | * @return Destination vertex id. | |
72 | */ | |
73 | public int generateDestVertex(int sourceVertexId, Random rand) { | |
74 | 0 | if (rand.nextFloat() < minLocalEdgesRatio) { |
75 | 0 | int partitionId = sourceVertexId % numPartitions; |
76 | 0 | return partitionId + numPartitions * rand.nextInt(partitionSize); |
77 | } else { | |
78 | 0 | return rand.nextInt(numVertices); |
79 | } | |
80 | } | |
81 | } |