I am not sure I understand well. Do you intend to parallelize the Seq.sumBy, or the outer loop?

  • If you want to parallelize your for obj in objList, you can do something that looks like your second try:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    
    [for obj in objList ->
      async {
        let preCalc = calc A
        return
          obj.someList
          |> Seq.sumBy calculations
      }
    ]
    |> Async.Parallel
    |> Async.RunSynchronously

    or, equivalently but in a more F#-ish style:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    
    objList
    |> Seq.map (fun obj ->
      async {
        let preCalc = calc A
        return
          obj.someList
          |> Seq.sumBy calculations
      })
    |> Async.Parallel
    |> Async.RunSynchronously
  • If you want to parallelize the Seq.sumBy, the problem is that Seq.sumBy f actually does two things:

    1. it applies f to each element of the sequence;
    2. it sums the results.

    While the first step can indeed be made parallel, the second obviously needs all the results in order to sum them. So if you want to parallelize the calculations, you need to break your sumBy in two: first, a parallel mapping; then, a sum of the resulting values.

    The code would probably be something like the following:

    1
    2
    3
    4
    5
    6
    7
    
    for obj in objList do
      let preCalc = calc A
    
      [for e in obj.someList -> async { return calculations e }]
      |> Async.Parallel
      |> Async.RunSynchronously
      |> Seq.sum

    or again:

    1
    2
    3
    4
    5
    6
    7
    8
    
    for obj in objList do
      let preCalc = calc A
    
      obj.someList
      |> Seq.map (fun e -> async { return calculations e })
      |> Async.Parallel
      |> Async.RunSynchronously
      |> Seq.sum

Hope this helps.

By on 11/23/2011 7:26 AM ()

Good job, Loic!

By on 12/5/2011 3:01 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