简体   繁体   中英

Out of boundary detection in OpenCL

I was experimenting with OpenCL (C++ interface) and, without noticing it, I created a buffer for 10 integers using buffer size equal to 10 , instead of 10 * sizeof(int) , but the code run apparently without issues.

Now, I believe that this could be possible because I created the buffer with CL_MEM_USE_HOST_PTR flag and this made possible to access out of boundary memory (though I am not sure of this).

So, my question is: is it possible to enforce out of bounds error checking in OpenCL, so that any access outsize a given area is reported?

You might want to try the WebCL Validator: https://github.com/KhronosGroup/webcl-validator

It's a command-line tool that instruments your OpenCL kernel source code with run-time checks for out-of-bounds memory accesses. It's still work in progress, so any feedback would be much appreciated.

As other posters have already indicated, out-of-bound checking is currently not supported by OpenCL drivers. While tools like the WebCL Validator are promising in this area, I would like to mention another path, based on existing tools, which has helped me in the past. By using the FreeOCL CPU driver , which relies on a standard C++ compiler to compile your kernels (after a source-to-source translation step) you can use a tool like valgrind on your final program and get the typical valgrind error messages like this:

==5863== Thread 6:
==5863== Invalid write of size 1
==5863==    at 0xD61FA5D: __FCL_kernel_krnl_route_pkt (filehFymmN:27)

You can then directly refer to the C++ version of the kernel ( /tmp/filehFymmN line 27 in the example) to find where the offending operation happened.

Short answer: no.

Long answer: ATI and NVIDIA are very forgiving for accessing memory out-of-bounds, but Intel will crash (haven't tested AMD cpus).

For something like an Anisotropic filter where you are accessing n, n + 1, and n - 1, you should either use global offsets to avoid accessing memory out of bounds or check in the kernel with if statements. Global Offset is nice, but NVIDIA doesn't support it, so there is that.

Unfortunately, using try / catch on the host code doesn't seem to work either since there is magic being done in the OpenCL.dll you are linking to.

Note: This is on SDK and code about 4 months old, dunno if it has changed since then.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM