使用带有原始指针的推力函数:控制内存分配 [英] Using Thrust Functions with raw pointers: Controlling the allocation of memory

查看:71
本文介绍了使用带有原始指针的推力函数:控制内存分配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于使用 CUDA 时推力库的问题.我正在使用推力函数,即 exclusive_scan,我想使用原始指针.我使用原始(设备)指针是因为我想完全控制分配和释放内存的时间.

I have a question regarding the thrust library when using CUDA. I am using a thrust function, i.e. exclusive_scan, and I want to use raw pointers. I am using raw (device) pointers because I want to have full control of when the memory is allocated and deallocated.

在函数调用之后,我将把指针交给另一个数据结构,然后在这个数据结构的析构函数中释放内存,或者在下一个函数调用中,当我重新计算我的(设备)指针时.例如,我遇到了这个问题现在在这里,建议包装device_vector 中的数据结构.但是后来我遇到了一个问题,即一旦我的 device_vector 超出范围,内存就会被释放,这是我不想要的.全局设置设备指针也不是一种选择,因为我正在破解代码,即它被用作缓冲区,如果我想做这样的事情,我将不得不重写很多.

After the function call, I will hand over the pointer to another data structure and then free the memory in either the destructor of this data structure, or in the next function call, when I recompute my (device) pointers. I came across for example this problem here now, which recommends to wrap the data structure in a device_vector. But then I run into the problem that the memory is freed once my device_vector goes out of scope, which I do not want. Having the device pointer globally is also not an option, since I am hacking code, i.e. it is used as a buffer and I would have to rewrite a lot if I wanted to do something like that.

有没有人对此有好的解决方法?我现在看到的唯一机会是自己重写推力函数,只使用原始设备指针.

Does anyone have a good workaround regarding this? The only chance I do see right now is to rewrite the thrust-function on my own, only using raw device-pointers.

我误读了,我可以将它包装在 device_ptr 而不是 device_vector 中.进一步询问,如果没有使用 device_ptr 的选项,我如何解决这个问题?

I misread, I can wrap it in a device_ptr instead of a device_vector. Asking further though, how could I solve this if there wasn't the option of using a device_ptr?

推荐答案

在推力方法中使用普通指针没有问题.

There is no problem using plain pointers in thrust methods.

对于设备上的数据,请执行以下操作:

For data on the device do:

....
struct DoSomething {
    __device__ int operator()(int item) { return 1; }
};

int* IntData;
cudaMalloc(&IntData, sizeof(int) * count);
auto dev_data = device_pointer_cast(IntData);
thrust::generate(dev_data, dev_data + count, DoSomething());
thrust::sort(dev_data, dev_data + count);
....
cudaFree(IntData);

对于主机上的数据,请使用纯 malloc/freeraw_pointer_cast 而不是 device_pointer_cast.请参阅:推力:内存管理

For data on the host use plain malloc/free and raw_pointer_cast instead of device_pointer_cast. See: thrust: Memory management

这篇关于使用带有原始指针的推力函数:控制内存分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