Advertisement
jules0707

VerticalBoxBlurRunner

Feb 23rd, 2017
330
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 2.26 KB | None | 0 0
  1. package scalashop
  2.  
  3. import org.scalameter._
  4. import common._
  5.  
  6. object VerticalBoxBlurRunner {
  7.  
  8.   val standardConfig = config(
  9.     Key.exec.minWarmupRuns -> 5,
  10.     Key.exec.maxWarmupRuns -> 10,
  11.     Key.exec.benchRuns -> 10,
  12.     Key.verbose -> true) withWarmer (new Warmer.Default)
  13.  
  14.   def main(args: Array[String]): Unit = {
  15.     val radius = 3
  16.     val width = 1920
  17.     val height = 1080
  18.     val src = new Img(width, height)
  19.     val dst = new Img(width, height)
  20.     val seqtime = standardConfig measure {
  21.       VerticalBoxBlur.blur(src, dst, 0, width, radius)
  22.     }
  23.     println(s"sequential blur time: $seqtime ms")
  24.  
  25.     val numTasks = 128
  26.     val partime = standardConfig measure {
  27.       VerticalBoxBlur.parBlur(src, dst, numTasks, radius)
  28.     }
  29.     println(s"fork/join blur time: $partime ms")
  30.     println(s"speedup: ${seqtime / partime}")
  31.   }
  32.  
  33. }
  34.  
  35. /** A simple, trivially parallelizable computation. */
  36. object VerticalBoxBlur {
  37.  
  38.   /**
  39.    * Blurs the columns of the source image `src` into the destination image
  40.    *  `dst`, starting with `from` and ending with `end` (non-inclusive).
  41.    *
  42.    *  Within each column, `blur` traverses the pixels by going from top to
  43.    *  bottom.
  44.    */
  45.   def blur(src: Img, dst: Img, from: Int, end: Int, radius: Int): Unit = {
  46.     for (
  47.       x <- from until end;
  48.       y <- 0 until src.height
  49.     ) yield dst.update(x, y, boxBlurKernel(src, x, y, radius))
  50.   }
  51.  
  52.   /**
  53.    * Blurs the columns of the source image in parallel using `numTasks` tasks.
  54.    *
  55.    *  Parallelization is done by stripping the source image `src` into
  56.    *  `numTasks` separate strips, where each strip is composed of some number of
  57.    *  columns.
  58.    */
  59.   def parBlur(src: Img, dst: Img, numTasks: Int, radius: Int): Unit = {
  60.     val stripSize = Math.max(src.width / numTasks, 1)
  61.     val splittingPoints = (0 until src.width) by stripSize
  62.     val startEnd = splittingPoints zip splittingPoints.tail
  63.     val lastStrip =  if(startEnd.isEmpty){
  64.       (0,src.width) } else {(startEnd.last._2,src.width)}
  65.      
  66.         val tasks = (startEnd :+ lastStrip).map {
  67.           t =>
  68.             {
  69.               task {
  70.                 blur(src, dst, t._1, t._2, radius)
  71.               }
  72.             }
  73.         }
  74.         tasks.map { t => t.join() }  
  75.   }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement