The National Institute for Computational Sciences

Darter: How do I use the code bisection method to find a bug?

Darter: How do I use the code bisection method to find a bug?

While using tools is a preferable method of debugging to simply using print statements, sometimes the latter option is the only method to find the bug. In this case, the most effective way to isolate the error in your code is through the method of bisection, which is an iterative process for tracing the program manually.

Step 1: In the main routine of your code, comment out the second half of the code (or approximately the second half).

Step 2: Compile and run the code. Did it crash as before?

Step 3A: If yes, return to step one and comment out the second half of the part of the main routine that ran successfully. Repeat until you have narrowed it down to the line/routine causing issues, which may include following this same tack within a subroutine.

Step 3B: If no, then swap out the half which was commented and try compiling and running again. Then, go to Step 3A.

Additionally, the use of print statements to see variable values can give insight into some earlier piece of code that might have been run through just fine but is creating an errant, unacceptable value that causes a later routine to crash.

Finally, if there is any way to duplicate the error in serial, this makes the print statements more consistent (as far as being ordered chronologically, since they are not all coming from different processors' buffers).

Now, while this might sound like a lot of work, and it is non-trivial, here is a tip to make your burden lighter: Have three sessions open on Darter simultaneously.

1. One session to edit the code.
2. Another session to compile the code.
3. Another session in which you submit for an Interactive Job so that you do not have to submit your job every time and wait in the queue.