当前位置: 首页 > news >正文

Win11 install CUDA 12.5

1.Check pc supported Nvidia GPU

nvidia-smi

image

 2.Download CUDA12.5

https://developer.download.nvidia.cn/compute/cuda/12.5.0/local_installers/cuda_12.5.0_555.85_windows.exe

3.Install CUDA12.5

//validate
nvcc --version


C:\Users\fred>nvcc --version
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2024 NVIDIA Corporation
Built on Wed_Apr_17_19:36:51_Pacific_Daylight_Time_2024
Cuda compilation tools, release 12.5, V12.5.40
Build cuda_12.5.r12.5/compiler.34177558_0

image

 

 

 

4.Save below file as vector_add.cu

// 文件名:vector_add.cu

#include <stdio.h>
#include <cuda_runtime.h> // 必须包含CUDA头文件// 1. 定义核函数
// __global__ 声明这是一个在GPU上运行的核函数
// 它执行的任务是:对于每个索引i, c[i] = a[i] + b[i]
__global__ void vectorAdd(const float *A, const float *B, float *C, int numElements) {// 2. 计算当前线程的全局索引// blockIdx.x: 当前Block在Grid中的索引// blockDim.x: 一个Block中的线程数量// threadIdx.x: 当前线程在Block中的索引int i = blockDim.x * blockIdx.x + threadIdx.x;// 3. 检查索引是否在有效范围内(防止越界)if (i < numElements) {C[i] = A[i] + B[i];}
}int main(void) {// 设置向量长度int numElements = 50000;size_t size = numElements * sizeof(float);printf("[Vector addition of %d elements]\n", numElements);// 4. 在主机上分配内存并初始化float *h_A = (float *)malloc(size);float *h_B = (float *)malloc(size);float *h_C = (float *)malloc(size); // 存放GPU计算结果// 初始化输入向量for (int i = 0; i < numElements; ++i) {h_A[i] = rand() / (float)RAND_MAX; // 0~1之间的随机数h_B[i] = rand() / (float)RAND_MAX;}// 5. 在设备上分配内存float *d_A = NULL;float *d_B = NULL;float *d_C = NULL;cudaMalloc((void **)&d_A, size);cudaMalloc((void **)&d_B, size);cudaMalloc((void **)&d_C, size);// 6. 将数据从主机内存拷贝到设备内存
    cudaMemcpy(d_A, h_A, size, cudaMemcpyHostToDevice);cudaMemcpy(d_B, h_B, size, cudaMemcpyHostToDevice);// 7. 启动核函数!// 配置线程结构int threadsPerBlock = 256;// 计算需要多少个Block: (N + threadsPerBlock - 1) / threadsPerBlock// 这是一个常见的向上取整除法技巧int blocksPerGrid = (numElements + threadsPerBlock - 1) / threadsPerBlock;printf("CUDA kernel launch with %d blocks of %d threads\n", blocksPerGrid, threadsPerBlock);// 调用核函数,语法:<<<blocksPerGrid, threadsPerBlock>>>vectorAdd<<<blocksPerGrid, threadsPerBlock>>>(d_A, d_B, d_C, numElements);// 8. 等待GPU上所有计算完成,再继续执行主机代码
    cudaDeviceSynchronize();// 9. 将计算结果从设备内存拷贝回主机内存
    cudaMemcpy(h_C, d_C, size, cudaMemcpyDeviceToHost);// 10. 验证结果 (可选,但很重要)for (int i = 0; i < numElements; ++i) {if (fabs(h_A[i] + h_B[i] - h_C[i]) > 1e-5) {fprintf(stderr, "Result verification failed at element %d!\n", i);exit(EXIT_FAILURE);}}printf("Test PASSED\n");// 11. 释放设备内存
    cudaFree(d_A);cudaFree(d_B);cudaFree(d_C);// 12. 释放主机内存
    free(h_A);free(h_B);free(h_C);printf("Done\n");system("pause");return 0;
}

 

 

5.Run via 

nvcc -o vector_add vector_add.cu

nvcc -o vector_add vector_add.cu
nvcc -o vector_add vector_add.cu
nvcc fatal   : Cannot find compiler 'cl.exe' in PATH

 

 

6.Configure the value in system variables path,then restart.

C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.xx.xxxxx\bin\Hostx64\x64

 

 

7.Run with error again,because CUDA12.5 only support Visual Studio 2022 and can not support Visual Studio 2026(Insiders).

D:\AI>nvcc -o vector_add vector_add.cu
vector_add.cu
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v12.5\include\crt/host_config.h(170): fatal error C1189: #error:  -- unsupported Microsoft Visual Studio version! Only the versions between 2017 and 2022 (inclusive) are supported! The nvcc flag '-allow-unsupported-compiler' can be used to override this version check; however, using an unsupported host compiler may cause compilation failure or incorrect run time execution. Use at your own risk.

 

8.Run with compatible mode

nvcc -o vector_add vector_add.cu -allow-unsupported-compiler

 

image

 

http://www.jsqmd.com/news/32688/

相关文章:

  • 机器学习-逻辑回归算法-向量版代码
  • 星期三
  • 「学习笔记」文件包含
  • 【AI说Rust 03】如何在 macos m1 系统搭建 rust 开发环境
  • 厨房小白学做饭——4.干锅菜花
  • 操盘计划202511090017
  • Effective C++
  • 厨房小白学做饭——3.虎皮青椒
  • 20251105 之所思 - 人生如梦
  • 【Kubernetes】入门-部署Spring应用
  • 第16天(简单题中等题 二分查找)
  • Java项目常用异常处理场景与实战指南
  • newDay18
  • 11月5日
  • 层级结构
  • 2025.11.5总结 - A
  • C# POST Form
  • 每日反思(2025_11_04)
  • C++练习2
  • 买完学习机还需要去线下补课吗? AI 学习机 + 自习室是最优解!
  • 一次性删除所有的GitHub Action记录
  • 第三十四篇
  • 2025-11-05 PQ v.Next日志记录
  • 11月5日日记
  • 20232319 2024-2025-1 《网络与系统攻防技术》实验四实验报告
  • 汉字识别
  • AGC与AVC是什么
  • 链表1
  • 競プロ典型 90 問-难题
  • c++函数调用的大致工作过程