Skip to content

Commit ec9e9df

Browse files
committed
Add injection for int and long
1 parent cef500f commit ec9e9df

File tree

13 files changed

+371
-318
lines changed

13 files changed

+371
-318
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,3 +161,4 @@ project/metals.sbt
161161
project/project/project/metals.sbt
162162
project/project/metals.sbt
163163
.bsp/*
164+
.metals/

bench/src/main/scala/benchmarks/Settings.scala

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ package benchmarks
33
import scala.collection.mutable.Map
44

55
object Settings {
6-
val v_s : Integer = Integer.getInteger("benchmark.v" , 100000000);
7-
val vHi_s : Integer = Integer.getInteger("benchmark.vHi" , 10000000);
8-
val vLo_s : Integer = Integer.getInteger("benchmark.vLo" , 10);
9-
val vFaZ_s : Integer = Integer.getInteger("benchmark.vFaZ", 10000);
10-
val vZaF_s : Integer = Integer.getInteger("benchmark.vZaF", 10000000);
11-
val vLimit_s : Integer = Integer.getInteger("benchmark.vLimit", 20000000);
6+
val v_s : Int = Integer.getInteger("benchmark.v" , 100000000);
7+
val vHi_s : Int = Integer.getInteger("benchmark.vHi" , 10000000);
8+
val vLo_s : Int = Integer.getInteger("benchmark.vLo" , 10);
9+
val vFaZ_s : Int = Integer.getInteger("benchmark.vFaZ", 10000);
10+
val vZaF_s : Int = Integer.getInteger("benchmark.vZaF", 10000000);
11+
val vLimit_s : Int = Integer.getInteger("benchmark.vLimit", 20000000);
1212
}

bench/src/main/scala/benchmarks/TestPipelines.scala

Lines changed: 69 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -3,140 +3,139 @@ package benchmarks
33
import scala.quoted._
44
import scala.quoted.staging._
55
import strymonas._
6+
import strymonas.Code.given
67

78
object TestPipelines {
89
given Compiler = Compiler.make(getClass.getClassLoader)
9-
import Settings._
10-
11-
import strymonas.Code._
12-
import strymonas.Code.given
13-
import scala.language.implicitConversions
10+
given Raw = Raw(Code)
1411

1512
def sumPipeline(using Quotes) = '{ (array: Array[Long]) =>
1613
${ Cooked.of('{array})
17-
.fold(long(0), _+_) }
14+
.fold(0L, _+_) }
1815
}
1916

2017
def sumOfSquaresPipeline(using Quotes) = '{ (array: Array[Long]) =>
2118
${ Cooked.of('{array})
22-
.map((a) => a * a)
23-
.fold(long(0), _+_) }
19+
.map(a => a * a)
20+
.fold(0L, _+_) }
2421
}
2522

2623
def sumOfSquaresEvenPipeline(using Quotes) = '{ (array: Array[Long]) =>
2724
${ Cooked.of('{array})
28-
.filter(d => (d mod long(2)) === long(0))
29-
.map((a) => a * a )
30-
.fold(long(0), _+_) }
25+
.filter(d => (d mod 2L) === 0L)
26+
.map(a => a * a )
27+
.fold(0L, _+_) }
3128
}
3229

3330
def cartPipeline(using Quotes) = '{ (vHi: Array[Long], vLo: Array[Long]) =>
3431
${ Cooked.of('{vHi})
35-
.flatMap((d) => Cooked.of('{vLo}).map((dp) => d * dp))
36-
.fold(long(0), _+_) }
32+
.flatMap(d => Cooked.of('{vLo}).map(dp => d * dp))
33+
.fold(0L, _+_) }
3734
}
3835

3936
def mapsMegamorphicPipeline(using Quotes) = '{ (array: Array[Long]) =>
4037
${ Cooked.of('{array})
41-
.map((a) => a * long(1))
42-
.map((a) => a * long(2))
43-
.map((a) => a * long(3))
44-
.map((a) => a * long(4))
45-
.map((a) => a * long(5))
46-
.map((a) => a * long(6))
47-
.map((a) => a * long(7))
48-
.fold(long(0), _+_) }
38+
.map(a => a * 1L)
39+
.map(a => a * 2L)
40+
.map(a => a * 3L)
41+
.map(a => a * 4L)
42+
.map(a => a * 5L)
43+
.map(a => a * 6L)
44+
.map(a => a * 7L)
45+
.fold(0L, _+_) }
4946
}
5047

5148
def filtersMegamorphicPipeline(using Quotes) = '{ (array: Array[Long]) =>
5249
${ Cooked.of('{array})
53-
.filter((a) => a > long(1))
54-
.filter((a) => a > long(2))
55-
.filter((a) => a > long(3))
56-
.filter((a) => a > long(4))
57-
.filter((a) => a > long(5))
58-
.filter((a) => a > long(6))
59-
.filter((a) => a > long(7))
60-
.fold(long(0), _+_) }
50+
.filter(a => a > 1L)
51+
.filter(a => a > 2L)
52+
.filter(a => a > 3L)
53+
.filter(a => a > 4L)
54+
.filter(a => a > 5L)
55+
.filter(a => a > 6L)
56+
.filter(a => a > 7L)
57+
.fold(0L, _+_) }
6158
}
6259

6360
def filterPipeline(using Quotes) = '{ (array: Array[Long]) =>
6461
${ Cooked.of('{array})
65-
.filter(d => (d mod long(2)) === long(0))
66-
.fold(long(0), _+_) }
62+
.filter(d => (d mod 2L) === 0L)
63+
.fold(0L, _+_) }
6764
}
6865

6966
def takePipeline(using Quotes) = '{ (array: Array[Long]) =>
7067
${ Cooked.of('{array})
71-
.take(int(2))
72-
.fold(long(0), _+_) }
68+
.take(2)
69+
.fold(0L, _+_) }
7370
}
7471

7572
def flatMapTakePipeline(using Quotes) = '{ (array1: Array[Long], array2: Array[Long]) =>
7673
${ Cooked.of('{array1})
77-
.flatMap((d) => Cooked.of('{array2}).map(y => y * d))
78-
.take(int(vLimit_s))
79-
.fold(long(0), _+_) }
74+
.flatMap(d => Cooked.of('{array2}).map(y => y * d))
75+
.take(Settings.vLimit_s)
76+
.fold(0L, _+_) }
8077
}
8178

8279
def dotProductPipeline(using Quotes) = '{ (array1: Array[Long], array2: Array[Long]) =>
8380
${ Cooked.of('{array1})
84-
.zipWith[Long, Long](Cooked.of('{array2}), _*_)
85-
.fold(long(0), _+_) }
81+
.zipWith(Cooked.of('{array2}), _*_)
82+
.fold(0L, _+_) }
8683
}
8784

8885
def flatMapAfterZipPipeline(using Quotes) = '{ (array1: Array[Long], array2: Array[Long]) =>
8986
${ Cooked.of('{array1})
90-
.zipWith[Long, Long](Cooked.of('{array1}), _+_)
91-
.flatMap((d) => Cooked.of('{array2}).map((dp) => d + dp))
92-
.fold(long(0), _+_) }
87+
.zipWith(Cooked.of('{array1}), _+_)
88+
.flatMap(d => Cooked.of('{array2}).map(dp => d + dp))
89+
.fold(0L, _+_) }
9390
}
9491

9592
def zipAfterFlatMapPipeline(using Quotes) = '{ (array1: Array[Long], array2: Array[Long]) =>
9693
${ Cooked.of('{array1})
97-
.flatMap((d) => Cooked.of('{array2}).map((dp) => d + dp))
98-
.zipWith[Long, Long](Cooked.of('{array1}), _+_)
99-
.fold(long(0), _+_) }
94+
.flatMap(d => Cooked.of('{array2}).map(dp => d + dp))
95+
.zipWith(Cooked.of('{array1}), _+_)
96+
.fold(0L, _+_) }
10097
}
10198

10299
def zipFlatMapFlatMapPipeline(using Quotes) = '{ (array1: Array[Long], array2: Array[Long]) =>
103100
${ Cooked.of('{array1})
104-
.flatMap((d) => Cooked.of('{array2}).map((dp) => d * dp))
105-
.zipWith[Long, Long](Cooked.of('{array2}).flatMap((d) => Cooked.of('{array1}).map((dp) => d - dp)), _+_)
106-
.take(int(vLimit_s))
107-
.fold(long(0), _+_) }
101+
.flatMap(d => Cooked.of('{array2}).map(dp => d * dp))
102+
.zipWith(Cooked.of('{array2}).flatMap(d => Cooked.of('{array1}).map(dp => d - dp)), _+_)
103+
.take(Settings.vLimit_s)
104+
.fold(0L, _+_) }
108105
}
109106

110107
def zipFilterFilterPipeline(using Quotes) = '{ (array1: Array[Long], array2: Array[Long]) =>
111-
${ Cooked.of('{array1}).filter((d) => d > long(7))
112-
.zipWith[Long, Long](Cooked.of('{array2}).filter((d) => d > long(5)), _+_)
113-
.fold(long(0), _+_) }
108+
${ Cooked.of('{array1}).filter(d => d > 7L)
109+
.zipWith(Cooked.of('{array2}).filter(d => d > 5L), _+_)
110+
.fold(0L, _+_) }
114111
}
115112

116113
extension (st: Cooked[Long])
114+
117115
def decode()(using Quotes): Cooked[Boolean] =
118-
st
119-
.map(e => toInt(e))
120-
.flatMap {el =>
121-
def newShape(using raw: Raw) =
122-
import raw._
123-
import raw.code._
124-
import raw.code.given
125-
mkPullArray[Cde[Boolean]](el, i => k =>
126-
if_(i<el,
127-
k(bool(false)),
128-
if1(i<int(255), k(bool(true)))
116+
import strymonas.Code._
117+
118+
st.map(e => toInt(e))
119+
.flatMap {el =>
120+
def newShape(using raw: Raw) =
121+
import raw._
122+
mkPullArray[Cde[Boolean]](el, i => k =>
123+
if_(i<el,
124+
k(bool(false)),
125+
if1(i<int(255), k(bool(true)))
126+
)
129127
)
130-
)
131-
132-
Cooked(st.raw, newShape)
133-
}
128+
129+
Cooked(newShape)
130+
}
134131

135132
def decodingPipeline(using Quotes) = '{ (array1: Array[Long], array2: Array[Long]) =>
133+
import strymonas.Code._
134+
136135
${ Cooked.of('{array1})
137-
.decode()
138-
.zipWith[Boolean, Boolean](Cooked.of('{array2}).decode(), _||_)
139-
.map(x => cond(x, long(1), long(0)))
140-
.fold(long(0), _+_) }
136+
.decode()
137+
.zipWith[Boolean, Boolean](Cooked.of('{array2}).decode(), _||_)
138+
.map(x => cond(x, 1L, 0L))
139+
.fold(0L, _+_) }
141140
}
142141
}

build.sbt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
val scala3Version = "3.0.0"
1+
val scala3Version = "3.2.0"
22

33
lazy val root = project
44
.in(file("."))

project/build.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
sbt.version=1.5.0-RC2
1+
sbt.version=1.7.1

src/main/scala/strymonas/CdeSpec.scala

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ trait IntCde[C[_]] {
5454
def ===(c2: Cde[Int])(using Quotes): Cde[Boolean] = int_eq(c1, c2)
5555
def !==(c2: Cde[Int])(using Quotes): Cde[Boolean] = int_neq(c1, c2)
5656
given IntOps = new IntOps{ }
57+
given inject_int(using Quotes): Conversion[Int, Cde[Int]] = x => int(x)
58+
5759
}
5860

5961
trait LongCde[C[_]] {
@@ -91,7 +93,8 @@ trait LongCde[C[_]] {
9193
def >=(c2: Cde[Long])(using Quotes): Cde[Boolean] = long_geq(c1, c2)
9294
def ===(c2: Cde[Long])(using Quotes): Cde[Boolean] = long_eq(c1, c2)
9395
def !==(c2: Cde[Long])(using Quotes): Cde[Boolean] = long_neq(c1, c2)
94-
given LongOps = new LongOps{ }
96+
given LongOps = new LongOps{ }
97+
given toLong(using Quotes): Conversion[Long, Cde[Long]] = x => long(x)
9598
}
9699

97100
trait BasicCde[C[_]] {
@@ -144,7 +147,6 @@ trait VarCde[C[_]] {
144147
given VarOps = new VarOps{ }
145148
}
146149

147-
148150
trait ArrayCde[C[_]] {
149151
type Cde[A] = C[A]
150152

src/main/scala/strymonas/Code.scala

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,23 @@ package strymonas
33
import scala.quoted._
44
import scala.reflect.ClassTag
55

6-
type CodeRaw[A] = CodeRaw.Cde[A]
7-
type VarRaw[A] = CodeRaw.Var[A]
8-
96
enum Annot[A] {
10-
case Sta[A](x: A) extends Annot[A]
11-
case Global[A]() extends Annot[A]
12-
case Unk[A]() extends Annot[A]
7+
case Sta[A](x: A) extends Annot[A]
8+
case Global[A]() extends Annot[A]
9+
case Unk[A]() extends Annot[A]
1310
}
1411

15-
case class Cde[A](sta : Annot[A], dyn : CodeRaw[A])
12+
case class Cde[A](sta : Annot[A], dyn : CodeRaw.Cde[A])
1613

1714
/**
1815
* The Scala's code generator which applies online partial evaluation
1916
*/
2017
object Code extends CdeSpec[Cde] {
2118
type Compiler = staging.Compiler
2219

20+
type CodeRaw[A] = CodeRaw.Cde[A]
21+
type VarRaw[A] = CodeRaw.Var[A]
22+
2323
given toExpr[A]: Conversion[Cde[A], Expr[A]] = x => x.dyn
2424
given ofExpr[A]: Conversion[Expr[A], Cde[A]] = x => Cde(Annot.Unk[A](), CodeRaw.ofExpr(x))
2525

src/main/scala/strymonas/Raw.scala

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -455,26 +455,6 @@ class Raw(val code : CdeSpec[Code.Cde]) extends Stream_Raw {
455455
mmain(false, st)
456456
}
457457

458-
// def linearize_score[A](st: Stream[A])(using ctx: Quotes): Int = {
459-
// st match {
460-
// case Initializer(ILet(i, t), sk) => linearize_score(sk(blackhole(t, ctx)))
461-
// case Initializer(IVar(i, t), sk) => {
462-
// var score = 0
463-
// val _ =
464-
// letVar(i)(z => {
465-
// score = linearize_score(sk(z))
466-
// unit
467-
// })(t, summon[Type[Unit]], ctx)
468-
// score
469-
// }
470-
// case Initializer(IArr(_, t), sk) => linearize_score(sk(blackhole_arr(t, ctx)))
471-
// // case Initializer(IUArr(_, _, t), sk) => linearize_score(sk(blackhole(t, ctx)))
472-
// case Flattened(Linear, _, _) => 0
473-
// case Flattened(_) => 3
474-
// case Nested(_, s, t, sk) =>
475-
// 5 + linearize_score(Flattened(s)) + linearize_score(sk(blackhole(t, ctx)))
476-
// }
477-
// }
478458
def linearize_score[A](st: Stream[A])(using ctx: Quotes): Int = {
479459
st match {
480460
case Initializer(ILet(i, t), sk) => linearize_score(sk(i))

0 commit comments

Comments
 (0)