(CUDA) thrust::device_vector中使用自定义类型时的引用

部署运行你感兴趣的模型镜像

示例:

thrust::device_vector < MyClass >dvc_vec;

MyClass& tmp = dvc_vec[blockIdx.x].operator MyClass();

若不采用此方法,而是

MyClass& tmp = dvc_vec[blockIdx.x];

则会报错:非常量引用的初始值必须为左值。

类似的,试图以传统方式访问成员变量也会报错,具体如下:

dvc_vec[0].func();// 报错:...没有成员"func"

dvc_vec[0].operator MyClass().func();// 不报错

您可能感兴趣的与本文相关的镜像

PyTorch 2.5

PyTorch 2.5

PyTorch
Cuda

PyTorch 是一个开源的 Python 机器学习库,基于 Torch 库,底层由 C++ 实现,应用于人工智能领域,如计算机视觉和自然语言处理

void ConvertMatToPointCloudThrust(cv::Mat& depth, pcl::PointCloud<pcl::PointXYZ>& cloud) { const float xInternal = 0.004916f * 4; const float yInternal = 0.005f * 4; const int width = depth.cols; const int height = depth.rows; // 1. 统一内存管理 thrust::device_vector<float> d_depth(height * width); thrust::device_vector<DevicePoint> d_cloud(height * width); thrust::device_vector<bool> d_validmask(height * width); thrust::copy_n(reinterpret_cast<float*>(depth.data), height * width, d_depth.begin()); // 2. 融合计算与压缩 auto zipped = thrust::make_zip_iterator( thrust::make_tuple(d_depth.begin(), thrust::make_counting_iterator(0))); thrust::transform( thrust::cuda::par, zipped, zipped + height * width, thrust::make_zip_iterator( thrust::make_tuple(d_cloud.begin(), d_validmask.begin())), [=] __device__(const auto & in) { const int idx = thrust::get<1>(in); const float z = thrust::get<0>(in); if (z > 800.0f) return thrust::make_tuple(DevicePoint(), false); const int x = idx % width; const int y = idx / width; return thrust::make_tuple( DevicePoint( (x - width / 2) * xInternal * z, (y - height / 2) * yInternal * z, z ), true ); }); // 3. 高效压缩 thrust::device_vector<DevicePoint> compressed_cloud(height * width); auto new_end = thrust::copy_if( thrust::cuda::par, d_cloud.begin(), d_cloud.end(), d_validmask.begin(), compressed_cloud.begin(), [](bool valid) { return valid; } ); // 4. 零拷贝传输 const int valid_count = thrust::distance(compressed_cloud.begin(), new_end); cloud.resize(valid_count); cudaMemcpy( cloud.points.data(), compressed_cloud.data().get(), valid_count * sizeof(pcl::PointXYZ), cudaMemcpyDevi
03-14
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值