cuda - 推力device_malloc和device_new

标签 cuda thrust

使用 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/

相关文章:

CUDA 5.0 - cudaGetDeviceProperties 奇怪的网格大小或我的代码中的错误?

c++ - 如何使用 NVIDIA cuDNN 计算 'full' 卷积?

c++ - 使用推力库操作时使用袖套

c++ - Thrust 库 - 如何编写包装器?

cuda - CUDA内核中的数据结构

cuda - 为什么只能在计算能力为 2.0+ 的 CUDA GPU 设备上设置堆栈大小?

c++ - CUB模板类似于推力

sorting - Thrust::sort 有多快以及最快的基数排序实现是什么

cuda - 在推力中处理交错数据

multithreading - 从多线程 c 应用程序调用 cuda 内核