The Art of Debugging: How to think like a programmer. Melissa Sulprizio GEOS-Chem Support Team

Similar documents
Debugging with the new GEOS-Chem Unit Tester

Getting Started with GCHP v11-02c

Getting Started with High Performance GEOS-Chem

Debugging and Profiling

AMath 483/583 Lecture 2

AMath 483/583 Lecture 2. Notes: Notes: Homework #1. Class Virtual Machine. Notes: Outline:

Unit Testing, Difference Testing, Profiling, and Musings on Software Engineering

Performance Analysis of Parallel Scientific Applications In Eclipse

Improving Your Life With Git

Profiling with TAU. Le Yan. User Services LSU 2/15/2012

Workshop on High Performance Computing (HPC08) School of Physics, IPM February 16-21, 2008 HPC tools: an overview

Debugging with gdb and valgrind

Hands-on Workshop on How To Debug Codes at the Institute

Revision Control. How can 4. Slides #4 CMPT 276 Dr. B. Fraser. Local Topology Simplified. Git Basics. Revision Control:

CSC 2700: Scientific Computing

The Eclipse Parallel Tools Platform

Revision Control. An Introduction Using Git 1/15

Debugging with GDB and DDT

Updating the Compiler?

CSE 331 Software Design & Implementation

Language Translation. Compilation vs. interpretation. Compilation diagram. Step 1: compile. Step 2: run. compiler. Compiled program. program.

Developing Android applications in Windows

Computing and compilers

Compiling environment

Working in Teams CS 520 Theory and Practice of Software Engineering Fall 2018

AMath 483/583 Lecture 7. Notes: Notes: Changes in uwhpsc repository. AMath 483/583 Lecture 7. Notes:

What is version control? (discuss) Who has used version control? Favorite VCS? Uses of version control (read)

Version control CSE 403

Section 1: Tools. Contents CS162. January 19, Make More details about Make Git Commands to know... 3

Welcome. HRSK Practical on Debugging, Zellescher Weg 12 Willers-Bau A106 Tel

AMath 483/583 Lecture 7

Software Development I

Profiling with TAU. Le Yan. 6/6/2012 LONI Parallel Programming Workshop

Compiling environment

Compilation I. Hwansoo Han

Prof. Thomas Sterling

HPCC - Hrothgar. Getting Started User Guide TotalView. High Performance Computing Center Texas Tech University

DDT: A visual, parallel debugger on Ra

GETTING TO KNOW GIT: PART II JUSTIN ELLIOTT PENN STATE UNIVERSITY

Debugging with GDB and DDT

Review Version Control Concepts

From theory to practice: Standard tools Software carpentry, Part II. Pietro Berkes, Brandeis University

Programming in the Real World. Dr. Baldassano Yu s Elite Education

Introduction to Supercomputing

Introduction to debugging. Martin Čuma Center for High Performance Computing University of Utah

What is git? Distributed Version Control System (VCS); Created by Linus Torvalds, to help with Linux development;

Using Git For Development. Shantanu Pavgi, UAB IT Research Computing

Software Engineering Practices. PHY 688: Numerical Methods for (Astro)Physics

Branching and Merging

Follow us on Twitter for important news and Compiling Programs

Version Control System - Git. zswu

The Old World. Have you ever had to collaborate on a project by

Profiling and debugging. Carlos Rosales September 18 th 2009 Texas Advanced Computing Center The University of Texas at Austin

Coding Tools. (Lectures on High-performance Computing for Economists VI) Jesús Fernández-Villaverde 1 and Pablo Guerrón 2 March 25, 2018

Version Control. Second level Third level Fourth level Fifth level. - Software Development Project. January 17, 2018

DEBUGGING ON FERMI PREPARING A DEBUGGABLE APPLICATION GDB. GDB on front-end nodes

SGI Altix Getting Correct Code Reiner Vogelsang SGI GmbH

OpenMP Example. $ ssh # You might have to type yes if this is the first time you log on

Git and GitHub. Dan Wysocki. February 12, Dan Wysocki Git and GitHub February 12, / 48

Section 2: Developer tools and you. Alex Mariakakis (staff-wide)

Introduction to Performance Tuning & Optimization Tools

Developing Scientific Applications with the IBM Parallel Environment Developer Edition

Oracle Developer Studio 12.6

Laboratorio di Programmazione. Prof. Marco Bertini

Technology Background Development environment, Skeleton and Libraries

G E T T I N G S TA R T E D W I T H G I T

IBM C Rational Functional Tester for Java. Download Full Version :

Module Road Map. 7. Version Control with Subversion Introduction Terminology

