I have a simple particle based rigid body dynamics code, each rigid body consists of many small particles with mass, position, velocity, etc... Now I want to port this cpu code to gpu.
For the structure, I chose to use a pointer array int** d_rigid_particle_indices
each of its element points to an array of its child particles indices(since I used radix sort to sort particles every frame, the indices will change every frame. So I will use a double buffering scheme -> int** d_rigid_particle_indices[2];
)
Here is the data structure & function to copy data from host to device:
typedef struct{
// Particle based rigid body dynamics
float4* d_rb_pos;
float4* d_rb_vel;
float4* d_rb_angular_velocity;
float4* d_rb_linear_momentum;
float4* d_rb_angular_momentum;
float4* d_rb_force;
float4* d_rb_torque;
float4* d_rb_quaternion;
float* d_rb_rotation_matrix;
float* d_rb_inv_inertia_local;
float* d_rb_inv_inertia_world;
float* d_rb_mass;
int** d_rigid_particle_indices[2];
}d_RigidBody;
void CopyRigidBodyDataHostToDevice(d_RigidBody& d_rb,std::vector<RigidBody*>& rigidbodies_h){
const int num_rigid_bodies = rigidbodies_h.size();
// allocate host mem for top level pointer array
int** h_rigid_bodies = (int**)malloc(num_rigid_bodies*sizeof(int*));
for (int i = 0; i < num_rigid_bodies; ++i)
h_rigid_bodies[i] = NULL;
// allocate host&device mem for child particle index array
for (int i = 0; i < num_rigid_bodies; ++i)
{
RigidBody* rb = rigidbodies_h[i];
if (rb)
{
std::vector<int>& rp_indices = rb->getRigidBodyParticleIndicesArray();
const int num_particles = rp_indices.size();
int* temp_rp_indices = (int*)malloc(num_particles*sizeof(int));
for (int j = 0; j < num_particles; ++j)
{
temp_rp_indices[j] = rp_indices[j];
}
int * temp_ptr;
CUDA_SAFE_CALL( cudaMalloc( ( (void**)&temp_ptr, num_particles*sizeof(int) ) ) ); // allocate device mem for child pointer
h_rigid_bodies[i] = temp_ptr;
temp_ptr = NULL;
for (int k = 0; k < num_particles; ++k)
{
// copy RigidParticleIndices per rigid body from host to device
CUDA_SAFE_CALL( cudaMemcpy(h_rigid_bodies[i], temp_rp_indices, num_particles*sizeof(int), cudaMemcpyHostToDevice) ); //h_rigid_bodies[i] stores the device mem pointer
}
if (temp_rp_indices)
free(temp_rp_indices);
}
}
// allocate device mem for top level pointer array
CUDA_SAFE_CALL( cudaMalloc( (void**)&(d_rb.d_rigid_particle_indices[0]), num_rigid_bodies*sizeof(int*) ) ); // pointer array
CUDA_SAFE_CALL( cudaMalloc( (void**)&(d_rb.d_rigid_particle_indices[1]), num_rigid_bodies*sizeof(int*) ) ); // pointer array
// now we're ready to copy top level pointer array from host to device
CUDA_SAFE_CALL( cudaMemcpy(d_rb.d_rigid_particle_indices[0], h_rigid_bodies, num_rigid_bodies*sizeof(int*), cudaMemcpyHostToDevice) );
// clean up
if (h_rigid_bodies)
{
for (int i = 0; i < num_rigid_bodies; ++i)
{
if (h_rigid_bodies[i])
{
free(h_rigid_bodies[i]);
}
}
free(h_rigid_bodies);
}
}
After compiling, I got an error "no instance of overloaded function "cudaMalloc" matches the argument list argument types are: (unsigned int)" for this line of code:
CUDA_SAFE_CALL( cudaMalloc( ( (void**)&temp_ptr, num_particles*sizeof(int) ) ) ); // allocate device mem for child pointer.
Any advice?
Thanks.