Parallel Tasks and Parallel Stacks content updated for VS2010 Beta2

Mon, October 19, 2009, 08:59 AM under ParallelComputing
For VS2010 Beta2, rather than write new introductory and overview posts on my two favorite debugger windows (Parallel Tasks and Parallel Stacks), I thought I’d update 4 of the existing blog posts on those topics.

Please read the updated text and enjoy the updated screenshots
- Parallel Tasks
- Parallel Stacks
- Parallel Stacks – Tasks View
- Parallel Stacks – Method View

Also, I have updated the recordings behind the screencast URLs on channel9:
- Parallel Stacks
- Parallel Tasks

Each one of the links above has a comment section, feel free to use it for feedback and questions!

Installing HPC Server 2008

Sun, October 18, 2009, 08:47 PM under ParallelComputing | HPC
Recently I decided to play around with developing for a cluster on the Microsoft platform and below are the steps I had to take with regards to installation.

First I gathered a number of old machines at the office, in my case that is 3 dual-core boxes, but you could have done it with just 2 PCs (or many more of course). On each one of those you must install: Windows HPC Server 2008 (link to trial)
a. This includes a trial of the Windows Server 2008 RTM 64-bit operating system (SRVHPC_EN.iso). I already had Windows Server on a separate disc, so I installed what I had. I did not try R2, which is the same code base as Win7, but that should work too. Note that this is just the vanilla operating system (even Standard Edition is good enough) and the important part is the 64-bit. I will assume that you are all capable of installing an OS, so no more instruction or special consideration needed here (window update, join domain, add users etc).

b. The second part of the link above is an add-on to the operating system, namely the HPC Pack 2008 (HPCEval.iso). It is a wizard with a set of steps to complete which are all fairly explanatory of the "click Next" flavor. After installation of the HPC Pack, the HPC Cluster Manager application runs up automatically (or else you can run it through the Start menu) for further step-by-step configuration in an easy to follow outofthebox To-do List.



Here are some tips for step b from above, for your developer cluster setup:
i) In the HPC Pack wizard, you will create one of the machines as the Head Node.

ii) In the HPC Pack wizard, the remainder machines will be created as the Compute Nodes.

iii) In Cluster Manager, click on "Configure your network" and choose topology 5 "All Nodes only on an Enterprise Network".

iv) In Cluster Manager, click on "Add compute nodes" and then select "Add compute nodes that have already been configured".

v) In Cluster Manager, click "Change the role of the head node" and also make it a WCF Broker Node.

vi) In Cluster Manager, "Validate your cluster" under Diagnostics.

It may be useful to cross-reference the text above with the following screenshot of HPC Cluster Manager


Now that your cluster is installed, you need to set up your development machine (x86 or x64) for developing on this cluster. Here are the things you need to install:
1. Visual Studio 2010

2. HPC Pack 2008. This is essentially the same msi you installed on the server (step b above), but here you will choose the 3rd option Client Utilities (which will probably be your only enabled option as per my screenshot further above).

3. HPC Pack 2008 SDK. This allows writing client applications that interact with the Job Scheduler running on the Head Node. MSDN has a dedicated page to the HPC SDK.

4. (optional) Install the separate MPI Project Template, for VS2010.

You are now ready to develop your cluster applications on your development machine and to debug/deploy them on your cluster. More on that in future blog posts.

PDC09 or SC09

Fri, October 16, 2009, 05:19 PM under Events
Recently I was given the choice to attend the PDC09 or the Super Computing conference: SC09. They both run in the same week, so attending both was not an option.

From a personal perspective I like visiting new cities, but in this case I have been to both Los Angeles and Portland so that is not a factor. From a geek perspective I like getting a feel for as many conferences as possible and I did PDC last year (repeated at Tech ED EMEA), whereas I have never been to an SC conference – so that tipped the balance a bit. More importantly from a more_useful_to_my_job perspective, I need to "connect" with more developers that are interested in the HPC and GPU space… This is the customer I want to delight with the work our team will deliver beyond VS2010…

