Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

allocating shared memory

i am trying to allocate shared memory by using a constant parameter but getting an error. my kernel looks like this:

__global__ void Kernel(const int count) {     __shared__ int a[count]; } 

and i am getting an error saying

error: expression must have a constant value

count is const! Why am I getting this error? And how can I get around this?

like image 230
lina Avatar asked Apr 03 '11 17:04

lina


People also ask

How is shared memory allocated?

Shared memory is allocated per thread block, so all threads in the block have access to the same shared memory. Threads can access data in shared memory loaded from global memory by other threads within the same thread block.

What is meant by allocation of memory?

Memory allocation is the process of reserving a partial or complete portion of computer memory for the execution of programs and processes. Memory allocation is achieved through a process known as memory management.

What are the two types of memory allocation?

There are two types of memory allocations. Static and dynamic.

How do we allocate memory?

There are two basic types of memory allocation: When you declare a variable or an instance of a structure or class. The memory for that object is allocated by the operating system. The name you declare for the object can then be used to access that block of memory.


2 Answers

CUDA supports dynamic shared memory allocation. If you define the kernel like this:

__global__ void Kernel(const int count) {     extern __shared__ int a[]; } 

and then pass the number of bytes required as the the third argument of the kernel launch

Kernel<<< gridDim, blockDim, a_size >>>(count) 

then it can be sized at run time. Be aware that the runtime only supports a single dynamically declared allocation per block. If you need more, you will need to use pointers to offsets within that single allocation. Also be aware when using pointers that shared memory uses 32 bit words, and all allocations must be 32 bit word aligned, irrespective of the type of the shared memory allocation.

like image 84
talonmies Avatar answered Sep 19 '22 17:09

talonmies


const doesn't mean "constant", it means "read-only".

A constant expression is something whose value is known to the compiler at compile-time.

like image 21
Oliver Charlesworth Avatar answered Sep 19 '22 17:09

Oliver Charlesworth