CUDA compilation issue with CMake

后端 未结 3 506
灰色年华
灰色年华 2021-02-05 11:22

I am having issues with compiling my CUDA code with CMake. I am using CUDA 7 and the version information from nvcc is as follows:

nvcc: NVIDIA (R) Cuda compiler          


        
相关标签:
3条回答
  • 2021-02-05 11:30

    This worked for me using CUDA 7, gcc 4.8.2 and CMake 3.0.2.

    I updated the code and added a simple thrust-based example to make it clear that you can use C++11 in CUDA code

    CMakeLists.txt

    project(cpp11)
    find_package(CUDA)
    list(APPEND CUDA_NVCC_FLAGS "-arch=sm_20;-std=c++11;-O2;-DVERBOSE")
    SET(CUDA_PROPAGATE_HOST_FLAGS OFF)
    CUDA_ADD_EXECUTABLE(cpp11 main.cpp test.h test.cu)
    

    test.h

    #ifndef TEST_H
    #define TEST_H
    int run();
    #endif
    

    test.cu

    #include "test.h"
    #include <thrust/device_vector.h>
    #include <thrust/reduce.h>
    #include <thrust/sequence.h>
    
    template<typename T>
    struct Fun
    {
            __device__ T operator()(T t1, T t2)
            {
                auto result = t1+t2;
                return result;
            }
    };
    
    int run()
    {
        const int N = 100;
        thrust::device_vector<int> vec(N);
        thrust::sequence(vec.begin(),vec.end());
        auto op = Fun<int>();
        return thrust::reduce(vec.begin(),vec.end(),0,op);
    }
    

    main.cpp

    #include <iostream>
    #include "test.h"
    
    int main()
    {
        std::cout << run() << std::endl;
        return 0;
    }
    
    0 讨论(0)
  • 2021-02-05 11:44

    If stumbling across this question while searching for a way to compile Genoils CPP-Ethereum build for Ethereum CUDA mining, my problem was solved by editing the CMakeLists.txt file in the cpp-ethereum/libethash-cuda folder.

    Where it states:

    set(CUDA_NVCC_FLAGS
    ${CUDA_NVCC_FLAGS};
    -gencode etc etc)
    

    add "-std=c++11" after the semi-colon, as follows:

    set(CUDA_NVCC_FLAGS 
    ${CUDA_NVCC_FLAGS};
    -std=c++11
    -gencode etc etc) 
    
    0 讨论(0)
  • 2021-02-05 11:52

    list(APPEND CUDA_NVCC_FLAGS "-std=c++11") is enough,SET(CUDA_PROPAGATE_HOST_FLAGS OFF) may be not necessary, and it cause me could not set breakpoint in .cu file

    0 讨论(0)
提交回复
热议问题