Home » C++ » Kinect » Lambda expressions » Parallel processing » Parallel Processing and Lambda expressions in C++11 – Part 1

Parallel Processing and Lambda expressions in C++11 – Part 1

One of the exciting new features introduced with C++11 is lambda expressions. .NET has had these for a while so its nice to be able to use them in C++ too.

A common task in video processing and graphics is to perform some operation over an image. When doing this you’ll typically process the image buffer a row at a time as this speeds up processing. Microsoft have introduced the Concurrency Runtime with their C++11 compiler. This gives you the ability to process the image in a parallel fashion. If you’ve got Visual Studio 2013 and have installed the help, just do a search for “Concurrency Runtime”.

What I’d like to do is take one of the Kinect Native Samples that has some basic video processing in C++. I’ll then convert it to parallel processing using the Concurrency Runtime and some lambda functions. I’m hoping by the end to have something that will look straightforward at the high level.

The sample I’m starting with is the DepthBasics-D2D Native Sample. If you open up the DepthBasics.cpp file and go up to the top, add the following code underneath the #include “stdafx.h” to include the Concurrency Runtime:

You’ll notice I’m hiding the warning messages you’ll get when you include ppl.h. I like to keep my compilations free of warnings. If you don’t mind the warnings then don’t bother with the #pragma statements.

The bit of code we’re going to be changing is in the ProcessDepth method. Here’s a cut down version of the method so you can see the bit we’ll be changing:

This code is going through the input depth buffer (which contains 16-bit unsigned short integer values of the depth in millimetres) one value at a time. It is then converting them to an 8-bit byte value (0-255) that will correspond to the intensity. By putting the same 8-bit value into the red, green and blue channels we’ll end up with a grayscale image. If you compile and run the sample that’s what you’ll see.

Our first change is to split this processing up into rows and then to execute the same process on the rows in parallel. We can do this because it doesn’t matter in which order we process the rows. To accomplish this we’ll use the Concurrency::parallel_for method and a lambda expression for our row processing. Here’s the change:

C++11 has introduced the new auto variable type. This is similar to the var variable type in C#. Here I’m assigning my row lambda expression to the auto variable rowLambda. I’m doing this to (hopefully) make it easier to understand what’s going on. Later on (in part 2) I won’t be using an interim variable – I’ll simply put the lambda expression itself as the last parameter of the parallel_for.

One important thing to note is that the parameters for the parallel_for and the lambda function must have exactly the same type. Here I’m using UINT. Note how I cast the second parameter to a UINT because it’s actually a signed int. If you don’t do this you’ll get a compiler error that is difficult to interpret – but this is what it will be trying to tell you, in it’s own special way 🙂

In my next post – Parallel Processing and Lambda expressions in C++11 – Part 2 – I’ll complete the transformation of this code.