英偉達的顯卡首先要下載安裝CUDA開發包,可以參考這里的步驟: VS2015編譯環境下CUDA安裝配置
安裝好CUDA之后,OpenCL的配置就已經完成了80%了,剩下的工作就是把OpenCL的路徑添加到工程中。
1. 新建一個win32控制臺應用程序,在工程的屬性管理器Debug中添加一個屬性頁“OpenCL.PRops”,之后雙擊打開
2. 在C/C++ ->常規->附加包含目錄 中添加CUDA的include文件夾路徑,我的路徑是“D:/Software/CUDA/Development/include”

3. 在鏈接器->常規->附加庫目錄 中添加lib文件夾路徑,我的路徑是“D:/Software/CUDA/Development/lib/Win32”

4. 在鏈接器->輸入->附加依賴項 里添加lib文件 OpenCL.lib

經過以上4個步驟,OpenCL編譯環境就已經配置好了,可以把屬性頁“OpenCL.props”保存起來,下次直接這個屬性頁就可以了,不用每次都重復配置。以下是測試程序:
#include <stdio.h> #include <stdlib.h> #include <iostream> #include <CL/cl.h> int main(){ //cl_platform 表示一個OpenCL的執行平臺,關聯到GPU硬件,如N卡,AMD卡 cl_platform_id *platforms; //OpenCL中定義的跨平臺的usigned int和int類型 cl_uint num_platforms; cl_int i, err, platform_index = -1; char* ext_data; size_t ext_size; const char icd_ext[] = "cl_khr_icd"; //要使platform工作,需要兩個步驟。1 需要為cl_platform_id結構分配內存空間。2 需要調用clGetPlatformIDs初始化這些數據結構。一般還需要步驟0:詢問主機上有多少platforms //查詢計算機上有多少個支持OpenCL的設備 err = clGetPlatformIDs(5, NULL, &num_platforms); if (err < 0) { perror("Couldn't find any platforms."); exit(1); } printf("本機上支持OpenCL的環境數量: %d/n", num_platforms); //為platforms分配空間 platforms = (cl_platform_id*) malloc(sizeof(cl_platform_id) * num_platforms); clGetPlatformIDs(num_platforms, platforms, NULL); //獲取GPU平臺的詳細信息 for (i = 0; i < num_platforms; i++) { //獲取緩存大小 err = clGetPlatformInfo(platforms[i], CL_PLATFORM_EXTENSIONS, 0, NULL, &ext_size); if (err < 0) { perror("Couldn't read extension data."); exit(1); } printf("緩存大小: %d/n", ext_size); ext_data = (char*)malloc(ext_size); //獲取支持的擴展功能 clGetPlatformInfo(platforms[i], CL_PLATFORM_EXTENSIONS, ext_size, ext_data, NULL); printf("平臺 %d 支持的擴展功能: %s/n", i, ext_data); //獲取顯卡的名稱 char *name = (char*)malloc(ext_size); clGetPlatformInfo(platforms[i], CL_PLATFORM_NAME, ext_size, name, NULL); printf("平臺 %d 是: %s/n", i, name); //獲取顯卡的生產商名稱 char *vendor = (char*)malloc(ext_size); clGetPlatformInfo(platforms[i], CL_PLATFORM_VENDOR, ext_size, vendor, NULL); printf("平臺 %d 的生產商是: %s/n", i, vendor); //獲取平臺版本 char *version = (char*)malloc(ext_size); clGetPlatformInfo(platforms[i], CL_PLATFORM_VERSION, ext_size, version, NULL); printf("平臺 %d 的版本信息: %s/n", i, version); //查詢顯卡是獨立的還是嵌入的 char *profile = (char*)malloc(ext_size); clGetPlatformInfo(platforms[i], CL_PLATFORM_PROFILE, ext_size, profile, NULL); printf("平臺 %d 是獨立的(full profile)還是嵌入式的(embeded profile)?: %s/n", i, profile); //查詢是否支持ICD擴展 if (strstr(ext_data, icd_ext) != NULL) platform_index = i; std::cout << "平臺ID = " << platform_index << std::endl; /* Display whether ICD extension is supported */ if (platform_index > -1) printf("平臺 %d 支持ICD擴展: %s/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); getchar(); //釋放資源 free(platforms); return 0;}在本機上執行輸出:
新聞熱點
疑難解答