Manipulating image pixels stored in 1D array

Hi all,

I’m new to CUDA and C itself :) I’ve tried coding out a simple matrix manipulation (guided by the manual) and it worked well, so I decided to bring things further to do some Image Processing. Before implementing any algorithms, I’d like to test out the GPUs manipulating pixels in parallel.

Here goes the problem >>

The main application launched an OpenGL window with the image (BMP loaded from a file) rendered on its background. This test application is supposed to randomly set a pixel’s color value to 255 (since it runs non-stop over time, eventually we can see the image being distorted). The code runs fine serially on CPU (the commented line at the end of cudaProcess() but when trying to run on the GPUs, it doesn’t seem to do anything to the pixels at all. The [u]img->data[\u] is stored as [u]unisgned char *[\u].

I’ve also tried changing the “block” and “grid” dimensions, still no difference :no: The reason I’ve not used threadIdx or blockIdx is to simplify matters.

Below is a part of my code extracted for reference:


//CUDA parallel process

global void CUDA_PROCESS( unsigned char *data, int n){

//Do something to pixels

data[n] = 255;


//Initialize GPU and invoke process

void cudaProcess(pixmap *img){

printf("\nInitiate CUDA function...\n");

unsigned int imageSize = img->width * img->height * 3;

unsigned char *d_data;

size_t memSize = imageSize * sizeof(unsigned char*);

cudaMalloc( (void**) &d_data, memSize);

cudaMemcpy( &d_data, &img->data, memSize, cudaMemcpyHostToDevice);

dim3 block(10);

dim3 grid(10);

int randomNum = (rand() % img->height) * (rand() % img->width) + (rand() % 3);


cudaMemcpy( &img->data, &d_data, memSize, cudaMemcpyDeviceToHost);


printf("\nFinish executing CUDA function.\n");

//img->data[randomNum] = 255;



Any pointers or hints would be much appreciated. Thanks in advance!

You dont pass “&d_data” to cudaMemcpy… Thats a GRAVE error. Your cudaMemcpys must be failing saying “bad address”.
Just passing “d_data” should do…