-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Make metals happy * Include metals files in gitignore * .map .sequence => .traverse * First cut -- ParallelInterpreter * Update changelog * Prove consistency with NaiveInterpreter * Fix evaluation (thanks, tests!) * scalafmt * Add target monad to interpreter so ParallelInterpreter can extend it Necessary for downstream use in e.g. geotrellis-server * Add sleep expression and use it to test parallelism * Don't know how to interpret sleep by default * Make test look better * ParallelInterpreter -> ConcurrentInterpreter * Add actually parallel interpreter + tests
- Loading branch information
1 parent
f92a30b
commit 81197d4
Showing
15 changed files
with
844 additions
and
12 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -48,3 +48,6 @@ nohup.out | |
|
||
site/ | ||
.metadata/ | ||
|
||
.metals | ||
.bloop |
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 @@ | ||
version=2.0.0-RC4 |
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,58 @@ | ||
package com.azavea.maml.eval | ||
|
||
import com.azavea.maml.ast._ | ||
import com.azavea.maml.error._ | ||
import com.azavea.maml.eval.directive._ | ||
|
||
import cats._ | ||
import cats.implicits._ | ||
import cats.data.Validated._ | ||
import cats.data.{NonEmptyList => NEL, _} | ||
import cats.effect.{Concurrent, Fiber} | ||
|
||
import scala.reflect.ClassTag | ||
|
||
class ConcurrentInterpreter[F[_]](directives: List[Directive])( | ||
implicit Conc: Concurrent[F] | ||
) extends Interpreter[F] { | ||
def apply(exp: Expression): F[Interpreted[Result]] = { | ||
val children = evalInF(exp) | ||
val out = children map { | ||
_.andThen({ childRes => | ||
instructions(exp, childRes) | ||
}) | ||
} | ||
out | ||
} | ||
|
||
def evalInF(expression: Expression): F[Interpreted[List[Result]]] = { | ||
val fibsF: F[List[Fiber[F, Interpreted[Result]]]] = | ||
expression.children traverse { expr => | ||
Conc.start(apply(expr)) | ||
} | ||
fibsF flatMap { _.traverse { _.join } } map { _.sequence } | ||
} | ||
|
||
val fallbackDirective: Directive = { | ||
case (exp, res) => Invalid(NEL.of(UnhandledCase(exp, exp.kind))) | ||
} | ||
|
||
def prependDirective(directive: Directive) = | ||
new ConcurrentInterpreter[F](directive +: directives) | ||
|
||
def appendDirective(directive: Directive) = | ||
new ConcurrentInterpreter[F](directives :+ directive) | ||
|
||
def instructions( | ||
expression: Expression, | ||
children: List[Result] | ||
): Interpreted[Result] = | ||
directives | ||
.reduceLeft(_ orElse _) | ||
.orElse(fallbackDirective)((expression, children)) | ||
} | ||
|
||
object ConcurrentInterpreter { | ||
def DEFAULT[T[_]: Concurrent] = | ||
new ConcurrentInterpreter[T](NaiveInterpreter.DEFAULT.directives) | ||
} |
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,65 @@ | ||
package com.azavea.maml.eval | ||
|
||
import com.azavea.maml.ast._ | ||
import com.azavea.maml.error._ | ||
import com.azavea.maml.eval.directive._ | ||
|
||
import cats._ | ||
import cats.implicits._ | ||
import cats.data.Validated._ | ||
import cats.data.{NonEmptyList => NEL, _} | ||
import cats.effect.ContextShift | ||
|
||
import scala.reflect.ClassTag | ||
|
||
class ParallelInterpreter[F[_]: Monad, G[_]](directives: List[Directive])( | ||
implicit Par: Parallel[F, G], | ||
contextShift: ContextShift[F] | ||
) extends Interpreter[F] { | ||
def apply(exp: Expression): F[Interpreted[Result]] = { | ||
val children = evalInF(exp) | ||
val out = children map { | ||
_.andThen({ childRes => | ||
instructions(exp, childRes) | ||
}) | ||
} | ||
out | ||
} | ||
|
||
def evalInF( | ||
expression: Expression | ||
)(implicit contextShift: ContextShift[F]): F[Interpreted[List[Result]]] = { | ||
val resultsF: F[List[Interpreted[Result]]] = | ||
expression.children parTraverse { expr => | ||
apply(expr) | ||
} | ||
resultsF map { _.sequence } | ||
} | ||
|
||
val fallbackDirective: Directive = { | ||
case (exp, res) => Invalid(NEL.of(UnhandledCase(exp, exp.kind))) | ||
} | ||
|
||
def prependDirective(directive: Directive) = | ||
new ParallelInterpreter[F, G](directive +: directives) | ||
|
||
def appendDirective(directive: Directive) = | ||
new ParallelInterpreter[F, G](directives :+ directive) | ||
|
||
def instructions( | ||
expression: Expression, | ||
children: List[Result] | ||
): Interpreted[Result] = | ||
directives | ||
.reduceLeft(_ orElse _) | ||
.orElse(fallbackDirective)((expression, children)) | ||
} | ||
|
||
object ParallelInterpreter { | ||
def DEFAULT[T[_], U[_]]( | ||
implicit P: Parallel[T, U], | ||
M: Monad[T], | ||
contextShift: ContextShift[T] | ||
) = | ||
new ParallelInterpreter[T, U](NaiveInterpreter.DEFAULT.directives) | ||
} |
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
Oops, something went wrong.