I am using CUDA. I have the following class on host:
class Particle{
public:
float x;
float v;
// several other variables
}
Then I have a vector of particles
vector <Particle> p_all(512);
On the GPU, I want to operate on an array of all x's (taken from all the Particles), and want to copy the data from the Particles array into a float array on device. I have a hunch that cudaMemcpy can be used, and I tried the following code, but it gives invalid pitch error.
cudaMalloc( (void**) &pos_dev, sizeof(float)*512);
cudaMemcpy2D( (void*) &pos_dev, sizeof(float), (void*)&p_all[0].x, sizeof(Particle), sizeof(Particle), 512*sizeof(float), cudaMemcpyHostToDevice);
Is it at all possible to do so? Of course, the backup solution is to create an array of x's using a for loop and then copy it to the device. But I am looking for a more efficient solution.
Thanks.
FULL CODE BELOW.
#include <cuda_runtime.h>
#include <iostream>
#include <vector>
using namespace std;
// This will output the proper error string when calling cudaGetLastError
void getLastCudaError(string s=""){
string errMessage = s;
cudaError_t err = cudaGetLastError();
if( err != cudaSuccess){
cerr << __FILE__ << "(" << __LINE__ << ") : Last Cuda Error - " << errMessage
<< " (" << int(err) << "): " << cudaGetErrorString(err) << ".\n";
exit(-1);
}
}
class Particle{
public:
float x;
float v;
int a;
char c;
short b;
Particle(){
a=1988; c='a'; v=5.56; x=1810; b=1.66;
}
};
template <class T>
void printVec(vector <T> &v, string name = "v"){
cout << name << " = ";
for (int i=0; i<v.size(); ++i) cout << v[i] << " " ;
cout << '\n';
}
int main(){
const int N = 512;
vector <float> pos(N,5);
vector <Particle> p_all(N);
float * pos_dev;
float * vel_dev;
cudaMalloc( (void**) &pos_dev, sizeof(float)*N);
printVec(pos, "pos");
cudaMemcpy2D( (void*) &pos_dev, sizeof(float), (void*)&(p_all[0].x), sizeof(Particle), sizeof(float), N, cudaMemcpyHostToDevice);
getLastCudaError("HtoD");
cudaMemcpy( (void*) &pos[0], (void*)&pos_dev, N*sizeof(float), cudaMemcpyDeviceToHost);
getLastCudaError("DtoH");
printVec(pos, "pos_new");
return 0;
}