.NET Mutterings

Andy's observations as he continues to attempt to know all that is .NET...

Thursday, July 16, 2009

Testing on varying number of cores

I’ve written many blog articles in the past that show that the performance of a piece of parallel code can vary dramatically based on the number of available cores. With that it mind, its obviously desirable even when given a machine with 8 cores that you test your code against a machine that could have substantially less.  You can resort to task manager and set Process Affinity and reduce the number of cores available for the process, but this is tedious.  There is a .NET API that allows access to controlling which cores to make available for a process.  The API requires the use of a bitmask to identity which cores to use, that's a bit ( no pun intended) overkill for what I'm trying to do, so I created a  facade that allows me to simply say use N cores.

public static class Cores
{
public static int Max
{
get
{
return Environment.ProcessorCount;
}
}
public static int CoresInUse
{
get
{
IntPtr cores =
Process.
GetCurrentProcess()
.ProcessorAffinity;


int nCores = 0;
while( cores != IntPtr.Zero )
{
if ( ((int)cores & 1) == 1 )
{
nCores++;
}
cores = (IntPtr)((int)cores >> 1);
}
return nCores;
}

set
{
if ((value < 1) || (value > Environment.ProcessorCount))
{
throw new ArgumentException("Illegal number of cores");
}

int cores = 1;
for (int nShift = 0; nShift < value-1; nShift++)
{
cores = 1 | (cores << 1);
}

Process.GetCurrentProcess().ProcessorAffinity = (IntPtr)cores;
}
}
}





The following code prints out the number of active cores and then reduces the number of cores to 4




Console.WriteLine("Using {0} out of {1}" , Cores.CoresInUse , Cores.Max);
Cores.CoresInUse = 4;
Console.WriteLine("Using {0} out of {1}", Cores.CoresInUse, Cores.Max);




Saturday, July 11, 2009

Guerilla .NET Demos from 6th July 2009

Had loads of fun as normal teaching Guerilla .NET with Rich and Marcus , All the demos from class here

Friday, June 26, 2009

Parallel.For and Parallel.ForEach…moving past Thread.Sleep

Just released a screen cast demoing Parallel.For and Parallel.ForEach where we are not demoing the virtues of Parallel Sleep….

Click here to see this and many other Rock Solid Knowledge Screen casts

Wednesday, June 24, 2009

Short and Long running tasks in .NET 4

Just added a screen cast on how to create short and long running tasks in .NET 4.  You can get to all the Rock Solid Knowledge screen casts via www.rocksolidknowledge.com/screencasts

Monday, June 15, 2009

Rock Solid Knowledge at Software Architect 2009

Just to say that we will be delivering talks at Software Architect 2009, here is a full list of our sessions.

Long or Short running tasks

Been playing around with the new version of Pfx for .NET.  I must say Ive been very impressed with the improvements since the last CTP for .NET 3.5.  So here goes a series of blogs and screen casts on some on various bits of Pfx for .NET 4 BETA 1.

The one thing that really shouts is that the original  Pfx types are no longer something for just  fine grained parallelism its is a unification of the various threading API’s.  Unifying Api’s is something not new to the .NET framework its been happening since 1.1.  As developers its great that whilst the framework is evolving a constant effort is being made to refactor and simplify previous complexity.

So previously If  I wanted to create a short running piece of background work I would favour the thread pool, if I was to write a long running piece of background activity I would have to create my own custom thread.   Creating a short running thread using the thread pool meant either QueueUserWorkItem or Delegate.BeginInvoke, and a long running via new Thread(), and calling Start.

Now for either types of situation  you simply create a new Task using the new Task type, either via the Task Factory ( not a real GOF Factory, but don’t get me started on that ), or via new Task()

Task bgTask = Task.Factory.StartNew( MyShortRunningTask);

For a long running task we use the same API but this time giving it a hint that this is a long running task and shouldn’t therefore use a thread pool thread, but create a new thread outside the thread pool

Task bgTask = Task.Factory.StartNew( MyLongRunningTask, TaskCreationOptions.LongRunning);

So too very similar calls but leaving it up to the framework to decide how best to schedule the work. 

Thursday, June 11, 2009

VS2010 Edit window outside the IDE

Just noticed that I can drag source windows outside the IDE in VS2010, this is something Ive wanted for ages…Now I can really use multiple monitors

Wednesday, June 10, 2009

Pfx team examples moving into the real world

I’ve written many articles on my blog about how Im sick of trade show demos of Pfx ( Parallel framework extensions ).  You know the ones using simple Parallel.For with Thread.Sleep or Thread.SpinWait as the piece of work.  These examples scale wonderfully but the moment people take those simple examples and apply them to their own for loops terrible performance often results.  Thankfully the Pfx team have written a blog article offering some suggestions about what to do when the piece of work inside the for loop is too small. ( Blog article ).  Interesting this is the first time I’ve seen them utilise the number of processors in the machine to determine the number of tasks, something I’ve advocated many times in the past for tasks of equal cost. 

Blog Archive

About Me

My Photo
Andy Clymer
Im a freelance consultant for .NET based technology. My last real job, was at Cisco System were I was a lead architect for Cisco's identity solutions. I arrived at Cisco via aquisition and prior to that worked in small startups. The startup culture is what appeals to me, and thats why I finally left Cisco after seven years.....I now filll my time through a combination of consultancy and teaching for Developmentor...and working on insane startups that nobody with an ounce of sense would look twice at...
View my complete profile