2012-04-21 30 views
2

我想在CUDA中的設備上存儲背景圖像數據。後來,當我從視頻源中讀取新場景時,我想將新場景作爲前景圖像發送到GPU,並將其從背景圖像中減去。我不希望爲每個場景重新發送背景圖像到GPU。我怎樣才能做到這一點?CUDA:我如何存儲持久性數據?

回答

3

存放在設備的存儲器陣列的背景圖像(即在GPU)。然後當您讀取前景圖像時使用cudaMemcpy將其複製到另一個設備存儲器陣列。然後啓動一個內核,將兩個設備內存數組作爲參數並執行圖像減法。應該很簡單。

假設你使用默認的上下文創建,這是所有在同一個CPU線程中運行,你不必擔心做具體,讓您的CUDA上下文「完整​​」的巴特評論什麼。但是,如果你做任何CPU多線程,你將需要做一些上下文管理。

+0

感謝您的回覆。我正在使用C#接口並將其綁定到CUDA dll。我必須存儲哪個GPU內存?我首先想到的是存儲在不斷的記憶中,但我無法在其他框架中找到它。 – 2012-04-24 05:33:17

+1

常量應該沒問題,但它只有在塊中的所有線程同時訪問相同地址時纔有效。你可能想使用cudaMalloc的設備內存,並傳遞指針,然後cudaFree它,當你完成。如果你不能在另一個框架中訪問常量內存,那麼你必須以某種方式破壞你的上下文,這是不好的。 – harrism 2012-04-24 06:16:14

1

下面是一個簡單的例子..

int main(int argc, char **argv) { 
    uint *hostBackground, *hostForeground; //new uint[].. 
    uint *background, *foreground; 

首先初始化背景和前景數據..

cudaMalloc(background, ..); 
    cudaMalloc(foreground, ..); 

然後加載後臺數據

cudaMemCpy(background, hostBackground, ..); //copy to device.. 

然後讀取前景數據

while (applicationRuns) { 
     readImage(hostForeground); //read image.. 
     cudaMemcpy(foreground, hostForeground, ..); //copy to device 

     //operate on foreground.. 
     substruct_kernel<<<threads, blocks>>>(foreground, background, width, height); 

     cudaMemcpy(hostForeground, foreground, ..); //copy to host 

     //use hostForeground 
    } 

釋放他們,

cudaFree(foreground); 
    cudaFree(background); 
} 

下面是一個簡單substruct內核..

__global__ void substruct_kernel(uint *foreground, uint *backgroung, int width, int height) 
{ 
    int idx = threadIdx.x + threadDim.x * blockIdx.x; 
    int idy = threadIdx.y + threadDim.y * blockIdx.y; 

    if (idx < width && idy < height) 
     foreground[idx + idy * width] -= background[idx + idy * width]; //clamp may be required.. 
} 

我不建議使用庫這種簡單的操作。 Blas庫或Thrust庫可能是選項。