malloc of one kernel in another kernel Memory allocated in one kernel can be accessed in another ker

Is it possible to access memory, which is allocated in one kernel in another kernel function.

for example: some thing like this:

__global__ void testmalloc()

{

float *p1, *p2;

p1 = malloc(10*sizeof(float));

}

__global__ void testm()

{

int i;

for(i=0; i<10; i++)

p1[i] = 10.0 + i;

}

main()

{

testmalloc<<<1,1>>>();

testm<<<1,1>>>();

}

In which method this can be achieved?

Does Your code even compile? Beyond that isn’t it better to allocate global memory from within the host code and then pass pointer to it to both kernels?

Regards,

MK

I was not aware you can allocate memory from kernels, if it is I wonder what happens since every thread will try to allocate the same array.

As far as I know using ‘malloc’ in kernel refers to dynamic allocation of local memory (or the one for a single thread only in other words). But, please, correct me if I’m wrong.

Regards,

MK

And what would be the benefit of using malloc inside a kernel vs declaring like this float p[10];
Did someone ever use malloc inside kernel successful? In the tutorials I read the examples never had malloc inside a kernel. This is why it just seems out of places.

In the example given in the first post the p1 is allocated in one kernel and then used in another so the intent is to use it as a variable in the global memory.

pasoleatis: You are absolutely right. Calling ‘malloc’ for every thread can cause a great drop in performance of Your application. It is possible to use it (as like as the ‘new’ operator, with device compute capability >= 2.0), but not very efficient, I think.