如何编译 CUDA 代码然后将其链接到 C++项目?

发布于 2025-01-08 09:03:43 字数 429 浏览 0 评论 0原文

我正在寻求帮助来开始涉及 CUDA 的项目。我的目标是拥有一个可以在本机 g++ 编译器中编译但使用 CUDA 代码的项目。我知道我必须在 nvcc 编译器中编译我的 CUDA 代码,但根据我的理解,我可以以某种方式将 CUDA 代码编译成 cubin 文件或 ptx 文件。

这是我的问题:

  1. 如何使用nvcc编译成cubin文件或ptx文件?我不需要 -c 之类的吗?
  2. 我想使用哪种文件类型?
  3. 正确编译和链接项目的 g++ 命令是什么?

假设如下:

  1. 我有一个名为“main.cpp”的文件,其中有一个 main 函数并包含 cuda.h。
  2. 我有另一个名为“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:

  1. How do I use nvcc to compile into a cubin file or a ptx file? Don't I need a -c or something?
  2. Which file type do I want to use?
  3. What are the g++ commands to correctly compile and link the project together?

Assume the following:

  1. I have a file called "main.cpp" that has a main function in it and includes cuda.h.
  2. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

楠木可依 2025-01-15 09:03:43

我能够通过几个不同的帖子(包括这些帖子)解决我的问题。不要忘记,如果您使用的是 64 位机器,则要链接到 64 位库!这听起来有点明显,但对于像我这样的小丑来说,这是我忘记的事情。这是我现在使用的 make 文件...如果你能理解这个 make 文件,你应该能够做我想做的事情,即单独编译 cuda 代码和其他 G++ 代码。另请记住,您必须拥有某些版本的 gcc、g++ 编译器(我正在使用 g++-4.4,它对我有用)无论如何,这是 make 文件...

all: program

program: cudacode.o
    g++ -o program -L/usr/local/cuda/lib64 -lcuda -lcudart main.cpp  cudacode.o 

cudacode.o:
    nvcc -c -arch=sm_20 cudacode.cu 

clean: rm -f *.o program

希望您能看到我做的第一件事要做的是使用 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...

all: program

program: cudacode.o
    g++ -o program -L/usr/local/cuda/lib64 -lcuda -lcudart main.cpp  cudacode.o 

cudacode.o:
    nvcc -c -arch=sm_20 cudacode.cu 

clean: rm -f *.o program

Hopefully you can see that the first thing I do is compile the cudacode (that has been saved as a .cu) using the nvcc compiler and -c option (also note that you may want to remove the -arch=sm_20). This created a cudacode.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 my main.cpp and then linking the cudacode.o. Hope this helps someone!

旧情别恋 2025-01-15 09:03:43

对最近这个问题的回答可能描述了您需要什么。

一些附加说明:

  1. 您不需要将 .cu 编译为 .cubin.ptx 文件。您需要将其编译为 .o 目标文件,然后将其与使用 g++ 编译的 .cpp 文件中的 .o 目标文件链接。
  2. 除了将 cuda 内核代码放入 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:

  1. You don't need to compile your .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++.
  2. In addition to putting your cuda kernel code in 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.
才能让你更想念 2025-01-15 09:03:43

我发现将编译后的 cuda 目标代码与 g++ 链接可能很麻烦。
尝试像这样编译它:

all:
nvcc cudafile.cu mainfile.cpp -o executable

clean: rm -rf *.o

I found that linking the compiled cuda object code with g++ can be troublesome.
Try compiling it like this:

all:
nvcc cudafile.cu mainfile.cpp -o executable

clean: rm -rf *.o
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文