0

我有以下一段代码,我想以某种方式使其并行。我犯了一个错误,因此并非所有线程都像我认为的那样运行循环。如果有人能帮我找出那个错误,那就太好了。

这是计算直方图的代码。

#pragma omp parallel default(shared) private(iIndex2, iIndex1, fDist) shared(iSize, dense) reduction(+:iCount)
{

chunk = (unsigned int)(iSize / omp_get_num_threads());
threadID = omp_get_thread_num();
svtout << "Number of threads available " << omp_get_num_threads() << endl;
svtout << "The threadID is " << threadID << endl;

//want each of the thread to execute the loop
    for (iIndex1=0; iIndex1 < chunk; iIndex1++)
    {
        for (iIndex2=iIndex1+1; iIndex2 < chunk; iIndex2++)
        {   
            iCount++;

            fDist = (*this)[iIndex1 + threadID*chunk].distance( (*this)[iIndex2 + threadID*chunk] );
            idx = (int)(fDist/fWidth);

            if ((int)fDist % (int)fWidth >= 0)
            {
               #pragma omp atomic
               dense[idx] += 1;
            }  
}
}

iCount 变量跟踪迭代次数,我注意到串行版本和并行版本之间存在显着差异。我猜不是所有线程都在运行,因此我从并行程序获得的直方图值远小于实际读数(密集数组存储直方图值)。

谢谢,
萨扬

4

1 回答 1

2

您是一个循环块,而不是具有多个线程的 iSize。尝试用 iSize 替换循环边界。

于 2010-06-29T15:51:41.573 回答