Problem with "shared" dinamic arrays

I write kernel and I have one problem with “shared” dinamic arraysl.

__global__ void calculate(int size, int N, float* a){
	__shared__ float arr[N*2];

It returns error:
Exception in thread "main" Could not create .ptx file: error: expression must have a constant value

Please, help me. How I can create this array? The value of N defines in Java Code.


You can add this as a constant by passing it as a ‘defined’ value to the NVCC (or the KernelLauncher, which you may be using) - For example, you may call this…

k = KernelLauncher.create(fileName, functionName, true, "-DMY_SIZE=123"); // Add -D... parameter here

and then use MY_SIZE in the kernel like this:

extern "C"
__global__ void calculate(int size, float* a){
    __shared__ float arr[MY_SIZE*2];

Just from the tip of my head, if it does not work, I’ll have another look at this any possibly post an example.


Thank you very much, Marco!
It resolved this issue. And you teached me how to create constants :wink:

You can also create constants with the “const” or “constant” keywords, but you cannot declare arrays with them!