3
votes

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

2
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
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

2 Answers

4
votes

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.

1
votes

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 */ );