为完成CUDA 和OPEN CL的任务, 这两天配置了基于WIN10和VS2012/2015的环境。
分了3篇记录, 另外两篇分别是:
解决win10+VS2012+CUDA NVIDIA安装失败
注:
- 我同时装了中文版的VS2012和英文版的VS2015, 总体来说两者在配置上一样, 为方便其他朋友理解, 我截图了中文版的VS2012。
流程如下:
1. 新建项目
2. 添加文件
文件名称后缀 按需用 .c 或.cpp 等
3. 设置生成属性
点击项目右键, 选择生成自定义,并勾选CUDA10.2
4. 设置包含和库:
右键项目-> 属性 ->VC++目录->包含目录, 库目录
“包含目录”中选择路径:
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.2\include
“库目录”中选择路径:
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.2\lib\x64
需要注意的是,如果创建的是Win32控制台应用程序,必须选择lib下的Win32目录,而不能选择x64目录。
5. 添加依赖项:
右键项目-> 属性 ->连接器 ->输入 ->附加依赖项
添加:
OpenCL.lib
cudart_static.lib
6. 选择文件编译类型为cuda
右键文件 -> 属性 -> 配置属性-> 常规 -> 项类型
选择CUDA C/C++
7. 放段代码测试一下:
sample.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#ifdef MAC
#include <OpenCL/cl.h>
#else
#include <CL/cl.h>
#endif
int main() {
/* Host data structures */
cl_platform_id *platforms;
cl_uint num_platforms;
cl_int i, err, platform_index = -1;
/* Extension data */
char* ext_data;
size_t ext_size;
const char icd_ext[] = "cl_khr_icd";
err = clGetPlatformIDs(5, NULL, &num_platforms);
if(err < 0) {
perror("Couldn't find any platforms.");
exit(1);
}
printf("I have platforms: %d\n", num_platforms);
/* Access all installed platforms */
platforms = (cl_platform_id*)
malloc(sizeof(cl_platform_id) * num_platforms);
clGetPlatformIDs(num_platforms, platforms, NULL);
/* Find extensions of all platforms */
for(i=0; i<num_platforms; i++)
{
/* Find size of extension data */
err = clGetPlatformInfo(platforms[i],
CL_PLATFORM_EXTENSIONS, 0, NULL, &ext_size);
if(err < 0)
{
perror("Couldn't read extension data.");
exit(1);
}
printf("The size of extension data is: %d\n", ext_size);
/* Access extension data */
ext_data = (char*)malloc(ext_size);
clGetPlatformInfo(platforms[i], CL_PLATFORM_EXTENSIONS,
ext_size, ext_data, NULL);
//printf("Platform %d supports extensions: %s\n", i, ext_data);
char *name = (char*)malloc(ext_size);
clGetPlatformInfo(platforms[i], CL_PLATFORM_NAME,
ext_size, name, NULL);
printf("Platform %d name: %s\n", i, name);
char *vendor = (char*)malloc(ext_size);
clGetPlatformInfo(platforms[i], CL_PLATFORM_VENDOR,
ext_size, vendor, NULL);
printf("Platform %d vendor: %s\n", i, vendor);
char *version = (char*)malloc(ext_size);
clGetPlatformInfo(platforms[i], CL_PLATFORM_VERSION,
ext_size, version, NULL);
printf("Platform %d version: %s\n", i, version);
char *profile = (char*)malloc(ext_size);
clGetPlatformInfo(platforms[i], CL_PLATFORM_PROFILE,
ext_size, profile, NULL);
printf("Platform %d full profile or embeded profile?: %s\n", i, profile);
/* Look for ICD extension */
if(strstr(ext_data, icd_ext) != NULL)
platform_index = i;
std::cout<<"Platform_index = "<<platform_index<<std::endl;
/* Display whether ICD extension is supported */
if(platform_index > -1)
printf("Platform %d supports the %s extension.\n",
platform_index, icd_ext);
std::cout<<std::endl;
free(ext_data);
free(name);
free(vendor);
free(version);
free(profile);
}
if(platform_index <= -1)
printf("No platforms support the %s extension.\n", icd_ext);
/* Deallocate resources */
free(platforms);
return 0;
}
- 成功后,执行会看到这个: