First time here? Check out the FAQ!

Ask Your Question
3

parallel_for_

asked Aug 15 '13

updated Aug 15 '13

I'm not sure, that I can understand parallel_for_... I wrote simple class:

//header
class ParallelClass: public cv::ParallelLoopBody
{
    private:

    public:
    ParallelClass();
    void operator()( const cv::Range &r )const;
};
//cpp
 ParallelClass::ParallelClass(){}
 void ParallelClass::operator()( const cv::Range &r )const{
    cout<<"start"<<endl;
  }

and in the main function:

  parallel_for_(cv::Range(0,10), ParallelClass());

I was expecting to see ten times "start",,, but looks like I'm totally wrong...

Preview: (hide)

Comments

1

Works perfectly fine for me. Have you forgot to build with tbb-support? Also note (not directly related to your problem) that you should later write sth like for (int i = r.start; i &lt; r.end; ++i){} to give each thread some more to do.

Guanta gravatar imageGuanta (Aug 15 '13)edit

3 answers

Sort by » oldest newest most voted
5

answered Aug 16 '13

updated Aug 18 '13

If you don't see ten times "start" it's because the parallel framework doesn't launch as many threads as your range. Write this instead:

void ParallelClass::operator()( const cv::Range & r ) const {
    // loop for the range
    for( int i = r.start ; i < r.end ; ++r ) {
        std::cout << "start" << std::endl;
    }
}

Therefore you will see your start written 10 times. The reason is the parallel binding used (concurency, tbb, etc.) cut your loop in smaller pieces, and you still need to execute your code for a range (argument is a range not an index), sometimes the range could be one, but it's an exception, not the norm.

Preview: (hide)

Comments

Shouldn't he have seen 10 times "start" even without the for-loop? At least it works perfectly fine for me without it, however, I guess that only one thread actually executes this part then...

Guanta gravatar imageGuanta (Aug 16 '13)edit

It depends on your hardware (and probably the binding used: OpenMP, TBB, Concurrency, c=, ...). You should print the index (r.start and r.end) to see what happens on your system.

Mathieu Barnachon gravatar imageMathieu Barnachon (Aug 17 '13)edit
1

@Mathieu, there a typo in your 'for' loop. It should be: for( int i = r.start ; i < r.end ; ++i )

Michael Burdinov gravatar imageMichael Burdinov (Aug 18 '13)edit

Thanks, I fix it!

Mathieu Barnachon gravatar imageMathieu Barnachon (Aug 18 '13)edit
3

answered Aug 18 '13

Michael Burdinov gravatar image

When I encountered parallel_for_ for the first time I posted here a question about how to use it properly. I got very good answers from @alexcv and @Vladislav Vinogradov. I know that this is not a whole answer to your question, i.e. it doesn't explain why your approach is not working, but at least it shows you what approach will work.

Preview: (hide)
0

answered Aug 19 '13

Finally I gave up on parallel_for_ and make it threads (pthread.h). ;)

Preview: (hide)

Question Tools

2 followers

Stats

Asked: Aug 15 '13

Seen: 5,529 times

Last updated: Aug 19 '13