0

We know that if we have a vector like

std::vector<int> a_vector;

and this vector has elements, we can have the array of elements from

int *the_array= a_vector.data();

My question is: How can we do the opposite?

int the_array[5]={1,2,3,4,5};

std::vector<int> a_vector;

a_vector.data=the_array; //this does not seem to work

To clarify, I don't wish to copy the values , nor create an array *from * the values but have them in the same memory area.

Also , why am I asking this?

I would like to have this original array, in a managed (unified) area of memory

__device__ __managed__ int the_array[5];

and build the vector to point there.

KansaiRobot
  • 7,564
  • 11
  • 71
  • 150
  • 2
    You could probably do it with a custom allocator, but, why go to the effort? Plus you wouldn't be able to expand the vector. See https://stackoverflow.com/questions/8049657/stack-buffer-based-stl-allocator – Den-Jason Jun 18 '21 at 07:28
  • 3
    [`std::span`](https://en.cppreference.com/w/cpp/container/span) (c++20) might be a better "container" for this. – stefaanv Jun 18 '21 at 07:39
  • Firstly, `int *the_array={1,2,3,4,5}` is invalid. Second, if you want to control where the data managed by a `std::vector` resides, use a custom allocator. If you want to create an array of automatic storage duration, and then have a `std::vector` manage that array, my guess is that an allocator for the job will be a hairy job (lots of possibilities of introducing undefined behaviour in response to innocent operations by other code on the vector). – Peter Jun 18 '21 at 07:40
  • There is also `boost::static_vector`, if boost is your thing.... https://stackoverflow.com/a/21163028/1607937 – Den-Jason Jun 18 '21 at 07:47
  • 1
    https://stackoverflow.com/a/21918950/1863938 – parktomatomi Jun 18 '21 at 07:48
  • 1
    In the latest versions of thrust, a thrust vector can be of a [universal_vector type](https://developer.nvidia.com/blog/support-for-cuda-unified-memory-now-available-in-thrust/), which means that it is using a managed allocator. – Robert Crovella Jun 18 '21 at 15:13

2 Answers2

1

we can have the array of elements from

What we get from std::vector::data is a pointer to the first element.

My question is: How can we do the opposite?

a_vector.data=the_array; //this does not seem to work

No. Only way for a vector to take ownership of an array like that is from another vector of same type.

eerorika
  • 232,697
  • 12
  • 197
  • 326
1

Being able to construct a vector with already allocated memory would somewhat defeat the RAI design principle which is propably the reason it's isn't part of the std::vector API.

If all you need is a nice modern C++ interface to your allocated memory, span would be the right choice. You don't need to wait for NVIDIA to implement C++20, but could use the gsl-lite implementation of span which provides __host__ __device__ member functions such that you can use their span inside (or as arguments to) kernels as well.

If you want an actual container (not a view), which manages the memory (also allocates it), Thrust would be the obvious choice. While it was possible before to use their device_vector with a custom allocator, nowadays they provide a universal_vector for unified memory.

paleonix
  • 2,293
  • 1
  • 13
  • 29