Skip to content

Commit

Permalink
Add other small programs to macro
Browse files Browse the repository at this point in the history
  • Loading branch information
aherlihy committed Nov 29, 2023
1 parent 62c134e commit 4a9d33a
Show file tree
Hide file tree
Showing 5 changed files with 353 additions and 50 deletions.
225 changes: 224 additions & 1 deletion bench/src/test/scala/datalog/benchmarks/BenchMacro.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,16 @@ import datalog.{
AckermannOptimizedMacroCompiler,
AckermannWorstMacroCompilerWithFactsOnline as AckermannMacroCompilerWithFactsOnline,
AckermannWorstMacroCompilerOnline as AckermannMacroCompilerOnline,
FibWorstMacroCompiler as FibMacroCompiler,
FibWorstMacroCompilerWithFacts as FibMacroCompilerWithFacts,
FibOptimizedMacroCompiler,
FibWorstMacroCompilerWithFactsOnline as FibMacroCompilerWithFactsOnline,
FibWorstMacroCompilerOnline as FibMacroCompilerOnline,
PrimeWorstMacroCompiler as PrimeMacroCompiler,
PrimeWorstMacroCompilerWithFacts as PrimeMacroCompilerWithFacts,
PrimeOptimizedMacroCompiler,
PrimeWorstMacroCompilerWithFactsOnline as PrimeMacroCompilerWithFactsOnline,
PrimeWorstMacroCompilerOnline as PrimeMacroCompilerOnline,
}

import scala.compiletime.uninitialized
Expand All @@ -24,6 +34,16 @@ object BenchMacro {
val ackermannOptimizedCompiled = AckermannOptimizedMacroCompiler.compile()
val ackermannOnlineCompiled = AckermannMacroCompilerOnline.compile()
val ackermannWithFactsOnlineCompiled = AckermannMacroCompilerWithFactsOnline.compile()
val fibCompiled = FibMacroCompiler.compile()
val fibWithFactsCompiled = FibMacroCompilerWithFacts.compile()
val fibOptimizedCompiled = FibOptimizedMacroCompiler.compile()
val fibOnlineCompiled = FibMacroCompilerOnline.compile()
val fibWithFactsOnlineCompiled = FibMacroCompilerWithFactsOnline.compile()
val primeCompiled = PrimeMacroCompiler.compile()
val primeWithFactsCompiled = PrimeMacroCompilerWithFacts.compile()
val primeOptimizedCompiled = PrimeOptimizedMacroCompiler.compile()
val primeOnlineCompiled = PrimeMacroCompilerOnline.compile()
val primeWithFactsOnlineCompiled = PrimeMacroCompilerWithFactsOnline.compile()
}
import BenchMacro.*

