我如何编译CUDA代码,然后链接到一个C ++项目? [英] How can I compile CUDA code then link it to a C++ project?

查看:354
本文介绍了我如何编译CUDA代码,然后链接到一个C ++项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



在这里是我的问题:


  1. 如何使用nvcc编译成cubin文件或ptx文件?

  2. 我要使用哪种文件类型?

  3. 正确编译的g ++命令是什么?


  4. 假设如下:


    1. 我有一个名为main.cpp的文件,其中有一个main函数并包含cuda.h。

    2. 我有另一个名为cudaFunc.cu的文件有CUDA代码。比方说,我想在main.cpp中添加两个整数数组。


    解决方案

    我能够解决我的问题与几个不同的帖子,包括这些。不要忘记,如果你使用64位机器链接到64位库!它接缝很明显,但对于像我这样的小丑,这是我忘了的东西。这里是make文件,我现在使用...如果你可以消化这个make文件,你应该能够做我想做的是单独的编译cuda代码和其他G ++代码。还要记住,你必须有gcc,g ++编译器在某些版本(我使用g ++ - 4.4,它是为我工作)无论如何,这里是make文件...

      all:程序

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

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

    clean:rm -rf * o程序

    希望你能看到我做的第一件事是编译cudacode a .cu)使用nvcc编译器和-c选项(还要注意,您可能要删除-arch = sm_20)。这创建了一个cudacode.o。然后我使用g ++编译器与-o选项和链接到lib64库和链接lcuda和lcudart文件,编译我的main.cpp,然后链接cudacode.o。希望这有助于某人!


    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.

    解决方案

    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 -rf *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 files along with compiling my main.cpp and then linking the cudacode.o. Hope this helps someone!

    这篇关于我如何编译CUDA代码,然后链接到一个C ++项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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