lambda use for TBB in Parallel Studio

lambda use for TBB in Parallel Studio

Clay Breshears (Intel)'s picture

I'm using Intel Parallel Studio SP1 within Visual Studio 2008 and cannot get the compiler (v. 11.1.071 [IA-32]) to recognize the lambda expression within a TBB parallel_for. Using several different documentation resources, I've settled on the following code (for matrix multiplication):

parallel_for(blocked_range(0, m, 1),
[=](const blocked_range& r){ // use lambda facility of TBB 3.0
for (size_t i = r.begin(); i < r.end(); i++)
for (int j = 0; j < n; j++){
C[i][j]=0;
for (int k = 0; k < p; k++)
C[i][j] += A[i][k]*B[k][j];
}
}, auto_partitioner());

I keep getting a compiler error ".\\MMmult.cpp(36): error: expected an expression" pointing to the open square bracket of the lambda. I've tried without the '=' and putting the lambda in a separate declaration (as was pointed out in an earlier forum post). All with the same negative results.

Any advice on what I'm doing wrong or what might be missing?

--clay

6 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.
robert-reed (Intel)'s picture

Did you turn it on?

/Qstd=c++0x, or go to the appropriate compiler configuration page to enable the C++0x extensions.

robert-reed (Intel)'s picture

p.s., While TBB from version 2.2 forward has had support for selected parallel constructs that take advantage of lambda expressions, lambdas are really an attribute of the compiler.

p.p.s TBB 3.0 uses auto_partitioner as the default argument, so you could drop the autopartitioner argument for brevity and clarity.

p.p.p.s You might want to consider reference sematics for the arrays referenced inside the lambda expression, rather than using the copy semantics ("[=]") used in the example: I think you want to use the arrays in place, shared among the threads rather than having each threadmake their own private copy.

Clay Breshears (Intel)'s picture
Quoting Robert Reed (Intel) Did you turn it on?

/Qstd=c++0x, or go to the appropriate compiler configuration page to enable the C++0x extensions.

Aha, that was the missing piece. Thank you, Robert.

Clay Breshears (Intel)'s picture

Not being a C++ programmer and not having read anything about the syntax or semantics of lambda's, I was just copying examples from other codes.

I've used the '[&]' notation. Is this the reference semantics? The execution seem faster with the ampersand instead of the equal sign.

robert-reed (Intel)'s picture

Yes, use of & in the context specification sets the default linkage for symbols found in the lambda expression that are defined in the enclosing scope to reference semantics: the variable visible in the expression will be a reference to the actual variable rather than a copy to be used within the lambda construction. If the arrays were big enough or the computation run long enough, you should notice a performance boost by avoiding copying the arrays with each invocation. And if you actually cared about the results of your matrix multiply, you might notice using [=] thatthey're allwrong! ;-)

Login to leave a comment.