cancel
Showing results for 
Search instead for 
Did you mean: 

Archives Discussions

edwen
Journeyman III

Problem with arrays with dynamic size

In my kernel function, I need to build an array. However, the size of this array is passed as a parameter from the host code. I could not name a constant for it since this value will be input by the users. And the code is somehow like:

__kernel void valuation(__global float *d_v, const int num)

{

...

const int num_ = num;

float L_c[num_];

...

}

However, this doesn't work and it always gives a programbuild error. Any suggestions would be appriciated. Thanks,

 

0 Likes
12 Replies
nou
Exemplar

OpenCL do not support variable arrays in kernel. size must be know at compile time.

0 Likes
HarryH
Journeyman III

Wrong post

0 Likes

edwen,
If the pointer size is known at runtime, create a global memory object and pass that in instead of using private memory.
0 Likes

Hi!

I have a similar problem that should have a simple solution. I too want to be able to use kernels with varying width arrays, though I know the size when I complie the kernel. I had an idea to simply use a #define in my main code so that I only have to change the code in one place while wroking on it. However, the kernel will still not comiple. I find this strange as a #define is constant. 

What I have done is simply #include "GlobalConstants.h" in the kernel. It does not complain about not finding the h-file. In the h-file I have "#define array_width 32". Still. using array_width in the kernel does not work as it is not viewed as constant. I can not see why this does not work. Am I doing something wrong or is there a reason why this approach will not work either?

Thanks!

Ian

0 Likes

do you specify include dir via -I parameter in clBuildProgram()? i pass whole kernell as #include file and my kernel which i pass to clCreateProgramFromSource is only one #include "some_file.cl"

whole source code is then in include file.

another option is pass -D array_width=32 to clBuildProgram()

0 Likes

Please provide a test case to replicate the problem.

0 Likes

Hi again!

Thanks nou! I didn't know I could do that. A tip for anyone else who wants to try the same:

wrting:

"-D array_width=%d\0"   works

"-D array_width = %d\0"    does not work.

I'm using sprintf, where %d is a #define macro so that I can simply change the macro when performing some benchmarktests. 

 

I still have a problem though...

const uint filter_size = array_width / 2;

__local float4 filter[filter_size];

does not work. When compiling I recieve:

"error: expression must have a constant value" and a " ^ " pointing at filter_size. I find this strange as everything should be seen as constant, or am I missing something?

 

Again, thank you for your help!

Cheers!

 

(sorry for the code below, I can't seem to remove it once I pasted it in)

"-D array_width=%d\0"

0 Likes

const do not make variable constant expression. you can compute value of const variable at run time so compiler can't consider value of filter size as constant expression.

even const int filter_size = 16; will not work.

try #define filter_size (array_width/2)

that bracket are just for sure.

0 Likes

Wohoo!

It worked! Thanks a lot! Its small things like this that make OpenCL more tedious than CUDA for me. But I'm set on learning doing things the AMD OpenCL way. 

 

Again, thank you for your help. Much appreciated. 

0 Likes

Oh, and a quick tip for anyone who wants to write several preprocessor options such as several constant variables. write:

"-D array_size=16 -D filter_size=32" and so on. No commas or new lines. 

0 Likes

wainwright,
If you have issues that you think should be improved, please provide a quick test case and a reason why you think it should be fixed and we will look into the issue. If we decide that your issue is valid, we will fix it in our upcoming release.
0 Likes

MicahVillmow,

OK! Thanks. Will do if I come to think of anything that isn't simply a cause of me being a novice.

/ Wainwright

 

0 Likes