CLSETMEMOBJECTDESTRU(3clc)
π NAME π
okay bestie, so clSetMemObjectDestructorCallback β it registers a user callback function with a memory object. that's the whole vibe. it's the "hey text me when this memory is free" of OpenCL π«Ά
and here's the whole signature, character for character, no cap:
cl_int clSetMemObjectDestructorCallback(cl_mem memobj,
void (CL_CALLBACK *pfn_notify) (cl_mem memobj,
void *user_data),
void *user_data);
it's giving "OpenCL Manual, section 3clc" energy if you were wondering where this page lives π
β¨ PARAMETERS β¨
memobjβ a valid memory object. yes it has to be valid, we're not doing chaos here πpfn_notifyβ the callback function that can be registered by the application. lowkey important: this callback function may be called asynchronously by the OpenCL implementation. so it is the application's responsibility to make sure the callback function is thread-safe. not optional, bestie. the parameters to this callback function are:memobjβ the memory object being deleted. when the user callback is called by the implementation, this memory object is no longer valid.memobjis only provided for reference purposes. (so like... it's a ghost π» you can look but you cannot touch)user_dataβ a pointer to user supplied data.
user_dataβ data which will be passed as theuser_dataargument whenpfn_notifyis called.user_datacan beNULL. she's flexible like that πΈ
π NOTES π
every single call to clSetMemObjectDestructorCallback registers the specified user callback function on a callback stack associated with memobj. and here's the iconic part: the registered user callback functions are called in the reverse order in which they were registered. yes, LIFO, stack behavior, main character energy. the user callback functions are called and then the memory object's resources are freed and the memory object is deleted.
so why do we care? because this provides a mechanism for the application (and libraries) using memobj to be notified when the memory referenced by host_ptr β specified when the memory object is created and used as the storage bits for the memory object β can be reused or freed. it's literally a little "it's safe now bestie" ping β¨
when the user callback function is called by the implementation, the contents of the memory region pointed to by host_ptr (if the memory object is created with CL_MEM_USE_HOST_PTR) are undefined. the callback function is typically used by the application to either free or reuse the memory region pointed to by host_ptr.
β οΈ okay real talk, this is the dangerous part. the behavior of calling expensive system routines, OpenCL API calls to create contexts or command-queues, or blocking OpenCL operations from the following list below, in a callback, is undefined. undefined. do not do it. i'm serious π
clFinish(3clc)clWaitForEvents(3clc)- blocking calls to
clEnqueueReadBuffer(3clc),clEnqueueReadBufferRect(3clc),clEnqueueWriteBuffer(3clc),clEnqueueWriteBufferRect(3clc) - blocking calls to
clEnqueueReadImage(3clc)andclEnqueueWriteImage(3clc) - blocking calls to
clEnqueueMapBuffer(3clc)andclEnqueueMapImage(3clc) - blocking calls to
clBuildProgram(3clc),clCompileProgram(3clc), orclLinkProgram(3clc)
so if an application needs to wait for completion of a routine from the above list in a callback, please use the non-blocking form of the function, and assign a completion callback to it to do the remainder of your work. π§
note that when a callback (or other code) enqueues commands to a command-queue, the commands are not required to begin execution until the queue is flushed. in standard usage, blocking enqueue calls serve this role by implicitly flushing the queue. since blocking calls are not permitted in callbacks, those callbacks that enqueue commands on a command queue should either call clFlush(3clc) on the queue before returning or arrange for clFlush(3clc) to be called later on another thread. slay, get that flush in π
π« and one more: the user callback function may not call OpenCL APIs with the memory object for which the callback function is invoked, and for such cases the behavior of OpenCL APIs is considered to be undefined.
π₯ ERRORS π₯
returns CL_SUCCESS if the function is executed successfully. otherwise, it returns one of the following errors:
CL_INVALID_MEM_OBJECTifmemobjis not a valid memory object.CL_INVALID_VALUEifpfn_notifyisNULL.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]
π SEE ALSO π
clCreateCommandQueue(3clc), clGetCommandQueueInfo(3clc), clReleaseCommandQueue(3clc), clRetainCommandQueue(3clc)
π AUTHORS π
The Khronos Group
π 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.
β NOTES β
- OpenCL Specification page 119, section 5.4.1 - Retaining and Releasing Memory Objects
okay that's the whole page bestie, you ate it πβ¨π¦ this was brought to you by The Khronos Group, dated 01/14/2021, and the page you just read is CLSETMEMOBJECTDESTRU(3clc). no cap, go flush your queues π«Άπ
πΈ
π¦ 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