nvcc编译

文件后缀说明

后缀名Description说明
.cuCUDA source file, containing host code and device functionscuda源文件
.cC source filec源文件
.cc, .cxx, .cppC++ source fileC++源文件
.ptxPTX intermediate assembly file中间汇编文件
.cubinCUDA device code binary file (CUBIN) for a single GPU architecture包括一个GPU构架的设备二进制文件
.fatbinCUDA fat binary file that may contain multiple PTX and CUBIN files多个PTX和CUBIN的二进制文件
.o.obj Object file目标文件
.a,.lib Library file库文件
.resResource file资源文件
.soShared object file动态库文件

编译步骤

在这里插入图片描述

常用命令说明

阶段性编译命令

下面说的名字是没有使用–outputfile指明生成文件名的时候,按照默认生成的。

阶段编译器擦参数输入文件默认文件名
.cu文件编译成c/c++文件nvcc–cuda
-cuda
x.cux.cu.cpp.ii.
C/C++ 预处理–preprocess
-E
标准输出
C/C++ 编译成目标文件–compile
-c
x.o
cuda原文件生成cubin–cubin
-cubin
x.cux.cubin
ptx文件生成cubin–cubin
-cubin
x.ptxx.cubin
cu文件生成ptx–ptx
-ptx
x.cux.ptx
cu,ptx,cubin文件生成fatbinary–fatbin
-fatbin
x.fatbin
可链接设备代码生成–device-link
-dlink
a_dlink.o
cubin从可链接设备代码生成–device-link–cubin
-dlink-cubin
a_dlink.cubin
fatbinary从可链接设备代码生成–device-link–fatbin
-dlink-fatbin
a_dlink.fatbin
生成可执行文件a.out
生成库文件–lib
-lib
a.lib
make dependency generation–generate-dependencies
-M
标准输出
make dependency generation without headers in system paths–generate-nonsystem-dependencies
-MM
标准输出
Running an executable–run
-run

参考链接:https://docs.nvidia.com/cuda/cuda-compiler-driver-nvcc/index.html#options-for-steering-gpu-code-generation

### 使用 NVCC 在 Ubuntu 系统中编译 CUDA C++ 代码 #### 准备工作 为了确保能够在 Ubuntu 上成功使用 NVCC 编译 CUDA C++ 代码,需要先安装合适的 NVIDIA 驱动程序以及 CUDA Toolkit。对于 CUDA 10.0 支持的最高版本为 Ubuntu 18.04[^1]。 #### 安装 CUDA Toolkit 可以通过官方文档获取详细的安装指南,通常包括下载.run 文件或.deb 包并按照提示完成安装过程。安装完成后需设置环境变量 PATH 和 LD_LIBRARY_PATH 来指向 CUDA 的 bin 和 lib64 目录。 #### 创建简单的 CUDA C++ 源文件 创建一个新的 `.cu` 文件作为入口点,例如 `vectorAdd.cu`: ```cpp // vectorAdd.cu #include <stdio.h> __global__ void add(int *a, int *b, int *c) { int index = threadIdx.x; c[index] = a[index] + b[index]; } int main() { const int ARRAY_SIZE = 64; const int ARRAY_BYTES = ARRAY_SIZE * sizeof(int); // Host allocations and initializations... int ha[ARRAY_SIZE], hb[ARRAY_SIZE], hc[ARRAY_SIZE]; for (int i = 0; i < ARRAY_SIZE; ++i){ ha[i] = rand() % 100; hb[i] = rand() % 100; } // Device allocations... int *da, *db, *dc; cudaMalloc((void **)&da, ARRAY_BYTES); cudaMalloc((void **)&db, ARRAY_BYTES); cudaMalloc((void **)&dc, ARRAY_BYTES); cudaMemcpy(da, ha, ARRAY_BYTES, cudaMemcpyHostToDevice); cudaMemcpy(db, hb, ARRAY_BYTES, cudaMemcpyHostToDevice); add<<<1, ARRAY_SIZE>>>(da, db, dc); cudaMemcpy(hc, dc, ARRAY_BYTES, cudaMemcpyDeviceToHost); printf("Result:\n"); for (int i = 0; i< ARRAY_SIZE; i++) { printf("%d + %d = %d\n", ha[i], hb[i], hc[i]); } cudaFree(da); cudaFree(db); cudaFree(dc); return 0; } ``` #### 编写 Makefile 或者命令行调用 NVCC 进行编译 可以直接在终端输入以下命令来编译上述代码: ```bash nvcc -o vectorAdd vectorAdd.cu ``` 这将会生成名为 `vectorAdd` 的可执行文件。如果希望更灵活地管理项目结构,则可以考虑编写一个完整的 Makefile 或者利用 CMake 构建工具[^2]。 当涉及到静态链接库时,还可以参考如下 .sh 脚本内容来进行多阶段构建[^3]: ```bash #!/bin/bash # Compile the CUDA source into an object file. nvcc -c foo.cu -o foo.o # Create static library from object files. ar cr libfoo.a foo.o # List symbols defined within this archive. nm -g --defined-only libfoo.a # Link everything together with your application code. gcc main.c -o main libfoo.a -lcudart -lcuda -lstdc++ ``` 以上就是如何在 Ubuntu 中使用 NVCC 编译 CUDA C++ 代码的方法概述。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值