# Keep previously allocated memory on GPU

**URL:** <https://forums.developer.nvidia.com/t/keep-previously-allocated-memory-on-gpu/17486>\
**Category:** CUDA Programming and Performance\
**Created:** [July 2, 2010, 2:31pm UTC](https://forums.developer.nvidia.com/t/keep-previously-allocated-memory-on-gpu/17486 "2010-07-02T14:31:16Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Twz](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@Twz](https://forums.developer.nvidia.com/u/Twz)\
**Post date:** [July 2, 2010, 2:31pm UTC](https://forums.developer.nvidia.com/t/keep-previously-allocated-memory-on-gpu/17486/1 "2010-07-02T14:31:16Z")

</div>

Hi all,

I’m currently working on an application that calls N times the same CUDA function. Basically in this function I need to allocate several memory space (using cudaMalloc) to process my data. So I was wondering if there is a way to allocate and free this global memory only one time instead of doing it N times (I expect a performance increase doing that way).  
I have tried to store in a pointer the address of all global memories but without success.  
Does someone know how to do it ?

Thank you. :rolleyes:

Greg

---

<div class="post-metadata">

**Author:** ![Jimmy\_Pettersson](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@Jimmy\_Pettersson](https://forums.developer.nvidia.com/u/Jimmy_Pettersson)\
**Post date:** [July 2, 2010, 4:25pm UTC](https://forums.developer.nvidia.com/t/keep-previously-allocated-memory-on-gpu/17486/2 "2010-07-02T16:25:51Z")

</div>

Something similar to this won’t work?

```auto
cudaMalloc((void**)&d_big_ptr, N*size_of_arrays);

......

for(int i = 0; i < N; i++)

	my_kernel<<<blocks, threads>>>(d_big_ptr+i*array_length);

......

cudaFree( d_big_ptr );

```

Or am I missing something here?

---

<div class="post-metadata">

**Author:** ![Twz](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@Twz](https://forums.developer.nvidia.com/u/Twz)\
**Post date:** [July 2, 2010, 5:37pm UTC](https://forums.developer.nvidia.com/t/keep-previously-allocated-memory-on-gpu/17486/3 "2010-07-02T17:37:55Z")

</div>

Hi Jimmy,

Thank you for your reply. I’ll try to better explain my problem. In fact I m working on a dll that call a CUDA function (not the kernel), and I have to call this function N times ( I’m working with video so for each image I need to call it)

In this function I allocate memories using cudaMalloc, call my kernels and free memories.

I want to do the memory allocation and free on the GPU only one time. Basically the allocation will be done with the first image and the memory free will be done with the last. So for the other images I will just need to write on the previously allocated memory on the GPU.

My idea was to store the address of pointers in another pointer , something like this :

[codebox]void RenderingCudaHost(void\*\* &CUDAPtr, BYTE\* Source\_host, int sizeData)

{

BYTE \*Source ;

…

if(!CUDAPtr)

```
{

	CUDAPtr = (void**) malloc(10*sizeof(void*)) ;

	cudaMalloc((void**) &Source, sizeData);

	

	CUDAPtr[0] = (void*) Source;		

	

}

else

{

	Source = (BYTE*) CUDAPtr[0];

}

```

cudaMemcpy(Source, Source\_host, sizeData, cudaMemcpyHostToDevice) ;

…

}[/codebox]

Thanks a lot

---

<div class="post-metadata">

**Author:** ![Jimmy\_Pettersson](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@Jimmy\_Pettersson](https://forums.developer.nvidia.com/u/Jimmy_Pettersson)\
**Post date:** [July 2, 2010, 7:14pm UTC](https://forums.developer.nvidia.com/t/keep-previously-allocated-memory-on-gpu/17486/4 "2010-07-02T19:14:32Z")

</div>

Hey,

I think i understand what you are wanting to do now… You want to allocate the needed memory once when you first call “RenderingCudaHost” and then reuse that the next time you call RenderingCudaHost.

Unfortunately you can’t allcoate memory inside of “RenderingCudaHost” and expect it to be there later ( at least this goes for when writing normal C code).

Ex (this DOES NOT work):

```auto
void allocateAndFill(float* ptr, int N)

{

	int i;

	ptr = (float*)malloc(N*sizeof(float));

	

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

		ptr[i] = 1.0f;

}

void main(int argc, int agv[])

{

	float* ptr;

	int N = 5;

	int i;

	allocateAndFill(ptr,N);

	

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

		printf("\n %0.3f", ptr[i]); // print garbage

	printf("\n");

}

=> Garbage values

```

Im afraid you have allocate and free the memory at a higher up in your call stack.

So rather:

cudaMalloc((void\*\*) &Source, sizeData);

RenderingCudaHost(Source, Source\_host, int sizeData); // with some changes…

cudaFree(…);

free(…)

---

<div class="post-metadata">

**Author:** ![Twz](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@Twz](https://forums.developer.nvidia.com/u/Twz)\
**Post date:** [July 2, 2010, 11:03pm UTC](https://forums.developer.nvidia.com/t/keep-previously-allocated-memory-on-gpu/17486/5 "2010-07-02T23:03:18Z")

</div>

Yes that’s exactly my problem.

RenderingCudaHost is the function that is inside my dll and I call it for each image. If I allocated memory higher up in my call stack, I’ll have to create a new function in my dll that I call earlier than RenderingCudaHost. But I guess it won’t solve my problem. I’m afraid I’m a little bit stuck now. [External Image](http://hqnveipbwb20/public/style_emoticons/<#EMO_DIR#>/confused.gif "Image hosted on another site. Click to open in a new tab.") Any ideas ?

Thank you Jimmy

---

<div class="post-metadata">

**Author:** ![seibert](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/seibert/32/520688_2.png) [@seibert](https://forums.developer.nvidia.com/u/seibert)\
**Post date:** [July 2, 2010, 11:23pm UTC](https://forums.developer.nvidia.com/t/keep-previously-allocated-memory-on-gpu/17486/6 "2010-07-02T23:23:35Z")

</div>

I think any solution to this problem requires that either your API requires the user to allocate some opaque storage and pass it to your DLL in subsequent calls (Lots of APIs do this.) _or_ you do something terrible and make your code non-reentrant by storing such local data in a global variable. Clearly the first option is preferable. :)
