thrust::device_vector使用提供的分配器构造它包含的元素,就像std::vector.当向量要求分配器构造元素时,可以控制分配器的作用.
使用自定义分配器来避免向量元素的默认初始化:
// uninitialized_allocator is an allocator which
// derives from device_allocator and which has a
// no-op construct member function
template<typename T>
struct uninitialized_allocator
: thrust::device_malloc_allocator<T>
{
// note that construct is annotated as
// a __host__ __device__ function
__host__ __device__
void construct(T *p)
{
// no-op
}
};
// to make a device_vector which does not initialize its elements,
// use uninitialized_allocator as the 2nd template parameter
typedef thrust::device_vector<float, uninitialized_allocator<float> > uninitialized_vector;
Run Code Online (Sandbox Code Playgroud)
您仍将承担调用内核启动的成本uninitialized_allocator::construct,但该内核将是一个快速退出的无操作.您真正感兴趣的是避免填充阵列所需的内存带宽,这是该解决方案所做的.
有一个完整的示例代码在这里.
请注意,此技术需要Thrust 1.7或更高版本.
| 归档时间: |
|
| 查看次数: |
545 次 |
| 最近记录: |