使用 CMake 编译 CU 和 C 文件
compile CU and C files with CMake
我正在尝试使用 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
CUDA 是您的 cmake 版本中第一个 class 语言(iirc 这是在 cmake 3.9 中添加的)。但是,当您创建新项目时,默认情况下不会启用 CUDA 支持。
如果您的项目定义为 project(my_project_name)
,则只需指定您在那里使用的语言,例如project(my_objrect_name C CUDA)
。然后 cmake 将使用 nvidia 编译器编译具有 .cu
扩展名
的任何文件
我正在尝试使用 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
CUDA 是您的 cmake 版本中第一个 class 语言(iirc 这是在 cmake 3.9 中添加的)。但是,当您创建新项目时,默认情况下不会启用 CUDA 支持。
如果您的项目定义为 project(my_project_name)
,则只需指定您在那里使用的语言,例如project(my_objrect_name C CUDA)
。然后 cmake 将使用 nvidia 编译器编译具有 .cu
扩展名