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 */1819package org.apache.giraph.benchmark;
2021import org.apache.giraph.graph.BasicComputation;
22import org.apache.giraph.edge.Edge;
23import org.apache.giraph.edge.MutableEdge;
24import org.apache.giraph.graph.Vertex;
25import org.apache.hadoop.io.DoubleWritable;
26import org.apache.hadoop.io.LongWritable;
2728import java.io.IOException;
2930/**31 * Implementation of Page Rank algorithm on a weighted graph.32 */33publicclassWeightedPageRankComputationextends BasicComputation<LongWritable,
34 DoubleWritable, DoubleWritable, DoubleWritable> {
35/** Number of supersteps */36publicstaticfinal String SUPERSTEP_COUNT =
37"giraph.weightedPageRank.superstepCount";
3839 @Override
40publicvoid compute(
41 Vertex<LongWritable, DoubleWritable, DoubleWritable> vertex,
42 Iterable<DoubleWritable> messages) throws IOException {
43if (getSuperstep() == 0) {
44// Normalize out edge weights45double outEdgeSum = 0;
46for (Edge<LongWritable, DoubleWritable> edge : vertex.getEdges()) {
47 outEdgeSum += edge.getValue().get();
48 }
49for (MutableEdge<LongWritable, DoubleWritable> edge :
50 vertex.getMutableEdges()) {
51 edge.setValue(new DoubleWritable(edge.getValue().get() / outEdgeSum));
52 }
53 } else {
54double messageSum = 0;
55for (DoubleWritable message : messages) {
56 messageSum += message.get();
57 }
58 vertex.getValue().set(
59 (0.15f / getTotalNumVertices()) + 0.85f * messageSum);
60 }
6162if (getSuperstep() < getConf().getInt(SUPERSTEP_COUNT, 0)) {
63for (Edge<LongWritable, DoubleWritable> edge : vertex.getEdges()) {
64 sendMessage(edge.getTargetVertexId(),
65new DoubleWritable(
66 vertex.getValue().get() * edge.getValue().get()));
67 }
68 } else {
69 vertex.voteToHalt();
70 }
71 }
72 }