Link to home
Create AccountLog in
C++

C++

--

Questions

--

Followers

Top Experts

Avatar of letharion
letharionπŸ‡ΈπŸ‡ͺ

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.
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'

Open in new window

Zero AI Policy

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


Avatar of Infinity08Infinity08πŸ‡§πŸ‡ͺ

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);

Open in new window


ASKER CERTIFIED SOLUTION
Avatar of Infinity08Infinity08πŸ‡§πŸ‡ͺ

Link to home
membership
Log in or create a free account to see answer.
Signing up is free and takes 30 seconds. No credit card required.
Create Account

Avatar of letharionletharionπŸ‡ΈπŸ‡ͺ

ASKER

Compiles fine, thanks :)
Mind elaborating on that?

SOLUTION
Avatar of Infinity08Infinity08πŸ‡§πŸ‡ͺ

Link to home
membership
Log in or create a free account to see answer.
Signing up is free and takes 30 seconds. No credit card required.

Avatar of letharionletharionπŸ‡ΈπŸ‡ͺ

ASKER

One can always count on your answers being accurate and informative Infinity.
Thank you again :)

Reward 1Reward 2Reward 3Reward 4Reward 5Reward 6

EARN REWARDS FOR ASKING, ANSWERING, AND MORE.

Earn free swag for participating on the platform.


Avatar of Infinity08Infinity08πŸ‡§πŸ‡ͺ

>> One can always count on your answers being accurate and informative Infinity.
>>Β Thank you again :)

Glad to be of assistance :)
C++

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.