b. Developing multiple versions of a software project in parallel

AccuRev Plugin for Crucible Installation and Release Notes

Version control. what is version control? setting up Git simple command-line usage Git basics

Important new structural developments in GEOS-Chem v11

CVS. Computer Science and Engineering College of Engineering The Ohio State University. Lecture 21

Using Intel VTune Amplifier XE and Inspector XE in.net environment

1. Which of these Git client commands creates a copy of the repository and a working directory in the client s workspace. (Choose one.

SECTION 1: CODE REASONING + VERSION CONTROL + ECLIPSE

CS354 gdb Tutorial Written by Chris Feilbach

Git better. Collaborative project management using Git and GitHub. Matteo Sostero March 13, Sant Anna School of Advanced Studies

Version control CSE 403

git commit --amend git rebase <base> git reflog git checkout -b Create and check out a new branch named <branch>. Drop the -b

21. This is a screenshot of the Android Studio Debugger. It shows the current thread and the object tree for a certain variable.

Performance Analysis and Debugging Tools

6 Git & Modularization

Get an Easy Performance Boost Even with Unthreaded Apps. with Intel Parallel Studio XE for Windows*

Dalhousie University CSCI 2132 Software Development Winter 2018 Lab 8, March 22

Tutorial: Analyzing MPI Applications. Intel Trace Analyzer and Collector Intel VTune Amplifier XE

[Software Development] Development Tools. Davide Balzarotti. Eurecom Sophia Antipolis, France

Technology Background Development environment, Skeleton and Libraries

Tau Introduction. Lars Koesterke (& Kent Milfeld, Sameer Shende) Cornell University Ithaca, NY. March 13, 2009

CIS 403: Lab 6: Profiling and Tuning

Software Project (Lecture 4): Git & Github

Lab 08. Command Line and Git

Outline The three W s Overview of gits structure Using git Final stuff. Git. A fast distributed revision control system

Software Revision Control for MASS. Git Installation / Configuration / Use

CSE 374 Programming Concepts & Tools

Git Tutorial. Version: 0.2. Anders Nilsson April 1, 2014

Debugging (Part 1) The material for this lecture is drawn, in part, from The Practice of Programming (Kernighan & Pike) Chapter 5

2/9/2013 LAB OUTLINE INTRODUCTION TO VCS WHY VERSION CONTROL SYSTEM(VCS)? II SENG 371 SOFTWARE EVOLUTION VERSION CONTROL SYSTEMS

Tools for the programming mid-semester projects

CS 520: VCS and Git. Intermediate Topics Ben Kushigian

Transcription:

The Art of Debugging: How to think like a programmer Melissa Sulprizio GEOS-Chem Support Team geos-chem-support@as.harvard.edu Graduate Student Forum 23 February 2017

GEOS-Chem Support Team Bob Yantosca (Harvard) Melissa Sulprizio (Harvard) Lizzie Lundgren (Harvard) Mike Long (Harvard) Colin Thackray (Harvard) Junwei Xu (Dalhousie) Yanko Davila (CU Boulder)

Overview 1) Good coding practice 2) Debugging tips 3) Validation tips 4) Profiling to optimize speed 5) Demos (if time)

Good Coding Practice

Good Coding Practice Use copious comments Comments will make sure that other users will be able to understand the modifications that you have added Writing comments before actually writing the code may help in your planning Well documented code is as important as properly working code Tips for adding comments Flag your updates with your initials and the date, e.g. (mps, 2/23/17) Separate sections of the code with one or more divider comments When citing papers, make sure to include the complete citation at the top of the routine and use shorthand to reference that paper within the code Limit line length to avoid wrapping the text on the page

Good Coding Practice Use a text editor with syntax highlighting Use ProTeX headers ProTeX headers are specially formatted comments at the top of each subroutine, function, and module that list arguments, modification history, and references Documentation is automatically generated when you compile with make doc Follow these tips for writing effective code Indent new blocks of code within loops and if statements The exception to this is nested loops (e.g. looping over lat, lon, lev) Use lots of white space to separate code Use all capitals for Fortran reserve words You may at your discretion mix capitals and lowercase in variable names Structure loops efficiently In Fortran loop over arrays using N, L, J, I order Loops in the wrong order can dramatically slow down your code

General Coding Tips Understand the problem and break it down into small steps Write pseudocode to focus on the logic without being distracted by syntax Syntax varies between languages, but the thought process is the same Look for relevant examples in preexisting code and modify Don t start from scratch if you can help it Use Google, Stack Overflow, fellow group members Compile/run your code often Comment out sections or routine calls to locate problematic code Use print statements to check variables, determine where code stops, etc. Fix bugs before writing new code Use version control Lets you track your files over time so you can revert to a previous working version if necessary

