parallel-collections

How do I replace the fork join pool for a Scala 2.9 parallel collection?

假装没事ソ 提交于 2019-12-04 08:35:33
问题 I've been looking at the new Scala 2.9 parallel collections and am hoping to abandon a whole lot of my crufty amateur versions of similar things. In particular, I'd like to replace the fork join pool which underlies the default implementation with something of my own (for example, something that distributes evaluation of tasks across a network, via actors). My understanding is that this is simply a matter of applying Scala's paradigm of "stackable modifications", but the collections library

Is this scala parallel array code threadsafe?

南楼画角 提交于 2019-12-04 07:00:48
I want to use parallel arrays for a task, and before I start with the coding, I'd be interested in knowing if this small snipept is threadsafe: import collection.mutable._ var listBuffer = ListBuffer[String]("one","two","three","four","five","six","seven","eight","nine") var jSyncList = java.util.Collections.synchronizedList(new java.util.ArrayList[String]()) listBuffer.par.foreach { e => println("processed :"+e) // using sleep here to simulate a random delay Thread.sleep((scala.math.random * 1000).toLong) jSyncList.add(e) } jSyncList.toArray.foreach(println) Are there better ways of

Will Scala's parallel collections guarantee ordering?

落花浮王杯 提交于 2019-12-03 22:29:45
If I have this: val a = Array(...) and I write a.par.map(e => someFunc(e)) Will the resulting collection be in the same order as the non-parallel collection? Yes, but the function itself is executed without any particular order. List(1,2,3).par foreach print // could print out 213 The parallel collections maintain all of the contracts of their non-parallel equivalents. On collections in which a map operation preserves order, such as List , order will be preserved by the parallel map as well. On collections in which map does not preserve order, such as Set , order will not be preserved in the

Parallel map operations?

好久不见. 提交于 2019-12-03 08:45:57
问题 Does Scala provide a way to execute parallel map operations as part of the standard language? For example, given: scala> val a = List((1,2), (3,4), (3,6)) a: List[(Int, Int)] = List((1,2), (3,4), (3,6)) I can do: scala> a.map(tup => tup._1 + tup._2) res0: List[Int] = List(3, 7, 9) However, to the best of my knowledge this maps the provided function over the list objects sequentially. Is there a built-in way to have the function applied to each element in a separate thread (or equivalent), and

Parallel map operations?

邮差的信 提交于 2019-12-02 22:39:55
Does Scala provide a way to execute parallel map operations as part of the standard language? For example, given: scala> val a = List((1,2), (3,4), (3,6)) a: List[(Int, Int)] = List((1,2), (3,4), (3,6)) I can do: scala> a.map(tup => tup._1 + tup._2) res0: List[Int] = List(3, 7, 9) However, to the best of my knowledge this maps the provided function over the list objects sequentially. Is there a built-in way to have the function applied to each element in a separate thread (or equivalent), and the results then gathered into a resulting list? Akos Krivachy If you add par then you will get a

How do I replace the fork join pool for a Scala 2.9 parallel collection?

江枫思渺然 提交于 2019-12-02 22:29:37
I've been looking at the new Scala 2.9 parallel collections and am hoping to abandon a whole lot of my crufty amateur versions of similar things. In particular, I'd like to replace the fork join pool which underlies the default implementation with something of my own (for example, something that distributes evaluation of tasks across a network, via actors). My understanding is that this is simply a matter of applying Scala's paradigm of "stackable modifications", but the collections library is intimidating enough that I'm not exactly sure which bits need modifying! Some concrete questions: Is

Calling map on a parallel collection via a reference to an ancestor type

坚强是说给别人听的谎言 提交于 2019-12-01 13:25:28
I tried to make it optional to run a map operation sequentially or in parallel, for example using the following code: val runParallel = true val theList = List(1,2,3,4,5) (if(runParallel) theList.par else theList) map println //Doesn't run in parallel What I noticed is that the 'map' operation does not run in parallel as I'd expected. Although without the conditional, it would: theList.par map println //Runs in parallel as visible in the output The type of the expression (if(runParallel) theList else theList.par) which I expect to be the closest common ancestor of both types of theList and

Understanding parallel exists and find

Deadly 提交于 2019-12-01 03:33:08
I take a List[Int] and want to search for a value x where x * 10 > 500 in parallel. So exists should return true if the list contains any value of 51 or greater. def f(x: Int) = { println("calculating for " + x) Thread.sleep(100 - x) println("finished " + x) x * 10 } val res = List.range(1, 100).par.exists(f(_) > 500) Which gives results: calculating for 1 calculating for 25 calculating for 50 calculating for 75 calculating for 13 finished 75 // <-- first valid result found: 75 * 10 > 500 finished 50 calculating for 51 // but it kicks off more expensive calculations finished 25 calculating for

Understanding parallel exists and find

独自空忆成欢 提交于 2019-11-30 23:59:28
问题 I take a List[Int] and want to search for a value x where x * 10 > 500 in parallel. So exists should return true if the list contains any value of 51 or greater. def f(x: Int) = { println("calculating for " + x) Thread.sleep(100 - x) println("finished " + x) x * 10 } val res = List.range(1, 100).par.exists(f(_) > 500) Which gives results: calculating for 1 calculating for 25 calculating for 50 calculating for 75 calculating for 13 finished 75 // <-- first valid result found: 75 * 10 > 500

Missing import scala.collection.parallel in Scala 2.13

自作多情 提交于 2019-11-28 03:10:52
问题 Parallel collections in Scala 2.12 were importable out-of-the-box like so import scala.collection.parallel.immutable.ParVector val pv = new ParVector[Int] however why in Scala 2.13 package scala.collection.parallel seems to be missing? 回答1: Parallel collections have been moved in Scala 2.13 to separate module scala/scala-parallel-collection This Scala standard module contains the package scala.collection.parallel, with all of the parallel collections that used to be part of the Scala standard