#pragma unroll issue on linux


How come this doesn’t work on linux:

__global__ void  kernel(int* p)
  #define n 3
  #pragma unroll n
  for (int i = 0; i < n; ++i)
   p[threadIdx.x] = 0;

I get following warning only on linux:
test.cu(8): warning: extra characters in the unroll pragma (expected a single positive integer), ignoring pragma for this loop


I don’t think you need the “n”.

I think it should just be :

#pragma unroll

It expects a single positive integer. Getting preprocessor macros to expand within other (implementation-specific) compiler directives is a non-standard behavior, and will vary by implementation.

Ok, I didn’t know that. Thanks.

I am not even sure this is invoking implementation-defined behavior. The C/C++ preprocessor works in multiple phases as defined by the relevant language standards; phase ordering may preclude the desired expansion (that is: #pragma is applied before substitution of #define symbols). I am not enough of a language lawyer to know off the top of my head, and too lazy to dig up my copy of the standard.