ksm001
ksm001

Reputation: 4022

Is it possible to deallocate memory for the N last elements of a thrust::device_vector without using resize?

I'm using a device_vector in order to store information about an array of user input data. This information is necessary in order to speed things up when I call the second kernel, which runs the main algorithm.

After the end of the first kernel the device_vector will contain only a small amount of important elements. So for example if initially the device_vector has a size of 10000 in the end only 10-20 elements will be describing the user input data.

What I'm doing right now is, I use the function thrust::remove in order to remove the unnecessary values, however if I understand correctly, this function doesn't really remove the unnecessary values, but returns an iterator to the last necessary value.

For example suppose that we have this array:

0 0 0 0 0 5 0 0 8 0 0 0 0 13 0 0

if I call remove for value 0, it will do something like this:

5 8 13 0 0 0 0 0 0 0 0 0 0 0 0 0

and it will return an end iterator to the last necessary element, which in my case is 13.

However, I would like to use this information for the second kernel, and if I understand correctly, the unnecessary elements will still be allocated, so I would be using memory that I don't need.

My question is, can I, by having the end iterator to the last necessary element, remove the unnecessary elements without using resize?

resize isn't very efficient because it removes all the elements, and allocates memory for the necessary elements as well, but the memory is already allocated so I don't see the point of doing this.

Upvotes: 2

Views: 242

Answers (1)

Jared Hoberock
Jared Hoberock

Reputation: 11416

device_vector::resize doesn't necessarily cause a reallocation to occur upon a shrink event. It is allowed to do so, but as of Thrust 1.8, the current implementation does not.

For your example, you should be able to simply call resize(3) without worrying about a reallocation occurring.

Upvotes: 1

Related Questions