cmake混合编译cuda和c++代码

Qwen3-32B-Chat 私有部署镜像 | RTX4090D 24G 显存 CUDA12.4 优化版

Qwen3-32B-Chat 私有部署镜像 | RTX4090D 24G 显存 CUDA12.4 优化版

Qwen
文本生成
Qwen3

本镜像基于 RTX 4090D 24GB 显存 + CUDA 12.4 + 驱动 550.90.07 深度优化,内置完整运行环境与 Qwen3-32B 模型依赖,开箱即用。

cmake混合编译cpp和cuda代码

实际背景

在cpp项目中需要调用一些.cu代码,但是两个代码的编译是不同的:cpp代码使用g++编译,cuda代码使用nvvc编译,因此需要分别编译,再链接,同时需要在cuda侧进行一些简单的封装。

简单例子

目录结构
projectDir
├── CMakeLists.txt(final project)
├── src.cpp
├── ...
├── cudaCode
│   ├── CMakeLists.txt(cuda project)
│   ├── cudaTest.cu
│   └── cudaTest.h

其中cudaTest.h声明了函数,cudaTest.cu,定义了声明cuda的函数体,调用了cuda api

CMakeLists for cuda project

将cuda编译成动态库

cmake_minimum_required (VERSION 3.8 FATAL_ERROR)
project (GpuBTree)

find_package(CUDA 8.0 REQUIRED)

option(CMAKE_VERBOSE_MAKEFILE ON)
option(DGTEST,  "DGTEST"  ON)

set(CUDA_NVCC_FLAGS -std=c++11)
set (CMAKE_CXX_STANDARD 11)

if (CUDA_VERBOSE_PTXAS)
  set(VERBOSE_PTXAS --ptxas-options=-v)
endif (CUDA_VERBOSE_PTXAS)

#set(CMAKE_BUILD_TYPE "Release")

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)

set(GENCODE_SM30
  -gencode=arch=compute_30,code=sm_30 -gencode=arch=compute_30,code=compute_30)
set(GENCODE_SM35
  -gencode=arch=compute_35,code=sm_35 -gencode=arch=compute_35,code=compute_35)
set(GENCODE_SM37
  -gencode=arch=compute_37,code=sm_37 -gencode=arch=compute_37,code=compute_37)
set(GENCODE_SM50
  -gencode=arch=compute_50,code=sm_50 -gencode=arch=compute_50,code=compute_50)
set(GENCODE_SM60
  -gencode=arch=compute_60,code=sm_60 -gencode=arch=compute_60,code=compute_60)
set(GENCODE_SM61
  -gencode=arch=compute_61,code=sm_61 -gencode=arch=compute_61,code=compute_61)
set(GENCODE_SM70
  -gencode=arch=compute_70,code=sm_70 -gencode=arch=compute_70,code=compute_70)
set(GENCODE_SM71
  -gencode=arch=compute_71,code=sm_71 -gencode=arch=compute_71,code=compute_71)
set(GENCODE_SM75
-gencode=arch=compute_75,code=sm_75 -gencode=arch=compute_75,code=compute_75)

option(GPUBTREE_GENCODE_SM30 "GENCODE_SM30" OFF)
option(GPUBTREE_GENCODE_SM35 "GENCODE_SM35" OFF)
option(GPUBTREE_GENCODE_SM37 "GENCODE_SM37" OFF)
option(GPUBTREE_GENCODE_SM50 "GENCODE_SM50" OFF)
option(GPUBTREE_GENCODE_SM60 "GENCODE_SM60" OFF)
option(GPUBTREE_GENCODE_SM61 "GENCODE_SM61" OFF)
option(GPUBTREE_GENCODE_SM70 "GENCODE_SM70" OFF)
option(GPUBTREE_GENCODE_SM71 "GENCODE_SM71" OFF)
option(GPUBTREE_GENCODE_SM75 "GENCODE_SM75" ON)

if (GPUBTREE_GENCODE_SM30)
  set(GENCODE ${GENCODE} ${GENCODE_SM30})
endif(GPUBTREE_GENCODE_SM30)

if (GPUBTREE_GENCODE_SM35)
  set(GENCODE ${GENCODE} ${GENCODE_SM35})
endif(GPUBTREE_GENCODE_SM35)

if (GPUBTREE_GENCODE_SM37)
  set(GENCODE ${GENCODE} ${GENCODE_SM37})
endif(GPUBTREE_GENCODE_SM37)

if (GPUBTREE_GENCODE_SM50)
  set(GENCODE ${GENCODE} ${GENCODE_SM50})
endif(GPUBTREE_GENCODE_SM50)

if (GPUBTREE_GENCODE_SM60)
  set(GENCODE ${GENCODE} ${GENCODE_SM60})
endif(GPUBTREE_GENCODE_SM60)

if (GPUBTREE_GENCODE_SM61)
  set(GENCODE ${GENCODE} ${GENCODE_SM61})
endif(GPUBTREE_GENCODE_SM61)

if (GPUBTREE_GENCODE_SM70)
  set(GENCODE ${GENCODE} ${GENCODE_SM70})
endif(GPUBTREE_GENCODE_SM70)

if(GPUBTREE_GENCODE_SM71)
  set(GENCODE ${GENCODE} ${GENCODE_SM71})
endif(GPUBTREE_GENCODE_SM71)

if(GPUBTREE_GENCODE_SM75)
  set(GENCODE ${GENCODE} ${GENCODE_SM75})
endif(GPUBTREE_GENCODE_SM75)

set(CUFILES
  cudaTest.h
  cudaTest.cu)

cuda_add_library(cuda_test SHARED
	${CUFILES}
	OPTIONS ${GENCODE} ${VERBOSE_PTXAS})

CMakeLists for final project

在主函数中生成可执行文件,并链接

cmake_minimum_required(VERSION 3.3)

project(GpuTest)

add_subdirectory(cudaCode)
include_directories(cudaCode)

set(test_source_file src.cpp)
add_executable(test_src ${test_source_file})
target_link_libraries(
  test_src
  cuda_test
)

您可能感兴趣的与本文相关的镜像

Qwen3-32B-Chat 私有部署镜像 | RTX4090D 24G 显存 CUDA12.4 优化版

Qwen3-32B-Chat 私有部署镜像 | RTX4090D 24G 显存 CUDA12.4 优化版

Qwen
文本生成
Qwen3

本镜像基于 RTX 4090D 24GB 显存 + CUDA 12.4 + 驱动 550.90.07 深度优化,内置完整运行环境与 Qwen3-32B 模型依赖,开箱即用。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值