Advertisement
Guest User

parallel sh!t

a guest
Jun 12th, 2014
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 3.43 KB | None | 0 0
  1.  
  2. \begin{lstlisting}
  3. import util.Random
  4. import System.currentTimeMillis
  5. import actors.Futures._
  6.  
  7. object Parallel2 extends App{
  8.  
  9.   val rnd = new Random(1337)//Seeded RNG
  10.  
  11.   //Create 2 matrices with dimension NxN
  12.   val N = 500
  13.   val A:Array[Array[Double]] = Array.fill(N,N){ rnd.nextDouble }
  14.   val B:Array[Array[Double]] = Array.fill(N,N){ rnd.nextDouble }
  15.  
  16.   //Time a call-by-name block and return milli-seconds
  17.   def time(b: => Unit):Long = {
  18.     val start = currentTimeMillis
  19.     b
  20.     currentTimeMillis-start
  21.   }
  22.  
  23.   val sequentialProgram = new LinearAlgebra with SequentialLinearAlgebra {
  24.     println("Sequential time: "+time { A * B } )
  25.   }
  26.  
  27.   val parColProgram = new LinearAlgebra with ParColLinearAlgebra {
  28.     println("ParCol time: "+time { A * B } )
  29.   }
  30.  
  31.   val futureProgram = new LinearAlgebra with FutureLinearAlgebra {
  32.     println("Future time: "+time { A * B } )
  33.   }  
  34.  
  35.   val imperativeProgram = new LinearAlgebra with ImperateiveLinearAlgebra {
  36.     println("Imperative time: "+time { A * B } )
  37.   }
  38.  
  39. }
  40.  
  41. //Interface, knows nothing about implementation
  42. trait LinearAlgebra{
  43.  
  44.   type Vector = Array[Double]
  45.   type Matrix = Array[Array[Double]]
  46.  
  47.   implicit class VectorOps(v:Vector){
  48.     def dot(that:Vector):Double = innerProd(v,that)
  49.   }
  50.   implicit class MatrixOps(m:Matrix){
  51.     def *(that:Matrix):Matrix = matMul(m,that)
  52.   }
  53.  
  54.   //Functionality will is deferred to implementing traits
  55.   def innerProd(a:Vector,b:Vector):Double
  56.   def matMul(A:Matrix,B:Matrix):Matrix
  57. }
  58.  
  59. //Implements LinearAlgebra interface in a sequential fashion
  60. trait SequentialLinearAlgebra extends LinearAlgebra {
  61.  
  62.   def innerProd(a:Vector,b:Vector) =
  63.     ((a,b).zipped map (_*_)).sum
  64.  
  65.   def matMul(A:Matrix,B:Matrix) = {
  66.       val Bt = B.transpose      
  67.       A.map(row => Bt.map(col => row dot col))
  68.     }
  69.  
  70. }
  71.  
  72. //Implements LinearAlgebra interface using parallel collections
  73. trait ParColLinearAlgebra extends LinearAlgebra {
  74.  
  75.   def innerProd(a:Vector,b:Vector) =
  76.     ((a,b).zipped map (_*_)).sum
  77.  
  78.   def matMul(A:Matrix,B:Matrix) = {
  79.     val Bt = B.transpose
  80.     (A.par map (row => Bt map (col => row dot col))).toArray
  81.   }
  82.  
  83. }
  84.  
  85. //Implements LinearAlgebra interface using futures, i.e. explicit workload distribution
  86. trait FutureLinearAlgebra extends LinearAlgebra {
  87.  
  88.   def innerProd(a:Vector,b:Vector) =
  89.     ((a,b).zipped map (_*_)).sum
  90.  
  91.   //One thread per row in A
  92.   def matMul(A:Matrix,B:Matrix) = {
  93.       val Bt = B.transpose
  94.       val res = A map ( row => future {Bt map (col => row dot col)})
  95.       res.map(_.apply)
  96.     }
  97.  
  98. }
  99.  
  100. //Implements LinearAlgebra interface in imperative algorithms
  101. trait ImperateiveLinearAlgebra extends LinearAlgebra {
  102.  
  103.   //Actually, we don't need this for this benchmark
  104.   def innerProd(a:Vector,b:Vector) = {
  105.     var accum = 0d
  106.     var i = 0
  107.     while(i<a.length) {
  108.       accum += a(i)*b(i) //State mutation!
  109.       i += 1  //State mutation!
  110.     }
  111.     accum
  112.   }
  113.  
  114.   def matMul(A:Matrix,B:Matrix) = {
  115.     val res = Array.ofDim[Double](A.length,B(0).length)
  116.     var i=0
  117.     while(i<A.length){
  118.       var j=0
  119.       while(j<B(0).length){
  120.         var k=0
  121.         while(k<A(0).length){
  122.           res(i)(j) += A(i)(k)*B(k)(j)  //State mutation!
  123.           k+=1                          //State mutation!
  124.         }
  125.         j+=1                            //State mutation!
  126.       }
  127.       i+=1                              //State mutation!
  128.     }
  129.     res
  130.   }
  131.  
  132. }
  133.  
  134.  
  135. \end{lstlisting}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement