Determining which gencode (compute_, arch_) values I need for nvcc - within CMake

前端 未结 3 845
情话喂你
情话喂你 2021-01-13 09:10

I\'m using CMake as a build system for my code, which involves CUDA. I was thinking of automating the task of deciding which compute_XX and arch_XX

3条回答
  •  醉话见心
    2021-01-13 09:33

    My strategy has been to compile and run a bash script that probes the card and returns the gencode for cmake. Inspiration came from University of Chicago's SLURM. To handle errors or multiple gpus or other circumstances, modify as necessary.

    In your project folder create a file cudaComputeVersion.bash and ensure it is executable from the shell. Into this file put:

    #!/bin/bash
    
    # create a 'here document' that is code we compile and use to probe the card
    cat << EOF > /tmp/cudaComputeVersion.cu
    #include 
    int main()
    {
    cudaDeviceProp prop;
    cudaGetDeviceProperties(&prop,0);
    int v = prop.major * 10 + prop.minor;
    printf("-gencode arch=compute_%d,code=sm_%d\n",v,v);
    }
    EOF
    
    # probe the card and cleanup
    /usr/local/cuda/bin/nvcc /tmp/cudaComputeVersion.cu -o /tmp/cudaComputeVersion
    /tmp/cudaComputeVersion
    rm /tmp/cudaComputeVersion.cu
    rm /tmp/cudaComputeVersion
    

    And in your CMakeLists.txt put:

    # at cmake-build-time, probe the card and set a cmake variable
    execute_process(COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/cudaComputeVersion.bash OUTPUT_VARIABLE GENCODE)
    # at project-compile-time, include the gencode into the compile options
    set(CUDA_NVCC_FLAGS ${CUDA_NVCC_FLAGS}; "${GENCODE}")
    
    # this makes CMake all chatty and allows you to see that GENCODE was set correctly
    set(CMAKE_VERBOSE_MAKEFILE TRUE)
    

    cheers

提交回复
热议问题