C++
--
Questions
--
Followers
Top Experts
Passing vector to cuda device
I'm trying to pass a vector to a Cuda enabled graphics card.
My somewhat lacking pointer understanding makes me unable to understand why it doesn't work.
The first example works, the second doesn't.
My somewhat lacking pointer understanding makes me unable to understand why it doesn't work.
The first example works, the second doesn't.
float a[num] = { 0 };
float* Ad;
size = num * sizeof(float);
cudaMalloc((void**)&Ad, size); //Allocate memory on graphics device
cudaMemcpy(Ad, a, size, cudaMemcpyHostToDevice); //Copy contents from host memory to device memory
vector<int> tmp;
for(int i = 0; i < 10; i++) tmp.push_back(i);
vector<int>* tmp2;
cudaMalloc((void**)&tmp2, tmp.size() * sizeof(int));
cudaMemcpy(tmp2, tmp, tmp.size() * sizeof(int), cudaMemcpyHostToDevice);
Compiler error: 'error: no suitable conversion function from "std::vector<int, std::allocator<int>>" to "const void *" exists'
Zero AI Policy
We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.
Try this :
vector<int> tmp;
for(int i = 0; i < 10; i++) tmp.push_back(i);
int* tmp2 = &tmp[0];
cudaMalloc((void**)&tmp2, tmp.size() * sizeof(int));
cudaMemcpy(tmp2, tmp, tmp.size() * sizeof(int), cudaMemcpyHostToDevice);
ASKER CERTIFIED SOLUTION
membership
Log in or create a free account to see answer.
Signing up is free and takes 30 seconds. No credit card required.
Compiles fine, thanks :)
Mind elaborating on that?
Mind elaborating on that?
membership
Log in or create a free account to see answer.
Signing up is free and takes 30 seconds. No credit card required.
One can always count on your answers being accurate and informative Infinity.
Thank you again :)
Thank you again :)






EARN REWARDS FOR ASKING, ANSWERING, AND MORE.
Earn free swag for participating on the platform.
>> One can always count on your answers being accurate and informative Infinity.
>>Β Thank you again :)
Glad to be of assistance :)
>>Β Thank you again :)
Glad to be of assistance :)
C++
--
Questions
--
Followers
Top Experts
C++ is an intermediate-level general-purpose programming language, not to be confused with C or C#. It was developed as a set of extensions to the C programming language to improve type-safety and add support for automatic resource management, object-orientation, generic programming, and exception handling, among other features.