【发布时间】:2015-08-12 16:06:36
【问题描述】:
我只是想知道如何将下面的 openMP 程序转换为 openCL 程序。
使用 openMP 实现的算法的并行部分如下所示:
#pragma omp parallel
{
int thread_id = omp_get_thread_num();
//double mt_probThreshold = mt_nProbThreshold_;
double mt_probThreshold = nProbThreshold;
int mt_nMaxCandidate = mt_nMaxCandidate_;
double mt_nMinProb = mt_nMinProb_;
int has_next = 1;
std::list<ScrBox3d> mt_detected;
ScrBox3d sample;
while(has_next) {
#pragma omp critical
{ // '{' is very important and define the block of code that needs lock.
// Don't remove this pair of '{' and '}'.
if(piter_ == box_.end()) {
has_next = 0;
} else{
sample = *piter_;
++piter_;
}
} // '}' is very important and define the block of code that needs lock.
if(has_next){
this->SetSample(&sample, thread_id);
//UpdateSample(sample, thread_id); // May be necesssary for more sophisticated features
sample._prob = (float)this->Prob( true, thread_id, mt_probThreshold);
//sample._prob = (float)_clf->LogLikelihood( thread_id);
InsertCandidate( mt_detected, sample, mt_probThreshold, mt_nMaxCandidate, mt_nMinProb );
}
}
#pragma omp critical
{ // '{' is very important and define the block of code that needs lock.
// Don't remove this pair of '{' and '}'.
if(mt_detected_.size()==0) {
mt_detected_ = mt_detected;
//mt_nProbThreshold_ = mt_probThreshold;
nProbThreshold = mt_probThreshold;
} else {
for(std::list<ScrBox3d>::iterator it = mt_detected.begin();
it!=mt_detected.end(); ++it)
InsertCandidate( mt_detected_, *it, /*mt_nProbThreshold_*/nProbThreshold,
mt_nMaxCandidate_, mt_nMinProb_ );
}
} // '}' is very important and define the block of code that needs lock.
}//parallel section end
我的问题是:这个部分可以用 openCL 实现吗? 我遵循了一系列openCL教程,我了解了工作方式,我在.cu文件中编写代码,(我之前安装了CUDA工具包)但这种情况下情况更复杂,因为使用了很多使用了头文件、模板类和面向对象编程。
如何将在 openMP 中实现的这个部分转换为 openCL? 我应该创建一个新的 .cu 文件吗?
任何建议都会有所帮助。 提前致谢。
编辑:
使用 VS 分析器我注意到大部分执行时间都花在了 InsertCandidate() 函数上,我正在考虑编写一个内核来在 GPU 上执行这个函数。此函数最昂贵的操作是for 指令。但是可以看出,每个for循环包含3条if指令,这会导致发散,导致序列化,即使在GPU上执行也是如此。
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 ++;
}
作为结论,这个程序可以转换成openCL吗?
【问题讨论】:
-
我有几个关于您的问题的问题。你用的是哪个版本的opencl?您的 openmp 实现是否获得了高水平的并行性?如果我理解正确,#pragma omp 关键块一次执行 1 个线程。 InsertCandidate 是否添加到问题集中?
-
我使用的是 opencl 1.2。目前我无法估计使用 openmp 实现的加速值。确实,#pragma omp critical blocks 一次执行 1 个线程,所以这意味着即使我使用 opencl,这些部分也无法加速。是的,InsertCandidate 添加到问题集中。
标签: c++ parallel-processing opencl openmp