.NET 4.0's Parallel Extensions
|
I've been doing some catching up lately after being buried under Entity Framework for the past year. Last night, I was fortunate to be able to host Eric De Carufel at the Vermont.NET User Group where he did a presentation introducing us to Parallel Extensions. Microsoft has been making big leaps into parallel computing and in Visual Studio 2010, they will take away a huge layer of pain for allowing mere mortal developers (such as myself) to benefit from spreading processing tasks around not only to different threads bu tto different cores on your computer. Have you ever tried spinning up your own threads to do some asynchronous processing? It is not for the feint of heart. But a processor only has so many threads anyway. Can you imagine trying to pass the task onto one of the other cores in your computer? The parallel processing capabilities are so fully baked into .NET 4.0 that you won't even need to reference a special namespace. There are two key ways to take advantage of it. The most fun is when executing a LINQ query that might have to process a slew of data in order to create results. Imagine having to run through a series of files on your hard drive locating ones which are larger than 1MB. The query would look something like this: from file in MyFiles where file.FileSize>100000 select file Today, the runtime will only be able to check the file size one file at a time. By signalling that you want the query to run in parallel, however, the framework will figure out what threads (even if they are on different cores) are available to help out with the chore and have all of the chosen threads working concurrently on the task. THe implementation is a no brainer. from file in MyFiles.AsParallel() where file.FileSize>1000000 select file Since the files won't be inspected synchronously, it is easy for them to get out of order when they are returned. Using AsOrdered will ensure that they come back in their original order. This will mean however tha tyou have to wait until all of the files have been selected. from file in MyFiles.AsParallel().AsOrdered() where file.FileSize>1000000 select file The parallel extensions also provide a way to do loops such as For or ForEach concurrently. Watching a demo of a normal ForEach followed by a comparable demo using the Parallel ForEach really made a big impression on me as to what a huge impact this can have on our apps with so little effort. The smart guys have already done the hard work for me! Because the parallel processing is so thread intensive, you need to use thread safe classes and features. There is a whole new set of collections that are thread safe which you can use. Another interesting thing that Eric showed was that you can customize how the parallel process chooses the threads and cores. There is a default algorithm, but you can change that by suggestnig the minimum number of cores to use along with teh ideal number of threads and cores. The June 2008 CTP can be used with Visual Studio 2008, but things have changed dramatically since then and you will more likely want to wait until you have your hands on the VS2010 beta to really play with this stuff. In the meantime, there's a great 10-4 screencast with Stephen Toub demoing the core features. Additionally, check out the Parallel Computing Developer Center on MSDN. Many thanks to Eric for driving from Montreal down to Burlington last night to open our eyes to this new technology! |

