使用 CUDA 模块构建 GPL C 程序 [英] Building GPL C program with CUDA module

查看:21
本文介绍了使用 CUDA 模块构建 GPL C 程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试修改用 C 编写的 GPL 程序.我的目标是用 CUDA 实现替换一种方法,这意味着我需要使用 nvcc 而不是 gcc 进行编译.我需要帮助来构建项目 - 而不是实施它(我认为您不需要了解任何有关 CUDA C 的信息来提供帮助).

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).

这是我第一次尝试更改涉及 .configure 和 Makefile 的中等复杂度的 C 项目.老实说,这是我很长时间以来第一次用 C 做任何事情,包括涉及 gcc 或 g++ 的任何事情,所以我很迷茫.

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.

我对学习配置和 Makefile 不是很感兴趣——这更像是一个实验.在花时间创建正确的构建脚本之前,我想看看项目实施是否顺利.(不是不​​愿意学习必要的,只是想给出一个范围的想法).

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...

  • 我尝试在 AC_PROG_CC 之后的 configure.in 文件中添加CC=nvcc".这似乎有效 - 运行 configure 和 make 的输出显示 nvcc 作为编译器.但是 make 无法使用 CUDA 内核编译源文件,无法识别 CUDA 特定的语法.我不知道为什么,希望这能奏效.

  • 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.

是否可以用 nvcc 编译一个源文件,然后在主程序的 make 过程的链接步骤中包含它?如果是这样,怎么做?(这个问题可能没有意义——我对此真的很生疏)

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?

是否有一些每个人都可以使用的秘密工具来设置和理解这些配置和 Makefile?这比我习惯的 Apache Ant 脚本还要糟糕(是的,我不在我的领域)

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)

推荐答案

你不需要用 nvcc 编译所有东西.您猜测您可以使用 NVCC 编译您的 CUDA 代码并保留其他所有内容(链接除外)是正确的.这是我开始使用的方法.

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.

  1. 添加 1 个新标头(例如 myCudaImplementation.h)和 1 个新源文件(扩展名为 .cu,例如 myCudaImplementation.cu).源文件包含您的内核实现以及一个(主机)C 包装函数,该函数使用适当的执行配置调用内核(又名 <<<>>>) 和参数.头文件包含 C 包装函数的原型.让我们调用这个包装函数 runCudaImplementation()

  1. 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 function runCudaImplementation()

我还将在源文件中提供另一个主机 C 函数(在头文件中带有原型),它查询和配置存在的 GPU 设备,如果成功则返回 true,否则返回 false.我们称这个函数为 configureCudaDevice().

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().

现在,在您通常调用 CPU 实现的原始 C 代码中,您可以执行此操作.

Now in your original C code, where you would normally call your CPU implementation you can do this.

// must include your new header
#include "myCudaImplementation.h"

// at app initialization
// store this variable somewhere you can access it later
bool deviceConfigured = configureCudaDevice;          
...                             
// then later, at run time
if (deviceConfigured) 
    runCudaImplementation();
else
    runCpuImplementation(); // run the original code

  • 现在,由于您将所有 CUDA 代码放在一个新的 .cu 文件中,您只需使用 nvcc 编译该文件.其他一切都保持不变,除了您必须链接到 nvcc 输出的目标文件.例如

  • 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.

    nvcc -c -o myCudaImplementation.o myCudaImplementation.cu <other necessary arguments>
    

  • 然后将 myCudaImplementation.o 添加到您的链接行(类似于:)g++ -o myApp myCudaImplementation.o

    Then add myCudaImplementation.o to your link line (something like:) g++ -o myApp myCudaImplementation.o

    现在,如果您有一个复杂的应用程序使用 configure 并且已经有一个复杂的 makefile,那么它可能比上述更复杂,但这是一般方法.底线是您不想用 nvcc 编译所有源文件,只需要 .cu 文件.使用您的主机编译器进行其他所有操作.

    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.

    我不是配置方面的专家,因此无法真正提供帮助.您可以运行 configure 来生成一个 makefile,然后编辑该 makefile——这不是一个通用的解决方案,但它会让您开始.

    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.

    请注意,在某些情况下,您可能还需要将 .cu 文件的编译与链接它们分开.在这种情况下,您需要使用 NVCC 的单独编译和链接功能,为此 这篇博文可能会有所帮助.

    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.

    这篇关于使用 CUDA 模块构建 GPL C 程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