Changes between Version 3 and Version 4 of DebugMode


Ignore:
Timestamp:
Nov 4, 2021, 5:13:22 PM (3 years ago)
Author:
abierjon
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • DebugMode

    v3 v4  
    1313You can add or remove debug flag manually in the file trunk/LMDZ.COMMON/arch.fcm at the line %DEBUG_FFLAGS. Make sure you are using correct flags regarding your compiler, and the debug mode performs no optimization (e.g: -O0 with mpif90).
    1414
    15 == Methodology: 1 + 1 = 2
     15== METHODOLOGY: 1 + 1 = 2
    1616
    1717This methodology is very useful when:
     
    5757You can do this check quickly by using the GCM variable 'ndynstep' in run.def file, this variable means the number of dynamical steps you want to perform during the simulation. Be sure to set this variable equals to iphysiq to perform at least one physical call. For example, with iphysiq = 10, you can do : 10 + 10 = 20 dynamical steps, corresponding to 1 call to physiq_mod + 1 call to physiq_mod = 2 calls to physiq_mod.
    5858
    59 == Methodology: 1 = 1
     59== METHODOLOGY: 1 = 1
    6060
    6161Same as 1 + 1 = 2, except you perform runs with 1 day with 1 CPU, and 1 day with 24 CPUs! Very useful when you have memory issues.
    6262
    6363
    64 === USING GDB FOR DEBUG
     64----
    6565
     66== USING GDB FOR DEBUG
     67=== Intro
    6668If you have a really vicious bug in your code, you may want to use the **gdb** tool. Here is some advice to use it, but it is not exhaustive, so please complete this info if you can. You can also find more details in the [https://sourceware.org/gdb/current/onlinedocs/gdb/GNU-Free-Documentation-License.html#GNU-Free-Documentation-License GDB documentation]
    6769
    68 To use gdb, you have to compile your model with the {{{-debug}}} option.
     70To use gdb, you have to compile your model with the {{{-debug}}} option. This option especially contains two necessary compilation options that are {{{-O0}}} and {{{-g3}}}.
     71 
    6972Once it's done, go in your simulation repository (where your have all your .def and start files, and your gcm executable ''gcm_exec_debug.e''), and source your arch.env from your trunk/LMDZ.COMMON to have the required librairies to run the model.
    7073Then, run the command  {{{ gdb gcm_exec_debug.e }}}
    7174
    72 It opens a gdb session. You can now run the model and interact with it while it runs. For instance, you can create ''break points'', which stop the run at a given point. **Be careful** : if you specify a break point at a given line of the code, it has to be the line number from the **compiled** version of the program! (.f or .f90, in LMDZ.COMMON/tmp_src/).
    73 
    74 You can then look at some variables via the ''print'' command, then continue the run. Here is an example below :
     75It opens a gdb session. You can now run the model and interact with it while it runs. For instance, you can create ''break points'', which pause the run at a given point. You can then look at some variables via the ''print'' command, then continue the run. Here is an example below :
    7576
    7677{{{
     
    110111}}}
    111112
    112 Try  {{{(gdb) print MODULENAME::VARNAME}}}  if you want to print a variable (''VARNAME'') from another module (''MODULENAME'') that the subroutine is using. If you want to look at a variable saved in a Fortran ''COMMON'' block (ex: variables from the infamous ''callkeys.h''), doing  {{{(gdb) info common}}}  will print the value of all the variables from the ''COMMON'' blocks.
     113Below, we give some more details on specific **gdb** commands.
     114
     115=== Break points
     116You can define break points by specifying a line where the program will pause : {{{(gdb) break PROGRAM_NAME:LINE_NB}}}
     117**Careful** : if you specify a break point at a given line of the GCM code, it has to be the line number from the **pre-processed** version of the program ! (.f or .f90, in LMDZ.COMMON/tmp_src/).
     118
     119Break points also work by specifying a call to a function/subroutine : {{{(gdb) break SUBROUTINE_NAME}}} will pause the run when it encounters a {{{call SUBROUTINE_NAME}}}. It still enters the subroutine, which becomes the current context. If the subroutine is defined in a module, try {{{(gdb) break MODULE_NAME::SUBROUTINE_NAME}}}
     120
     121Break points can be defined before and during the {{{(gdb) run}}}  (see the example above).
     122
     123=== Watch points
     124''Watch points'' enables you to follow the state of a variable, by pausing the program when the value of this variable changes, and by displaying the change that has been done. You can set up a watch point via the command {{{(gdb) watch VAR_NAME}}}.
     125
     126**Careful** : you can set a watch point only on variables that have already been defined in your current context ! It can thus only be used after the {{{(gdb) run}}}, and usually requires to do a break point before in the subroutine (if you want to watch a local variable of this subroutine).
     127
     128=== Print
     129After you have stopped at some point in the code (thanks to a break or watch point for instance), you can print the value of variables.
     130The syntax for printing variables that are defined in your current context (''ex:'' local variables or arguments of your subroutine) is simply {{{(gdb) print VAR_NAME}}}
     131
     132Try  {{{(gdb) print MODULE_NAME::VAR_NAME}}}  if you want to print a variable (''VAR_NAME'') from another module (''MODULE_NAME'') that the subroutine is using.
     133
     134If you want to look at a variable saved in a Fortran ''COMMON'' block (''ex:'' variables from the infamous ''callkeys.h''), doing  {{{(gdb) info common}}}  will print the value of all the variables from the ''COMMON'' blocks.
     135
     136''NB:'' if you have stopped in a subroutine which is now your current context, you can go back to your main program (where you have the instruction {{{call SUBROUTINE_NAME}}}) and thus change the context thanks to the command {{{(gdb) up}}}. To go back in your subroutine, use {{{(gdb) down}}}.
     137
     138=== List
     139Pretty useful, the command {{{(gdb) list}}} displays the code line where you just stopped, as well as some of the lines that will come just after.
     140
     141Remember that when you pause at a given line, you haven't executed it yet ! (you're stopped at the beginning of the line)
     142
     143=== Continue / Step
     144After you have paused the run, you can resume running with the command {{{(gdb) continue}}}
     145
     146You can also just run the next line and pause again with the command {{{(gdb) step}}}
     147
     148=== Debug in parallel
     149If you want to do some debug while running in parallel, it should be possible to do a {{{mpirun gdb gcm_exec_debug.e}}}, which will run multiple instances of {{{gdb gcm_exec_debug.e}}} (one in each thread) that will still communicate and wait for each other when they need it (as when you run the model directly, in parallel).
     150Note that this is a bit complicated and not so well tested among us. It may be better to use other tools, like **ddd** or **ddt** (though they are not installed on all machines).