Advertisement
Guest User

Untitled

a guest
Aug 28th, 2014
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 3.34 KB | None | 0 0
  1. package it.vigtig.linalg
  2.  
  3. import scala.virtualization.lms.common._
  4. import language.implicitConversions
  5. import language.higherKinds
  6. import scala.reflect.SourceContext
  7. import scala.language.reflectiveCalls
  8.  
  9. trait LinearAlgebraExp extends LinearAlgebra with BaseExp with DslExp with LinAlgFWTransform {
  10.  
  11.   // Reification of the concept of scaling a vector `v` by a factor `k`
  12.   case class VectorScale[T: Manifest: Numeric](v: Exp[Vector[T]], s: Exp[T]) extends Def[Vector[T]]
  13.   case class VectorAdd[T:Manifest:Numeric](v:Exp[Vector[T]],u:Exp[Vector[T]]) extends Def[Vector[T]]
  14.  
  15.   override def vector_scale[T: Manifest: Numeric](v: Exp[Vector[T]], s: Exp[T]) = toAtom(VectorScale(v, s))
  16.     //override def vector_add[T: Manifest : Numeric](v:Rep[Vector[T]],u:Rep[Vector[T]]) = toAtom(VectorAdd(v,u))
  17.  
  18.   override type Vector[T] = Array[T]
  19.  
  20. }
  21. trait LinAlgFWTransform extends BaseFatExp with EffectExp with IfThenElseFatExp with LoopsFatExp { self =>
  22.  
  23.   class MyWorklistTransformer extends WorklistTransformer { val IR: self.type = self }
  24.  
  25.   // ---------- Exp api
  26.  
  27.   implicit def toAfter[A:Manifest](x: Def[A]) = new { def atPhase(t: MyWorklistTransformer)(y: => Exp[A]) = transformAtPhase(x)(t)(y) }
  28.   implicit def toAfter[A](x: Exp[A]) = new { def atPhase(t: MyWorklistTransformer)(y: => Exp[A]) = transformAtPhase(x)(t)(y) }
  29.  
  30.   // transform x to y at the *next* iteration of t.
  31.   // note: if t is currently active, it will continue the current pass with x = x.
  32.   // do we need a variant that replaces x -> y immediately if t is active?
  33.  
  34.   def transformAtPhase[A](x: Exp[A])(t: MyWorklistTransformer)(y: => Exp[A]): Exp[A] = {
  35.     t.register(x)(y)
  36.     x
  37.   }
  38.    
  39.  
  40.   def onCreate[A:Manifest](s: Sym[A], d: Def[A]): Exp[A] = s
  41.  
  42.   // ----------
  43.  
  44.   override def createDefinition[T](s: Sym[T], d: Def[T]): Stm = {
  45.     onCreate(s,d)(s.tp)
  46.     super.createDefinition(s,d)
  47.   }
  48.  
  49. }
  50. trait LinAlg2Loops extends LinAlgFWTransform with LinearAlgebraExp {
  51.  
  52.   implicit def any2rep[T: Manifest](t: T) = unit(t)
  53.   /*
  54.     we enrich Vectors (now, Arrays) with foreach and zipWith.
  55.     This is virtual code because we work on Rep[T]s and
  56.     works because we extend BaseExp+DslExp which in turn
  57.     contain AST representations of while loops, array-constructors,
  58.     etc...
  59.   */
  60.   implicit class enrichArray[T:Manifest:Numeric](a: Rep[Array[T]]) {
  61.     def foreach(f: Rep[T] => Rep[Unit]):Rep[Unit] = {
  62.       var i=0; while(i<a.length) { f(a(i)); i+= 1 }
  63.     }
  64.     def zipWith(b:Rep[Array[T]])(f: (Rep[T],Rep[T]) => Rep[T]) = {
  65.       val res = NewArray[T](a.length min b.length)
  66.       var i = 0;while(i<res.length) { res(i) = f(a(i),b(i)); i+=1 }
  67.       res
  68.     }
  69.     def map[U:Manifest](f:(Rep[T]) => Rep[U]) = {
  70.       val res = NewArray[U](a.length)
  71.       var i = 0;while(i<res.length) { res(i) = f(a(i)); i+=1}
  72.       res
  73.     }
  74.     def *(u:Rep[T]) = {
  75.       val res = NewArray[T](a.length)
  76.       var i = 0;while(i<res.length) { res(i) = a(i)*u; i+=1}
  77.       res
  78.     }
  79.   }
  80.  
  81.   def vscale_loopform[T:Manifest:Numeric](a:Rep[Vector[T]],s:Rep[T]) = a * s
  82.  
  83.   override def onCreate[A:Manifest](s: Sym[A], d: Def[A]) = (d match {
  84.     case VectorScale(v,scalar) => s.atPhase(xform) { vscale_loopform(xform(v),xform(scalar)).asInstanceOf[Exp[A]] }
  85.     case _ => super.onCreate(s,d)
  86.   }).asInstanceOf[Exp[A]]
  87.  
  88.   val xform = new MyWorklistTransformer
  89.  
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement