這是前幾天在 GPGPU.org 上看到的消息,原文是《Boost.Compute v0.2 Released》。
基本上,Boost.Compute 是一個基於 OpenCL 的 C++ GPGPU、大量平行化函式庫,主要的設計目的,是用來簡化 GPGPU 程式的開發。Boost.Compute 實際上是一個 OpenCL C API 的一個 C++ wrapper,讓開發者可以用 C++ 的概念、以及 STL-like 的程式風格、來開發 OpenCL 的程式。個人會覺得他的形式應該相當接近 nVIDIA CUDA 的 Thrust 這個函式庫。
他的原始碼目前是放在 GitHub 上,網址是:
目前還僅只是 0.2 版、同時也還沒有被納為 Boost C++ Libraries 的一部分,基本上,應該還是算是在測試階段吧~
而由於他是採取 Header-only 的形式,所以使用前本身並不需要建置。不過因為他本身是基於 OpenCL 來做的,所以專案的設定也需要按照開發 OpenCL 時一樣做設定(include path、linking OpenCL.lib);另外由於他也有用到 Boost C++ Libraries 的其他函式庫,所以也需要準備整個 Boost 的環境。
下面就是他的基本範例:
#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; }
以這個範例來看,他基本上在初始化完成之後,把資料複製到 compute::vector<> 這種放在 GPU 上的資料型別裡,然後就可以透過 Boost.Compute 提供的函式來做平行化處理了~像在上面的例子裡面,就是使用了 compute::sort() 這個演算法,來幫資料做排序。
在 Boost.Compute 的 algorithm 裡面,也還提供了很多其他的函式,可以拿來直接使用。
而如果希望寫自己的處理函式給 transform() 這類的函式用的話,Boost.Compute 也有幾種提供對應的方法可以撰寫;下面就是一個使用 boost::compute::function<> 搭配 make_function_from_source<>() 的範例:
boost::compute::function<int (int)> add_four = boost::compute::make_function_from_source<int (int)>( "add_four", "int add_four(int x) { return x + 4; }" ); compute::transform( device_vector.begin(), device_vector.end(), device_vector.begin(), add_four );
而如果使用 BOOST_COMPUTE_FUNCTION 這個 macro 的話,或許會比較好撰寫:
BOOST_COMPUTE_FUNCTION(int, add_four, (int x), { return x + 4; });
這邊大概就簡單介紹到這,更詳細的說明,就請參考官方文件了~
不過話說,Heresy 個人比較希望可以直接把 C++14 的 Parallel STL 直接 GPU 化啊~ XD
[…] NVIDIA CUDA 的話,早就有 Thrust(連結)可以用了。以 OpenCL 為基礎的話,也有 Boost.Compute(1.61.0 已正式進入 Boost)、或是官方的 […]
讚讚