std::vector to array in CUDA
cuda, gpgpu
Solution
In a word, no there isn't. The CUDA API doesn't support deep copying and also doesn't know anything about `std::vector` either. If you insist on having a vector of vectors as a host source, it will require doing something like this:
int *d_information;
cudaMalloc((void**)&d_information, sizeof(int)*size);
int *dst = d_information;
for (std::vector<std::vector<int> >::iterator it = information.begin() ; it != information.end(); ++it) {
int *src = &((*it)[0]);
size_t sz = it->size();
cudaMemcpy(dst, src, sizeof(int)*sz, cudaMemcpyHostToDevice);
dst += sz;
}
[disclaimer: written in browser, not compiled or tested. Use at own risk]
This would copy the host memory to an allocation in GPU linear memory, requiring one copy for each vector. If the vector of vectors is a "jagged" array, you will want to store an indexing somewhere for the GPU to use as well.
Problem
Is there a way to convert a 2D vector into an array to be able to use it in CUDA kernels? It is declared as: ``` vector<vector<int>> information; ``` I want to cudaMalloc and copy from host to device, what would be the best way to do it? ``` int *d_information; cudaMalloc((void**)&d_information, sizeof(int)*size); cudaMemcpy(d_information, information, sizeof(int)*size, cudaMemcpyHostToDevice); ```