Here's a really simple example that walks an array and squares each element. Notice the order that is outputted to the console:

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

#light
open System
open System.Threading

let a = Array.init 100 (fun x -> x)

Parallel.For(0, 100, (fun ind ->
    let sq = a.[ind] * a.[ind]
    Console.WriteLine("a[{0}] = {1}", ind, sq)
    a.[ind] <- sq))
By on 12/7/2007 3:01 PM ()

Hi Naveen,

I ported the parallel matrix multiply code to use the task parallel library and posted it on my blog. You might find some inspiration there to solve this problem.

Jurgen

By on 12/7/2007 2:02 AM ()

Jurgen

Thanks appreciate that. However, the link does not seem to work.

By on 12/7/2007 3:02 AM ()

Can you check the link again. I think it should be working now.

Cheers, -J

By on 12/7/2007 2:41 PM ()

Hi Naveen,

I'm not sure what you want this code to do but it seems like you are trying to convert all words in a list of words to uppercase. A more straight forward way of doing this in F# would be

1
let convertlist xs = List.map String.uppercase xs

As Don says, you can just download and use the TPL from F# as it is just a regular .NET library.

You can, however, achieve a fair degree of TPL-style asynchronous programming directly in F# using F#'s asynchronous workflows. For example you could write a parallel map (i.e. foreach) like so.

1
let pmap f xs = xs |> Seq.map (fun x -> async { return f x }) |> Async.Parallel

Thus you could write your example

1
Async.Run (pmap String.uppercase words)
By on 12/5/2007 11:06 PM ()

See [link:cs.hubfs.net] from which the following is an excerpt.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
 

#light

let evals =
    let z = 4.0
    [ async { do printf "Computing z*z\n"
              return z * z };
      async { do printf "Computing sin(z)\n"
              return (sin z) };
      async { do printf "Computing log(z)\n"
              return (log z) } ]
 
let awr =
    async { let! vs = Async.Parallel evals
            do printf "Computing v1+v2+v3\n"
            return (Array.fold_left (fun a b -> a + b) 0.0 vs) }
 
let R = Async.Run awr
printf "Result = %f\n" R
By on 12/5/2007 5:41 AM ()

Thanks but it doesn't use Task Parallel Library. I wanted to know how to use TPL to do this.

By on 12/5/2007 6:50 AM ()

Hi Naveen,

Task Parallel Library (now Parallel Extensions to the .NET FX) has recently been released in CTP form.

Have you tried translating some of the C# samples in that component? You will be able to use Parallel.For etc. directly.

If you hit problems perhaps you could post your partially compelted code here on the hub. It's easier for people to help you if they see how far you've got and what specific questions you have.

Cheers

don

By on 12/5/2007 9:32 PM ()
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