diff --git a/CHANGELOG.md b/CHANGELOG.md index 22e2a6f5..13887388 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,14 @@ All notable changes to CableDyn are recorded in this file. The format follows ## [Unreleased] +### Added + +- The run-time profile (`CABLEDYN_PROFILE=1`, standalone driver and OpenFAST) splits the steps + of `EI = 0` lines into residual-only evaluations, residual and tangent evaluations, LU + factorisations and back-substitutions, with their counts and times, and separates the + factorisations of finite-EI cables from their back-substitutions (which now include the + force-blend correction that ends each step). Results do not change. + ## [0.2.0] - 2026-10-20 ### Added diff --git a/CMakeLists.txt b/CMakeLists.txt index fa0bb6dc..c8915904 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2047,7 +2047,7 @@ add_test( -DDRIVER=$ -DDECK=${CMAKE_CURRENT_SOURCE_DIR}/examples/dynamic_chain_waves.dat -DOUTPUT_ROOT=${CMAKE_CURRENT_BINARY_DIR}/driver_profile_ei0 - "-DEXPECT=\n EI = 0 line steps: [1-9][0-9]* \\(substeps and retries included\\), Newton iterations: [1-9]" + "-DEXPECT=\n EI = 0 line steps: [1-9][0-9]* \\(substeps and retries included\\), Newton iterations: [1-9][^\n]*\n EI = 0 line step kernels: residual [0-9]+ \\([0-9.]+ s\\), residual and tangent [1-9][0-9]* \\([0-9.]+ s\\), factorisation [1-9][0-9]* \\([0-9.]+ s\\), back-substitution [1-9][0-9]* \\([0-9.]+ s\\)\\." -P ${CMAKE_CURRENT_SOURCE_DIR}/tests/check_driver_profile.cmake) add_test( NAME driver_profile_finite_ei @@ -2055,7 +2055,7 @@ add_test( -DDRIVER=$ -DDECK=${CMAKE_CURRENT_SOURCE_DIR}/tests/data/held_start_gomex80_power_cable.dat -DOUTPUT_ROOT=${CMAKE_CURRENT_BINARY_DIR}/driver_profile_finite_ei - "-DEXPECT=\n Finite-EI cable steps: [1-9][0-9]*, residual evaluations: [1-9]" + "-DEXPECT=\n Finite-EI cable steps: [1-9][0-9]*, residual evaluations: [1-9][0-9]*, tangent assemblies: [1-9][0-9]*, factorisations: [1-9][0-9]*, linear solves: [1-9][0-9]*, force-blend corrections: [0-9]+[^\n]*\n Finite-EI cable step time \\(s\\): residual [0-9.]+, tangent [0-9.]+, effective matrix [0-9.]+, factorisation [0-9.]+, back-substitution [0-9.]+, added mass [0-9.]+, other [0-9.]+\\." -P ${CMAKE_CURRENT_SOURCE_DIR}/tests/check_driver_profile.cmake) add_test( NAME driver_profile_failure diff --git a/app/cabledyn.f90 b/app/cabledyn.f90 index 1f4bf251..ad8dedec 100644 --- a/app/cabledyn.f90 +++ b/app/cabledyn.f90 @@ -37,7 +37,9 @@ PROGRAM cabledyn USE CableDyn_Profile, ONLY: CD_Profile_Requested, CD_Profile_Arm, CD_Profile_Armed, CD_Profile_Enter, & CD_Profile_Flush, CD_Profile_Step, CD_Profile_Seconds, CD_Profile_Counts, & CD_PROF_INPUT, CD_PROF_INIT, CD_PROF_FLUID, CD_PROF_SOLVE, CD_PROF_CHECK, & - CD_PROF_OUTPUT, CD_PROF_PROGRESS, CD_PROF_FINAL, CD_PROF_NPHASE + CD_PROF_OUTPUT, CD_PROF_PROGRESS, CD_PROF_FINAL, CD_PROF_NPHASE, & + CD_Profile_Kernels, CD_PROF_K_RESIDUAL, CD_PROF_K_TANGENT, CD_PROF_K_FACTOR, & + CD_PROF_K_SOLVE, CD_PROF_NKERNEL USE CableDyn_HermiteCableDynamic, ONLY: CD_HermiteCable_Dyn_Reset_Profile, CD_HermiteCable_Dyn_Get_Profile, & CD_HermiteCable_Dyn_Recovery_Reset, CD_HermiteCable_Dyn_Recovery_Count USE, INTRINSIC :: ISO_FORTRAN_ENV, ONLY: error_unit, output_unit, int64 @@ -857,9 +859,10 @@ SUBROUTINE print_profile() !! of each phase of the run, the step counts and the Newton iterations, and the interior of !! the finite-EI cable steps. Printed once, on stdout, at the end of the run. LOGICAL, SAVE :: printed = .FALSE. - REAL(wp) :: seconds(CD_PROF_NPHASE), total, march, t_step, t_resid, t_eff, t_solve, t_am, t_tan - INTEGER(int64) :: march_steps, line_steps, line_iterations - INTEGER :: line_max_iterations, n_step, n_resid, n_solve, n_am, n_alloc, n_tan, k + REAL(wp) :: seconds(CD_PROF_NPHASE), total, march, t_step, t_resid, t_eff, t_solve, t_am, t_tan, t_factor + REAL(wp) :: kernel_seconds(CD_PROF_NKERNEL) + INTEGER(int64) :: march_steps, line_steps, line_iterations, kernel_calls(CD_PROF_NKERNEL) + INTEGER :: line_max_iterations, n_step, n_resid, n_solve, n_am, n_alloc, n_tan, n_factor, n_correct, k IF (printed .OR. .NOT. CD_Profile_Armed()) RETURN printed = .TRUE. ! the running phase up to now: the finalisation of a completed run, or the phase in which a @@ -887,16 +890,24 @@ SUBROUTINE print_profile() IF (line_steps > 0_int64) WRITE (output_unit, '(A,I0,A,I0,A,A,A,I0,A)') ' EI = 0 line steps: ', & line_steps, ' (substeps and retries included), Newton iterations: ', line_iterations, ' (', & fixed(REAL(line_iterations, wp)/REAL(line_steps, wp), 3), ' per line step, at most ', line_max_iterations, ').' + CALL CD_Profile_Kernels(kernel_calls, kernel_seconds) + IF (line_steps > 0_int64) WRITE (output_unit, '(A,I0,3A,I0,3A,I0,3A,I0,3A)') & + ' EI = 0 line step kernels: residual ', kernel_calls(CD_PROF_K_RESIDUAL), ' (', & + fixed(kernel_seconds(CD_PROF_K_RESIDUAL), 3), ' s), residual and tangent ', kernel_calls(CD_PROF_K_TANGENT), & + ' (', fixed(kernel_seconds(CD_PROF_K_TANGENT), 3), ' s), factorisation ', kernel_calls(CD_PROF_K_FACTOR), & + ' (', fixed(kernel_seconds(CD_PROF_K_FACTOR), 3), ' s), back-substitution ', kernel_calls(CD_PROF_K_SOLVE), & + ' (', fixed(kernel_seconds(CD_PROF_K_SOLVE), 3), ' s).' CALL CD_HermiteCable_Dyn_Get_Profile(n_step, n_resid, n_solve, n_am, n_alloc, t_step, t_resid, t_eff, & - t_solve, t_am, n_tan, t_tan) + t_solve, t_am, n_tan, t_tan, n_factor, t_factor, n_correct) IF (n_step > 0) THEN - WRITE (output_unit, '(A,I0,A,I0,A,I0,A,I0,A,I0,A)') ' Finite-EI cable steps: ', n_step, & - ', residual evaluations: ', n_resid, ', tangent assemblies: ', n_tan, ', linear solves: ', n_solve, & + WRITE (output_unit, '(A,I0,A,I0,A,I0,A,I0,A,I0,A,I0,A,I0,A)') ' Finite-EI cable steps: ', n_step, & + ', residual evaluations: ', n_resid, ', tangent assemblies: ', n_tan, ', factorisations: ', n_factor, & + ', linear solves: ', n_solve, ', force-blend corrections: ', n_correct, & ', intervals recovered by substeps: ', CD_HermiteCable_Dyn_Recovery_Count(), '.' - WRITE (output_unit, '(13A)') ' Finite-EI cable step time (s): residual ', fixed(t_resid, 3), & - ', tangent ', fixed(t_tan, 3), ', effective matrix ', fixed(t_eff, 3), ', linear solve ', & - fixed(t_solve, 3), ', added mass ', fixed(t_am, 3), ', other ', & - fixed(MAX(0.0_wp, t_step - (t_resid + t_tan + t_eff + t_solve + t_am)), 3), '.' + WRITE (output_unit, '(15A)') ' Finite-EI cable step time (s): residual ', fixed(t_resid, 3), & + ', tangent ', fixed(t_tan, 3), ', effective matrix ', fixed(t_eff, 3), ', factorisation ', & + fixed(t_factor, 3), ', back-substitution ', fixed(t_solve, 3), ', added mass ', fixed(t_am, 3), ', other ', & + fixed(MAX(0.0_wp, t_step - (t_resid + t_tan + t_eff + t_factor + t_solve + t_am)), 3), '.' ELSE IF (.NOT. profile_cable_detail .AND. n_finite_ei > 0) THEN WRITE (output_unit, '(A)') ' Finite-EI cable steps: not counted on the mixed EI = 0 + finite-EI route, '// & 'whose cables step concurrently.' diff --git a/doc/cli.rst b/doc/cli.rst index d9f0ec02..27a5db31 100644 --- a/doc/cli.rst +++ b/doc/cli.rst @@ -157,20 +157,21 @@ breakdown such as: .. code-block:: text Run-time profile (CABLEDYN_PROFILE), wall-clock time: - input parsing 0.002 s 0.0 % - initialisation and statics 0.233 s 3.1 % - dynamic march 7.232 s 96.8 % - fluid sampled by the driver 0.050 s 0.7 % - line, cable and object steps 7.168 s 96.0 % - checks and line failures 0.001 s 0.0 % - outputs 0.013 s 0.2 % + input parsing 0.002 s 0.1 % + initialisation and statics 0.237 s 8.8 % + dynamic march 2.444 s 91.1 % + fluid sampled by the driver 0.050 s 1.9 % + line, cable and object steps 2.379 s 88.6 % + checks and line failures 0.002 s 0.1 % + outputs 0.012 s 0.5 % progress and bookkeeping 0.000 s 0.0 % finalisation 0.000 s 0.0 % - total 7.467 s 100.0 % - March: 600 committed step(s), 12.0528 ms per step. - EI = 0 line steps: 3621 (substeps and retries included), Newton iterations: 4661 (1.287 per line step, at most 2). - Finite-EI cable steps: 4849, residual evaluations: 21255, tangent assemblies: 10391, linear solves: 10391, intervals recovered by substeps: 0. - Finite-EI cable step time (s): residual 1.416, tangent 2.674, effective matrix 0.136, linear solve 0.943, added mass 0.167, other 0.457. + total 2.684 s 100.0 % + March: 600 committed step(s), 4.0735 ms per step. + EI = 0 line steps: 3621 (substeps and retries included), Newton iterations: 4739 (1.309 per line step, at most 2). + EI = 0 line step kernels: residual 6947 (0.039 s), residual and tangent 3322 (0.064 s), factorisation 3322 (0.013 s), back-substitution 5126 (0.009 s). + Finite-EI cable steps: 1423, residual evaluations: 6140, tangent assemblies: 2914, factorisations: 2914, linear solves: 2914, force-blend corrections: 1423, intervals recovered by substeps: 0. + Finite-EI cable step time (s): residual 0.412, tangent 0.883, effective matrix 0.039, factorisation 0.211, back-substitution 0.075, added mass 0.049, other 0.085. The phases add up to the total, which runs from the start of the deck parse to the report. If the run stops with an error, the time up to the stop counts to the phase in which it stopped, and @@ -193,9 +194,18 @@ The counts follow the march: * **EI = 0 line steps** counts every step of every tension-only line, including recovery substeps, retries and, on the body marches, each re-step of a line within the body iterations. **Newton iterations** is their total. +* **EI = 0 line step kernels** splits the work inside those steps: the residual-only + evaluations (predictor tests and line-search trials), the evaluations that also assemble the + effective tangent, the LU factorisations of that tangent, and the back-substitutions with it + (Newton updates, a reused factor and the end tangent of a body junction). Each count is + followed by its time, summed over the lines: it can exceed the wall time when lines step + concurrently, so set ``OMP_NUM_THREADS=1`` to compare it with the phase rows. * **Finite-EI cable steps** counts the same for the cubic-Hermite cables. A cable clamped to a body is re-stepped in every body iteration, so the count can exceed the number of march - steps. + steps. Its step time is split into the residual and tangent assemblies, the effective + matrix, the factorisations, the back-substitutions (of the Newton solves and of the + force-blend correction that ends each step), the added mass and the rest; the rarely used + torsion and consistent-acceleration solves count to the rest. An unset or empty variable, or ``0``, leaves the profile off. The driver reads the variable once, when it starts. diff --git a/doc/faq.rst b/doc/faq.rst index 226b305f..33ad71c7 100644 --- a/doc/faq.rst +++ b/doc/faq.rst @@ -104,8 +104,10 @@ OpenFAST coupling benchmark does **not** imply a fast coupled run: OpenFAST may call ``CalcOutput`` several times per physical step with different trial kinematics. Set the environment variable ``CABLEDYN_PROFILE=1`` before running ``openfast.exe`` to separate CableDyn solve, SeaState - sampling, output-probe, and native-I/O time; the standalone driver reads the same variable - and prints its own breakdown (:ref:`cli-profile`). Keep ``ModCoupling`` fixed while + sampling, output-probe, and native-I/O time, and to split the steps of ``EI = 0`` lines and + finite-EI cables into residual, tangent, factorisation and back-substitution time (with + ``OMP_NUM_THREADS=1`` the line times add up to the solve time); the standalone driver reads + the same variable and prints its own breakdown (:ref:`cli-profile`). Keep ``ModCoupling`` fixed while comparing configurations. Do not switch to loose coupling merely to meet a wall-clock target; on the IEA-15MW reference model it materially changed platform and tension responses. Test ``OMP_NUM_THREADS=1`` as well as the intended threaded setting: three short lines can be too diff --git a/src/CableDyn_Dynamic.f90 b/src/CableDyn_Dynamic.f90 index c5cb4fb6..20c2ca98 100644 --- a/src/CableDyn_Dynamic.f90 +++ b/src/CableDyn_Dynamic.f90 @@ -37,6 +37,9 @@ MODULE CableDyn_Dynamic CD_Cable_Free_Bandwidth USE CableDyn_Linalg, ONLY: CD_Factor_Banded, CD_Solve_Banded, CD_Solve_Dense_As_Banded, & CD_Solve_Factored_Banded + USE CableDyn_Profile, ONLY: CD_Profile_Kernel_Begin, CD_Profile_Kernel_End, CD_PROF_K_RESIDUAL, & + CD_PROF_K_TANGENT, CD_PROF_K_FACTOR, CD_PROF_K_SOLVE + USE, INTRINSIC :: ISO_FORTRAN_ENV, ONLY: int64 IMPLICIT NONE PRIVATE @@ -755,6 +758,8 @@ SUBROUTINE CD_Cable_Gen_Alpha_Step(q, v, a, elem_conn, l0, ea, rho_a, tension_on LOGICAL :: accepted, has_load_proc, has_load_band_proc, has_added_mass_proc, has_added_mass_band_proc LOGICAL :: has_load_force_proc LOGICAL :: has_prescribed, direct_banded, have_eff, tangent_fresh, use_element_mass_band + ! clock at the start of a profiled kernel (CABLEDYN_PROFILE; zero when not profiling) + INTEGER(int64) :: prof_k0 LOGICAL :: factored_now, guess_used, prev_end_ok, reuse_factor, reused, warm_call, r_at_qk LOGICAL :: scales_stale, trial_is_iterate INTEGER :: es @@ -1239,8 +1244,10 @@ SUBROUTINE CD_Cable_Gen_Alpha_Step(q, v, a, elem_conn, l0, ea, rho_a, tension_on END DO ! eval_dynamic has just checked this band finite when a banded load or added-mass ! callback contributes to it; the factorization then skips repeating that scan. + CALL CD_Profile_Kernel_Begin(prof_k0) CALL CD_Factor_Banded(eff_band, kl, ku, ipiv, es, em, & matrix_validated=has_load_band_proc .OR. has_added_mass_band_proc) + CALL CD_Profile_Kernel_End(CD_PROF_K_FACTOR, prof_k0) factored_now = es == 0 work%step_factor = factored_now .AND. work%end_request IF (es /= 0) THEN @@ -1256,10 +1263,15 @@ SUBROUTINE CD_Cable_Gen_Alpha_Step(q, v, a, elem_conn, l0, ea, rho_a, tension_on IF (direct_banded) THEN ! A tangent factored in this iteration is the finite LU CD_Factor_Banded returned; a ! reused one keeps the full check. + CALL CD_Profile_Kernel_Begin(prof_k0) CALL CD_Solve_Factored_Banded(eff_band, kl, ku, ipiv, dq, es, em, factor_validated=tangent_fresh) + CALL CD_Profile_Kernel_End(CD_PROF_K_SOLVE, prof_k0) ELSE eff_free = eff(free, free) + ! the dense path factors and solves in one call: profiled as a factorisation + CALL CD_Profile_Kernel_Begin(prof_k0) CALL CD_Solve_Dense_As_Banded(eff_free, dq, es, em) + CALL CD_Profile_Kernel_End(CD_PROF_K_FACTOR, prof_k0) END IF IF (es /= 0 .OR. .NOT. CD_All_Finite(dq)) THEN ErrStat = CD_DYN_SINGULAR @@ -1497,7 +1509,9 @@ SUBROUTINE end_junction_data() eff_band_unfactored(i, j) = eff_band(i, j) END DO END DO + CALL CD_Profile_Kernel_Begin(prof_k0) CALL CD_Factor_Banded(eff_band, kl, ku, ipiv, es2, em2) + CALL CD_Profile_Kernel_End(CD_PROF_K_FACTOR, prof_k0) END IF work%step_factor = es2 == 0 IF (es2 /= 0) THEN @@ -1539,7 +1553,9 @@ SUBROUTINE end_junction_data() DO i = 1, nfx work%end_k(i, j) = (R_trial(fixed(i)) - R(fixed(i)))/h END DO + CALL CD_Profile_Kernel_Begin(prof_k0) CALL CD_Solve_Factored_Banded(eff_band, kl, ku, ipiv, dq, es2, em2) + CALL CD_Profile_Kernel_End(CD_PROF_K_SOLVE, prof_k0) IF (es2 /= 0 .OR. .NOT. CD_All_Finite(dq)) THEN ErrStat = CD_DYN_SINGULAR ErrMsg = 'CableDyn_Dynamic: junction end tangent back-solve failed: '//TRIM(em2) @@ -1912,6 +1928,25 @@ SUBROUTINE form_trial(stp) END SUBROUTINE form_trial SUBROUTINE eval_dynamic(q_eval, Rout, es_e, em_e, effout, effbandout) + !! eval_dynamic_core, timed by the run-time profile (CABLEDYN_PROFILE) as a residual-only + !! evaluation or, with an effective tangent requested, as a residual and tangent one. + REAL(wp), INTENT(IN) :: q_eval(:) + REAL(wp), INTENT(OUT) :: Rout(:) + INTEGER, INTENT(OUT) :: es_e + CHARACTER(*), INTENT(OUT) :: em_e + REAL(wp), INTENT(OUT), OPTIONAL :: effout(:, :) + REAL(wp), INTENT(OUT), OPTIONAL :: effbandout(:, :) + INTEGER(int64) :: clock0 + CALL CD_Profile_Kernel_Begin(clock0) + CALL eval_dynamic_core(q_eval, Rout, es_e, em_e, effout, effbandout) + IF (PRESENT(effout) .OR. PRESENT(effbandout)) THEN + CALL CD_Profile_Kernel_End(CD_PROF_K_TANGENT, clock0) + ELSE + CALL CD_Profile_Kernel_End(CD_PROF_K_RESIDUAL, clock0) + END IF + END SUBROUTINE eval_dynamic + + SUBROUTINE eval_dynamic_core(q_eval, Rout, es_e, em_e, effout, effbandout) !! R = M a_alpha + (1-af) r(q_eval, v_eval) + af r(q_n, v_n) with the force blend, else !! M a_alpha + f_int(q_alpha) - f_ext - load(q_alpha, v_alpha) (f_ext constant). When !! the OPTIONAL effout is present it also returns the effective tangent @@ -2150,7 +2185,7 @@ SUBROUTINE eval_dynamic(q_eval, Rout, es_e, em_e, effout, effbandout) CALL apply_release_cap(q_eval, Rout) END IF END IF - END SUBROUTINE eval_dynamic + END SUBROUTINE eval_dynamic_core SUBROUTINE structural_inertia(Rout) !! Rout = M a_alpha with the element consistent mass (of mass_l0 when present, the lengths diff --git a/src/CableDyn_HermiteCableDynamic.f90 b/src/CableDyn_HermiteCableDynamic.f90 index 87878b61..d92621ad 100644 --- a/src/CableDyn_HermiteCableDynamic.f90 +++ b/src/CableDyn_HermiteCableDynamic.f90 @@ -169,6 +169,8 @@ MODULE CableDyn_HermiteCableDynamic INTEGER, SAVE :: prof_n_resid = 0 ! eval_res calls (predictor + accepted iterates + line-search trials) INTEGER, SAVE :: prof_n_tan = 0 ! tangent assemblies (once per continuing solve iteration) INTEGER, SAVE :: prof_n_solve = 0 ! effective-tangent linear solves (= Newton iterations) + INTEGER, SAVE :: prof_n_factor = 0 ! LU factorisations of the effective tangent + INTEGER, SAVE :: prof_n_correct = 0 ! force-blend corrections after convergence (one back-substitution) INTEGER, SAVE :: prof_n_am = 0 ! frozen added-mass assemblies (once per step when enabled) ! Arrays heap-allocated on the step path: the plain Step allocates none; the recovery ! rewind of CD_HermiteCable_Dyn_Step_Recovering allocates its interval buffers. @@ -177,7 +179,8 @@ MODULE CableDyn_HermiteCableDynamic REAL(wp), SAVE :: prof_t_resid = 0.0_wp ! eval_res wall: element/drag/FK/contact assembly + inertia band matvec REAL(wp), SAVE :: prof_t_tan = 0.0_wp ! tangent-assembly wall (element Hessian + drag/FK Jacobians + band scatters) REAL(wp), SAVE :: prof_t_eff = 0.0_wp ! banded Keff triad + in-band Dirichlet wall - REAL(wp), SAVE :: prof_t_solve = 0.0_wp ! banded linear-solve wall + REAL(wp), SAVE :: prof_t_solve = 0.0_wp ! back-substitution wall (Newton solves and force-blend corrections) + REAL(wp), SAVE :: prof_t_factor = 0.0_wp ! LU factorisation wall of the effective tangent REAL(wp), SAVE :: prof_t_am = 0.0_wp ! frozen added-mass assembly wall (band) ! Number of coupling intervals completed by internal generalised-alpha substeps after the nominal @@ -499,9 +502,9 @@ SUBROUTINE CD_HermiteCable_Dyn_Reset_Profile() !! the SYSTEM_CLOCK overhead in the step/residual hot path. prof_enabled = .TRUE. prof_n_step = 0; prof_n_resid = 0; prof_n_solve = 0; prof_n_am = 0; prof_n_alloc = 0 - prof_n_tan = 0 + prof_n_tan = 0; prof_n_factor = 0; prof_n_correct = 0 prof_t_step = 0.0_wp; prof_t_resid = 0.0_wp; prof_t_eff = 0.0_wp - prof_t_tan = 0.0_wp + prof_t_tan = 0.0_wp; prof_t_factor = 0.0_wp prof_t_solve = 0.0_wp; prof_t_am = 0.0_wp END SUBROUTINE CD_HermiteCable_Dyn_Reset_Profile @@ -513,23 +516,31 @@ SUBROUTINE CD_HermiteCable_Dyn_Disable_Profile() END SUBROUTINE CD_HermiteCable_Dyn_Disable_Profile SUBROUTINE CD_HermiteCable_Dyn_Get_Profile(n_step, n_resid, n_solve, n_am, n_alloc, & - t_step, t_resid, t_eff, t_solve, t_am, n_tan, t_tan) + t_step, t_resid, t_eff, t_solve, t_am, n_tan, t_tan, & + n_factor, t_factor, n_correct) !! Read the diagnostic step-profile counters accumulated since the last reset. - !! t_step is the total wall inside CD_HermiteCable_Dyn_Step; t_resid + t_eff + - !! t_solve + t_am partition its instrumented interior (the remainder is - !! predictor/commit bookkeeping and workspace allocation). + !! t_step is the total wall inside CD_HermiteCable_Dyn_Step; t_resid + t_tan + t_eff + + !! t_factor + t_solve + t_am partition its instrumented interior (the remainder is + !! predictor/commit bookkeeping and workspace allocation). t_solve is the back-substitution + !! time of the n_solve Newton solves and the n_correct force-blend corrections; t_factor the + !! time of the n_factor LU factorisations. INTEGER, INTENT(OUT) :: n_step, n_resid, n_solve, n_am, n_alloc REAL(wp), INTENT(OUT) :: t_step, t_resid, t_eff, t_solve, t_am ! n_tan/t_tan report the tangent assemblies separately from the residual evaluations, ! so the eval_res averages stay honest (OPTIONAL so callers may omit them). INTEGER, INTENT(OUT), OPTIONAL :: n_tan REAL(wp), INTENT(OUT), OPTIONAL :: t_tan + INTEGER, INTENT(OUT), OPTIONAL :: n_factor, n_correct + REAL(wp), INTENT(OUT), OPTIONAL :: t_factor n_step = prof_n_step; n_resid = prof_n_resid; n_solve = prof_n_solve n_am = prof_n_am; n_alloc = prof_n_alloc t_step = prof_t_step; t_resid = prof_t_resid; t_eff = prof_t_eff t_solve = prof_t_solve; t_am = prof_t_am IF (PRESENT(n_tan)) n_tan = prof_n_tan IF (PRESENT(t_tan)) t_tan = prof_t_tan + IF (PRESENT(n_factor)) n_factor = prof_n_factor + IF (PRESENT(t_factor)) t_factor = prof_t_factor + IF (PRESENT(n_correct)) n_correct = prof_n_correct END SUBROUTINE CD_HermiteCable_Dyn_Get_Profile SUBROUTINE CD_HermiteCable_Dyn_Init(model, l0, EA, EI, rho_a, w, seed, fixed_dofs, & @@ -3502,7 +3513,8 @@ SUBROUTINE dyn_step_core(model, dt, max_iter, tol, ErrStat, ErrMsg, iters_out, r CALL CD_Factor_Banded(model%ws_Keffb, KL_D, KU_D, model%ws_ipiv, es, em2, matrix_validated=.TRUE.) IF (prof_enabled) THEN CALL SYSTEM_CLOCK(pc1) - prof_t_solve = prof_t_solve + REAL(pc1 - pc0, wp)/REAL(pcr, wp) + prof_n_factor = prof_n_factor + 1 + prof_t_factor = prof_t_factor + REAL(pc1 - pc0, wp)/REAL(pcr, wp) END IF IF (es /= CD_LINALG_OK) THEN ErrStat = CD_HCDYN_NOCONVERGE; ErrMsg = 'CD_HermiteCable_Dyn_Step: tangent factor: '//TRIM(em2) @@ -3733,8 +3745,14 @@ SUBROUTINE dyn_step_core(model, dt, max_iter, tol, ErrStat, ErrMsg, iters_out, r DO i = 1, ndof IF (.NOT. model%solve_mask(i)) model%ws_dq(i) = CD_ZERO END DO + IF (prof_enabled) CALL SYSTEM_CLOCK(pc0) CALL CD_Solve_Factored_Banded(model%ws_Keffb, KL_D, KU_D, model%ws_ipiv, model%ws_dq, es, em2, & factor_validated=.TRUE.) + IF (prof_enabled) THEN + CALL SYSTEM_CLOCK(pc1) + prof_n_correct = prof_n_correct + 1 + prof_t_solve = prof_t_solve + REAL(pc1 - pc0, wp)/REAL(pcr, wp) + END IF IF (es == CD_LINALG_OK) THEN IF (model%torsion%active) CALL torsion_sm_correct() CALL transform_rigid_vector_to_global(model, step_rigid_basis, model%ws_dq) diff --git a/src/CableDyn_Profile.f90 b/src/CableDyn_Profile.f90 index c40fcb92..d0fe6db8 100644 --- a/src/CableDyn_Profile.f90 +++ b/src/CableDyn_Profile.f90 @@ -11,17 +11,23 @@ MODULE CableDyn_Profile !! a run that stops with an error has its time up to the stop in the phase it stopped in. !! Each time march enters its fluid update, its step, its checks, its output and its !! progress report in turn; CD_Profile_Step counts the committed steps. CD_Step_Model counts - !! the EI = 0 line steps and their Newton iterations. Unarmed, every entry point returns after - !! one test, so a run that does not ask for the profile, and a host that loads the library, - !! are not affected. The state is process-global and serves one run: the phase marks are - !! taken on the driver's thread, and the line-step counters are updated atomically because - !! lines step concurrently. + !! the EI = 0 line steps and their Newton iterations, and the step of an EI = 0 line times its + !! kernels between CD_Profile_Kernel_Begin and CD_Profile_Kernel_End: the residual-only + !! evaluations, the evaluations that also assemble the effective tangent, the factorisations of + !! that tangent and the back-substitutions with it (CD_Profile_Kernels reads them; their times + !! are summed over the lines, so they exceed the wall time when lines step concurrently). + !! Unarmed, every entry point returns after one test, so a run that does not ask for the + !! profile, and a host that loads the library, are not affected. The state is process-global + !! and serves one run: the phase marks are taken on the driver's thread, and the line-step and + !! kernel counters are updated atomically because lines step concurrently. The OpenFAST module + !! arms it with its own profiler and disarms it (CD_Profile_Disarm) when the host does not ask. USE, INTRINSIC :: ISO_FORTRAN_ENV, ONLY: int64 USE CableDyn_Precision, ONLY: wp IMPLICIT NONE PRIVATE - PUBLIC :: CD_Profile_Requested, CD_Profile_Arm, CD_Profile_Armed, CD_Profile_Enter, CD_Profile_Flush, & - CD_Profile_Step, CD_Profile_Count_Line_Step, CD_Profile_Seconds, CD_Profile_Counts + PUBLIC :: CD_Profile_Requested, CD_Profile_Arm, CD_Profile_Disarm, CD_Profile_Armed, CD_Profile_Enter, & + CD_Profile_Flush, CD_Profile_Step, CD_Profile_Count_Line_Step, CD_Profile_Seconds, CD_Profile_Counts, & + CD_Profile_Kernel_Begin, CD_Profile_Kernel_End, CD_Profile_Kernels INTEGER, PARAMETER, PUBLIC :: CD_PROF_INPUT = 1 ! deck parsing and auxiliary input files INTEGER, PARAMETER, PUBLIC :: CD_PROF_INIT = 2 ! model build, static equilibrium, initial outputs @@ -33,12 +39,20 @@ MODULE CableDyn_Profile INTEGER, PARAMETER, PUBLIC :: CD_PROF_FINAL = 8 ! after the march: closing files, range files INTEGER, PARAMETER, PUBLIC :: CD_PROF_NPHASE = 8 + ! Kernels of the EI = 0 line step (CD_Cable_Gen_Alpha_Step) + INTEGER, PARAMETER, PUBLIC :: CD_PROF_K_RESIDUAL = 1 ! residual-only evaluation + INTEGER, PARAMETER, PUBLIC :: CD_PROF_K_TANGENT = 2 ! residual and effective-tangent evaluation + INTEGER, PARAMETER, PUBLIC :: CD_PROF_K_FACTOR = 3 ! LU factorisation of the effective tangent + INTEGER, PARAMETER, PUBLIC :: CD_PROF_K_SOLVE = 4 ! back-substitution with a factored tangent + INTEGER, PARAMETER, PUBLIC :: CD_PROF_NKERNEL = 4 + LOGICAL, SAVE :: armed = .FALSE. INTEGER, SAVE :: current = CD_PROF_INPUT INTEGER(int64), SAVE :: rate = 0_int64, last = 0_int64 INTEGER(int64), SAVE :: ticks(CD_PROF_NPHASE) = 0_int64 INTEGER(int64), SAVE :: n_march_steps = 0_int64, n_line_steps = 0_int64, n_line_iter = 0_int64 INTEGER, SAVE :: max_line_iter = 0 + INTEGER(int64), SAVE :: k_ticks(CD_PROF_NKERNEL) = 0_int64, k_calls(CD_PROF_NKERNEL) = 0_int64 CONTAINS @@ -58,11 +72,18 @@ SUBROUTINE CD_Profile_Arm() n_line_steps = 0_int64 n_line_iter = 0_int64 max_line_iter = 0 + k_ticks = 0_int64 + k_calls = 0_int64 current = CD_PROF_INPUT CALL SYSTEM_CLOCK(last, rate) armed = rate > 0_int64 END SUBROUTINE CD_Profile_Arm + SUBROUTINE CD_Profile_Disarm() + !! Stop recording; the counts and times so far stay readable until the next CD_Profile_Arm. + armed = .FALSE. + END SUBROUTINE CD_Profile_Disarm + LOGICAL FUNCTION CD_Profile_Armed() RESULT(is_armed) is_armed = armed END FUNCTION CD_Profile_Armed @@ -107,6 +128,39 @@ SUBROUTINE CD_Profile_Count_Line_Step(n_iter) max_line_iter = MAX(max_line_iter, n_iter) END SUBROUTINE CD_Profile_Count_Line_Step + SUBROUTINE CD_Profile_Kernel_Begin(start) + !! The clock at the start of a kernel (zero when the profile is not armed). + INTEGER(int64), INTENT(OUT) :: start + start = 0_int64 + IF (.NOT. armed) RETURN + CALL SYSTEM_CLOCK(start) + END SUBROUTINE CD_Profile_Kernel_Begin + + SUBROUTINE CD_Profile_Kernel_End(kernel, start) + !! Count one call of kernel (a CD_PROF_K_* value) and add the time since start, taken by + !! CD_Profile_Kernel_Begin. Lines step concurrently, so the updates are atomic. + INTEGER, INTENT(IN) :: kernel + INTEGER(int64), INTENT(IN) :: start + INTEGER(int64) :: now + IF (.NOT. armed) RETURN + ! a kernel that began before the profile was armed has no start time + IF (start == 0_int64 .OR. kernel < 1 .OR. kernel > CD_PROF_NKERNEL) RETURN + CALL SYSTEM_CLOCK(now) + !$OMP ATOMIC UPDATE + k_calls(kernel) = k_calls(kernel) + 1_int64 + !$OMP ATOMIC UPDATE + k_ticks(kernel) = k_ticks(kernel) + MAX(0_int64, now - start) + END SUBROUTINE CD_Profile_Kernel_End + + SUBROUTINE CD_Profile_Kernels(calls, seconds) + !! Calls and summed time [s] of each CD_PROF_K_* kernel since the profile was armed. + INTEGER(int64), INTENT(OUT) :: calls(CD_PROF_NKERNEL) + REAL(wp), INTENT(OUT) :: seconds(CD_PROF_NKERNEL) + calls = k_calls + seconds = 0.0_wp + IF (rate > 0_int64) seconds = REAL(k_ticks, wp)/REAL(rate, wp) + END SUBROUTINE CD_Profile_Kernels + REAL(wp) FUNCTION CD_Profile_Seconds(phase) RESULT(seconds) !! Wall time attributed to phase so far [s]; zero when the profile is not armed. INTEGER, INTENT(IN) :: phase diff --git a/src/openfast/CableDyn_OF.f90 b/src/openfast/CableDyn_OF.f90 index 694d40c9..e56a93f5 100644 --- a/src/openfast/CableDyn_OF.f90 +++ b/src/openfast/CableDyn_OF.f90 @@ -97,6 +97,8 @@ MODULE CableDyn CD_HermiteCable_Dyn_Get_Profile, & CD_HermiteCable_Dyn_Recovery_Count, & CD_HermiteCable_Dyn_Recovery_Reset + USE CableDyn_Profile, ONLY: CD_Profile_Arm, CD_Profile_Disarm, CD_Profile_Kernels, CD_PROF_K_RESIDUAL, & + CD_PROF_K_TANGENT, CD_PROF_K_FACTOR, CD_PROF_K_SOLVE, CD_PROF_NKERNEL USE CableDyn_System, ONLY: CD_System_NLines, CD_System_Line_NDOF, CD_System_Line_NElem, & CD_Get_System_DynamicPoint_States, CD_Set_System_DynamicPoint_States, & CD_System_NSystemCoupledDOF, CD_Get_System_CoupledMotion, & @@ -494,6 +496,7 @@ SUBROUTINE CD_Init(InitInp, u, p, x, xd, z, other, y, m, DTcoupling, InitOut, Er ! Disarm before any static/dynamic initialization work. This also covers an ! initialization that later fails and never reaches the live-instance setup. CALL CD_HermiteCable_Dyn_Disable_Profile() + CALL CD_Profile_Disarm() END IF ! ------------------------------------------------------------------------------ @@ -2433,8 +2436,12 @@ SUBROUTINE CD_End(u, p, x, xd, z, other, y, m, ErrStat , ErrMsg) character(*), parameter :: RoutineName = 'CD_End' INTEGER(IntKi) :: ErrStat2 - INTEGER :: hn_step, hn_resid, hn_solve, hn_am, hn_alloc, hn_tan + INTEGER :: hn_step, hn_resid, hn_solve, hn_am, hn_alloc, hn_tan, hn_factor, hn_correct INTEGER(int64) :: sb_calls, sb_events, sb_capped, sb_fallbacks + INTEGER(int64) :: k_calls(CD_PROF_NKERNEL) + REAL(wp) :: k_seconds(CD_PROF_NKERNEL), ht_factor + CHARACTER(24) :: k_txt(CD_PROF_NKERNEL) + INTEGER :: k_i INTEGER :: sb_max, sb_ios CHARACTER(24) :: sb_txt(5) REAL(wp) :: ht_step, ht_resid, ht_eff, ht_solve, ht_am, ht_tan @@ -2479,20 +2486,39 @@ SUBROUTINE CD_End(u, p, x, xd, z, other, y, m, ErrStat , ErrMsg) ' wave='//TRIM(Num2LStr(Inst(m%CDInst)%prof_n_wave))// & ' calcout='//TRIM(Num2LStr(Inst(m%CDInst)%prof_n_calcout))// & ' fresh='//TRIM(Num2LStr(Inst(m%CDInst)%prof_n_fresh)) ) + ! the kernels of the EI = 0 line steps (summed over the lines) + CALL CD_Profile_Kernels(k_calls, k_seconds) + IF (SUM(k_calls) > 0_int64) THEN + DO k_i = 1, CD_PROF_NKERNEL + WRITE (k_txt(k_i), '(I0)', IOSTAT=sb_ios) k_calls(k_i) + END DO + CALL WrScr( ' CableDyn EI = 0 profile: residual_s='// & + TRIM(Num2LStr(REAL(k_seconds(CD_PROF_K_RESIDUAL), DbKi)))// & + ' tangent_s='//TRIM(Num2LStr(REAL(k_seconds(CD_PROF_K_TANGENT), DbKi)))// & + ' factor_s='//TRIM(Num2LStr(REAL(k_seconds(CD_PROF_K_FACTOR), DbKi)))// & + ' backsolve_s='//TRIM(Num2LStr(REAL(k_seconds(CD_PROF_K_SOLVE), DbKi))) ) + CALL WrScr( ' CableDyn EI = 0 counts: residual='//TRIM(k_txt(CD_PROF_K_RESIDUAL))// & + ' tangent='//TRIM(k_txt(CD_PROF_K_TANGENT))// & + ' factor='//TRIM(k_txt(CD_PROF_K_FACTOR))// & + ' backsolve='//TRIM(k_txt(CD_PROF_K_SOLVE)) ) + END IF IF (Inst(m%CDInst)%agg%ncable > 0) THEN CALL CD_HermiteCable_Dyn_Get_Profile(hn_step, hn_resid, hn_solve, hn_am, hn_alloc, & ht_step, ht_resid, ht_eff, ht_solve, ht_am, & - hn_tan, ht_tan) + hn_tan, ht_tan, hn_factor, ht_factor, hn_correct) CALL WrScr( ' CableDyn Hermite profile: step_s='//TRIM(Num2LStr(REAL(ht_step, DbKi)))// & ' residual_s='//TRIM(Num2LStr(REAL(ht_resid, DbKi)))// & ' tangent_s='//TRIM(Num2LStr(REAL(ht_tan, DbKi)))// & ' effective_s='//TRIM(Num2LStr(REAL(ht_eff, DbKi)))// & - ' solve_s='//TRIM(Num2LStr(REAL(ht_solve, DbKi)))// & + ' factor_s='//TRIM(Num2LStr(REAL(ht_factor, DbKi)))// & + ' backsolve_s='//TRIM(Num2LStr(REAL(ht_solve, DbKi)))// & ' added_mass_s='//TRIM(Num2LStr(REAL(ht_am, DbKi))) ) CALL WrScr( ' CableDyn Hermite counts: step='//TRIM(Num2LStr(hn_step))// & ' residual='//TRIM(Num2LStr(hn_resid))// & ' tangent='//TRIM(Num2LStr(hn_tan))// & + ' factor='//TRIM(Num2LStr(hn_factor))// & ' solve='//TRIM(Num2LStr(hn_solve))// & + ' correction='//TRIM(Num2LStr(hn_correct))// & ' added_mass='//TRIM(Num2LStr(hn_am))// & ' alloc='//TRIM(Num2LStr(hn_alloc)) ) CALL WrScr( ' CableDyn Hermite recoveries: intervals='// & @@ -4235,8 +4261,11 @@ SUBROUTINE reset_instance_profile( id, enabled, init_seconds, reset_global ) CALL CD_HermiteCable_Dyn_Recovery_Reset() IF (enabled) THEN CALL CD_HermiteCable_Dyn_Reset_Profile() + ! the EI = 0 line-step kernels (CableDyn_Profile, process-global like the above) + CALL CD_Profile_Arm() ELSE CALL CD_HermiteCable_Dyn_Disable_Profile() + CALL CD_Profile_Disarm() END IF END IF END SUBROUTINE reset_instance_profile diff --git a/tests/test_profile.f90 b/tests/test_profile.f90 index ed1244a2..a47b00b8 100644 --- a/tests/test_profile.f90 +++ b/tests/test_profile.f90 @@ -12,6 +12,10 @@ PROGRAM test_profile !! finalisation of such a run) stays exactly zero. !! 4. The EI = 0 line-step counters are exact when the lines of an OpenMP team update them !! at once. + !! 5. The EI = 0 kernel counters: unarmed, a kernel records nothing; armed, the calls of each + !! kernel are exact under an OpenMP team and their times add up; a kernel that began + !! before the profile was armed is not counted; disarmed, nothing more is recorded and + !! the counts so far stay readable; arming again zeroes them. USE CableDyn_Precision, ONLY: wp USE CableDyn_Profile USE CableDyn_FatalReport, ONLY: CD_Fatal_Thread_Init @@ -23,6 +27,7 @@ PROGRAM test_profile CALL case_unarmed() CALL case_partition_and_failure() CALL case_concurrent_counts() + CALL case_kernels() IF (nfail > 0) THEN WRITE (*, '(A,I0,A)') 'test_profile: ', nfail, ' failure(s)' ERROR STOP 1 @@ -123,4 +128,52 @@ SUBROUTINE case_concurrent_counts() CALL require(max_iterations == 6, 'counts:most-iterations') END SUBROUTINE case_concurrent_counts + SUBROUTINE case_kernels() + INTEGER, PARAMETER :: N = 20000 + INTEGER(int64) :: calls(CD_PROF_NKERNEL), c0 + REAL(wp) :: seconds(CD_PROF_NKERNEL) + INTEGER :: i, kernel + ! unarmed: nothing is recorded + CALL CD_Profile_Disarm() + CALL CD_Profile_Kernel_Begin(c0) + CALL require(c0 == 0_int64, 'kernels:unarmed-begin-has-no-start') + CALL CD_Profile_Arm() + CALL CD_Profile_Disarm() + CALL CD_Profile_Kernel_End(CD_PROF_K_FACTOR, 1_int64) + CALL CD_Profile_Kernels(calls, seconds) + CALL require(ALL(calls == 0_int64) .AND. .NOT. ANY(seconds > 0.0_wp), 'kernels:unarmed-records-nothing') + ! armed: exact calls of each kernel from an OpenMP team, and a measurable time + CALL CD_Profile_Arm() + !$OMP PARALLEL NUM_THREADS(4) DEFAULT(SHARED) PRIVATE(i, kernel, c0) + CALL CD_Fatal_Thread_Init() + !$OMP DO SCHEDULE(STATIC) + DO i = 1, N + kernel = 1 + MOD(i, CD_PROF_NKERNEL) + CALL CD_Profile_Kernel_Begin(c0) + CALL CD_Profile_Kernel_End(kernel, c0) + END DO + !$OMP END DO + !$OMP END PARALLEL + CALL CD_Profile_Kernel_Begin(c0) + CALL busy(0.01_wp) + CALL CD_Profile_Kernel_End(CD_PROF_K_TANGENT, c0) + ! a kernel that began unarmed (start 0) and an unknown kernel are not counted + CALL CD_Profile_Kernel_End(CD_PROF_K_SOLVE, 0_int64) + CALL CD_Profile_Kernel_End(CD_PROF_NKERNEL + 1, c0) + CALL CD_Profile_Kernels(calls, seconds) + CALL require(calls(CD_PROF_K_RESIDUAL) == N/CD_PROF_NKERNEL, 'kernels:residual-calls-exact') + CALL require(calls(CD_PROF_K_TANGENT) == N/CD_PROF_NKERNEL + 1, 'kernels:tangent-calls-exact') + CALL require(calls(CD_PROF_K_FACTOR) == N/CD_PROF_NKERNEL, 'kernels:factor-calls-exact') + CALL require(calls(CD_PROF_K_SOLVE) == N/CD_PROF_NKERNEL, 'kernels:solve-calls-exact') + CALL require(seconds(CD_PROF_K_TANGENT) >= 0.009_wp .AND. ALL(seconds >= 0.0_wp), 'kernels:times') + ! disarmed: nothing more is recorded, the counts stay readable; arming again zeroes them + CALL CD_Profile_Disarm() + CALL CD_Profile_Kernel_End(CD_PROF_K_FACTOR, c0) + CALL CD_Profile_Kernels(calls, seconds) + CALL require(calls(CD_PROF_K_FACTOR) == N/CD_PROF_NKERNEL, 'kernels:disarmed-keeps-counts') + CALL CD_Profile_Arm() + CALL CD_Profile_Kernels(calls, seconds) + CALL require(ALL(calls == 0_int64), 'kernels:arm-zeroes') + END SUBROUTINE case_kernels + END PROGRAM test_profile