如果我使用选项CL_MEM_USE_HOST_PTR创建一个OpenCL缓冲区,然后在使用它之后在缓冲区上调用clReleaseMemObject,我仍然需要在动态分配的内存上调用free。 此外,如果内核写入缓冲区,它会在调用clReleaseMemObject后与主机指针同步,以便我可以读取结果吗?
If I create an OpenCL buffer with the option CL_MEM_USE_HOST_PTR and then call clReleaseMemObject on the buffer after using it do I still need to call free on the memory I dynamically allocated. Also if a kernel write to the buffer will it sync with the host pointer after I call clReleaseMemObject so I can read the result?
最满意答案
你必须释放记忆本身。
想想看 - OpenCL如何知道为您释放内存的正确方法? 也许它来自malloc ,或者它来自alloca ,或者它来自VirtualAlloc (在Windows上)或sbrk (在Linux上),或者它可能是一个全局变量,或者它可能是一个内存映射文件,或者它可能是字符串字面量。 也许你可以想到更多。
OpenCL不知道哪些是真的,即使它确实如此,它甚至不知道你想要释放内存。 (取消映射该文件只是因为你已经完成了对它的使用OpenCL?也许你想在相同的数据上运行一些非OpenCL代码!)
You must free the memory itself.
Think about it - how would OpenCL know the right way to free the memory for you? Maybe it came from malloc, or maybe it came from alloca, or maybe it came from VirtualAlloc (on Windows) or sbrk (on Linux), or maybe it's a global variable, or maybe it's a memory-mapped file, or maybe it's a string literal. Perhaps you can think of even more.
OpenCL doesn't know which of those are true, and even if it did, it doesn't even know that you want the memory freed. (Unmap that file just because you've finished using OpenCL on it? Maybe you want to run some non-OpenCL code on the same data!)
更多推荐
发布评论