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

 

posted @ 2025-11-06 00:43  FredGrit  阅读(8)  评论(0)    收藏  举报