CLENQUEUEREADBUFFER(3clc)

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

πŸ’– CLENQUEUEREADBUFFER πŸ’–

okay bestie, so this is a page from the OpenCL Manual (section 3clc) and it's literally all about getting data OUT of a buffer object and INTO your host memory. no cap. let's get into it 🫢


✨ NAME ✨

clEnqueueReadBuffer β€” enqueues commands to read from a buffer object to host memory. it's giving "bring the data home" energy 🏠✨


✨ SYNOPSIS ✨

cl_int clEnqueueReadBuffer(cl_command_queue command_queue,
                           cl_mem buffer, cl_bool blocking_read,
                           size_t offset, size_t size, void *ptr,
                           cl_uint num_events_in_wait_list,
                           const cl_event *event_wait_list,
                           cl_event *event);

look at her. nine arguments. main character energy fr fr πŸ‘‘


✨ PARAMETERS ✨

  • command_queue β€” this is the command-queue where your read command is gonna get queued up. IMPORTANT: command_queue and buffer have to be created with the same OpenCL context. they're a package deal, no exceptions πŸ’…
  • buffer β€” has to be a valid buffer object. that's it, that's the vibe.
  • blocking_read β€” tells us if the read ops are blocking or non-blocking, and bestie this one actually matters:
    • if blocking_read is CL_TRUE, i.e. the read command is blocking, then clEnqueueReadBuffer does not return until the buffer data has been read and copied into the memory that ptr points to. she waits. patiently. 🧘
    • if blocking_read is CL_FALSE, i.e. the read command is non-blocking, then clEnqueueReadBuffer queues a non-blocking read command and dips. the contents of the buffer that ptr points to cannot be used until the read command has completed. the event argument returns an event object you can use to query the execution status of the read command. once the read command has completed, the contents of the buffer that ptr points to are finally usable by your application. πŸ¦‹
  • offset β€” the offset in bytes in the buffer object to read from. where we start, basically.
  • size β€” the size in bytes of the data being read. how much we're grabbing.
  • ptr β€” the pointer to the buffer in host memory where the data is gonna be read into. the landing zone. πŸ›¬
  • event_wait_list, num_events_in_wait_list β€” these two specify events that need to complete before this particular command can execute. here's the tea:
    • if event_wait_list is NULL, then this particular command does not wait on any event to complete.
    • if event_wait_list is NULL, then num_events_in_wait_list must be 0.
    • if event_wait_list is not NULL, then the list of events pointed to by event_wait_list must be valid and num_events_in_wait_list must be greater than 0.
    • the events specified in event_wait_list act as synchronization points ⏳
    • the context associated with events in event_wait_list and command_queue must be the same.
    • the memory associated with event_wait_list can be reused or freed after the function returns. so like, don't stress about cleanup timing, she's done with it. πŸ’…
  • event β€” returns an event object that identifies this particular read command and can be used to query or queue a wait for this particular command to complete. event can be NULL, in which case it will not be possible for the application to query the status of this command or queue a wait for this command to complete. and heads up: if the event_wait_list and the event arguments are not NULL, the event argument should not refer to an element of the event_wait_list array. don't do it. πŸ™ˆ

✨ NOTES ✨

so there's a specific situation here and it's lowkey important. calling clEnqueueReadBuffer to read a region of the buffer object with the ptr argument value set to host_ptr + offset, where host_ptr is a pointer to the memory region specified when the buffer object being read is created with CL_MEM_USE_HOST_PTR, must meet the following requirements in order to avoid undefined behavior:

  • All commands that use this buffer object or a memory object (buffer or image) created from this buffer object have finished execution before the read command begins execution.
  • The buffer object or memory objects created from this buffer object are not mapped.
  • The buffer object or memory objects created from this buffer object are not used by any command-queue until the read command has finished execution.
🚨 bestie these three are not suggestions. undefined behavior is that girl you do NOT want to meet in a dark alley. respect the rules. 🚨

✨ ERRORS ✨

clEnqueueReadBuffer returns CL_SUCCESS if the function is executed successfully. otherwise, she returns one of the following errors:

  • CL_INVALID_COMMAND_QUEUE if command_queue is not a valid command-queue. πŸ’€
  • CL_INVALID_CONTEXT if the context associated with command_queue and buffer are not the same or if the context associated with command_queue and events in event_wait_list are not the same.
  • CL_INVALID_MEM_OBJECT if buffer is not a valid buffer object.
  • CL_INVALID_VALUE if the region being read specified by (offset, size) is out of bounds or if ptr is a NULL value or if size is 0. (yes, zero bytes counts as invalid, sorry πŸ₯²)
  • CL_INVALID_EVENT_WAIT_LIST if event_wait_list is NULL and num_events_in_wait_list greater than 0, or event_wait_list is not NULL and num_events_in_wait_list is 0, or if event objects in event_wait_list are not valid events.
  • CL_MISALIGNED_SUB_BUFFER_OFFSET if buffer is a sub-buffer object and offset specified when the sub-buffer object is created is not aligned to CL_DEVICE_MEM_BASE_ADDR_ALIGN value for device associated with queue. 🧐
  • CL_EXEC_STATUS_ERROR_FOR_EVENTS_IN_WAIT_LIST if the read and write operations are blocking and the execution status of any of the events in event_wait_list is a negative integer value.
  • CL_MEM_OBJECT_ALLOCATION_FAILURE if there is a failure to allocate memory for data store associated with buffer.
  • CL_INVALID_OPERATION if clEnqueueReadBuffer is called on buffer which has been created with CL_MEM_HOST_WRITE_ONLY or CL_MEM_HOST_NO_ACCESS. (like, you literally said "don't read this" and then tried to read it. rude. 😭)
  • 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]


✨ SEE ALSO ✨

clEnqueueCopyBuffer(3clc), clEnqueueWriteBuffer(3clc), clEnqueueWriteBufferRect(3clc), clEnqueueReadBufferRect(3clc)

the whole friend group fr fr πŸ«‚


✨ AUTHORS ✨

The Khronos Group πŸ‘‘


Copyright Β© 2007-2010 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.


✨ NOTES ✨

  1. OpenCL Specification β€” page 72, section 5.2.2 - Reading, Writing, Copying Buffer Objects πŸ“–

and that's the whole page bestie!! this lil masterpiece is brought to you by The Khronos Group, dated 01/14/2021, and it lives under the name CLENQUEUEREADBUFFER(3clc). go read some buffers, slay, and remember: check your blocking_read flag before you touch ptr. mwah πŸ’‹βœ¨πŸŽ€

πŸ“¦ where this page comes from