问题
Is there any way to kill a running OpenCL kernel through the OpenCL API? I haven't found anything in the spec.
The only solutions I could come up with are 1) periodically checking a flag in the kernel that the host writes to when it wants the kernel to stop, or 2) running the kernel in a separate process and killing the entire process. I don't think either of those are very elegant solutions, and I'm not sure #1 would even work reliably.
回答1:
No, the OpenCL API doesn't allow to interrupt a running kernel. On some systems, a kernel running for more than a few seconds will be killed by the driver.
Ref. for Windows: Timeout Detection and Recovery of GPUs through WDDM, and for Linux+NVidia: Using CUDA and X.
Nothing in the standard guarantees your option 1 will work. It almost certainly won't. Your option 2 will work, but then you will have to communicate with the other process.
Splitting the kernel in smaller kernels, as suggested in the comments, is probably the best option.
来源:https://stackoverflow.com/questions/16308667/killing-opencl-kernels