文章目录
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