一、编写cuda程序
# 检查cuda版本 nvcc -V # 编写cuda程序 helloworld.cu vim helloworld.cu #include <stdio.h> // CUDA Kernel函数,用于在GPU上并行执行 __global__ void helloCUDA() { // 获取当前线程的ID int threadID = blockIdx.x * blockDim.x + threadIdx.x; // 打印Hello World和线程ID printf("Hello World from thread %d\n", threadID); } int main() { // 定义线程块大小和线程块数量 int blockSize = 256; int numBlocks = 1; // 在GPU上启动Kernel函数 helloCUDA<<<numBlocks, blockSize>>>(); // 等待GPU执行完毕 cudaDeviceSynchronize(); return 0; } # 保存并退出
二、编译运行cuda程序
nvcc -o helloworld helloworld.cu
./helloworld