-
Notifications
You must be signed in to change notification settings - Fork 28.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[SPARK-11496][GRAPHX] Parallel implementation of personalized pagerank #14998
Closed
+121
−1
Closed
Changes from 18 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
f41975e
Parallel personalized pagerank implementation
3605e40
Scala style tweaks
8b34e5c
Removing breeze dependency from mllib (available through graphx)
508ba45
Renaming SparseVector to BSV
09d31c8
Removing extra space, extra line
8506353
Code-style changes
2d1dee7
Parallel personalized pagerank implementation
202acb2
Scala style tweaks
69db385
Renaming SparseVector to BSV
a42d272
Removing extra space, extra line
53ab670
Code-style changes
31e2e98
Moving to mllib-local
13fbf55
Merge branch 'parallel-ppr' of github.com:moustaki/spark into paralle…
c7ca220
Cleaning up pom dependencies
1ec345f
Removing unused import
2d00fc0
Import style
46381c8
More import refactor
4b2d564
Alphabetical ordering of imports
40f5780
Destroying broadcast map
7dc2c23
Minor styling
adc5fc3
Undoing destroy
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,8 +19,11 @@ package org.apache.spark.graphx.lib | |
|
||
import scala.reflect.ClassTag | ||
|
||
import breeze.linalg.{Vector => BV} | ||
|
||
import org.apache.spark.graphx._ | ||
import org.apache.spark.internal.Logging | ||
import org.apache.spark.ml.linalg.{Vector, Vectors} | ||
|
||
/** | ||
* PageRank algorithm implementation. There are two implementations of PageRank implemented. | ||
|
@@ -162,6 +165,85 @@ object PageRank extends Logging { | |
rankGraph | ||
} | ||
|
||
/** | ||
* Run Personalized PageRank for a fixed number of iterations, for a | ||
* set of starting nodes in parallel. Returns a graph with vertex attributes | ||
* containing the pagerank relative to all starting nodes (as a sparse vector) and | ||
* edge attributes the normalized edge weight | ||
* | ||
* @tparam VD The original vertex attribute (not used) | ||
* @tparam ED The original edge attribute (not used) | ||
* | ||
* @param graph The graph on which to compute personalized pagerank | ||
* @param numIter The number of iterations to run | ||
* @param resetProb The random reset probability | ||
* @param sources The list of sources to compute personalized pagerank from | ||
* @return the graph with vertex attributes | ||
* containing the pagerank relative to all starting nodes (as a sparse vector) and | ||
* edge attributes the normalized edge weight | ||
*/ | ||
def runParallelPersonalizedPageRank[VD: ClassTag, ED: ClassTag](graph: Graph[VD, ED], | ||
numIter: Int, resetProb: Double = 0.15, | ||
sources: Array[VertexId]): Graph[Vector, Double] = { | ||
// TODO if one sources vertex id is outside of the int range | ||
// we won't be able to store its activations in a sparse vector | ||
val zero = Vectors.sparse(sources.size, List()).asBreeze | ||
val sourcesInitMap = sources.zipWithIndex.map { case (vid, i) => | ||
val v = Vectors.sparse(sources.size, Array(i), Array(resetProb)).asBreeze | ||
(vid, v) | ||
}.toMap | ||
val sc = graph.vertices.sparkContext | ||
val sourcesInitMapBC = sc.broadcast(sourcesInitMap) | ||
// Initialize the PageRank graph with each edge attribute having | ||
// weight 1/outDegree and each source vertex with attribute 1.0. | ||
var rankGraph = graph | ||
// Associate the degree with each vertex | ||
.outerJoinVertices(graph.outDegrees) { (vid, vdata, deg) => deg.getOrElse(0) } | ||
// Set the weight on the edges based on the degree | ||
.mapTriplets(e => 1.0 / e.srcAttr, TripletFields.Src) | ||
.mapVertices { (vid, attr) => | ||
if (sourcesInitMapBC.value contains vid) { | ||
sourcesInitMapBC.value(vid) | ||
} else { | ||
zero | ||
} | ||
} | ||
|
||
var i = 0 | ||
while (i < numIter) { | ||
val prevRankGraph = rankGraph | ||
// Propagates the message along outbound edges | ||
// and adding start nodes back in with activation resetProb | ||
val rankUpdates = rankGraph.aggregateMessages[BV[Double]]( | ||
ctx => ctx.sendToDst(ctx.srcAttr :* ctx.attr), | ||
(a : BV[Double], b : BV[Double]) => a :+ b, TripletFields.Src) | ||
|
||
rankGraph = rankGraph.joinVertices(rankUpdates) { | ||
(vid, oldRank, msgSum) => | ||
val popActivations: BV[Double] = msgSum :* (1.0 - resetProb) | ||
val resetActivations = if (sourcesInitMapBC.value contains vid) { | ||
sourcesInitMapBC.value(vid) | ||
} else { | ||
zero | ||
} | ||
popActivations :+ resetActivations | ||
}.cache() | ||
|
||
rankGraph.edges.foreachPartition(x => {}) // also materializes rankGraph.vertices | ||
prevRankGraph.vertices.unpersist(false) | ||
prevRankGraph.edges.unpersist(false) | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add |
||
logInfo(s"Parallel Personalized PageRank finished iteration $i.") | ||
|
||
i += 1 | ||
} | ||
|
||
rankGraph | ||
.mapVertices { (vid, attr) => | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. move it up one line. |
||
Vectors.fromBreeze(attr) | ||
} | ||
} | ||
|
||
/** | ||
* Run a dynamic version of PageRank returning a graph with vertex attributes containing the | ||
* PageRank and edge attributes containing the normalized edge weight. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We found that
breeze
is fairly slow by doing this operation. Is it possible to use native spark vector, and use our linear algebra package?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Chatted offline, but decided to not switch to native Spark operations for now, as it doesn't support adding sparse vectors, which the above does