I would reccomend using the Task Parallel Library to do this instead of async workflows.

You can get the June 2008 CTP here.

Once you add a reference to the System.Threadding .dll to your project, the following code should get you started:

1
2
3
4
5
6
7
8
9
10
11
12
13
#light

open System.Linq

module ParallelSeq =
    let map (f:('a -> 'b)) (ie:seq<'a>) =
        let pie = ie.AsParallel<'a>()
        ParallelEnumerable.Select(pie, f)

let main =
    [1..360]
    |> ParallelSeq.map (fun i -> printfn "%d" i)
    |> Seq.to_list //Force evaluation of sequence

Note: The use of printf here is not syncronized, so
your output can potentially be garbled. AFAIK there is no easy way to
do multhreaded IO as you're describing without using some sort of
locking scheme. You do have a couple of options.

Assuming your operation will see performance gains from running in parallel, you could do something like this:

1
2
3
4
5
6
7
8
9
let main =
    //get results in parallel
    let results =
        [1..360]
        |> ParallelSeq.map FUNCTION_TO_MULTITHREAD

    //print results single threaded
    results
    |> Seq.iter (fun res -> printf "%A" res)

No matter what you do, you have to be extremely mindful of any shared
state that you have across threads. An easy way to do this is to avoid
any mutable state, but keep in mind that parallel programming can be
extremely difficult.

Note that there is a LOT more that can be said about this topic...

By on 5/5/2009 10:02 AM ()

I don't think you can have multiple threads sampling or choosing without replacement without a lock somewhere- either to make sure the choice function is one-to-one, or to make sure the sampling is sequential. You can hide the lock lots of places, though.

By on 5/4/2009 5:04 PM ()

Is there any particular reason why you want to run exactly 96 operations at a time? It might be better to let the threadpool handle how many run at a time.

1
2
3
4
let a i:int = async{ printfn "%i" i } // use return x to return a value

seq{ for i in 1..96 do yield i } |> Seq.map a |> Async.Parallel |> Async.Run
By on 5/4/2009 6:11 AM ()
IntelliFactory Offices Copyright (c) 2011-2012 IntelliFactory. All rights reserved.
Home | Products | Consulting | Trainings | Blogs | Jobs | Contact Us | Terms of Use | Privacy Policy | Cookie Policy
Built with WebSharper