Using shared memory dynamically

Hi all, I am very new in CUDA. I want to use Shared Memory and the array I want to add to share memory has a size “int a” (not constant, can change from 1 initial values to another). So my question is: can I allocate shared memory dynamically, like in c++ with the help of new function? Should I delete it at the end of my global function?

Can I use std::vector?

Yes, you can allocate shared memory dynamically. You do it by using the “extern” keyword while not specifying an array size on the shared declaration in your kernel code:

extern __shared__ int my_shared_data[];

and then specify the size dynamically at kernel launch time, by passing the desired size in bytes of the shared memory array as the 3rd kernel configuration parameter:

my_kernel<<<gridSize, blockSize, sharedSize>>>(...);

You may also want to read the programming guide:

and there are many examples of this as well:

std::vector is not usable in CUDA device code.

Thanks for the reply! So I cannot use std::vector anywhere within device kernel?
That is sad-((
Also can I use my own structure in device kernel? Something like:

struct Pixel{
      float p;

Yes, you can use your structure. CUDA supports most aspects of C and C++, including objects. Most items from the standard template library are not usable in device code.