Since you’re running on windows, this may be nothing more than you hitting the windows TDR mechanism. Kernels that run longer than about 2 seconds on windows may trigger the TDR mechanism. Usually when this happens, you will see the screen flash to black and then repaint itself, as the GPU goes through the windows-triggered reset. You may also see a systray message. The exact behavior may also be different if you are running the code from within VS versus running from the command line.
Refer to the above link or search on the CUDA tag for how to modify the TDR mechanism.
Windows WDDM Driver Timeout Detection and Recovery mechanism can be disabled or the timeout can be extended to be greater than the default 2 seconds.Timeout Detection and Recovery is documented on MSDN.
Nsight Visual Studio Edition Nsight.Monitor has settings to disable or increase the timeout. Otherwise, you can use the registry keys in the MSDN article. Make sure to restart the computer after making changes.
I recommend that you increase TdrDelay before completely disabling TDR.
Tesla GPUs can use the Tesla Compute Cluster driver which does not have a timeout watchdog.