Friday, April 25, 2008 3:33 PM
10 Hours in F#: Exploring Concurrency Through An Ant Colony Simulation
I gave an hour long talk today, here at Atalasoft, on Concurrency in F#. It featured some slides and a small ant colony simulation to demonstrate different kinds of threading. Overall, I liked developing in F# quite a bit; however, puzzling through the interpreter errors was a brutal process indeed.
You can grab my slides here and my ant colony simulation here.
The Long Version
It all started two weeks ago…
…I had been reading about F# on various blogs for months and had done little more than dabble with it. At the time there was an open slot at my office to give a lunchtime talk. So, in order to spur myself to actually get some real experience I committed to giving a talk about F#.
Thinking back to Rich Hickey’s Clojure talk in Northampton (which I was very impressed with) I decided to write a simple ant colony simulation. An ant colony is a great environment to test out different threading techniques because it involves a large number of very small tasks. Incidentally, it’s also pretty for an audience to look at.
I built my simulation with threading abstracted as much as possible. It was surprising how well this worked out in F#; it was particularly easy due to the functional nature of the language. I kept the behavioral code, forms code and in-between code separate while using wrapper functions for locking, looping and other small threading things.
Originally, I had planned to build 5 different styles of threading into my simulation for the sake of testing:
1) Single Thread via Array2.iter
2) Asynchronous Execution via Async.Parallel
3) Massive Shared-Memory Threading via .NET’s lock and Thread
4) Message Passing via Mailbox
5) Software Transactional Memory via Greg Neverov’s Library
In the end, I ended up struggling with the new syntax much more than I had anticipated and so only managed to get Single Thread, Async and Shared-Memory done for my presentation. If you are interested, you can grab the simulation here and the slides here.
This is how it ended up looking:
I plan on finishing up my sim by adding the other two threading styles (Mailboxes and STM) and making it convenient to switch between them with form buttons. This should make it easy to profile and see how the different setups compare performance wise.
After that I think the next step is to post sections of it on the fshub forums and let the experts tear it apart. Really, that’s the only way to learn anything truly useful.
The Ant Colony Simulation attached to this post is provided as-is with no warrantees or guarantees of any kind. I claim no responsibility for any effect it may have on you or your computer. I would like to hear from you if you do anything cool with it though.
edit: It looks like the image and download links were broken all weekend. They should work now.