将静态cuda库包含到c ++项目中 [英] Include a static cuda library into a c++ project

查看:162
本文介绍了将静态cuda库包含到c ++项目中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个模板化的静态CUDA库,我想包含在一个通用的c ++项目中。当我包括库的头文件时,编译器崩溃,并说它无法解析CUDA特定的符号。当然,g ++编译器不能解释这些符号。我知道问题,但我不知道如何使用nsight IDE来解决这个问题。



我使用nsight的cuda / nvcc库和

  make all 
构建文件:../src/MedPrak.cpp
调用:GCC C ++编译器
g ++ -I / home / voodoocode / Praktikum / MedPrak / PrivateRepo / MedPrakCuda / src -O0 -g3 -Wall -c -fmessage-length = 0-MMD -MP -MFsrc / MedPrak.d-MTsrc / MedPrak.d-osrc / MedPrak.o../src/MedPrak.cpp

在../src/cudaWrapper.cu:8:0中包含的文件中,
来自../src/MedPrak.cpp:3:

/ home /voodoocode/Praktikum/MedPrak/PrivateRepo/MedPrakCuda/src/kernel.h:15:23:错误:'__global__'不命名类型
模板< typename T> __global__ void squareVector(T * input,T * output,int size){

我有一个cuda项目与库中相同的文件。 cuda项目编译正常,运行正常,所以我认为在我的代码中没有一个巨大的错误。



Edit2:避免模板库的想法。我有一个包装器实际的模板类。因此,没有空库。

解决方案

这里有一组说明,可以帮助:


A。创建库项目:





  1. 选择文件...新建... CUDA C / C ++项目

  2. 选择静态库...空项目并为项目命名(test8)

  3. 下一步...下一步...完成创建项目

  4. 在项目浏览器窗口中右键单击项目名称,选择新建...头文件,给它一个名称(test8lib.h)

  5. 编辑test8lib.h(包含来自下面的内容),保存

  6. 为cuda模板创建另一个新头文件(test8.cuh)

  7. 保存

  8. 创建新的源文件(test8.cu)

  9. 编辑test8.cu




  10. 选择项目...构建项目(libtest8.a现已生成)

    test8lib.h:

      #ifndef TEST8LIB_H_ 
    #define TEST8LIB_H_
    b $ b void calc_square_vec_float(float * in_data,float * out_data,int size);


    #endif / * TEST8LIB_H_ * /

    test8。 cuh:

      #ifndef TEST8_CUH_ 
    #define TEST8_CUH_

    template< typename T> __global__ void squareVector(T * input,T * output,int size){
    int idx = threadIdx.x + blockDim.x * blockIdx.x;
    if(idx< size)output [idx] = input [idx] * input [idx];
    }


    #endif / * TEST8_CUH_ * /

    test8.cu:

      #includetest8lib.h
    #includetest8.cuh
    #define nTPB 256

    void calc_square_vec_float(float * in_data,float * out_data,int size){
    float * d_in_data,* d_out_data;
    cudaMalloc(& d_in_data,size * sizeof(float));
    cudaMalloc(& d_out_data,size * sizeof(float));
    cudaMemcpy(d_in_data,in_data,size * sizeof(float),cudaMemcpyHostToDevice);
    squareVector<<<(size + nTPB-1)/ nTPB,nTPB>>(d_in_data,d_out_data,size);
    cudaMemcpy(out_data,d_out_data,size * sizeof(float),cudaMemcpyDeviceToHost);
    }




    创建主项目:





    1. 文件... new ... C ++项目...空项目。 ..Linux GCC工具链,给它一个名称(test9)

    2. 下一步...完成创建项目

    3. <源文件...默认C ++源模板,给它一个名称(test9.cpp)
    4. 使用下面的内容编辑文件,保存。

    5. 添加包含路径:
      Project ...属性...构建...设置...工具设置... GCC C ++
      编译器...包括...包含路径.. 。添加和添加目录
      其中test8lib.h所在。

    6. 添加lib:工具设置... GCC C ++链接器...库...库。添加并添加之前构建的库(test8)的名称

    7. 也添加CUDA运行时库(cudart)

    8. 添加lib路径:设置... GCC C ++链接器...库...库路径...添加和添加到以前构建的库的路径(例如 / path / to / cuda-workspace / test8 / Debug

    9. 也添加cudart的路径(例如 / usr / local / cuda / lib64

    10. 构建项目

    11. li>

    test9.cpp:

      include< stdio.h> 
    #include< stdlib.h>
    #includetest8lib.h
    #define DSIZE 4
    #define TEST_VAL 2.0f

    int main(){
    float * in,*出口;
    in =(float *)malloc(DSIZE * sizeof(float));
    out =(float *)malloc(DSIZE * sizeof(float));
    for(int i = 0; i in [i] = TEST_VAL;
    out [i] = 0.0f;
    }
    calc_square_vec_float(in,out,DSIZE);
    for(int i = 0; i< DSIZE; i ++)
    if(out [i]!=(float)(TEST_VAL * TEST_VAL)){
    printf ,is:%f,应该是:%f \\\
    ,i,out [i],(float)(TEST_VAL * TEST_VAL)
    return 1;
    }
    printf(Success!\\\
    );
    return 0;
    }


    I have a templated static CUDA library which I want to include into a common c++ project. When I include the headers of the library the compiler crashes and says It cannot resolve the CUDA-specific symbols. Of course the g++ compiler cannot interpret these symbols. I know the problem, but I do not know how to fix this problem using the nsight IDE.

    I'm using nsight for both, the cuda/nvcc library and the c++/g++ project.

    Console output:

    make all 
    Building file: ../src/MedPrak.cpp
    Invoking: GCC C++ Compiler
    g++ -I/home/voodoocode/Praktikum/MedPrak/PrivateRepo/MedPrakCuda/src -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/MedPrak.d" -MT"src/MedPrak.d" -o "src/MedPrak.o" "../src/MedPrak.cpp"
    
    In file included from ../src/cudaWrapper.cu:8:0,
                     from ../src/MedPrak.cpp:3:
    
    /home/voodoocode/Praktikum/MedPrak/PrivateRepo/MedPrakCuda/src/kernel.h:15:23: error: ‘__global__’ does not name a type
     template <typename T> __global__ void squareVector(T *input, T *output, int size) {
    

    Edit: Forgot to mention that I have a cuda project with the same files as in the library. The cuda project compiles fine and runs properly, so I think there is not a huge error within my code.

    Edit2: To avoid the "template library" idea. I have a wrapper around the actual template classes. So there is no "empty" library.

    解决方案

    Here's a set of instructions that should help:

    A. Create library project:

    1. select File...New...CUDA C/C++ Project
    2. Select Static Library...Empty Project and give the project a name (test8)
    3. Next...Next...Finish to finish creating the project
    4. right click on project name in Project Explorer window, select New...Header File, give it a name (test8lib.h)
    5. edit test8lib.h (with contents from below), save it
    6. create another new header file for cuda template, (test8.cuh)
    7. edit test8.cuh (with contents from below), save it
    8. create a new source file, (test8.cu)
    9. edit test8.cu (with contents from below), save it
    10. select Project...Build Project (libtest8.a is now built)

    test8lib.h:

    #ifndef TEST8LIB_H_
    #define TEST8LIB_H_
    
    void calc_square_vec_float(float *in_data, float *out_data, int size);
    
    
    #endif /* TEST8LIB_H_ */
    

    test8.cuh:

    #ifndef TEST8_CUH_
    #define TEST8_CUH_
    
    template <typename T> __global__ void squareVector(T *input, T *output, int size) {
        int idx = threadIdx.x+blockDim.x*blockIdx.x;
        if (idx < size) output[idx]=input[idx]*input[idx];
    }
    
    
    #endif /* TEST8_CUH_ */
    

    test8.cu:

    #include "test8lib.h"
    #include "test8.cuh"
    #define nTPB 256
    
    void calc_square_vec_float(float *in_data, float *out_data, int size){
        float *d_in_data, *d_out_data;
        cudaMalloc(&d_in_data,  size*sizeof(float));
        cudaMalloc(&d_out_data, size*sizeof(float));
        cudaMemcpy(d_in_data, in_data, size*sizeof(float),cudaMemcpyHostToDevice);
        squareVector<<<(size+nTPB-1)/nTPB, nTPB>>>(d_in_data, d_out_data, size);
        cudaMemcpy(out_data, d_out_data, size*sizeof(float),cudaMemcpyDeviceToHost);
    }
    

    B. Create main project:

    1. File...new...C++ project...empty project...Linux GCC toolchain, give it a name (test9)
    2. Next...Finish to finish creating the project
    3. File...New Source File...Default C++ source template, give it a name (test9.cpp)
    4. edit the file with contents from below, save it.
    5. add the include path: Project...Properties...Build...Settings...Tool Settings...GCC C++ Compiler...Includes...Include Paths...Add and add the directory where test8lib.h is located.
    6. add the lib: Tool Settings...GCC C++ Linker...Libraries...Libraries...Add and add the name of the previously built library (test8)
    7. also add CUDA runtime library (cudart)
    8. add the lib path: Tool Settings...GCC C++ Linker...Libraries...Library Paths...Add and add the path to the previously built library (e.g. /path/to/cuda-workspace/test8/Debug)
    9. also add the path to cudart (e.g. /usr/local/cuda/lib64)
    10. Build Project
    11. Run Project

    test9.cpp:

    #include <stdio.h>
    #include <stdlib.h>
    #include "test8lib.h"
    #define DSIZE 4
    #define TEST_VAL 2.0f
    
    int main(){
        float *in, *out;
        in = (float *)malloc(DSIZE*sizeof(float));
        out = (float *)malloc(DSIZE*sizeof(float));
        for (int i=0; i<DSIZE; i++){
            in[i] = TEST_VAL;
            out[i] = 0.0f;
        }
        calc_square_vec_float(in, out, DSIZE);
        for (int i=0; i<DSIZE; i++)
            if (out[i] != (float)(TEST_VAL*TEST_VAL)){
                printf("mismatch at %d, was: %f, should be: %f\n", i, out[i], (float)(TEST_VAL*TEST_VAL));
                return 1;
            }
        printf("Success!\n");
        return 0;
    }
    

    这篇关于将静态cuda库包含到c ++项目中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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