Simple parallel programming

0

Posted on : 18-05-2009 | By : matteosp | In : .Net Programming, ParallelProgramming, Threading

While waiting for Parallel Extensions (wikipedia), that will be shipped with next .Net Framework release, I designed a couple of class that allow to quickly (and easily!) write code that make use of multi-threading and, therefore, speed-up many tasks. What I show here is a simple class able to collect pieces of code (in form of delegate) to be executed concurrently, and parallelize and synchronize the execution. The main goals I kept in mind while realizing the solution were:

  • Ease of use, minimal requirement in terms of multi-threading skills
  • Allowing single-threaded code to be ported to multi-threading with few changes
  • Code readability
  • Support for exception management

Here what I came up with:

The API exposes one class, ParallelExecutor, and one interface, IExecutionResult (plus a generic specialization of the interface and the necessary implementations). Before seeing the internals of the solution, I want to show the ease of use of the API.
Let’s start with this example:

   1:  void SerialExecution()
   2:  {
   3:      Uri uri; int value;
   4:  
   5:      CallWebService(uri);
   6:      PerformComputation(value);
   7:  }

CallWebService() and PerformComputation() represent the most classic example of methods that can (and should!) run in parallel as they perform respectively I/O and CPU computation. But usually programmers haven’t enough confidence with multi-threading execution and synchronization to do things in the right way. In any case they shouldn’t care about this stuff, they should focus on coding logic, and not on coding about how the logic get’s executed. Let’s see how ParallelExecutor comes in help:

   1:  void ParallelExecution
   2:  {
   3:    Uri uri; int value;
   4:  
   5:    ParallelExecutor executor = new ParallelExecutor();
   6:  
   7:    executor.Add(CallWebService, uri);
   8:    executor.Add(PerformComputation, value);
   9:  
  10:    executor.WaitAll();
  11:  }

Not too bad, right? With a couple of lines I got the two methods run concurrently. This is pretty simple: the Add() method takes as arguments the method to invoke and the arguments of that method. The WaitAll() causes the calling thread to wait for all the methods passed to Add() to complete. Take a (partial) look to the ParallelExecutor definition:

   1:  public class ParallelExecutor
   2:  {
   3:    public IExecutionInfo<object> Add(Action workItem)
   4:    public IExecutionInfo<object> Add<T>(Action<T> workItem, T arg)
   5:    //overloads for other Action versions 
   6:  
   7:    public IExecutionInfo<TResult> Add<TResult>(Func<TResult> workItem)
   8:    public IExecutionInfo<TResult> Add<T, TResult>(Func<T, TResult> workItem, T arg)
   9:    //overloads for other Func versions 
  10:  
  11:    public bool WaitAll():
  12:  }

As you can see, method to be parallelized are passed to Add() in form of variations of Action and Func generics delegate (there are also overloads that take MethodInfo and the standard Delegate). But what about the Add() return type, IExecutionResult? Let’s see the interface declaration:

   1:  public interface IExecutionInfo
   2:  {
   3:    object[] Args { get; }
   4:  
   5:    Exception Exception { get; }
   6:  
   7:    object Result { get; }
   8:  }
   9:  
  10:  public interface IExecutionInfo<TResult> : IExecutionInfo
  11:  {
  12:    TResult Result { get; }
  13:  }

At every invocation of the Add() method an instance of a class implementing IExecutionResult is created, and its Args property is filled with method invocation arguments. After the invocation completes, the Result and Exception property are filled too (in both case, if any). And the magic of generics allow the code to be type safe. So, returning to the example, if CallWebService() has a return value and if you want to check for exceptions, you can write:

   1:  void ParallelExecution
   2:  {
   3:    Uri uri; int value;
   4:  
   5:    ParallelExecutor executor = new ParallelExecutor();
   6:  
   7:    IExecutionInfo<int> execution = executor.Add<Uri, int>(CallWebService, uri);
   8:    executor.Add(PerformComputation, value);
   9:  
  10:    executor.WaitAll();
  11:  
  12:    if (execution.Exception != null)
  13:        ManageException(execution.Exception);
  14:    else
  15:        Console.WriteLine("Result: {0}", execution.Result);
  16:  }

Another couple of properties exposed by ParallelExecutor allow you to control when the execution starts (immediately after Add() or when WaitAll() gets called) and the aggressiveness of the threading policy (one new Thread for every method passed to Add() or threads got from ThreadPool).

Obviously this is a very simple implementation, far away from what Parallel Extensions will be. But its simplicity may suite well some implementation with no heavy requirements.

Source Code.

How I got Started in Software Development

1

Posted on : 30-07-2008 | By : matteosp | In : Fun, Miscellaneous, Programming

I had the great pleasure of having been tagged by Adrian. So… it’s my turn:

How old were you when you started programming?

In 1994, when I was 16 and I was at the high school. But I must say also that at the age of about 10 year a “played” with BASIC on a Laser 500, something similar to a Commodre 64.

How did you get started in programming?

… in those days I learned Turbo Pascal, and I used it to solve not too complex math and physics problems.

What was your first language?

As I said, the very first language was Pascal. But, as a professional, I consider my first language to be Visual Basic 6. It is for sure the language that made me falling in love with programming.

What was the first real program you wrote?

At the end of a one year programming training I build a RSE – really simple ERP ;-) – for demo purposes. A WinForm application over an MS Access database. Presentation Layer e Business Logic were mixed up in the Visual Basic 6 forms, but I think here I wrote my first Data Layer.

What languages have you used since you started programming?

In order Basic, Turno Pascal, C++, Visual Basic 6, Java, PHP, ASP, JavaScript, VB Script, C#, Visual Basic .Net, Python.

What was your first professional programming gig?

In Brain Force, the company I’m still working for. 6 years ago.

If you knew then what you know now, would you have started programming?

I love programming: it consume a lot of resources but gives back a lot of satisfaction. But I would have choosen something else.

If there is one thing you learned along the way that you would tell new developers, what would it be?

Start from the theory and with the doc. Ever. Ever!! And pay attention to who wrote what you read.

What’s the most fun you’ve ever had … programming?

An ORM. I’m really proud of it.

Now, let’s tag someone else…

The final truth about OOP

0

Posted on : 14-04-2008 | By : matteosp | In : Programming, Quotes, Software sucks

My guess is that object-oriented programming will be in the 1980s what structured programming will be in 1970s. Everyone will be in favor of it. Every manufacturer will promote his products as supporting it. Every manager will pay lip service to it. Every programmer will practice it (differently). And no one will know just what it is.

T. Rentsch

The more I read these words, the more I’m convinced this is one of the more accurate prediction I’ve heard about in programming. I feel it so… real.

via Booch, Grady. Object-Oriented Analysis and Design with Applications, Second Edition. Boston: Addison-Wesley, 1993. Link.
via West, David. Object Thinking. Redmon: Microsft Press, 2003. Link.