简体   繁体   中英

CUDA: How would I pass a host stored vector to a Device side int array?

I'm currently working with CUDA to process lists of co-ordinates. I'm thus reading coordinates from a text file to the host and passing those coordinates into an array on the GPU.

Unfortunately I do not know in advanced how many coordinates are contained in each text file. I thus use a vector to store the coordinates host side - as non-vector array sizes can not be allocated dynamically.

As declaring device variables is done on the fly however, I was wondering if there was a way I could pass the values of my host vector into a device side float array?

Passing data from vector can be done the same way as an array. Values in a std::vector are stored in contiguous memory.

Is it safe to assume that STL vector storage is always contiguous?

As a side note, most scientific storage methods I've come into contact will define up front how many coordinates they are. If you create this file, I would highly suggest doing so as it will be faster than using std::vector which has to dynamically re-allocate and copy memory when the capacity grows.

Exactly as Christian Sarofeen mentions, you can access raw vector data directly, more info here . The nicest way, in case you are on C++11, is to use data() function for this purpose.

On the other hand, I am a bit surprised by this statement of yours:

as non-vector array sizes can not be allocated dynamically

I have to disagree, this should work for you just fine:

int x;
cin >> x;
int* dynamicArray = new int[x];
// Do computations on dynamicArray,
// possibly copy to GPU memory.
// Then deallocate.
delete [] dynamicArray;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM