gpt4 book ai didi

当我尝试将指针数组复制到设备时出现 cudaMalloc 错误

转载 作者:太空宇宙 更新时间:2023-11-04 01:31:55 25 4
gpt4 key购买 nike

我有一个简单的基于粒子的刚体动力学代码,每个刚体由许多具有质量、位置、速度等的小粒子组成......现在我想将这个 cpu 代码移植到 gpu。

对于结构,我选择使用指针数组 int** d_rigid_particle_indices 它的每个元素都指向其子粒子索引的数组(因为我使用基数排序对每一帧的粒子进行排序,索引会在每一帧发生变化。所以我将使用双缓冲方案 -> int** d_rigid_particle_indices[2];)

这是从主机复制数据到设备的数据结构和函数:

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

编译后,对于这行代码,我得到一个错误“没有重载函数“cudaMalloc”的实例匹配参数列表参数类型是:(unsigned int)”:

CUDA_SAFE_CALL( cudaMalloc( ( (void**)&temp_ptr, num_particles*sizeof(int) ) ) );       // allocate device mem for child pointer.

有什么建议吗?

谢谢。

最佳答案

一对托槽用得太多了。因此,编译器只会看到一个参数被传递给 cudaMalloc 调用。将行更改为

CUDA_SAFE_CALL( cudaMalloc( (void**)&temp_ptr, num_particles*sizeof(int) ) );

而且有效。

关于当我尝试将指针数组复制到设备时出现 cudaMalloc 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21136849/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com