Debugging HPC Applications Notes
Debugging HPC Applications
Tools
Debugging code is complex, aided by open-source and proprietary tools.
Tools allow stepping through code, placing breakpoints, and viewing memory.
Common open-source debugging tools are serial but adaptable for parallel debugging.
Key tools:
GNU Debugger (GDB): Breakpoints, watchpoints, catchpoints, backtrace, variable setting, thread debugging, cheat sheet.
Valgrind: Memory analysis tool.
Commercial Parallel Debuggers: Enhanced features for multi-threaded and distributed applications.
GNU Debugger (GDB)
Enables stepping through code, setting breakpoints, viewing/changing variables, and tracking variables.
Command-line debugger: requires commands for setting breakpoints, continuing execution, etc.
Compile code with debugging information: use the
-gflag.Inspects program execution, sets breakpoints, analyzes memory, and controls execution flow.
Breakpoints: Stop execution at specific points using
breakcommand (e.g.,break main.c:10).Watchpoints: Stop execution when a variable's value changes (e.g.,
watch variable_name).Catchpoints: Halt execution on specific events like system calls or exceptions (e.g.,
catch throw).Backtrace: Displays the call stack (
backtraceorbtcommand).Setting Variables: Modifies variable values during debugging using
set variable var_name = new_value(e.g.,set variable x = 10).Threads: Debugs multi-threaded apps;
info threadslists threads; switch withthread <thread-id>.GDB Cheat Sheet Commands
run: Starts the program.break <function/line>: Sets a breakpoint.continue: Resumes execution.next: Executes the next line.step: Steps into a function.print <variable>: Prints a variable's value.
Valgrind
Memory analysis tool: detects memory leaks, buffer overflows, and uninitialized memory reads.
Primarily used with memcheck.
Command:
valgrind --leak-check=full ./programImproves memory efficiency, assists in debugging memory-related errors.
Commercial Parallel Debuggers
Enhanced debugging for multi-threaded/distributed applications.
Examples:
TotalView: Supports MPI and OpenMP debugging.
DDT (Arm Forge): Advanced debugging for parallel HPC applications.
Intel Inspector: Debugs memory/threading issues on Intel architectures.
Features: graphical interfaces, automated bug detection, scalability.
GDB Sample Execution
Compile:
$gcc –g –o example example.cStart GDB:
gdb exampleSet breakpoint:
(gdb) break mainRun:
(gdb) runStep:
(gdb) nextor(gdb) stepPrint variable:
(gdb) print a,(gdb) print sumContinue:
(gdb) continueQuit:
(gdb) quit
Debugging on HPC
Requires detailed view of supercomputer software and hardware.
Invoked via command:
gdb <executable name>.-gflag during compilation is important.
Break Points (GDB)
Interrupts code execution to examine program state.
Set using function name, line number, file name, or condition.
Examples (using code from Fig. 14.2):
break printfbreak 17break dotprod_serial.c:17break dotprod_serial.c:16 if i==4
info breakpoints: Queries break point information.Disable:
disable <breakpoint_id>; enable:enable <breakpoint_id>; delete:delete <breakpoint_id>; temporary breakpoint:tbreak <location>.
Watch Points and Catch Points
Conditional breakpoints based on variable writes or events.
watch <expression>: Watches for changes to a variable.Example: To watch for changes to the value of the sum variable in Fig. 14.2, the command watch sum would be issued to the gdb command line once the variable sum was in the current context at line 20
info watchpoints: Command to get the information on watch points.
Back Trace
Shows the call stack when execution pauses.
Use
backtrace(orbt) command.Use
upanddownto traverse the call stack.
Setting a Variable (GDB)
Modify variable values during execution to test scenarios.
Use the
setcommand:set variable var_name = new_value(e.g.,set variable x = 10).
Threads (GDB)
For multithreaded applications (e.g., OpenMP).
info threads: Lists threads and IDs.thread <thread-id>: Switches between threads.
Valgrind
Tool suite for debugging, especially memory errors and thread data races.
Compile with
-gflag.Usage:
valgrind –tool=helgrind <program executable>(for data race detection).If no tool specified, runs Memcheck (memory error identification).
See table 14.2 for GDB command summaries.
Debugging Parallel Bugs
Parallel programs can have "parallel-only" bugs.
Race Conditions
Are a result from misuse of shared variables
Dead Locks
When tasks are perpetually waiting for a message or signal that never arrives.
Race Conditions
Occur due to misuse of shared variables without proper protection.
Mitigation:
Use
default(none)in OpenMP pragmas.Test code without
nowaitclauses.
Manifest as wrong and variable code results.
Debugging: focus on shared variables.
Deadlocks
Threads lock up waiting on resources that never become available.
Sign: program hangs.
Prevention:
Be careful with threadID conditional clauses.
Use
flushpragma for thread communication.Unset locks after setting them.
Tools for Parallel Debugging
Use proper debugging and profiling tools.
SHARCNET tools:
DDT: commercial debugger (MPI, OpenMP, CUDA).
VALGRIND: open-source memory debugger.
DDT (Distributed Debugging Tool)
Commercial debugger with GUI for parallel programs (MPI, OpenMP, CUDA).
Supports C, C++, Fortran.
Debugging OpenMP Example: Unprotected Shared Variable
Common error: accessing shared variable without protection.
Use Valgrind to identify data races.
Example:
Preventing race conditions is done through synchronization mechanisms such as:
Critical Section
Atomic Operations
Reduction Clause
Locks
Debugging MPI Example: Deadlock
Common error: competing requests block progress.
Debug using GDB for each process.
Two approaches:
Launch xterm window for each process (may not work on all clusters).
Modify code to print PIDs and pause execution.
Compiler Flags for Debugging
Compiler warnings assist in debugging.
Use command-line options to check for mistakes.
See table 14.5 for GNU, Intel, LLVM, and PGI options.
System Monitors to Aid Debugging
Clusters use monitoring software for node hardware status and workload information.
Monitors CPU utilization, memory usage, I/O, network traffic, disk space.
Lightweight daemons collect information regularly.
Common monitors: Nagios, Ganglia.
Types of Errors and Debugging Tools
Syntax Errors:
Compiler Error Messages
Static Analysis Tools
Runtime Errors:
GDB
Valgrind
AddressSanitizer
Core Dumps
Logical Errors:
Printf Debugging
GDB/LLDB
Unit Testing Frameworks
Memory Errors:
Valgrind
AddressSanitizer
Heaptrack
Concurrency Errors:
ThreadSanitizer
Intel Inspector
Helgrind
Distributed System Errors
TotalView
MPICHECK
DTrace/strace
Performance Errors:
Perf
Gprof
Vtune Profiler
Profiling Tools
Linux: Perf, PAPI, Perfctr, Gprof
Windows: Intel VTune Profiler
Measure CPU usage, memory allocation, function call frequencies, and more.
Identify bottlenecks and optimize code.
Find hotspots, measure cache efficiency, analyze branch mispredictions, and track I/O bottlenecks.
Perf Tool
Profiler tool for Linux.
Identifies hotspots, measures cache efficiency and analyzes branch mispredictions
Typical output Metrics: the number of CPU cycles, the number of instructions executed, cache hits and misses
Basic Commands:
perf stat: Provides a summary of performance counters while a command runs.perfrecord: Captures detailed performance data for later analysis.
Gnuplot
Graphing utility, command line driven.
Cross-platform functionality
Generate a wide range of plots
Controlled via Scripts
Basic Command Sequence:
$gnuplot <gnuplot> plot sin(x)