Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PSyAd] [LFRic] Errors in use of LBOUND and looping in generated adjoint kernel #2935

Open
tom-j-h opened this issue Mar 25, 2025 · 0 comments

Comments

@tom-j-h
Copy link

tom-j-h commented Mar 25, 2025

Found when using PSyAD to generate adjoint kernel for science/gungho/source/kernel/solver/apply_mixed_operator_kernel_mod.F90 for LFRic Apps ticket 669.

The relevant bits of the required patch file for the generated adjoint kernel are:

-        u_e(idx_12 - iw3 + LBOUND(u_e(:,:), 1),df) = u_e(idx_12 - iw3 + LBOUND(u_e(:,:), 1),df) + q32(idx_12 + ij - iw3,1,df) * &
+        u_e(idx_12 - iw3 + LBOUND(u_e, dim=1),df) = u_e(idx_12 - iw3 + LBOUND(u_e, dim=1),df) + q32(idx_12 + ij - iw3,1,df) * &

-          u_e(idx_10 - iw2v + LBOUND(u_e(:,:), 1),df2) = u_e(idx_10 - iw2v + LBOUND(u_e(:,:), 1),df2) + mu_cd(idx_10 + ij - &
+          u_e(idx_10 - iw2v + LBOUND(u_e, dim=1),df2) = u_e(idx_10 - iw2v + LBOUND(u_e, dim=1),df2) + mu_cd(idx_10 + ij - &

-    do idx_7 = UBOUND(t_col, dim=1), LBOUND(t_col, dim=1), -1
-      t_col(:) = t_col(:) + mt_lumped_inv(idx_7 + iwt - LBOUND(t_col(idx_7), 1)) * t_col(idx_7)
-      t_col(idx_7) = 0.0
-    enddo
+    t_col(:) = t_col(:) * mt_lumped_inv(iwt:iwt+1+nm1)

-        u_e(idx_6 + LBOUND(u_e(:,:), 1) - 1,df) = u_e(idx_6 + LBOUND(u_e(:,:), 1) - 1,df) + (-pt2(idx_6 + ij - 1,2,df) * &
+        u_e(idx_6 + LBOUND(u_e, dim=1) - 1,df) = u_e(idx_6 + LBOUND(u_e, dim=1) - 1,df) + (-pt2(idx_6 + ij - 1,2,df) * &

-          u_e(idx_3 - iw2h + LBOUND(u_e(:,:), 1),df2) = u_e(idx_3 - iw2h + LBOUND(u_e(:,:), 1),df2) + mu_cd(idx_3 + ij - &
+          u_e(idx_3 - iw2h + LBOUND(u_e, dim=1),df2) = u_e(idx_3 - iw2h + LBOUND(u_e, dim=1),df2) + mu_cd(idx_3 + ij - &

There appears to be two problems here:

  1. The lines starting with the variable u_e are calling LBOUND on an array section rather than an array, so this always returns 1 (https://gcc.gnu.org/onlinedocs/gfortran/LBOUND.html) and the adjoint produced was incorrect.
  2. The idx_7 loop: firstly the LBOUND statement within the loop does not compile since it's being called on a scalar, but even so, the entire loop is unnecessary; the forward code contains the line:
    t_col(:) = t_col(:) * mt_lumped_inv(iwt:iwt+1+nm1)
    and since t_col is an active variable but mt_lumped_inv is a constant, the adjoint should contain the identical line (as seen in the patch).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant