带有 Boost 的 Cuda
我目前正在编写 CUDA 应用程序,并希望使用 boost:: program_options 库来获取所需的参数和用户输入。
我遇到的问题是 NVCC 无法处理编译 boost 文件 any.hpp
,出现错误,例如
1>C:\boost_1_47_0\boost/any.hpp(68): error C3857: 'boost::any': multiple template parameter lists are not allowed
我在网上搜索并发现这是因为 NVCC 无法处理 boost 代码中使用的某些结构,但 NVCC 应将主机代码的编译委托给 C++ 编译器。就我而言,我使用的是 Visual Studio 2010,因此主机代码应传递给 cl
。
由于 NVCC 似乎变得困惑,我什至围绕升压内容编写了一个简单的包装器,并将其粘贴在单独的 .cpp
(而不是 .cu
)文件中,但我仍然出现构建错误。奇怪的是,在编译我的 main.cu
而不是 wrapper.cpp
时抛出了错误,但仍然是由 boost 引起的,即使 main.cu
没有不包含任何增强代码。
有谁知道这个问题的解决方案甚至解决方法?
I am currently writing a CUDA application and want to use the boost::program_options library to get the required parameters and user input.
The trouble I am having is that NVCC cannot handle compiling the boost file any.hpp
giving errors such as
1>C:\boost_1_47_0\boost/any.hpp(68): error C3857: 'boost::any': multiple template parameter lists are not allowed
I searched online and found it is because NVCC cannot handle the certain constructs used in the boost code but that NVCC should delegate compilation of host code to the C++ compiler. In my case I am using Visual Studio 2010 so host code should be passed to cl
.
Since NVCC seemed to be getting confused I even wrote a simple wrapper around the boost stuff and stuck it in a separate .cpp
(instead of .cu
) file but I am still getting build errors. Weirdly the error is thrown upon compiling my main.cu
instead of the wrapper.cpp
but still is caused by boost even though main.cu
doesn't include any boost code.
Does anybody know of a solution or even workaround for this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Dan,我过去使用 boost::program_options 编写了 CUDA 代码,并回顾了它,看看我是如何处理你的问题的。 nvcc 编译链中肯定存在一些怪癖。我相信,如果您适当地分解了您的类,并且意识到 NVCC 通常无法处理 C++ 代码/标头,但您的 C++ 编译器可以很好地处理与 CUDA 相关的标头,您通常可以处理这个问题。
我本质上有 main.cpp,其中包括我的 program_options 标头,以及指示如何处理选项的解析内容。然后,program_options 标头包含 CUDA 相关标头/类原型。重要的部分(正如我认为您已经看到的)是不要让 CUDA 代码和随附的标头包含该选项标头。将您的对象传递给选项函数并填充相关信息。有点像策略模式的丑陋版本。连接:
这可能有点烦人,但 nvcc 编译器似乎在处理更多 C++ 代码方面做得越来越好。这在 VC2008/2010 和 linux/g++ 中对我来说效果很好。
Dan, I have written a CUDA code using boost::program_options in the past, and looked back to it to see how I dealt with your problem. There are certainly some quirks in the nvcc compile chain. I believe you can generally deal with this if you've decomposed your classes appropriately, and realize that often NVCC can't handle C++ code/headers, but your C++ compiler can handle the CUDA-related headers just fine.
I essentially have main.cpp which includes my program_options header, and the parsing stuff dictating what to do with the options. The program_options header then includes the CUDA-related headers/class prototypes. The important part (as I think you've seen) is to just not have the CUDA code and accompanying headers include that options header. Pass your objects to an options function and have that fill in relevant info. Something like an ugly version of a Strategy Pattern. Concatenated:
It can be a bit annoying, but the nvcc compiler seems to be getting better at handling more C++ code. This has worked fine for me in VC2008/2010, and linux/g++.
您必须将代码分为两部分:
然后将两个对象链接在一起,一切都应该正常工作。
仅编译 CUDA 内核代码时需要 nvcc。
You have to split the code in two parts:
Then link the two objects together and everything should be working.
nvcc is required only to compile the CUDA kernel code.
感谢@ronag的评论,我意识到我仍然(间接)在我的标头中间接包含
boost/program_options.hpp
,因为我的包装类定义中有一些需要它的成员变量。为了解决这个问题,我将这些变量移到了类之外,因此可以将它们移到类定义之外并移到 .cpp 文件中。它们不再是成员变量,现在是全局的
wrapper.cpp
这似乎可以工作,但它很丑陋,我感觉 nvcc 应该优雅地处理这个问题;如果其他人有正确的解决方案,请仍然发布:)
Thanks to @ronag's comment I realised I was still (indirectly) including
boost/program_options.hpp
indirectly in my header since I had some member variables in my wrapper class definition which needed it.To get around this I moved these variables outside the class and thus could move them outside the class defintion and into the .cpp file. They are no longer member variables and now global inside
wrapper.cpp
This seems to work but it is ugly and I have the feeling nvcc should handle this gracefully; if anybody else has a proper solution please still post it :)
另一种选择是将仅 cpp 的代码包装在
Another option is to wrap cpp only code in