C++ 这个函数可以使用OpenCL并行化吗?

C++ 这个函数可以使用OpenCL并行化吗?,c++,parallel-processing,opencl,C++,Parallel Processing,Opencl,我试图通过使用OpenCL在GPU上移植项目中的一些CPU函数来提高执行时间 使用VS profiler,我注意到大部分执行时间都花在InsertCandidate()函数上,我正在考虑编写一个内核在GPU上执行这个函数。此函数最昂贵的操作是指令的。但可以看到,每个for循环包含3个if指令,这可能导致发散,导致序列化,即使在GPU上执行也是如此 template <class Tp> int InsertCandidate( std::list<Tp> &det

我试图通过使用OpenCL在GPU上移植项目中的一些CPU函数来提高执行时间

使用VS profiler,我注意到大部分执行时间都花在InsertCandidate()函数上,我正在考虑编写一个内核在GPU上执行这个函数。此函数最昂贵的操作是指令的
。但可以看到,每个for循环包含3个if
指令,这可能导致发散,导致序列化,即使在GPU上执行也是如此

template <class Tp>
int InsertCandidate( std::list<Tp> &detected, const Tp &box, double &nProbThreshold, int nMaxCandidate, double nMinProb )
{
    if( box._prob < nMinProb && box._prob < nProbThreshold )
        return -1;
    // Only use detection score to select positives.
    if( nMaxCandidate == 0 )
    {
        if( box._prob > nMinProb )
            detected.push_back( box );
        return 0;
    }

    typename std::list<Tp>::iterator    iter;
    int nCandidate = 0;

    for( iter = detected.begin(); iter != detected.end(); iter++ )
    {
        if( nCandidate == nMaxCandidate-1 )
            nProbThreshold = iter->_prob;

        if( box._prob >= iter->_prob )
            break;
        if( nCandidate >= nMaxCandidate && box._prob <= nMinProb )
            break;
        nCandidate ++;

    }

    if( nCandidate < nMaxCandidate || box._prob > nMinProb )
        detected.insert( iter, box );
    return 0;
}
模板
int InsertCandidate(标准::列表和检测,常量Tp和box,双精度和非双精度阈值,int nMaxCandidate,双精度nMinProb)
{
如果(框。\u probnMinProb)
检测到。推回(框);
返回0;
}
typename std::list::iterator iter;
int nCandidate=0;
for(iter=detected.begin();iter!=detected.end();iter++)
{
如果(nCandidate==nmaxandidate-1)
nProbThreshold=iter->u prob;
如果(框.\u prob>=iter->\u prob)
打破
如果(nCandidate>=nmaxandidate&&box.\u prob nMinProb)
检测。插入(iter,盒子);
返回0;
}

作为一个结论,这个程序可以转换成OpenCL?< /P>,在我看来,你正在使用线性搜索来插入列表中的对象。线性搜索是缓慢的,你应该考虑是否可以改变算法以避免不断修改对象列表。从你提供的代码,几乎不可能知道EMP是否存在。LoopEOpenCL是个好主意。在我看来,你正在使用线性搜索来插入列表中的对象。线性搜索是缓慢的,你应该考虑是否可以改变算法以避免不断修改对象列表。从你提供的代码来看,几乎不可能知道采用OpenCL是否是个好主意。