Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are F# Async.Parallel results guaranteed to be in order?

Tags:

f#

f#-async

Are the results from F#'s Async.Parallel operation guaranteed to arrive in the order jobs were submitted? My sample code returns the results in order, but I can't find any mention in the MSDN docs, or the F# spec, assuring this must be the case -- that it's not a coincidence.

Here is my sample code:

let r = System.Random()
Async.Parallel [
    for i in 0..10 ->
        async {
            let rand_num = r.Next(10)
            do! Async.Sleep(rand_num)  (* Simulate jobs taking a variable amount of time *)
            printfn "%i %i" i rand_num
            return i
        }
] 
|> Async.RunSynchronously
|> printfn "%A"

And here's the output.

0 0
5 1
4 1
3 3
10 6
9 4
7 5
2 5
1 5
8 7
6 9
[|0; 1; 2; 3; 4; 5; 6; 7; 8; 9; 10|]

You can see that, in this run, the async functions complete in indeterminate order, yet the resulting array is sorted. Is this behavior guaranteed?

like image 337
spiffytech Avatar asked Jun 16 '14 01:06

spiffytech


People also ask

Is Harry's better than Dollar Shave Club?

First things first, you won't go wrong with either razor. We've personally tried them both and they provide a nice shave. That said, we prefer the Dollar Shave Club Executive over the Harry's Truman razor. It's close, but we thought the Executive from DSC provided a nice close shave, had great grip, and pivoted nicely.

Is Dollar Shave Club worthwhile?

if you're looking for a good shave at an affordable price, Dollar Shave club offers some of the best razors made of high quality materials with free shipping. With a comfortable shave at a great price, we highly recommend Dollar Shave Club as they truly make great products.

How did the Dollar Shave Club go viral?

It got its first big boost from a 2012 YouTube video, in which Dubin stars, that cost $4,500 and took a single day to shoot. It went supernova-viral in 72 hours. Article continues after video.

What did Dollar Shave Club sell for?

Unilever Bought Dollar Shave Club for $1 Billion.


1 Answers

At the moment, the source of the function is written so that this guarantee is enforced. Looking at control.fs around line #1300 for the definition, we can see the function that puts the results into the output array is

let recordSuccess i res = 
    results.[i] <- res;
    finishTask(Interlocked.Decrement count) 

this function is called in this segment

 tasks |> Array.iteri (fun i p ->
     queueAsync
         innerCTS.Token
         // on success, record the result
         (fun res -> recordSuccess i res)

where tasks has the original tasks in sorted order. This guarantees that the output list is in the same order as the input.

UPDATE

The spec at least seems to imply that the order is fixed - it contains this code:

let rec fib x = if x < 2 then 1 else fib(x-1) + fib(x-2)

let fibs = 
    Async.Parallel [ for i in 0..40 -> async { return fib(i) } ]
    |> Async.RunSynchronously

printfn "The Fibonacci numbers are %A" fibs //I changed this line to be accurate

System.Console.ReadKey(true)

If the spec didn't guarantee the output order, this code would be incorrect.

like image 55
John Palmer Avatar answered Sep 25 '22 02:09

John Palmer