Ask Your Question


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

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

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

class ParallelClass: public cv::ParallelLoopBody

    void operator()( const cv::Range &r )const;
 void ParallelClass::operator()( const cv::Range &r )const{

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



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 -0500 )edit

3 answers

Sort by ยป oldest newest most voted

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

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

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


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 -0500 )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 -0500 )edit

@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 -0500 )edit

Thanks, I fix it!

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

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

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

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

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

edit flag offensive delete link more
Login/Signup to Answer

Question Tools



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

Seen: 4,090 times

Last updated: Aug 19 '13