concurrency - How to spawn concurrent computation in Haskell? -
If I get a function that calculates four very long and returns a list with the result of four calculations, but Where each calculation is not dependent on another, how do you "parallel" in Haskell?
To better explain my question, here is a close example of my thoughts:
(defan some-function [arg1 arg2] [c1 C2 (c1 c3 c4]] [c1 c2 c3 c4])
cgi (future (very long-calculated ARGU1)) C3 (future (very-long-calculation logic 2)) 3 (Future (very long-computed hypothesis)) C4 (long-term calculations) Beggary]]; There is no need to wrap the C4 in the future [@ C1 @ C2 @ C3C4])
What will happen in Haskell as follows?
someFunction :: (a -> A -> A -> a) - & gt; (A) C1 and Lt; - Rapper (very long computed computation) C2 and Lt; - Rapper (very long-calculated logic 2) C3 and LT; - Rapper (very long-calculated computation) C4 and LT; - (very-long-calculated arg4) rseq c1 rseq c2 rseq c3 returns (c1, c2, c3, c4)
Do I need rpar / rseq c4?
What is the rpar / Rseq way to go for such concurrent calculation?
What if I do not do rseq, then what program will wait, later, when I try to use return values in the returned list?
Is it transparent or do you need to do something like "DRF" that you are in closure when you use '@'?
You are looking for a package. For example, if you want to run three computations and want to take one that completes this first:
come back or you can wait for
special async
thread Share the situation via, and stm
. This is a very useful package for this type of thing. The exact version you asked in OP will look something like this:
Some functions: IO (a, b, c, d) someFunction = Do c1 & lt; - async $ veryLongComputation1C2 & lt; - async $ veryLongComputation2 c3 & lt; - async $ long long computation 3 v4 & lt; - Very long Compliment 4 - Wait for all the results and wait for a Tupple $ (,,,) & lt; $ & Gt; C1 and LT; * & Gt; C2 and lieutenant; * & Gt; C3 & Lt; * & Gt; (Return v4)
It surely assumes that c1, c2, c3
are all side-effects and you are not interested in the result. Wait
and elections
get value for you.
I also highly recommend "Haskell in parallel and concurrent programming" by Simon Marlowe.
Comments
Post a Comment