Hello everybody, i have to do a project in C for CUDA and i don’t understand why it doesn’t works.
I want to convert a picture in grayscale. I just did it on CPU (c++) and i have to do it in CUDA too.
I’m Belgian so i will try to translate some part of the code for a better understanding.
Look at this code :
#include <stdio.h>
#include <cv.h>
#include <highgui.h>
#include <math.h>
#include <cutil_inline.h>
#define LONG 490000
float* h_rouge;
float* h_vert;
float* h_bleu;
float* h_gris;
float* d_rouge;
float* d_vert;
float* d_bleu;
float* d_gris;
void Cleanup(void);
// device code
__global__ void NVG(const float* R, const float* V, const float* B,float* G, int N)
{
int i = blockDim.x * blockIdx.x + threadIdx.x;
if (i < N)
{
G[i]=((0.299*R[i])+(0.587*V[i])+(0.114*B[i]))/3; // operation for grayscale -> (RED * 0.299 + GREEN * 0.587 + BLEU * 0.114) / 3
}
}
// Code sur l'host
int main(int argc, char** argv)
{
//Image de base et niveau de gris
IplImage* imgori = cvLoadImage("1.jpg"); // i load a picture in the same directory
IplImage* img = cvCloneImage(imgori);
IplImage* imgnvg = cvCreateImage(cvGetSize(img), IPL_DEPTH_8U, 1); // i create a picture to put the gray values in it
// conversion en niveau de gris
int N = LONG;
int haut = imgori->height; // i put the size of the picture
int larg = imgori->width;
int emplacement = 0;
int bleu = 0;
int rouge = 0;
int vert = 0;
size_t size = N * sizeof(float);
uchar* dataimg = (uchar *)imgnvg->imageData;
for(int numerolign=0;numerolign<(haut);numerolign++)
{
for(int numerocolonne=0;numerocolonne<(larg);numerocolonne++) // i traverse the picture
{
bleu = ((uchar *)(img->imageData + numerolign*img->widthStep))[numerocolonne*img->nChannels + 0]; // B
vert = ((uchar *)(img->imageData + numerolign*img->widthStep))[numerocolonne*img->nChannels + 1]; // G
rouge = ((uchar *)(img->imageData + numerolign*img->widthStep))[numerocolonne*img->nChannels + 2]; // R
h_bleu[emplacement] = bleu;
h_vert[emplacement] = vert; // i put the RGB values in 3 tables on the host
h_rouge[emplacement] = rouge;
emplacement++;
}
}
//allocating host memory
h_bleu = (float*)malloc(size);
if (h_bleu == 0) Cleanup();
h_vert = (float*)malloc(size);
if (h_vert == 0) Cleanup();
h_rouge = (float*)malloc(size);
if (h_rouge == 0) Cleanup();
h_gris = (float*)malloc(size);
if (h_gris == 0) Cleanup();
//allocating device memory
cutilSafeCall( cudaMalloc((void**)&d_bleu, size) );
cutilSafeCall( cudaMalloc((void**)&d_vert, size) );
cutilSafeCall( cudaMalloc((void**)&d_rouge, size) );
cutilSafeCall( cudaMalloc((void**)&d_gris, size) );
// copy from host to device
cutilSafeCall( cudaMemcpy(d_bleu, h_bleu, size, cudaMemcpyHostToDevice) );
cutilSafeCall( cudaMemcpy(d_vert, h_vert, size, cudaMemcpyHostToDevice) );
cutilSafeCall( cudaMemcpy(d_rouge, h_rouge, size, cudaMemcpyHostToDevice) );
// kernel
int threadsPerBlock = 256;
int blocksPerGrid = (N + threadsPerBlock - 1) / threadsPerBlock;
NVG<<<blocksPerGrid, threadsPerBlock>>>(d_rouge, d_vert, d_bleu, d_gris, N);
// copy gray from device to host
cutilSafeCall( cudaMemcpy(h_gris, d_gris, size, cudaMemcpyDeviceToHost) );
int gris = 0;
emplacement = 0;
for(int numerolign=0;numerolign<(haut);numerolign++)
{
for(int numerocolonne=0;numerocolonne<(larg);numerocolonne++) // i put the gray values in the picture
{
gris = h_gris[emplacement];
dataimg[(numerolign*haut)+numerocolonne]=gris;
emplacement++;
}
}
// Affichage
cvNamedWindow( "originale", CV_WINDOW_AUTOSIZE ); //i open a window and i put original picture in it
cvShowImage( "originale", img );
cvNamedWindow( "nvg", CV_WINDOW_AUTOSIZE ); //i open a window and i put gray picture in it
cvShowImage( "nvg", imgnvg );
cvWaitKey(0);
cvDestroyAllWindows();
//free memory
cvReleaseImage( &imgori );
cvReleaseImage( &imgnvg );
cvReleaseImage( &img );
Cleanup();
system("PAUSE");
}
void Cleanup(void)
{
if (d_rouge)
cudaFree(d_rouge);
if (d_vert)
cudaFree(d_vert);
if (d_bleu)
cudaFree(d_bleu);
if (d_gris)
cudaFree(d_gris);
if (h_rouge)
free(h_rouge);
if (h_vert)
free(h_vert);
if (h_bleu)
free(h_bleu);
if (h_gris)
free(h_gris);
}
i copy the error but it’s in french i try to translate it :
Exception non gérée à 0x0080119a dans testcudaimage.exe : 0xC0000005: Violation d’accès lors de l’écriture à l’emplacement 0x00000000.
→ Exception not managed to 0x0080119a in testcudaimage.exe: 0xC0000005: Access violation at the time of the writing to the site 0x00000000.