blue-sky
blue-sky

Reputation: 53786

What is meant by parallelism in this case?

In this blog post about parallel collections in Scala http://beust.com/weblog/2011/08/15/scalas-parallel-collections/

This is mentioned on a comment by Daniel Spiewak:

Other people have already commented about the mkString example, so I’m going to leave it alone. It does reflect a much larger point about collection semantics in general. Basically, this is it: in the absence of side-effects (in user code), parallel collections have the exact same semantics as the sequential collections. Put another way: forAll { (xs: Vector[A], f: A => B) => (xs.par map f) == (xs map f) }

Does this mean that if there are no side effects parallelism is not achieved? If this is true can this point be expanded to explain why this the case?

Upvotes: 0

Views: 83

Answers (2)

vptheron
vptheron

Reputation: 7466

Does this mean that if there are no side effects parallelism is not achieved?

No, that's not what it means. When Daniel Spiewak says that

Basically, this is it: in the absence of side-effects (in user code), parallel collections have the exact same semantics as the sequential collections.

It means that if your function has no side-effects then using it to map over a simple collection or a parallel collection will yield the same outcome. Which is why:

Put another way: forAll { (xs: Vector[A], f: A => B) => (xs.par map f) == (xs map f) }

if f is side-effect free.

So, it's actually the opposite: if there are side effects parallelism is not a good idea since the outcome will be inconsistent.

Upvotes: 4

Kigyo
Kigyo

Reputation: 5768

It will always be run in parallel, but the result may differ when side-effects are there.

Let's say A = Int and B = Int with following code:

var tmp = false

def f(i: Int) = if(!tmp) {tmp = true; 0} else i + 1

So here we have a function f with a side-effect. I assume that tmp is false before running the code.

Running Vector(1,2,3).map(f) will always result in Vector(0,3,4)

But Vector(1,2,3).par.map(f) can have different results. It can be Vector(0,3,4) but since its parallel, the second element maybe is mapped first etc. So something like this might happen Vector(2,0,4).

You can be sure that the result will be the same, when in this case f would not have side-effects.

Upvotes: 3

Related Questions