0

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?

Alot
  • 129
  • 1
  • 7

2 Answers2

0

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.

Community
  • 1
  • 1
Christian Sarofeen
  • 2,202
  • 11
  • 18
0

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;
Community
  • 1
  • 1
Michal Hosala
  • 5,570
  • 1
  • 22
  • 49
  • I think what was meant, is you can't resize a GPU array (however this is still not true). You would just do what they do for std::vector already in GPU memory, but I didn't want to go there. – Christian Sarofeen Feb 21 '15 at 18:42
  • @ChristianSarofeen hm, so I guess the statement should rather be "as non-vector array sizes can not be _changed_ dynamically", correct? – Michal Hosala Feb 21 '15 at 18:51
  • I'm guessing, with reference to GPU? I honestly don't know, we probably need to wait until the user clarifies. – Christian Sarofeen Feb 21 '15 at 18:56
  • Apologies. I muddled some things while writing this. I'm busy sorting sets of coordinates into multiple kd trees (which im aiming to build on the GPU side). Each file I read does note the number of data elements in it, it just happens to change from file to file. As I cant guarantee the kd trees will be completely balanced I was worried about having to increase the array size in the middle of processing. Looking into it now, I can probably overcome that with math. My badly worded question was on how vectors are passed into int arrays or gpu based int array - which you've both answered in full. – Alot Feb 22 '15 at 21:58