I think it happens because you are trying to access a value of a vector stored on the device from the host. Run the same operation inside a kernel or pass the top_idxs to the cpu and tell me what happens please.