简体   繁体   English

如何调用boost_compute'BOOST_COMPUTE_FUNCTION'定义函数?

[英]How to call boost_compute 'BOOST_COMPUTE_FUNCTION' defined function?

I'm currently exploring boost_compute. 我正在探索boost_compute。 Unfortunately there are less documentation pages and examples, than I need to understand what to do. 不幸的是,文档页面和示例比我需要了解的更少。

Given the following minified code: 鉴于以下缩小的代码:

BOOST_COMPUTE_FUNCTION(bool, add, (int* values, int* results, int constant),
{
    // Whats the indexing variable?
    // In opencl it would be get_global_id(0)
    int index = // ?

    results[index] = values[index] + values[index + 1] + values[index + 2] + constant;
});

void compute(float* results, compute::context* ctx, compute::command_queue* queue)
{
    compute::vector<float> device_values(100, *ctx);
    compute::vector<float> device_results(98, *ctx);

    compute::copy(
        parameters->values.begin(), parameters->values.end(), device_values.begin(), *queue
    );

    // Actual computation
    // HOW TO CALL 'add' for every device_results element?

    compute::copy(
        device_results.begin(), device_results.end(), results, *queue
    );
}

How to call the 'add' function and what's the iterating variable inside of this function? 如何调用'add'函数以及这个函数内部的迭代变量是什么? Furthermore I need this structure of code to make more complex calculation. 此外,我需要这种代码结构来进行更复杂的计算。

Kind Regards, Toni 亲切的问候,托尼

In short boost:compute functions are not OpenCL kernel functions. 简而言之boost:compute函数不是 OpenCL内核函数。 They are more like OpenGL kernel functions. 它们更像是OpenGL内核函数。

I believe that your function takes too many parameters to be used with the boost:compute algorithms. 我相信你的函数需要太多参数才能与boost:compute算法一起使用。
However, a slightly simpler function, just adding adjacent values without the constant, would be: 但是,稍微简单的函数,只是添加没有常量的相邻值,将是:

BOOST_COMPUTE_FUNCTION(boost::compute::float_, add,
                        (boost::compute::float_ values0, boost::compute::float_ values1),
{
  return values0 + values1;
});

And could be called using boost::compute::transform as @ddemidov suggested: 可以使用boost::compute::transform调用@ddemidov建议:

boost::compute::transform(values.begin(), values.end() -1, // values0
                          values.begin() +1, // values1
                          results.begin(), // results
                          add, queue);

It may be possible to implement your function using boost::compute::lambda functions. 可以使用boost::compute::lambda函数实现您的函数。 eg: 例如:

using namespace boost::compute::lambda;

float c = 1.234; // some constant

boost::compute::transform(values.begin(), values.end() -1, // values0
                          values.begin() +1, // values1
                          results.begin(), // results
                          _1 + _2 + c, queue);

But it's still short of a set of values... 但它仍缺少一系列价值观......

Your function could be written as an OpenCL kernel in boost:compute using the BOOST_COMPUTE_STRINGIZE_SOURCE macro: 您的函数可以使用BOOST_COMPUTE_STRINGIZE_SOURCE宏在boost:compute编写为OpenCL内核:

const char kernel_function_source[] = BOOST_COMPUTE_STRINGIZE_SOURCE(

  kernel void add(global float* values, global float* results, global float* constant)
  {
    size_t index = get_global_id(0);
    results[index] = values[index] + values[index + 1] + values[index + 2] + *constant;
  }

);

After you've built your kernel program and created your kernel (using boost::compute::program ), you can set the kernel arguments individually and call the boost::compute::command_queue enqueue_1d_range_kernel function: 在构建内核程序并创建内核(使用boost::compute::program )之后,可以单独设置内核参数并调用boost::compute::command_queue enqueue_1d_range_kernel函数:

kernel.set_arg(0, values.get_buffer());
kernel.set_arg(1, results.get_buffer());
kernel.set_arg(2, &constant);
queue.enqueue_1d_range_kernel(kernel, 0, count, 0);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM