blob: c0379b819e482fdc3dd0a76e51d9f688c833c01e (
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
71
72
73
74
|
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
}
def unsupported(msg: String) = throw new UnsupportedOperationException(msg)
def unsupported = throw new UnsupportedOperationException
/** 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 array2ParArray[T <: AnyRef](array: Array[T]) = new Parallelizable[mutable.ParArray[T]] {
def par = mutable.ParArray.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 isParIterable = t.isInstanceOf[ParIterable[_]]
def asParIterable = t.asInstanceOf[ParIterable[T]]
def isParSeq = t.isInstanceOf[ParSeq[_]]
def asParSeq = t.asInstanceOf[ParSeq[T]]
def ifParSeq[R](isbody: ParSeq[T] => R) = new {
def otherwise(notbody: => R) = if (isParallel) isbody(asParSeq) else notbody
}
}
}
|