如何将openmp改为tbb?

时间:2017-04-03 07:34:04

标签: openmp tbb

hellow,

我想将我的代码openmp更改为TBB

这是我的代码。

    for (long y = 0; y < _src.rows; y++)
    {
#pragma omp parallel 
       {
#pragma omp for firstprivate(i)
        for (long x = 0; x < _src.cols; x++)
        {
            const uchar* data = _src.ptr((int)y, (int)x);
            i = (y * _src.cols + x);
            //update model+ background subtract
          //  cout << "parallel: " << i << endl;
            uchar include = 0;
            int result = _cvCheckPixelBackgroundNP(i, data, nchannels,
                m_nN, m_aModel, m_fTb, m_nkNN, m_fTau, m_bShadowDetection, include);


            _cvUpdatePixelBackgroundNP(i, data, nchannels,
                m_nN, m_aModel,
                m_nNextLongUpdate,
                m_nNextMidUpdate,
                m_nNextShortUpdate,
                m_aModelIndexLong,
                m_aModelIndexMid,
                m_aModelIndexShort,
                m_nLongCounter,
                m_nMidCounter,
                m_nShortCounter,
                m_nLongUpdate,
                m_nMidUpdate,
                m_nShortUpdate,
                include
            );
            switch (result)
            {
            case 0:
                //foreground
                *_dst.ptr((int)y, (int)x) = 255;
                break;
            case 1:
                //background
                *_dst.ptr((int)y, (int)x) = 0;
                break;
            case 2:
                //shadow
                *_dst.ptr((int)y, (int)x) = nShadowDetection;
                break;
            }
           // i++;
        }
     }
    }
};

我尝试更改TBB代码

     long i = 0;


    tbb::parallel_for(tbb::blocked_range2d<int>(0,_src.rows,0,_src.cols)
        , [&](tbb::blocked_range2d<int>r) {

        for (int y = r.rows().begin(); y < r.rows().end();y++)
        {
            for (int x = r.cols().begin(); x != r.cols().end(); x++)
            {
                const uchar* data = _src.ptr((int)y, (int)x);
                i = (y * _src.cols + x);
                //update model+ background subtract
                uchar include = 0;
                int result = _cvCheckPixelBackgroundNP(i, data, nchannels,
                    m_nN, m_aModel, m_fTb, m_nkNN, m_fTau, m_bShadowDetection, include);

                _cvUpdatePixelBackgroundNP(i, data, nchannels,
                    m_nN, m_aModel,
                    m_nNextLongUpdate,
                    m_nNextMidUpdate,
                    m_nNextShortUpdate,
                    m_aModelIndexLong,
                    m_aModelIndexMid,
                    m_aModelIndexShort,
                    m_nLongCounter,
                    m_nMidCounter,
                    m_nShortCounter,
                    m_nLongUpdate,
                    m_nMidUpdate,
                    m_nShortUpdate,
                    include
                );
                switch (result)
                {
                case 0:
                    //foreground
                    *_dst.ptr((int)y, (int)x) = 255;
                    break;
                case 1:
                    //background
                    *_dst.ptr((int)y, (int)x) = 0;
                    break;
                case 2:
                    //shadow
                    *_dst.ptr((int)y, (int)x) = nShadowDetection;
                    break;
                }

                //  i++;

            }

        }
    },tbb::auto_partitioner());

我确实改变了时间。两个结果时间是simillary但是, 表现不同。

我该如何解决?抱歉我的英语不好。

0 个答案:

没有答案