19b92b1d3SBarry Smith# Code Management 29b92b1d3SBarry Smith 39b92b1d3SBarry SmithWe list some of the techniques that may be used to increase one's 49b92b1d3SBarry Smithefficiency when developing PETSc application codes. We have learned to 59b92b1d3SBarry Smithuse these techniques ourselves, and they have improved our efficiency 69b92b1d3SBarry Smithtremendously. 79b92b1d3SBarry Smith 89b92b1d3SBarry Smith## Editing and Compiling 99b92b1d3SBarry Smith 109b92b1d3SBarry SmithThe biggest time sink in code development is generally the cycle of 119b92b1d3SBarry SmithEDIT-COMPILE-LINK-RUN. We often see users working in a single window 129b92b1d3SBarry Smithwith a cycle such as: 139b92b1d3SBarry Smith 149b92b1d3SBarry Smith- Edit a file with `emacs` or `vim`. 159b92b1d3SBarry Smith- Exit `emacs` or `vim`. 169b92b1d3SBarry Smith- Run `make` and see some error messages. 179b92b1d3SBarry Smith- Start `emacs` or `vim` and try to fix the errors; often starting 189b92b1d3SBarry Smith the editor hides the error messages so that users cannot remember all 199b92b1d3SBarry Smith of them and thus do not fix all the compiler errors. 209b92b1d3SBarry Smith- Run `make` generating a bunch of object (.o) files. 219b92b1d3SBarry Smith- Link the executable (which also removes the .o files). Users may 229b92b1d3SBarry Smith delete the .o files because they anticipate compiling the next time 239b92b1d3SBarry Smith on a different machine that uses a different compiler. 249b92b1d3SBarry Smith- Run the executable. 259b92b1d3SBarry Smith- Detect some error condition and restart the cycle. 269b92b1d3SBarry Smith 279b92b1d3SBarry SmithIn addition, during this process the user often moves manually among 289b92b1d3SBarry Smithdifferent directories for editing, compiling, and running. 299b92b1d3SBarry Smith 309b92b1d3SBarry Smith## Several easy ways to improve the cycle 319b92b1d3SBarry Smith 329b92b1d3SBarry Smith- `emacs` and `vim` have a feature to allow the user to compile 339b92b1d3SBarry Smith using make and have the editor automatically jump to the line of 349b92b1d3SBarry Smith source code where the compiler detects an error, even when not 359b92b1d3SBarry Smith currently editing the erroneous file. 369b92b1d3SBarry Smith 379b92b1d3SBarry Smith- The etags feature of `emacs` or tags feature of `vim` enables one 389b92b1d3SBarry Smith to search quickly through a group of user-defined source files 399b92b1d3SBarry Smith (and/or PETSc source files) regardless of the directory in which they 409b92b1d3SBarry Smith are located. [GNU Global](http://www.gnu.org/s/global) is a richer 419b92b1d3SBarry Smith alternative to etags. 429b92b1d3SBarry Smith 439b92b1d3SBarry Smith- Also, `emacs` and `vim` easily enable: 449b92b1d3SBarry Smith 459b92b1d3SBarry Smith - editing files that reside in any directory and retaining one's 469b92b1d3SBarry Smith place within each of them 479b92b1d3SBarry Smith - searching for files in any directory as one attempts to load them 489b92b1d3SBarry Smith into the editor 499b92b1d3SBarry Smith 509b92b1d3SBarry SmithYou might consider using `Microsoft Visual Studio`, `Eclipse` or 51*7f296bb3SBarry Smithother advanced software development systems. See the {ref}`Users Manual<sec_developer_environments>`. 529b92b1d3SBarry Smith 539b92b1d3SBarry Smith## Debugging 549b92b1d3SBarry Smith 559b92b1d3SBarry SmithMost code development for PETSc codes should be done on one processor; 569b92b1d3SBarry Smithhence, using a standard debugger such as dbx, gdb, xdbx, etc. is fine. 579b92b1d3SBarry SmithFor debugging parallel runs we suggest **Totalview** if it is available 589b92b1d3SBarry Smithon your machine. Otherwise, you can run each process in a separate 599b92b1d3SBarry Smithdebugger; this is not the same as using a parallel debugger, but in most 609b92b1d3SBarry Smithcases it is not so bad. The PETSc run-time options 619b92b1d3SBarry Smith`-start_in_debugger` [-display xdisplay:0] will open separate windows 629b92b1d3SBarry Smithand debuggers for each process. You should debug using the debugging 639b92b1d3SBarry Smithversions of the libraries (run ./configure with the additional option 649b92b1d3SBarry Smith--with-debugging (the default)). 659b92b1d3SBarry Smith 669b92b1d3SBarry SmithIt really pays to learn how to use a debugger; you will end up writing 679b92b1d3SBarry Smithmore interesting and far more ambitious codes once it is easy for you to 689b92b1d3SBarry Smithtrack down problems in the codes. 699b92b1d3SBarry Smith 709b92b1d3SBarry Smith## Other suggestions 719b92b1d3SBarry Smith 729b92b1d3SBarry Smith- Choose consistent and obvious names for variables and functions. 739b92b1d3SBarry Smith (Short variable names may be faster to type, but by using longer 749b92b1d3SBarry Smith names you don't have to remember what they represent since it is 759b92b1d3SBarry Smith clear from the name.) 769b92b1d3SBarry Smith- Use informative comments. 779b92b1d3SBarry Smith- Leave space in the code to make it readable. 789b92b1d3SBarry Smith- Line things up in the code for readability. Remember that any code 799b92b1d3SBarry Smith written for an application will be visited **over and over** again, 809b92b1d3SBarry Smith so spending an extra 20 percent of effort on it the first time will 819b92b1d3SBarry Smith make each of those visits faster and more efficient. 829b92b1d3SBarry Smith- Realize that you **will** have to debug your code. **No one** writes 839b92b1d3SBarry Smith perfect code, so always write code that may be debugged and learn how 849b92b1d3SBarry Smith to use a debugger. In most cases using the debugger to track down 859b92b1d3SBarry Smith problems is much faster than using print statements. 869b92b1d3SBarry Smith- **Never** hardwire a large problem size into your code. Instead, 879b92b1d3SBarry Smith allow a command line option to run a small problem. We've seen people 889b92b1d3SBarry Smith developing codes who have to wait 15 minutes after starting a run to 899b92b1d3SBarry Smith reach the crashing point; this is an inefficient way of developing 909b92b1d3SBarry Smith code. 919b92b1d3SBarry Smith- Develop your code on the simplest machine to which you have access. 929b92b1d3SBarry Smith We have accounts on a variety of large parallel machines, but we 939b92b1d3SBarry Smith write and initially test all our code on laptops or workstations 949b92b1d3SBarry Smith because the user interface is friendlier, and the turn-around time 959b92b1d3SBarry Smith for compiling and running is much faster than for the parallel 969b92b1d3SBarry Smith machines. We use the parallel machines only for large jobs. Since 979b92b1d3SBarry Smith PETSc code is completely portable, switching to a parallel machine 989b92b1d3SBarry Smith from our laptop/workstation development environment simply means 999b92b1d3SBarry Smith logging into another machine -- there are no code or makefile 1009b92b1d3SBarry Smith changes. 1019b92b1d3SBarry Smith- Never develop code directly on a multi-node computing system; your 1029b92b1d3SBarry Smith productivity will be much lower than if you developed on a 1039b92b1d3SBarry Smith well-organized workstation. 1049b92b1d3SBarry Smith- Keep your machines' operating systems and compilers up-to-date (or 1059b92b1d3SBarry Smith force your systems people to do this :-). You should always work with 1069b92b1d3SBarry Smith whatever tools are currently the best available. It may seem that you 1079b92b1d3SBarry Smith are saving time by not spending the time upgrading your system, but, 1089b92b1d3SBarry Smith in fact, your loss in efficiency by sticking with an outdated system 1099b92b1d3SBarry Smith is probably larger than then the time required to keep it up-to-date. 110