C++ 03 在 Boost 1.62.0 中添加
分类: 并发
并行/GPU 计算库
本次发布
依赖项
Boost.Compute
Boost.Compute 是一个基于 OpenCL 的 C++ GPU/并行计算库。
核心库是 OpenCL API 的一个薄 C++ 包装器,提供对计算设备、上下文、命令队列和内存缓冲区的访问。
在核心库之上是一个通用的、类似 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)。