mscommunity.net

Interactive mscommunity.net online activities
Signed in as anonymous | Edit Profile | Sign out | Help
in Search

Weblog :: Boris Ševo

Sporadic posts about my interests, e.g. software development (mostly .NET), technology in general and some occasional rant.

Concurrent programming in C# - Parallel.Do method

This is my first one but certainly not the last one post about the concurrent programming in C#. Free lunch is over
is an excellent text which shows why concurrent programming matters. If you ever read anything about it, you
have probably noticed that people talk about it as something hard and complicated. Is it really so hard and
complicated? Well, in my opinion it depends. It depends about how much concurrent your program need to be,
what kind of knowledge do you have about this programming model, which libraries are you using for threading etc.
I have some knowledge about this topic, but it's very far from being profound. Because of that I'm happy that
there are some smart people in Parallel Computing Developer Center who made an excellent extension
for .NET - ParallelFX.

There is a lot in it and there's a lot interesting topics to write about, but because this is my first post, I will start
with something simple and still very powerful. I will write only about Do method. According to documentation,
it's a method with following declaration:

public static void Do(
params Action[] actions
)

This method "executes each of the provided actions inside a discrete, asynchronous task". It doesn't
finishes until each of provided actions has completed. That means, it will finish when the slowest action finishes. Imagine
that you have two long running methods called foo and bar. If you call them in sequential manner you will need to
wait until foo finishes its execution, then you will run bar method and wait until it finishes its execution. If you run
them in parallel you will only need to wait as much as the longest of this two is executing. Let's see an example.

    class Program
{
private static double x = 1000000000;

private static void foo()
{ for (double i = 0; i < x; i++) ;}

private static void bar()
{ for (double i = 0; i < x; i++) ;}


static void Main(string[] args)
{
Stopwatch watch = new Stopwatch();

watch.Start();
// *** sequential
foo();
bar();
watch.Stop();

Console.WriteLine("Not parallel: " +
(watch.ElapsedMilliseconds / 1000.0).ToString() +
"s");

watch.Reset();
watch.Start();
// *** parallel
Parallel.Do(() => foo(), () => bar());
watch.Stop();

Console.WriteLine("Parallel: " +
(watch.ElapsedMilliseconds / 1000.0).ToString() +
"s");
}
}

The time which you get running the above program will depend about your computer CPU. On my computer
(Core Duo, 2.33GHz) I get:
Not parallel: 8,769s
Parallel: 4,595s
So, with a help of just one line of code we have a multithreaded program!

That's all for the first time. It was just a little introduction. When I learn more about ParallelFX I will write again.

Comments

 

tbronzin said:

Bernard Katić have plans to make a hole session on upcoming WinDays 2008, vote for him at www.mscommunity.net :-)

veljača 27, 2008 7:02
 

boris.sevo said:

I did the homework :). Unfortunately I want be able to attend the session.

veljača 27, 2008 4:33
 

bernard said:

Thanks a lot for the great ParallelFX intro. It seems that "real programmers" really like it and it helped my PnP session, since after you've published it, I got a whole bunch of votes for Paralell Programming  session :-) I owe you a beer! Keep on writing...

veljača 29, 2008 10:38
 

boris.sevo said:

Don't thank me, thank to Tomislav. Without his comment, there wouldn't be so many votes.

I also hope that you will start writing about this topic, maybe from enterprise perspective ;).

ožujak 1, 2008 12:17

Leave a Comment

(required) 
(optional)
(required) 
Submit
Powered by Community Server (Commercial Edition), by Telligent Systems