问题
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.
回答1:
See A Gentle Introduction to Glasgow Parallel Haskell.
Parallelism is introduced in GPH by the
parcombinator, which takes two arguments that are to be evaluated in parallel. The expressionp `par` e(here we use Haskell's infix operator notation) has the same value ase, and is not strict in its first argument, i.e.bottom `par` ehas the value ofe. (bottomdenotes a non-terminating or failing computation.) Its dynamic behaviour is to indicate thatpcould be evaluated by a new parallel thread, with the parent thread continuing evaluation ofe. We say thatphas been sparked, and a thread may subsequently be created to evaluate it if a processor becomes idle. Since the thread is not necessarily created,pis similar to a lazy future.
[Emphasis in original]
回答2:
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:
回答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.
回答4:
It looks like it is similar to a "task" in Intel Threading Building Blocks.
来源:https://stackoverflow.com/questions/958449/what-is-a-spark-in-haskell