Ask Your Question
3

parallel_for_

asked 2013-08-15 03:28:51 -0600

updated 2013-08-15 03:29:47 -0600

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...

edit retag flag offensive close merge delete

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 ( 2013-08-15 03:54:19 -0600 )edit

3 answers

Sort by ยป oldest newest most voted
5

answered 2013-08-15 18:36:29 -0600

updated 2013-08-18 17:06:53 -0600

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.

edit flag offensive delete link more

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 ( 2013-08-16 03:44:52 -0600 )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 ( 2013-08-16 18:10:17 -0600 )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 ( 2013-08-18 06:34:25 -0600 )edit

Thanks, I fix it!

Mathieu Barnachon gravatar imageMathieu Barnachon ( 2013-08-18 17:09:08 -0600 )edit
3

answered 2013-08-18 06:24:51 -0600

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.

edit flag offensive delete link more
0

answered 2013-08-19 02:27:25 -0600

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

edit flag offensive delete link more

Question Tools

2 followers

Stats

Asked: 2013-08-15 03:28:51 -0600

Seen: 5,382 times

Last updated: Aug 19 '13