Search code examples
cudathrust

Swapping CUDA Thrust device vectors without memory movements


If I have two cudaMalloced arrays, I can swap them without memory movements by simply swapping the related pointers.

If I have two CUDA Thrust device_vectors, say d_a and d_b, I can swap them by using a third temorary vector, say d_c, but this will require memory movements.

My question is: is there a way to swap CUDA Thrust device_vectors without memory movements?


Solution

  • Not that I am aware of.

    There is no constructor exposed which takes an existing device_ptr, and the underlying base vector within device_vector is private, so there is no way to dive in and perform pointer exchange yourself. Those would be the only ways I can think of to make this work without triggering the standard copy constructor.


    Edit to add that it appears this answer is wrong. It seems that recent (probably around thrust 1.6) changes have implemented an internal pointer exchange swap mechanism which can be called via device_vector.swap(). This bypasses the usual copy-constructor idiom for swap() and will not trigger memory transfers .