0

For instance:

thrust::device_vector<float> vec(...);
thrust::device_vector<float>::iterator i = vec.begin();
vec.resize(...);    // vec may get reallocated and moved in memory here

Now, has vec.begin() also updated and still point validly to the start of vec?

talonmies
  • 70,661
  • 34
  • 192
  • 269
mchen
  • 9,808
  • 17
  • 72
  • 125
  • 5
    @EdS. Well, theoretically that doesn't say anything about `thrust::device_vector`, but practically it will probably indeed behave exactly like a `std::vector` in this regard. – Christian Rau May 05 '13 at 22:44
  • After a shrink yes; after a grow, no. – Pieter Geerkens May 05 '13 at 22:44
  • @PieterGeerkens: That almost certainly isn't correct. The CUDA runtime library lacks a `realloc` function, so without checking the code, I would be incredibly surprised if `resize` isn't a `malloc`+ `memcpy`+ `free` sequence in thrust. In that case iterators will be invalidated. – talonmies May 06 '13 at 05:14
  • @talonmies: Except that the STL documentation **requires** what I stated to be true. – Pieter Geerkens May 06 '13 at 05:28
  • 2
    @PieterGeerkens: Who is talking about the STL? This question is specifically asking about thrust, which is an *NVIDIA CUDA* template library. Some of its functionality is modelled after the STL, but it otherwise is completely different. – talonmies May 06 '13 at 05:31
  • @PieterGeerkens The **C++ standard** (which I assume you meant with *"STL documentation"*, otherwise it would be even more out of place) can require what it wants, that doesn't create a conformance error in any third-party library completely unrelated to the C++ standard library and its rules. – Christian Rau May 06 '13 at 08:28
  • @talonmies I would still wonder if *Thrust* does a reallocation even if the new size is smaller than the old one (neither do I know, though). You don't neccessarily need a `realloc` (`std::vector` doesn't have that either), you can just leave the memory as is and adjust the vector's size indicator appropriately. – Christian Rau May 06 '13 at 08:30
  • @talonmies Just looked into the source and you'll indeed be surprised ;) – Christian Rau May 06 '13 at 08:54
  • 1
    @ChristianRau: Are you going to share? I spend 30 minutes last night looking through the Thrust source code files, but couldn't find that for the vector resize method. (I'm a C & C# programmer mostly, so the template structure is new to me.) – Pieter Geerkens May 06 '13 at 21:37
  • 1
    @PieterGeerkens Well, if one finally managed to crawl through all the indirections, it indeed behaves like `std::vector`, reallocating on growth and just decreasing the size counter on shrinkage, contrary to what *talonmies* wrote and in line with what you wrote (but without the wrong implication that the C++ standard alone is any proof of the behaviour of *Thrust*, of course). – Christian Rau May 06 '13 at 22:57
  • @ChristianRau: Merci beaucoup. – Pieter Geerkens May 06 '13 at 23:14

1 Answers1

3

Upon resizing, if the vector originally did not have enough space, your iterator will be invalidated, so you'd have to recall vec.begin() to get a new, valid iterator.

user123
  • 8,970
  • 2
  • 31
  • 52