NAG Library Manual, Mark 30.2
Interfaces:  FL   CL   CPP   AD 

NAG AD Library Introduction
Example description
    Program f06ya_a1w_fe

!     F06YA_A1W_F Example Program Text

!     Mark 30.2 Release. NAG Copyright 2024.

!     .. Use Statements ..
      Use iso_c_binding, Only: c_ptr
      Use nagad_library, Only: f06ya_a1w_f, nagad_a1w_get_derivative,          &
                               nagad_a1w_inc_derivative,                       &
                               nagad_a1w_ir_interpret_adjoint_sparse,          &
                               nagad_a1w_ir_register_variable,                 &
                               nagad_a1w_ir_remove, nagad_a1w_ir_zero_adjoints &
                               , nagad_a1w_w_rtype, x10aa_a1w_f, x10ab_a1w_f,  &
                               x10za_a1w_f, Assignment (=)
      Use nag_library, Only: nag_wp, x04caf
!     .. Implicit None Statement ..
      Implicit None
!     .. Parameters ..
      Integer, Parameter               :: nin = 5, nout = 6
!     .. Local Scalars ..
      Type (c_ptr)                     :: ad_handle
      Type (nagad_a1w_w_rtype)         :: alpha, beta
      Real (Kind=nag_wp)               :: alphar, betar
      Integer                          :: i, ifail, j, k, lda, ldb, ldc, m, n
!     .. Local Arrays ..
      Type (nagad_a1w_w_rtype), Allocatable :: a(:,:), b(:,:), c(:,:),         &
                                          c_in(:,:)
      Real (Kind=nag_wp), Allocatable  :: ar(:,:), br(:,:), cr(:,:), dr(:,:)
!     .. Executable Statements ..
      Write (nout,*) 'F06YA_A1W_F Example Program Results'
      Write (nout,*)
!     Skip heading in data file
      Read (nin,*)
      Read (nin,*) m, n, k
      Read (nin,*) alphar, betar
      alpha = alphar
      beta = betar
      lda = k
      ldb = n
      ldc = m
      Allocate (a(lda,m),b(ldb,k),c(ldc,n),c_in(ldc,n))
      Allocate (ar(lda,m),br(ldb,k),cr(ldc,n),dr(m,n))

!     Read A from data file
      Read (nin,*)(ar(i,1:m),i=1,k)

!     Read B from data file
      Read (nin,*)(br(i,1:k),i=1,n)

      cr(1:m,1:n) = 1.0_nag_wp
      c_in(1:m,1:n) = cr(1:m,1:n)

!     Create AD tape
      Call x10za_a1w_f

!     Create AD configuration data object
      ifail = 0
      Call x10aa_a1w_f(ad_handle,ifail)

      Call nagad_a1w_ir_register_variable(alpha)
      Call nagad_a1w_ir_register_variable(beta)

      a = ar
      b = br
      c = c_in

      ifail = 0
      Call f06ya_a1w_f(ad_handle,'T','T',m,n,k,alpha,a,lda,b,ldb,beta,c,ldc,   &
        ifail)

      Write (nout,*)
      Write (nout,*) 'Product solution C = beta*C + alpha*A^T*B^T:'
      Write (nout,*)
      cr = c
!     Print solution
      ifail = 0
      Call x04caf('G','N',m,n,cr,ldc,'updated C',ifail)

      Write (nout,*)
      Write (nout,*) ' Derivatives calculated: First order adjoints'
      Write (nout,*) ' Computational mode    : algorithmic'

      Write (nout,*)
      Write (nout,*) ' Derivatives of C w.r.t. alpha and beta'
      Write (nout,*)

!     Setup evaluation of derivatives via adjoints
      Do i = 1, m
        Do j = 1, n
          Call nagad_a1w_ir_zero_adjoints
          Call nagad_a1w_inc_derivative(c(i,j),1.0_nag_wp)
          ifail = 0
          Call nagad_a1w_ir_interpret_adjoint_sparse(ifail)

!         Get derivatives
          cr(i,j) = nagad_a1w_get_derivative(alpha)
          dr(i,j) = nagad_a1w_get_derivative(beta)
        End Do
      End Do
      Call x04caf('General',' ',m,n,cr,ldc,'       dC/dalpha',ifail)
      Write (nout,*)
      Call x04caf('General',' ',m,n,dr,m,'       dC/dbeta',ifail)

!     Remove computational data object and tape
      Call x10ab_a1w_f(ad_handle,ifail)
      Call nagad_a1w_ir_remove

    End Program f06ya_a1w_fe