I'm running a C program where I call twice a cuda host function. I want to clean up the device memory between these 2 calls. Is there a way I can flush GPU device memory?? I'm on a Tesla M2050 with computing capability of 2.0
Asked
Active
Viewed 4,539 times
3
-
Could you be a little more precise with what you mean by "flush" or "clean up"? Do you mean you want to zero the memory, or set it to some other known non-initialised value, or do you mean something else? And do you want to "flush" just memory you have allocated, or do you mean the whole device? – talonmies Mar 01 '12 at 14:55
-
I'd like to zero the memory i've allocated, to "forget" the values that were stored there by the various kernels.Sorry for the vagueness of the question. – chemeng Mar 01 '12 at 15:03
2 Answers
5
If you only want to zero the memory, then cudaMemset
is probably the simplest way to do this. For example:
const int n = 10000000;
const int sz = sizeof(float) * n;
float *devicemem;
cudaMalloc((void **)&devicemem, sz);
kernel<<<...>>>(devicemem,....);
cudaMemset(devicemem, 0, sz); // zeros all the bytes in devicemem
kernel<<<...>>>(devicemem,....);
Note that the value cudaMemset
takes is a byte value, and all bytes in the specified range are set to that value, just like the standard C memset
. If you have a specific word value, then you will need to write your own memset kernel to assign the values.

talonmies
- 70,661
- 34
- 192
- 269
-
1To memset 16- and 32-bit values, you can call the driver API's cuMemset* family of functions, e.g. cuMemsetD16() or cuMemsetD32(), even from CUDA runtime applications. – ArchaeaSoftware Mar 02 '12 at 12:57
-
@talonmies: thanks for all your help with CUDA answers on SO. Can you contact me directly (First Initial Last Name At My Company Dot Com)? I have a question for you (sorry, SO doesn't support private messages). – harrism Mar 04 '12 at 10:26
1
If you are using Thrust vectors, then you can call thrust::fill()
on the vector you want to reset with the reset value you want.
thrust::device_vector< FooType > fooVec( FooSize );
kernelCall1<<< x, y >>>( /* Pass fooVec here */ );
// Reset memory of fooVec
thrust::fill( fooVec.begin(), fooVec.end(), FooDefaultValue );
kernelCall2<<< x, y >>>( /* Pass fooVec here */ );

Ashwin Nanjappa
- 76,204
- 83
- 211
- 292