前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >windows cuda安装_虚拟机 cuda

windows cuda安装_虚拟机 cuda

作者头像
全栈程序员站长
发布2022-09-21 20:42:28
2.3K0
发布2022-09-21 20:42:28
举报

大家好,又见面了,我是你们的朋友全栈君。

1. cuda的安装

https://developer.nvidia.com/cuda-downloads (旧:URL )去下载。在安装的时候一定要自定义安装,否则将会安装很多无用的东西。安装的选项,可以选择不更新驱动程序。

windows cuda安装_虚拟机 cuda
windows cuda安装_虚拟机 cuda

或者下载离线文件安装

windows cuda安装_虚拟机 cuda
windows cuda安装_虚拟机 cuda

安装,选择自定义安装。

windows cuda安装_虚拟机 cuda
windows cuda安装_虚拟机 cuda
windows cuda安装_虚拟机 cuda
windows cuda安装_虚拟机 cuda
windows cuda安装_虚拟机 cuda
windows cuda安装_虚拟机 cuda

安装后,和英伟达cuda相关的程序如下图所示。

windows cuda安装_虚拟机 cuda
windows cuda安装_虚拟机 cuda

注意,千万不要勾选 Nsight Visual Studio Edition 2019.2等类似的无用的东西。

2. 测试环境是否安装成功

运行cmd,输入nvcc --version 即可查看版本号;

set cuda,可以查看cuda设置的环境变量。

windows cuda安装_虚拟机 cuda
windows cuda安装_虚拟机 cuda

3. 运行官方自带的demo

在任务管理器中搜索,Browse CUDA Samples。 或者一般位于 C:\ProgramData\NVIDIA Corporation\CUDA Samples

windows cuda安装_虚拟机 cuda
windows cuda安装_虚拟机 cuda

未编译前,Debug文件夹中只有三个文件,如图。

windows cuda安装_虚拟机 cuda
windows cuda安装_虚拟机 cuda

成功编译后这个位置(具体路径见上图)将生成很多文件,在其中找到deviceQueryDrv.exe的程序拖入到cmd中,回车运行。

4. 自己配置cuda项目

(1)打开vs2017,创建一个空win32程序,即cuda_test项目。

(2)选择cuda_test,点击右键–>项目依赖项–>自定义生成,选择CUDA10.1。

(3)右键源文件文件夹->添加->新建项->选择CUDA C/C++File,取名cuda_main。

(4)点击cuda_main.cu的属性,在配置属性–>常规–>项类型–>选择“CUDA C/C++”。

注意:以下步骤中的项目属性设置均针对x64。

(5)包含目录配置:

  右键点击项目属性–>属性–>配置属性–>VC++目录–>包含目录

  添加包含目录:$(CUDA_PATH)\include

(6)库目录配置

  VC++目录–>库目录

  添加库目录:$(CUDA_PATH)\lib\x64

(7)依赖项

  配置属性–>链接器–>输入–>附加依赖项

  添加库文件:cublas.lib;cuda.lib;cudadevrt.lib;cudart.lib;cudart_static.lib;OpenCL.lib

cuda_main.cu代码如下:

代码语言:javascript
复制
#include "cuda_runtime.h" #include "cublas_v2.h" #include <time.h> #include <iostream> using namespace std; // 定义测试矩阵的维度 int const M = 5; int const N = 10; int main() { // 定义状态变量 cublasStatus_t status; // 在 内存 中为将要计算的矩阵开辟空间 float *h_A = (float*)malloc(N*M * sizeof(float)); float *h_B = (float*)malloc(N*M * sizeof(float)); // 在 内存 中为将要存放运算结果的矩阵开辟空间 float *h_C = (float*)malloc(M*M * sizeof(float)); // 为待运算矩阵的元素赋予 0-10 范围内的随机数 for (int i = 0; i < N*M; i++) { h_A[i] = (float)(rand() % 10 + 1); h_B[i] = (float)(rand() % 10 + 1); } // 打印待测试的矩阵 cout << "矩阵 A :" << endl; for (int i = 0; i < N*M; i++) { cout << h_A[i] << " "; if ((i + 1) % N == 0) cout << endl; } cout << endl; cout << "矩阵 B :" << endl; for (int i = 0; i < N*M; i++) { cout << h_B[i] << " "; if ((i + 1) % M == 0) cout << endl; } cout << endl; /* ** GPU 计算矩阵相乘 */ // 创建并初始化 CUBLAS 库对象 cublasHandle_t handle; status = cublasCreate(&handle); if (status != CUBLAS_STATUS_SUCCESS) { if (status == CUBLAS_STATUS_NOT_INITIALIZED) { cout << "CUBLAS 对象实例化出错" << endl; } getchar(); return EXIT_FAILURE; } float *d_A, *d_B, *d_C; // 在 显存 中为将要计算的矩阵开辟空间 cudaMalloc( (void**)&d_A, // 指向开辟的空间的指针 N*M * sizeof(float) // 需要开辟空间的字节数 ); cudaMalloc( (void**)&d_B, N*M * sizeof(float) ); // 在 显存 中为将要存放运算结果的矩阵开辟空间 cudaMalloc( (void**)&d_C, M*M * sizeof(float) ); // 将矩阵数据传递进 显存 中已经开辟好了的空间 cublasSetVector( N*M, // 要存入显存的元素个数 sizeof(float), // 每个元素大小 h_A, // 主机端起始地址 1, // 连续元素之间的存储间隔 d_A, // GPU 端起始地址 1 // 连续元素之间的存储间隔 ); cublasSetVector( N*M, sizeof(float), h_B, 1, d_B, 1 ); // 同步函数 cudaThreadSynchronize(); // 传递进矩阵相乘函数中的参数,具体含义请参考函数手册。 float a = 1; float b = 0; // 矩阵相乘。该函数必然将数组解析成列优先数组 cublasSgemm( handle, // blas 库对象 CUBLAS_OP_T, // 矩阵 A 属性参数 CUBLAS_OP_T, // 矩阵 B 属性参数 M, // A, C 的行数 M, // B, C 的列数 N, // A 的列数和 B 的行数 &a, // 运算式的 α 值 d_A, // A 在显存中的地址 N, // lda d_B, // B 在显存中的地址 M, // ldb &b, // 运算式的 β 值 d_C, // C 在显存中的地址(结果矩阵) M // ldc ); // 同步函数 cudaThreadSynchronize(); // 从 显存 中取出运算结果至 内存中去 cublasGetVector( M*M, // 要取出元素的个数 sizeof(float), // 每个元素大小 d_C, // GPU 端起始地址 1, // 连续元素之间的存储间隔 h_C, // 主机端起始地址 1 // 连续元素之间的存储间隔 ); // 打印运算结果 cout << "计算结果的转置 ( (A*B)的转置 ):" << endl; for (int i = 0; i < M*M; i++) { cout << h_C[i] << " "; if ((i + 1) % M == 0) cout << endl; } // 清理掉使用过的内存 free(h_A); free(h_B); free(h_C); cudaFree(d_A); cudaFree(d_B); cudaFree(d_C); // 释放 CUBLAS 库对象 cublasDestroy(handle); getchar(); return 0; } 

