推到 STL 副本无法按预期工作
Thrust to STL copy doesn't work as intended
我不确定thrust::copy
到STL向量的实际工作方式。当我做以下操作时,它会给我预期的结果:
struct TestOperation
{
TestOperation(){}
__host__ __device__
CustomPoint operator()(const CustomPoint& point)
{
CustomPoint pt;
pt.x = point.x * 2;
pt.y = point.y * 2;
pt.z = point.z;
return pt;
}
};
void CudaLoader::TestLoader(std::vector<CustomPoint>& customPoints) //Host vector reference
{
thrust::device_vector<CustomPoint> devicePoints(customPoints.begin(), customPoints.end());
thrust::device_vector<CustomPoint> output;
output.reserve(devicePoints.size());
thrust::transform(devicePoints.begin(), devicePoints.end(), output.begin(), TestOperation());
for (int i = 0; i < customPoints.size(); i++)
{
customPoints[i] = output[i];
}
}
但是循环所有元素,尤其是当有很多元素时,对我来说似乎不是最佳的,所以我想使用copy
。但当我尝试做:
thrust::copy(output.begin(), output.end(), customPoints.begin());
而不是循环,那么我就不会得到预期的结果——作为参数给出的引用的主机stl向量保持不变。此外,output.size()
返回0,但我看到存储大小是正确的。为什么?
问题的根源是:
thrust::device_vector<CustomPoint> output;
output.reserve(devicePoints.size());
CCD_ 4仅改变向量的保证最小存储分配。它不会更改大小。在上述代码中,output.size()
仍然为0。还要注意,thrust::transform
不会改变输出向量的大小。只要有足够的有效内存来保存转换的输出,执行转换操作的推力闭包内核就不会产生非法的内存访问错误。
改为:
thrust::device_vector<CustomPoint> output;
output.resize(devicePoints.size());
thrust::transform(devicePoints.begin(), devicePoints.end(), output.begin(), TestOperation());
然后
thrust::copy(output.begin(), output.end(), customPoints.begin());
将按预期工作,因为CCD_ 7具有非零大小。
相关文章:
- QSqlquery prepare()和bindvalue()不工作
- 在C++STL中是否有Polyval(Matlab函数)等价物?
- 为什么这个运算符<重载函数对 STL 算法不可见?
- 导入库可以跨dll版本工作吗
- 在C应用程序中运行C++(带有STL)函数
- 使用2个键的cpp-stl::优先级队列排序不正确
- 以螺旋方式打印矩阵的程序.(工作不好)
- 对象指针在c++中是如何工作的
- C++17:如何在并行 STL 中获取工作项的索引
- 使用 stl 库创建图形是如何工作的?
- 在 stl 中的字符串中擦除关键字的工作?
- 容器类别在STL中的工作方式
- C++ STL . 自定义比较器如何工作?
- 当我使用unordered_map时,如何使c ++_static stl在Android Studio中正常工作
- C++STL映射键和值工作不正常
- STL代码typedef类型名value_type,size_type等如何工作
- STL 地图查找无法正常工作
- stl 容器的"less"参数如何工作?
- c++ STL或TR1(不是boost:: TR1)只在Windows上工作吗?
- 推到 STL 副本无法按预期工作