[英]Pass a vector of custom structs by reference to a boost::compute closure or function
我是opencl的新手,正在嘗試學習正確使用boost :: compute。 考慮以下代碼:
#include <iostream>
#include <vector>
#include <boost/compute.hpp>
const cl_int cell_U_size{ 4 };
#pragma pack (push,1)
struct Cell
{
cl_double U[cell_U_size];
};
#pragma pack (pop)
BOOST_COMPUTE_ADAPT_STRUCT(Cell, Cell, (U));
int main(int argc, char* argv[])
{
using namespace boost;
auto device = compute::system::default_device();
auto context = compute::context(device);
auto queue = compute::command_queue(context, device);
std::vector<Cell> host_Cells;
host_Cells.reserve(10);
for (auto j = 0; j < host_Cells.capacity(); ++j) {
host_Cells.emplace_back(Cell());
for (auto i = 0; i < cell_U_size; ++i) {
host_Cells.back().U[i] = static_cast<cl_double>(i+j);
}
}
std::cout << "Before:\n";
for (auto const& hc : host_Cells) {
for (auto const& u : hc.U)
std::cout << " " << u;
std::cout << "\n";
}
compute::vector<Cell> device_Cells(host_Cells.size(), context);
auto f = compute::copy_async(host_Cells.begin(), host_Cells.end(), device_Cells.begin(), queue);
try {
BOOST_COMPUTE_CLOSURE(Cell, Step1, (Cell cell), (cell_U_size), {
for (int i = 0; i < cell_U_size; ++i) {
cell.U[i] += 1.0;
}
return cell;
});
f.wait(); // Wait for data to finish being copied
compute::transform(device_Cells.begin(), device_Cells.end(), device_Cells.begin(), Step1, queue);
//BOOST_COMPUTE_CLOSURE(void, Step2, (Cell &cell), (cell_U_size), {
// for (int i = 0; i < cell_U_size; ++i) {
// cell.U[i] += 1.0;
// }
//});
//compute::for_each(device_Cells.begin(), device_Cells.end(), Step2, queue);
compute::copy(device_Cells.begin(), device_Cells.end(), host_Cells.begin(), queue);
}
catch (std::exception &e) {
std::cout << e.what() << std::endl;
throw;
}
std::cout << "After:\n";
for (auto const& hc : host_Cells) {
for (auto const& u : hc.U)
std::cout << " " << u;
std::cout << "\n";
}
}
我有一個要在GPU上處理的自定義結構向量(實際上比這里顯示的要復雜得多)。 在未注釋的BOOST_COMPUTE_CLOSURE中, compute::transform
按值傳遞結構,對其進行處理,然后將其復制回。
我想以引用的方式傳遞這些內容,如帶注釋的BOOST_COMPUTE_CLOSURE和compute::for_each
,但是當程序運行時內核無法編譯( Build Program Failure
),並且我還沒有找到任何文檔說明如何實現。
我知道我可以通過使用BOOST_COMPUTE_STRINGIZE_SOURCE
並傳遞指向整個結構向量的指針來實現按引用傳遞(實際上是指針,因為它是C99),但是我想使用compute::...
函數,因為這些函數看起來更優雅。
如果您定義BOOST_COMPUTE_DEBUG_KERNEL_COMPILATION
宏而構建OpenCL程序失敗,則該程序源和構建日志將被寫入stdout。
您不能在OpenCL C中按引用傳遞,而要在BOOST_COMPUTE_CLOSURE
中嘗試這樣做。 我知道您想將__global
指針傳遞給您的閉包,並修改全局內存中變量的值,而不是該值的本地副本。 我認為Boost.Compute不支持它,因為在for_each
(和其他算法)中,Boost.Compute始終將值傳遞給函數/閉包。
當然,您始終可以實施變通方法-添加一元&
運算符,或實施自定義設備迭代器。 但是,在給出的示例中,這只會降低性能,因為這將導致非逐級讀取和寫入內存。 如果您有非常復雜的數組(AoS),請嘗試更改其數組結構(SoA)或/和破壞您的結構。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.