繁体   English   中英

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

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

我正在探索boost_compute。 不幸的是,文档页面和示例比我需要了解的更少。

鉴于以下缩小的代码:

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
    );
}

如何调用'add'函数以及这个函数内部的迭代变量是什么? 此外,我需要这种代码结构来进行更复杂的计算。

亲切的问候,托尼

简而言之boost:compute函数不是 OpenCL内核函数。 它们更像是OpenGL内核函数。

我相信你的函数需要太多参数才能与boost:compute算法一起使用。
但是,稍微简单的函数,只是添加没有常量的相邻值,将是:

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

可以使用boost::compute::transform调用@ddemidov建议:

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

可以使用boost::compute::lambda函数实现您的函数。 例如:

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);

但它仍缺少一系列价值观......

您的函数可以使用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;
  }

);

在构建内核程序并创建内核(使用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