Arun’s Blog

January 5, 2011

Monostate Pattern

Monostate is a very simple pattern. It is an alternative to Singleton pattern in achieving singularity. However, it differs significantly from Singleton pattern in its implementation. Unlike the Singleton pattern which restricts the instantiation of a class to a single instance through a private constructor, Monostate pattern does not attempt to restrict the number of instances of a class. It achieves the singularity through static fields declared in the class.

Static members of a class do not belong to the instance of a class. Instead, they belong to the type itself. Therefore static members have just one copy of themselves unlike non-static members which have one copy for each of their instances.

Coming to the main point, what is the usefulness of this interestingly simple pattern? Let us look at a simple scenario to answer that.

We have an Employee class. This class has three properties Name, Id and Counter backed by three fields _name, _id and _counter. The name field holds the name of the employee and the id field holds the ID of the employee. The third field, counter holds the number of employees. Our requirement is such that no matter which instance of the Employee adds the new employee, counter must get updated in the correct increment. Also when the employee gets removed, we want to decrement the counter. We are going to use Monostate pattern to accomplish this.

We are going to build a small application to demonstrate this pattern.

Step 1: Fire up your Visual Studio 2010. Express edition is OK.

Step 2: Create a new MVC project and name your project MvcMonostate. You can create any type of project such as WPF, console app, ASP.Net, etc. This is not an exercise in MVC at all.

Step 3: Right click on MvcMonostate project in your Solutions Explorer and add a folder called Class.

Step 4: Add a class file to the Class folder. Name the file Employee.cs.

Step 5: Copy and paste the code below into the Employee.cs file.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.ComponentModel;

namespace MvcMonostate.Class

{

public class Employee : IDisposable

{

private bool _disposed = false;

private Component component = new Component();

private string _name;

private int _id;

private static int _counter;

public string Name

{

get { return _name; }

set { _name = value; }

}

public int Id

{

get { return _id; }

set { _id = value; }

}

public static int Counter

{

get { return _counter; }

}

public bool IsDisposed

{

get { return _disposed; }

set { _disposed = value; }

}

public void AddEmployee(string name, int id)

{

Name = name;

Id = id;

lock (this)

{

_counter++;

}

}

public void Dispose()

{

Dispose(true);

GC.SuppressFinalize(this);

}

protected virtual void Dispose(bool disposing)

{

if (!this._disposed)

{

if (disposing)

{

component.Dispose();

}

_disposed = true;

}

lock (this)

{

_counter–;

}

}

}

}

Step 6: Replace the content of the HomeController.cs file with the content below. The file is located inside the Controller folder.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.Mvc;

using MvcMonostate.Class;

using System.Text;

namespace MvcMonostate.Controllers

{

[HandleError]

public class HomeController : Controller

{

public ActionResult Index()

{

Employee e1 = new Employee();

e1.AddEmployee(“Arun K Pandey”, 01);

Employee e2 = new Employee();

e2.AddEmployee(“S Denise Pandey”, 02);

Employee e3 = new Employee();

e3.AddEmployee(“Amber N K”, 03);

StringBuilder sbEmployee = new StringBuilder();

if (!e1.IsDisposed)

{

sbEmployee.Append(e1.Name);

sbEmployee.Append(“, “);

}

if (!e2.IsDisposed)

{

sbEmployee.Append(e2.Name);

sbEmployee.Append(“, “);

}

if (!e3.IsDisposed)

{

sbEmployee.Append(e3.Name);

sbEmployee.Append(“, “);

}

ViewData["Counter"] = Employee.Counter;

ViewData["Employee"] = sbEmployee.ToString();

/*e3.Dispose();

if (!e1.IsDisposed)

{

sbEmployee.Append(e1.Name);

sbEmployee.Append(“, “);

}

if (!e2.IsDisposed)

{

sbEmployee.Append(e2.Name);

sbEmployee.Append(“, “);

}

if (!e3.IsDisposed)

{

sbEmployee.Append(e3.Name);

sbEmployee.Append(“, “);

}

ViewData["Counter"] = Employee.Counter;

ViewData["Employee"] = sbEmployee.ToString();    */

return View();

}

public ActionResult About()

{

return View();

}

}

}

Let us take a look at our code now. Note that the counter field is static while everything else in the Employee class is not static. This is to ensure that there can only be one copy of counter instance regardless of the number of existing instances of Employee class itself.  In the HomeController class, we are creating three instances of Employee e1, e2 and e3. Each of these instances adds an employee and increments the counter field. At the end, value of Counter property which is backed by static counter field is stored in the ViewData["Counter"] field to be displayed in the View (UI).

Hit F5 to run the application. When the application loads, UI will display Number of Employees as 3. Even though the AddEmployee method was called from three different instances of Employee class, the value of counter was incremented in one central location, resulting in accurate count of number of employees. Now, uncomment the code block between lines e3.Dispose() and ViewData["Employee"] = sbEmployee.ToString(); comment out the code block between the lines if (!e1.IsDisposed) and ViewData["Employee"] = sbEmployee.ToString(). Run the application again. This time the UI will display Number of Employees as 2 because the last Employee was removed and the object was disposed. Keep in mind that in managed code, you cannot force the garbage collection even when you call the dispose method especially when the object has been referenced. So to keep the track of disposed item, we are using IsDisposed Boolean property.

