Ask Your Question

The problem about tbb and Vector<Mat.

asked 2014-03-31 07:01:38 -0500

我干过豪哥 gravatar image
class ApplyFoo {
    void operator()( const blocked_range<size_t>& r ) const {
        vector<Mat> a = my_a;
        for( size_t i=r.begin(); i!=r.end(); ++i ) 
    ApplyFoo( vector<Mat> a ) :

void test(vector<Mat> &src)

int main(int argc,char **argv)
    vector<Mat> Imgs(3);
    vector<Mat> omp(3);
    vector<Mat> tbb(3);
    Imgs[0] = imread("1.jpg",-1);
    Imgs[1] = imread("2.jpg",-1);
    Imgs[2] = imread("3.jpg",-1);
    tbb = Imgs;
    omp = Imgs;
    int64 t = getTickCount();
    for(size_t i = 0 ;i < Imgs.size(); ++i)
    cout<<"serial cost is "<<(getTickCount() - t)/getTickFrequency()<<endl;
    t = getTickCount();
#pragma omp parallel num_threads(4)
    for(int i = 0 ;i < Imgs.size(); ++i)
    cout<<"omp cost is "<<(getTickCount() - t)/getTickFrequency()<<endl;    
    t = getTickCount();
    cout<<"tbb cost is "<<(getTickCount() - t)/getTickFrequency()<<endl;    

    return 0;

I want to compare the results between openmp and tbb in the opencv code(as above),but in the section of tbb,the result image is the same as the origin img,it is not working.I don't use cv::ParallelLoopBody,because I think there is no difference between them . I am new to tbb and hope some point out error about tbb

edit retag flag offensive close merge delete

1 answer

Sort by » oldest newest most voted

answered 2014-04-01 10:23:56 -0500

kbarni gravatar image


I think the problem is that you are not returning the resulting image.

Try to implement the test function and the ApplyFoo class for two parameters (vector<Mat> &src, vector<Mat> &dest). Then call test(tbb,tbb).

Otherwise I'm not sure it's a good idea to measure elapsed time by counting ticks in multithreaded application: it measures CPU cycles (operations), which is the same in single threaded and parallelized applications. Try clock_gettime or similar functions instead.

edit flag offensive delete link more
Login/Signup to Answer

Question Tools


Asked: 2014-03-31 07:01:38 -0500

Seen: 134 times

Last updated: Apr 01 '14