 # Vectors in CUSA? how to store dynamic matrix?

Hi,

I need to use 3d and 2d matrices in my code. I have them in form of vectors. I don’t know if it’s possible to copy them as vector to GPU and use them?

or since they have different random sizes in different dimension , if I want to change them to arrays I need to keep the size of each dimension. for example the 3D matrix in two matrices of 2D and 1D.

what is the efficient way of storing a 3D matrix with of course sizes for each dimension?

Thanks so much for your help!

You can do a direct copy from host to device, as long as you know the size of the vector.

As for the efficient way of storing 3D matrices, that would be dependent on your access pattern. In what way are you going to access the elements?

so I know size of the vectors, with vector.size(). but I don’t know how to copy it to device ?

and more about the data, I read them from arbitrary number of files, and each file contains random number of lines, each line with 50 numbers.

I want to do make a 3D matrix or 3D vector with these data. later I’m going to do calculations on them.

You can get the pointer to the array using vector.front().

``````Type *dev_ptr;

vector<Type> matrix;

int size = matrix.size() * sizeof(Type);

cudaMalloc((void**)&dev_ptr, size);

cudaMemcpy(dev_ptr, matrix.front(), size, cudaMemcpyHostToDevice);
``````

But seriously, you shouldn’t use vector to do this unless the size of a specific matrix could change during runtime, which I believe is not what you need.

Are you reading from text files? Better do it with binary files; that’s much easier and faster. Of course, if you want the data on your GPU to be stored linearly, there’s nothing you’ll need to do apart from the direct copying shown above. I believe this would be the case for you, if your calculations on those matrices aren’t exactly peculiar.

Thanks again!

so I did the same as you said as below and I get this error

error: no suitable conversion function from “std::vector<float, std::allocator>” to “const void *” exists

``````vector<vector<float> > ref_list;

float *dev_ptr;

int size = ref_list.size()*ref_list.size() * sizeof(float);

cudaMalloc((void**)&dev_ptr, size);

cudaMemcpy(dev_ptr, ref_list.front(), size, cudaMemcpyHostToDevice);
``````

and about using vector, I thought that’s clean. if I want to use ***p for my matrix and then keep their sizes somewhere, and copying all to gpu. I thought it’s not a good idea.

and I’m reading from text, I dont know what is binary file. if it influences only the first loading time, It’s not really a big deal now.

You can also check Thrust,it was built for cases like this

Sorry, I was using an online reference and didn’t look at it carefully enough. The code I gave you should (probably) have been

``````cudaMemcpy(dev_ptr, &matrix.front(), size, cudaMemcpyHostToDevice);
``````

If this doesn’t work try some explicit casting.

But the vector structure you are using is a vector of vectors. You can’t do direct copying like that. You incur even more overhead this way.

``````int offset = 0;

int subsize = ref_list.size() * sizeof(float);

for(int i =0; i<ref_list.size(); i++)

cudaMemcpy(dev_ptr+offset, &ref_list.front(), subsize, cudaMemcpyHostToDevice);
``````