我的問題的列表如下:我有,我發現使用GPU的興趣幾點的圖像。檢測是一個重量級的測試,但是隻有25個點中有1個平均通過測試。該算法的最後階段是建立一個點的列表。在CPU上,這將實現爲:與CUDA共享內存互斥 - 增加項目
forall pixels x,y
{
if(test_this_pixel(x,y))
vector_of_coordinates.push_back(Vec2(x,y));
}
在GPU上,我有每個CUDA塊處理16x16像素。問題是我需要做一些特殊的事情,最終在全局內存中有一個統一的點列表。目前我正在嘗試在每個塊的共享內存中生成一個本地列表,這些列表最終將被寫入全局內存。我試圖避免發送任何回到CPU,因爲在此之後有更多的CUDA階段。
我期待,我可以用原子操作來實現對共享內存的push_back功能。但是我無法得到這個工作。有兩個問題。第一個煩人的問題是,我經常遇到以下編譯器崩潰:「使用原子操作時,nvcc錯誤:'ptxas'死於狀態0xC0000005(ACCESS_VIOLATION)」。我是否可以編譯某些東西時遇到了問題。有誰知道是什麼原因造成的?
以下內核將重現錯誤:
__global__ void gpu_kernel(int w, int h, RtmPoint *pPoints, int *pCounts)
{
__shared__ unsigned int test;
atomicInc(&test, 1000);
}
其次,我的代碼,其中包括共享內存互斥鎖掛在GPU和我不明白爲什麼:
__device__ void lock(unsigned int *pmutex)
{
while(atomicCAS(pmutex, 0, 1) != 0);
}
__device__ void unlock(unsigned int *pmutex)
{
atomicExch(pmutex, 0);
}
__global__ void gpu_kernel_non_max_suppress(int w, int h, RtmPoint *pPoints, int *pCounts)
{
__shared__ RtmPoint localPoints[64];
__shared__ int localCount;
__shared__ unsigned int mutex;
int x = blockIdx.x * blockDim.x + threadIdx.x;
int y = blockIdx.y * blockDim.y + threadIdx.y;
int threadid = threadIdx.y * blockDim.x + threadIdx.x;
int blockid = blockIdx.y * gridDim.x + blockIdx.x;
if(threadid==0)
{
localCount = 0;
mutex = 0;
}
__syncthreads();
if(x<w && y<h)
{
if(some_test_on_pixel(x,y))
{
RtmPoint point;
point.x = x;
point.y = y;
// this is a local push_back operation
lock(&mutex);
if(localCount<64) // we should never get >64 points per block
localPoints[localCount++] = point;
unlock(&mutex);
}
}
__syncthreads();
if(threadid==0)
pCounts[blockid] = localCount;
if(threadid<localCount)
pPoints[blockid * 64 + threadid] = localPoints[threadid];
}
在這個例子中代碼在this site,作者設法成功地在共享內存上使用原子操作,所以我很困惑爲什麼我的情況不起作用。如果我註釋掉鎖和解鎖行,代碼運行正常,但顯然不正確地添加到列表中。
我將不勝感激,爲什麼這個問題正在發生,一些建議也說不定,如果有一個更好的解決方案,以實現這一目標,因爲我擔心反正關於使用原子操作或互斥鎖的性能問題。
這是相當有趣的。謝謝。 – Robotbugs 2012-02-28 20:03:03
我只是試圖實現此一件事是,我發現掃描功能位於行不正確的:「溫度[POUT * N + THID] + =溫度[銷* N + THID - 偏移];」。這實際上應該是「temp [pout * n + thid] = temp [pin * n + thid] + temp [pin * n + thid - offset];」 – Robotbugs 2012-02-28 23:09:36
好的,我基本上實現了你所擁有的,我將在稍後發佈最終代碼。非常感謝。 – Robotbugs 2012-02-28 23:41:50