Search code examples
ccudac-preprocessornvcc

Pre-processor commands (#if defined) not working in CUDA program?


In this code snippet, I would expect the "printf" command inside the MatrixMultiplication() method to display its text. It does not, even though "size" has been declared on the previous line and "test" is set to 1. Here's the code:

void MatrixMultiplication(float * M, float * N, float * P, int Width, int test)
{
        int size = Width * Width * sizeof(float);

        #if defined size
        if (test)
        {
                printf("Should be equal to %d.  Int size:%d", Width * Width * sizeof(float), size);
        }
        #endif
}

int main (int argc, char ** argv)
{

        // Omitted for brevity...

        int test = 1;                                                   

        // Omitted for brevity...

        MatrixMultiplication(hostM, hostN, reference, atoi(matrix_id), test);

        // Omitted for brevity...

}

I'm compiling with this command:

nvcc -I/home/sbu/NVIDIA_GPU_Computing_SDK/C/common/inc -L/home/sbu/NVIDIA_GPU_Computing_SDK/C/lib -o matrixmul matrixmul.cu -lcutil_x86_64

Is there something about nvcc that makes this sort of "#if defined" command not work? I've used this sort of syntax before in native C and C++ code using gcc and it worked just fine.

Any illumination on this issue would be great!

Here is the full code on Pastebin: http://pastebin.com/SusnpgFc


Solution

  • It does not, even though "size" has been declared on the previous line

    The #if defined size tests whether a preprocessor macro with that name has been defined, it does not check whether a variable with that name is declared in the program.

    It will only evaluate to true if you have a #define size or #define size some replacement tokens before that.