parallel-collections

Transforming arrays in-place with parallel collections

守給你的承諾、 提交于 2020-01-13 09:37:06
问题 When one has an array of objects it is often desirable (e.g. for performance reasons) to update (replace) some of the objects in place. For example, if you have an array of integers, you might want to replace the negative integers with positive ones: // Faster for primitives var i = 0 while (i < a.length) { if (a(i) < 0) a(i) = -a(i) i += 1 } // Fine for objects, often okay for primitives for (i <- a.indices) if (a(i) < 0) a(i) = -a(i) What is the canonical way to perform a modification like

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

[亡魂溺海] 提交于 2019-12-19 11:24:40
问题 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)

With parallel collection, does aggregate respect order?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-13 16:44:15
问题 in scala, i have a parallel Iterable of items and i want to iterate over them and aggregate the results in some way, but in order. i'll simplify my use case and say that we start with an Iterable of integers and want to concatenate the string representation of them in paralle, with the result in order. is this possible with either fold or aggregate? it's unclear from the documentation which methods work parallelized but maintain order. 回答1: Yes, order is gauranteed to be preserved for fold

Efficiency/scalability of parallel collections in Scala (graphs)

孤者浪人 提交于 2019-12-12 09:13:00
问题 So I've been working with parallel collections in Scala for a graph project I'm working on, I've got the basics of the graph class defined, it is currently using a scala.collection.mutable.HashMap where the key is Int and the value is ListBuffer[Int] (adjacency list). (EDIT: This has since been change to ArrayBuffer[Int] I had done a similar thing a few months ago in C++, with a std::vector<int, std::vector<int> > . What I'm trying to do now is run a metric between all pairs of vertices in

How to define a function which accept both Seq[T] and ParSeq[T] as parameter?

荒凉一梦 提交于 2019-12-12 04:31:24
问题 In many cases, I want to apply the same filter or map function to a Seq or ParSeq collection. However I don't want to write the code twice. def fun(data:ParSeq[String], num_start:Int,num_end:Int) = { data filter { x=> val temp = extract_number(x) num_start <= temp && temp <= num_end } } Like the code above, for a Seq[String] I need to apply fun , I have to rewrite it again and the content are exactly the same. How can I avoid it? 回答1: You can use GenSeq[String] . Both ParSeq and Seq extends

When a ConcurrentBag is better than a List?

久未见 提交于 2019-12-06 18:57:42
问题 I am using a Parallel.Foreach for populating an external ConcurrentBag. I tried also to use a common List and everything works fine. I have been lucky or I missed the special scope of ConcurrentBag? 回答1: You have been lucky; Parallel.ForEach to populate a List is not thread-safe, you will eventually run into problems. According to MSDN, List<T> is not thread safe: Any instance members are not guaranteed to be thread safe. A List<T> can support multiple readers concurrently, as long as the

Is this scala parallel array code threadsafe?

烂漫一生 提交于 2019-12-06 03:59:47
问题 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 *

Will Scala's parallel collections guarantee ordering?

99封情书 提交于 2019-12-05 12:24:44
问题 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? 回答1: Yes, but the function itself is executed without any particular order. List(1,2,3).par foreach print // could print out 213 回答2: 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

When a ConcurrentBag is better than a List?

感情迁移 提交于 2019-12-05 00:13:16
I am using a Parallel.Foreach for populating an external ConcurrentBag. I tried also to use a common List and everything works fine. I have been lucky or I missed the special scope of ConcurrentBag? You have been lucky; Parallel.ForEach to populate a List is not thread-safe, you will eventually run into problems. According to MSDN, List<T> is not thread safe: Any instance members are not guaranteed to be thread safe. A List<T> can support multiple readers concurrently, as long as the collection is not modified. Enumerating through a collection is intrinsically not a thread-safe procedure. In

Efficiency/scalability of parallel collections in Scala (graphs)

人走茶凉 提交于 2019-12-04 17:03:10
So I've been working with parallel collections in Scala for a graph project I'm working on, I've got the basics of the graph class defined, it is currently using a scala.collection.mutable.HashMap where the key is Int and the value is ListBuffer[Int] (adjacency list). (EDIT: This has since been change to ArrayBuffer[Int] I had done a similar thing a few months ago in C++, with a std::vector<int, std::vector<int> > . What I'm trying to do now is run a metric between all pairs of vertices in the graph, so in C++ I did something like this: // myVec = std::vector<int> of vertices for (std::vector