Expand Down Expand Up @@ -108,7 +128,7 @@ class BenchMacro {
* Baseline, interp no optimization
*/
@Benchmark
def ackermann_interpreter_worst_baseline_offline(blackhole: Blackhole) = {
def zackermann_interpreter_worst_baseline_offline(blackhole: Blackhole) = {
val facts = Paths.get(AckermannMacroCompiler.factDir)
val engine = StagedExecutionEngine(DefaultStorageManager(), JITOptions(
mode = Mode.Interpreted, granularity = Granularity.NEVER, sortOrder = SortOrder.Unordered
Expand All @@ -132,4 +152,207 @@ class BenchMacro {
blackhole.consume(res)
// println(s"baseline hand-optimized results =${res.size}")
}

/** ---------- Fib ---------- **/
/**
* Both facts + rules available at compile-time, no online optimization
*/
@Benchmark
def fib_macro_aot_offline(blackhole: Blackhole) = {
val facts = Paths.get(FibMacroCompilerWithFacts.factDir)
val res = FibMacroCompilerWithFacts.runCompiled(fibWithFactsCompiled)( // facts already loaded at compile-time
program => () //// println(s"size succ = ${program.namedRelation("succ").get().size}")
)
blackhole.consume(res)
// println(s"macro AOT offline results =${res.size}")
}

/**
* Only rules available at compile-time, no online optimization
*/
@Benchmark
def fib_macro_runtimefacts_offline(blackhole: Blackhole) = {
val facts = Paths.get(FibMacroCompiler.factDir)
val res = FibMacroCompiler.runCompiled(fibCompiled)(
program =>
// // println(s"size succ = ${program.namedRelation("succ").get().size}")
program.loadFromFactDir(facts.toString)
)
blackhole.consume(res)
// println(s"macro runtimefacts offline, results =${res.size}")
}

/**
* Both facts + rules available at compile-time, online optimization
*/
@Benchmark
def fib_macro_aot_online(blackhole: Blackhole) = {
val facts = Paths.get(FibMacroCompilerWithFactsOnline.factDir)
val res = FibMacroCompilerWithFactsOnline.runCompiled(fibWithFactsOnlineCompiled)(
program => {} // facts already loaded at compile-time
)
blackhole.consume(res)
// println(s"macro AOT online, results =${res.size}")
}

/**
* Only rules available at compile-time, online optimization
*/
@Benchmark
def fib_macro_runtimefacts_online(blackhole: Blackhole) = {
val facts = Paths.get(FibMacroCompilerOnline.factDir)
val res = FibMacroCompilerOnline.runCompiled(fibOnlineCompiled)(
program => program.loadFromFactDir(facts.toString)
)
blackhole.consume(res)
// println(s"macro runtimefacts online, results =${res.size}")
}

/**
* Nothing available at compile-time, runtime optimization, lambda
*/
@Benchmark
def fib_jit_lambda_online(blackhole: Blackhole) = {
val facts = Paths.get(FibMacroCompiler.factDir)
val engine = StagedExecutionEngine(DefaultStorageManager(), JITOptions(
backend = Backend.Lambda,
mode = Mode.JIT, granularity = Granularity.DELTA, compileSync = CompileSync.Blocking, sortOrder = SortOrder.Sel
))
val program = FibMacroCompiler.makeProgram(engine)
program.loadFromFactDir(facts.toString)
val res = program.namedRelation(program.toSolve).solve()
blackhole.consume(res)
// println(s"lambda results =${res.size}")
}

/**
* Baseline, interp no optimization
*/
@Benchmark
def zfib_interpreter_worst_baseline_offline(blackhole: Blackhole) = {
val facts = Paths.get(FibMacroCompiler.factDir)
val engine = StagedExecutionEngine(DefaultStorageManager(), JITOptions(
mode = Mode.Interpreted, granularity = Granularity.NEVER, sortOrder = SortOrder.Unordered
))
val program = FibMacroCompiler.makeProgram(engine)
program.loadFromFactDir(facts.toString)
val res = program.namedRelation(program.toSolve).solve()
blackhole.consume(res)
// println(s"baseline results =${res.size}")
}

@Benchmark
def fib_interpreter_best_baseline_offline(blackhole: Blackhole) = {
val facts = Paths.get(FibOptimizedMacroCompiler.factDir)
val engine = StagedExecutionEngine(DefaultStorageManager(), JITOptions(
mode = Mode.Interpreted, granularity = Granularity.NEVER, sortOrder = SortOrder.Unordered
))
val program = FibOptimizedMacroCompiler.makeProgram(engine)
program.loadFromFactDir(facts.toString)
val res = program.namedRelation(program.toSolve).solve()
blackhole.consume(res)
// println(s"baseline hand-optimized results =${res.size}")
}

/** ---------- Prime ---------- * */

/**
* Both facts + rules available at compile-time, no online optimization
*/
@Benchmark
def prime_macro_aot_offline(blackhole: Blackhole) = {
val facts = Paths.get(PrimeMacroCompilerWithFacts.factDir)
val res = PrimeMacroCompilerWithFacts.runCompiled(primeWithFactsCompiled)( // facts already loaded at compile-time
program => () //// println(s"size succ = ${program.namedRelation("succ").get().size}")
)
blackhole.consume(res)
// println(s"macro AOT offline results =${res.size}")
}

/**
* Only rules available at compile-time, no online optimization
*/
@Benchmark
def prime_macro_runtimefacts_offline(blackhole: Blackhole) = {
val facts = Paths.get(PrimeMacroCompiler.factDir)
val res = PrimeMacroCompiler.runCompiled(primeCompiled)(
program =>
// // println(s"size succ = ${program.namedRelation("succ").get().size}")
program.loadFromFactDir(facts.toString)
)
blackhole.consume(res)
// println(s"macro runtimefacts offline, results =${res.size}")
}

/**
* Both facts + rules available at compile-time, online optimization
*/
@Benchmark
def prime_macro_aot_online(blackhole: Blackhole) = {
val facts = Paths.get(PrimeMacroCompilerWithFactsOnline.factDir)
val res = PrimeMacroCompilerWithFactsOnline.runCompiled(primeWithFactsOnlineCompiled)(
program => {} // facts already loaded at compile-time
)
blackhole.consume(res)
// println(s"macro AOT online, results =${res.size}")
}

/**
* Only rules available at compile-time, online optimization
*/
@Benchmark
def prime_macro_runtimefacts_online(blackhole: Blackhole) = {
val facts = Paths.get(PrimeMacroCompilerOnline.factDir)
val res = PrimeMacroCompilerOnline.runCompiled(primeOnlineCompiled)(
program => program.loadFromFactDir(facts.toString)
)
blackhole.consume(res)
// println(s"macro runtimefacts online, results =${res.size}")
}

/**
* Nothing available at compile-time, runtime optimization, lambda
*/
@Benchmark
def prime_jit_lambda_online(blackhole: Blackhole) = {
val facts = Paths.get(PrimeMacroCompiler.factDir)
val engine = StagedExecutionEngine(DefaultStorageManager(), JITOptions(
backend = Backend.Lambda,
mode = Mode.JIT, granularity = Granularity.DELTA, compileSync = CompileSync.Blocking, sortOrder = SortOrder.Sel
))
val program = PrimeMacroCompiler.makeProgram(engine)
program.loadFromFactDir(facts.toString)
val res = program.namedRelation(program.toSolve).solve()
blackhole.consume(res)
// println(s"lambda results =${res.size}")
}

/**
* Baseline, interp no optimization
*/
@Benchmark
def zprime_interpreter_worst_baseline_offline(blackhole: Blackhole) = {
val facts = Paths.get(PrimeMacroCompiler.factDir)
val engine = StagedExecutionEngine(DefaultStorageManager(), JITOptions(
mode = Mode.Interpreted, granularity = Granularity.NEVER, sortOrder = SortOrder.Unordered
))
val program = PrimeMacroCompiler.makeProgram(engine)
program.loadFromFactDir(facts.toString)
val res = program.namedRelation(program.toSolve).solve()
blackhole.consume(res)
// println(s"baseline results =${res.size}")
}

@Benchmark
def prime_interpreter_best_baseline_offline(blackhole: Blackhole) = {
val facts = Paths.get(PrimeOptimizedMacroCompiler.factDir)
val engine = StagedExecutionEngine(DefaultStorageManager(), JITOptions(
mode = Mode.Interpreted, granularity = Granularity.NEVER, sortOrder = SortOrder.Unordered
))
val program = PrimeOptimizedMacroCompiler.makeProgram(engine)
program.loadFromFactDir(facts.toString)
val res = program.namedRelation(program.toSolve).solve()
blackhole.consume(res)
// println(s"baseline hand-optimized results =${res.size}")
}
}
108 changes: 105 additions & 3 deletions src/test/scala/test/MacroCompilerPrograms.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ import datalog.dsl.*
import datalog.execution.ir.*
import datalog.storage.{DefaultStorageManager, StorageManager}
import test.examples.ackermann.*
import test.examples.cbaexprvalue.*
import test.examples.equal.*
import test.examples.fib.*
import test.examples.prime.*
import test.examples.fib.*
import test.examples.prime.*
import test.examples.tastyslistlib.*
import test.examples.tastyslistlibinverse.*
import test.examples.prime.*

import java.nio.file.Paths

Expand Down Expand Up @@ -96,3 +96,105 @@ object AckermannWorstMacroCompilerWithFactsOnline extends MacroCompiler(Ackerman
inline def compile(): StorageManager => Any = ${compileImpl()}
val factDir = s"${BuildInfo.baseDirectory}/src/test/scala/test/examples/ackermann/facts"
}

/** ---------- Fib ---------- **/
class FibOptimizedProgram(engine: ExecutionEngine) extends SolvableProgram(engine) with fib_optimized {
val factDir = s"${BuildInfo.baseDirectory}/src/test/scala/test/examples/fib/facts"
initializeEmptyFactsFromDir(factDir)
pretest(this)
}
object FibOptimizedMacroCompiler extends MacroCompiler(FibOptimizedProgram(_), JITOptions(backend = Backend.MacroQuotes, sortOrder = SortOrder.IntMax)) {
inline def compile(): StorageManager => Any = ${this.compileImpl()}
val factDir = s"${BuildInfo.baseDirectory}/src/test/scala/test/examples/fib/facts"
}

class FibWorstProgram(engine: ExecutionEngine) extends SolvableProgram(engine) with fib_worst {
val factDir = s"${BuildInfo.baseDirectory}/src/test/scala/test/examples/fib/facts"
initializeEmptyFactsFromDir(factDir)
pretest(this)
}
object FibWorstMacroCompiler extends MacroCompiler(FibWorstProgram(_), JITOptions(backend = Backend.MacroQuotes, sortOrder = SortOrder.IntMax)) {
inline def compile(): StorageManager => Any = ${this.compileImpl()}
val factDir = s"${BuildInfo.baseDirectory}/src/test/scala/test/examples/fib/facts"
}

class FibWorstProgramWithFacts(engine: ExecutionEngine) extends SolvableProgram(engine) with fib_worst {
val factDir = s"${BuildInfo.baseDirectory}/src/test/scala/test/examples/fib/facts"
loadFromFactDir(factDir)
pretest(this)
}
object FibWorstMacroCompilerWithFacts extends MacroCompiler(FibWorstProgramWithFacts(_), JITOptions(backend = Backend.MacroQuotes, sortOrder = SortOrder.IntMax)) {
inline def compile(): StorageManager => Any = ${compileImpl()}
val factDir = s"${BuildInfo.baseDirectory}/src/test/scala/test/examples/fib/facts"
}
class FibWorstProgramOnline(engine: ExecutionEngine) extends SolvableProgram(engine) with fib_worst {
val factDir = s"${BuildInfo.baseDirectory}/src/test/scala/test/examples/fib/facts"
initializeEmptyFactsFromDir(factDir)
pretest(this)
}
object FibWorstMacroCompilerOnline extends MacroCompiler(FibWorstProgramOnline(_), JITOptions(backend = Backend.MacroQuotes, sortOrder = SortOrder.IntMax, runtimeSort = SortOrder.Sel)) {
inline def compile(): StorageManager => Any = ${this.compileImpl()}
val factDir = s"${BuildInfo.baseDirectory}/src/test/scala/test/examples/fib/facts"
}

class FibWorstProgramWithFactsOnline(engine: ExecutionEngine) extends SolvableProgram(engine) with fib_worst {
val factDir = s"${BuildInfo.baseDirectory}/src/test/scala/test/examples/fib/facts"
loadFromFactDir(factDir)
pretest(this)
}
object FibWorstMacroCompilerWithFactsOnline extends MacroCompiler(FibWorstProgramWithFactsOnline(_), JITOptions(backend = Backend.MacroQuotes, sortOrder = SortOrder.IntMax, runtimeSort = SortOrder.Sel)) {
inline def compile(): StorageManager => Any = ${compileImpl()}
val factDir = s"${BuildInfo.baseDirectory}/src/test/scala/test/examples/fib/facts"
}

/** ---------- Prime ---------- **/
class PrimeOptimizedProgram(engine: ExecutionEngine) extends SolvableProgram(engine) with prime_optimized {
val factDir = s"${BuildInfo.baseDirectory}/src/test/scala/test/examples/prime/facts"
initializeEmptyFactsFromDir(factDir)
pretest(this)
}
object PrimeOptimizedMacroCompiler extends MacroCompiler(PrimeOptimizedProgram(_), JITOptions(backend = Backend.MacroQuotes, sortOrder = SortOrder.IntMax)) {
inline def compile(): StorageManager => Any = ${this.compileImpl()}
val factDir = s"${BuildInfo.baseDirectory}/src/test/scala/test/examples/prime/facts"
}

class PrimeWorstProgram(engine: ExecutionEngine) extends SolvableProgram(engine) with prime_worst {
val factDir = s"${BuildInfo.baseDirectory}/src/test/scala/test/examples/prime/facts"
initializeEmptyFactsFromDir(factDir)
pretest(this)
}
object PrimeWorstMacroCompiler extends MacroCompiler(PrimeWorstProgram(_), JITOptions(backend = Backend.MacroQuotes, sortOrder = SortOrder.IntMax)) {
inline def compile(): StorageManager => Any = ${this.compileImpl()}
val factDir = s"${BuildInfo.baseDirectory}/src/test/scala/test/examples/prime/facts"
}

class PrimeWorstProgramWithFacts(engine: ExecutionEngine) extends SolvableProgram(engine) with prime_worst {
val factDir = s"${BuildInfo.baseDirectory}/src/test/scala/test/examples/prime/facts"
loadFromFactDir(factDir)
pretest(this)
}
object PrimeWorstMacroCompilerWithFacts extends MacroCompiler(PrimeWorstProgramWithFacts(_), JITOptions(backend = Backend.MacroQuotes, sortOrder = SortOrder.IntMax)) {
inline def compile(): StorageManager => Any = ${compileImpl()}
val factDir = s"${BuildInfo.baseDirectory}/src/test/scala/test/examples/prime/facts"
}
class PrimeWorstProgramOnline(engine: ExecutionEngine) extends SolvableProgram(engine) with prime_worst {
val factDir = s"${BuildInfo.baseDirectory}/src/test/scala/test/examples/prime/facts"
initializeEmptyFactsFromDir(factDir)
pretest(this)
}
object PrimeWorstMacroCompilerOnline extends MacroCompiler(PrimeWorstProgramOnline(_), JITOptions(backend = Backend.MacroQuotes, sortOrder = SortOrder.IntMax, runtimeSort = SortOrder.Sel)) {
inline def compile(): StorageManager => Any = ${this.compileImpl()}
val factDir = s"${BuildInfo.baseDirectory}/src/test/scala/test/examples/prime/facts"
}

class PrimeWorstProgramWithFactsOnline(engine: ExecutionEngine) extends SolvableProgram(engine) with prime_worst {
val factDir = s"${BuildInfo.baseDirectory}/src/test/scala/test/examples/prime/facts"
loadFromFactDir(factDir)
pretest(this)
}
object PrimeWorstMacroCompilerWithFactsOnline extends MacroCompiler(PrimeWorstProgramWithFactsOnline(_), JITOptions(backend = Backend.MacroQuotes, sortOrder = SortOrder.IntMax, runtimeSort = SortOrder.Sel)) {
inline def compile(): StorageManager => Any = ${compileImpl()}
val factDir = s"${BuildInfo.baseDirectory}/src/test/scala/test/examples/prime/facts"
}


22 changes: 22 additions & 0 deletions src/test/scala/test/examples/prime/facts/succ.facts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
String String
0 1
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10
10 11
11 12
12 13
13 14
14 15
15 16
16 17
17 18
18 19
19 20
20 21
Loading

0 comments on commit 4a9d33a

Please sign in to comment.