Ask Your Question
0

The problem about tbb and Vector<Mat.

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

我干过豪哥 gravatar image
class ApplyFoo {
    vector<Mat>my_a;
public:
    void operator()( const blocked_range<size_t>& r ) const {
        vector<Mat> a = my_a;
        for( size_t i=r.begin(); i!=r.end(); ++i ) 
           //Foo(a[i]);
           pyrDown(a[i],a[i],cv::Size(),4);
    }
    ApplyFoo( vector<Mat> a ) :
        my_a(a)
    {}
};


void test(vector<Mat> &src)
{
    parallel_for(blocked_range<size_t>(0,3),ApplyFoo(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)
    {
        pyrDown(Imgs[i],Imgs[i],cv::Size(),4);
    }
    cout<<"serial cost is "<<(getTickCount() - t)/getTickFrequency()<<endl;
    t = getTickCount();
#pragma omp parallel num_threads(4)
    for(int i = 0 ;i < Imgs.size(); ++i)
    {
        pyrDown(omp[i],omp[i],cv::Size(),4);
    }
    cout<<"omp cost is "<<(getTickCount() - t)/getTickFrequency()<<endl;    
    t = getTickCount();
    test(tbb);
    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
1

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

kbarni gravatar image

Hi,

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

Question Tools

Stats

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

Seen: 186 times

Last updated: Apr 01 '14