CUDA Code compilation errors

Hello and good morning guys,

I tryied to compile this simple Ray tracer code from CUDA By Example book and i get a whole bunch of errors can you please help me sort this out…?

The code is this

#include <cuda.h>
#include <GL\gl.h>
#include <cpu_bitmap.h>
#include <stdio.h>
#include <math.h>

#define rnd( x ) (x * rand() / RAND_MAX)
#define SPHERES 20
#define INF 2e10f


__global__ void kernel (unsigned char *ptr)
{
	int x = threadIdx.x + blockIdx.x * blockDim.x;
	int y = threadIdx.y + blockIdx.y * blockDim.y;
	int offset = x + y * blockDim.x * gridDim.x;

	float ox = (x - DIM/2);
	float oy = (y - DIM/2);

	float r=0, g=0, b=0;
	float maxz = -INF;

	for(int i=0; i<SPHERES; i++)
	{
		float n;
		float t = s[i].hit( ox, oy, &n);

		if(t > maxz)
		{
			float fscale = n;
			r = s[i].r * fscale;
			g = s[i].g * fscale;
			b = s[i].b * fscale;
		}
	}

	ptr[offset*4 + 0] = (int)(r * 255);
	ptr[offset*4 + 1] = (int)(g * 255);
	ptr[offset*4 + 2] = (int)(b * 255);
	ptr[offset*4 + 3] = 255;	
}


struct Sphere {

	float x,y,z;
	float r,g,b;
	float radius;

	__device__ float hit(float ox, float oy, float *n)
	{
		float dx = ox - x;
		float dy = oy - y;

		if((dx*dx + dy*dy) < (radius*radius))
		{
			float dz = sqrtf( radius*radius - dx*dx - dy*dy );
			*n = dz / sqrtf(radius*radius);

			return dz + z;
		}

		return -INF;
	}
}







int main(void)
{
	Sphere *s;

	cudaEvent_t   start,stop;

	cudaEventCreate( &start );
	cudaEventCreate( &stop );
	cudaEventRecord( start , 0 );

	CPUBitmap bitmap( DIM,DIM);

	unsigned char *dev_bitmap;

	cudaMalloc(&dev_bitmap,bitmap.image_size());
	cudaMalloc(&s,sizeof(Sphere * SPHERES));

	Sphere *temp_s = (Sphere*)malloc( sizeof(Sphere) * SPHERES );

		for (int i=0; i<SPHERES; i++) 
		{
				temp_s[i].r = rnd( 1.0f );
				temp_s[i].g = rnd( 1.0f );
				temp_s[i].b = rnd( 1.0f );
				temp_s[i].x = rnd( 1000.0f ) - 500;
				temp_s[i].y = rnd( 1000.0f ) - 500;
				temp_s[i].z = rnd( 1000.0f ) - 500;
				temp_s[i].radius = rnd( 100.0f ) + 20;
		}

		cudaMemcpy(s, temp_s, sizeof((Sphere * SPHERES)), cudaMemcpyHostToDevice);

		free(temp_s);

		dim3 grids(DIM/16,DIM/16);
		dim3 threads(16,16);
		kernel<<<grids,threads>>>( dev_bitmap );

		cudaMemcpy(bitmap.get_bitmap_ptr(), dev_bitmap, bitmap.image_size(), cudaMemcpyDeviceToHost);

		bitmap.display_and_exit();

		cudaFree( dev_bitmap );
		cudaFree( s );	

		return 0;
}

and the list of errors i get is this

  • C:/Users/Angelos/Documents/Visual Studio 2010/Projects/CUDA/RayTracing/RayTracing/main.cu(18): error : identifier "DIM" is undefined

    C:/Users/Angelos/Documents/Visual Studio 2010/Projects/CUDA/RayTracing/RayTracing/main.cu(27): error : identifier “s” is undefined

    C:/Users/Angelos/Documents/Visual Studio 2010/Projects/CUDA/RayTracing/RayTracing/main.cu(74): error : expected a “;”

    C:/Users/Angelos/Documents/Visual Studio 2010/Projects/CUDA/RayTracing/RayTracing/main.cu(84): error : identifier “DIM” is undefined

    C:/Users/Angelos/Documents/Visual Studio 2010/Projects/CUDA/RayTracing/RayTracing/main.cu(89): error : expected a “)”
    1>

    C:/Users/Angelos/Documents/Visual Studio 2010/Projects/CUDA/RayTracing/RayTracing/main.cu(104): error : expected a “)”

    C:/Users/Angelos/Documents/Visual Studio 2010/Projects/CUDA/RayTracing/RayTracing/main.cu(104): error : expected an expression

The line numbers in the error messages do not line up with the line numbers of the code you posted, suggesting that the code you are showing above does not match the code you actually compiled. It seems that the actual project may consist of multiple files, and that certain entities (like DIM) might be imported from included files (e.g. header files) or should be locally defined by the user. So the first thing you would want to do is find out where the undefined identifiers "DIM’ and “s” are supposed to be coming from, and then adjust your rroject accordingly.

Thank you for your reply but i don’t think that the error messages are not alligned with the code. For example there is an error in line 89 in the definition of sizeof command. Line 89 should be like this

cudaMalloc(&s,sizeof(Sphere) * SPHERES);

not like this

cudaMalloc(&s,sizeof(Sphere * SPHERES));

Seems I was incorrect about the line numbering. There is indeed an undefined symbol DIM in line 18 of your code and an undefined symbol “s” in line 27. That clearly indicates that part of the program’s code is missing (such as a #define for DIM, and the definition of the array s).

You will need to find out where the missing code is. There may be a missing include file, or a second .cu file. I am sure you can figure it out.

[Later]

Looking at the code in the book, I see additional files being included at the start. The ray tracing code in the book starts with:

#include "cuda.h"
#include "../common/book.h"
#include "../common/cpu_bitmap.h"

Make sure your project incorprorates all relevant files.

Also as an aside, if you’re looking into Ray Tracing, after you’re done with your testing, take a look at the OptiX toolkit, which includes many useful samples. Also, there are 2 talks about OptiX, one of them was at GTC 2013 titled “GPU Ray Tracing Exposed: Under the Hood of the NVIDIA OptiX Ray Tracing Engine”:

http://www.gputechconf.com/gtcnew/on-demand-gtc.php?searchByKeyword=optix&searchItems=&sessionTopic=&sessionEvent=&sessionYear=&sessionFormat=&submit=&select=+#2253
There’s also an advanced one in GTC 2013 titled “Advanced OptiX Programming and Optimization” listed below that first watch link above.

The other was at GTC 2010, same title, but different content:
http://nvidia.fullviewmedia.com/gtc2010/0921-c-2250.html

I mention these because OptiX while being quite a capable API does not have the greatest documentation… and my coworker working to fix an inherited OptiX-based ray-tracing code found these 2 talks to be very useful.

If you find information missing from documentation, or existing information needs clarification, it would be helpful if bugs were filed for this. Thank you for your help.