December 30, 2010

Interface Segregation Principle (ISP)

Filed under: Software Development — arunscottsdale @ 5:41 pm
Tags: , , ,

Hello everybody! It has been a while since I wrote my last blog on parallel programming. We are going to put a halt to that series unless someone is interested in it. Before we get to the topic of our blog, I just wanted to give you all an update on what I am currently doing. I have undertaken a project in Washington, DC to work for the DC government. So currently I am fighting cold, blizzard and traffic in a big city called Washington. It has been a drastic change in my lifestyle as life in DC is nothing like the life in Beaverton, OR. After first week of attempting to drive to work and spending two hours in traffic each way, I gave up on driving. Now, I take metro to and from work; it takes me about 35 minutes to commute. Since I returned my rental car, I have nowhere to go in the evenings and weekends, which means I have lots of time at hand. I decided to put this time to good use and accelerated reading Robert C Martin’s Agile Principles, Patterns and Practices. It is a very good book and I will highly recommend it to anyone who is in the field of software development. This book covers a range of topics including UML, Agile Development, Agile Design, case studies and a funny must read satire.

Agile Design section covers various design principles. One of those principles Interface Segregation Principle will be the topic of our blog. In the ISP chapter of the book, I came across this UML diagram:

This blog is the result of an exercise I undertook to implement this UML diagram into C# code. Some of you may know ISP by a different name, Role Interface. If you are not familiar with ISP then I would suggest you read following topics in that order first before proceeding with the rest of the blog:

-          Single Responsibility Principle

-          Open Close Principle

-          Liskov Substitution Principle

-          Dependency Inversion Principle

-          Interface Segregation Principle

All of these topics are covered under Agile Design section of Agile Principles, Patterns and Practices.

Let us talk about business specification of the application first. We will call this application Electronic Gate System. This system has two classes called Door and TimedDoor. Door class has two methods Lock() and Unlock(). It also has a Boolean property called isDoorOpen. When a door needs to be locked, the Lock() method is called. When a door needs to be opened, the Unlock() method is called. IsDoorOpen property is a read only property which stores the current status (open or closed) of the door. Then we have a need to create a door system which will sound an alarm if the door has been left open for a certain period of time. This new class called TimedDoor will have all the attributes of Door class but in addition, it will have method called TimeOut() which will sound the alarm if the door has been left open beyond certain period of time. So the TimedDoor will derive from Door class. TimedDoor will be a client of another object called Timer. TimedDoor will use Timer object to monitor the elapsed time period.

There are two points to note in the above design:

a)      The design has segregated the Interface (TimerClient) which acts as a client to Timer because the functionality (TimeOut method) is specific to TimedDoor class. So, it is TimedDoor which implements the TimerClient interface.

b)      Under ordinary design, Timer will depend on TimedDoor but in this case we have inverted the dependency; now the TimedDoor depends on Timer. This promotes the decoupling between Timer and its client. Thus advances the cause of code reusability.

Now we are going to write our C# code. I have made some changes in the naming convention. I have named my interface ITimer instead of TimerClient and I have changed the name of Timer class to dTimer to avoid ambiguity with Timer class from FCL.

Here is the code for Door class:

using System;

using System.Windows;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace ISP.Class

{

public class Door

{

private bool _isDoorOpen = false;

public bool IsDoorOpen

{

get { return _isDoorOpen; }

}

public void Lock()

{

_isDoorOpen = false;

}

public void Unlock()

{

_isDoorOpen = true;

}

}

}

Here is the code for TimedDoor class (Note that this class derives from Door and implements ITimer interface):

using System;

using System.Windows;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace ISP.Class

{

public class TimedDoor : Door, ITimer

{

public void TimeOut(DateTime dtSignalTime)

{

MessageBox.Show(“The door has been open since:” + dtSignalTime.ToString());

}

}

}

Here is the code for ITimer interface, which is a client to Timer class:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace ISP.Class

{

public interface ITimer

{

void TimeOut(DateTime dtSignalTime);

}

}

Here is the code for dTimer class:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Timers;

namespace ISP.Class

{

public class dTimer

{

//private int _timeOut;

private ITimer _client;

public dTimer(int timeOut, ITimer client)

{

_client = client;

Timer aTimer = new Timer();

aTimer.Elapsed +=new ElapsedEventHandler(CallTheAlarm);

aTimer.Interval = timeOut;

aTimer.Enabled = true;

}

public void CallTheAlarm(object sender, ElapsedEventArgs e)

{

DateTime dtSignalTime = e.SignalTime;

_client.TimeOut(dtSignalTime);

}

}

}

In real life these classes will interact with some sort of interface to a hardware system; also the TimeOut method will probably trigger some kind of sound or loud beep instead of message box. Similarly, Lock() and Unlock() methods will transmit signals to the hardware interface, which will result in opening or closing of the doors.

August 16, 2010

Parallel Diagnostic Tools

As promised in my previous blog (Parallel Programming in .Net), we will get acquainted with diagnostics tools related to parallel development. Let us create a sample application, which we will use to debug.

  1. Open up your Visual Studio 2010
  2. Create a WPF application using the template provided by Visual Studio. I named my solution ParallelPattern. You can name it whatever you feel like.
  3. You should have four files in your project folder: App.xaml, App.xaml.cs, MainWindow.xaml and MainWindow.xaml.cs.
  4. Open MainWindow.xaml file and copy the code provided below into it:

