Scanners? Aren’t those the guys that make your head explode?

Back in the 80’s, there was a movie called “Scanners”. Scanners were mutants that could think intensely about you with a very constipated look and veins standing out on their faces. Then your head would explode. That’s the way the parallel_scan construct makes me feel.

There is no example for parallel_scan in the TBB tutorial, but there is a terse explanation in the reference manual. Basically, parallel_scan breaks a range into subranges and computes a partial result in each subrange in parallel. Then, the partial result for subrange k is used to update the information in subrange k+1, starting from k=0 and proceeding sequentially up to the last subrange. Then each subrange uses its updated information to compute its final result in parallel with all the other subranges. Read more of this post

Advertisements

Reductio ad absurdum

I used parallel_for in my first TBB program to multiply two vectors element-by-element and store the products into a third vector. But what about calculating their dot product where the vector products are added together? TBB supports this type of operation with the parallel_reduce construct.

In order to use parallel_reduce, I needed to add two methods to the object that performs the parallel operations:

  • a splitting constructor that can cut off a piece of an existing object and initialize it;
  • a join method that combines the answers of two smaller problems into a result that applies to their total.

I modified my vector multiplication program to compute the dot product using parallel_reduce. Here’s the result: Read more of this post

Upgrading to tbb20_017oss

I downloaded the 017 commercial aligned release of TBB. I changed my TBB20_INSTALL_DIR environment variable to C:llpanoramaTBBtbb20_017oss_src and rebuilt TBB as I did before. No problems occurred during the build, so I’m ready to go. From this point on, my TBB-related posts will refer to the 017 release.

Take a look at Kevin Farnham’s post to see what changed between versions 014 and 017. It doesn’t look significant for someone at my current level of expertise.

Let’s try this again…

In my first attempt at writing a parallel program with TBB, my speedup was disappointing (about 4%). I guessed that vector multiplication wasn’t computationally intensive enough to make good use of multiple cores. I tried to remedy that by writing a program to find points in a Julia set. The Julia set is just the set of points that don’t shoot off to infinity when they are iterated by the function:

z(n+1) = z(n) * z(n) + c

So the basic technique is to take a candidate point z and repeatedly square it and add a constant (both z and c are complex numbers). If it exceeds a bound within a set number of iterations, then z is probably not a Julia point (this is an approximate test). By varying the bound and the number of iterations, I could easily vary the computational load. Read more of this post

Making TBB a bit easier…

I don’t like having to manually set the various properties in order to get a TBB program to compile. So I built a Visual C++ 2005 project template that does it for me. All I have to do is click on the New Project button, select Win32 as the project type and Win32 Console Application using TBB as the template. That automatically sets the properties to use TBB and presents a top-level .cpp file that already contains the TBB include files.

You can download the installer for the TBB project template here.

By default, the project template expects that you have modified the PATH environment variable to include the directory where the DLLs are stored: %TBB20_INSTALL_DIR%\ia32\vc8\bin. If you want to use the method where the TBB DLLs are copied to the application directory before it is run, then you can uncomment lines 46, 51-52, 57 and 62-63 in the file default.js at C:\Program Files\Microsoft Visual Studio 8\VC\VCWizards\AppWiz\Generic\ApplicationTBB\scripts\1033.

If you want to know how to make or modify a VC++ project template, this is a good place to start.

My first TBB program!

In my previous post, I showed how to setup the software tools for creating C++ programs that use TBB. Now I’ll show how I wrote my first TBB program.

I wanted to concentrate on the mechanics of creating a TBB application with Microsoft Visual C++ and not get too bogged down in the details of the algorithm. So I wrote a simple program that just multiplies two vectors item by item and places the products into another, equal-sized vector.

I started by cranking up Visual C++ and clicked on the New Project button (you can also use File→New→Project… from the menu). In the New Project window, I selected Win32 as the project type and Win32 Console Application as the template. I gave the project the creative name of example1 and set its location to the C:\llpanorama\TBB\examples directory. After clicking OK in the New Project window, and then clicking Finish in the Win32 Application Wizard window, a window opened with a simple code skeleton. I made additions to the skeleton and arrived at the following program which I will explain below. Read more of this post

Getting started with TBB

I downloaded the TBB documentation and the Commercial Aligned Release. First, I unpacked the source archive (tbb20_014oss_src.tar.gz) into the directory C:\llpanorama\TBB. Then I unpacked the Windows binaries (tbb20_014oss_win.tar.gz) and copied its ia32 subdirectory into the top level of the source directory.

At this point, I created the environment variable TBB20_INSTALL_DIR and set it to C:\llpanorama\TBB\tbb20_014oss_src.  I needed to do this manually to get TBB working under Vista.

I downloaded and installed the free Visual C++ 2005 Express Edition to use with TBB. After installing that, I downloaded and installed the Microsoft Assembler which is also needed to build TBB. Because the Visual C++ 2005 Express Edition only supports .NET applications by default, I also needed to install the Microsoft Platform SDK so I could create Win32 applications. Don’t neglect to do the things listed in steps 3, 4 and 5 after installing the SDK! I also wanted to be able to use the VC++ compiler from the command line, so I used the paths from step 3 to modify the PATH, INCLUDE and LIB variables in the C:\Program Files\Microsoft Visual Studio 8\Common7\Tools\vsvars32.bat file as shown below. (I know you can’t see the entire line, but you can select-copy-paste them into your own vsvars32.bat file and you should get all of it.)

@set PATH=C:\Program Files\Microsoft Visual Studio 8\Common7\IDE;C:\Program Files\Microsoft Visual Studio 8\VC\BIN;C:\Program Files\Microsoft Visual Studio 8\Common7\Tools;C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\bin;C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727;C:\Program Files\Microsoft Visual Studio 8\VC\VCPackages;C:\Program Files\Microsoft Platform SDK for Windows Server 2003 R2\Bin;%PATH%
@set INCLUDE=C:\Program Files\Microsoft Visual Studio 8\VC\INCLUDE;C:\Program Files\Microsoft Platform SDK for Windows Server 2003 R2\Include;%INCLUDE%
@set LIB=C:\Program Files\Microsoft Visual Studio 8\VC\LIB;C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\lib;C:\Program Files\Microsoft Platform SDK for Windows Server 2003 R2\Lib;%LIB%

I also installed MinGW make (version 3.80) on my system so I could run the TBB makefile.

After all that was installed and patched together, I went to the C:\llpanorama\TBB\tbb20_014oss_src directory, opened a command window and issued the commands:

C:> vsvars32.bat
C:> make

The first command initialized all the environment variables for the command-line version of the VC++ compiler. The second started the build process for the TBB libraries and then compiled and ran a suite of test programs. This completed without any errors, so I figured I’d set up everything correctly.

Next, I read the Getting Started Guide for TBB and it described how to build an example application. Rather than type all that in, I just jumped to the directory for this example at …\tbb20_014oss_src\examples\GettingStarted\sub_string_finder\vc8. Double-clicking the sub_string_finder.sln file brought up the VC++ 2005 window. I built the sub_string_finder project and ran it successfully.

So now I’m ready to begin using TBB.