GPU代码编写笔记
1. 内存拷贝
//重新排序points thrust::device_ptr<unsigned int> rank_ptr_points;//新建一个指针 allocThrustDevicePtr(&rank_ptr_points, state.numPoints);//给指针申请空间,大小为state.numPoints thrust::copy(state.pointsRankList, state.pointsRankList+state.numPoints, rank_ptr_points);//将state.pointsRankList中前state.numPoints个项复制到rank_ptr_points中 thrust::device_ptr<float3> d_points_ptr = thrust::device_pointer_cast(state.params.points);//新建一个指向state.params.points的指针,这里应该是指向的一个GPU内存 sortByKey( rank_ptr_points, d_points_ptr, state.numPoints);//用rank_ptr_points作为key,对d_points_ptr进行排序,元素数量为state.numPoints,排完序后rank_ptr_points和d_points_ptr都会变的有序 thrust::copy(d_points_ptr, d_points_ptr+state.numPoints, state.h_points);//将d_points_ptr拷贝回state.h_points,相当于将device拷贝回host
memcpy(pResult.points, state.h_points, state.numPoints*sizeof(float3));//将pResult.points中的内容拷贝到state.h_points中,大小为state.numPoints*sizeof(float3)