<Window x:Class=”ParallelPattern.MainWindow”

xmlns=”http://schemas.microsoft.com/winfx/2006/xaml/presentation”

xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml”

Title=”MainWindow” Height=”350″ Width=”525″>

<Grid>

<StackPanel x:Name=”spBody” Orientation=”Vertical” Margin=”5px”>

<StackPanel x:Name=”spSearchTerm” Orientation=”Horizontal” Margin=”5px”>

<TextBox x:Name=”tbSearchTerm” MinWidth=”95px”/>

<Button x:Name=”bSearch” Content=”Search” Click=”SearchForNews”/>

</StackPanel>

<Separator Margin=”5px”/>

<StackPanel x:Name=”spSearchResult” Orientation=”Vertical” Margin=”5px”>

<TextBlock x:Name=”tbSearchResult”/>

</StackPanel>

</StackPanel>

</Grid>

</Window>

  1. Open MainWindow.xaml.cs file and copy the code provided below into it:

using System;

using System.IO;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Text.RegularExpressions;

using System.Windows;

using System.Windows.Controls;

using System.Windows.Data;

using System.Windows.Documents;

using System.Windows.Input;

using System.Windows.Media;

using System.Windows.Media.Imaging;

using System.Windows.Navigation;

using System.Windows.Shapes;

using System.Threading.Tasks;

using System.Threading;

namespace ParallelPattern

{

/// <summary>

/// Interaction logic for MainWindow.xaml

/// </summary>

public partial class MainWindow : Window

{

public MainWindow()

{

InitializeComponent();

}

private void SearchForNews(object sender, RoutedEventArgs e)

{

string sDir = @”C:\Users\akpandey\Desktop\NewsData”;

string sSearch = tbSearchTerm.Text;

//string sNewsItems = ParallelDirSearch(sDir, sSearch);

string sNewsItems = DirSearch(sDir, sSearch);

tbSearchResult.Text = sNewsItems;

}

private string DirSearch(string sDir, string sSearch)

{

StringBuilder sbFileList = new StringBuilder();

string[] directories = Directory.GetDirectories(sDir);

DateTime dtBegin = DateTime.Now;

Parallel.ForEach(directories, currentDirectory =>

{

string[] files = Directory.GetFiles(currentDirectory, “*.*”);

Parallel.ForEach (files, currentFile =>

{

StreamReader srFileText = new StreamReader(currentFile);

string sFileRead = srFileText.ReadToEnd();

srFileText.Close();

string sMatchExpression = sSearch;

if (Regex.IsMatch(sFileRead, sMatchExpression))

{

sbFileList.Append(System.IO.Path.GetFileName(currentFile) + Environment.NewLine);

}

});

DirSearch(currentDirectory, sSearch);

});

DateTime dtEnd = DateTime.Now;

TimeSpan tsTotalTime = dtEnd.Subtract(dtBegin);

sbFileList.Append(Environment.NewLine);

sbFileList.Append(tsTotalTime);

string sResult = sbFileList.ToString();

return sResult;

}

public string ParallelDirSearch(string sDir, string sSearch)

{

StringBuilder sbFileList = new StringBuilder();

string[] directories = Directory.GetDirectories(sDir);

DateTime dtBegin = DateTime.Now;

ParallelForEach<string>(directories, currentDirectory =>

{

string[] files = Directory.GetFiles(currentDirectory, “*.*”);

ParallelForEach<string>(files, currentFile =>

{

StreamReader srFileText = new StreamReader(currentFile);

string sFileRead = srFileText.ReadToEnd();

srFileText.Close();

string sMatchExpression = sSearch;

if (Regex.IsMatch(sFileRead, sMatchExpression))

{

sbFileList.Append(System.IO.Path.GetFileName(currentFile) + Environment.NewLine);

}

});

ParallelDirSearch(currentDirectory, sSearch);

});

DateTime dtEnd = DateTime.Now;

TimeSpan tsTotalTime = dtEnd.Subtract(dtBegin);

sbFileList.Append(Environment.NewLine);

sbFileList.Append(tsTotalTime);

string sResult = sbFileList.ToString();

return sResult;

}

public static void ParallelForEach<T>(IEnumerable<T> source, Action<T> body)

{

int iProcessors = Environment.ProcessorCount;

int remainingTasks = iProcessors;

using (var v = source.GetEnumerator())

{

using (ManualResetEvent mre = new ManualResetEvent(false))

{

for (int p = 0; p < iProcessors; p++)

{

ThreadPool.QueueUserWorkItem(delegate

{

while (true)

{

T nextItem;

lock (v)

{

if (!v.MoveNext()) break;

nextItem = v.Current;

}

body(nextItem);

}

if (Interlocked.Decrement(ref remainingTasks) == 0)

{

mre.Set();

}

});

}

mre.WaitOne();

}

}

}

}

}

  1. Create a folder named NewsData on your desktop or anywhere you prefer. Also, you can name your folder anything you like if you do not like the name NewsData.
  2. Create a few subfolders in the NewsData folder. Place some text files in those subfolders. If you like, you can grab some news items from an RSS feed to put in the text files.
  3. Go to the SearchForNews method inside MainWindow.xaml.cs file. Locate sDir string variable and replace its value with the actual path to the NewsData folder on your machine.
  4. Build your solution.

