Local memory allocation inside kernel with passed variable

Hi to everyone! I’m takeru and i’m a newbie on opencl and i’m trying to understand how can i transfer the content of an host variable to set another variable to the local memory on kernel.

This is an extract of the kernel:

#define LS 16

__kernel void matrix_mul(__global const int *A,
__global const int *B,
__global int *C,
int n, int k, int local_size
) {

__local int al[LS][LS];
__local int bl[LS][LS];

}

How can i set the variable “al” without use “LS”, that use “define” and use “local_size”?

Because i would to use the content of variable “local_size” and if I substitute “LS” with “local_size” i receive the error:

            Error: failed to build program executable
            <program source>:23:14: error: automatic variable qualified with an address space
            __local int Al[local_size][local_size];

And this is the extract of the host code:

err = clSetKernelArg(kernel, 0, sizeof(cl_mem), (void *)&a_mem_obj);
err |= clSetKernelArg(kernel, 1, sizeof(cl_mem), (void *)&b_mem_obj);
err |= clSetKernelArg(kernel, 2, sizeof(cl_mem), (void *)&c_mem_obj);
err |= clSetKernelArg(kernel, 3, sizeof(int), (void *)&n);
err |= clSetKernelArg(kernel, 4, sizeof(int), (void *)&k);
err |= clSetKernelArg(kernel, 5, sizeof(int), (void *)&ls);

Can someone help me? Thanks a lot!

You should define your al and bl to be kernel arguments and use clSetKernelArg in the host code to set the array size.

__kernel void matrix_mul(__global const int *A, 
__global const int *B, 
__global int *C,
int n, int k, int local_size, __local int* al, __local int*, __local int* bl
) {
...

...
}
clSetKernelArg(whatever, 6, sizeof(int) * local_size * local_size, NULL)
clSetKernelArg(whatever, 7, sizeof(int) * local_size * local_size, NULL)

Not being able to use 2D-arrays can be a pain, but you can try using a function that emulates them.

Salabar is right!
If u wanna use “local_size” to set your array al and bl, u have to define them to be the kernel arguments and use API clSetKernelArg at the host code to set array size.
U cannot use variables to set the array size at c99, the size must be a constant.
According to OpenCL spec, u cannot use 2 level pointer as a argument of kernel, so u are not able to use 2d-arrays as a argument.
Unless u use LS define to set array size as what u had done.