0
我有以下一段代碼,我想以某種方式進行並行處理。我犯了一個錯誤,因此並不是所有線程都像我認爲的那樣運行循環。如果有人能幫我找出那個錯誤,那將是非常好的。OpenMP - 在塊上執行線程
這是計算直方圖的代碼。
#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變量記錄迭代次數,我注意到串行版本和並行版本之間存在顯着差異。我猜並不是所有的線程都在運行,因此我從並行程序獲得的直方圖值遠遠小於實際讀數(密集數組存儲直方圖值)。
感謝,
薩揚
是的,它會工作,但我想如果我可以讓每個線程並行運行for循環,那麼iSize的整個範圍將被覆蓋(因此下標就像[i + threadNum * chunk]) ,並且可能它會比在外部循環中放置一個平行和在稠密之前放置一個雜注原子更快。但我想我應該刪除完整的雜注。 – Sayan 2010-06-29 16:21:49
對不起,我能夠通過使用OpenMP論壇中描述的策略來加快代碼的執行速度。可以幫助像我這樣的新手。 - http://openmp.org/forum/viewtopic.php?f=3&t=896 – Sayan 2010-07-07 03:11:20