因此,我已經多次瀏覽過這些內容,但似乎無法解決這個問題。發生了什麼變化是我試圖從GPU內存複製到CPU內存的變量總是顯示爲空白。並行CUDA編程
從我的理解,我應該有一個變量或多個變量,並創建這些的副本,我將發送到GPU以及一些數據進行計算,一旦完成計算,返回並插入變量的內容從GPU轉換成CPU。
但每次我這樣做,我的變量'd_result'總是空的。如果任何人有關於如何解決這個問題的想法,將非常感激。
我的CUDA功能:
__global__ void gpu_histogram_equalization(unsigned char * img_out, unsigned char * img_in,
int * hist_in, int img_size, int nbr_bin){
int *lut = (int *)malloc(sizeof(int)*nbr_bin);
int i, cdf, min, d;
/* Construct the LUT by calculating the CDF */
cdf = 0;
min = 0;
i = threadIdx.x;
while(min == 0){
min = hist_in[i++];
}
d = img_size - min;
if(i < nbr_bin){
cdf += hist_in[i];
//lut[i] = (cdf - min)*(nbr_bin - 1)/d;
lut[i] = (int)(((float)cdf - min)*255/d + 0.5);
if(lut[i] < 0){
lut[i] = 0;
}
}
/* Get the result image */
if(i < img_size){
if(lut[img_in[i]] > 255){
img_out[i] = 255;
}
else{
img_out[i] = (unsigned char)lut[img_in[i]];
}
}
}
然後我的函數調用它:
PGM_IMG gpu_contrast_enhancement_g(PGM_IMG img_in)
{
PGM_IMG result;
int hist[256];
unsigned char * d_result;
result.w = img_in.w;
result.h = img_in.h;
result.img = (unsigned char *)malloc(result.w * result.h * sizeof(unsigned char));
cudaMalloc(&d_result, result.w * result.h * sizeof(unsigned char));
cudaMemcpy(d_result, result.img, result.w * result.h * sizeof(unsigned char), cudaMemcpyHostToDevice);
histogram(hist, img_in.img, img_in.h * img_in.w, 256);
gpu_histogram_equalization<<<1,result.w * result.h * sizeof(unsigned char)>>>(d_result,img_in.img,hist,result.w*result.h, 256);
cudaMemcpy(result.img, d_result, result.w * result.h * sizeof(unsigned char), cudaMemcpyDeviceToHost);
cudaFree(d_result);
return result;
}
如果您需要調試幫助,您將不得不提供其他人可以編譯和運行的最短,完整的示例,因爲您提供的代碼是不夠的。另外,每個CUDA API調用都會返回一個狀態,您應該檢查它們是否存在運行時錯誤。如果使用cuda-memcheck運行代碼會發生什麼情況?它報告任何問題嗎? – talonmies
另外,你能否編輯你的問題標題來描述你的問題。標題對於搜索非常重要,並且標題中絕對說明沒有任何關於你的問題實際上是什麼 – talonmies