簡體   English   中英

CUDA 並行編程

[英]CUDA programming in parallel

所以我已經多次討論過這個問題,但似乎無法弄清楚。 發生的事情是我試圖從 GPU 內存復制到 CPU 內存的變量似乎總是空白。

根據我的理解,我應該有一個或多個變量並創建這些副本的副本,這些副本將與一些數據一起發送到 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;
}

讓我們看看這一行:

gpu_histogram_equalization<<<1,result.w*result.h*sizeof(unsigned char)>>>
        (d_result,img_in.img,hist,result.w*result.h, 256);

以下是您遇到的一些問題:

  1. img_in.img - 這是主機內存
  2. hist - 這是主機內存

發生的情況是您的內核由於無效的內存訪問而崩潰。

在此處閱讀有關錯誤檢查的信息。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM