Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
PageRankComputation |
|
| 2.5;2.5 |
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 | package org.apache.giraph.benchmark; | |
19 | ||
20 | import org.apache.giraph.graph.MutableVertex; | |
21 | import org.apache.hadoop.io.DoubleWritable; | |
22 | import org.apache.hadoop.io.LongWritable; | |
23 | ||
24 | /** | |
25 | * Shared computation of class Pregel-style PageRank computation for benchmark | |
26 | * classes. | |
27 | */ | |
28 | public class PageRankComputation { | |
29 | /** Number of supersteps */ | |
30 | public static final String SUPERSTEP_COUNT = | |
31 | "PageRankBenchmark.superstepCount"; | |
32 | ||
33 | /** | |
34 | * Do not construct. | |
35 | */ | |
36 | 0 | private PageRankComputation() { } |
37 | ||
38 | /** | |
39 | * Generic page rank algorithm. | |
40 | * | |
41 | * @param vertex Vertex to compute on. | |
42 | * @param messages Iterator of messages from previous superstep. | |
43 | */ | |
44 | public static void computePageRank( | |
45 | MutableVertex<LongWritable, DoubleWritable, DoubleWritable, | |
46 | DoubleWritable> vertex, Iterable<DoubleWritable> messages) { | |
47 | 1313 | if (vertex.getSuperstep() >= 1) { |
48 | 1010 | double sum = 0; |
49 | 1010 | for (DoubleWritable message : messages) { |
50 | 2020 | sum += message.get(); |
51 | } | |
52 | 1010 | DoubleWritable vertexValue = new DoubleWritable( |
53 | (0.15f / vertex.getTotalNumVertices()) + 0.85f * sum); | |
54 | 1010 | vertex.setValue(vertexValue); |
55 | } | |
56 | ||
57 | 1313 | if (vertex.getSuperstep() < vertex.getConf().getInt(SUPERSTEP_COUNT, -1)) { |
58 | 1010 | long edges = vertex.getNumEdges(); |
59 | 1010 | vertex.sendMessageToAllEdges( |
60 | new DoubleWritable(vertex.getValue().get() / edges)); | |
61 | 1010 | } else { |
62 | 303 | vertex.voteToHalt(); |
63 | } | |
64 | 1313 | } |
65 | } |