|
Boost.Compute :用于OpenCL的C++ GPU计算库
Found Boost: /usr/include (found version "1.65.1")自带的Compute报错:
/usr/include/boost/compute/memory/svm_ptr.hpp:131:16: error: binding reference of type ‘boost::compute::context&’ to ‘const boost::compute::context’ discards qualifiers
return m_context;
第三方:https://github.com/boostorg/compute
The full documentation is available at http://boostorg.github.io/compute/
源码编译安装:
基于以下:
-- Found OpenCL: /usr/lib/x86_64-linux-gnu/libOpenCL.so (found version "2.2")
-- Found Boost: /usr/include (found suitable version "1.65.1", minimum required is "1.54")
mkdir build
cd build
cmake ..
make
sudo make install
会安装到目录 /usr/local/include 下
安装后使用Boost.Compute的步骤:
- CMAKE_MINIMUM_REQUIRED(VERSION 2.8)
- set(project_name "main_cl")
- PROJECT(${project_name})
- #FIND_PACKAGE(CUDA REQUIRED)
- FIND_PACKAGE(OpenCL REQUIRED)
- find_package(Boost 1.54 REQUIRED)
- FIND_PACKAGE(BoostCompute REQUIRED)
- # 这个很关键 头文件不能歧义
- set(BoostCompute_INCLUDE_DIRS "/usr/local/include/compute")
- include_directories(${BoostCompute_INCLUDE_DIRS})
- message(WARNING ${BoostCompute_INCLUDE_DIRS})
- ADD_EXECUTABLE(${project_name} copy_data.cpp)
- target_link_libraries(${project_name} -lboost_system OpenCL)
复制代码
- //[copy_data_example
- #include <vector>
- #include <iostream>
- #include <boost/compute/algorithm/copy.hpp>
- #include <boost/compute/container/vector.hpp>
- namespace compute = boost::compute;
- //警告很多 为啥?
- int main()
- {
- // get default device and setup context
- compute::device device = compute::system::default_device();
- // print the device's name and platform
- std::cout << "hello from " << device.name();
- std::cout << " (platform: " << device.platform().name() << ")" << std::endl;
- compute::context context(device);
- compute::command_queue queue(context, device);
- // create data array on host
- int host_data[] = { 1, 3, 5, 7, 9 };
- // create vector on device
- compute::vector<int> device_vector(5, context);
- // copy from host to device
- compute::copy(
- host_data, host_data + 5, device_vector.begin(), queue
- );
- // create vector on host
- std::vector<int> host_vector(5);
- // copy data back to host
- compute::copy(
- device_vector.begin(), device_vector.end(), host_vector.begin(), queue
- );
- //遍历看看 对不对啊?
- for(std::vector<int>::iterator it=host_vector.begin();it!=host_vector.end();it++){
- std::cout << *it << " ";
- }
- std::cout << std::endl;
- getchar();
- return 0;
- }
复制代码
|
|