0

私はCUDAを学んでいますが、まだ初心者レベルです。簡単な代入を試みていますが、コードを実行するとクラッシュし、その理由がわかりません。どんな助けでも大歓迎です。

編集:cudaMemcpy構造上およびImage構造内でクラッシュします。pixelValタイプはint**です。それが原因ですか?

元の C++ コード:

void Image::reflectImage(bool flag, Image& oldImage)
/*Reflects the Image based on users input*/
{
    int rows = oldImage.N;
    int cols = oldImage.M;
    Image tempImage(oldImage);

    for(int i = 0; i < rows; i++)
    {
        for(int j = 0; j < cols; j++)
        tempImage.pixelVal[rows - (i + 1)][j] = oldImage.pixelVal[i][j];
    }
    oldImage = tempImage;
}

私のCUDAカーネルとコード:

#define NTPB 512
__global__ void fliph(int* a, int* b, int r, int c)
{
    int i = blockIdx.x * blockDim.x + threadIdx.x;
    int j = blockIdx.y * blockDim.y + threadIdx.y;

    if (i >= r || j >= c)
        return;
    a[(r - i * c) + j] = b[i * c + j];
}
void Image::reflectImage(bool flag, Image& oldImage)
/*Reflects the Image based on users input*/
{
    int rows = oldImage.N;
    int cols = oldImage.M;
    Image tempImage(oldImage);
    if(flag == true) //horizontal reflection
    {
     //Allocate device memory
     int* dpixels;
     int* oldPixels;
     int n = rows * cols;
     cudaMalloc((void**)&dpixels, n * sizeof(int));
     cudaMalloc((void**)&oldPixels, n * sizeof(int));
     cudaMemcpy(dpixels, tempImage.pixelVal, n * sizeof(int), cudaMemcpyHostToDevice);
     cudaMemcpy(oldPixels, oldImage.pixelVal, n * sizeof(int), cudaMemcpyHostToDevice);
     int nblks = (n + NTPB - 1) / NTPB;
     fliph<<<nblks, NTPB>>>(dpixels, oldPixels, rows, cols);
     cudaMemcpy(tempImage.pixelVal, dpixels, n * sizeof(int), cudaMemcpyDeviceToHost);
     cudaFree(dpixels);
     cudaFree(oldPixels);
    }
    oldImage = tempImage;
}
4

1 に答える 1

1

i2D インデックスとを使用して画像を処理するには、2D グリッドを作成する必要がありjます。現在のケースでは、カーネルは画像の最初の行のみを処理しています。

2D インデックス メカニズムを作成するには、次のように 2D ブロックと 2D グリッドを作成します。

const int BLOCK_DIM = 16;

dim3 Block(BLOCK_DIM,BLOCK_DIM);

dim3 Grid;
Grid.x = (cols + Block.x - 1)/Block.x;
Grid.y = (rows + Block.y - 1)/Block.y;

fliph<<<Grid, Block>>>(dpixels, oldPixels, rows, cols);
于 2013-04-04T18:03:42.183 に答える