Git Version Control Git version control software is a distributed source code management system Offers many improvements over other source code management software (e.g. CVS, Subversion) Allows you to keep an identical copy of the source code repository on your own system Includes the option to create branches for different streams of development Lets you easily apply and merge code from several developers Contains easy-to-use graphical tools Git is already installed on AS and Odyssey Online services include Bitbucket, GitHub, GitLab, etc. GEOS-Chem repositories are stored on Bitbucket: https://bitbucket.org/gcst/

Git Graphical Tools gitk This command opens the GitK window where you can browse the complete revision history For each commit, you can see the changes made to each file

Git Graphical Tools git gui This command opens the Git GUI, a graphical user interface that can be used for most Git commands Create new branches of development Commit changes to the repository Merge branches back into the master branch

Git Graphical Tools git gui This command opens the Git GUI, a graphical user interface that can be used for most Git commands Create new branches of development Commit changes to the repository Merge branches back into the master branch We recommend using the Git GUI rather than typing commands at the Unix prompt. The Git GUI greatly simplifies things.

Debugging Tips

General Debugging Tips Here are some tips to isolate the source of errors: 1) Look at the log file(s) 2) Make sure you didn t max out the allotted time or memory 3) Check if someone else has already reported the bug 4) Rerun with debug flags and verbose options turned on 5) Identify whether the error happens consistently 6) Use a debugger to locate the source of the error 7) Isolate the problem to a particular area of the code 8) Revert to a clean copy of the code 9) Focus on areas of the code that you modified 10) Check for math errors (divide by zero, log of negative, etc.) 11) When in doubt, print it out!

GEOS-Chem Debugging Tips Check the GEOS-Chem log file and the HEMCO log file Turn on ND70 in input.geos to print debug output to the log file Set Verbose and Warnings to 3 in HEMCO_Config.rc Compile with debug options turned on: BOUNDS=yes turns on array out-of-bounds error checking TRACEBACK=yes returns a list of routines that were called when the error occurred (now on by default) FPE=yes checks for floating-point exceptions (div-by-zero, NaN, etc.) DEBUG=yes compiles GEOS-Chem for use in a debugger Isolate the error to a particular operation (e.g. transport, chemistry, wet deposition) by turning on one at a time If stuck, send the GCST an email including the log files Please report any bugs in the standard code to the GCST and post on the GEOS-Chem wiki for other users to see!

Fortran Debuggers Using a debugger can save you a lot of time and hassle Debuggers allow you to: Examine data when a program stops Navigate the stack when a program stops Set break points Common debuggers include TotalView, IDB, GDB, DBX For GEOS-Chem, add DEBUG=yes to the make command make -j4 mpbuild DEBUG=yes # With parallelization make -j4 spbuild DEBUG=yes # Without parallelization DEBUG=yes evokes -g for symbolic debug information and -O0 to switch off compiler optimization

TotalView TotalView is a GUI-based source code and memory debugging tool for parallel applications written in C, C++, and Fortran To use type totalview geos.mp

Validation Tips

GEOS-Chem Unit Tester The GEOS-Chem Unit Tester is a package that will compile and run several simulations with a standard set of debugging flags A unit test will run simulations for a given combination of: 1. Meteorology field 2. Horizontal grid 3. Simulation type The steps of a unit test are: 1. Single processor (sp) phase: Run with OpenMP parallelization turned OFF 2. Multi processor (mp) phase: Run with OpenMP parallelization turned ON 3. Compare output of the sp and mp phases. If the outputs differ, this indicated a parallelization error Strict debugging flags are used for all simulations to look for floating-point math exceptions, array-out-of-bounds errors, and creation of array temporaries

GEOS-Chem Unit Tester Download the GEOS-Chem Unit Tester using Git: git clone https://bitbucket.org/gcst/geos-chem-unittest Navigate to the perl subdirectory Modify the UnitTest.input file for your desired settings Start the GEOS-Chem Unit Tester using./gcunittest UnitTest.input Check output generated by the GEOS-Chem Unit Tester and saved to the logs/{version} directory {VERSION}.results.txt: A text file summarizing results Evaluate Unit Test results Green = unit test passed Yellow = requires further investigation (diagnostic output differs) Red = unit test failed

Difference Tests A difference test validates two versions of a model (Ref and Dev) You can only expect a difference test to pass if the Dev run should produce identical results to the Ref run A difference test will not pass if the Dev code contains scientific changes, but this may still be useful for validation Difference tests with GEOS-Chem are set up using the GEOS-Chem Unit Tester

