在 __device/global__ CUDA 内核中动态分配内存
根据 CUDA 编程指南,第 122 页,只要我们使用计算架构 2.x,就可以在 device/global 函数内动态分配内存。
我的问题是,当我尝试执行此操作时,我收到命令行消息:
命令“某些命令” -gencode=arch=compute_10,code=\"sm_10,compute_10\" -gencode=arch=compute_20,code=\"sm_20,compute_20\" 等...
后面跟着一个错误,指出您不能从设备/全局函数调用主机函数 (malloc)。
上面的消息显示它正在尝试在compute 1.x 下进行编译。我正在使用 VS2010 并在“CUDA C/C++”属性页中将“代码生成”设置为“compute_20,sm_20”,所以我不确定为什么它仍然尝试在计算 1.x 下进行编译。我肯定使用的是支持 2.x 的卡。有什么想法吗?
According to the CUDA Programming Guide , Page 122, it is possible to dynamically allocate memory inside a device/global function so long as we're using compute architecture 2.x.
My problem is that when I attempt this I get the command line message:
The command "some command" -gencode=arch=compute_10,code=\"sm_10,compute_10\" -gencode=arch=compute_20,code=\"sm_20,compute_20\" etc...
This is followed by an error saying that you cannot call a host function (malloc) from a device/global function.
The above message is showing that it is attempting to compile under compute 1.x. I am using VS2010 and have "Code Generation" set to "compute_20,sm_20" in the "CUDA C/C++" property page, so I am not sure why it is still trying to compile under compute 1.x. I am definitely using a card that supports 2.x. Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该能够在输出中看到 nvcc 命令行。事实上,我认为你粘贴了所有 -gencode/etc 的内容。其中是您的命令行。因此,这也证明您正在编译 sm_10 和 sm_20 的代码,这就是为什么您在调用 malloc 时收到错误的原因。
您可以通过使用
#if __CUDA_ARCH__ >= 200
包装对 malloc 的调用进行确认,并查看错误是否消失。我猜测您在项目中 .cu 文件的默认属性中设置了要编译 sm_20 的属性,但是在您将 .cu 文件添加到项目中。将文件添加到项目时,默认值可能设置为 sm_10 和 sm_20(这是 .rules 文件的默认值)。如果右键单击文件本身,您可能会看到 sm_20 被选中。只是一种预感。
You should be able to see the nvcc command line in the output. In fact, I think that bit you pasted with all the -gencode/etc. in it is your command line. Therefore, it is also proof that you are compiling the code for both sm_10 and sm_20, which is why you get the error when you call malloc.
You can confirm by wrapping the calls to malloc with
#if __CUDA_ARCH__ >= 200
and see if the error goes away.I'm guessing that you set the properties to compile for sm_20 in the default properties for .cu files in your project, but after you added the .cu file to the project. When the file was added to the project, the defaults were probably set to sm_10 and sm_20 (which is the default for the .rules file). If you right-click on the file itself you might see that sm_20 is checked. Just a hunch.