更新时间:2022-05-20 23:53:08
我不会尝试回答这个问题的所有内容,它太大了.话虽如此,以下是对您发布的代码的一些观察,可能会有所帮助:
I am not going to attempt to answer everything in this question, it is just too large. Having said that here are some observations about the code you posted which might help:
new
运算符从私有运行时堆中分配内存.从 CUDA 6 开始,主机端 CUDA API 无法访问该内存.您可以从内核和设备函数中访问内存,但主机无法访问该内存.因此,在推力设备函子中使用 new
是一个永远无法工作的破碎设计.这就是您的指针向量"模型失败的原因.new
operator allocates memory from a private runtime heap. As of CUDA 6, that memory cannot be accessed by the host side CUDA APIs. You can access the memory from within kernels and device functions, but that memory cannot be accessed by the host. So using new
inside a thrust device functor is a broken design that can never work. That is why your "vector of pointers" model fails.浏览您发布的代码后,我的总体建议是回到绘图板上.如果你想看一些非常优雅的 CUDA/C++ 设计,花点时间阅读 CUB的代码库> 和 CUSP.它们都非常不同,但都可以从中学到很多东西(我怀疑 CUSP 是建立在 Thrust 之上的,这使得它与您的用例更加相关).
Having skim read the code you posted, my overall recommendation is to go back to the drawing board. If you want to look at some very elegant CUDA/C++ designs, spend some time reading the code bases of CUB and CUSP. They are both very different, but there is a lot to learn from both (and CUSP is built on top of Thrust, which makes it even more relevant to your usage case, I suspect).