How to use an optional attribute for alleged form arrays

I encountered the following situation when using the optional Fortran attribute for the intended array of forms and trying to find the best (in terms of performance) solution. I would be very happy if someone could give me a good hint. Please note that I am interested in every performance increase I can get, since my arrays are large, and the number of loops and their loops are even greater.

I have a situation where the calculation is performed either using an optional argument, or if it is absent, it uses another array in its place.

  subroutine compute(tmat,av,av2)
  implicit none
  complex,intent(out)           :: tmat(:)   
  complex,intent(in)             :: av(:)      
  complex,intent(in),optional    :: av2(:)     

  if(present(av2)) then
      tmat = av *av2
  else
      tmat = av *av
  end if

  end subroutine compute_transition_matrices_bosonic

. , . , ( ), . :

  if(present(av2)) then
      tmat = "function"(av,av2)
  else
      tmat = "function"(av,av)
  end if

"" (, ""). , if, . , av2 , , () .

, . -,

  complex,                :: phi_tmp(:)

  if(present(av2)) then
      phi_tmp = av2
  else
      phi_tmp = av
  end if

  tmat = "function"(av,phi_tmp)

. COPY , .

  complex,intent(in),target             :: av(:)      
  complex,intent(in),optional,target    :: av2(:)   
  complex,pointer                :: phi_tmp(:)

  if(present(av2)) then
      phi_tmp => av2
  else
      phi_tmp => av
  end if

  tmat = "function"(av,phi_tmp)

TARGET av av2. , , , av av2 , INTENT(IN) . , , , , TARGET? ( !)

- ? "" ? ( ) ?

+4
2

, , , undefined , , .

(, , ) , , , , , intent(in), . , .

subroutine compute(tmat, av, av2)
  implicit none
  complex, intent(out)                  :: tmat(:)
  complex,intent(in),target             :: av(:)      
  complex,intent(in),optional,target    :: av2(:)   
  complex,pointer                :: phi_tmp(:)

  if(present(av2)) then
     phi_tmp => av2
  else
     phi_tmp => av
  end if

  call compute_internal(tmat, av, phi_tmp)

contains
  subroutine compute_internal(tmat, av, av2)
    complex,intent(out)       :: tmat(:)
    complex,intent(in)        :: av(:)      
    complex,intent(in)        :: av2(:)

    .....

  end subroutine
end subroutine
+2

- .

subroutine compute1(tmat,av)
  implicit none
  complex,intent(out)           :: tmat(:)   
  complex,intent(in)            :: av(:)      

  call compute2(tmat, av, av)  ! Ensure there is an explicit interface for compute2 here
end subroutine

subroutine compute2(tmat,av,av2)
  implicit none
  complex,intent(out)           :: tmat(:)   
  complex,intent(in)            :: av(:)      
  complex,intent(in)            :: av2(:)     

  tmat = "function"(av,av2)
end subroutine

.

, compute .

, , intent(in) av av2 compute2 aliasing.

+4

Source: https://habr.com/ru/post/1607426/


All Articles