Нужна помощь в очень простой программе Cuda и C ++ - PullRequest
1 голос
/ 23 апреля 2011

Я новичок в программировании на Cuda.Я пытаюсь создать простую программу обработки изображений cuda и cpp, чтобы изменить яркость, насыщенность, контрастность и т. Д. Я начал с очень простой функции - просто изменить яркость изображения, умножив все компоненты RGB:изображение со значением альфа.

Это моя программа CPP:

#include <cutil_inline.h>
#include <cutil_gl_inline.h>

#include <cuda_runtime_api.h>
#include <cuda_gl_interop.h>

using namespace std;

struct ImageData {
    unsigned char *data;    /* Points to large array of R,G,B-order data */
    int  height;
    int width;
};

ImageData imageData;
float *imageResult; // to store the image result from cuda after running the kernel
unsigned char *d_Input;
unsigned char *d_Output;
unsigned char *h_Output;

// These are CUDA functions to handle allocation and launching the kernels
extern "C" void initInput( unsigned char *data, unsigned char **device, unsigned int size); 
extern "C" void filter(unsigned char *d_src, unsigned char *d_dest, int width, int height, int filterMode, 
                float alpha, float contrast, float saturation, bool use_array );

void initCuda()
{
        unsigned int size = imageData.width * imageData.height * 3 * sizeof(unsigned char);
        cutilSafeCall(cudaMalloc ((void**) &d_Input, size));  // allocate storage for device image input
        cutilSafeCall(cudaMalloc ((void**) &d_Output, size)); // allocate storage for device image output
    initInput( imageData.data, &d_Input, size); 
}

int main () {


    loadPPMImageData( (char *)"boxes.ppm", &imageData); //this function is defined in another file
    cudaGLSetGLDevice( 0 );
    initCuda();
    filter(  d_Input, d_Output, imageData.width, imageData.height, 1, 0.8, 1.0, 1.0, 1 );

    cutilSafeCall(cudaMemcpy( h_Output, d_Output, size, cudaMemcpyDeviceToHost)); // copy output data from device to host
    //print the output
    for (int i = 0; i < imageData.size; i++) {
        cout << d_Output
    }
    // do some memory cleanups

    //done
    return 0
}

И это мой файл kernel.cu:

#include <iostream>
#include <cstdlib>
#include <string>
#include <cmath>

#include <shrUtils.h>
#include <cutil_inline.h>
#include <cutil_math.h>


//Kernel function
__global__ void
applyAlpha(unsigned char* input, unsigned char* output, int width, int height, float alpha) 
{
// calculate normalized coordinates
unsigned int x = blockIdx.x * blockDim.x + threadIdx.x;
unsigned int y = blockIdx.y * blockDim.y + threadIdx.y;
output[ ((y * width + x) * 3) + 0] = (int) ( (int)input [ ((y * width + x) * 3) + 0] * alpha); // r
output[ ((y * width + x) * 3) + 1] = (int) ( (int)input [ ((y * width + x) * 3) + 1] * alpha); // g
output[ ((y * width + x) * 3) + 2] = (int) ( (int)input [ ((y * width + x) * 3) + 2] * alpha); // b
}


extern "C"
int iDivUp( int a, int b ){
    return (a % b != 0) ? (a / b + 1) : (a / b);
}


extern "C" 
void initInput( unsigned char *data, unsigned char **deviceArray, unsigned int size) // /* image data, device pointer, etc */ )
{
    /* TODO: Array version DONE
     *  Initialize device memory for array version
     *  and cuda arrays for texture version here
     */
    cutilSafeCall(cudaMemcpy( deviceArray, data, size, cudaMemcpyHostToDevice)); // copy image data from host to device (array version)
    //TODO: Texture version

}


extern "C" 
void filter( unsigned char *d_src, unsigned char *d_dest, int width, int height, int filter_mode, float alpha, float contrast, float saturation, bool use_array )
{
    /*  TODO
     * run different kernels for array and texture version
     */

    dim3 dimBlock(16, 16, 1);
    dim3 dimGrid( iDivUp (width, dimBlock.x), iDivUp( height, dimBlock.y), 1);

    if (use_array) { // Array version
        if (filter_mode == 1) { // filter mode: brightness (alpha)

            applyAlpha<<< dimGrid, dimBlock >>>(d_src, d_dest, width, height, alpha);

            // check if kernel execution generated an error
            cutilCheckMsg("Kernel execution failed");

            cutilSafeCall( cutilDeviceSynchronize() );
        }
    }
    else { //Texture Version
        //not yet implemented
    }

}

// РЕДАКТИРОВАТЬ Я изменил вышеуказанный файл, основываясь на ответе Эндрю.Однако теперь я получил следующие ошибки после компиляции:

ld: warning: ignoring file kernel.o, file was built for i386 which is not the architecture being linked (x86_64)
Undefined symbols for architecture x86_64:
  "_initInput", referenced from:
      initCuda()    in CS380_prog4.o
  "_filter", referenced from:
      display()     in CS380_prog4.o
     (maybe you meant: ___GLEW_SGIS_texture_filter4, ___GLEW_EXT_texture_filter_anisotropic , ___GLEW_NV_multisample_filter_hint )
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
make: *** [testprog] Error 1

Я использовал команду "extern C" в обеих этих функциях: initInput и filter.Объявление функции (в test.cpp) и определение (в kernel.cu) также имеют одинаковые аргументы, но все еще жалуются, что не могут найти функцию.Как я могу исправить эту проблему с ссылками?

Ответы [ 2 ]

4 голосов
/ 23 апреля 2011

Вы включаете ваш файл .cu непосредственно в файл .cpp, который эффективно копирует содержимое в файл .cpp.nvcc будет использовать стандартный компилятор C ++ для его компиляции (g ++ на платформе Unix), который не будет знать, что означает любой синтаксис Cuda.

Вы должны скомпилировать каждый из них как объектный файл, а затем связать их с компилятором C ++, создавая заголовок для экспортируемых функций в файле .cu так же, как для стандартного C.

0 голосов
/ 23 апреля 2011

Прекрасный пример в cuda-grayscale .

Используется для компиляции на CUDA 3.1. Там есть Makefile, возьмите его на себя.

CXX=g++

CUDA_INSTALL_PATH=/usr/local/cuda
CFLAGS= -I. -I$(CUDA_INSTALL_PATH)/include `pkg-config --cflags opencv`
LDFLAGS= -L$(CUDA_INSTALL_PATH)/lib -lcudart `pkg-config --libs opencv`    

all:
        $(CXX) $(CFLAGS) -c main.cpp -o Debug/main.o
        nvcc $(CUDAFLAGS) -c kernel_gpu.cu -o Debug/kernel_gpu.o
        $(CXX) $(LDFLAGS) Debug/main.o Debug/kernel_gpu.o -o Debug/grayscale

clean:
        rm -f Debug/*.o Debug/grayscale
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...