[英]boost::compute, passing pointer to a closure
Good evening!晚上好! I am writing a high-performance application and trying to use boost to speed up complex computations.我正在编写一个高性能应用程序并尝试使用 boost 来加速复杂的计算。
The essence of my question: is there a way to pass an external pointer to array (like float4_ *
) to a BOOST_COMPUTE_CLOSURE
?我的问题的本质:有没有办法将指向数组的外部指针(如float4_ *
)传递给BOOST_COMPUTE_CLOSURE
? I'd like to get something like:我想得到类似的东西:
float4_ *normals = new float4_[NORMALS_NO];
BOOST_COMPUTE_CLOSURE(void, evalNormals, (int4_ indices), (normals), {
...
});
Okay, I have finally found out how to implement the declared options.好的,我终于找到了如何实现声明的选项。 First thing to do is to pass boost::compute::detail::device_ptr<float4_>
instance to the function.首先要做的是将boost::compute::detail::device_ptr<float4_>
实例传递给函数。 Next we should declare a typename generator for `OpenCL backend` and operator<<
to write pointer information into meta_kernel
instance, which is being used in a hidden way in closure definition.接下来,我们应该为 `OpenCL backend` 和operator<<
声明一个类型名生成器,将指针信息写入meta_kernel
实例,该实例在闭包定义中以隐藏方式使用。 So, the code:所以,代码:
1) Passing device_ptr
instance 1) 传递device_ptr
实例
...
#include <boost/compute/detail/device_ptr.hpp>
...
float4_ *normalsData = new float4_[NORMALS_NO];
device_ptr<float4_> normalsDataDP = normalsData;
...
BOOST_COMPUTE_CLOSURE(void, evalNormals, (int4_ indices), (normalsDataDP), {
...
});
...
2) Implement typename generator: 2) 实现类型名生成器:
...
namespace boost {
namespace compute {
template<>
inline const char *type_name<detail::device_ptr<float4_>>()
{
return "__global float4 *";
}
}
}
...
3) Implement operator<<
3) 实现operator<<
...
namespace boost {
namespace compute {
namespace detail {
meta_kernel &operator<<(meta_kernel &kern,
const device_ptr<float4_> &ptr)
{
std::string nodes_info = kern.get_buffer_identifier<float4_>(ptr.get_buffer());
kern << kern.var<float4_ *>(nodes_info);
return kern;
}
}
}
}
...
The documentation of BOOST_COMPUTE_CLOSURE
is slightly sparse as reported here by the library author, but some test cases show how to capture vector
s and array
s. BOOST_COMPUTE_CLOSURE
的文档如库作者在此处报告的BOOST_COMPUTE_CLOSURE
略显稀疏,但一些测试用例显示了如何捕获vector
s 和array
s。 It actually works transparently, the same than with scalar.它实际上透明地工作,与标量相同。
For instance, capturing vec
:例如,捕获vec
:
int data[] = {6, 7, 8, 9};
compute::vector<int> vec(data, data + 4, queue);
BOOST_COMPUTE_CLOSURE(int, get_vec, (int i), (vec), { return vec[i]; });
// run using a counting iterator to copy from vec to output
compute::vector<int> output(4, context);
compute::transform(
compute::make_counting_iterator(0),
compute::make_counting_iterator(4),
output.begin(),
get_vec,
queue);
CHECK_RANGE_EQUAL(int, 4, output, (6, 7, 8, 9));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.