summaryrefslogtreecommitdiff
path: root/test/benchmarks/src/scala/collection/parallel/benchmarks/parallel_array/ReducePrime.scala
blob: 8fb90981ac3411c5826d5992ab32a0ec91ef8668 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package scala.collection.parallel.benchmarks.parallel_array


import scala.collection.parallel.benchmarks._


object IntWrapCreator extends (Int => IntWrap) {
  def apply(idx: Int) = new IntWrap(shiftaround(idx))
  def shiftaround(idx: Int) = idx * 40192 + 717
}

case class IntWrap(val num: Int)

object IntOps {
  val op = (a: IntWrap, b: IntWrap) => primereduce(a, b)
  val reducer = new extra166y.Ops.Reducer[IntWrap] {
    def op(a: IntWrap, b: IntWrap) = primereduce(a, b)
  }
  
  def primereduce(a: IntWrap, b: IntWrap) = {
    val check = (checkPrime(a.num), checkPrime(b.num))
    if (a.num > b.num) a else b
  }
  
  def checkPrime(n: Int) = {
    var isPrime = true
    var i = 2
    val until = scala.math.sqrt(n).toInt + 1
    while (i < until) {
      if (n % i == 0) isPrime = false
      i += 1
    }
    isPrime
  }
}

class ReducePrime(sz: Int, p: Int, what: String)
extends Resettable[IntWrap](sz, p, what, IntWrapCreator, new Array[Any](_), classOf[IntWrap])
with HavingResult[IntWrap] {
  def companion = ReducePrime
  
  def runseq = runresult = sequentialReduce(IntOps.op, sz, new IntWrap(0))
  def runpar = runresult = pa.reduce(IntOps.op)
  def runjsr = runresult = jsrarr.reduce(IntOps.reducer, new IntWrap(0))
  override def comparisonMap = collection.Map("jsr" -> runjsr _)
}

object ReducePrime extends Companion {
  def benchName = "reduce-prime";
  def apply(sz: Int, p: Int, what: String) = new ReducePrime(sz, p, what)
  override def comparisons = List("jsr")
  override def defaultSize = 100
}