2017-05-09 1 views
0

GPU에서 필터가있는 .bmp 파일을 처리하려고합니다. 이를 위해 장치에 변수 d_bufferRGB를 할당하고 채울 필요가 있습니다. 하지만 그렇게하면 컴파일러에서 호스트에 정의되어 있고 "identifier d_bufferRGB is undefined"오류로 종료된다고 알려줍니다. load_BMP 및 save_BMP 함수는 완전히 작동합니다. d_bufferRGB에 bufferRGB를 복사하는 방법을 알아야합니다. 분명히 실수를 저지르고 있습니다. 도와주세요.CUDA에서 변수 할당 C++ - 장치 변수를 할당 할 수 없습니다.

소스 코드 여기 >>>

#include <stdio.h> 
#include <stdlib.h> 
#include <Windows.h> 
#include <cuda_runtime.h> 
#include <cuda.h> 
#include "device_launch_parameters.h" 
#include <iostream> 
//using namespace std; 

int width, heigth; 
long size, *d_size; 
RGBTRIPLE *bufferRGB, *new_bufferRGB; 
RGBTRIPLE *d_bufferRGB, *d_new_bufferRGB; 


void load_bmp(RGBTRIPLE **bufferRGB, int *width, int *heigth, const char *file_name) 
{ 
    BITMAPFILEHEADER bmp_file_header; 
    BITMAPINFOHEADER bmp_info_header; 
    FILE *file; 

    file = fopen(file_name, "rb"); 

    fread(&bmp_file_header, sizeof(BITMAPFILEHEADER), 1, file); 

    fread(&bmp_info_header, sizeof(BITMAPINFOHEADER), 1, file); 

    *width = bmp_info_header.biWidth; 
    *heigth = bmp_info_header.biHeight; 
    size = (bmp_file_header.bfSize - bmp_file_header.bfOffBits); 

    int x, y; 
    *bufferRGB = (RGBTRIPLE *)malloc(*width* *heigth * 4); 

    fseek(file, bmp_file_header.bfOffBits - sizeof(bmp_file_header) - sizeof(bmp_info_header), SEEK_CUR); 

    for (y = 0; y < *heigth; y++) 
    { 
     for (x = 0; x < *width; x++) 
     { 
      (*bufferRGB)[(y * *width + x)].rgbtBlue = fgetc(file); 
      (*bufferRGB)[(y * *width + x)].rgbtGreen = fgetc(file); 
      (*bufferRGB)[(y * *width + x)].rgbtRed = fgetc(file); 
     } 
     for (x = 0; x < (4 - (3 * *width) % 4) % 4; x++) 
      fgetc(file); 
    } 
    fclose(file); 
} 

void save_bmp(RGBTRIPLE *bufferRGB, const char *new_name, const char *old_name) 
{ 
    BITMAPFILEHEADER bmp_file_header; 
    BITMAPINFOHEADER bmp_info_header; 
    FILE *file; 

    file = fopen(old_name, "rb"); 

    fread(&bmp_file_header, sizeof(BITMAPFILEHEADER), 1, file); 

    fread(&bmp_info_header, sizeof(BITMAPINFOHEADER), 1, file); 
    fclose(file); 

    file = fopen(new_name, "wb"); 

    fwrite(&bmp_file_header, sizeof(BITMAPFILEHEADER), 1, file); 
    fwrite(&bmp_info_header, sizeof(BITMAPINFOHEADER), 1, file); 
    fseek(file, bmp_file_header.bfOffBits - sizeof(bmp_file_header) - sizeof(bmp_info_header), SEEK_CUR); 

    int alligment_x = (4 - (3 * width) % 4) % 4; 
    unsigned char *to_save = (unsigned char *)malloc((width * 3 + alligment_x)*heigth); 
    unsigned int index = 0; 
    int x, y; 

    for (y = 0; y < heigth; y++) 
    { 
     for (x = 0; x < width; x++) 
     { 
      to_save[index++] = bufferRGB[(y * width + x)].rgbtBlue; 
      to_save[index++] = bufferRGB[(y * width + x)].rgbtGreen; 
      to_save[index++] = bufferRGB[(y * width + x)].rgbtRed; 
     } 
     for (x = 0; x < alligment_x; x++) 
      to_save[index++] = 0; 
    } 
    fwrite(to_save, (width * 3 + alligment_x)*heigth, 1, file); 

    fclose(file); 
    free(to_save); 
} 


__global__ void CUDA_filter_grayscale() 
{ 
    int idx = blockIdx.x*blockDim.x + threadIdx.x; 
    BYTE grayscale; 

    if (idx < *d_size) 
    { 
     grayscale = ((d_bufferRGB[idx].rgbtRed + d_bufferRGB[idx].rgbtGreen + d_bufferRGB[idx].rgbtBlue)/3); 
     d_new_bufferRGB[idx].rgbtRed = grayscale; 
     d_new_bufferRGB[idx].rgbtGreen = grayscale; 
     d_new_bufferRGB[idx].rgbtBlue = grayscale; 
    } 
} 

int main() 
{ 

    //GPU cast ---------------------------------------------------------------------------------------------- 

    cudaMalloc((void**)&d_new_bufferRGB, width*heigth * 4); 
    cudaMalloc((void**)&d_bufferRGB, width*heigth * 4); 
    cudaMalloc((void**)&d_size, sizeof(size)); 

    load_bmp(&bufferRGB, &width, &heigth, "test.bmp"); 

    cudaMemcpy(&d_size, &size, sizeof(size), cudaMemcpyHostToDevice); 
    cudaMemcpy(d_bufferRGB, bufferRGB, sizeof(bufferRGB), cudaMemcpyHostToDevice); 

    CUDA_filter_grayscale << <256, 512 >> >(); //size dont bother me for now 

    cudaMemcpy(d_new_bufferRGB, new_bufferRGB, sizeof(new_bufferRGB), cudaMemcpyDeviceToHost); 

    save_bmp(new_bufferRGB, "filter_grayscale_GPU", "test.bmp"); 

} 

답변

1

커널 (예컨대 가변 d_bufferRGB)을 호스트 코드 변수에 액세스 할 수있다. 커널에서 호스트 코드 변수에 액세스해야하는 경우 커널에 인수로 전달해야합니다.

__global__ void CUDA_filter_grayscale(const RGBTRIPLE *d_bufferRGB, RGBTRIPLE *d_new_bufferRGB) 
{ 
    // Code same as before 
} 

// Usage in main(): 
CUDA_filter_grayscale << <256, 512 >> >(d_bufferRGB, d_new_bufferRGB); 
+0

Thx 많이 있습니다. 나는 2 일 동안 이걸로 귀찮게하고 대답을 찾을 수 없었다. –