summaryrefslogtreecommitdiff
path: root/src/library/scala/collection/parallel/package.scala
blob: cddf0989669193a8b24c7ee282f2900ebc3d0a2c (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
66
67
68
69
70
package scala.collection


import java.lang.Thread._

import scala.collection.generic.CanBuildFrom
import scala.collection.generic.CanCombineFrom


/** Package object for parallel collections.
 */
package object parallel {
  val MIN_FOR_COPY = -1 // TODO: set to 5000
  val CHECK_RATE = 512

  /** Computes threshold from the size of the collection and the parallelism level.
   */
  def thresholdFromSize(sz: Int, parallelismLevel: Int) = {
    val p = parallelismLevel
    if (p > 1) 1 + sz / (8 * p)
    else sz
  }

  /** An implicit conversion providing arrays with a `par` method, which
   *  returns a parallel array.
   *
   *  @tparam T      type of the elements in the array, which is a subtype of AnyRef
   *  @param array   the array to be parallelized
   *  @return        a `Parallelizable` object with a `par` method
   */
  implicit def array2ParallelArray[T <: AnyRef](array: Array[T]) = new Parallelizable[mutable.ParallelArray[T]] {
    def par = mutable.ParallelArray.handoff[T](array)
  }

  implicit def factory2ops[From, Elem, To](bf: CanBuildFrom[From, Elem, To]) = new {
    def isParallel = bf.isInstanceOf[Parallel]
    def asParallel = bf.asInstanceOf[CanCombineFrom[From, Elem, To]]
    def ifParallel[R](isbody: CanCombineFrom[From, Elem, To] => R) = new {
      def otherwise(notbody: => R) = if (isParallel) isbody(asParallel) else notbody
    }
  }

  implicit def traversable2ops[T](t: TraversableOnce[T]) = new {
    def isParallel = t.isInstanceOf[Parallel]
    def isParallelIterable = t.isInstanceOf[ParallelIterable[_]]
    def asParallelIterable = t.asInstanceOf[ParallelIterable[T]]
    def isParallelSeq = t.isInstanceOf[ParallelSeq[_]]
    def asParallelSeq = t.asInstanceOf[ParallelSeq[T]]
    def ifParallelSeq[R](isbody: ParallelSeq[T] => R) = new {
      def otherwise(notbody: => R) = if (isParallel) isbody(asParallelSeq) else notbody
    }
  }

}