![](/img/trans.png)
[英]How to use Shared Virtual Memory in Boost::Compute and custom kernel?
[英]How to use boost::compute::atan2?
我想使用boost :: compute計算復數的相位
這是我的嘗試,我希望結果等於atan2(0.5f):
namespace bc = boost::compute;
bc::vector<std::complex<float>> vec{ {1.0f, 2.0f} };
bc::vector<float> result(1);
bc::transform(vec.begin(), vec.end(), result.begin(), bc::atan2<float>());
但是我收到一個編譯錯誤,聲稱“非一元函數調用了一個參數”
boost::compute
的atan2
似乎是一個二進制函數 ,就像std::atan2
。
我假設您正在嘗試獲取復數的相位角? 用於此目的的標准C ++函數為std::arg()
-我看不到boost::compute
定義了該boost::compute
,盡管我可能會錯過它。
如果確實缺少arg()
,那么您完全可以通過atan2
實現-您需要提取虛構的( boost::compute::imag()
)和real( boost::compute::real()
)組件首先,然后將它們作為單獨的參數傳遞給atan2
。
我找到了使之工作的方法。
階段1:分配2個向量:
bc::vector<std::complex<float>> vec{ {1.0f, 2.0f}, {3.0f, 4.0f}, {5.0f, 6.0f} };
bc::vector<float> result(3);
階段2:將復數向量解釋為浮點緩沖區迭代器
當您有一個強類型的向量並將其作為另一種類型傳遞給算法時, buffer_iterator
非常有用。
auto beginf = bc::make_buffer_iterator<float>(vec.get_buffer(), 0);
auto endf = bc::make_buffer_iterator<float>(vec.get_buffer(), 6); // note end point to final index + 1
階段3:定義跨步迭代器,以便我們可以使用與tan2參數相同的緩沖區。 每個迭代器以2個索引的跨度迭代緩沖區,它們為tan2提供對緩沖區的交錯訪問:
auto begin_a = bc::make_strided_iterator(beginf + 1, 2); // access imaginary part
auto end_a = bc::make_strided_iterator_end(beginf + 1, endf , 2);
auto begin_b = bc::make_strided_iterator(beginf, 2); // access real part
最后,調用轉換:
bc::transform(begin_a, end_a, begin_b, result.begin(), bc::atan2<float>()); // atan(b/a)
bc::system::default_queue().finish();
我認為您也可以為此使用Boost.Compute的lambda表達式:
bc::vector<float2> input{ {1.0f, 2.0f}, {3.0f, 4.0f}, {5.0f, 6.0f} };
bc::vector<float> output(3);
using boost::compute::lambda::atan2;
using boost::compute::_1;
using boost::compute::lambda::get;
bc::transform(
float2_input.begin(),
float2_input.end(),
float_output.begin(),
atan2(get<1>(_1), get<0>(_1)),
queue
);
float2
在Boost.Compute中非常復雜。 您還可以檢查test_lambda.cpp 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.