-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
39 additions
and
2 deletions.
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 |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package workflow | ||
|
||
/** | ||
* Optimizes a Pipeline DAG | ||
*/ | ||
object Optimizer extends RuleExecutor { | ||
protected val batches: Seq[Batch] = Batch("DAG Optimization", FixedPoint(100), EquivalentNodeMerger) :: Nil | ||
} | ||
|
||
/** | ||
* Merges equivalent node indices in the DAG. | ||
* Nodes are considered equivalent if: | ||
* - The nodes at the indices themselves are equal | ||
* - They point to the same data dependencies | ||
* - They point to the same fit dependencies | ||
*/ | ||
object EquivalentNodeMerger extends Rule { | ||
def apply[A, B](plan: Pipeline[A, B]): Pipeline[A, B] = { | ||
val fullNodes = plan.nodes.zip(plan.dataDeps.zip(plan.fitDeps)).zipWithIndex | ||
val mergableNodes = fullNodes.groupBy(_._1).mapValues(_.map(_._2)).toSeq | ||
|
||
if (mergableNodes.size == plan.nodes.size) { | ||
// no nodes are mergable | ||
plan | ||
} else { | ||
val oldToNewNodeMapping = mergableNodes.zipWithIndex.map(x => x._1._2.map(y => (y, x._2))).flatMap(identity).toMap + (Pipeline.SOURCE -> Pipeline.SOURCE) | ||
val newNodes = mergableNodes.map(_._1._1) | ||
val newDataDeps = mergableNodes.map(_._1._2._1.map(x => oldToNewNodeMapping(x))) | ||
val newFitDeps = mergableNodes.map(_._1._2._2.map(x => oldToNewNodeMapping(x))) | ||
val newSink = oldToNewNodeMapping(plan.sink) | ||
|
||
Pipeline(newNodes, newDataDeps, newFitDeps, newSink) | ||
} | ||
} | ||
} |