-
Notifications
You must be signed in to change notification settings - Fork 29.1k
[SPARK-3936] Add aggregateMessages, which supersedes mapReduceTriplets #3100
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
Changes from all commits
4a566dc
b567be2
c85076d
e0f8ecc
194a2df
1e80aca
f5b65d0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.apache.spark.graphx | ||
|
|
||
| /** | ||
| * Represents an edge along with its neighboring vertices and allows sending messages along the | ||
| * edge. Used in [[Graph#aggregateMessages]]. | ||
| */ | ||
| abstract class EdgeContext[VD, ED, A] { | ||
| /** The vertex id of the edge's source vertex. */ | ||
| def srcId: VertexId | ||
|
Contributor
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. didn't we decide we want to get rid of VertexId when we expose it to the user? |
||
| /** The vertex id of the edge's destination vertex. */ | ||
| def dstId: VertexId | ||
| /** The vertex attribute of the edge's source vertex. */ | ||
| def srcAttr: VD | ||
| /** The vertex attribute of the edge's destination vertex. */ | ||
| def dstAttr: VD | ||
| /** The attribute associated with the edge. */ | ||
| def attr: ED | ||
|
|
||
| /** Sends a message to the source vertex. */ | ||
| def sendToSrc(msg: A): Unit | ||
| /** Sends a message to the destination vertex. */ | ||
| def sendToDst(msg: A): Unit | ||
|
|
||
| /** Converts the edge and vertex properties into an [[EdgeTriplet]] for convenience. */ | ||
| def toEdgeTriplet: EdgeTriplet[VD, ED] = { | ||
| val et = new EdgeTriplet[VD, ED] | ||
| et.srcId = srcId | ||
| et.srcAttr = srcAttr | ||
| et.dstId = dstId | ||
| et.dstAttr = dstAttr | ||
| et.attr = attr | ||
| et | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -207,8 +207,39 @@ abstract class Graph[VD: ClassTag, ED: ClassTag] protected () extends Serializab | |
| * }}} | ||
| * | ||
| */ | ||
| def mapTriplets[ED2: ClassTag](map: EdgeTriplet[VD, ED] => ED2): Graph[VD, ED2] = { | ||
| mapTriplets((pid, iter) => iter.map(map)) | ||
| def mapTriplets[ED2: ClassTag]( | ||
| map: EdgeTriplet[VD, ED] => ED2): Graph[VD, ED2] = { | ||
|
Contributor
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. note: this can fit on one line now |
||
| mapTriplets((pid, iter) => iter.map(map), TripletFields.All) | ||
| } | ||
|
|
||
| /** | ||
| * Transforms each edge attribute using the map function, passing it the adjacent vertex | ||
| * attributes as well. If adjacent vertex values are not required, | ||
| * consider using `mapEdges` instead. | ||
| * | ||
| * @note This does not change the structure of the | ||
| * graph or modify the values of this graph. As a consequence | ||
| * the underlying index structures can be reused. | ||
| * | ||
| * @param map the function from an edge object to a new edge value. | ||
| * @param tripletFields which fields should be included in the edge triplet passed to the map | ||
| * function. If not all fields are needed, specifying this can improve performance. | ||
| * | ||
| * @tparam ED2 the new edge data type | ||
| * | ||
| * @example This function might be used to initialize edge | ||
| * attributes based on the attributes associated with each vertex. | ||
| * {{{ | ||
| * val rawGraph: Graph[Int, Int] = someLoadFunction() | ||
| * val graph = rawGraph.mapTriplets[Int]( edge => | ||
| * edge.src.data - edge.dst.data) | ||
| * }}} | ||
| * | ||
| */ | ||
| def mapTriplets[ED2: ClassTag]( | ||
| map: EdgeTriplet[VD, ED] => ED2, | ||
| tripletFields: TripletFields): Graph[VD, ED2] = { | ||
| mapTriplets((pid, iter) => iter.map(map), tripletFields) | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -223,12 +254,15 @@ abstract class Graph[VD: ClassTag, ED: ClassTag] protected () extends Serializab | |
| * the underlying index structures can be reused. | ||
| * | ||
| * @param map the iterator transform | ||
| * @param tripletFields which fields should be included in the edge triplet passed to the map | ||
| * function. If not all fields are needed, specifying this can improve performance. | ||
| * | ||
| * @tparam ED2 the new edge data type | ||
| * | ||
| */ | ||
| def mapTriplets[ED2: ClassTag](map: (PartitionID, Iterator[EdgeTriplet[VD, ED]]) => Iterator[ED2]) | ||
| : Graph[VD, ED2] | ||
| def mapTriplets[ED2: ClassTag]( | ||
| map: (PartitionID, Iterator[EdgeTriplet[VD, ED]]) => Iterator[ED2], | ||
| tripletFields: TripletFields): Graph[VD, ED2] | ||
|
|
||
| /** | ||
| * Reverses all edges in the graph. If this graph contains an edge from a to b then the returned | ||
|
|
@@ -287,6 +321,8 @@ abstract class Graph[VD: ClassTag, ED: ClassTag] protected () extends Serializab | |
| * "sent" to either vertex in the edge. The `reduceFunc` is then used to combine the output of | ||
| * the map phase destined to each vertex. | ||
| * | ||
| * This function is deprecated in 1.2.0 because of SPARK-3936. Use aggregateMessages instead. | ||
| * | ||
| * @tparam A the type of "message" to be sent to each vertex | ||
| * | ||
| * @param mapFunc the user defined map function which returns 0 or | ||
|
|
@@ -296,13 +332,15 @@ abstract class Graph[VD: ClassTag, ED: ClassTag] protected () extends Serializab | |
| * be commutative and associative and is used to combine the output | ||
| * of the map phase | ||
| * | ||
| * @param activeSetOpt optionally, a set of "active" vertices and a direction of edges to | ||
| * consider when running `mapFunc`. If the direction is `In`, `mapFunc` will only be run on | ||
| * edges with destination in the active set. If the direction is `Out`, | ||
| * `mapFunc` will only be run on edges originating from vertices in the active set. If the | ||
| * direction is `Either`, `mapFunc` will be run on edges with *either* vertex in the active set | ||
| * . If the direction is `Both`, `mapFunc` will be run on edges with *both* vertices in the | ||
| * active set. The active set must have the same index as the graph's vertices. | ||
| * @param activeSetOpt an efficient way to run the aggregation on a subset of the edges if | ||
| * desired. This is done by specifying a set of "active" vertices and an edge direction. The | ||
| * `sendMsg` function will then run only on edges connected to active vertices by edges in the | ||
| * specified direction. If the direction is `In`, `sendMsg` will only be run on edges with | ||
| * destination in the active set. If the direction is `Out`, `sendMsg` will only be run on edges | ||
| * originating from vertices in the active set. If the direction is `Either`, `sendMsg` will be | ||
| * run on edges with *either* vertex in the active set. If the direction is `Both`, `sendMsg` | ||
| * will be run on edges with *both* vertices in the active set. The active set must have the | ||
| * same index as the graph's vertices. | ||
| * | ||
| * @example We can use this function to compute the in-degree of each | ||
| * vertex | ||
|
|
@@ -319,15 +357,88 @@ abstract class Graph[VD: ClassTag, ED: ClassTag] protected () extends Serializab | |
| * predicate or implement PageRank. | ||
| * | ||
| */ | ||
| @deprecated("use aggregateMessages", "1.2.0") | ||
| def mapReduceTriplets[A: ClassTag]( | ||
| mapFunc: EdgeTriplet[VD, ED] => Iterator[(VertexId, A)], | ||
| reduceFunc: (A, A) => A, | ||
| activeSetOpt: Option[(VertexRDD[_], EdgeDirection)] = None) | ||
| : VertexRDD[A] | ||
|
|
||
| /** | ||
| * Joins the vertices with entries in the `table` RDD and merges the results using `mapFunc`. The | ||
| * input table should contain at most one entry for each vertex. If no entry in `other` is | ||
| * Aggregates values from the neighboring edges and vertices of each vertex. The user-supplied | ||
| * `sendMsg` function is invoked on each edge of the graph, generating 0 or more messages to be | ||
| * sent to either vertex in the edge. The `mergeMsg` function is then used to combine all messages | ||
| * destined to the same vertex. | ||
| * | ||
| * @tparam A the type of message to be sent to each vertex | ||
| * | ||
| * @param sendMsg runs on each edge, sending messages to neighboring vertices using the | ||
| * [[EdgeContext]]. | ||
| * @param mergeMsg used to combine messages from `sendMsg` destined to the same vertex. This | ||
| * combiner should be commutative and associative. | ||
| * @param tripletFields which fields should be included in the [[EdgeContext]] passed to the | ||
| * `sendMsg` function. If not all fields are needed, specifying this can improve performance. | ||
| * | ||
| * @example We can use this function to compute the in-degree of each | ||
| * vertex | ||
| * {{{ | ||
| * val rawGraph: Graph[_, _] = Graph.textFile("twittergraph") | ||
| * val inDeg: RDD[(VertexId, Int)] = | ||
| * aggregateMessages[Int](ctx => ctx.sendToDst(1), _ + _) | ||
| * }}} | ||
| * | ||
| * @note By expressing computation at the edge level we achieve | ||
| * maximum parallelism. This is one of the core functions in the | ||
| * Graph API in that enables neighborhood level computation. For | ||
| * example this function can be used to count neighbors satisfying a | ||
| * predicate or implement PageRank. | ||
| * | ||
| */ | ||
| def aggregateMessages[A: ClassTag]( | ||
| sendMsg: EdgeContext[VD, ED, A] => Unit, | ||
| mergeMsg: (A, A) => A, | ||
| tripletFields: TripletFields = TripletFields.All) | ||
|
Contributor
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. parameter and, sendMsg will be construct outside of api, it's not reasonable, i think.
Contributor
Author
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. I don't understand - aggregateMessages needs the user to specify the triplet fields information; it can't reliably infer this on its own. Similarly, the user has to specify sendMsg. |
||
| : VertexRDD[A] = { | ||
| aggregateMessagesWithActiveSet(sendMsg, mergeMsg, tripletFields, None) | ||
| } | ||
|
|
||
| /** | ||
| * Aggregates values from the neighboring edges and vertices of each vertex. The user-supplied | ||
| * `sendMsg` function is invoked on each edge of the graph, generating 0 or more messages to be | ||
| * sent to either vertex in the edge. The `mergeMsg` function is then used to combine all messages | ||
| * destined to the same vertex. | ||
| * | ||
| * This variant can take an active set to restrict the computation and is intended for internal | ||
| * use only. | ||
| * | ||
| * @tparam A the type of message to be sent to each vertex | ||
| * | ||
| * @param sendMsg runs on each edge, sending messages to neighboring vertices using the | ||
| * [[EdgeContext]]. | ||
| * @param mergeMsg used to combine messages from `sendMsg` destined to the same vertex. This | ||
| * combiner should be commutative and associative. | ||
| * @param tripletFields which fields should be included in the [[EdgeContext]] passed to the | ||
| * `sendMsg` function. If not all fields are needed, specifying this can improve performance. | ||
| * @param activeSetOpt an efficient way to run the aggregation on a subset of the edges if | ||
| * desired. This is done by specifying a set of "active" vertices and an edge direction. The | ||
| * `sendMsg` function will then run on only edges connected to active vertices by edges in the | ||
| * specified direction. If the direction is `In`, `sendMsg` will only be run on edges with | ||
| * destination in the active set. If the direction is `Out`, `sendMsg` will only be run on edges | ||
| * originating from vertices in the active set. If the direction is `Either`, `sendMsg` will be | ||
| * run on edges with *either* vertex in the active set. If the direction is `Both`, `sendMsg` | ||
| * will be run on edges with *both* vertices in the active set. The active set must have the | ||
| * same index as the graph's vertices. | ||
| */ | ||
| private[graphx] def aggregateMessagesWithActiveSet[A: ClassTag]( | ||
| sendMsg: EdgeContext[VD, ED, A] => Unit, | ||
| mergeMsg: (A, A) => A, | ||
| tripletFields: TripletFields, | ||
| activeSetOpt: Option[(VertexRDD[_], EdgeDirection)]) | ||
| : VertexRDD[A] | ||
|
|
||
| /** | ||
| * Joins the vertices with entries in the `table` RDD and merges the results using `mapFunc`. | ||
| * The input table should contain at most one entry for each vertex. If no entry in `other` is | ||
| * provided for a particular vertex in the graph, the map function receives `None`. | ||
| * | ||
| * @tparam U the type of entry in the table of updates | ||
|
|
||
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.
it'd be better for long's directly instead of VertexId when we expose this. let's try to make this source compatible though.