51

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.

4 Answers 4

94

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

4
  • 1
    Did you grab the image from here: expcodes.com/125624 ? Some attribution may be nice. May 11, 2013 at 6:06
  • 53
    @phresnel this is original work by me, created for this question, using inkscape. your link actually uses my work without attribution - as you can tell my looking at the date of the content. May 11, 2013 at 10:53
  • 6
    I see; I mostly wondered about why the linked content seems to be of larger resolution. My failure to not look at date, thanks for clarification :) May 12, 2013 at 10:01
  • If -threaded is not compiled in, what happens to the sparks and forkIO invocations? In other words, is the main thread its own "Haskell Lightweight Thread"? Aug 24, 2016 at 5:56
32

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]

2
  • 1
    As I read it, a "spark" is not so much a thread as it is a computation which may be run in a thread. The runtime might have a fixed ceiling limit on threads which rotate through and evaluate sparks.
    – ephemient
    Jun 6, 2009 at 0:08
  • 1
    This is not correct. Sparks are not threads. GHC supports sparks, which are unevaluated computations in a queue, Haskell (lightweight threads) and OS threads. May 1, 2011 at 17:16
3

If I understand it correctly, a spark is an entry in a queue of jobs requiring work. A pool of threads take entries from this queue and runs them. Typically there is one thread per physical processor, so this scheme maximises throughput and minimizes thread context switching.

1

It looks like it is similar to a "task" in Intel Threading Building Blocks.

1
  • 3
    I'd give a +1, but feel like it needs a little more to be an answer, as it stands its more like a comment. Add a couple links and discuss how the concepts are similar with a focus on helping us understand Sparks specifically.
    – derekv
    Jun 25, 2015 at 15:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.