How to debug kernels called by a jcuda program ?

Hello everyone,

Is there any way to debug cuda functions called by java (jcuda) ?

My program launch this exception:

Exception in thread "main" jcuda.CudaException: CUDA_ERROR_ILLEGAL_ADDRESS
at jcuda.driver.JCudaDriver.checkResult(JCudaDriver.java:288)
at jcuda.driver.JCudaDriver.cuCtxSynchronize(JCudaDriver.java:1852)
at CalculateurGPU.updateAndCompute(rGPUcalculator.java:129)
at Test.main(Test.java:90)

I have a very long cuda code, and i can’t find any information in that to help me find where the error is.

Another question: when i click on “JCudaDriver.java:288” i don’t have access to it, it says “source not found”. How can i attach thesesources to my project ?

Thanks in advance

First, concerning the hotlinking of the source code: It depends on the IDE. In eclipse, it should open a window containing the bytecode, with a button at the top “Change attached source”. There, you can select, for example, a directory where you unpacked the source code.

(EDIT> But, by the way, this code should hardly be of interest for you - it does nothing than call the native method, and check the return value, and throws the exception when the return value indicates that the native call did not succeed. So you won’t gain any helpful information from there. <EDIT)

Concerning the debugging itself: It is possible to use cuda-memcheck with JCuda programs. I wrote a few words about this in this post: http://forum.byte-welt.net/byte-welt-projekte-projects/swogl-jcuda-jocl/jcuda/4435-jcuda-kernel-doesnt-dimension-data.html#post19277 . Basically, you have to create a .BAT file that starts your program, and pass this .BAT file to cuda-memcheck. (I aways wanted to add a tutorial page for this (and for JCuda/CUDA profiling), and have not tested this for a while, with the newer CUDA versions, but in principle, it should work). In any case, it would of course be beneficial to reduce the code that is to be tested to the minimum that is necessary to reproduce the error.

Thanks Marco, I’ll try that

And I’ll try to increase the priority of the “Debugging and Profiling HowTo” that I intended to write (together with some general remarks and “best practices”, as far as I can give an advice here)

So it took a while, but I just updated the documentation a bit, and added a dedicated section about debugging: jcuda.org - Debugging

“the only option for detecting errors in kernels is to use cuda-memcheck”
That’s not true.

I’ve had great success in using Nsight Visual Studio Edition to debug JCuda kernels. It’s very simple, as well. You just have to do the usual things:

  • Compile your kernel with -G flag
  • Set NSIGHT_CUDA_DEBUGGER environment variable to 1
  • Start Nsight Monitor
  • Attach Nsight debugger to the running Java process (the Java process can be paused on a breakpoint or running as long as it’s initialized a cuda context)
  • Open your .cu file in Visual Studio (the magic trick is that nvcc saves the absolute path to the source file inside the fatbin, so it automatically knows you’ve opened the right file)
  • Set a breakpoint in Visual Studio, or use asm volatile ("brkpt;");to break from within the cuda code (I make it part of an ASSERT macro).

All the other Nsight features are also available.

I would also remind everyone that putting cudaDeviceSynchronize after every kernel launch is a good way of quickly identifying which kernel is responsible for an exception. It would be nice if JCuda had a flag to do that automatically.

[QUOTE=alexd457]„the only option for detecting errors in kernels is to use cuda-memcheck“
That’s not true.

I’ve had great success in using Nsight Visual Studio Edition to debug JCuda kernels.
[/quote]

I don’t have the appropriate VS version here. But I tried this in my office, and … did not manage to manually attach to the Java process (although I don’t remember the exact reasons).

I would also remind everyone that putting cudaDeviceSynchronize after every kernel launch is a good way of quickly identifying which kernel is responsible for an exception. It would be nice if JCuda had a flag to do that automatically.

One could consider adding some sort of debug mode, where this is called after each method invocation, but I’ll have to think about this more throughoutly.

BTW: If you elaborated these debugging steps a little, I could add them to the website :wink: Otherwise, I’ll give it another try, and try to figure out what exactly went wrong during my first attempt…