Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add BarrierOps and StringStructOps from https://github.com/manojo/experiments. #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
70 changes: 70 additions & 0 deletions src/main/scala/lms/BarrierOps.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/**
* Barrier synchronization using simple effects
*/
package lms

import lms._
import scala.virtualization.lms.common._
import scala.virtualization.lms.internal.Effects

import java.io.PrintWriter
import scala.reflect.SourceContext

trait BarrierOps extends Base {
def barrierSync(cmt: String)(implicit pos: SourceContext): Rep[Unit]
}

trait BarrierOpsExp extends BarrierOps with EffectExp {
case class BarrierSync(cmt: String) extends Def[Unit]

def barrierSync(cmt: String)(implicit pos: SourceContext) = {
printlog("warning: inserting barrier sync.")
printsrc(raw"in $pos")
reflectEffect(BarrierSync(cmt)) // Simple effect
}

override def mirror[A: Manifest](e: Def[A], f: Transformer)(implicit pos: SourceContext): Exp[A] = (e match {
case Reflect(BarrierSync(cmt), u, es) => reflectMirrored(Reflect(BarrierSync(cmt), mapOver(f, u), f(es)))(mtype(manifest[A]), pos)
case _ => super.mirror(e, f)
}).asInstanceOf[Exp[A]]
}

trait ScalaGenBarrierOps extends ScalaGenEffect {
val IR: BarrierOpsExp
import IR._

override def emitNode(sym: Sym[Any], rhs: Def[Any]) = rhs match {
case BarrierSync(cmt) => gen"""// Barrier sync: $cmt"""
case _ => super.emitNode(sym, rhs)
}
}

trait CGenBarrierOps extends CGenEffect {
val IR: BarrierOpsExp
import IR._

override def emitNode(sym: Sym[Any], rhs: Def[Any]) = rhs match {
case BarrierSync(cmt) => gen"""/* Barrier sync: $cmt */"""
case _ => super.emitNode(sym, rhs)
}
}

trait CudaGenBarrierOps extends CudaGenEffect {
val IR: BarrierOpsExp
import IR._

override def emitNode(sym: Sym[Any], rhs: Def[Any]) = rhs match {
case BarrierSync(cmt) => gen"""/* Barrier sync: $cmt */"""
case _ => super.emitNode(sym, rhs)
}
}

trait OpenCLGenBarrierOps extends OpenCLGenEffect {
val IR: BarrierOpsExp
import IR._

override def emitNode(sym: Sym[Any], rhs: Def[Any]) = rhs match {
case BarrierSync(cmt) => gen"""/* Barrier sync: $cmt */"""
case _ => super.emitNode(sym, rhs)
}
}
90 changes: 90 additions & 0 deletions src/main/scala/lms/util/StringStructOps.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package lms.util

import lms._

import scala.virtualization.lms.common._
import scala.reflect.SourceContext

trait StringStructOps extends lms.StructOps with While with IfThenElse
with NumericOps with ArrayOps with Equal with StringOps with OrderingOps
with BooleanOps with CharOps with PrimitiveOps with StaticData with MiscOps
with LiftVariables {

type StringStruct = lms.Record {
val input: Array[Char]
val start: Int
val length: Int
}

def String(in: Rep[Array[Char]], st: Rep[Int], len: Rep[Int] = unit(0)) = new lms.Record {
val input = in
val start = st
val length = len
}

def toLower(c: Rep[Char]): Rep[Char] =
(c.toInt | unit(0x20)).toChar

//unsound operation in general, we need to have the same input !!
/* def infix_+(l: Rep[StringStruct], r: Rep[StringStruct])(implicit pos: SourceContext)
= stringStruct_plus(l,r)
def stringStruct_plus(l: Rep[StringStruct], r: Rep[StringStruct])(implicit pos: SourceContext) : Rep[StringStruct]
= String(in = l.input, st = l.start, len = (r.length:Rep[Int]))
*/
/*FIXME: for now always assume r is lower cased*/
def __equal(l: Rep[StringStruct], r: Rep[Array[Char]])(implicit pos: SourceContext): Rep[Boolean] = {
if (l.length == r.length) {
val in: Rep[Array[Char]] = l.input
val st = l.start
var i = unit(0); var tmp = unit(true)
while (i < l.length && tmp) {
if (toLower(in(i + st)) != r(i)) {
tmp = unit(false)
}
i = i + unit(1)
}
tmp
} else {
unit(false)
}
}

def __equal(l: Rep[StringStruct], r: String)(implicit pos: SourceContext): Rep[Boolean] = {
//TODO: lms.Array.apply not working
//val tmp: Rep[Array[Char]] = array_obj_fromseq{
// staticData(r.toArray)
//}
l == staticData(r.toArray)
}

def infix_mkString(st: Rep[StringStruct])(implicit pos: SourceContext): Rep[String] = {
var s = unit(""); var i = unit(0)
while (i < st.length) {
s = string_plus(s, st.input(st.start + readVar(i)))
i = i + unit(1)
}
s
}

def infix_toStr(st: Rep[StringStruct])(implicit pos: SourceContext): Rep[String]

}

trait StringStructOpsExp extends StringStructOps with StructOpsExpOptCommon with WhileExp with IfThenElseExpOpt
with BooleanOpsExp with NumericOpsExp with ArrayOpsExp with EqualExpOpt with StringOpsExp with OrderingOpsExp
with CharOpsExp with PrimitiveOpsExp with MiscOpsExp with VariablesExp with StaticDataExp {
case class StringStructToString(s: Rep[StringStruct]) extends Def[String]
def infix_toStr(s: Rep[StringStruct])(implicit pos: SourceContext) = StringStructToString(s)
}

trait ScalaGenStringStructOps extends ScalaGenBase with ScalaGenStructOps with ScalaGenWhile with ScalaGenIfThenElse
with ScalaGenNumericOps with ScalaGenArrayOps with ScalaGenEqual with ScalaGenStringOps with ScalaGenOrderingOps
with ScalaGenBooleanOps with ScalaGenCharOps with ScalaGenPrimitiveOps with ScalaGenMiscOps with ScalaGenVariables with ScalaGenStaticData {
val IR: StringStructOpsExp
import IR._

override def emitNode(sym: Sym[Any], rhs: Def[Any]) = rhs match {
case StringStructToString(s) => emitValDef(sym, src"$s.input.slice($s.start,$s.start+$s.length).mkString")
case _ => super.emitNode(sym, rhs)
}
}