续CUDA环境配置后,我又研究了openCL环境的配置,由于CUDA环境在之前已经安装过,而openCL的头文件会自动集成在CUDA的文件中,在windows下是#include
选择toolchain的时候不能选择visual studio的,要用mingw。
个人觉得VS当学习工具,实在有些臃肿,百度CLION的时候,许多答案都是牛头不对马嘴,为了让大家不浪费时间在配置环境上,故写博客记录
我的CMakeLists.txt如下
cmake_minimum_required(VERSION 3.7)
project (OpenCLProject)
add_executable(OpenCLProject Ch8/full_context/full_context.cpp)
find_package(OpenCL REQUIRED)
target_link_libraries(OpenCLProject PRIVATE OpenCL::OpenCL)
在配置C++环境的时候,我发现opencl in action这本书上的代码运行不了,查阅了资源以后发现我的显卡是NVIDIA GeForce GTX 1050,支持的openCL 1.2而书上的代码是1.1
只要加一句代码即可
#define CL_USE_DEPRECATED_OPENCL_1_1_APIS
测试代码如下
#define _CRT_SECURE_NO_WARNINGS
#define __CL_ENABLE_EXCEPTIONS
#define CL_USE_DEPRECATED_OPENCL_1_1_APIS
#include
#ifdef MAC
#include
#else
#include
#endif
using namespace std;
int main(void) {
vector<cl::Platform> platforms;
vector<cl::Device> platformDevices, ctxDevices;
cl::string device_name;
cl_uint i;
try {
// Access all devices in first platform
cl::Platform::get(&platforms);
platforms[0].getDevices(CL_DEVICE_TYPE_ALL, &platformDevices);
// Create context and access device names
cl::Context context(platformDevices);
ctxDevices = context.getInfo<CL_CONTEXT_DEVICES>();
for(i=0; i<ctxDevices.size(); i++) {
device_name = ctxDevices[i].getInfo<CL_DEVICE_NAME>();
cout << "Device: " << device_name.c_str() << endl;
}
}
catch(cl::Error e) {
cout << e.what() << ": Error code " << e.err() << endl;
}
return 0;
}
输出如下