tags:

views:

387

answers:

7

I am new to .Net platform. I did a search and found that there are several ways to do parallel computing in .Net:

  1. Parallel task in Task Parallel Library, which is .Net 3.5.

  2. PLINQ, .Net 4.0

  3. Asynchounous Programming, .Net 2.0, (async is mainly used to do I/O heavy tasks, F# has a concise syntax supporting this). I list this because in Mono, there seem to be no TPL or PLINQ. Thus if I need to write cross platform parallel programs, I can use async.

  4. .Net threads. No version limitation.

Could you give some short comments on these or add more methods in this list? Thanks.

+2  A: 

There is also the Reactive Extensions for .NET (Rx)

Rx is basically linq queries for events. It allows you to process and combine asynchronous data streams in the same way linq allows you to work with collections. So you would probably use it in conjunction with other parallel technologies as a way of bringing the results of your parallel operations together without having to worry about locks and other low level threading primitives.

Expert to Expert: Brian Beckman and Erik Meijer - Inside the .NET Reactive Framework (Rx) gives a good overview of what Rx is all about.

EDIT: Another library worth mention is the Concurrency and Coordination Runtime (CCR), it's been around for a long time (earlier than '06) and is shipped as part of the Microsoft Robotics Studio.

Rx has a lot of the same cool ideas that the CCR has inside it, but with a much nicer API in my opinion. There's still some interesting stuff in the CCR though so it might be worth checking out. There's also a distributed services framework that works with the CCR that might make it useful depending on what you're doing.

Expert to Expert: Meijer and Chrysanthakopoulos - Concurrency, Coordination and the CCR

Jacob Stanley
I haven't a clue how I managed to get a downvote for this, Rx makes parallel programming significantly easier in some situations - I use it extensively.
Jacob Stanley
+1 for being the first to mention Rx. ;)
Stephen Cleary
@Mauricio they are highly related, running everything in parallel is useless if you can't coordinate your results. It's clear from the question that the OP is interested in this aspect as well. He states "F# has a concise syntax supporting this" referring to the continuation monad, Rx is essentially the continuation monad for all .NET languages that support LINQ.
Jacob Stanley
@Jacob: sorry I deleted my comment, I'm writing an answer trying to separate the concepts for the OP.
Mauricio Scheffer
@Mauricio no problem :) It's certainly worth explaining. I think async and parallel go hand in hand, just knowing about how to make things go in parallel is only half the story. As George Chrysanthakopoulos always says in the CCR videos: "it's not about concurrency, it's about coordination, when you have coordination, concurrency just happens"
Jacob Stanley
+2  A: 

One more is the new Task Parallel library in .NET 4.0, which is similar and along the lines of what you've already discovered, but this may be an interesting read:

Task Parallel Library

byte
+6  A: 

You do need to do a fair amount of research in order to determine how to effectively multithread. There are some good technical articles, part of the Microsoft Parallel Computing team's site.

Off the top of my head, there are several ways to go about multithreading:

  1. Thread class.
  2. ThreadPool, which also has support for I/O-bound operations and an I/O completion port.
  3. Begin*/End* asynchronous operations.
  4. Event-based asynchronous programming (or "EBAP") components, which use SynchronizationContext.
  5. BackgroundWorker, which is an EBAP that defines an asynchronous operation.
  6. Task class (Task Parallel Library) in .NET 4.
  7. Parallel LINQ. There is a good article on Parallel.ForEach (Task Parallel Library) vs. PLINQ.
  8. Rx or "LINQ to Events", which does not yet have a non-Beta version but is nearing completion and looks promising.
  9. (F# only) Asynchronous workflows.

Update: There is an article Understanding and Applying Parallel Patterns with the .NET Framework 4 available for download that gives some direction on which solutions to use for which kinds of parallel scenarios (though it assumes .NET 4 and doesn't cover Rx).

Stephen Cleary
+1 for an extensive listing
Jacob Stanley
+2  A: 

Strictly speaking, the distinction between parallel, asynchronous and concurrent should be made here.

Parallel means that a "task" is split among several smaller sub-"tasks" that can be run at the same time. This requires a multi-core CPU or a multi-CPU computer, where each task has its dedicated core or CPU. Or multiple computers. PLINQ (data parallelism) and TPL (task parallelism) fall into this category.

Asynchronous means that tasks run without blocking each other. F#'s async expression, Rx, Begin/End pattern are all APIs for async programming.

Concurrency is a concept more broad than parallelization and asynchrony. Concurrency means that several "tasks" run at the same time, interacting with each other. But these "tasks" don't have to run on separate physical computing units, as is meant in parallelization. For example, multitasking operating systems can execute multiple processes concurrently even on single-core single-CPU computers, using time slices. Concurrency can be achieved for example with the Actor model and message passing (e.g. F#'s mailbox, Erlang processes (Retlang in .Net))

Threads are a relatively low-level concept compared to the concepts above. Threads are tasks running within a process, running concurrently and managed directly by the operating system's scheduler. You can implement parallelization when the operating system maps each thread to a separate core, or an Actor model by implementing message queuing, routing, etc on each thread.

Mauricio Scheffer
I post this as community wiki so others can correct and refine my definitions...
Mauricio Scheffer
A: 

There are also some .NET libraries for data parallel programming which target the Graphics Processing Unit (GPU) including:

Microsoft Accelerator is for data parallel programming and can target either the GPU or multi-core processors.

Brama is for LINQ style data transformations that run on the GPU.

CUDA.NET provides a wrapper to allow to CUDA to be used from .NET programs.

Paul Dixon
A: 

Hi,

two major ways to do parallel are threads and the new task based library TPL.

Asynchronous Programming you mention is nothing more then one new thread in the threadpool.

PLINQ, Rx and others mentioned are actually extensions sitting on the top of the new task scheduler.

the best article explaining exactly the new architecture for new task scheduler and all libraries on the top of it, Visual Studio 2010 and new TPL .NET 4.0 Task-based Parallelism is here (by Steve Teixeira, Product Unit Manager for Parallel Developer Tools at Microsoft):

http://www.drdobbs.com/visualstudio/224400670

otherwise Dr Dobbs has dedicated parallel programming section here: http://www.drdobbs.com/go-parallel/index.jhtml

The main difference between say threads and new task based parallel programming is you do not need to think anymore in terms of threads, how do you manage pools and underlying OS and hardware anymore. TPL takes care for that you just use tasks. That is a huge change in the way you do parallel on any level including abstraction.

So in .NET actually you do not have many choices:

  1. Thread
  2. New task based, task scheduler.

Obviously the task based is the way to go.

cheers Valko

Valko
A: 

A good article explaining parallel computing in .net is here.

HotTester