Skip to content

Commit 29b5ad1

Browse files
committed
All tests for agreement of results pass.
That prerequisite for sane benchmarking is therefore met.
1 parent 75a625d commit 29b5ad1

File tree

1 file changed

+53
-43
lines changed

1 file changed

+53
-43
lines changed

benchmark/src/main/scala/bench/CodeGen.scala

Lines changed: 53 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,24 @@ import scala.util._
44
import control.NonFatal
55

66
object Generator {
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)) })
7+
// Trailing * means that a collection is not expected to work efficiently in parallel
8+
// Trailing ! means that a collection is not expected to maintain original order
9+
val annotated = "arr ish! lst* ils*! que* stm* trs*! vec arb ars ast* mhs! lhs*! prq*! muq* wra jix jln".split(' ')
10+
11+
// Parallel version if any appears after /
12+
// Trailing ! means that collection must maintain original order (i.e. don't use if collection is marked !)
13+
val allops = Seq(("OnInt", "I", "sum/psum trig/ptrig fmc/pfmc mdtc!"), ("OnString", "S", "nbr/pnbr htrg/phtrg fmc/pfmc mdtc!"))
14+
15+
def parsefs(fs: String) = fs.split(' ').map(_.split('/') match {
16+
case Array(x) => (x.takeWhile(_.isLetter), None, x contains "!")
17+
case Array(x,y) => (x.takeWhile(_.isLetter), Some(y.takeWhile(_.isLetter)), (x+y) contains "!")
18+
})
1019

1120
val names = annotated.map(_.takeWhile(_.isLetter))
1221
val nojname = names.filterNot(_ startsWith "j").toSet
13-
val parname = annotated.filter(_.forall(_.isLetter)).toSet
22+
val parname = annotated.filterNot(_ contains "*").map(_.takeWhile(_.isLetter)).toSet
1423
val sqnname = names.filterNot(parname).toSet union names.filterNot(nojname).toSet
24+
val ordname = annotated.filterNot(_ contains "!").map(_.takeWhile(_.isLetter)).toSet
1525

1626
def writeTo(f: java.io.File)(pr: (String => Unit) => Unit): Either[Throwable, Unit] = {
1727
try {
@@ -30,53 +40,53 @@ object Generator {
3040
val q = "\""
3141
if (target.exists) throw new java.io.IOException("Generator will not write to existing file: " + target.getPath)
3242
writeTo(target){ pr =>
33-
pr("""package bench.test""")
34-
pr("""""")
35-
pr("""import bench.generate._, bench.operate._, bench.generate.EnableIterators._""")
36-
pr("""import scala.compat.java8.StreamConverters._""")
37-
pr("""""")
38-
pr("""object Agreement {""")
39-
pr(""" def run() {""")
40-
pr(""" val wrong = new collection.mutable.ArrayBuffer[String]""")
41-
pr(""" def check[A](a1: A, a2: => A, msg: String) {""")
42-
pr(""" var t = System.nanoTime""")
43-
pr(""" if (!CloseEnough(a1, { val ans = a2; t = System.nanoTime - t; ans}))""")
44-
pr(""" wrong += msg""")
45-
pr(""" if (t > 2000000000) wrong += "Slow " + msg""")
46-
pr(""" }""")
47-
pr( s" val m = (new bench.generate.Things(${sayArrayI(sizes)})).N;" )
43+
pr( """package bench.test""")
44+
pr( """""")
45+
pr( """import bench.generate._, bench.operate._, bench.generate.EnableIterators._""")
46+
pr( """import scala.compat.java8.StreamConverters._""")
47+
pr( """""")
48+
pr( """object Agreement {""")
49+
pr( """ def run() {""")
50+
pr( """ val wrong = new collection.mutable.ArrayBuffer[String]""")
51+
pr( """ def check[A](a1: A, a2: => A, msg: String) {""")
52+
pr( """ var t = System.nanoTime""")
53+
pr( """ if (!CloseEnough(a1, { val ans = a2; t = System.nanoTime - t; ans}))""")
54+
pr( """ wrong += msg""")
55+
pr( """ if (t > 2000000000) wrong += "Slow " + msg""")
56+
pr( """ }""")
57+
pr( s" val m = (new bench.generate.Things(${sayArrayI(sizes)})).N;" )
4858
allops.foreach{ case (o, t, fs) =>
4959
names.foreach{ n =>
50-
pr( s" { // Scope for operations $o collection $n")
51-
pr( s" val x = new bench.generate.Things(${sayArrayI(sizes)})" )
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)")
60+
pr( s" { // Scope for operations $o collection $n")
61+
pr( s" val x = new bench.generate.Things(${sayArrayI(sizes)})" )
62+
parsefs(fs).foreach{ case (f, pf, ord) =>
63+
if (ordname(n) || !ord) {
64+
pr( """ for (i <- 0 until m) {""")
65+
pr( s" val z = $o.$f(x.arr.c$t(i))")
66+
if (nojname(n)) {
67+
pr( s" check(z, $o.$f(x.$n.c$t(i)), ${q}c$t $f $n ${q}+i.toString)");
68+
pr( s" check(z, $o.$f(x.$n.i$t(i)), ${q}i$t $f $n ${q}+i.toString)")
6369
}
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)")
70+
if (sqnname(n)) {
71+
pr( s" check(z, $o.$f(x.$n.ss$t(i)), ${q}ss$t $f $n ${q}+i.toString)")
72+
if (nojname(n))
73+
pr( s" check(z, $o.$f(x.$n.zs$t(i)), ${q}zs$t $f $n ${q}+i.toString)")
74+
}
75+
if (parname(n) && pf.isDefined) {
76+
pr( s" check(z, $o.$f(x.$n.sp$t(i)), ${q}sp$t $f $n ${q}+i.toString)")
77+
if (nojname(n))
78+
pr( s" check(z, $o.$f(x.$n.zp$t(i)), ${q}zp$t $f $n ${q}+i.toString)")
6979
}
80+
pr( s" }")
7081
}
71-
pr( s" }")
7282
}
73-
pr( s" } // End scope for operations $o collection $n")
83+
pr( s" } // End scope for operations $o collection $n")
7484
}
7585
}
76-
pr(""" wrong.foreach(println)""")
77-
pr(""" if (wrong.nonEmpty) sys.exit(1) """)
78-
pr(""" }""")
79-
pr("""}""")
86+
pr( """ wrong.foreach(println)""")
87+
pr( """ if (wrong.nonEmpty) sys.exit(1) """)
88+
pr( """ }""")
89+
pr( """}""")
8090
} match {
8191
case Left(t) => println("Did not successfully write file: " + target.getPath); throw t
8292
case _ =>

0 commit comments

Comments
 (0)