Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a "spark" in Haskell

I'm confused about the notion of "spark"

Is it a thread in Haskell? Or is the action of spawning a new thread ?

Thanks everybody:

So to summarize, sparks are not thread but more of unit of computation (tasks to put it in C#/Java terms). So it's the Haskell way of implementing the task parallelism.

like image 347
Cheick Avatar asked Jun 05 '09 22:06

Cheick


2 Answers

Sparks are not threads. forkIO introduces Haskell threads (which map down onto fewer real OS threads). Sparks create entries in the work queues for each thread, from which they'll take tasks to execute if the thread becomes idle.

As a result sparks are very cheap (you might have billions of them in a program, while you probably won't have more than a million Haskell threads, and less than a dozen OS threads on half a dozen cores).

Think of it like this:

spark model

like image 178
Don Stewart Avatar answered Sep 22 '22 07:09

Don Stewart


See A Gentle Introduction to Glasgow Parallel Haskell.

Parallelism is introduced in GPH by the par combinator, which takes two arguments that are to be evaluated in parallel. The expression p `par` e (here we use Haskell's infix operator notation) has the same value as e, and is not strict in its first argument, i.e. bottom `par` e has the value of e. (bottom denotes a non-terminating or failing computation.) Its dynamic behaviour is to indicate that p could be evaluated by a new parallel thread, with the parent thread continuing evaluation of e. We say that p has been sparked, and a thread may subsequently be created to evaluate it if a processor becomes idle. Since the thread is not necessarily created, p is similar to a lazy future.

[Emphasis in original]

like image 37
Apocalisp Avatar answered Sep 23 '22 07:09

Apocalisp