xref: /petsc/doc/miscellaneous/codemanagement.md (revision 9b92b1d3a7bbc5a081edc9021bfd15a36804dd1c)
1*9b92b1d3SBarry Smith# Code Management
2*9b92b1d3SBarry Smith
3*9b92b1d3SBarry SmithWe list some of the techniques that may be used to increase one's
4*9b92b1d3SBarry Smithefficiency when developing PETSc application codes. We have learned to
5*9b92b1d3SBarry Smithuse these techniques ourselves, and they have improved our efficiency
6*9b92b1d3SBarry Smithtremendously.
7*9b92b1d3SBarry Smith
8*9b92b1d3SBarry Smith## Editing and Compiling
9*9b92b1d3SBarry Smith
10*9b92b1d3SBarry SmithThe biggest time sink in code development is generally the cycle of
11*9b92b1d3SBarry SmithEDIT-COMPILE-LINK-RUN. We often see users working in a single window
12*9b92b1d3SBarry Smithwith a cycle such as:
13*9b92b1d3SBarry Smith
14*9b92b1d3SBarry Smith- Edit a file with `emacs` or `vim`.
15*9b92b1d3SBarry Smith- Exit `emacs` or `vim`.
16*9b92b1d3SBarry Smith- Run `make` and see some error messages.
17*9b92b1d3SBarry Smith- Start `emacs` or `vim` and try to fix the errors; often starting
18*9b92b1d3SBarry Smith  the editor hides the error messages so that users cannot remember all
19*9b92b1d3SBarry Smith  of them and thus do not fix all the compiler errors.
20*9b92b1d3SBarry Smith- Run `make` generating a bunch of object (.o) files.
21*9b92b1d3SBarry Smith- Link the executable (which also removes the .o files). Users may
22*9b92b1d3SBarry Smith  delete the .o files because they anticipate compiling the next time
23*9b92b1d3SBarry Smith  on a different machine that uses a different compiler.
24*9b92b1d3SBarry Smith- Run the executable.
25*9b92b1d3SBarry Smith- Detect some error condition and restart the cycle.
26*9b92b1d3SBarry Smith
27*9b92b1d3SBarry SmithIn addition, during this process the user often moves manually among
28*9b92b1d3SBarry Smithdifferent directories for editing, compiling, and running.
29*9b92b1d3SBarry Smith
30*9b92b1d3SBarry Smith## Several easy ways to improve the cycle
31*9b92b1d3SBarry Smith
32*9b92b1d3SBarry Smith- `emacs` and `vim` have a feature to allow the user to compile
33*9b92b1d3SBarry Smith  using make and have the editor automatically jump to the line of
34*9b92b1d3SBarry Smith  source code where the compiler detects an error, even when not
35*9b92b1d3SBarry Smith  currently editing the erroneous file.
36*9b92b1d3SBarry Smith
37*9b92b1d3SBarry Smith- The etags feature of `emacs` or tags feature of `vim` enables one
38*9b92b1d3SBarry Smith  to search quickly through a group of user-defined source files
39*9b92b1d3SBarry Smith  (and/or PETSc source files) regardless of the directory in which they
40*9b92b1d3SBarry Smith  are located. [GNU Global](http://www.gnu.org/s/global) is a richer
41*9b92b1d3SBarry Smith  alternative to etags.
42*9b92b1d3SBarry Smith
43*9b92b1d3SBarry Smith- Also, `emacs` and `vim` easily enable:
44*9b92b1d3SBarry Smith
45*9b92b1d3SBarry Smith  - editing files that reside in any directory and retaining one's
46*9b92b1d3SBarry Smith    place within each of them
47*9b92b1d3SBarry Smith  - searching for files in any directory as one attempts to load them
48*9b92b1d3SBarry Smith    into the editor
49*9b92b1d3SBarry Smith
50*9b92b1d3SBarry SmithYou might consider using `Microsoft Visual Studio`, `Eclipse` or
51*9b92b1d3SBarry Smithother advanced software development systems. See the {ref}`Users Manual<sec-developer-environments>`.
52*9b92b1d3SBarry Smith
53*9b92b1d3SBarry Smith## Debugging
54*9b92b1d3SBarry Smith
55*9b92b1d3SBarry SmithMost code development for PETSc codes should be done on one processor;
56*9b92b1d3SBarry Smithhence, using a standard debugger such as dbx, gdb, xdbx, etc. is fine.
57*9b92b1d3SBarry SmithFor debugging parallel runs we suggest **Totalview** if it is available
58*9b92b1d3SBarry Smithon your machine. Otherwise, you can run each process in a separate
59*9b92b1d3SBarry Smithdebugger; this is not the same as using a parallel debugger, but in most
60*9b92b1d3SBarry Smithcases it is not so bad. The PETSc run-time options
61*9b92b1d3SBarry Smith`-start_in_debugger` [-display xdisplay:0] will open separate windows
62*9b92b1d3SBarry Smithand debuggers for each process. You should debug using the debugging
63*9b92b1d3SBarry Smithversions of the libraries (run ./configure with the additional option
64*9b92b1d3SBarry Smith--with-debugging (the default)).
65*9b92b1d3SBarry Smith
66*9b92b1d3SBarry SmithIt really pays to learn how to use a debugger; you will end up writing
67*9b92b1d3SBarry Smithmore interesting and far more ambitious codes once it is easy for you to
68*9b92b1d3SBarry Smithtrack down problems in the codes.
69*9b92b1d3SBarry Smith
70*9b92b1d3SBarry Smith## Other suggestions
71*9b92b1d3SBarry Smith
72*9b92b1d3SBarry Smith- Choose consistent and obvious names for variables and functions.
73*9b92b1d3SBarry Smith  (Short variable names may be faster to type, but by using longer
74*9b92b1d3SBarry Smith  names you don't have to remember what they represent since it is
75*9b92b1d3SBarry Smith  clear from the name.)
76*9b92b1d3SBarry Smith- Use informative comments.
77*9b92b1d3SBarry Smith- Leave space in the code to make it readable.
78*9b92b1d3SBarry Smith- Line things up in the code for readability. Remember that any code
79*9b92b1d3SBarry Smith  written for an application will be visited **over and over** again,
80*9b92b1d3SBarry Smith  so spending an extra 20 percent of effort on it the first time will
81*9b92b1d3SBarry Smith  make each of those visits faster and more efficient.
82*9b92b1d3SBarry Smith- Realize that you **will** have to debug your code. **No one** writes
83*9b92b1d3SBarry Smith  perfect code, so always write code that may be debugged and learn how
84*9b92b1d3SBarry Smith  to use a debugger. In most cases using the debugger to track down
85*9b92b1d3SBarry Smith  problems is much faster than using print statements.
86*9b92b1d3SBarry Smith- **Never** hardwire a large problem size into your code. Instead,
87*9b92b1d3SBarry Smith  allow a command line option to run a small problem. We've seen people
88*9b92b1d3SBarry Smith  developing codes who have to wait 15 minutes after starting a run to
89*9b92b1d3SBarry Smith  reach the crashing point; this is an inefficient way of developing
90*9b92b1d3SBarry Smith  code.
91*9b92b1d3SBarry Smith- Develop your code on the simplest machine to which you have access.
92*9b92b1d3SBarry Smith  We have accounts on a variety of large parallel machines, but we
93*9b92b1d3SBarry Smith  write and initially test all our code on laptops or workstations
94*9b92b1d3SBarry Smith  because the user interface is friendlier, and the turn-around time
95*9b92b1d3SBarry Smith  for compiling and running is much faster than for the parallel
96*9b92b1d3SBarry Smith  machines. We use the parallel machines only for large jobs. Since
97*9b92b1d3SBarry Smith  PETSc code is completely portable, switching to a parallel machine
98*9b92b1d3SBarry Smith  from our laptop/workstation development environment simply means
99*9b92b1d3SBarry Smith  logging into another machine -- there are no code or makefile
100*9b92b1d3SBarry Smith  changes.
101*9b92b1d3SBarry Smith- Never develop code directly on a multi-node computing system; your
102*9b92b1d3SBarry Smith  productivity will be much lower than if you developed on a
103*9b92b1d3SBarry Smith  well-organized workstation.
104*9b92b1d3SBarry Smith- Keep your machines' operating systems and compilers up-to-date (or
105*9b92b1d3SBarry Smith  force your systems people to do this :-). You should always work with
106*9b92b1d3SBarry Smith  whatever tools are currently the best available. It may seem that you
107*9b92b1d3SBarry Smith  are saving time by not spending the time upgrading your system, but,
108*9b92b1d3SBarry Smith  in fact, your loss in efficiency by sticking with an outdated system
109*9b92b1d3SBarry Smith  is probably larger than then the time required to keep it up-to-date.
110