CLCREATESUBBUFFER(3clc)
CLCREATESUBBUFFER π (3clc)
okay bestie, buckle UP, because we're doing OpenCL today and honestly? this one is kind of a banger. it's part of the OpenCL Manual, and no cap, it's giving "I make smaller buffers out of bigger buffers" energy. iconic behavior fr fr. π¦
β¨ NAME β¨
clCreateSubBuffer β bestie, it literally creates a new buffer object (we call this lil guy a sub-buffer object) from an existing buffer object. it's like taking a slice of cake π° but the cake is memory and the slice is legally its own buffer. periodt.
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);
that's the whole signature, copy it exactly, don't get cute with it π
π PARAMETERS π
bufferβ this one has to be a valid object, and here's the tea: it cannot be a sub-buffer object. nope. not allowed. π like, you can't make a sub-buffer out of a sub-buffer. the vibes simply do not permit it. so bring a real, original buffer to the function, bestie, and we'll take it from there. β¨flagsβ a bit-field, bestie. It's the thing that tells OpenCL all the allocation + usage tea about the sub-buffer memory object you're creating, and it's all laid out in the table below. Here's the β¨dramaβ¨ though: if you don't specifyCL_MEM_READ_WRITE,CL_MEM_READ_ONLYorCL_MEM_WRITE_ONLYinflags, they get inherited from the corresponding memory access qualifiers associated withbuffer. Same energy forCL_MEM_USE_HOST_PTR,CL_MEM_ALLOC_HOST_PTRandCL_MEM_COPY_HOST_PTRβ you literally cannot put those inflags, they're inherited from the corresponding memory access qualifiers associated withbuffer. And ngl, ifCL_MEM_COPY_HOST_PTRis specified in the memory access qualifier values associated withbuffer, it does not imply any additional copies when the sub-buffer is created frombuffer(no sneaky extra copying, we love that). And ifCL_MEM_HOST_WRITE_ONLY,CL_MEM_HOST_READ_ONLYorCL_MEM_HOST_NO_ACCESSaren't specified inflags? You guessed it β inherited from the corresponding memory access qualifiers associated withbuffer. π
πΈ the cl_mem_flags table but make it a vibe check πΈ
CL_MEM_READ_WRITEβ says the memory object will be read and written by a kernel. This one is the default, so if you're not about to make a choice, this is the choice. πCL_MEM_WRITE_ONLYβ says the memory object will be written but not read by a kernel. Now the scary part, no cap: reading from a buffer or image object created withCL_MEM_WRITE_ONLYinside a kernel is undefined. π AlsoCL_MEM_READ_WRITEandCL_MEM_WRITE_ONLYare mutually exclusive β pick one, not both, fr fr.CL_MEM_READ_ONLYβ says the memory object is a read-only memory object when used inside a kernel. And again, the warning: writing to a buffer or image object created withCL_MEM_READ_ONLYinside a kernel is undefined. π AndCL_MEM_READ_WRITEorCL_MEM_WRITE_ONLYandCL_MEM_READ_ONLYare mutually exclusive. Choose your fighter.CL_MEM_USE_HOST_PTRβ this flag is valid only ifhost_ptris notNULL. If you do specify it, it means the application wants the OpenCL implementation to use the memory referenced byhost_ptras the storage bits for the memory object. π«Ά
OpenCL implementations are allowed to cache the buffer contents pointed to by host_ptr in device memory. That cached copy can be used when kernels are executed on a device.
Big yikes zone: the result of OpenCL commands that operate on multiple buffer objects created with the same host_ptr or overlapping host regions is considered to be undefined.
Go read the dataTypes(3clc) page for host_ptr for memory objects (buffer and images) created using CL_MEM_USE_HOST_PTR. β¨
CL_MEM_ALLOC_HOST_PTRβ says the application wants the OpenCL implementation to allocate memory from host accessible memory. And yup,CL_MEM_ALLOC_HOST_PTRandCL_MEM_USE_HOST_PTRare mutually exclusive. π¦CL_MEM_COPY_HOST_PTRβ this flag is valid only ifhost_ptris notNULL. If specified, it means the application wants the OpenCL implementation to allocate memory for the memory object and copy the data from memory referenced byhost_ptr. π
And yeah, CL_MEM_COPY_HOST_PTR and CL_MEM_USE_HOST_PTR are mutually exclusive.
But CL_MEM_COPY_HOST_PTR can be used with CL_MEM_ALLOC_HOST_PTR to initialize the contents of the cl_mem object allocated using host-accessible (e.g. PCIe) memory. That combo ate, honestly. π₯
CL_MEM_HOST_WRITE_ONLYβ says 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 β like enabling write combined allocations for memory objects for devices that communicate with the host over a system bus such as PCIe. It's giving optimization main character energy. βCL_MEM_HOST_READ_ONLYβ says the host will only read the memory object (using OpenCL APIs that enqueue a read or a map for read). And bestie,CL_MEM_HOST_WRITE_ONLYandCL_MEM_HOST_READ_ONLYare mutually exclusive. πCL_MEM_HOST_NO_ACCESSβ says the host will not read or write the memory object. Iconic boundary-setting, honestly. π
And CL_MEM_HOST_WRITE_ONLY or
CL_MEM_HOST_READ_ONLYandCL_MEM_HOST_NO_ACCESSare mutually exclusive β bestie you literally cannot pick both, pick one, ok? π
π buffer_create_type and buffer_create_info π
So buffer_create_type tells the universe what kind of buffer object you're cooking up, and buffer_create_info is the pointer to the descriptor that goes with it. Here's the whole menu of supported values, no gatekeeping:
CL_BUFFER_CREATE_TYPE_REGIONβ creates a buffer object that represents a specific region insidebuffer. β¨
For that one, buffer_create_info is a pointer to this structure, and yes you may screenshot it:
typedef struct _cl_buffer_region {
size_t origin;
size_t size;
} cl_buffer_region;
So what do (origin, size) even mean? They define the offset and the size in bytes inside buffer. That's it. That's the whole vibe. π¦
Now the deets, in order, because order matters and I refuse to be sloppy:
- If
bufferwas created withCL_MEM_USE_HOST_PTR, then thehost_ptrassociated with the buffer object that gets returned is literallyhost_ptr + origin. Math. Iconic. π - The buffer object returned references the data store allocated for
bufferand points to a specific region given by(origin, size)inside that data store. CL_INVALID_VALUEgets returned inerrcode_retif the region specified by(origin, size)is out of bounds inbuffer. Rude of you honestly.CL_INVALID_BUFFER_SIZEifsizeis0. Zero is not a size bestie, it's a cry for help.CL_MISALIGNED_SUB_BUFFER_OFFSETgets returned inerrcode_retif there are no devices incontextassociated withbufferfor which theoriginvalue is aligned to theCL_DEVICE_MEM_BASE_ADDR_ALIGNvalue. Alignment queen, she does not negotiate. π
π NOTES π
Okay this one is important, so listen up, no cap:
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. π
The only thing that's actually defined is:
- reading from both a buffer object and its sub-buffer objects, or
- reading from multiple overlapping sub-buffer objects.
Read-only gang, you're safe. Everyone else, chaos. π
π¨ ERRORS π¨
If everything slays, you get a valid non-zero buffer object back and errcode_ret is set to CL_SUCCESS. Periodt. π
Otherwise, errcode_ret gets one of these, and honestly, some of them are your fault:
CL_INVALID_MEM_OBJECTifbufferis not a valid buffer object, or if it is a sub-buffer object. No sub-buffers of sub-buffers, we don't do that here.CL_INVALID_VALUEifbufferwas created withCL_MEM_WRITE_ONLYandflagsspecifiesCL_MEM_READ_WRITEorCL_MEM_READ_ONLY, or ifbufferwas created withCL_MEM_READ_ONLYandflagsspecifiesCL_MEM_READ_WRITEorCL_MEM_WRITE_ONLY, or ifflagsspecifiesCL_MEM_USE_HOST_PTRorCL_MEM_ALLOC_HOST_PTRorCL_MEM_COPY_HOST_PTR. Yeah that's a lot of conditions, I know, breathe. π«ΆCL_INVALID_VALUEifbufferwas created withCL_MEM_HOST_WRITE_ONLYandflagsspecifiesCL_MEM_HOST_READ_ONLY, or ifbufferwas created withCL_MEM_HOST_READ_ONLYandflagsspecifiesCL_MEM_HOST_WRITE_ONLY, or ifbufferwas created withCL_MEM_HOST_NO_ACCESSandflagsspecifiesCL_MEM_HOST_READ_ONLYorCL_MEM_HOST_WRITE_ONLY. The host access flags really said "mutually exclusive or nothing." πCL_INVALID_VALUEif the value specified inbuffer_create_typeis not valid.CL_INVALID_VALUEif the value(s) specified inbuffer_create_info(for a givenbuffer_create_type) is not valid, or ifbuffer_create_infoisNULL. NULL is not a personality, bestie.CL_INVALID_BUFFER_SIZEifsizeis0.CL_MEM_OBJECT_ALLOCATION_FAILUREif there is a failure to allocate memory for the sub-buffer object. πCL_OUT_OF_RESOURCESif there is a failure to allocate resources required by the OpenCL implementation on the device.CL_OUT_OF_HOST_MEMORYif there is a failure to allocate resources required by the OpenCL implementation on the host.
π SPECIFICATION π
OpenCL Specification[1] β go read it, it's giving β¨ primary source β¨.
π SEE ALSO π
The friend group, all linked, all valid:
π«Ά AUTHORS π«Ά
The Khronos Group β the blueprint. The blueprint. π§
π COPYRIGHT π
Copyright Β© 2007-2011 The Khronos Group Inc.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and/or associated documentation files (the "Materials"), to deal in the Materials without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Materials, and to permit persons to whom the Materials are furnished to do so, subject to the condition that this copyright notice and permission notice shall be included in all copies or substantial portions of the Materials.
- The OpenCL Specification β page 69, section
5.2.1 - Creating Buffer Objectsπ that's the receipt, bestie, the whole vibe of this page lives right there. Go read it if you want the full lore, iykyk πβ¨
And that's a wrap on clCreateSubBuffer, no cap π whole page, every detail, nothing skipped. This one comes to you courtesy of The Khronos Group (they're the source, the moment, the main characters fr), stamped 01/14/2021, and it lives in the world as CLCREATESUBBUFFER(3clc) ππ
go make some sub-buffers, you absolute icon π¦π
π¦ where this page comes from
- ubuntu jammy: opencl-1.2-man-doc 1.0~svn33624-5 Β· other
- ubuntu noble: opencl-1.2-man-doc 1.0~svn33624-5 Β· other
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms