Programming Multi-core | Part 1

Until now, still very hard to write, test and debug parallel algorithms that actually take advantage of multi-core processors. Most applications used today are just a single core by design and see no speed improvements when run on a multi-core machine. I prefer to name this problem as "performance-oriented threading problem on multi-core" but term like "concurrency" is much simpler.

Concurrency is new for many developers, but there is a long history of threading problem in scientific and high-performance computing that partially solved by many approaches and can be leveraged, at least theoretically. Threading oriented API like OpenMP (Shared Memory) has been around, also MPICH (Message Passing). In this tutorial, I would like to take you in my journey with TBB and Microsoft Parallel Extension (Native and Managed), both of them adopt task parallelism. Let me start with Intel Threading Building Blocks (TBB) then move to Paralell Extension.

TBB has two versions, commercial and open source but as developer I prefer to download TBB source codes and compile it on my own machine. Ok, lets start.

Preparation:

1 - Download TBB 2.0 source codes here. Extract it to your working directory.
2 - Download TBB documentation, Getting Started Guide, Reference Manual and Tutorial.
3 - Install Visual C++ 2005/2008. For Express Edition, you will need MASM 8.0.   
4 - Set environment variables by run your vsvars32.bat. It is located at for VS 2008 :
     C:\Program Files\Microsoft Visual Studio 9.0\Common7\Tools
5 - TBB make file needs GNU Make Utility. I am using MinGW (mingw32-make.exe).
     Copy this file to TBB working directory where make file is located.
6 - You are ready to compile, please use mingw32-make [option] command, for example:

PLINQ01

mingw32-make : Default build. Equivalent to 'make tbb tbbmalloc'.
mingw32-make all  : Equivalent to 'make tbb tbbmalloc test examples'. Compile all codes.
mingw32-make tbb  : Make TBB release and debug libraries.
mingw32-make tbbmalloc  : Make TBB scalable memory allocator libraries.
mingw32-make test : Compile and run unit-tests
mingw32-make examples  : Build libraries and run all examples
mingw32-make clean : Clean all compiled binaries.

Note : Please consult to TBB/build/index.html document for compilation other than using CL.EXE.


I compiled TBB 2.0 in my laptop around 1 minute and I got tbb.dll and tbbmalloc.dll including all lib needed. Things that you need to check is TBB header files in includes directory and object file library (*.lib). If you want to have quick review of TBB 2.0 facilities, take a look on each header files name (and of course TBB Tutorial).

PLINQ02 

Your First TBB Program:

Now lets start to use TBB.
1 - Create an empty VC++ project.
2 - Manipulate your project property.
     Set VC++ configuration property.
     Linker > Additional Library Directories = path to the TBB binaries
     C/C++ > General > Additional Include Directories = path to TBB include directory
3 - Write your first code as following (get the codes from TBB Getting Started guide):

PLINQ03


Any thread that will use TBB has to initialize task_scheduler_init object. It has default constructor and destructor that will manage the lifecycle of task. To add a call to the parallel_for template function, the first parameter of the call is a blocked_range object that describes the iteration space. Blocked_range is a template class provided by the TBB library. The constructor takes three parameters:

⎯ The lower bound of the range.
⎯ The upper bound of the range.
⎯ The <grainsize>.

Modify your pre-processor code by adding the following:

#include <iostream>
#include <string>
#include <algorithm>
#include "tbb/task_scheduler_init.h"
#include "tbb/parallel_for.h"
#include "tbb/blocked_range.h"

And then add the parallel_for to your for looping.

PLINQ04.jog

The parallel_for subdivides the range into sub-ranges that have approximately <grainsize> elements. You can get the implementation of SubStringFinder class in TBB Getting Started guide. Ok, now you are ready to use Intel TBB other features to write your algorithm and I am sure you can do that. I will move to Microsoft initiative on "concurrency".


Parallel Extension is Microsoft Implementation to handle concurrency of managed and native codes in Windows OS. I have installed Parallel Extension for .NET Framework 3.5 Dec 2007 CTP since it first release. Its default installation directory is C:\Program Files\Microsoft Parallel Extensions Dec07 CTP where you can find one managed DLL (System.Threading.dll) there. System.Threading contains six namespaces around with native dependencies on Kernel32.dll.

PLINQ01

Parallel Extensions to the .NET Framework has broader scope comparing to TBB. It is a managed programming model for data parallelism, task parallelism, and coordination on parallel hardware unified by a common work scheduler. Parallel Extensions makes it easier for developers to write programs that scale to take advantage of parallel hardware without having to deal with many of the complexities of today’s concurrent programming models. Parallel Extensions provide several new ways to express parallelism in our codes:

- Declarative data  : LINQ-to-Objects that executes queries in parallel (PLINQ).
- Imperative data   : Imperative data-oriented operations such as for and foreach loops.
- Imperative task   : Express potential parallelism via expressions and statements

Hopefully I can cover those three approaches and compare it with TBB in my next post. If you want to follow me in this concurrency journey, please install the TBB and Parallel Extension directly to your OS (don't use VPC or VMWare) and start read its documentation. I have around ~4GB Channel9 videos on Parallelism that is very useful to inspire you. If you want to have the videos, just drop your address as comment in this post, I will ship its DVD for you.

 

Hope this helps!


Regards,


Risman Adnan

Share this post: | | | |
Published Saturday, May 17, 2008 6:56 PM by Risman Adnan Mattotorang

Comments

# re: Programming Multi-core | Part 1

We have posted a link to your article on www.multicoreinfo.com , Programming section. Please let us know when you write other parts of your "Programming Multicore". We would be glad to provide links.

Best wishes.

Saturday, May 17, 2008 8:30 PM by Suren

# re: Programming Multi-core | Part 1

@Suren

This tutorials will take around 5 parts. I will let you know the progress to www.multicoreinfo.com.

Thx

Sunday, May 18, 2008 7:17 AM by Risman Adnan Mattotorang

# Pages tagged "concurrent"

Pingback from  Pages tagged "concurrent"

Sunday, May 18, 2008 11:17 AM by Pages tagged "concurrent"

# re: Programming Multi-core | Part 1

Pak risman,

saya tertarik dengan Prallelism,

minta videonya ya :)

Muhammad Hakim A

Jl. Sarimanis Blok. 13 No.53

Bandung 40151

Sunday, May 18, 2008 2:52 PM by hakimrie

# Programming Multi-core

I have decided to start writing about multi-core for all developers and ISVs in Indonesia. First release

Monday, May 19, 2008 8:45 AM by BinaISV Weblog

# handle exe parameters

Pingback from  handle exe parameters

Tuesday, May 20, 2008 10:11 PM by handle exe parameters

# re: Programming Multi-core | Part 1

Hey

Your tutorial is great.

And I am wondering what IDE you are using. I mean in the picture(geeks.netindonesia.net/.../PLINQ03_thumb.jpg) I like this IDE, is it Visual C++ 2005/2008? How can I make the UI as beautiful as yours?

Thank you.

Saturday, September 27, 2008 9:46 PM by chengpengsu_at__gmail_com