5 使用VS下的模板创建

打开VS 2017,我们可以观察到,在VS2017模板一栏下方出现了“NVIDIA/CUDA 10.1”。

windows cuda安装_虚拟机 cuda
windows cuda安装_虚拟机 cuda

直接新建一个CUDA 10.1 Runtime 项目。

右键项目 → 属性 → 配置属性 → 链接器 → 常规 → 附加库目录,添加以下目录:

$(CUDA_PATH_V10_0)\lib$(Platform)

示例代码如下:

代码语言:javascript
复制
#include "cuda_runtime.h" #include "device_launch_parameters.h" #include <stdio.h> int main() { int deviceCount; cudaGetDeviceCount(&deviceCount); int dev; for (dev = 0; dev < deviceCount; dev++) { int driver_version(0), runtime_version(0); cudaDeviceProp deviceProp; cudaGetDeviceProperties(&deviceProp, dev); if (dev == 0) if (deviceProp.minor = 9999 && deviceProp.major == 9999) printf("\n"); printf("\nDevice%d:\"%s\"\n", dev, deviceProp.name); cudaDriverGetVersion(&driver_version); printf("CUDA驱动版本: %d.%d\n", driver_version / 1000, (driver_version % 1000) / 10); cudaRuntimeGetVersion(&runtime_version); printf("CUDA运行时版本: %d.%d\n", runtime_version / 1000, (runtime_version % 1000) / 10); printf("设备计算能力: %d.%d\n", deviceProp.major, deviceProp.minor); printf("Total amount of Global Memory: %u bytes\n", deviceProp.totalGlobalMem); printf("Number of SMs: %d\n", deviceProp.multiProcessorCount); printf("Total amount of Constant Memory: %u bytes\n", deviceProp.totalConstMem); printf("Total amount of Shared Memory per block: %u bytes\n", deviceProp.sharedMemPerBlock); printf("Total number of registers available per block: %d\n", deviceProp.regsPerBlock); printf("Warp size: %d\n", deviceProp.warpSize); printf("Maximum number of threads per SM: %d\n", deviceProp.maxThreadsPerMultiProcessor); printf("Maximum number of threads per block: %d\n", deviceProp.maxThreadsPerBlock); printf("Maximum size of each dimension of a block: %d x %d x %d\n", deviceProp.maxThreadsDim[0], deviceProp.maxThreadsDim[1], deviceProp.maxThreadsDim[2]); printf("Maximum size of each dimension of a grid: %d x %d x %d\n", deviceProp.maxGridSize[0], deviceProp.maxGridSize[1], deviceProp.maxGridSize[2]); printf("Maximum memory pitch: %u bytes\n", deviceProp.memPitch); printf("Texture alignmemt: %u bytes\n", deviceProp.texturePitchAlignment); printf("Clock rate: %.2f GHz\n", deviceProp.clockRate * 1e-6f); printf("Memory Clock rate: %.0f MHz\n", deviceProp.memoryClockRate * 1e-3f); printf("Memory Bus Width: %d-bit\n", deviceProp.memoryBusWidth); } return 0; }

参考文章

win10+VS2017+Cuda10.0环境配置

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/166473.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1. cuda的安装
  • 2. 测试环境是否安装成功
  • 3. 运行官方自带的demo
  • 4. 自己配置cuda项目
  • 5 使用VS下的模板创建
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档