組裝的電腦沒帶獨立顯卡,用的是CPU自帶的核顯,型號是Intel HD Graphics 530,關於顯卡是否可以使用OpenCL,可以下載GPU-Z軟件查看。
本文在Windows 10 64位系統上搭建OpenCL開發環境。
一、准備文件
將顯卡驅動更新到最新版本,Windows 驅動中自動包含了OpenCL驅動,Linux系統需要另外下載OpenCL驅動。
VS2012-VS2017任意版本。
下載Intel SDK for OpenCL applications,注意選擇Windows平台,然后注冊帳號后即可下載。
二、測試環境
下載示例項目,解壓后打開CapsBasic目錄下的sln文件(高版本自動升級項目)
上一步安裝正確的話VS工具欄會有如下菜單
點擊[生成]-[生成解決方案],不會有錯誤出現。
運行結果可能如下(不同機器輸出結果不同)
三、從模板新建項目
上面是直接下載別人已經配置好的項目,這里測試新建一個項目,打開VS,新建項目如下圖:
添加一個新文件HelloOpenCL.cpp,在文件中添加如下代碼:
#include <cstdlib> #include <iostream> #include <iomanip> #include <cstring> #include <cassert> #include <CL/cl.h> /* * 修改自官方示例intel_ocl_caps_basic_win,用於測試手工配置項目 */ int main() { using namespace std; const char* required_platform_subname = "Intel"; //函數返回值,CL_SUCCESS表示成功 cl_int err = CL_SUCCESS; // 判斷返回值是否正確的宏 #define CAPSBASIC_CHECK_ERRORS(ERR) \ if(ERR != CL_SUCCESS) \ { \ cerr \ << "OpenCL error with code " << ERR \ << " happened in file " << __FILE__ \ << " at line " << __LINE__ \ << ". Exiting...\n"; \ exit(1); \ } // 遍歷系統中所有OpenCL平台 cl_uint num_of_platforms = 0; // 得到平台數目 err = clGetPlatformIDs(0, 0, &num_of_platforms); CAPSBASIC_CHECK_ERRORS(err); cout << "Number of available platforms: " << num_of_platforms << endl; cl_platform_id* platforms = new cl_platform_id[num_of_platforms]; // 得到所有平台的ID err = clGetPlatformIDs(num_of_platforms, platforms, 0); CAPSBASIC_CHECK_ERRORS(err); //列出所有平台 cl_uint selected_platform_index = num_of_platforms; cout << "Platform names:\n"; for (cl_uint i = 0; i < num_of_platforms; ++i) { size_t platform_name_length = 0; err = clGetPlatformInfo( platforms[i], CL_PLATFORM_NAME, 0, 0, &platform_name_length ); CAPSBASIC_CHECK_ERRORS(err); // 調用兩次,第一次是得到名稱的長度 char* platform_name = new char[platform_name_length]; err = clGetPlatformInfo( platforms[i], CL_PLATFORM_NAME, platform_name_length, platform_name, 0 ); CAPSBASIC_CHECK_ERRORS(err); cout << " [" << i << "] " << platform_name; if ( strstr(platform_name, required_platform_subname) && selected_platform_index == num_of_platforms // have not selected yet ) { cout << " [Selected]"; selected_platform_index = i; } cout << endl; delete[] platform_name; } delete[] platforms; return 0; }運行結果如下:
四、后續
后續會增加GPU計算相關文章。