opencl学习


opencl

1. 创建内存对象

cl_uint uarr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
cl_uint uout[sizeof(uarr) / sizeof(uarr[0])] = { 0 };
cl_mem clArrBuffer = clCreateBuffer(context, CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR, sizeof(uarr), uarr, 0);
cl_mem clArrOutBuffer = clCreateBuffer(context, CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR, sizeof(uout), uout, 0);

clCreateBuffer创建缓存,需要给定缓存大小(sizeof),以及数组

1.1. 接口可以是对象吗

不行。可以传递对象的同时,传递一些OffSet信息,kenel端根据这些信息来解码对象。

1.1.1. clCreateBuffer

ref[1] 7
Memory objects are fundamental in working with OpenCL and include the following types:

  • Buffers: one-dimensional arrays of bytes
  • Sub-buffers: one-dimensional views into buffers
  • Images: two-dimensional or three-dimensional data structured arrays, which have limited access operators and a selection of different formats, sampling, and clamping features
cl_mem clCreateBuffer(cl_context context,
 cl_mem_flags flags,
 size_t size,
 void * host_ptr,
 cl_int *errcode_ref)
1.1.1.1. host_ptr
  • A pointer to data, allocated by the application; its use in a call to clCreateBuffer is determined by the flags parameter.
  • The size of the data pointed to by host_ptr must be at least that of the requested allocation, that is, >= size bytes.

void * 可以指向cl_uint, float

1.1.2. clSetKernelArg

clSetKernelArg(kernel, 0, sizeof(cl_mem), (void*)&clArrBuffer);	

__kernel void square(__global float * buffer)
{
 size_t id = get_global_id(0);
 buffer[id] = buffer[id] * buffer[id];
}

clSetKernelArg将buffer传递给kernel

kernel除了使用float *之外还能使用哪些类型,能不能使用自定义类型?

1.1.3. sub-buffers

  • An alternative, more general approach to subdividing the work performed on buffers is to use sub-buffers.
  • Sub-buffers provide a view into a particular buffer, for example, enabling the developer to divide a single buffer into chunks that can be worked on independently.
  • Sub-buffers are purely a software abstraction; anything that can be done with a sub-buffer can be done using buffers, explicit offsets, and so on.
  • Sub-buffers provide a layer of additional modality not easily expressed using just buffers.
cl_mem clCreateSubBuffer(
 cl_mem buffer,
 cl_mem_flags flags,
 cl_buffer_create_type buffer_create_type,
 const void * buffer_create_info,
 cl_int *errcode_ref)

1.2. 接口可以是多维数组吗

可以

2. 事件、消息、协作

3. 参考文献

[1] OpenCL Programming Guide

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值