..

怎么处理参数

这篇文章要解决的问题

  • 怎么创建参数
  • 怎么给传递参数
  • 怎么用

set_arg 1

void set_arg(size_t index, size_t size, const void * value);
Sets the argument at index to value with size.

See the documentation for clSetKernelArg() for more information.

这是 kernel 的方法,用来给kernel添加参数。
index : 表示该参数的顺序,从 0 开始, 及第一个,以此类推。
value 的解释

 Pointer to primitive data】—Transfers simple primitives to the device
 Pointer to a memory object】—Transfers significant or complex data
 Pointer to a sampler object】—Transfers an object that defines how images are read
 NULL】—Transfers no data from the host; the device will just reserve memory in
its local address space for the kernel argument

代码展示一下

// 创建 kernel
std::string source = "__kernel void foo(__global int *data) { }";

boost::compute::program foo_program =
    boost::compute::program::create_with_source(source, context);

// build the program
foo_program.build();

// create a kernel from the compiled program
boost::compute::kernel foo_kernel = foo_program.create_kernel("foo");

float x = 0.1;

foo_kernel.set_arg(0, sizeof(float), &x);

memory objects

  • buffer objects and
  • image objects.

创建 buffer object

// Buffers are allocated within a compute context. For example, to allocate a memory buffer for 32 float's:
boost::compute::buffer buf(context, 32 * sizeof(float));

几个事项

1) copy host to device

int data[] = { 1, 2, 3, 4 };

queue.enqueue_write_buffer(buf, 0, 4 * sizeof(int), data);

2) 也可以用 copy,更灵活
3)默认的copy 只是 创建一个引用,没有真正的拷贝数据
4)深拷贝,考虑 clone() 方法

Demo

#include <vector>

#include <boost/compute/algorithm/copy.hpp>
#include <boost/compute/container/vector.hpp>

namespace compute = boost::compute;

int main()
{
    // get default device and setup context
    compute::device device = compute::system::default_device();
    compute::context context(device);
    compute::command_queue queue(context, device);

    // create data array on host
    int host_data[] = { 1, 3, 5, 7, 9 };

    // create vector on device
    compute::vector<int> device_vector(5, context);

    // copy from host to device
    compute::copy(
        host_data, host_data + 5, device_vector.begin(), queue
    );

    // create vector on host
    std::vector<int> host_vector(5);

    // copy data back to host
    compute::copy(
        device_vector.begin(), device_vector.end(), host_vector.begin(), queue
    );

    return 0;
}

其他API参考文档。 2