Advertisement
jules0707

ParallelCountChangeRunner

Mar 9th, 2017
492
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 3.44 KB | None | 0 0
  1. package reductions
  2.  
  3. import org.scalameter._
  4.  
  5. object ParallelCountChangeRunner {
  6.  
  7.   @volatile var seqResult = 0
  8.  
  9.   @volatile var parResult = 0
  10.  
  11.   val standardConfig = config(
  12.     Key.exec.minWarmupRuns -> 20,
  13.     Key.exec.maxWarmupRuns -> 40,
  14.     Key.exec.benchRuns -> 80,
  15.     Key.verbose -> true) withWarmer (new Warmer.Default)
  16.  
  17.   def main(args: Array[String]): Unit = {
  18.     val amount = 250
  19.     val coins = List(1, 2, 5, 10, 20, 50)
  20.     val seqtime = standardConfig measure {
  21.       seqResult = ParallelCountChange.countChange(amount, coins)
  22.     }
  23.     println(s"sequential result = $seqResult")
  24.     println(s"sequential count time: $seqtime ms")
  25.  
  26.     def measureParallelCountChange(threshold: ParallelCountChange.Threshold): Unit = {
  27.       val fjtime = standardConfig measure {
  28.         parResult = ParallelCountChange.parCountChange(amount, coins, threshold)
  29.       }
  30.       println(s"parallel result = $parResult")
  31.       println(s"parallel count time: $fjtime ms")
  32.       println(s"speedup: ${seqtime / fjtime}")
  33.     }
  34.  
  35.     measureParallelCountChange(ParallelCountChange.moneyThreshold(amount))
  36.     measureParallelCountChange(ParallelCountChange.totalCoinsThreshold(coins.length))
  37.     measureParallelCountChange(ParallelCountChange.combinedThreshold(amount, coins))
  38.   }
  39. }
  40.  
  41. object ParallelCountChange {
  42.  
  43.   /**
  44.    * Returns the number of ways change can be made from the specified list of
  45.    *  coins for the specified amount of money.
  46.    */
  47.   def countChange(money: Int, coins: List[Int]): Int = {
  48.     //hint: we either decide to continue subtracting the next coin in the coins list from the money amount,
  49.     //or we decide to drop the coin from the list of coins
  50.     if (money < 0) 0
  51.     else if (money == 0) 1
  52.     else if (money > 0 && coins.isEmpty) 0
  53.     else (
  54.         countChange(money - coins.head, coins)
  55.         +
  56.         countChange(money, coins.tail)
  57.         )
  58.  
  59.   }
  60.  
  61.   type Threshold = (Int, List[Int]) => Boolean
  62.  
  63.   /**
  64.    * In parallel, counts the number of ways change can be made from the
  65.    *  specified list of coins for the specified amount of money.
  66.    */
  67.   def parCountChange(money: Int, coins: List[Int], threshold: Threshold): Int = {
  68.     if (threshold(money, coins) || coins.isEmpty || money <= 0) countChange(money, coins)
  69.     else {
  70.       val pair = parallel(
  71.         parCountChange(money - coins.head, coins, threshold),
  72.         parCountChange(money, coins.tail,threshold))
  73.       pair._1 + pair._2
  74.     }
  75.   }
  76.  
  77.   /** Threshold heuristic based on the starting money. */
  78.   def moneyThreshold(startingMoney: Int): Threshold =
  79.     (money, coins) =>
  80.       money <= (2 * startingMoney) / 3
  81.  
  82.   /** Threshold heuristic based on the total number of initial coins. */
  83.   def totalCoinsThreshold(totalCoins: Int): Threshold = (money, coins) => coins.size <= (2 * totalCoins) / 3
  84.  
  85.   /** Threshold heuristic based on the starting money and the initial list of coins. */
  86.   def combinedThreshold(startingMoney: Int, allCoins: List[Int]): Threshold = {
  87.       (money,coins) => money * coins.size <= (startingMoney*allCoins.size)/2
  88.      
  89.       /* credit TomLous looks nice but yields a test error with message:
  90.        * combinedThreshold should return true when the number of coins times money is
  91.        * less than or equal to half of the initial number of coins times starting money */
  92.     //(money,coins) => moneyThreshold(startingMoney)(money,coins) && totalCoinsThreshold(allCoins.size)(money,coins)
  93.  
  94.   }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement