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

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

问题描述

我试图修改用C语言编写的GPL程序。我的目标是用CUDA实现替换一个方法,这意味着我需要使用nvcc而不是gcc编译。我需要帮助建立项目 - 没有实现它(你不需要知道任何关于CUDA C帮助,我不认为)。



这是我的首次尝试更改包含.configure和Makefile的中度复杂的C项目。老实说,这是我第一次在C中做任何事情,包括任何涉及gcc或g ++,所以我很迷失。



我不是超级对学习configure和Makefile感兴趣 - 这更多的是一个实验。我想看看项目实现在花费时间创建正确的构建脚本之前是否正常。 (不愿意在必要时学习,只是试图给出范围的想法)。



有了这个说法,我的建造这个项目的选择是什么?我有很多问题...




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


  • 可以使用nvcc编译源文件,在主程序的make过程中的步骤?如果是,如何?


  • 这是正确的方法是什么?

  • 设置和理解这些配置和Makefile?这比我用过的Apache Ant脚本更糟糕(是啊,我出了我的境界)



解决方案

您不需要使用nvcc编译所有内容。你的猜测,你可以只是编译CUDA代码与NVCC和离开一切(除了链接)是正确的。这里是我将使用的方法开始。


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


  2. 文件(在标题中有原型)查询和配置GPU设备,如果成功则返回true,否则返回false。让我们调用这个函数 configureCudaDevice()


  3. 调用你的CPU实现,你可以这样做。

      //必须包含你的新标题
    #includemyCudaImplementation.h

    //在应用程序初始化
    //将此变量存储在稍后可以访问的位置
    bool deviceConfigured = configureCudaDevice;
    ...
    //然后,在运行时
    if(deviceConfigured)
    runCudaImplementation();
    else
    runCpuImplementation(); //运行原始代码


  4. 现在, .cu文件,你只需要编译该文件与nvcc。一切都保持不变,除非你必须链接到nvcc输出的目标文件。例如

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


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



现在,如果你有一个复杂的应用程序使用configure,并有一个复杂的makefile已经,它可能比上面更多的参与,但这是一般的方法。底线是你不想编译所有的源文件与nvcc,只是.cu的。使用你的主机编译器的一切。



我不是专家配置所以不能真正帮助那里。你可以运行configure来生成makefile,然后编辑makefile - 它不会是一个通用的解决方案,但它会让你开始。


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)

解决方案

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

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

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

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

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.

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

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