【问题标题】:compile CU and C files with CMake用 CMake 编译 CU 和 C 文件
【发布时间】:2020-02-05 16:04:43
【问题描述】:

我正在尝试使用 CMakeLists 编译包含 CUDA 文件和 C(非 C++)文件的程序。

我的程序由一些文件 .c(没有 CUDA)、一个文件 .c(调用 cuBLAS 函数和基本 CUDA 函数,如 cudaMalloc 并且它可以工作)和包含以下代码的 cuda.cu 组成(我把所有#includes 以防丢失):


#include "cuda_runtime.h"
#include <stdio.h>
#include <stdlib.h>
#include "device_launch_parameters.h"
#include <device_functions.h>
#include <assert.h>
#include <cuda.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <float.h>
#include <sys/time.h>
//#include "cublas_kernels.h"
#define TILE_DIM 32
#define BLOCK_ROWS 8

#define GRID_SIZE 32
#define BLOCK_SIZE 32


__global__ void kernelfunction(float *a, float *b, int n) {
    int i = threadIdx.x + blockIdx.x * blockDim.x;
    int stride = blockDim.x * gridDim.x;

    while (i < n)
    {
        b[i] = (a[i] > 0.f) ? 1.f : 0.f;
        i += stride;
    }
}

void function(float *a, float *b, int n)
{
    dim3 dimGrid(GRID_SIZE, GRID_SIZE);
    dim3 dimBlock(BLOCK_SIZE, BLOCK_SIZE);
    kernelfunction<<<dimGrid, dimBlock>>>(a, b, n);
}

这是 CMakeLists.txt 文件的 CUDA 部分:

    include(FindCUDA)
    find_package(CUDA)
    if(CUDA_FOUND)
        target_link_libraries(myprogram PRIVATE ${CUDA_CUBLAS_LIBRARIES} dl)
        target_link_libraries(myprogram PRIVATE ${CUDA_LIBRARIES} dl)
        set_target_properties(myprogram PROPERTIES CUDA_SEPARABLE_COMPILATION ON)
    endif()


当我尝试使用 CMakeLists 文件编译它时,我收到以下错误:

cuda.cu: undefined reference to `threadIdx'
cuda.cu: undefined reference to `blockIdx'
cuda.cu: undefined reference to `blockDim'
cuda.cu: undefined reference to `gridDim'

我猜我的 makefile 不完整,因为如果我用 nvcc 单独编译文件 (cuda.cu),它编译没有问题。

我的 nvcc 版本: Cuda 编译工具,9.1 版,V9.1.85

我的 CMake 版本: cmake 版本 3.10.2

【问题讨论】:

  • 我猜你正在尝试用你的 C 编译器而不是 nvcc 来编译 CUDA 源代码
  • 我会删除您正在显示的 CMakeLists.txt 的 CUDA 部分,而是使用 enable_language(CUDA)。
  • 更新到具有一流 CUDA 支持的较新 CMake 版本,并按照 Raul 的建议摆脱 FindCUDA 和 FindPackage(CUDA)。

标签: c compiler-errors compilation cuda cublas


【解决方案1】:

CUDA 是您的 cmake 版本中的一流语言(iirc,这是在 cmake 3.9 中添加的)。但是,创建新项目时默认不启用 CUDA 支持。

如果您的项目定义为 project(my_project_name),则只需指定您在其中使用的语言,例如project(my_objrect_name C CUDA)。然后 cmake 将使用 nvidia 编译器编译任何带有.cu 扩展名的文件

【讨论】:

    猜你喜欢
    • 2016-04-29
    • 2018-02-02
    • 2021-12-19
    • 2023-03-23
    • 2015-06-01
    • 2019-04-16
    • 2015-03-07
    • 1970-01-01
    • 2021-12-19
    相关资源
    最近更新 更多