Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .scalafmt.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
version = 2.6.4
maxColumn = 120
align.preset = none
assumeStandardLibraryStripMargin = true
align.stripMargin = true
optIn.configStyleArguments = true
runner.optimizer.forceConfigStyleOnOffset = 5
runner.optimizer.forceConfigStyleMinArgCount = 10
newlines.alwaysBeforeMultilineDef = false
newlines.alwaysBeforeElseAfterCurlyIf = false
1 change: 1 addition & 0 deletions project/plugins.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
addSbtPlugin("org.scalameta" % "sbt-scalafmt" % "2.4.0") // "2.4.0" is just sbt plugin version
125 changes: 75 additions & 50 deletions src/main/scala/lms/collection/ArrayOps.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,43 @@ import lms.macros.SourceContext

trait ArrayOps { b: Base =>

def NewArray[T:Manifest](x: Rep[Int]): Rep[Array[T]] = {
def NewArray[T: Manifest](x: Rep[Int]): Rep[Array[T]] = {
Wrap[Array[T]](Adapter.g.reflectMutable("NewArray", Unwrap(x)))
}
def Array[T:Manifest](xs: Rep[T]*): Rep[Array[T]] = {
Wrap[Array[T]](Adapter.g.reflectMutable("Array", xs.map(Unwrap(_)):_*))
def Array[T: Manifest](xs: Rep[T]*): Rep[Array[T]] = {
Wrap[Array[T]](Adapter.g.reflectMutable("Array", xs.map(Unwrap(_)): _*))
}
implicit class ArrayOps[A:Manifest](x: Rep[Array[A]]) {
def apply(i: Rep[Int]): Rep[A] = x match {
case Wrap(_) => Wrap[A](Adapter.g.reflectRead("array_get", Unwrap(x), Unwrap(i))(Unwrap(x)))
case EffectView(x, base) => Wrap[A](Adapter.g.reflectRead("array_get", Unwrap(x), Unwrap(i))(Unwrap(base)))
}
def update(i: Rep[Int], y: Rep[A]): Unit = x match {
case Wrap(_) => Adapter.g.reflectWrite("array_set", Unwrap(x), Unwrap(i), Unwrap(y))(Unwrap(x))
case EffectView(x, base) => Adapter.g.reflectWrite("array_set", Unwrap(x), Unwrap(i), Unwrap(y))(Unwrap(base))
}
implicit class ArrayOps[A: Manifest](x: Rep[Array[A]]) {
def apply(i: Rep[Int]): Rep[A] =
x match {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

prefer the old format

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you mean x match { should be on the same line of def ...?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, I tried to fix it myself but didn't got too much time to read the documentation for formatting

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree - the x match { should be on the same line

case Wrap(_) => Wrap[A](Adapter.g.reflectRead("array_get", Unwrap(x), Unwrap(i))(Unwrap(x)))
case EffectView(x, base) => Wrap[A](Adapter.g.reflectRead("array_get", Unwrap(x), Unwrap(i))(Unwrap(base)))
}
def update(i: Rep[Int], y: Rep[A]): Unit =
x match {
case Wrap(_) => Adapter.g.reflectWrite("array_set", Unwrap(x), Unwrap(i), Unwrap(y))(Unwrap(x))
case EffectView(x, base) => Adapter.g.reflectWrite("array_set", Unwrap(x), Unwrap(i), Unwrap(y))(Unwrap(base))
}
def length: Rep[Int] = Wrap[Int](Adapter.g.reflect("array_length", Unwrap(x)))
def slice(s: Rep[Int], e: Rep[Int]): Rep[Array[A]] = EffectView[Array[A]](Wrap[Array[A]](Adapter.g.reflect("array_slice", Unwrap(x), Unwrap(s), Unwrap(e))), x) // (Unwrap(x), Adapter.STORE)())
def slice(s: Rep[Int], e: Rep[Int]): Rep[Array[A]] =
EffectView[Array[A]](
Wrap[Array[A]](Adapter.g.reflect("array_slice", Unwrap(x), Unwrap(s), Unwrap(e))),
x
) // (Unwrap(x), Adapter.STORE)())
def free: Unit = Adapter.g.reflectFree("array_free", Unwrap(x))(Unwrap(x))
def copyToArray(arr: Rep[Array[A]], start: Rep[Int], len: Rep[Int]) = Adapter.g.reflectEffect("array_copyTo", Unwrap(x), Unwrap(arr), Unwrap(start), Unwrap(len))(Unwrap(x))(Unwrap(arr))
def copyToLongArray(arr: Rep[LongArray[A]], start: Rep[Long], len: Rep[Int]) = Adapter.g.reflectEffect("array_copyTo", Unwrap(x), Unwrap(arr), Unwrap(start), Unwrap(len))(Unwrap(x))(Unwrap(arr))
def copyToArray(arr: Rep[Array[A]], start: Rep[Int], len: Rep[Int]) =
Adapter.g.reflectEffect("array_copyTo", Unwrap(x), Unwrap(arr), Unwrap(start), Unwrap(len))(Unwrap(x))(
Unwrap(arr)
)
def copyToLongArray(arr: Rep[LongArray[A]], start: Rep[Long], len: Rep[Int]) =
Adapter.g.reflectEffect("array_copyTo", Unwrap(x), Unwrap(arr), Unwrap(start), Unwrap(len))(Unwrap(x))(
Unwrap(arr)
)

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These formatting changes also don't seem like improvements to me. It may be worth looking at refactoring the code for clarity, but just putting the last Unwrap(...) on another line make it more difficult to read IMO.

// FIXME: currently copy!!
def sort(size: Rep[Int] /* , sort: (Rep[A], Rep[A]) => Rep[Int] */) = Wrap[Array[A]](Adapter.g.reflectMutable("array_sort_scala", Unwrap(x), Unwrap(size))) //, Adapter.g.reify((x, y) => Unwrap(sort(Wrap[A](x), Wrap[A](y)))))(Unwrap(x))(Unwrap(x))
def sort(size: Rep[Int] /* , sort: (Rep[A], Rep[A]) => Rep[Int] */ ) =
Wrap[Array[A]](
Adapter.g.reflectMutable("array_sort_scala", Unwrap(x), Unwrap(size))
) //, Adapter.g.reify((x, y) => Unwrap(sort(Wrap[A](x), Wrap[A](y)))))(Unwrap(x))(Unwrap(x))
}
implicit class CharArrayOps(x: Rep[Array[Char]]) {
def ArrayOfCharToString(): Rep[String] = {
Expand All @@ -40,25 +55,33 @@ trait ArrayOps { b: Base =>
}

trait LongArray[+T]
def NewLongArray[T:Manifest](x: Rep[Long], init: Option[Int] = None): Rep[LongArray[T]] = init match {
case Some(v) => Wrap[LongArray[T]](Adapter.g.reflectMutable("NewArray", Unwrap(x), Backend.Const(v)))
case _ => Wrap[LongArray[T]](Adapter.g.reflectMutable("NewArray", Unwrap(x)))
}
def LongArray[T:Manifest](xs: Rep[T]*): Rep[LongArray[T]] = {
Wrap[LongArray[T]](Adapter.g.reflectMutable("Array", xs.map(Unwrap(_)):_*))
}
implicit class LongArrayOps[A:Manifest](x: Rep[LongArray[A]]) {
def apply(i: Rep[Long]): Rep[A] = x match {
case Wrap(_) => Wrap[A](Adapter.g.reflectRead("array_get", Unwrap(x), Unwrap(i))(Unwrap(x)))
case EffectView(x, base) => Wrap[A](Adapter.g.reflectRead("array_get", Unwrap(x), Unwrap(i))(Unwrap(base)))
}
def update(i: Rep[Long], y: Rep[A]): Unit = x match {
case Wrap(_) => Adapter.g.reflectWrite("array_set", Unwrap(x), Unwrap(i), Unwrap(y))(Unwrap(x))
case EffectView(x, base) => Adapter.g.reflectWrite("array_set", Unwrap(x), Unwrap(i), Unwrap(y))(Unwrap(base))
def NewLongArray[T: Manifest](x: Rep[Long], init: Option[Int] = None): Rep[LongArray[T]] =
init match {
case Some(v) => Wrap[LongArray[T]](Adapter.g.reflectMutable("NewArray", Unwrap(x), Backend.Const(v)))
case _ => Wrap[LongArray[T]](Adapter.g.reflectMutable("NewArray", Unwrap(x)))
}
def LongArray[T: Manifest](xs: Rep[T]*): Rep[LongArray[T]] = {
Wrap[LongArray[T]](Adapter.g.reflectMutable("Array", xs.map(Unwrap(_)): _*))
}
implicit class LongArrayOps[A: Manifest](x: Rep[LongArray[A]]) {
def apply(i: Rep[Long]): Rep[A] =
x match {
case Wrap(_) => Wrap[A](Adapter.g.reflectRead("array_get", Unwrap(x), Unwrap(i))(Unwrap(x)))
case EffectView(x, base) => Wrap[A](Adapter.g.reflectRead("array_get", Unwrap(x), Unwrap(i))(Unwrap(base)))
}
def update(i: Rep[Long], y: Rep[A]): Unit =
x match {
case Wrap(_) => Adapter.g.reflectWrite("array_set", Unwrap(x), Unwrap(i), Unwrap(y))(Unwrap(x))
case EffectView(x, base) => Adapter.g.reflectWrite("array_set", Unwrap(x), Unwrap(i), Unwrap(y))(Unwrap(base))
}
def length: Rep[Long] = Wrap[Long](Adapter.g.reflect("array_length", Unwrap(x)))
def slice(s: Rep[Long], e: Rep[Long] = unit(-1L)): Rep[LongArray[A]] = EffectView[LongArray[A]](Wrap[LongArray[A]](Adapter.g.reflect("array_slice", Unwrap(x), Unwrap(s), Unwrap(e))), x) // FIXME: borrowing effect?
def resize(s: Rep[Long]): Rep[LongArray[A]] = Wrap[LongArray[A]](Adapter.g.reflectRealloc("array_resize", Unwrap(x), Unwrap(s))(Unwrap(x)))
def slice(s: Rep[Long], e: Rep[Long] = unit(-1L)): Rep[LongArray[A]] =
EffectView[LongArray[A]](
Wrap[LongArray[A]](Adapter.g.reflect("array_slice", Unwrap(x), Unwrap(s), Unwrap(e))),
x
) // FIXME: borrowing effect?
def resize(s: Rep[Long]): Rep[LongArray[A]] =
Wrap[LongArray[A]](Adapter.g.reflectRealloc("array_resize", Unwrap(x), Unwrap(s))(Unwrap(x)))
def free: Unit = Adapter.g.reflectFree("array_free", Unwrap(x))(Unwrap(x))
}
}
Expand All @@ -68,31 +91,33 @@ trait ArrayOps { b: Base =>
// StackArray is the array using stack (i.e. int a[5] = {1,2,3,4,5};)
trait StackArrayOps extends ArrayOps { b: Base =>

def NewStackArray[T:Manifest](x: Rep[Int]): Rep[Array[T]] = {
def NewStackArray[T: Manifest](x: Rep[Int]): Rep[Array[T]] = {
Wrap[Array[T]](Adapter.g.reflectMutable("NewStackArray", Unwrap(x)))
}

def StackArray[T:Manifest](xs: Rep[T]*): Rep[Array[T]] = {
def StackArray[T: Manifest](xs: Rep[T]*): Rep[Array[T]] = {
Wrap[Array[T]](Adapter.g.reflectMutable("StackArray", xs.map(Unwrap(_)): _*))
}
}

trait CCodeGenStackArray extends ExtendedCCodeGen {

override def traverse(n: Node): Unit = n match {
case n @ Node(s, "NewStackArray", List(x), _) =>
val tpe = remap(typeMap.get(s).map(_.typeArguments.head).getOrElse(manifest[Unknown]))
emit(s"$tpe "); shallow(s); emit("["); shallow(x); emitln("];")
case n @ Node(s, "StackArray", xs, _) =>
val tpe = remap(typeMap.get(s).map(_.typeArguments.head).getOrElse(manifest[Unknown]))
emit(s"$tpe "); shallow(s); emit("[] = {"); shallow(xs.head)
xs.tail.foreach(x => {emit(", "); shallow(x)}); emitln("};")
case _ => super.traverse(n)
}
override def traverse(n: Node): Unit =
n match {
case n @ Node(s, "NewStackArray", List(x), _) =>
val tpe = remap(typeMap.get(s).map(_.typeArguments.head).getOrElse(manifest[Unknown]))
emit(s"$tpe "); shallow(s); emit("["); shallow(x); emitln("];")
case n @ Node(s, "StackArray", xs, _) =>
val tpe = remap(typeMap.get(s).map(_.typeArguments.head).getOrElse(manifest[Unknown]))
emit(s"$tpe "); shallow(s); emit("[] = {"); shallow(xs.head)
xs.tail.foreach(x => { emit(", "); shallow(x) }); emitln("};")
case _ => super.traverse(n)
}

override def mayInline(n: Node): Boolean = n match {
case Node(s, "NewStackArray", _, _) => false
case Node(s, "StackArray", _, _) => false
case _ => super.mayInline(n)
}
override def mayInline(n: Node): Boolean =
n match {
case Node(s, "NewStackArray", _, _) => false
case Node(s, "StackArray", _, _) => false
case _ => super.mayInline(n)
}
}
Loading