GEOS-Chem Difference Tests Navigate to the perl subdirectory of the GEOS-Chem Unit Tester Modify the DiffTest.input file for your desired settings Create a difference test run directory using./gccreatedifftest DiffTest.input Navigate to the run directory created by gccreatedifftest

GEOS-Chem Difference Tests Navigate to the perl subdirectory of the GEOS-Chem Unit Tester Modify the DiffTest.input file for your desired settings Create a difference test run directory using./gccreatedifftest DiffTest.input Navigate to the run directory created by gccreatedifftest

GEOS-Chem Difference Tests Navigate to the perl subdirectory of the GEOS-Chem Unit Tester Modify the DiffTest.input file for your desired settings Create a difference test run directory using./gccreatedifftest DiffTest.input Navigate to the run directory created by gccreatedifftest Start a difference test using make -j4 difftest Check results of the difference test in logs/log.*.results Explore differences between Dev and Ref using summarizediff.sh: provides overview of differences in BPCH files locatediff.sh: provides detailed list of differences in BPCH files plots/plot_diffs.pro: generate difference and ratio maps

Profiling

Profiling If you think your code is taking too long to run, you can use profiling tools to generate a list of time spent in each routine Profilers can help identify the source of computational bottlenecks caused by poorly written or parallelized code

GNU profiler The GNU profiler (gprof) is a free, open source package For GEOS-Chem, add GPROF=yes to the make command GPROF=yes adds the following compiler switches: -p for Intel Fortran compiler (ifort) -pg for GNU Fortran compiler (gfortran) -pg for PGI Fortran compiler (pgfortran) Run the program as usual A binary file called gmon.out will be produced Generate readable output using gprof program gprof geos.mp gmon.out > GC_Profile.txt

GNU profiler

GNU profiler The GNU profiler (gprof) is a free, open source package For GEOS-Chem, add GPROF=yes to the make command GPROF=yes adds the following compiler switches: -p for Intel Fortran compiler (ifort) -pg for GNU Fortran compiler (gfortran) -pg for PGI Fortran compiler (pgfortran) Run the program as usual A binary file called gmon.out will be produced Generate readable output using gprof program gprof geos.mp gmon.out > GC_Profile.txt Use PLOT_GPROF routine in GAMAP v2-19 to create bar chart IDL> plot_gprof, 'GC_Profile.txt'

GNU profiler

TAU profiler TAU Performance System is the Tuning and Analysis Utilities supported by ParaTools, Inc. TAU is a profiling tool for performance analysis of parallel programs in Fortran, C, C++, Java, and Python Load TAU on Odyssey using: module load tau/2.24.1-fasrc01 # Options for TAU profiler export TAU_OPTIONS="-optRevert -optverbose..." NOTE: TAU on Odyssey is currently built for IFORT 15. Use the alias load_15 to load both IFORT 15 compiler and TAU. TAU uses a visualization tool called ParaProf to create graphical displays of the performance analysis results

TAU profiler For GEOS-Chem, add TAU_PROF=yes to the make command IMPORTANT: Compile on a single processor to allow TAU to properly instrument the code (i.e. don t pass -j4 to the make command) TAU_PROF=yes compiles with TAU using tau_f90.sh instead of ifort, gfortran, or pgfortran Run the program as usual One or more profile.* files will be produced, depending on the number of CPUs that you use Pack the profiling data into a single file using paraprof --pack Profile_Results.ppk Run ParaProf on the packed format (.ppk) file using paraprof Profile_Results.ppk

TAU Profiler: ParaProf ParaProf will open two windows:

TAU Profiler: ParaProf ParaProf will open two windows 1. The ParaProf Manager Window This window is used to manage profile data (e.g. upload/download profile data, edit metadata, launch visual displays, export data)

TAU Profiler: ParaProf ParaProf will open two windows 1. The ParaProf Manager Window This window is used to manage profile data (e.g. upload/download profile data, edit metadata, launch visual displays, export data) 2. The Main Data window This window shows statistics and each thread as a combined bar graph Each routine is represented by a different color Clicking on the label for a bar (e.g. thread 0 ) will generate a histogram

TAU Profiler: ParaProf

Resources GEOS-Chem wiki: http://wiki.geos-chem.org Quick links: GEOS-Chem basics GEOS-Chem coding style guide GEOS-Chem coding and debugging Using Git with GEOS-Chem Debugging with the GEOS-Chem Unit Tester Performing difference tests with GEOS-Chem Profiling GEOS-Chem with gprof Profiling GEOS-Chem with TAU TotalView quick start guide GNU profiler documentation TAU Performance System documentation