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.

6
  • 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 stackoverflow.com/questions/8049657/… Commented Jun 18, 2021 at 7:28
  • 3
    std::span (c++20) might be a better "container" for this. Commented Jun 18, 2021 at 7: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). Commented Jun 18, 2021 at 7:40
  • 1
    stackoverflow.com/a/21918950/1863938 Commented Jun 18, 2021 at 7:48
  • 1
    In the latest versions of thrust, a thrust vector can be of a universal_vector type, which means that it is using a managed allocator. Commented Jun 18, 2021 at 15:13

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

so I guess the question is if I can define vectors in managed memory...
@KansaiRobot Perhaps by writing a custom allocator that calls cudaMallocManaged. I don't have experience with that though.
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.