You should get the message ‘Build succeeded’. Now run the application by hitting F5 or Debug > Start Debugging from the debug menu. Your application should run and you should see the main window with a search box in it. Go ahead and click the Search button. The application will search all the folders, subfolders and the files at the specified root folder for the search term which is an empty string in this case and return the result below the horizontal line. Your window should look something like this:

In my NewsData folder there are three subfolders Africa, America and Asia.

-          Africa folder contains two files Safrica.txt and Zimbabwe.txt.

-          America folder contains two files Hiroshima.txt and Mexico.txt

-          Asia folder contains two files China.txt and Skorea.txt

So, application ran and found empty string in all those files and returned the list of all those files. You can narrow the search by typing a text string in the search box. Keep in mind the search is case sensitive.

We will cover four diagnostic windows in this blog and they are Threads, Call Stack, Parallel Tasks and Parallel Stacks. To open these windows, go to the debug menu and follow the steps described below.

To open Threads window:

Debug > Windows > Threads or Ctrl+D, T

To open Call Stack window:

Debug > Windows > Call Stack or Ctrl+D, C

To open Parallel Tasks window:

Debug > Windows > Parallel Tasks or Ctrl+D, K

To open Parallel Stacks window:

Debug > Windows > Parallel Stacks or Ctrl+D, S

After opening all four diagnostic windows, your VS 2010 IDE should look something like the figure below:

You noticed that all diagnostic windows at the bottom of the VS 2010 IDE are blank. It is because all the threads related to this application completed their work and none of them are active.

Stop debugging. Go to DirSearch method in the MainWindow.xaml.cs file and locate following lines of code:

