|
| 1 | +--- |
| 2 | +description: |
| 3 | +globs: |
| 4 | +alwaysApply: true |
| 5 | +--- |
| 6 | +--- |
| 7 | +description: Full MFC project rules – consolidated for Agent Mode |
| 8 | +alwaysApply: true |
| 9 | +--- |
| 10 | + |
| 11 | +# 0 Purpose & Scope |
| 12 | +Consolidated guidance for the MFC exascale, many-physics solver. |
| 13 | +Written primarily for Fortran/Fypp; the OpenACC and style sections matter only when |
| 14 | +`.fpp` / `.f90` files are in view. |
| 15 | + |
| 16 | +--- |
| 17 | + |
| 18 | +# 1 Global Project Context (always) |
| 19 | +- **Project**: *MFC* is modern Fortran 2008+ generated with **Fypp**. |
| 20 | + - Sources `src/`, tests `tests/`, examples `examples/`. |
| 21 | + - Most sources are `.fpp`; CMake transpiles them to `.f90`. |
| 22 | +- **Fypp macros** live in `src/<subprogram>/include/` you should scan these first. |
| 23 | + `<subprogram>` ∈ {`simulation`,`common`,`pre_process`,`post_process`}. |
| 24 | +- Only `simulation` (+ its `common` calls) is GPU-accelerated via **OpenACC**. |
| 25 | +- Assume free-form Fortran 2008+, `implicit none`, explicit `intent`, and modern |
| 26 | + intrinsics. |
| 27 | +- Prefer `module … contains … subroutine foo()`; avoid `COMMON` blocks and |
| 28 | + file-level `include` files. |
| 29 | +- **Read the full codebase and docs *before* changing code.** |
| 30 | + Docs: <https://mflowcode.github.io/documentation/md_readme.html> and the respository root `README.md`. |
| 31 | + |
| 32 | +### Incremental-change workflow |
| 33 | +1. Draft a step-by-step plan. |
| 34 | +2. After each step, build: |
| 35 | + ```bash |
| 36 | + ./mfc.sh build -t pre_process simulation -j $(nproc) |
| 37 | + ``` |
| 38 | +3. If it compiles, run focused tests: |
| 39 | + ```bash |
| 40 | + ./mfc.sh test -j $(nproc) -f EA8FA07E -t 9E2CA336 |
| 41 | + ``` |
| 42 | +4. Roll back & fix if a step fails. |
| 43 | + |
| 44 | +* Do not run ./mfc.sh test -j $(nproc) without any other arguments (it takes too long to run all tests). |
| 45 | + |
| 46 | +--- |
| 47 | + |
| 48 | +# 2 Style & Naming Conventions (for \*.fpp / \*.f90) |
| 49 | + |
| 50 | +* **Indent 2 spaces**; continuation lines align under `&`. |
| 51 | +* Lower-case keywords and intrinsics (`do`, `end subroutine`, …). |
| 52 | +* **Modules**: `m_<feature>` (e.g. `m_transport`). |
| 53 | +* **Public procedures**: |
| 54 | + * Subroutine → `s_<verb>_<noun>` (e.g. `s_compute_flux`) |
| 55 | + * Function → `f_<verb>_<noun>` |
| 56 | +* Private helpers stay in the module; avoid nested procedures. |
| 57 | +* **Size limits**: subroutine ≤ 500 lines, helper ≤ 150, function ≤ 100, |
| 58 | + module/file ≤ 1000. |
| 59 | +* ≤ 6 arguments per routine; otherwise pass a derived-type “params” struct. |
| 60 | +* No `goto` (except unavoidable legacy); no global state (`COMMON`, `save`). |
| 61 | +* Every variable: `intent(in|out|inout)` + appropriate `dimension` / `allocatable` |
| 62 | + / `pointer`. |
| 63 | +* Use `s_mpi_abort(<msg>)` for errors, not `stop`. |
| 64 | +* Mark OpenACC-callable helpers that are called from OpenACC parallel loops immediately after declaration: |
| 65 | + ```fortran |
| 66 | + subroutine s_flux_update(...) |
| 67 | + !$acc routine seq |
| 68 | + ... |
| 69 | + end subroutine |
| 70 | + ``` |
| 71 | + |
| 72 | +--- |
| 73 | + |
| 74 | +# 3 OpenACC Programming Guidelines (for kernels) |
| 75 | + |
| 76 | +Wrap tight loops with |
| 77 | + |
| 78 | +```fortran |
| 79 | +!$acc parallel loop gang vector default(present) reduction(...) |
| 80 | +``` |
| 81 | +* Add `collapse(n)` to merge nested loops when safe. |
| 82 | +* Declare loop-local variables with `private(...)`. |
| 83 | +* Allocate large arrays with `managed` or move them into a persistent |
| 84 | + `!$acc enter data` region at start-up. |
| 85 | +* **Do not** place `stop` / `error stop` inside device code. |
| 86 | +* Must compile with Cray `ftn` and NVIDIA `nvfortran` for GPU offloading; also build CPU-only with |
| 87 | + GNU `gfortran` and Intel `ifx`/`ifort`. |
0 commit comments