Jan 6, 2009

Programming for multi-Core environments...

In day-to-day programming, we do not pay enough attention to the fact that we may not using the power of multi-core architectures optimally. We usually assign the total workload to a single thread. You might wonder that why I am blogging about the very basics of Concurrency. Yes, I am. But I am trying to show the power of concurrency in multi-core environments using some statistics that I collected for an assignment for the module "Concurrent Programming".

Sorting of an integer array using Quick Sort is the task that was used to collect the statistics. Quick Sort was implemented both sequentially and in parallel. And the elapsed time to sort the array was measured for different array sizes.

In the parallel approach, after breaking down the original array into two sub arrays based on the pivot element, two tasks are created for sorting these two sub arrays and assigned these tasks to the Java Executor Framework. (I used Java Executor Framework for managing threads, because it is not necessary to undertake the burden of creating, synchronizing, managing and terminating the threads manually.)

code

Following is the summary of the results obtained.

perThe most intersting observation is the CPU usage graph. I tested this on a dual core machine, hence I get two CPU plots representing each core.

For the sequential approach following graph is resulted.

screenshot-system-monitor_seq

For the parallel approach, following is the graph obtained.

screenshot-system-monitor-par

It is quite interesting to see both cores becomes busy at the sametime while resulting with the inproved performance.

Why use single core, leaving others idle.. ?  :-)

2 comments:

  1. Amazing post.

    I am very interested in multi-core programming using C#. However, as far as I can see Parallel Extensions are always changing... I can't trust them until Visual Studio 2010 is on the market.

    There are many books recommended in http://www.multicoreinfo.com/books/

    C# 2008 and 2005 Threaded Programming: Beginner’s Guide

    Author: Gastón C. Hillar

    395 pages, Expected January 2009

    ISBN 1847197108, ISBN 13 978-1-847197-10-8

    http://www.packtpub.com/beginners-guide-for-C-sharp-2008-and-2005-threaded-programming/book

    Concurrent Programming on Windows [Amazon Link]

    Author: Joe Duffy

    Publisher: Addison Wesley, Pages: 1,008, November 2008

    ISBN: 0-3214-3482-X

    https://www.amazon.com/dp/032143482X

    I've read Joe Duffy's book. It's a great piece of work. Highly recommended. However, It is too technichal for me. I need more real-life examples.

    I am going to buy Gastón's book, instead of worrying about future Parallel extensions that are always changing from beta to beta. I need safe multithreading now with final Visual C# 2008.
    ReplyDelete
  2. hi Tom,
    You have mentioned that you are interested in real examples. Even though it is not much advance, I have the working code for the above scenario. But it is written in Java. If you want it just send me an email to thilinamb@gmail.com, then I can send you the code.

    /thilina.
    ReplyDelete