CLCREATESUBBUFFER(3clc)

πŸ“¦
version 1.0~svn33624 Β· 3 versions
, in ubuntu jammy, ubuntu noble

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 specify CL_MEM_READ_WRITE, CL_MEM_READ_ONLY or CL_MEM_WRITE_ONLY in flags, they get inherited from the corresponding memory access qualifiers associated with buffer. Same energy for CL_MEM_USE_HOST_PTR, CL_MEM_ALLOC_HOST_PTR and CL_MEM_COPY_HOST_PTR β€” you literally cannot put those in flags, they're inherited from the corresponding memory access qualifiers associated with buffer. And ngl, 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 (no sneaky extra copying, we love that). And if CL_MEM_HOST_WRITE_ONLY, CL_MEM_HOST_READ_ONLY or CL_MEM_HOST_NO_ACCESS aren't specified in flags? You guessed it β€” inherited from the corresponding memory access qualifiers associated with buffer. πŸ’…

🌸 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 with CL_MEM_WRITE_ONLY inside a kernel is undefined. πŸ’€ Also CL_MEM_READ_WRITE and CL_MEM_WRITE_ONLY are 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 with CL_MEM_READ_ONLY inside a kernel is undefined. 😭 And CL_MEM_READ_WRITE or CL_MEM_WRITE_ONLY and CL_MEM_READ_ONLY are mutually exclusive. Choose your fighter.
  • CL_MEM_USE_HOST_PTR β€” this flag is valid only if host_ptr is not NULL. If you do specify it, it means the application wants the OpenCL implementation to use the memory referenced by host_ptr as 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_PTR and CL_MEM_USE_HOST_PTR are mutually exclusive. πŸ¦‹
  • CL_MEM_COPY_HOST_PTR β€” this flag is valid only if host_ptr is not NULL. If specified, it means the application wants the OpenCL implementation to allocate memory for the memory object and copy the data from memory referenced by host_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_ONLY and CL_MEM_HOST_READ_ONLY are 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_ONLY and CL_MEM_HOST_NO_ACCESS are 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 inside buffer. ✨

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 buffer was created with CL_MEM_USE_HOST_PTR, then the host_ptr associated with the buffer object that gets returned is literally host_ptr + origin. Math. Iconic. πŸ’…
  • The buffer object returned references the data store allocated for buffer and points to a specific region given by (origin, size) inside that data store.
  • CL_INVALID_VALUE gets returned in errcode_ret if the region specified by (origin, size) is out of bounds in buffer. Rude of you honestly.
  • CL_INVALID_BUFFER_SIZE if size is 0. Zero is not a size bestie, it's a cry for help.
  • CL_MISALIGNED_SUB_BUFFER_OFFSET gets returned in errcode_ret if there are no devices in context associated with buffer for which the origin value is aligned to the CL_DEVICE_MEM_BASE_ADDR_ALIGN value. 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_OBJECT if buffer is 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_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. Yeah that's a lot of conditions, I know, breathe. 🫢
  • CL_INVALID_VALUE if buffer was created with CL_MEM_HOST_WRITE_ONLY and flags specifies CL_MEM_HOST_READ_ONLY, or if buffer was created with CL_MEM_HOST_READ_ONLY and flags specifies CL_MEM_HOST_WRITE_ONLY, or if buffer was created with CL_MEM_HOST_NO_ACCESS and flags specifies CL_MEM_HOST_READ_ONLY or CL_MEM_HOST_WRITE_ONLY. The host access flags really said "mutually exclusive or nothing." 😭
  • CL_INVALID_VALUE if the value specified in buffer_create_type is not valid.
  • CL_INVALID_VALUE if the value(s) specified in buffer_create_info (for a given buffer_create_type) is not valid, or if buffer_create_info is NULL. NULL is not a personality, bestie.
  • CL_INVALID_BUFFER_SIZE if size is 0.
  • CL_MEM_OBJECT_ALLOCATION_FAILURE if there is a failure to allocate memory for the sub-buffer object. πŸ’€
  • CL_OUT_OF_RESOURCES if there is a failure to allocate resources required by the OpenCL implementation on the device.
  • CL_OUT_OF_HOST_MEMORY if 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 Β© 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.

  1. 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