Does thrust container aumatically initialize every element with 0? (original) (raw)

For example:

  1. thrust::host_vector h_vec(1000);
  2. thrust::device_vector d_vec(1000);
  3. thrust::device_vector d_vec;
    d_vec.resize(1000);

Is the above elements in each example all 0?
If so, how to resize without initialization for better performance?

Thrust vectors default-initialize elements, just like std::vector.

You could use RMM’s rmm::device_uvector which does not initilialize its content.

Alternatively, CCCL has cuda::experimental::uninitialized_buffer , which is still an experimental feature as the name suggests.

another option is to use an ordinary device allocation e.g. via cudaMalloc(), and then use a thrust::device_ptr with it. The thrust device pointer can be used in thrust algorithm invocations in a conceptually similar manner to container iterators, but it will not be usable e.g. with .resize() or such container manipulations. Here is an example.