Intel® oneAPI Threading Building Blocks
Ask questions and share information about adding parallelism to your applications when using this threading library.
Announcements
This community is designed for sharing of public information. Please do not share Intel or third-party confidential information here.

Parallel_for Explanation

adrael
Beginner
166 Views
Hello,

I'm new to TBB and I would like to better uderstand the way parallel_for works.
I am using the example of the tutorial :
====================================================================================
#include "tbb/blocked_range.h"

class ApplyFoo {
float *const my_a;
public:
void operator()( const blocked_range& r ) const {
float *a = my_a;
for( size_t i=r.begin(); i!=r.end(); ++i )
Foo(a);
}
ApplyFoo( float a[] ) :
my_a(a)
{}
};

...............................

#include "tbb/parallel_for.h"

void ParallelApplyFoo( float a[], size_t n ) {
parallel_for(blocked_range(0,n,IdealGrainSize), ApplyFoo(a) );
}


===================================================================================

Here are my questions :

- What really does parallel_for(blocked_range(0,n,IdealGrainSize), ApplyFoo(a) ); ?

Does it create multiple AppliFoo objects, which are handled by a thread, and then operator() is applied
on each object ?
Or does it create instead one single AppliFoo object ?

- I don't see where and how the operator() function operates : where are the parentheses ?


Well, I am a bit confused on parallel_for. I hope that your explanation will help me !

Thanks.
0 Kudos
3 Replies
Andrey_Marochko
New Contributor III
166 Views
Hello, Adrael

I'm afraid I cannot answer your first question in a more understandable way than TBB Tutorial does.

Regarding your second question, this forum is for discussing and helping with questions related to TBB and parallelism in general. To get assistance with C++ basics, please refer to one of the following forums:

http://groups.google.com/group/alt.comp.lang.learn.c-c++/topics

http://www.cplusplus.com/forum/beginner/

http://www.programmersheaven.com/mb/beginnercpp/board.aspx


Alexey_K_Intel3
Employee
166 Views

Answering the couple of more specific questions (though parallel_for can be used without knowing this level of details):

Adrael:
Does it (parallel_for - A.K.) create multiple AppliFoo objects, which are handled by a thread, and then operator() is applied on each object ?
Or does it create instead one single AppliFoo object ?

In the current implementation of parallel_for, multiple objects are created (much more than the number of threads). But this is an implementation detail that might change; we could as well use a single object while calling its "function call operator" (a.k.a. operator()) from multiple threads maintained by TBB.

adrael
Beginner
166 Views
Thanks. I am investigating...
Reply