English 中文(简体)
scala:并行集合不工作?
原标题:scala: parallel collections not working?
  • 时间:2011-06-03 01:30:22
  •  标签:
  • scala

我正试图通过.par以一种非常基本的方式使用并行集合-我希望集合会被无序地执行,但事实并非如此:

scala> (1 to 10) map println
1
2
3
4
5
6
7
8
9
10

scala> (1 to 10).par map println
1
2
3
4
5
6
7
8
9
10

在后一种情况下,顺序似乎不应该是连续的。这是scala2.9,我的机器有两个核心。这可能是某个地方的错误配置吗?谢谢

edit: i did indeed try running with a large set (100k) 和 the result was still sequential.

最佳回答

YMMV:

scala> (1 to 10).par map println
1
6
2
3
4
7
5
8
9

这也是一个双核。。。

我认为如果你尝试足够多的跑步,你可能会看到不同的结果。以下是一段代码,显示了发生的一些情况:

import collection.parallel._
import collection.parallel.immutable._

class ParRangeEx(range: Range) extends ParRange(range) {
  // Some minimal number of elements after which this collection 
  // should be handled sequentially by different processors.
  override def threshold(sz: Int, p:Int) = {
    val res = super.threshold(sz, p)
    printf("threshold(%d, %d) returned %d
", sz, p, res)
    res
  }
  override def splitter = {
    new ParRangeIterator(range) 
        with SignalContextPassingIterator[ParRangeIterator] {
      override def split: Seq[ParRangeIterator] = {
        val res = super.split
        println("split " + res) // probably doesn t show further splits
        res
      }
    }
  }
}

new ParRangeEx((1 to 10)).par map println

有些运行是穿插处理,有些运行是顺序处理。它似乎把负载一分为二。如果您将返回的阈值数更改为11,您将看到工作负载永远不会被拆分。

底层的调度机制是基于fork-join和工作窃取的。请参阅下面的JSR166源代码以获得一些见解。这可能是驱动同一个线程是否会同时处理两个任务(因此看起来是连续的)或两个线程处理每个任务的原因。

以下是我电脑上的输出示例:

threshold(10, 2) returned 1
split List(ParRangeIterator(over: Range(1, 2, 3, 4, 5)), 
  ParRangeIterator(over: Range(6, 7, 8, 9, 10)))
threshold(10, 2) returned 1
threshold(10, 2) returned 1
threshold(10, 2) returned 1
threshold(10, 2) returned 1
threshold(10, 2) returned 1
6
7
threshold(10, 2) returned 1
8
1
9
2
10
3
4
5
问题回答

答案可以很好地按顺序得出;这是无法保证的。在这样一个小的集合上,你通常会按顺序得到它。然而,println调用了一个系统调用,如果运行足够多次,可能会得到一个混乱的版本。





相关问题
How to flatten a List of different types in Scala?

I have 4 elements:List[List[Object]] (Objects are different in each element) that I want to zip so that I can have a List[List[obj1],List[obj2],List[obj3],List[obj4]] I tried to zip them and I ...

To use or not to use Scala for new Java projects? [closed]

I m impressed with Twitter and investigating to use Scala for a new large scale web project with Hibernate and Wicket. What do you think about Scala, and should I use it instead of Java? EDIT: And, ...

Why does Scala create a ~/tmp directory when I run a script?

When I execute a Scala script from the command line, a directory named "tmp" is created in my home directory. It is always empty, so I simply deleted it without any apparent problem. Of course, when I ...

Include jar file in Scala interpreter

Is it possible to include a jar file run running the Scala interpreter? My code is working when I compile from scalac: scalac script.scala -classpath *.jar But I would like to be able to include a ...

Scala and tail recursion

There are various answers on Stack Overflow which explain the conditions under which tail recursion is possible in Scala. I understand the limitations and how and where I can take advantage of tail ...

热门标签