Parallel.ForEach(directories, currentDirectory =>

{

It should be in line 48 through 49. Place a break point at curly braces i.e. line 49. Start debuggin your application by hitting F5. Click on the search button once the main window is up. As soon as you click the search button, all four diagnostic windows should get populated. Now, your VS 2010 IDE should look something like the figure below:

At this point we can take a good look at these diagnostic windows one by one.

Threads window has following named columns: ID, Managed ID, Category, Name, Location and Priority. These are the default columns. You can expand this window for a better look if you like. You can add more columns by clicking on ‘Columns’ in the thread window’s toolbar. You should have one main thread and few worker threads visible in the thread diagnostic window. Notice a green square next to ‘Main Thread’ in the ‘Category’ column and a yellow arrow in one of the two unnamed columns. The yellow arrow in the thread window means a selected thread. If you double click on a different thread then the yellow arrow will move to that thread. Also, you notice that the contents in Call Stack, Parallel Tasks and Parallel Stacks windows change as well. The left most column lets you flag threads. Why would you want to flag threads? Well, it is because, on a large application, you will encounter quite a few threads and you would probably want to focus on a few threads of interest to you to avoid the clutter. To test the flag feature, go to the toolbar in the Parallel Stacks window and click on Show Only Flagged icon, you will notice that all the contents in the Parallel Stacks window disappeared. It is because we have no threads flagged in the thread window. Now go ahead and flag a few threads, you will notice that the stack frames for the flagged threads appear in the Parallel Stacks window instantly. You can toggle the Show Only Flagged mode.

Call Stack: Before we discus Call Stack window, it will be a good idea to briefly define what a call stack is. Call stack is a stack data structure which stores information about an active method. The main purpose of a call stack is to store the return address of a caller method. Once the callee finishes execution, it passes the control to the caller at the return address. Call stack also stores the parameters that need to be passed between caller and callee. There is usually one call stack associated with each thread or task. A call stack is made-up of stack frames. Stack frames contain information about a method’s state. Each call stack corresponds to a call to a method which has been called but not returned yet. This should provide us with enough background information on call stack. Make sure that the Main Thread is selected in the thread diagnostic window (Threads). Go ahead and expand the Call Stack diagnostic window for a better view. As you noticed, Call Stack has two columns – Name and Language and it has three records. Each record represents a stack frame. You should have something like this in the name column of the first row =>                ParallelPattern.exe!ParallelPattern.MainWindow.DirSearch.AnonymousMethod__0(string currentDirectory = “C:\\Users\\akpandey\\Desktop\\NewsData\\Africa”) Line 49. Note that the parameters are stored in the call stack. The yellow arrow indicates the active stack frame of the current thread.

Parallel Tasks: In .Net 4.0, tasks are the preferred API for writing parallel applications because they allow more scalable use of system resources and provide for more programmatic control than a thread. The Task Parallel Library, as its name implies, is based on the concept of tasks. The term task parallelism indicates one or more independent tasks running concurrently. A task represents an asynchronous operation and in many ways resembles a work item of a Thread or ThreadPool. However, a task is an abstraction at much higher level. Parallel Tasks diagnostic window allows us to view all the tasks running in parallel. You can use the flag column to filter the call stacks of the task in the Parallel Stacks window. To try it yourself, go to Parallel Stacks window and switch to Tasks view. The default view is Threads. Then, click on Show Only Flagged icon. You should see following message: Parallel Tasks window has no flagged items with call stacks. Now flag a task in the Parallel Tasks window, you will see its call stack instantly appear in the Parallel Stacks window. The yellow arrow indicates the current task. You can change the current task by double clicking on a task. When you double click a task, the yellow arrow moves from the previous position to current position but a white arrow appears in the previous position denoting the original task where the break took place. Parallel Tasks window has following default columns ID, Status, Location, Task, Thread Assignment and AppDomain. You can add or remove columns by right clicking any of the columns. Also, you can sort the record by clicking on any of the headers. Go ahead and right click on any of the headers and add a new column called Parent. This column indicates if the task has a parent. In our example, Task-2 is a parent of Task-1 and Task-3. That is why you see ‘2’ in the parent columns of Task-1 and Task-3. Another important column is Status. As the name implies, it shows the status of tasks. In our application, we have at the moment two running and one scheduled tasks. You can freeze a thread by taking following steps: select a task > right click > Freeze Assigned Thread. You can thaw a thread by selecting Thaw Assigned Thread.

Parallel Stacks is a very useful window for it has two view modes – Threads and Tasks. You can change the mode by selecting the desired mode from the drop down menu in the toolbar. Let us look at the Threads mode first. This is the default mode but go ahead and make sure that you are in the right mode by selecting ‘Threads’ from the drop down. You can expand the Parallel Stacks for a better view. You will notice that there are three boxes. They are labeled 5 Threads, 1 Thread and 1 Thread (number of boxes and its contents may vary on your computer because we are relying on .Net’s parallelization algorithm). You will notice that the two of the boxes have bolded blue borders. This means that both boxes contain the current thread. You can find this out by hovering over headers of both boxes. Hovering will display the list of threads; the current thread in the list is bolded. The call flow is depicted from bottom to up. You can change the call flow from options menu if you like. I will stick with the default flow for the time being. I had unpleasant experience trying to post the picture in my blog. So, I am going to stay away from the pictures. But I created an ad-hoc diagram below to show the call flow of one thread from another. I hope it is not too horrible.

External Code && Main Thread =>

{(Main Thread && DirSearch.AnonymousMethod_X)

(Worker Thread && DirSearch.AnonymousMethod_X)}

What the above diagram depicts in a nutshell is that the framework creates two instances of the anonymous method from DirSearch method. The first instance is seeded inside the Main Thread. The second instance is seeded inside a newly created Worker Thread.

You can switch the Parallel Stacks’ mode from Threads to Tasks. This will let you see the call stack for the Tasks. Just like Threads, hovering over the header of the box will let you view all the tasks that are in the box. You can view the stack frames by hovering over the call stack. In this sense, Tasks feel a lot like Threads.

Now, let us step into our code. As you step through the code, pay attention to all four diagnostic windows. You will notice that the yellow arrow in the Threads window is moving to keep up with the current thread and Call Stack window is updating stack frames as we step. By the time debugger hits line 53, we notice several changes in the Parallel Tasks window:

-          Task-1 completed and is no longer visible in the Parallel Tasks

-          Task-2 and Task-3 went to Waiting status

-          And, we have two new tasks – Task-5 (Running) and Task-6 (Scheduled)

Keep stepping into the code and look at the flow of threads and tasks in the Parallel Stacks window. You will see threads going to sleep, waiting and joining. Also in the Parallel Stacks window, if you hover over call stack (either in Threads or Tasks mode), you will notice that the framework has assigned names to different instances of the anonymous method, e.g. AnonymousMethod_0, AnonymousMethod_1, etc. After numerous F11 key strokes, eventually the method returns and once that happens, all the windows except ‘Threads’ go blank. Because, we have no running tasks left.

I think this should be good enough an introduction to the topic of Parallel Diagnostic Tools. Now, it is time to dive into development of some parallel applications using Task Parallel Library (implemented as Task API) by Microsoft.

August 9, 2010

Parallel Programming in .Net

In the world of software development, parallel programming refers to a technique, which takes advantage of multiple processors on a computer. Basically, a work load is divided and distributed to each available logical processor for its completion. This of course speeds up the task.

Parallel programming has been around for a while but only confined to specialized computers because developing applications which target multiple CPUs has been a complex process. So for the most part, at least in the world of personal computers the performance of CPUs has been enhanced by frequency scaling as opposed to parallel scaling.

Let us look at a simple formula which shows us the relation between frequency scaling and performance of a processor:

R = (I/P) (C/I) (S/C)

where R = Runtime, I = Instructions, P = Program, C = Cycles and S = Seconds.

The last factor in the equation above is S/C, which is seconds divided by cycles. This is an inverse of frequency. Thus increase in frequency i.e. increase in cycles will reduce the overall value of the multiple (Runtime).

In the PC world, frequency scaling had been the preferred way of improving a processor’s performance so far, because of its associated simplicity. However, increasing the frequency also increases the power consumption rate. This caps the frequency scaling in the cost sensitive commercial world thus creating a need for parallel scaling.

Let us look at the formula which shows the effect of frequency scaling on the power consumption of a CPU:

P = Cp (VxV) (C/S)

where P = Power, Cp = Capacitance switched per cycle, V = Voltage, C = Cycles and S = Seconds. As the equation shows, an increase in the cycle count will result in the increase in power consumption of the processor.

Once we have realized that frequency scaling has its limit, we cannot help but turn to parallel scaling for faster computing. Hardware manufacturers have started producing personal computers with multiple cores for some time now, albeit software world has not caught up with it. Applications which target multiple logical processors are still rare. The reason is associated complexity in developing such applications.

Microsoft with the release of 4.0 framework has added Parallel class in the FCL which makes our lives much easier when writing applications which take advantage of what is called parallel pattern. This class resides in System.Threading.Tasks namespace. Parallel class is part of what Microsoft calls Task Parallel Library. Some other new technologies which should be of interest to a parallel programmer are Parallel LINQ, Parallel Diagnostic Tools, Data Structures for Parallel Programming, Custom Partitioners for PLINQ, Task Factories, Task Schedulers and much more.

We will get our blog on parallel programming going with the discussion of Parallel class. However if time permits, in the future, we will try to talk about some other equally impressive areas. Parallel class provides us with three methods, which are For, ForEach and Invoke. For and ForEach play important role in Data Parallelism while Invoke is crucial for implementing Task Parallelism. Both Data Parallelism and Task Parallelism are part of bigger picture – Task Parallel Library. There is a variety of overload provided for both For and ForEach methods. You can view the complete list of overloads here:

http://msdn.microsoft.com/en-us/library/dd992105.aspx

As you observed by looking at the overload list, the most basic of the ForEach method has this signature:

public static ParallelLoopResult ForEach<TSource>(

IEnumerable<TSource> source,

Action<TSource> body

)

It has a return type of ParallelLoopResult, which provides the completion status on the execution of Parallel loop. The first parameter is a collection, which represents a source. The second parameter is an Action delegate, which accepts a method with no return as an argument. So, when we call a Parallel.ForEach loop, we pass it the source (on which we want to perform the iteration) and the logic of iteration in the form of Lambda Expression.

Here is the syntax for Parallel.ForEach loop:

private string DirSearch(string sDir, string sSearch)

{

StringBuilder sbFileList = new StringBuilder();

string[] directories = Directory.GetDirectories(sDir);

Parallel.ForEach(directories, currentDirectory =>

{

string[] files = Directory.GetFiles(currentDirectory, “*.*”);

Parallel.ForEach (files, currentFile =>

{

StreamReader srFileText = new StreamReader(currentFile);

string sFileRead = srFileText.ReadToEnd();

srFileText.Close();

string sMatchExpression = sSearch;

if (Regex.IsMatch(sFileRead, sMatchExpression))

{

sbFileList.Append(System.IO.Path.GetFileName(currentFile) + Environment.NewLine);

}

});

DirSearch(currentDirectory, sSearch);

});

string sResult = sbFileList.ToString();

return sResult;

}

This is a very simple code. Once the root (sDir) and the search string (sSearch) are provided, the code iterates through the folders, sub folders and files to see if one of the files contains the string we are looking for. If it finds the file containing the search string, it adds the name of the file to a StringBuilder. You can add time stamps at the beginning and the end of the loop to calculate the time it takes to run the Paralle.ForEach loop in order to compare the performance with an equivalent sequential loop.

That was quite simple, indeed. We can implement Parallel.For loop in similar fashion. At this stage however, it is important to take a moment and think about what is happening in the background i.e. the work done by .Net parallelization support. This blog is not about the parallel pattern so we will not dive deep into this topic but I would mention that Parallel class takes into account parallel pattern and through a very sophisticated algorithm, picks up a suitable pattern for your specific situation. Parallelization in .Net 4.0 encapsulates Embarrassingly Parallel, Fork/Join, Producer/Consumer, Aggregations, Dependencies, Speculative Processing and Shared State parallel patterns among others. Once the task is initiated, the framework handles the creation of thread pools and their assignment to each logical processor. To understand some of the challenges involved in parallel programming, we will create our own method which implements the parallelization without using .Net’s new Parallel class.

At minimum, a parallelization effort requires following:

  1. Determine the number of logical processors on a machine
  2. Create a Thread or ThreadPool (recommended) for each logical processor to execute
  3. Partition the data and channel them into individual tasks
  4. Assign each task to a Thread or ThreadPool
  5. At the end, consolidate and return the composite to the caller

The scheme for data partitioning requires some decision making which means we will have to select between static and dynamic data partitioning. If we pick static data partitioning then we do not have to worry about synchronization between Threads. However, the downside is that if a particular Thread gets stuck with an unexpectedly long work then other Threads cannot come to its rescue. Dynamic data partitioning on the other hand dynamically assigns work to a Thread after each Thread completes an assignment. Obviously, this requires synchronization between Threads which translates into an overhead. There is a happy medium between static and dynamic data partitioning but for our example we will go with dynamic data partitioning, which will give us a balanced load across all logical processors, although not without an overhead especially if tasks are not time consuming.

We will write a method which will run the foreach query in parallel but to do this we will not use the Parallel class. We will implement our own method. So, let us look at the signature of the ForEach method again because the signature of our custom method will be similar. We will call our method ParallelForEach.

public static void ParallelForEach<T>(IEnumerable<T> source, Action<T> body)

{

int iProcessors = Environment.ProcessorCount;

int remainingTasks = iProcessors;

using (var v = source.GetEnumerator())

{

using (ManualResetEvent mre = new ManualResetEvent(false))

{

for (int p = 0; p < iProcessors; p++)

{

ThreadPool.QueueUserWorkItem(delegate

{

while (true)

{

T nextItem;

lock (v)

{

if (!v.MoveNext()) break;

nextItem = v.Current;

}

body(nextItem);

}

if (Interlocked.Decrement(ref remainingTasks) == 0)

{

mre.Set();

}

});

}

mre.WaitOne();

}

}

}

The signature of our custom method is similar to that of .Net’s ForEach method. Let us go over our method. It counts the number of processors to determine the number of tasks (i.e. work and not to be confused with .Net’s Task object). We are using ManualResetEvent class to notify waiting threads to continue after the current thread is done with its work. To avoid the overhead which comes with the individual thread creation and disposal, we are creating ThreadPool for each processor. An anonymous method is passed to the ThreadPool for its execution.

Next, we will modify our DirSearch method and rename it ParallelDirSearch. This method will call our custom method that we created instead of .Net’s ForEach method to implement parallelization. The code for modified (and renamed) DirSearch method is below.

public string ParallelDirSearch(string sDir, string sSearch)

{

StringBuilder sbFileList = new StringBuilder();

string[] directories = Directory.GetDirectories(sDir);

ParallelForEach(directories, currentDirectory =>

{

string[] files = Directory.GetFiles(currentDirectory, “*.*”);

ParallelForEach(files, currentFile =>

{

StreamReader srFileText = new StreamReader(currentFile);

string sFileRead = srFileText.ReadToEnd();

srFileText.Close();

string sMatchExpression = sSearch;

if (Regex.IsMatch(sFileRead, sMatchExpression))

{

sbFileList.Append(System.IO.Path.GetFileName(currentFile) + Environment.NewLine);

}

});

ParallelDirSearch(currentDirectory, sSearch);

});

string sResult = sbFileList.ToString();

return sResult;

}

When I implement parallelization using .Net’s ForEach method, the search completes in .0123 seconds. When I use my custom parallelization it takes about 1.0074 seconds to complete the search. The reason behind this difference is the sophisticated algorithm provided by parallel support in new .Net framework. This algorithm handles the task of choosing right data partitioning scheme and other complex parallelization tasks on your behalf. That is why there are so many overloads for For and ForEach methods. Even though the algorithm handles the parallelization for us, it is a good idea to understand the parallel patterns and what those overloads are for. I would highly recommend you get hold of an e-book called Patterns of Parallel Programming by Stephen Toub. This was published by Microsoft and I believe is free. You can download it from here: http://www.microsoft.com/downloads/details.aspx?FamilyID=86b3d32b-ad26-4bb8-a3ae-c1637026c3ee&displaylang=en.

I would like to keep our focus on parallelization support as provided within .Net 4.0. However, before we dive further into Task Parallel Library, let us explore Parallel Diagnostic Tools a bit. After all, in order to develop a parallel application, we have to be able to debug a parallel application.

Since, this blog has become too long, I will start a new blog and we will call it – Parallel Diagnostic Tools. So, let us regroup there.

June 16, 2009

Model View Controller

Filed under: Software Development — arunscottsdale @ 2:41 am
Tags: , , , , , , , ,

Recently, I had pleasure of working on projects that utilized Model View Controller design pattern. I wanted to share some of these experiences with you. Microsoft has released a new framework called MVC framework. This framework facilitates development of software that utilize Model View Controller design pattern.

If you would like an in-depth understanding of MVC framework then head to Scott Guthrie’s blog. He has written some great blogs on MVC framework. Stephan Walther is working on his new book called ASP.Net MVC Framework Unleashed. You can get sample of this upcoming book on Stephan Walther’s blog. Just google it.

To start playing around with MVC Framework, you will need following:

-         VS 2008 (express edition is OK)

-         MVC Framework

You can download VS 2008 Express from this location:

http://www.microsoft.com/express/download/

You can download MVC Framework from this location:

http://www.asp.net/mvc/

Now, you should be ready to get started with MVC.

Before we dive in, let me give you a very simple introduction to MVC. Model View Controller is a design pattern, which has been around for a while. It is one of the most popular design patterns for UI centric applications. So, finally Microsoft decided to release a framework, which speeds up the task of developing MVC applications.

Model View Controller design pattern consists of three layers – Model, View and Controller. Model is responsible for data representation. Controller is responsible for calling appropriate Views. And of course, View is responsible for displaying data. There is a complete separation of concern between View, Controller and Model. View contains no logic what so ever. So, essentially one View can be replaced with another View without any changes to Controllers or the Model.

This has some great advantages. For example, let us assume that we just finished developing a web application designed to be consumed by personal computers. Then the company decides to target group of users who use handheld devices. Under MVC design pattern, all we will need to do is design new sets of Views for handheld devices. Our Controllers and Model will remain the same. We have heard this kind of promises before under N-Tier design patterns but those promises were not entirely accurate. MVC does deliver on its promise of complete separation of concern.

Once you have VS 2008 Express and MVC Framework downloaded, we can get started on our MVC exercise. Please go to this URL and create a sample MVC Application called Movie Database:

http://www.asp.net/learn/mvc/tutorial-21-cs.aspx

This is a great tutorial designed by Stephan Walther (if my information is correct). Using this tutorial, you can create a full flag CRUD application within fifteen minutes. Of course, in real life our challenges will be much more complex and this tutorial is nowhere near there. However this tutorial does demonstrate following fundamental concepts:

  1. How to build a Model using Visual Studio’s built in ORM
  2. How to create Controllers
  3. How to create strongly typed and weakly typed Views

Once you build this sample application successfully, you gain basic understanding of MVC Framework. The movie database application uses LINQ to Entity ORM. However, I would like to caution you against using LINQ to Entity when developing real life application down the road. My suggestion would be to use LINQ to SQL. I will give you my reason later.

03/16/2010

When I initially wrote this blog, I had every intention of frequently adding more content to it. But I got busy with some projects and never got around to it. So, let us wrap this blog up with few more stuff because soon I am going to write a blog about my recent experience with Model View ViewModel (MVVM) design patter. I used this design pattern to architect a recent software development for a cosmetic spa.

Anyhow coming back to the MVC framework, no tutorial on MVC framework would be complete without mentioning JQuery. JQuery is light weigh JavaScript library, which allows developers to do client side programming with relative speed. As you have guessed it JQuery is packed with some useful routines, which can be used to write your day-to-day AJAX codes.  Microsoft provides full support for JQuery including intellisence support.

So, without wasting any more time, I will give you the link below so that you can get started on JQuery. Once you start writing JQuery codes, you will not want to use third party AJAX controls, it is a promise.

http://jquery.com/

Also, the new version of LINQ to Entity seems to have no issue. So, you should be able to use LINQ to Entity framework with your MVC project. Good luck with your MVC project. Please let me know if you have any questions. Thanks!

May 29, 2009

Oregon – A Friendly State

Filed under: Travel — arunscottsdale @ 2:35 am
Tags: , ,

Well, it has been about one month since I undertook this project in beautiful Oregon. I live in a hotel in Gresham and work in downtown Portland. Just like any other project and any other new city, I always complain, lord what I have gotten myself into. When I first moved to Oregon, it was raining cats and dogs, non-stop. Having lived in Arizona and Nevada for a considerable period of time (except for a brief stint in Oklahoma), it was a culture shock to see so much water.

Well, God of Rain had mercy upon my soul and the rain stopped after first couple of weeks. I had one less problem in my stack to worry about.

Let me give you a brief overview of the scenario that led to my arrival in Oregon. One of the departments (at my client’s location) wanted to build a tool that would help them with their marketing effort. The department approached the development team (client’s internal development team) and asked them to develop the said tool. But the development team was unable to get started on it. The department waited for about one year and after that they decided to bring in external software consultants. That is how I came in to the picture.

*

After having settled down, I started to note a few things about Portland and Oregon in general. People here are very environment friendly. As a result, Portland is the only city in USA with an excellent public transportation system. My co-workers told me about this wonderful train line called MAX, which runs from Gresham to Hillsboro going through downtown Portland. I resisted it for first couple of weeks but then I decided to give it a try. And, I loved it. It is very relaxing to get on it at the end of the day and head towards my hotel/home. If you ever visit Portland then please remember to ride MAX. It is fun.

Another thing I noticed about Portland is that every other girl wears a nose ring. People’s complexion is ultra pasty, which is reasonable considering; it rains nine out of twelve months. And, you cannot separate raccoon cubs from their mother. If they are in your backyard and you want them out then you must relocate entire family without harming them. I think it is a reasonable approach. Animals are people too.

**

Well, coming back to my MAX encounter in Portland; MAX arrived loaded fully at the station when I got there after work. I wanted to skip this train and catch the next one heading towards home. But I decided to get in. People were standing everywhere. As the train took off, I got lost in my thoughts. I realized that our lives have become very compartmentalized in a sense that we have very little interaction with anything but our cars and cubicles/offices.

MAX has a relaxing effect on my stress level. So, is it the train-ride itself or the interaction with people on board, which has relaxing effect on me? I think latter is true. The project seems to be coming under control after turbulent initial start. That too adds to my joy.

I recall sitting next to a young couple few days ago on the MAX. I noticed a bug on the shoulder of the young man. They too noticed it. I thought they were going to swat it just like most people do. Instead, the girl pulled out a newspaper from her bag and carefully got the bug to climb on the newspaper then she gently folded the newspaper to create a little fence for the bug and, then she took the bug out when the train stopped at the next station. Very compassionate couple indeed.

People in Oregon generally strike me as a group of compassionate people. I truly had a great experience living in the Oregon so far.

***

No blog on Oregon will be complete without mentioning great Indian restaurants of Portland and its suburbs. When I was in Oklahoma City, I ate at few Indian restaurants and I said to myself – boy, is that Indian food? They were awful. It is needless to say that those restaurants gave me a complex. But all of that was cured eating at Indian restaurants here in Portland. They are good and authentic. If you are in Portland, head to any Indian restaurants you can find. They are all good.

I think my Oregon blog is becoming way too big. So, I will keep it dormant till something really exciting happens.

Next, when I get some time at hand, I would like to post a blog about my recent experiences dealing with Model View Controller design pattern.

Theme: Rubric. Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.