3

Why does the following snippet not work with CUDA (both 3.2 and 4.0)?

#define NUM_BLOCKS 16

// lots of code.

dim3 dimBlock(NUM_BLOCKS, NUM_BLOCKS);

but this,

dim3 dimBlock(16, 16);

does?

I keep getting a error : expected a ")" and error : expected an expression. What am I missing?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Kristian D'Amato
  • 3,996
  • 9
  • 45
  • 69

3 Answers3

9

Are you certain you didn't write

#define NUM_BLOCKS 16;

(note the semicolon at the end)?

I get exactly the errors you described, when the erroneus semicolon is there.

CygnusX1
  • 20,968
  • 5
  • 65
  • 109
  • Hell, that was it! I was scrolling through pages and pages of preprocessed code, only to be laid low by a semicolon! Me > doofus. – Kristian D'Amato Mar 06 '11 at 12:58
  • @Kristian: See...As I said if you had used `const int` or `enum` instead of `#define`, you wouldn't have faced this problem :D – Nawaz Mar 06 '11 at 13:02
2

That is strange. It may be because of the usual problem with macros. If you know, macros don't respect scope. It may be that the same macro is defined elsewhere, but differently.

Why don't you use const int or enum instead of macro?

Do you know this: C++ - enum vs. const vs. #define ??

Community
  • 1
  • 1
Nawaz
  • 353,942
  • 115
  • 666
  • 851
  • Yeah, but in principle at least it should work, which is why I find it strange. – Kristian D'Amato Mar 06 '11 at 12:32
  • @Kristian: As I said, if there is the usual problem, then how will it work? Making sure that the problem doesn't exist, is difficult with macros, that is why `const int` or `enum` is preferred. – Nawaz Mar 06 '11 at 12:33
  • What do you mean Nawaz? I tried renaming the macro to an obviously user-created one. I don't see why it shouldn't work. Anyway, I'll try and get to the bottom of this later today... – Kristian D'Amato Mar 06 '11 at 12:38
1

One way to find out is to get your compiler driver to stop after the preprocessing stage so that you can see what has been generated. This will show you what the preprocessor substituted and therefore gives you something to search for.

The option is -E if you are using gcc and /E for MSVC.

For the nvcc compiler driver a typical command would be

nvcc -E file.cu -o file.cup
Troubadour
  • 13,334
  • 2
  • 38
  • 57
  • I suspect it's something to do with how NVCC sees preprocessor commands, since this is a `.cu` file? – Kristian D'Amato Mar 06 '11 at 12:33
  • @Kristian D'Amato: I'm not familiar with CUDA but I just had a quick look at the nvcc manual and it says it supports the `-E` option to generate preprocessed source from a .cu file. I'll edit my answer with a sample command line. – Troubadour Mar 06 '11 at 12:45