Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to free device_vector<int>

Tags:

cuda

gpu

thrust

I allocated some space using thrust device vector as follows:

thrust::device_vector<int> s(10000000000);

How do i free this space explicitly and correctly?

like image 741
Programmer Avatar asked Dec 01 '22 05:12

Programmer


2 Answers

device_vector deallocates the storage associated when it goes out of scope, just like any standard c++ container.

If you'd like to deallocate any Thrust vector's storage manually during its lifetime, you can do so using the following recipe:

// empty the vector
vec.clear();

// deallocate any capacity which may currently be associated with vec
vec.shrink_to_fit();

The swap trick mentioned in Roger Dahl's answer should also work.

like image 146
Jared Hoberock Avatar answered Dec 05 '22 09:12

Jared Hoberock


clear() sets the size of the vector to 0, but may not release the associated memory. The standard way to release the memory with STL is to swap the vector with an empty vector. It should work for Thrust as well.

v.clear();
device_vector<T>().swap(v);
like image 31
Roger Dahl Avatar answered Dec 05 '22 09:12

Roger Dahl