C++ 03 发布于 Boost 1.62.0
分类: 并发
并行/GPU 计算库
本次发布
依赖项
Boost.Compute
Boost.Compute 是一个基于 OpenCL 的 C++ GPU/并行计算库。
核心库是一个薄的 C++ OpenCL API 封装,提供对计算设备、上下文、命令队列和内存缓冲区的访问。
在核心库之上,是一个通用的、类似 STL 的接口,提供常用的算法(例如 transform(), accumulate(), sort())和常用的容器(例如 vector<T>, flat_set<T>)。它还包含一些扩展,包括并行计算算法(例如 exclusive_scan(), scatter(), reduce())和一些高级迭代器(例如 transform_iterator<>, permutation_iterator<>, zip_iterator<>)。
完整文档可在 http://boostorg.github.io/compute/ 找到。
示例
以下示例展示了如何在 GPU 上对浮点数向量进行排序
#include <vector>
#include <algorithm>
#include <boost/compute.hpp>
namespace compute = boost::compute;
int main()
{
// get the default compute device
compute::device gpu = compute::system::default_device();
// create a compute context and command queue
compute::context ctx(gpu);
compute::command_queue queue(ctx, gpu);
// generate random numbers on the host
std::vector<float> host_vector(1000000);
std::generate(host_vector.begin(), host_vector.end(), rand);
// create vector on the device
compute::vector<float> device_vector(1000000, ctx);
// copy data to the device
compute::copy(
host_vector.begin(), host_vector.end(), device_vector.begin(), queue
);
// sort data on the device
compute::sort(
device_vector.begin(), device_vector.end(), queue
);
// copy data back to the host
compute::copy(
device_vector.begin(), device_vector.end(), host_vector.begin(), queue
);
return 0;
}
Boost.Compute 是一个仅头文件的库,因此不需要链接。上面的示例可以使用以下命令编译:
g++ -I/path/to/compute/include sort.cpp -lOpenCL
支持
关于该库的问题(包括使用和开发)可以发布到 邮件列表。
错误和功能请求可以通过 问题跟踪器 报告。
也欢迎随时通过电子邮件发送您遇到的问题、疑问或反馈。
求贤
Boost.Compute 项目目前正在寻找对并行计算感兴趣的额外开发者。
请发送电子邮件至 Kyle Lutz (kyle.r.lutz@gmail.com) 获取更多信息。