[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Passing vector to cuda device

Posted on 2009-02-09
6
Medium Priority
?
3,965 Views
Last Modified: 2012-05-06
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

0
Comment
Question by:letharion
  • 4
  • 2
6 Comments
 
LVL 53

Expert Comment

by:Infinity08
ID: 23590481
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

0
 
LVL 53

Accepted Solution

by:
Infinity08 earned 2000 total points
ID: 23590506
Oops, misread, try this instead :
vector<int> tmp;
for(int i = 0; i < 10; i++) tmp.push_back(i);
int* tmp2 = 0;
cudaMalloc((void**)&tmp2, tmp.size() * sizeof(int));
cudaMemcpy(tmp2, &tmp[0], tmp.size() * sizeof(int), cudaMemcpyHostToDevice);

Open in new window

0
 
LVL 6

Author Comment

by:letharion
ID: 23590968
Compiles fine, thanks :)
Mind elaborating on that?
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 53

Assisted Solution

by:Infinity08
Infinity08 earned 2000 total points
ID: 23591347
A vector is guaranteed to contain its elements in contiguous memory locations. So, if you get the address of the first element of the vector, you can treat it as if it were an array. The address of the first element is simply &tmp[0]. The rest is the same as your float example without vectors.
0
 
LVL 6

Author Closing Comment

by:letharion
ID: 31544512
One can always count on your answers being accurate and informative Infinity.
Thank you again :)
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 23598511
>> One can always count on your answers being accurate and informative Infinity.
>> Thank you again :)

Glad to be of assistance :)
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
Suggested Courses

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question