动手配置CUDA执行环境
本文记录了如何在Windows操作系统上配置CUDA并行计算环境,并使用Clion 集成开发环境对CUDA 程序进行开发。
动手配置CUDA并行计算执行环境
在正式开始之前,请观察自己的CUDA版本,CUDA版本与Visual Studio版本
,CMAKE版本
都有关系。环境配置不成功多半是版本之间不协调的问题
CMAKE
CMAKE是一个支持跨平台的C++工程构建工具。对于CUDA工程来说,CLion官网要求CMAKE版本需要时3.8.0+。而CLion自带的CMAKE往往版本偏低,配置环境时需要自查CMAKE版本是否为3.8以上,若不是,需要在官网进行下载,以3.8.2版本的CMAKE为例。在CMAKE Archieve中找到自己平台上的安装包进行下载,并且安装。安装时不需要添加到环境变量,无脑选一个喜欢的盘区安装即可。
编译工具链
在Windows平台上进行CUDA开发,只能使用微软Visiual Studio下的MSVC
编译工具链(类似Windows 下的mingw,linux 下的gcc)
笔者之前一直在Linux平台上远程开发,对Visual Studio不熟悉,发现Visio Studio并不是所有版本都有社区版的。专业版需要花钱,心痛。
笔者这里遇到的最崩溃的问题是VS的版本与CUDA的版本兼容性的要求。参考这篇Stack Overflow博文。最开始博主的CUDA版本是12.3,所搭配的Visual Studio版本是19.10。不巧的是这二者并不兼容。最关键的一处在该图片。
考虑到Visio Studio并不是所有版本都有社区版,因此升级CUDA
是更靠谱的选择。
下载/升级CUDA
博主这里选择的是CUDA12.5进行下载,能够适配Visio Stdudio 19.10版本的MSVC工具链。以CUDA12.5的下载为例,选择自己的计算平台,点这里下载。下载完毕后需要记住CUDA的安装路径。
下载这里也是有坑的,推荐自定义安装。自定义安装过程中往往会出现一些问题,检查下面的几个点
- 检查C盘空闲空间,建议留出2-5G空闲空间
- 博主这里安装CUDA下的
Visio Studio Integration
以及Nsight 相关的东西
会安装失败。如果不使用Visio Studio进行开发,这些不安装完全OK。最关键的是CUDA 下的Development
和Runtime
。这个是CUDA执行一定要有的组件。
安装完成后检查环境变量,系统全局的CUDA变量需要写在CUDA_PATH
中
配置CLion
新建一个项目,选择CUDA Executable。
在File | Settings | Build, Execution, Deployment
中的Toolchain中做如下设置,需要填入刚刚下载好的Visio Stdudio 路径
,千万不能偷懒用自带的Mingw,Windows平台下的CUDA开发只支持MSVC工具链!平台选择amd64
, CMAKE这里一定要改成自己安装的CMAKE路径
。
在File | Settings | Build, Execution, Deployment
中的Cmake中设置CUDA编译器NVCC的路径。可参考博主的写法。
-DCMAKE_CUDA_COMPILER=“E:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v12.5/bin/nvcc.exe”
在Cmake options配置该编译选项。
Cmake配置写法可参考下图:这里需要自行百度自己GPU的CUDA_ARCHITECTURE,这个变量用于指示CUDA的计算能力。
输入如下代码:
#include <cuda_runtime.h>
#include <stdio.h>
int main(int argc,char** argv)
{
printf("%s Starting ...\n",argv[0]);
int deviceCount = 0;
cudaError_t error_id = cudaGetDeviceCount(&deviceCount);
if(error_id!=cudaSuccess)
{
printf("cudaGetDeviceCount returned %d\n ->%s\n",
(int)error_id,cudaGetErrorString(error_id));
printf("Result = FAIL\n");
exit(EXIT_FAILURE);
}
if(deviceCount==0)
{
printf("There are no available device(s) that support CUDA\n");
}
else
{
printf("Detected %d CUDA Capable device(s)\n",deviceCount);
}
int dev=0,driverVersion=0,runtimeVersion=0;
cudaSetDevice(dev);
cudaDeviceProp deviceProp;
cudaGetDeviceProperties(&deviceProp,dev);
printf("Device %d:\"%s\"\n",dev,deviceProp.name);
cudaDriverGetVersion(&driverVersion);
cudaRuntimeGetVersion(&runtimeVersion);
printf(" CUDA Driver Version / Runtime Version %d.%d / %d.%d\n",
driverVersion/1000,(driverVersion%100)/10,
runtimeVersion/1000,(runtimeVersion%100)/10);
printf(" CUDA Capability Major/Minor version number: %d.%d\n",
deviceProp.major,deviceProp.minor);
printf(" Total amount of global memory: %.2f GBytes (%llu bytes)\n",
(float)deviceProp.totalGlobalMem/pow(1024.0,3),deviceProp.totalGlobalMem);
printf(" GPU Clock rate: %.0f MHz (%0.2f GHz)\n",
deviceProp.clockRate*1e-3f,deviceProp.clockRate*1e-6f);
printf(" Memory Bus width: %d-bits\n",
deviceProp.memoryBusWidth);
if (deviceProp.l2CacheSize)
{
printf(" L2 Cache Size: %d bytes\n",
deviceProp.l2CacheSize);
}
printf(" Max Texture Dimension Size (x,y,z) 1D=(%d),2D=(%d,%d),3D=(%d,%d,%d)\n",
deviceProp.maxTexture1D,deviceProp.maxTexture2D[0],deviceProp.maxTexture2D[1]
,deviceProp.maxTexture3D[0],deviceProp.maxTexture3D[1],deviceProp.maxTexture3D[2]);
printf(" Max Layered Texture Size (dim) x layers 1D=(%d) x %d,2D=(%d,%d) x %d\n",
deviceProp.maxTexture1DLayered[0],deviceProp.maxTexture1DLayered[1],
deviceProp.maxTexture2DLayered[0],deviceProp.maxTexture2DLayered[1],
deviceProp.maxTexture2DLayered[2]);
printf(" Total amount of constant memory %lu bytes\n",
deviceProp.totalConstMem);
printf(" Total amount of shared memory per block: %lu bytes\n",
deviceProp.sharedMemPerBlock);
printf(" Total number of registers available per block:%d\n",
deviceProp.regsPerBlock);
printf(" Wrap size: %d\n",deviceProp.warpSize);
printf(" Maximun number of thread per multiprocesser: %d\n",
deviceProp.maxThreadsPerMultiProcessor);
printf(" Maximun number of thread per block: %d\n",
deviceProp.maxThreadsPerBlock);
printf(" Maximun size of each dimension of a block: %d x %d x %d\n",
deviceProp.maxThreadsDim[0],deviceProp.maxThreadsDim[1],deviceProp.maxThreadsDim[2]);
printf(" Maximun size of each dimension of a grid: %d x %d x %d\n",
deviceProp.maxGridSize[0],
deviceProp.maxGridSize[1],
deviceProp.maxGridSize[2]);
printf(" Maximu memory pitch %lu bytes\n",deviceProp.memPitch);
printf("----------------------------------------------------------\n");
printf("Number of multiprocessors: %d\n", deviceProp.multiProcessorCount);
printf("Total amount of constant memory: %4.2f KB\n",
deviceProp.totalConstMem/1024.0);
printf("Total amount of shared memory per block: %4.2f KB\n",
deviceProp.sharedMemPerBlock/1024.0);
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 block: %d\n", deviceProp.maxThreadsPerBlock);
printf("Maximum number of threads per multiprocessor: %d\n",
deviceProp.maxThreadsPerMultiProcessor);
printf("Maximum number of warps per multiprocessor: %d\n",
deviceProp.maxThreadsPerMultiProcessor/32);
return EXIT_SUCCESS;
}
观察控制台可以看到配置成功了。