5.2.1. Creating Buffer Objects
5.2.1. 创建缓冲区对象
A buffer object may be created using the function
可以使用函数创建缓冲区对象
cl_mem clCreateBuffer(
cl_context context,
cl_mem_flags flags,
size_t size,
void* host_ptr,
cl_int* errcode_ret);
A buffer object may also be created with additional properties using the function
也可以使用函数创建具有附加属性的缓冲区对象
cl_mem clCreateBufferWithProperties(
cl_context context,
const cl_mem_properties* properties,
cl_mem_flags flags,
size_t size,
void* host_ptr,
cl_int* errcode_ret);
| clCreateBufferWithProperties is missing before version 3.0. clCreateBufferWithProperties在版本3.0之前丢失。 |
-
context is a valid OpenCL context used to create the buffer object.
-
context是用于创建缓冲区对象的有效OpenCL上下文。
-
properties is an optional list of properties for the buffer object and their corresponding values. The list is terminated with the special property
0. If no properties are required, properties may beNULL. OpenCL 3.0 does not define any optional properties for buffers. -
properties是缓冲区对象的属性及其相应值的可选列表。该列表以特殊属性0终止。如果不需要任何属性,则属性可能为NULL。OpenCL 3.0没有为缓冲区定义任何可选属性。
-
flags is a bit-field that is used to specify allocation and usage information about the image memory object being created and is described in the supported memory flag values table.
-
flags是用于指定关于正在创建的图像存储器对象的分配和使用信息的比特字段,并且在支持的存储器标志值表中进行描述。
-
size is the size in bytes of the buffer memory object to be allocated.
-
size是要分配的缓冲存储器对象的大小(以字节为单位)。
-
host_ptr is a pointer to the buffer data that may already be allocated by the application. The size of the buffer that host_ptr points to must be greater than or equal to size bytes.
-
host_ptr是指向应用程序可能已经分配的缓冲区数据的指针。host_ptr指向的缓冲区大小必须大于或等于size字节。
-
errcode_ret may return an appropriate error code. If errcode_ret is
NULL, no error code is returned. -
errcode_ret可能返回适当的错误代码。如果errcode_ret为NULL,则不会返回任何错误代码。
The alignment requirements for data stored in buffer objects are described in Alignment of Application Data Types.
存储在缓冲区对象中的数据的对齐要求在应用程序数据类型的对齐中进行了描述。
If clCreateBuffer or clCreateBufferWithProperties is called with CL_MEM_USE_HOST_PTR set in its flags argument, the contents of the memory pointed to by host_ptr at the time of the clCreateBuffer call define the initial contents of the buffer object.
如果调用clCreateBuffer或clCreateBufferWithProperties时在其flags参数中设置了CL_MEM_USE_HOST_PTR,则在调用clCreateBuffer时HOST_PTR指向的内存内容定义缓冲区对象的初始内容。
If clCreateBuffer or clCreateBufferWithProperties is called with a pointer returned by clSVMAlloc as its host_ptr argument, and CL_MEM_USE_HOST_PTR is set in its flags argument, clCreateBuffer or clCreateBufferWithProperties will succeed and return a valid non-zero buffer object as long as the size argument is no larger than the size argument passed in the original clSVMAlloc call. The new buffer object returned has the shared memory as the underlying storage. Locations in the buffers underlying shared memory can be operated on using atomic operations to the devices level of support as defined in the memory model.
如果使用clSVMAlloc返回的指针作为其host_ptr参数调用clCreateBuffer或clCreateBufferWithProperties,并且在其flags参数中设置了CL_MEM_USE_HOST_PTR,只要size参数不大于原始clSVMAlloc调用中传递的size参数,clCreateBuffer或clCreateBufferWithProperties将成功并返回有效的非零缓冲区对象。返回的新缓冲区对象具有作为底层存储的共享内存。共享内存下的缓冲区中的位置可以使用对内存模型中定义的设备级支持的原子操作来操作。
clCreateBuffer and clCreateBufferWithProperties returns a valid non-zero buffer object and errcode_ret is set to CL_SUCCESS if the buffer object is created successfully. Otherwise, they return a NULL value with one of the following error values returned in errcode_ret:
clCreateBuffer和clCreateBufferWithProperties返回一个有效的非零缓冲区对象,如果缓冲区对象创建成功,则errcode_ret设置为CL_SUCCESS。否则,它们将返回一个NULL值,并在errcode_ret中返回以下错误值之一:
-
CL_INVALID_CONTEXT if context is not a valid context.
-
CL_INVALID_CONTEXT(如果上下文不是有效上下文)。
-
CL_INVALID_PROPERTY if a property name in properties is not a supported property name, if the value specified for a supported property name is not valid, or if the same property name is specified more than once.
-
CL_INVALID_PROPERTY如果属性中的属性名称不是支持的属性名称,如果为支持的属性名指定的值无效,或者如果多次指定相同的属性名。
-
CL_INVALID_VALUE if values specified in flags are not valid as defined in the Memory Flags table.
-
CL_INVALID_VALUE,如果在标志中指定的值不是内存标志表中定义的有效值。
-
CL_INVALID_BUFFER_SIZE if size is 0, or if size is greater than CL_DEVICE_MAX_MEM_ALLOC_SIZE for all devices in context, or if CL_MEM_USE_HOST_PTR is set in flags and host_ptr is a pointer returned by clSVMAlloc and size is greater than the size passed to clSVMAlloc.
-
CL_INVALID_BUFFER_SIZE如果size为0,或者如果上下文中所有设备的size大于CL_DEVICE_MAX_MEM_ALLOC_SIZE,或者如果在标志中设置了CL_MEM_USE_HOST_PTR并且HOST_PTR是clSVMAlloc返回的指针并且size大于传递给clSVMAlloc的大小。
-
CL_INVALID_HOST_PTR if host_ptr is
NULLand CL_MEM_USE_HOST_PTR or CL_MEM_COPY_HOST_PTR are set in flags or if host_ptr is notNULLbut CL_MEM_COPY_HOST_PTR or CL_MEM_USE_HOST_PTR are not set in flags. -
CL_INVALID_HOST_PTR,如果在标志中设置了host_ptr 为NULL且CL_MEM_USE_HOST_PTR或CL_MEM_COPY_HOST_PTR,或者如果host_ptr 不为NULL但在标志中未设置CL_MEM_COPY_HOST_PTR或CL_MEM_USE_HOST_PTR。
-
CL_MEM_OBJECT_ALLOCATION_FAILURE if there is a failure to allocate memory for buffer object.
-
CL_MEM_OBJECT_ALLOCATION_FAILURE(如果为缓冲区对象分配内存失败)。
-
CL_OUT_OF_RESOURCES if there is a failure to allocate resources required by the OpenCL implementation on the device.
-
CL_OUT_OF_RESOURCES,如果在设备上分配OpenCL实现所需的资源失败。
-
CL_OUT_OF_HOST_MEMORY if there is a failure to allocate resources required by the OpenCL implementation on the host.
-
CL_OUT_OF_HOST_MEMORY,如果在主机上分配OpenCL实现所需的资源失败。
| Memory Flags 内存标志 | Description 描述 |
|---|---|
| This flag specifies that the memory object will be read and written by a kernel. This is the default. 这个标志指定内存对象将由内核读取和写入。这是默认设置。 | |
| This flag specifies that the memory object will be written but not read by a kernel. 此标志指定内存对象将由内核写入而不是读取。 Reading from a buffer or image object created with CL_MEM_WRITE_ONLY inside a kernel is undefined. 从内核内使用CL_MEM_WRITE_ONLY创建的缓冲区或图像对象读取是未定义的。 CL_MEM_READ_WRITE and CL_MEM_WRITE_ONLY are mutually exclusive. CL_MEM_READ_WRITE和CL_MEM_WRITE_ONLY是互斥的。 | |
| This flag specifies that the memory object is a readonly memory object when used inside a kernel. 此标志指定在内核内部使用时,内存对象是只读内存对象。 Writing to a buffer or image object created with CL_MEM_READ_ONLY inside a kernel is undefined. CL_MEM_READ_WRITE or CL_MEM_WRITE_ONLY and CL_MEM_READ_ONLY are mutually exclusive. CL_MEM_READ_WRITE或CL_MEM_WRITE_ONLY和CL_MEM_READ_ONLY是互斥的。 | |
| This flag is valid only if host_ptr is not 只有当host_ptr不为NULL时,此标志才有效。如果指定,则表示应用程序希望OpenCL实现使用host_ptr引用的内存作为内存对象的存储位。 The contents of the memory pointed to by host_ptr at the time of the clCreateBuffer, clCreateBufferWithProperties, clCreateImage, clCreateImageWithProperties, clCreateImage2D, or clCreateImage3D call define the initial contents of the memory object. 在clCreateBuffer、clCreateBufferWithProperties、clCreateImage、clCreateImageWithProperties、clCreateImage2D或clCreateImage3D调用时,host_ptr指向的内存内容定义了内存对象的初始内容。 OpenCL implementations are allowed to cache the contents pointed to by host_ptr in device memory. This cached copy can be used when kernels are executed on a device. 允许OpenCL实现在设备内存中缓存host_ptr指向的内容。当内核在设备上执行时,可以使用此缓存副本。 The result of OpenCL commands that operate on multiple buffer objects created with the same host_ptr or from overlapping host or SVM regions is considered to be undefined. OpenCL命令对使用同一host_ptr 或从重叠的主机或SVM区域创建的多个缓冲区对象进行操作的结果被认为是未定义的。 | |
| This flag specifies that the application wants the OpenCL implementation to allocate memory from host accessible memory. 此标志指定应用程序希望OpenCL实现从主机可访问内存中分配内存。 CL_MEM_ALLOC_HOST_PTR and CL_MEM_USE_HOST_PTR are mutually exclusive. CL_MEM_ALLOC_HOST_PTR和CL_MEM_USE_HOST_PTR是互斥的。 | |
| This flag is valid only if host_ptr is not 只有当host_ptr不为NULL时,此标志才有效。如果指定,则表示应用程序希望OpenCL实现为内存对象分配内存,并从host_ptr引用的内存中复制数据。当clCreateBuffer、clCreateBufferWithProperties、clCreateImage、clCreateImageWithProperties、clCreateImage2D或clCreateImage3D操作返回时,实现将立即复制内存,并且host_ptr可供应用程序重用。 CL_MEM_COPY_HOST_PTR and CL_MEM_USE_HOST_PTR are mutually exclusive. CL_MEM_COPY_HOST_PTR和CL_MEM_USE_HOST_PSTR是互斥的。 CL_MEM_COPY_HOST_PTR can be used with CL_MEM_ALLOC_HOST_PTR to initialize the contents of the CL_MEM_COPY_HOST_TR可以与CL_MEM_ALLOC_HOST_PTR一起使用,以初始化使用主机可访问(例如PCIe)存储器分配的CL_MEM对象的内容。 | |
| missing before version 1.2. | This flag specifies that the host will only write to the memory object (using OpenCL APIs that enqueue a write or a map for write). This can be used to optimize write access from the host (e.g. enable write-combined allocations for memory objects for devices that communicate with the host over a system bus such as PCIe). 此标志指定主机将只写入内存对象(使用OpenCL API将写入或映射排入队列进行写入)。这可用于优化来自主机的写入访问(例如,为通过系统总线(如PCIe)与主机通信的设备启用存储器对象的写入组合分配)。 |
| missing before version 1.2. | This flag specifies that the host will only read the memory object (using OpenCL APIs that enqueue a read or a map for read). CL_MEM_HOST_WRITE_ONLY and CL_MEM_HOST_READ_ONLY are mutually exclusive. 此标志指定主机将仅读取内存对象(使用OpenCL API将读取或映射排入队列进行读取)。 |
| missing before version 1.2. | This flag specifies that the host will not read or write the memory object. CL_MEM_HOST_WRITE_ONLY or CL_MEM_HOST_READ_ONLY and CL_MEM_HOST_NO_ACCESS are mutually exclusive. 此标志指定主机不会读取或写入内存对象。 |
| missing before version 2.0. | This flag is only used by clGetSupportedImageFormats to query image formats that may be both read from and written to by the same kernel instance. To create a memory object that may be read from and written to use CL_MEM_READ_WRITE. 此标志仅由clGetSupportedImageFormats用于查询可以由同一内核实例读取和写入的图像格式。创建可以使用CL_MEM_READ_WRITE读取和写入的内存对象。 |
To create a new buffer object (referred to as a sub-buffer object) from an existing buffer object, call the function
要从现有缓冲区对象创建新的缓冲区对象(称为子缓冲区对象),请调用函数
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_ret);
| clCreateSubBuffer is missing before version 1.1. 版本1.1之前缺少clCreateSubBuffer。 |
-
buffer must be a valid buffer object and cannot be a sub-buffer object.
-
缓冲区必须是有效的缓冲区对象,不能是子缓冲区对象。
-
flags is a bit-field that is used to specify allocation and usage information about the sub-buffer memory object being created and is described in the Memory Flags table. If the CL_MEM_READ_WRITE, CL_MEM_READ_ONLY, or CL_MEM_WRITE_ONLY values are not specified in flags, they are inherited from the corresponding memory access qualifiers associated with buffer. The CL_MEM_USE_HOST_PTR, CL_MEM_ALLOC_HOST_PTR, and CL_MEM_COPY_HOST_PTR values cannot be specified in flags but are inherited from the corresponding memory access qualifiers associated with buffer. If CL_MEM_COPY_HOST_PTR is specified in the memory access qualifier values associated with buffer it does not imply any additional copies when the sub-buffer is created from buffer. If the CL_MEM_HOST_WRITE_ONLY, CL_MEM_HOST_READ_ONLY, or CL_MEM_HOST_NO_ACCESS values are not specified in flags, they are inherited from the corresponding memory access qualifiers associated with buffer.
-
flags是一个位字段,用于指定关于正在创建的子缓冲存储器对象的分配和使用信息,并在memory flags表中进行了描述。如果标志中未指定CL_MEM_READ_WRITE、CL_MEM_READ_ONLY或CL_MEM_WRITE_ONLY值,则从与缓冲区相关联的相应存储器访问限定符继承这些值。CL_MEM_USE_HOST_TR、CL_MEM_ALLOC_HOST_PTR和CL_MEM_COPY_HOST_PTR值不能在标志中指定,而是从与缓冲区相关联的相应内存访问限定符继承而来。如果在与缓冲区相关联的存储器访问限定符值中指定了CL_MEM_COPY_HOST_TR,则当从缓冲区创建子缓冲区时,这并不意味着任何额外的拷贝。如果在标志中未指定CL_MEM_HOST_WRITE_ONLY、CL_MEM_HOST_READ_ONLY或CL_MEM-HOST_NO_ACCESS值,则从与缓冲区相关联的相应存储器访问限定符继承这些值。
-
buffer_create_type and buffer_create_info describe the type of buffer object to be created. The list of supported values for buffer_create_type and corresponding descriptor that buffer_create_info points to is described in the SubBuffer Attributes table.
-
buffer_create_type和buffer_create_info描述了要创建的缓冲区对象的类型。子缓冲区属性表中描述了buffer_create_type支持的值列表以及buffer_create_info所指向的相应描述符。
| Buffer Creation Type 缓冲区创建类型 | Description 描述 |
|---|---|
| missing before version 1.1. | Create a buffer object that represents a specific region in buffer. 创建一个缓冲区对象,表示缓冲区中的特定区域。 buffer_create_info is a pointer to a cl_buffer_region structure specifying a region of the buffer. buffer_create_info是指向指定缓冲区区域的cl_buffer_regon结构的指针。 If buffer is created with CL_MEM_USE_HOST_PTR, the host_ptr associated with the buffer object returned is host_ptr + origin. 如果使用CL_MEM_USE_HOST_PTR创建缓冲区,则与返回的缓冲区对象相关联的HOST_PTR是host_ptr +原点。 The buffer object returned references the data store allocated for buffer and points to the region specified by buffer_create_info in this data store. 返回的缓冲区对象引用为缓冲区分配的数据存储,并指向该数据存储中由buffer_create_info指定的区域。 |
clCreateSubBuffer returns CL_SUCCESS if the function is executed successfully. Otherwise, it returns one of the following errors in errcode_ret:
如果函数执行成功,clCreateSubBuffer将返回CL_SUCCESS。否则,它将在errcode_ret中返回以下错误之一:
-
CL_INVALID_MEM_OBJECT if buffer is not a valid buffer object or is a sub-buffer object.
-
CL_INVALID_MEM_OBJECT,如果缓冲区不是有效的缓冲区对象或是子缓冲区对象。
-
CL_INVALID_VALUE if buffer was created with CL_MEM_WRITE_ONLY and flags specifies CL_MEM_READ_WRITE or CL_MEM_READ_ONLY, or if buffer was created with CL_MEM_READ_ONLY and flags specifies CL_MEM_READ_WRITE or CL_MEM_WRITE_ONLY, or if flags specifies CL_MEM_USE_HOST_PTR or CL_MEM_ALLOC_HOST_PTR or CL_MEM_COPY_HOST_PTR.
-
CL_INVALID_VALUE如果缓冲区是用CL_MEM_WRITE_ONLY创建的,并且标志指定CL_MEM_READ_WRITE或CL_MEM_READ_ONLY,或者如果缓冲区用CL_MEM_READ_ONLY创建的并且标志指定了CL_MEM_READ_WRITE或CL_MEM_WRITE_ONLY,或如果标志指定了CL _MEM_USE_HOST_PTR或CL _MEM_ALLOC_HOST_PTR或CL_MEM _COPY_HOST_PSTR。
-
CL_INVALID_VALUE if buffer was created with CL_MEM_HOST_WRITE_ONLY and flags specify CL_MEM_HOST_READ_ONLY, or if buffer was created with CL_MEM_HOST_READ_ONLY and flags specify CL_MEM_HOST_WRITE_ONLY, or if buffer was created with CL_MEM_HOST_NO_ACCESS and flags specify CL_MEM_HOST_READ_ONLY or CL_MEM_HOST_WRITE_ONLY.
-
CL_INVALID_VALUE如果缓冲区是用CL_MEM_HOST_WRITE_ONLY创建的并且标志指定CL_MEM_HOST_READ_ONLY,或者如果缓冲区用CL_MEM_HOST_READ_ONLY创建并且标志指定CL _MEM_HOST_WRITE_ONLY,或者缓冲区是使用CL_MEM-HOST_NO_ACCESS创建的并且标记指定CL_MEM_HOST_RED_ONLY或CL_MEM_ OST_WRITE_ONLY。
-
CL_INVALID_VALUE if the value specified in buffer_create_type is not valid.
-
CL_INVALID_VALUE(如果buffer_create_type中指定的值无效)。
-
CL_INVALID_VALUE if value(s) specified in buffer_create_info (for a given buffer_create_type) is not valid or if buffer_create_info is
NULL. -
CL_INVALID_VALUE,如果在buffer_create_info中指定的值(对于给定的buffer_create_type)无效或buffer_create_info为NULL。
-
CL_MEM_OBJECT_ALLOCATION_FAILURE if there is a failure to allocate memory for sub-buffer object.
-
CL_MEM_OBJECT_ALLOCATION_FAILURE(如果为子缓冲区对象分配内存失败)。
-
CL_OUT_OF_RESOURCES if there is a failure to allocate resources required by the OpenCL implementation on the device.
-
CL_OUT_OF_RESOURCES,如果在设备上分配OpenCL实现所需的资源失败。
-
CL_OUT_OF_HOST_MEMORY if there is a failure to allocate resources required by the OpenCL implementation on the host.
-
CL_OUT_OF_HOST_MEMORY,如果在主机上分配OpenCL实现所需的资源失败。
-
CL_INVALID_VALUE if the region specified by the cl_buffer_region structure passed in buffer_create_info is out of bounds in buffer.
-
CL_INVALID_VALUE,如果在buffer_create_info中传递的cl_buffer_regon结构指定的区域在缓冲区中越界。
-
CL_INVALID_BUFFER_SIZE if the size field of the cl_buffer_region structure passed in buffer_create_info is 0.
-
CL_INVALID_BUFFER_SIZE,如果在BUFFER_create_info中传递的buffer_create_info 结构的大小字段为0。
-
CL_MISALIGNED_SUB_BUFFER_OFFSET if there are no devices in context associated with buffer for which the origin field of the cl_buffer_region structure passed in buffer_create_info is aligned to the CL_DEVICE_MEM_BASE_ADDR_ALIGN value.
-
CL_MISALIGNED_SUB_BUFFER_OFFSET,如果在与缓冲区相关联的上下文中没有设备,则在buffer_create_info 中传递的cl_buffer_region结构的原点字段与CL_DEVICE_MEM_BASE_ADDR_ALIGN值对齐。
| Concurrent reading from, writing to and copying between both a buffer object and its sub-buffer object(s) is undefined. Concurrent reading from, writing to and copying between overlapping sub-buffer objects created with the same buffer object is undefined. Only reading from both a buffer object and its sub-buffer objects or reading from multiple overlapping sub-buffer objects is defined. 缓冲区对象及其子缓冲区对象之间的并发读取、写入和复制是未定义的。未定义使用同一缓冲区对象创建的重叠子缓冲区对象之间的并行读取、写入和复制。仅定义从缓冲区对象及其子缓冲区对象读取,或从多个重叠的子缓冲区目标读取。 |
The cl_buffer_region structure specifies a region of a buffer object:
cl_buffer_regon结构指定缓冲区对象的一个区域:
typedef struct cl_buffer_region {
size_t origin;
size_t size;
} cl_buffer_region;
-
origin is the offset in bytes of the region.
-
origin是区域的偏移量(以字节为单位)。
-
size is the size in bytes of the region.
-
size是区域的大小(以字节为单位)。
Constraints on the values of origin and size are specified for the clCreateSubBuffer function to which this structure is passed.
对原点和大小值的约束是为传递此结构的clCreateSubBuffer函数指定的。
1293




被折叠的 条评论
为什么被折叠?



