Hey guys,
I’ve got a problem with CUDA. I want to programm a simple raytracer which traces spheres and is calculated via the gpu. I’ve got all the pixels in a grid and the blocks are managing the samples for one specific pixel. After the hit function call, the color value of the sample should be added to the global pixel array. The problem is now, with 64 samples on 400x400 it works fine. But if I raise the sample count to 256 CUDA crashes and the screen gets black for a short ammount of time. After that I got a message: The display driver was restored after an error.
Here’s the source for the kernel main function:
__global__ void
raytrace_gpu( SSphere* spheres, int num_objects, SColorInt* pixels, SCameraParameter* cam_params)
{
// read sample index and number of sumples
int sample = threadIdx.x;
int num_samples = blockDim.x;
// read pixel indicees
int pixel_x = blockIdx.x;
int pixel_y = blockIdx.y;
// detect screen resolution
int res_x = gridDim.x;
int res_y = gridDim.y;
// construct ray
SRay ray;
SPoint2D sp, pp;
sp.x = sample / num_samples; sp.y = sample / num_samples;
pp.x = cam_params->s * (pixel_x - 0.5 * res_x + sp.x);
pp.y = cam_params->s * (pixel_y - 0.5 * res_y + sp.y);
ray.o = cam_params->eye;
ray.d = get_ray_direction(pp, cam_params->u, cam_params->v, cam_params->w, cam_params->d);
// raytrace
SShadeRec sample_hit = hit_objects(ray, spheres, num_objects);
sample_hit.color = col_div_float(sample_hit.color, (float)num_samples);
// compute sample value
SColorInt sample_color = {sample_hit.color.r * 255, sample_hit.color.g * 255, sample_hit.color.b * 255};
// add sample value to pixel
atomicAdd(&pixels[pixel_y * res_x + pixel_x].r, sample_color.r);
atomicAdd(&pixels[pixel_y * res_x + pixel_x].g, sample_color.g);
atomicAdd(&pixels[pixel_y * res_x + pixel_x].b, sample_color.b);
}
If I don’t execute the atomicAdd function calls, everything works fine with 256 samples.
Does anyone know why this happens or anyone encountered the same problem?
Thanks for help.
Daniel