使用 CUDA 模块构建 GPL C 程序
我正在尝试修改用 C 编写的 GPL 程序。我的目标是用 CUDA 实现替换一种方法,这意味着我需要使用 nvcc 而不是 gcc 进行编译。我需要帮助构建项目 - 而不是实现它(我认为你不需要了解有关 CUDA C 的任何信息来提供帮助)。
这是我第一次尝试更改涉及 .configure 和 Makefile 的中等复杂度的 C 项目。老实说,这是我很长一段时间以来第一次用 C 做任何事情,包括涉及 gcc 或 g++ 的事情,所以我很迷茫。
我对学习配置和 Makefile 不太感兴趣 - 这更像是一个实验。我想在花时间创建适当的构建脚本之前看看项目实施是否顺利。 (并非不愿意根据需要学习,只是试图给出范围的概念)。
话虽如此,我构建这个项目有哪些选择?我有很多问题...
我尝试在 AC_PROG_CC 之后将“CC=nvcc”添加到configure.in 文件中。这似乎有效 - 运行 configure 和 make 的输出显示 nvcc 作为编译器。然而,make 无法使用 CUDA 内核编译源文件,无法识别 CUDA 特定语法。我不知道为什么,希望这能起作用。
是否可以使用 nvcc 编译源文件,然后将其包含在主程序的 make 过程中的链接步骤中?如果是这样,怎么办? (这个问题可能没有意义 - 我对此真的很生疏)
执行此操作的正确方法是什么?
是否有一种快速而肮脏的方法可以用于测试目的?
是否有一些每个人都使用的秘密工具来设置和理解这些配置和 Makefile?这比我习惯的 Apache Ant 脚本还要糟糕(是的,我超出了我的范围)
I am attempting to modify a GPL program written in C. My goal is to replace one method with a CUDA implementation, which means I need to compile with nvcc instead of gcc. I need help building the project - not implementing it (You don't need to know anything about CUDA C to help, I don't think).
This is my first time trying to change a C project of moderate complexity that involves a .configure and Makefile. Honestly, this is my first time doing anything in C in a long time, including anything involving gcc or g++, so I'm pretty lost.
I'm not super interested in learning configure and Makefiles - this is more of an experiment. I would like to see if the project implementation goes well before spending time creating a proper build script. (Not unwilling to learn as necessary, just trying to give an idea of the scope).
With that said, what are my options for building this project? I have a myriad of questions...
I tried adding "CC=nvcc" to the configure.in file after AC_PROG_CC. This appeared to work - output from running configure and make showed nvcc as the compiler. However make failed to compile the source file with the CUDA kernel, not recognizing the CUDA specific syntax. I don't know why, was hoping this would just work.
Is it possible to compile a source file with nvcc, and then include it at the linking step in the make process for the main program? If so, how? (This question might not make sense - I'm really rusty at this)
What's the correct way to do this?
Is there a quick and dirty way I could use for testing purposes?
Is there some secret tool everyone uses to setup and understand these configure and Makefiles? This is even worse than the Apache Ant scripts I'm used to (Yeah, I'm out of my realm)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不需要使用 nvcc 编译所有内容。您认为可以使用 NVCC 编译 CUDA 代码并保留其他所有内容(链接除外)的猜测是正确的。这是我开始使用的方法。
添加 1 个新标头(例如 myCudaImplementation.h)和 1 个新源文件(扩展名为 .cu,例如 myCudaImplementation.cu)。源文件包含您的内核实现以及一个(主机)C 包装函数,该函数使用适当的执行配置调用内核(又名
<<<<>>>
)和参数。头文件包含 C 包装函数的原型。让我们调用该包装函数runCudaImplementation()
我还将在源文件中提供另一个主机 C 函数(标头中带有原型),用于查询和配置存在的 GPU 设备,如果满足则返回 true成功则为假,失败则为假。我们将此函数称为
configureCudaDevice()
。现在,在您通常调用 CPU 实现的原始 C 代码中,您可以执行此操作。
现在,由于您将所有 CUDA 代码放入新的 .cu 文件中,因此只需使用 nvcc 编译该文件即可。除了必须链接到 nvcc 输出的目标文件之外,其他一切都保持不变。例如
然后将 myCudaImplementation.o 添加到您的链接行(类似于:)
g++ -o myApp myCudaImplementation.o
现在,如果您有一个复杂的应用程序要使用,它使用configure并且已经有一个复杂的makefile,它可能比上面更复杂,但这是一般方法。最重要的是,您不想使用 nvcc 编译所有源文件,而只想使用 .cu 文件。使用你的主机编译器来做其他事情。
我不是配置方面的专家,所以无法真正提供帮助。您也许可以运行configure 来生成一个makefile,然后编辑该makefile——这不是一个通用的解决方案,但它可以帮助您入门。
请注意,在某些情况下,您可能还需要将
.cu
文件的编译与它们的链接分开。在这种情况下,您需要使用 NVCC 的单独编译和链接功能,为此 这篇博文可能会有所帮助。You don't need to compile everything with nvcc. Your guess that you can just compile your CUDA code with NVCC and leave everything else (except linking) is correct. Here's the approach I would use to start.
Add a 1 new header (e.g. myCudaImplementation.h) and 1 new source file (with .cu extension, e.g. myCudaImplementation.cu). The source file contains your kernel implementation as well as a (host) C wrapper function that invokes the kernel with the appropriate execution configuration (aka
<<<>>>
) and arguments. The header file contains the prototype for the C wrapper function. Let's call that wrapper functionrunCudaImplementation()
I would also provide another host C function in the source file (with prototype in the header) that queries and configures the GPU devices present and returns true if it is successful, false if not. Let's call this function
configureCudaDevice()
.Now in your original C code, where you would normally call your CPU implementation you can do this.
Now, since you put all your CUDA code in a new .cu file, you only have to compile that file with nvcc. Everything else stays the same, except that you have to link in the object file that nvcc outputs. e.g.
Then add myCudaImplementation.o to your link line (something like:)
g++ -o myApp myCudaImplementation.o
Now, if you have a complex app to work with that uses configure and has a complex makefile already, it may be more involved than the above, but this is the general approach. Bottom line is you don't want to compile all of your source files with nvcc, just the .cu ones. Use your host compiler for everything else.
I'm not expert with configure so can't really help there. You may be able to run configure to generate a makefile, and then edit that makefile -- it won't be a general solution, but it will get you started.
Note that in some cases you may also need to separate compilation of your
.cu
files from linking them. In this case you need to use NVCC's separate compilation and linking functionality, for which this blog post might be helpful.