用Cmake 编译cuda程序

运行系统及版本:

Jetson TK1
cmake version 2.8.12.2
Cuda compilation tools, release 6.0, V6.0.1

直接上代码

CmakeLists.txt
  1 cmake_minimum_required(VERSION 2.8)
  2
  3 project(vectorAdd)
  4
  5 FIND_PACKAGE(CUDA REQUIRED)
  6
  7 # set source files
  8 set(PROJECT_SRC vectorAdd.cu)
  9
 10 # build options
 11 set(GENCODE -gencode=arch=compute_30,code=sm_30 -gencode=arch=compute    _35,code=compute_35)
 12 set(GENCODE ${GENCODE} -gencode=arch=compute_20,code=sm_20)
 13 #set(GENCODE ${GENCODE} -gencode=arch=compute_10,code=sm_10)
 14
 15 # debug options
 16 set(CUDA_NVCC_FLAGS_DEBUG "-g -G")
 17 set(CMAKE_CXX_FLAGS_DEBUG "-g")
 18
 19 # release options
 20 set(CUDA_NVCC_FLAGS_RELEASE "-O2")
 21 set(CMAKE_CXX_FLAGS_RELEASE "-O2")
 22 #cuda_add_executable(vectorAdd vectorAdd.cu)
 23 cuda_add_executable(vectorAdd ${PROJECT_SRC} OPTIONS ${GENCODE})

vectorAdd.cu 代码见: ~/NVIDIA_CUDA-6.0_Samples/0_Simple/vectorAdd/vectorAdd.cu

运行效果:


ubuntu@tegra-ubuntu:~/work/learn_cuda/vector_add/build$ cmake ..
-- The C compiler identification is GNU 4.8.2
-- The CXX compiler identification is GNU 4.8.2
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Found CUDA: /usr/local/cuda (found version "6.0")
-- Configuring done
-- Generating done
-- Build files have been written to: /home/ubuntu/work/learn_cuda/vector_add/build
ubuntu@tegra-ubuntu:~/work/learn_cuda/vector_add/build$ make
-- Configuring done
-- Generating done
-- Build files have been written to: /home/ubuntu/work/learn_cuda/vector_add/build
make[2]: Warning: File `../vectorAdd.cu' has modification time 4.7e+08 s in the future
[100%] Building NVCC (Device) object CMakeFiles/vectorAdd.dir//./vectorAdd_generated_vectorAdd.cu.o
Scanning dependencies of target vectorAdd
make[2]: warning:  Clock skew detected.  Your build may be incomplete.
make[2]: Warning: File `../vectorAdd.cu' has modification time 4.7e+08 s in the future
[100%] Building NVCC (Device) object CMakeFiles/vectorAdd.dir//./vectorAdd_generated_vectorAdd.cu.o
Linking CXX executable vectorAdd
make[2]: warning:  Clock skew detected.  Your build may be incomplete.
[100%] Built target vectorAdd
ubuntu@tegra-ubuntu:~/work/learn_cuda/vector_add/build$ ls
CMakeCache.txt  CMakeFiles  cmake_install.cmake  Makefile  vectorAdd
ubuntu@tegra-ubuntu:~/work/learn_cuda/vector_add/build$ ./vectorAdd
[Vector addition of 50000 elements]
Copy input data from the host memory to the CUDA device
CUDA kernel launch with 196 blocks of 256 threads
Copy output data from the CUDA device to the host memory
Test PASSED
Done
ubuntu@tegra-ubuntu:~/work/learn_cuda/vector_add/build$


  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要使用CMake编译CUDA代码,你需要按照以下步骤进行操作: 1. 确保你已经安装了CUDA工具包,并且CUDA的bin目录已经添加到了系统的环境变量中。 2. 创建一个CMakeLists.txt文件,用于描述你的项目的构建过程。在该文件中,你需要指定CUDA的相关设置。 以下是一个示例的CMakeLists.txt文件: ``` cmake_minimum_required(VERSION 3.17) project(your_project_name CUDA) # 设置CUDA的相关设置 find_package(CUDA REQUIRED) include_directories(${CUDA_INCLUDE_DIRS}) set(CUDA_NVCC_FLAGS ${CUDA_NVCC_FLAGS};-O3 -arch=sm_35) # 添加你的源文件 add_executable(your_executable_name your_source_files.cu) ``` 在上述示例中,通过`find_package(CUDA REQUIRED)`来查找CUDA,并使用`include_directories`添加CUDA的头文件目录。 通过`set(CUDA_NVCC_FLAGS ...)`设置NVCC编译选项,例如指定优化级别和计算能力。 通过`add_executable`添加你的CUDA源文件。 3. 在项目的根目录下创建一个build目录,然后进入该目录。 4. 运行CMake来生成构建文件。可以使用下面的命令: ``` cmake .. ``` 这将根据CMakeLists.txt文件生成适用于你的操作系统的构建文件。 5. 运行构建命令来编译你的项目。可以使用下面的命令: ``` cmake --build . ``` 这将根据生成的构建文件进行编译。 完成上述步骤后,你的CUDA项目应该成功编译。请注意,以上仅提供了一个基本示例,你可能需要根据你的项目的实际情况进行适当的修改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值