If you, dear reader, are going to PDC09 be assured that the work our team is delivering in VS2010 will be well represented. View the relevant PDC sessions here.

If you are planning on attending SC09, please drop me a note (via the email link on the left) so we can meet and have a chat in Portland…

Parallelizing a loop with tasks, avoiding the pitfall

Tue, October 13, 2009, 02:39 PM under ParallelComputing
Most readers of this blog should know that it is extremely easy to parallelize loops with .NET 4. So serial code performing matrix multiplication like this:
    for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
int tmp = 0;
for (int k = 0; k < size; k++)
{
tmp += m1[i, k] * m2[k, j];
}
result[i, j] = tmp;
}
}
…can be parallelized like this:
      Parallel.For(0, size, (int i) =>
{
for (int j = 0; j < size; j++)
{
int tmp = 0;
for (int k = 0; k < size; k++)
{
tmp += m1[i, k] * m2[k, j];
}
result[i, j] = tmp;
}
});
How about parallelizing the code using Tasks directly (instead of indirectly)? The clever thing to do is somehow partition the data (statically or dynamically) and assign chunks to tasks, but even the naïve approach of one task per outer iteration is better than nothing:
    Task[] tasks = new Task[size];
for (int i = 0; i < size; i++)
{
tasks[i] = Task.Factory.StartNew(() =>
{
for (int j = 0; j < size; j++)
{
int tmp = 0;
for (int k = 0; k < size; k++)
{
tmp += m1[i, k] * m2[k, j];
}
result[i, j] = tmp;
}
});
}
Task.WaitAll(tasks);
Can you see the issue with the code above?

TIP: the issue has nothing to do with tasks, threads or even .NET 4. It has to do with anonymous methods since the day they were introduced. The clue is that the following snippet fixes the issue:
    Task[] tasks = new Task[size];
for (int n = 0; n < size; n++)
{
int i = n;
tasks[n] = Task.Factory.StartNew(() =>
{
for (int j = 0; j < size; j++)
{
int tmp = 0;
for (int k = 0; k < size; k++)
{
tmp += m1[i, k] * m2[k, j];
}
result[i, j] = tmp;
}
});
}
Task.WaitAll(tasks);
If you know the reason please move along. If however this was news to you, go read the entire page (and the outbound links) on stackoverflow.

By the way, we could have fixed the snippet with this approach too, since tasks accept a state parameter:
    Task[] tasks = new Task[size];
for (int i = 0; i < size; i++)
{
tasks[i] = Task.Factory.StartNew(ii =>
{
int iii = (int)ii;
for (int j = 0; j < size; j++)
{
int tmp = 0;
for (int k = 0; k < size; k++)
{
tmp += m1[iii, k] * m2[k, j];
}
result[iii, j] = tmp;
}
}, i);
}
Task.WaitAll(tasks);
More on why you may choose to pass the variable as state instead of capturing it, in a future post.

Visual Studio Profile

Fri, October 9, 2009, 04:51 PM under VisualStudio
Visual Studio 2005 introduced the concept of profiles (which still exists in VS2008 and VS2010). You first experience that through the annoying dialog you see the first time you launch VS asking to bucket yourself into one of General, Project Management, VB, C#, C++, F# and Web.

TIP: Click on each one of the options in the list to read the description on the right that gives insight into what will happen.

Once you choose your label, you get default settings that the product teams decided around default window & toolbar layouts, order of choices in the New Project dialog, menu configurations and keyboard shortcuts.

I guess I was used to the configurations of VS.NET 2002 and VS.NET 2003 so I did not embrace this VS2005 feature. FYI, I always pick General, which largely represents the settings that the early version of Visual Studio had and the one that caters better for developers programming in more than one language.

