| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | module diag_counters | ||
| 2 | !> Lock-free per-thread event counters for hot-loop diagnostics. | ||
| 3 | !> | ||
| 4 | !> The symplectic integrators hit non-convergence and out-of-domain cases | ||
| 5 | !> deep inside their inner Newton iterations. Printing there floods stdout | ||
| 6 | !> and serializes threads. Instead each event bumps a counter; progress_monitor | ||
| 7 | !> aggregates and reports them. Every thread owns its own padded column, so an | ||
| 8 | !> increment touches only that thread's cache line and needs no atomic or | ||
| 9 | !> critical. Totals are a plain reduction over the thread columns, taken | ||
| 10 | !> outside the hot path. | ||
| 11 | use, intrinsic :: iso_fortran_env, only: int64 | ||
| 12 | !$ use omp_lib, only: omp_get_max_threads, omp_get_thread_num | ||
| 13 | implicit none | ||
| 14 | private | ||
| 15 | |||
| 16 | public :: EVT_NEWTON1_MAXIT, EVT_NEWTON2_MAXIT, EVT_RK_GAUSS_MAXIT, & | ||
| 17 | EVT_RK_LOBATTO_MAXIT, EVT_FIXPOINT_MAXIT, EVT_R_NEGATIVE, & | ||
| 18 | EVT_FO_LOSS, EVT_FO_FAULT, EVT_MIDPOINT_MAXIT, & | ||
| 19 | EVT_WARNING_STEP_SKIP, EVT_SPECTRE_REF_INVERSE_MAXIT, & | ||
| 20 | EVT_SPECTRE_INVALID_STATE, EVT_SYMPLECTIC_RK_RECOVERY, & | ||
| 21 | EVT_SYMPLECTIC_RESUME, EVT_WARNING_MAXIT_ACCEPT, & | ||
| 22 | EVT_WARNING_MAXIT_REJECT_100, EVT_WARNING_MAXIT_REJECT_1E4, & | ||
| 23 | EVT_WARNING_MAXIT_REJECT_LARGE, EVT_WARNING_MAXIT_NONFINITE, & | ||
| 24 | EVT_RETRY_EXHAUSTED, EVT_WARNING_AXIS_CROSSING_ACCEPT, & | ||
| 25 | EVT_TOROIDAL_REGULARIZATION, EVT_AXIS_FO_BRIDGE, N_EVENT | ||
| 26 | public :: diag_counters_init, count_event, diag_counters_total, & | ||
| 27 | diag_counters_reset, event_name | ||
| 28 | |||
| 29 | integer, parameter :: EVT_NEWTON1_MAXIT = 1 | ||
| 30 | integer, parameter :: EVT_NEWTON2_MAXIT = 2 | ||
| 31 | integer, parameter :: EVT_RK_GAUSS_MAXIT = 3 | ||
| 32 | integer, parameter :: EVT_RK_LOBATTO_MAXIT = 4 | ||
| 33 | integer, parameter :: EVT_FIXPOINT_MAXIT = 5 | ||
| 34 | integer, parameter :: EVT_R_NEGATIVE = 6 | ||
| 35 | ! Full-orbit (FO) outcomes, kept separate from physical edge loss so a numerical | ||
| 36 | ! locate fault is never silently counted as a lost particle. LOSS = guiding-centre | ||
| 37 | ! s >= 1 crossing (physical loss); FAULT = field-inversion non-convergence. | ||
| 38 | integer, parameter :: EVT_FO_LOSS = 7 | ||
| 39 | integer, parameter :: EVT_FO_FAULT = 8 | ||
| 40 | integer, parameter :: EVT_MIDPOINT_MAXIT = 9 | ||
| 41 | integer, parameter :: EVT_WARNING_STEP_SKIP = 10 | ||
| 42 | integer, parameter :: EVT_SPECTRE_REF_INVERSE_MAXIT = 11 | ||
| 43 | integer, parameter :: EVT_SPECTRE_INVALID_STATE = 12 | ||
| 44 | integer, parameter :: EVT_SYMPLECTIC_RK_RECOVERY = 13 | ||
| 45 | integer, parameter :: EVT_SYMPLECTIC_RESUME = 14 | ||
| 46 | integer, parameter :: EVT_WARNING_MAXIT_ACCEPT = 15 | ||
| 47 | integer, parameter :: EVT_WARNING_MAXIT_REJECT_100 = 16 | ||
| 48 | integer, parameter :: EVT_WARNING_MAXIT_REJECT_1E4 = 17 | ||
| 49 | integer, parameter :: EVT_WARNING_MAXIT_REJECT_LARGE = 18 | ||
| 50 | integer, parameter :: EVT_WARNING_MAXIT_NONFINITE = 19 | ||
| 51 | integer, parameter :: EVT_RETRY_EXHAUSTED = 20 | ||
| 52 | integer, parameter :: EVT_WARNING_AXIS_CROSSING_ACCEPT = 21 | ||
| 53 | integer, parameter :: EVT_TOROIDAL_REGULARIZATION = 22 | ||
| 54 | integer, parameter :: EVT_AXIS_FO_BRIDGE = 23 | ||
| 55 | integer, parameter :: N_EVENT = 23 | ||
| 56 | |||
| 57 | ! Whole cache lines per thread column keep neighbouring threads from sharing | ||
| 58 | ! a line. The event id indexes within a column; STRIDE >= N_EVENT. | ||
| 59 | integer, parameter :: STRIDE = 32 | ||
| 60 | integer(int64), allocatable :: counts(:, :) ! (STRIDE, 0:nthreads-1) | ||
| 61 | |||
| 62 | contains | ||
| 63 | |||
| 64 | 51 | subroutine diag_counters_init() | |
| 65 | integer :: nthreads | ||
| 66 | |||
| 67 | 51 | nthreads = 1 | |
| 68 | 51 | !$ nthreads = omp_get_max_threads() | |
| 69 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 51 times.
|
51 | if (allocated(counts)) deallocate (counts) |
| 70 |
7/14✗ Branch 0 not taken.
✓ Branch 1 taken 51 times.
✓ Branch 2 taken 51 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 51 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 51 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✓ Branch 9 taken 51 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 51 times.
✗ Branch 14 not taken.
✓ Branch 15 taken 51 times.
|
153 | allocate (counts(STRIDE, 0:nthreads - 1)) |
| 71 |
4/4✓ Branch 0 taken 67 times.
✓ Branch 1 taken 51 times.
✓ Branch 2 taken 2144 times.
✓ Branch 3 taken 67 times.
|
2262 | counts = 0_int64 |
| 72 | 51 | end subroutine diag_counters_init | |
| 73 | |||
| 74 | 30 | subroutine count_event(id) | |
| 75 | !> Record one occurrence of event `id`. Safe to call from inside a | ||
| 76 | !> parallel region: writes only the calling thread's column. | ||
| 77 | integer, intent(in) :: id | ||
| 78 | integer :: tid | ||
| 79 | |||
| 80 |
2/2✓ Branch 0 taken 8 times.
✓ Branch 1 taken 22 times.
|
30 | if (.not. allocated(counts)) return |
| 81 | 8 | tid = 0 | |
| 82 | 8 | !$ tid = omp_get_thread_num() | |
| 83 | 8 | counts(id, tid) = counts(id, tid) + 1_int64 | |
| 84 | end subroutine count_event | ||
| 85 | |||
| 86 | 1761 | function diag_counters_total(id) result(total) | |
| 87 | !> Sum of event `id` across all threads. Call outside the hot path. | ||
| 88 | integer, intent(in) :: id | ||
| 89 | integer(int64) :: total | ||
| 90 | |||
| 91 | 1761 | total = 0_int64 | |
| 92 |
3/4✓ Branch 0 taken 1761 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2129 times.
✓ Branch 3 taken 1761 times.
|
3890 | if (allocated(counts)) total = sum(counts(id, :)) |
| 93 | 1761 | end function diag_counters_total | |
| 94 | |||
| 95 | ✗ | subroutine diag_counters_reset() | |
| 96 | ✗ | if (allocated(counts)) counts = 0_int64 | |
| 97 | ✗ | end subroutine diag_counters_reset | |
| 98 | |||
| 99 | 23 | function event_name(id) result(name) | |
| 100 | integer, intent(in) :: id | ||
| 101 | character(:), allocatable :: name | ||
| 102 | |||
| 103 | 23 | select case (id) | |
| 104 | case (EVT_NEWTON1_MAXIT) | ||
| 105 | 1 | name = 'newton1_maxit' | |
| 106 | case (EVT_NEWTON2_MAXIT) | ||
| 107 | 1 | name = 'newton2_maxit' | |
| 108 | case (EVT_RK_GAUSS_MAXIT) | ||
| 109 | 1 | name = 'rk_gauss_maxit' | |
| 110 | case (EVT_RK_LOBATTO_MAXIT) | ||
| 111 | 1 | name = 'rk_lobatto_maxit' | |
| 112 | case (EVT_FIXPOINT_MAXIT) | ||
| 113 | 1 | name = 'fixpoint_maxit' | |
| 114 | case (EVT_R_NEGATIVE) | ||
| 115 | 1 | name = 'r_negative' | |
| 116 | case (EVT_FO_LOSS) | ||
| 117 | 1 | name = 'fo_loss' | |
| 118 | case (EVT_FO_FAULT) | ||
| 119 | 1 | name = 'fo_fault' | |
| 120 | case (EVT_MIDPOINT_MAXIT) | ||
| 121 | 1 | name = 'midpoint_maxit' | |
| 122 | case (EVT_WARNING_STEP_SKIP) | ||
| 123 | 1 | name = 'warning_step_skip' | |
| 124 | case (EVT_SPECTRE_REF_INVERSE_MAXIT) | ||
| 125 | 1 | name = 'spectre_ref_inverse_maxit' | |
| 126 | case (EVT_SPECTRE_INVALID_STATE) | ||
| 127 | 1 | name = 'spectre_invalid_state' | |
| 128 | case (EVT_SYMPLECTIC_RK_RECOVERY) | ||
| 129 | 1 | name = 'symplectic_rk_recovery' | |
| 130 | case (EVT_SYMPLECTIC_RESUME) | ||
| 131 | 1 | name = 'symplectic_resume' | |
| 132 | case (EVT_WARNING_MAXIT_ACCEPT) | ||
| 133 | 1 | name = 'warning_maxit_accept' | |
| 134 | case (EVT_WARNING_MAXIT_REJECT_100) | ||
| 135 | 1 | name = 'warning_maxit_reject_100' | |
| 136 | case (EVT_WARNING_MAXIT_REJECT_1E4) | ||
| 137 | 1 | name = 'warning_maxit_reject_1e4' | |
| 138 | case (EVT_WARNING_MAXIT_REJECT_LARGE) | ||
| 139 | 1 | name = 'warning_maxit_reject_large' | |
| 140 | case (EVT_WARNING_MAXIT_NONFINITE) | ||
| 141 | 1 | name = 'warning_maxit_nonfinite' | |
| 142 | case (EVT_RETRY_EXHAUSTED) | ||
| 143 | 1 | name = 'retry_exhausted' | |
| 144 | case (EVT_WARNING_AXIS_CROSSING_ACCEPT) | ||
| 145 | 1 | name = 'warning_axis_crossing_accept' | |
| 146 | case (EVT_TOROIDAL_REGULARIZATION) | ||
| 147 | 1 | name = 'toroidal_regularization' | |
| 148 | case (EVT_AXIS_FO_BRIDGE) | ||
| 149 | 1 | name = 'axis_fo_bridge' | |
| 150 | case default | ||
| 151 |
23/24✓ Branch 0 taken 1 time.
✓ Branch 1 taken 1 time.
✓ Branch 2 taken 1 time.
✓ Branch 3 taken 1 time.
✓ Branch 4 taken 1 time.
✓ Branch 5 taken 1 time.
✓ Branch 6 taken 1 time.
✓ Branch 7 taken 1 time.
✓ Branch 8 taken 1 time.
✓ Branch 9 taken 1 time.
✓ Branch 10 taken 1 time.
✓ Branch 11 taken 1 time.
✓ Branch 12 taken 1 time.
✓ Branch 13 taken 1 time.
✓ Branch 14 taken 1 time.
✓ Branch 15 taken 1 time.
✓ Branch 16 taken 1 time.
✓ Branch 17 taken 1 time.
✓ Branch 18 taken 1 time.
✓ Branch 19 taken 1 time.
✓ Branch 20 taken 1 time.
✓ Branch 21 taken 1 time.
✓ Branch 22 taken 1 time.
✗ Branch 23 not taken.
|
23 | name = 'unknown' |
| 152 | end select | ||
| 153 | 23 | end function event_name | |
| 154 | |||
| 155 | end module diag_counters | ||
| 156 |