如何编译 CUDA 代码然后将其链接到 C++项目?
我正在寻求帮助来开始涉及 CUDA 的项目。我的目标是拥有一个可以在本机 g++ 编译器中编译但使用 CUDA 代码的项目。我知道我必须在 nvcc 编译器中编译我的 CUDA 代码,但根据我的理解,我可以以某种方式将 CUDA 代码编译成 cubin 文件或 ptx 文件。
这是我的问题:
- 如何使用nvcc编译成cubin文件或ptx文件?我不需要 -c 之类的吗?
- 我想使用哪种文件类型?
- 正确编译和链接项目的 g++ 命令是什么?
假设如下:
- 我有一个名为“main.cpp”的文件,其中有一个 main 函数并包含 cuda.h。
- 我有另一个名为“cudaFunc.cu”的文件,其中包含 CUDA 代码。比方说,我想添加 main.cpp 中存在的两个整数数组。
I am looking for help getting started with a project involving CUDA. My goal is to have a project that I can compile in the native g++ compiler but uses CUDA code. I understand that I have to compile my CUDA code in nvcc compiler, but from my understanding I can somehow compile the CUDA code into a cubin file or a ptx file.
Here are my questions:
- How do I use nvcc to compile into a cubin file or a ptx file? Don't I need a -c or something?
- Which file type do I want to use?
- What are the g++ commands to correctly compile and link the project together?
Assume the following:
- I have a file called "main.cpp" that has a main function in it and includes cuda.h.
- I have another file called "cudaFunc.cu" that has CUDA code in it. Let's say, for instance, that I want to add two integer arrays that exist in main.cpp.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我能够通过几个不同的帖子(包括这些帖子)解决我的问题。不要忘记,如果您使用的是 64 位机器,则要链接到 64 位库!这听起来有点明显,但对于像我这样的小丑来说,这是我忘记的事情。这是我现在使用的 make 文件...如果你能理解这个 make 文件,你应该能够做我想做的事情,即单独编译 cuda 代码和其他 G++ 代码。另请记住,您必须拥有某些版本的 gcc、g++ 编译器(我正在使用 g++-4.4,它对我有用)无论如何,这是 make 文件...
希望您能看到我做的第一件事要做的是使用
nvcc
编译器和-c
选项编译 cudacode(已保存为.cu
)(另请注意,您可以想要删除-arch=sm_20
)。这创建了一个cudacode.o
。然后,我使用带有-o
选项的 g++ 编译器并链接到 lib64 库,并链接-lcuda
和-lcudart
库并进行编译我的main.cpp
然后链接cudacode.o
。希望这对某人有帮助!I was able to resolve my issue with a couple of different posts including these ones. Don't forget that if you are using a 64 bit machine to link to the 64 bit library! It seams kind of obvious, but for clowns like me, that is something I forgot. Here is the make file that I now use... if you can digest this make file, you should be able to do what I was trying to do which was separate compilation of cuda code and other G++ code. Also keep in mind that you have to have the gcc, g++ compilers at certain versions (I am using g++-4.4 and it is working for me) Anyway, here is the make file...
Hopefully you can see that the first thing I do is compile the cudacode (that has been saved as a
.cu
) using thenvcc
compiler and-c
option (also note that you may want to remove the-arch=sm_20
). This created acudacode.o
. I then use the g++ compiler with the-o
option and link to the lib64 library and link the-lcuda
and-lcudart
libraries along with compiling mymain.cpp
and then linking thecudacode.o
. Hope this helps someone!我对最近这个问题的回答可能描述了您需要什么。
一些附加说明:
.cu
编译为.cubin
或.ptx
文件。您需要将其编译为.o
目标文件,然后将其与使用 g++ 编译的 .cpp 文件中的.o
目标文件链接。cudaFunc.cu
中之外,您还需要在该文件中放入一个 C 或 C++ 包装函数来启动内核(除非您使用 CUDA 驱动程序 API,该 API 是不太可能且不推荐)。还要添加一个包含此包装函数原型的头文件,以便您可以将其包含在需要调用 CUDA 代码的 C++ 代码中。然后使用标准 g++ 链接线将文件链接在一起。My answer to this recent question likely describes what you need.
A couple of additional notes:
.cu
to a.cubin
or.ptx
file. You need to compile it to a.o
object file and then link it with the.o
object files from your .cpp files compiled with g++.cudaFunc.cu
, you also need to put a C or C++ wrapper function in that file that launches the kernel (unless you are using the CUDA driver API, which is unlikely and not recommended). Also add a header file with the prototype of this wrapper function so that you can include it in your C++ code which needs to call the CUDA code. Then you link the files together using your standard g++ link line.我发现将编译后的 cuda 目标代码与 g++ 链接可能很麻烦。
尝试像这样编译它:
I found that linking the compiled cuda object code with g++ can be troublesome.
Try compiling it like this: