使用 Thrust device_malloc
代替普通的 cudaMalloc
有何优势?device_new
有什么作用?
对于 device_malloc
来说,使用它的唯一原因似乎是它更干净一点。
device_new
文档说:
"device_new implements the placement new operator for types resident in device memory. device_new calls T's null constructor on a array of objects in device memory. No memory is allocated by this function."
听不懂……
最佳答案
device_malloc
如果您计划将 Thrust 用于其他用途,则返回正确类型的对象。通常没有理由使用 cudaMalloc
如果您使用推力。封装 CUDA 调用使其变得更容易且通常更清晰。同样的情况也适用于 C++ 和 STL 容器与 C 样式数组和 malloc
.
对于device_new
,您应该阅读 documentation 的以下行:
template<typename T>
device_ptr<T> thrust::device_new (device_ptr< void > p, const size_t n = 1)
p: A device_ptr to a region of device memory into which to construct one or many Ts.
基本上,如果已经分配了内存,则可以使用此函数。仅调用默认构造函数,这将返回 device_pointer
转换为 T 的类型。
另一方面,以下方法分配内存并返回 device_ptr<T>
:
template<typename T >
device_ptr<T> thrust::device_new (const size_t n = 1)
关于cuda - 推力device_malloc和device_new,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16080630/