Parallel in C#?

I know we can do async stuff with let! and use! but is there a F# way of performing a set of operations in parallel (like Parallel.For(...) & Parallel.ForEach(...) in C#)?

You can use the Array.Parallel module for things like map and iter. There’s also nuget packages for ParallelSeq and (highly recommended) the Nessos Streams library, which is a high-performance sequence library with support for parallelism.

1 Like

@BanksySan I have made very good use of the PSeq module. Once you install the FSharp.Collections.ParallelSeq nuget package, sometimes it’s just as easy as changing “Seq.iter” to “PSeq.iter”, and viola: 8 times as fast (for instance). YMMV. :sunglasses:

1 Like

Going with @ScottHutchinson here. PSeq is dead simple to use and gets you pretty far rather quickly. If you want to invest a bit of time to learn a new concept Hopac is also a really nice way to write concurrent programs. Here is a row of very good blog posts describing the library.

2 Likes

For serious stuff I recommend Akka Streams (there is a nice F# wrapper, Akkling).

1 Like

More answers there than I expected.
Why would one chose one over another?

TL;DR: because this topic can be quite complex. Choose the library/pattern that suits your present needs the best.

Because there are many strategies to solve concurrency problems. What you choose depends on many different factors:

  • Do you mind to introduce additional dependencies to your project?
  • Do you have a concurrency model you are already familiar with?
  • Do you have special constraints which hinder you to choose a special concurrency model?

If you ‘just’ want to have a drop-in replacement for your Seq.map operations (and operations that fall in the same category of general sequence processing) and you don’t mind the extra dependency PSeq is more than enough.

If you don’t want to have extra dependencies @swlaschin wrote a blog post on ‘simple’ asynchronous and parallel processing patterns in F# which you can find here.

If you’d like to try out a concurrency pattern that is based on ConcurrentML Hopac is the way to go.

If you (or someone on your team) has a background (or interest) in agent/actor models you can either use the included MailboxProcessor or - as @vaskir recommended - you go to Akka.NET, which offers you a lot of extras.

Concurrent and asynchronous programming is a quite complex topic (also a reason why there are so many abstractions and libraries to make these tasks manageable). If you are looking for a good book in the .NET realm (with examples in C# and F#) this might interest you.

2 Likes

When using the async api’s don’t forget you can use Async.Parallel : seq<Async<'T>> -> Async<'T []> That’s pretty much equivalent to Parallel.ForEach of system threading. https://msdn.microsoft.com/en-us/visualfsharpdocs/conceptual/async.parallel['t]-method-[fsharp]