|
1 | 1 | package bench.codegen
|
2 | 2 |
|
3 | 3 | import scala.util._
|
4 |
| -import scala.util.control.NonFatal |
| 4 | +import control.NonFatal |
5 | 5 |
|
6 | 6 | object Generator {
|
7 | 7 | val annotated = "arr ish lst* ils* que* stm* trs* vec arb ars ast* mhs lhs* prq* muq* wra jix jln".split(' ')
|
| 8 | + val allops = Seq(("OnInt", "I", "sum/psum trig/ptrig fmc/pfmc mdtc"), ("OnString", "S", "nbr/pnbr htrg/phtrg fmc/pfmc mdtc")) |
| 9 | + def parsefs(fs: String) = fs.split(' ').map(_.split('/') match { case Array(x) => (x, None); case Array(x,y) => (x, Some(y)) }) |
| 10 | + |
8 | 11 | val names = annotated.map(_.takeWhile(_.isLetter))
|
9 |
| - val nojnames = names.filterNot(_ startsWith "j") |
| 12 | + val nojname = names.filterNot(_ startsWith "j").toSet |
| 13 | + val parname = annotated.filter(_.forall(_.isLetter)).toSet |
| 14 | + val sqnname = names.filterNot(parname).toSet union names.filterNot(nojname).toSet |
10 | 15 |
|
11 | 16 | def writeTo(f: java.io.File)(pr: (String => Unit) => Unit): Either[Throwable, Unit] = {
|
12 | 17 | try {
|
@@ -35,24 +40,39 @@ object Generator {
|
35 | 40 | pr(""" val wrong = new collection.mutable.ArrayBuffer[String]""")
|
36 | 41 | pr(""" def check[A](a1: A, a2: => A, msg: String) {""")
|
37 | 42 | pr(""" var t = System.nanoTime""")
|
38 |
| - pr(""" if (!CloseEnough(a1, { val ans = a2; t = System.nanoTime - t; ans})) wrong += msg""") |
| 43 | + pr(""" if (!CloseEnough(a1, { val ans = a2; t = System.nanoTime - t; ans}))""") |
| 44 | + pr(""" wrong += msg""") |
39 | 45 | pr(""" if (t > 2000000000) wrong += "Slow " + msg""")
|
40 | 46 | pr(""" }""")
|
| 47 | + pr( s" val m = (new bench.generate.Things(${sayArrayI(sizes)})).N;" ) |
| 48 | + allops.foreach{ case (o, t, fs) => |
| 49 | + names.foreach{ n => |
| 50 | + pr( s" { // Scope for operations $o collection $n") |
41 | 51 | pr( s" val x = new bench.generate.Things(${sayArrayI(sizes)})" )
|
42 |
| - pr(""" for (i <- 0 until x.N) {""") |
43 |
| - pr(""" val si = OnInt.sum(x.arr.cI(i))""") |
44 |
| - nojnames.tail.foreach{ n => |
45 |
| - pr( s" check(si, OnInt.sum(x.$n.cI(i)), ${q}cI sum $n ${q}+i.toString)") |
46 |
| - } |
47 |
| - nojnames.foreach{ n => |
48 |
| - pr( s" check(si, OnInt.sum(x.$n.iI(i)), ${q}iI sum $n ${q}+i.toString)") |
49 |
| - } |
50 |
| - annotated.foreach{ m => |
51 |
| - val n = m.takeWhile(_.isLetter) |
52 |
| - val c = if (m contains "*") "ssI" else "spI" |
53 |
| - pr( s" check(si, OnInt.sum(x.$n.$c(i)), ${q}$c sum $n ${q}+i.toString)") |
54 |
| - } |
| 52 | + parsefs(fs).foreach{ case (f, pf) => |
| 53 | + pr(""" for (i <- 0 until m) {""") |
| 54 | + pr( s" val z = $o.$f(x.arr.c$t(i))") |
| 55 | + if (nojname(n)) { |
| 56 | + pr( s" check(z, $o.$f(x.$n.c$t(i)), ${q}c$t $f $n ${q}+i.toString)"); |
| 57 | + pr( s" check(z, $o.$f(x.$n.i$t(i)), ${q}i$t $f $n ${q}+i.toString)") |
| 58 | + } |
| 59 | + if (sqnname(n)) { |
| 60 | + pr( s" check(z, $o.$f(x.$n.ss$t(i)), ${q}ss$t $f $n ${q}+i.toString)") |
| 61 | + if (nojname(n)) { |
| 62 | + pr( s" check(z, $o.$f(x.$n.zs$t(i)), ${q}zs$t $f $n ${q}+i.toString)") |
| 63 | + } |
| 64 | + } |
| 65 | + if (parname(n)) { |
| 66 | + pr( s" check(z, $o.$f(x.$n.sp$t(i)), ${q}sp$t $f $n ${q}+i.toString)") |
| 67 | + if (nojname(n)) { |
| 68 | + pr( s" check(z, $o.$f(x.$n.zp$t(i)), ${q}zp$t $f $n ${q}+i.toString)") |
| 69 | + } |
| 70 | + } |
55 | 71 | pr( s" }")
|
| 72 | + } |
| 73 | + pr( s" } // End scope for operations $o collection $n") |
| 74 | + } |
| 75 | + } |
56 | 76 | pr(""" wrong.foreach(println)""")
|
57 | 77 | pr(""" if (wrong.nonEmpty) sys.exit(1) """)
|
58 | 78 | pr(""" }""")
|
|
0 commit comments