最近我一直在使用推力很多.我注意到为了使用推力,必须始终将数据从cpu内存复制到gpu内存.
我们来看下面的例子:
int foo(int *foo)
{
host_vector<int> m(foo, foo+ 100000);
device_vector<int> s = m;
}
Run Code Online (Sandbox Code Playgroud)
我不太确定host_vector构造函数是如何工作的,但似乎我正在复制初始数据,来自*foo,两次 - 初始化时一次到host_vector,另一次device_vector初始化时.是否有更好的方法从cpu复制到gpu而不制作中间数据副本?我知道我可以device_ptr用作包装器,但这仍然无法解决我的问题.
谢谢!
Jar*_*ock 16
其中一个device_vector构造函数采用两个迭代器指定的一系列元素.理解示例中的原始指针足够聪明,因此您可以device_vector直接构造一个并避免临时host_vector:
void my_function_taking_host_ptr(int *raw_ptr, size_t n)
{
// device_vector assumes raw_ptrs point to system memory
thrust::device_vector<int> vec(raw_ptr, raw_ptr + n);
...
}
Run Code Online (Sandbox Code Playgroud)
如果您的原始指针指向CUDA内存,请引入device_ptr:
void my_function_taking_cuda_ptr(int *raw_ptr, size_t n)
{
// wrap raw_ptr before passing to device_vector
thrust::device_ptr<int> d_ptr(raw_ptr);
thrust::device_vector<int> vec(d_ptr, d_ptr + n);
...
}
Run Code Online (Sandbox Code Playgroud)
使用a device_ptr不分配任何存储空间; 它只是编码类型系统中指针的位置.
| 归档时间: |
|
| 查看次数: |
2017 次 |
| 最近记录: |