Repository navigation
Tutorial Fortran OpenMP
This hands-on tutorial demonstrates how to debug a hybrid MPI + OpenMP Fortran 90/2008 (.f90 / .f08) scientific code (tutorial/stencil_omp.f90) in GridLock. You will practice compiling with full DWARF debug symbols, navigating both MPI ranks and OpenMP worker threads across the Call Stack and Variables panels, stepping a single OpenMP thread cleanly with scheduler-locking, evaluating 1-based Fortran slice and Z-slice expressions in the Array & Field Plotter (Ctrl+Alt+G) and editor hover tooltips, and diagnosing OpenMP barrier or MPI collective stalls in MPI Diagnostics.
stencil_omp.f90 allocates two 4 x 6 double-precision (real(real64)) Fortran arrays, field and updated, on every MPI rank. Each element is initialized to an unmistakable coordinate-encoded value:
field(i, j) = real(rank * 1000 + j * 100 + i, real64)| Rank |
field(1:4, 1) (Col 1) |
field(1:4, 2) (Col 2) |
field(1:4, 3) (Col 3) |
|---|---|---|---|
| Rank 0 | 101, 102, 103, 104 |
201, 202, 203, 204 |
301, 302, 303, 304 |
| Rank 1 | 1101, 1102, 1103, 1104 |
1201, 1202, 1203, 1204 |
1301, 1302, 1303, 1304 |
Inside an !$omp parallel default(none) region, OpenMP threads compute a 5-point stencil over the interior elements updated(2:3, 2:5) and accumulate local_sum, followed by an MPI_Reduce to Rank 0.
- GDB >= 12 (for complete DWARF5 Fortran array descriptor and OpenMP thread frame support).
-
MPI Launcher:
mpirunormpiexec(OpenMPI, MPICH, or Intel MPI). -
Fortran Compiler Flags:
-
GNU Fortran (
gfortran/mpif90):mpif90 -g -O0 -fopenmp -Wall -Wextra -fcheck=bounds,do,mem stencil_omp.f90 -o stencil_omp
-
Intel Fortran (
ifx/mpiifx):mpiifx -g -O0 -qopenmp -warn all stencil_omp.f90 -o stencil_omp
-
GNU Fortran (
Important
Always build with -g -O0 when stepping through OpenMP regions so thread-private variables (tid, i, j) and array descriptors are not optimized into registers or folded away.
- Build the tutorial executable from the repository root:
-
Automatic Meson target (built automatically by
ninja -C builddirwhenevermpif90is onPATH, producingbuilddir/stencil_omp), or compile directly intotutorial/:
(For Intelmpif90 -g -O0 -fopenmp -Wall -Wextra -fcheck=bounds,do,mem tutorial/stencil_omp.f90 -o tutorial/stencil_omp
ifx:mpiifx -g -O0 -qopenmp -warn all tutorial/stencil_omp.f90 -o tutorial/stencil_omp.) -
Automatic Meson target (built automatically by
- Launch GridLock with
OMP_NUM_THREADS=4exported in your shell environment (so each MPI rank spawns 4 OpenMP worker threads):OMP_NUM_THREADS=4 gridlock
- Select
stencil_omp (Fortran MPI+OpenMP)from theTARGET:dropdown (or opentutorial/stencil_omp.f90via File -> Open Source File... and loadtutorial/stencil_omporbuilddir/stencil_ompvia TARGET: -> Open ELF / Binary...). - Set RANKS (-np): on the top toolbar to
2(giving2 MPI ranks x 4 OpenMP threads = 8concurrent workers).
To run on a Slurm cluster with 2 tasks and 4 CPUs per task:
- Enable Show "Submit Slurm Job..." in File menu in Edit -> Preferences -> HPC / Cluster (experimental).
- Open Edit -> Preferences -> HPC Integration, set Tasks/Node to
2, CPUs / Task to4, and exportOMP_NUM_THREADS=$SLURM_CPUS_PER_TASKinside the batch script template. - Use File -> Submit Slurm Job... to submit and attach to the allocated compute node(s).
- In
stencil_omp.f90, click the left gutter to place a breakpoint on line51(inside the!$omp doloop whereupdated(i, j)is assigned) and a second breakpoint on line60(call MPI_Reduce(...)). - Click
▶ Run Target(Alt+R). Both MPI ranks stop inside the parallel region. - Inspect the Server Rack (bottom-right) to switch between
RANK 0andRANK 1:- On
RANK 0,field(1, 1)is101.0. - On
RANK 1,field(1, 1)is1101.0.
- On
- Open the Threads and Call Stack tabs alongside Variables in the right-hand dock:
- You will see 4 active threads per rank: Thread 1 (the main thread) and 3 OpenMP worker threads running inside the compiler-outlined frame (for example,
stencil_omp._omp_fn.0undergfortran). - Click between threads in the Threads / Call Stack panel to switch active thread frames and inspect thread-local scalars (
tid,i,j) alongside shared arrays (field,updated) in the Variables panel. Notice that each worker thread holds a distincttid(0..3) and column indexj(2..5).
- You will see 4 active threads per rank: Thread 1 (the main thread) and 3 OpenMP worker threads running inside the compiler-outlined frame (for example,
When multiple OpenMP threads execute the same !$omp do loop concurrently, pressing Step Over (F10) allows sibling threads to hit the same breakpoint and steal debugger focus.
To step a single OpenMP worker cleanly line by line:
- Select the thread you want to trace in the Threads dock (for example, the thread where
tid == 1). - Open the GDB Console tab at the bottom of GridLock and lock the GDB thread scheduler to the active thread:
set scheduler-locking on - Press Step Over (
F10) several times to step throughi = 2andi = 3on that worker alone without other threads advancing. - To inspect
tidacross every OpenMP thread on the focused rank in one command, run:thread apply all print tid
- Before continuing to the implicit OpenMP barrier at
!$omp end doorMPI_Reduceon line60, unlock the scheduler so sibling threads can reach the barrier (otherwise the locked thread will wait forever at!$omp end parallel):set scheduler-locking off
Warning
Always run set scheduler-locking off (or set scheduler-locking step) before continuing past an !$omp barrier, !$omp end do, or !$omp end parallel directive so sibling OpenMP threads are allowed to join the barrier.
When GDB stops inside stencil_omp.f90, GridLock detects Fortran automatically (via GDB language metadata and .f90/.f08 extensions) and switches array inspection to Fortran (1-based, column-major) mode.
In the Variables / Watch dock, Evaluator tab, or by hovering over an array symbol directly in the .f90 / .f08 editor to open the live variable hover tooltip:
-
field(1:4, 2)-> displays Column 2 on Rank 0:(201, 202, 203, 204)(or(1201, 1202, 1203, 1204)on Rank 1). -
updated(2:3, 2:5)-> displays the2 x 4updated interior sub-matrix. - For general rectangular slices (
arr(1:10, 2:5)), GridLock preserves Fortran's inclusive 1-based bounds without applying C/C++ zero-based subscript conversion.
- Press
Ctrl+Alt+G(or choose Tools -> Array & Field Plotter...) while paused at line60(call MPI_Reduce(...)). - Confirm the footer badge displays
Fortran (1-based, column-major), the memory-order selector is set toFortran Col, and the offset spinbox lower bound is1. - Enter
fieldin Expr:, selectdouble, setNy = 4(rows) andNx = 6(columns), and click Plot / Refresh. - Switch to the Tensor Table view:
- Row and column headers are 1-based:
(1)..(4)and(1)..(6). - Cell
(1, 1)shows101at linear offset[0]. - Cell
(4, 1)shows104at linear offset[3]. - Cell
(1, 2)shows201at linear offset[4](nrows = 4), confirming contiguous column-major storage where consecutive rows occupy adjacent memory addresses.
- Row and column headers are 1-based:
- For 3D Fortran fields (
field3d(nx, ny, nz)), setNz > 1and use the Z-Slice spinbox (1..Nzin Fortran mode) to step through 2D planes along the slowest-varying third dimension without re-typing slice expressions. - Switch the Rank dropdown from
Rank 0toRank 1(or clickRANK 1in the Server Rack) before stepping overMPI_Reduce: cell(1, 2)updates to1201, letting you verify per-rank stencil output before the collective reduction combineslocal_sumintoglobal_sum.
- Right-click a breakpoint marker in the gutter of
stencil_omp.f90(or pressShift+Clickon the gutter) to open the Conditional Breakpoint popover. - Enter a native Fortran condition expression (for example,
j == 3 .and. tid == 1orrank == 1 .and. i == 2) and optionally restrict the Target Rank filter (for example,Rank 1) so only the selected MPI rank stops at that line. - Click Apply and resume (
F5). Only the matching rank and OpenMP thread satisfy the predicate, avoiding breakpoint noise across allranks x threadsworkers.
When a hybrid MPI + OpenMP job hangs (for example, one MPI rank stalls inside an !$omp barrier because a thread remained locked or took a divergent branch, while peer ranks block inside MPI_Reduce / MPI_Barrier):
- Click
Pause(F6/Alt+P) to halt all ranks simultaneously. - Open the MPI Diagnostics panel at the bottom of the workstation and inspect the Deadlock Analyzer table:
- Ranks waiting in
MPI_Reduce,MPI_Allreduce, orMPI_Barrierappear with their activeMPI PRIMITIVE,PEER / TARGET,COMMUNICATOR, and source location. - Any rank still stuck inside an OpenMP runtime barrier (
GOMP_barrier/__kmp_barrier/stencil_omp._omp_fn.0) stands out immediately against peer ranks already blocked inMPI_Reduce, revealing a collective divergence or asymmetric OpenMP stall.
- Ranks waiting in
- Double-click the divergent rank row in MPI Diagnostics to jump directly to its active frame in Call Stack, then inspect its OpenMP threads in the Threads tab to identify which worker failed to reach the barrier.
GridLock Repository | Releases | Report an Issue | Roadmap | Accessibility | License (MIT)
Documentation mirrored from docs/ on master.
Getting Started & Tutorials
Debugging Workflows
- UI & Debugging Manual
- Server Rack View
- MPI Diagnostics & HPC
- OpenMP OMPD Inspector
- Memory, RE & Profiling
- Struct Layout & Cache Lines
- Reference Manual & Packages
Customization & Reference
- Configuration & Themes
- Keyboard Shortcuts
- GDB Reference & Cheat Sheet
- Screenshot Gallery
- Engineering Roadmap
Extending & Architecture