The profile concept gives the product teams the power to say things like (NOTE: I am making up these examples!) "you are a VB developer, I don’t think you need the Threads window shown by default" or "you are a C# developer, you have no use for the Object Browser" and so on. As a developer, I'd rather see everything. Moreover, most settings that are pre-chosen can be changed via the Tools->Options dialog so why try and bucket me, just try to educate me more about how to configure things. In any case, that is probably the saving grace for the feature: you can override and change most of the decisions made on your behalf. This is another reason you must navigate every option in the Tools->Options dialog, which I always advocate to new VS users. Besides creating, you can also persist your own settings via the Tools->Import and Export menu and can even create macros attached to toolbarbuttons for switching fast between them.

To reset VS completely (which includes asking again for a profile choice when you start it next time), from an elevated VS command prompt type: devenv /resetuserdata (warning: all your preference settings will be lost, so back them up). Try the various profiles (with your existing projects) to see what you may be missing…

Windows Mobile development in VS2010

Wed, October 7, 2009, 09:55 PM under MobileAndEmbedded
With the release yesterday of Windows Phone (press release , blog post , training , launch site) and with the upcoming release of Visual Studio 2010 Beta2, we are starting to see the question again: "where is the smart device development support in Visual Studio 2010?".

This question was asked a lot during the VS2010 Beta1 timeframe and the answer remains the same: Mobile Development Tools are now aligned to the mobile platform releases and are hence decoupled from Visual Studio releases, so I personally guess we should see tool support in VS2010 when a new mobile platform is released. For now, stick with VS2008 for device development needs (official statement) and keep an eye on the Windows Mobile Development Center.

Visual Studio 2010 floating windows can be maximized

Mon, October 5, 2009, 08:47 PM under VisualStudio
Sometimes the little features give you the most pleasure. At long last, Visual Studio 2010 floating toolwindows can be maximized via the maximize button (or double clicking on their title bar, of course). This featurette came late in the Beta1 cycle.

Below a screenshot of the maximize button (when window is floating) plus the menu options when you hit the triangle drop down indicator on the title bar. Also circled in red the maximized button appearance when the window is maximized:


When you combine the floating window maximization support with the VS2010 multi-monitor support, you get the productivity of being able to quickly drag a window to the other monitor, maximize it (and when you are done, restore it and return it).

As I keep saying about VS2010, ILTNUI (I Love The New UI).

Visual Studio Documentary

Thu, October 1, 2009, 01:00 AM under Links
Hopefully anyone reading this blog monitors ch9, but on the remote chance that is not true, you must watch the two-part brilliant 1-hour documentary on Visual Studio: Part One and Part Two.

MSDN Mag: Parallel Debugging in VS2010

Wed, September 9, 2009, 06:14 PM under ParallelComputing
The September issue of the MSDN Magazine has gone live online and the article I co-authored is also available.

Warning: contains screenshots of VS2010 Beta2 that is not released yet ;-)

Enjoy: Debugging Task-Based Parallel Applications in Visual Studio 2010.

.NET 4: Monitor.Enter replaced by Monitor.Enter

Mon, August 31, 2009, 06:27 PM under dotNET
The most common way to protect resources from concurrent access from multiple threads in .NET is by using the C# lock (or SyncLock in VB) statement.
lock(obj)
{
// only 1 thread executes this at a time
}
Under the covers it generates code that is equivalent to:
    Monitor.Enter(obj);
try
{
// only 1 thread executes this at a time
}
finally
{
Monitor.Exit(obj);
}
If you compile the above under Visual Studio 2010 and look at the code generation in your favorite disassembler (e.g. Reflector before it gets a chance to be updated for .NET 4 and hide the detail) you'll find different code generation similar to this:
    bool taken = false;
try
{
Monitor.Enter(obj, ref taken);
// only 1 thread executes this at a time
}
finally
{
if (taken)
{
Monitor.Exit(obj);
}
}
The most important thing to note is the introduction of a new overload for Monitor.Enter (and there is one for TryEnter too) which are public so you can even use them directly.

Instead of explaining here what problem the new APIs are trying to address (asynchronous exceptions occurring just before the try block and after the call to Monitor.Enter), I defer you to Joe's old blog post on orphaned locks.

Don't be too surprised if the original Monitor.Enter overloads (without the boolean parameter) become obsolete.