# Pure subroutine error with do concurrent for external routine

**URL:** <https://fortran-lang.discourse.group/t/pure-subroutine-error-with-do-concurrent-for-external-routine/5604>\
**Category:** Help\
**Created:** [April 19, 2023, 2:20pm UTC](https://fortran-lang.discourse.group/t/pure-subroutine-error-with-do-concurrent-for-external-routine/5604 "2023-04-19T14:20:50Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Shahid](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/shahid/32/3672_2.png) [@Shahid](https://fortran-lang.discourse.group/u/Shahid)\
**Post date:** [April 19, 2023, 2:20pm UTC](https://fortran-lang.discourse.group/t/pure-subroutine-error-with-do-concurrent-for-external-routine/5604/1 "2023-04-19T14:20:50Z")

</div>

I am getting this error:

 ![error](https://global.discourse-cdn.com/free1/uploads/fortran_lang/original/2X/b/bab7dd81155949f4d004ee33963e2258a5f43b0e.jpeg)

for my main program

```auto
program test
  implicit none 

  integer ( kind = 4 ), parameter :: Nx = 64, Ny = 64
  real ( kind = 8 ) , dimension ( Nx, Ny ) :: phi, dfdphi
  integer ( kind = 4 ) :: i, j

  call random_number (phi)    

  do concurrent ( i = 1 : Nx , j = 1 : Ny )
     call Get_dphi ( phi, dfdphi, i, j )
  end do
  
end program test

```

The external routine is

```auto
  pure subroutine Get_dphi ( phi_, dfdphi_, i_, j_ )
    implicit none

    integer ( kind = 4 ), parameter :: Nx_ = 64,Ny_ = 64
    real ( kind = 8 ), dimension ( Nx_, Ny_ ), intent ( in ) :: phi_
    real ( kind = 8 ), dimension ( Nx_, Ny_ ), intent ( out ) :: dfdphi_
    integer ( kind = 4 ), intent ( in ) :: i_, j_

    dfdphi_(i_,j_) = 2.0*phi_(i_,j_)*( 1.0 - phi_(i_,j_) )**2 &
         *( 1.0 - 2*phi_(i_,j_) ) 

  end subroutine Get_dphi

```

I do not see any error if it is used as an internal routine. So what makes it **not pure** here?

---

<div class="post-metadata">

**Author:** ![Beliavsky](https://avatars.discourse-cdn.com/v4/letter/b/ba8739/32.png) [@Beliavsky](https://fortran-lang.discourse.group/u/Beliavsky)\
**Post date:** [April 19, 2023, 2:26pm UTC](https://fortran-lang.discourse.group/t/pure-subroutine-error-with-do-concurrent-for-external-routine/5604/2 "2023-04-19T14:26:23Z")

</div>

> [@Shahid](#):
>
> I do not see any error if it is used as an internal routine. So what makes it **not pure** here?

I think gfortran cannot see that the subroutine is pure because it is not in a module and an INTERFACE was not provided. Gfortran 13.0.0 20221218 compiles the following code, in which the subroutine is put in a module:

```auto
module m
contains
  pure subroutine Get_dphi ( phi_, dfdphi_, i_, j_ )
    implicit none

    integer ( kind = 4 ), parameter :: Nx_ = 64,Ny_ = 64
    real ( kind = 8 ), dimension ( Nx_, Ny_ ), intent ( in ) :: phi_
    real ( kind = 8 ), dimension ( Nx_, Ny_ ), intent ( out ) :: dfdphi_
    integer ( kind = 4 ), intent ( in ) :: i_, j_

    dfdphi_(i_,j_) = 2.0*phi_(i_,j_)*( 1.0 - phi_(i_,j_) )**2 &
         *( 1.0 - 2*phi_(i_,j_) ) 

  end subroutine Get_dphi
end module m

program test
  use m
  implicit none 

  integer ( kind = 4 ), parameter :: Nx = 64, Ny = 64
  real ( kind = 8 ) , dimension ( Nx, Ny ) :: phi, dfdphi
  integer ( kind = 4 ) :: i, j

  call random_number (phi)    

  do concurrent ( i = 1 : Nx , j = 1 : Ny )
     call Get_dphi ( phi, dfdphi, i, j )
  end do
  
end program test

```

---

<div class="post-metadata">

**Author:** ![FortranFan](https://avatars.discourse-cdn.com/v4/letter/f/96bed5/32.png) [@FortranFan](https://fortran-lang.discourse.group/u/FortranFan)\
**Post date:** [April 19, 2023, 7:13pm UTC](https://fortran-lang.discourse.group/t/pure-subroutine-error-with-do-concurrent-for-external-routine/5604/3 "2023-04-19T19:13:53Z")

</div>

> [@Shahid](#):
>
> … So what makes it **not pure** here?

@Shahid ,

As illustrated by @Beliavsky , a reference to a `PURE` subprogram (e.g., in your main program) requires an explicit interface.

Try to avoid using implicit interfaces as much as you can. Consider using an explicit interface as imperative with all features introduced in the language starting Fortran 90; `PURE` procedures a good example of this.

By the way, you may want to consider moving away from “magic numbers” such as 8 for the `KIND`s of intrinsic types, instead try using named constants with language intrinsics such as `SELECTED_REAL_KIND`.

---

<div class="post-metadata">

**Author:** ![Shahid](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/shahid/32/3672_2.png) [@Shahid](https://fortran-lang.discourse.group/u/Shahid)\
**Post date:** [April 19, 2023, 7:15pm UTC](https://fortran-lang.discourse.group/t/pure-subroutine-error-with-do-concurrent-for-external-routine/5604/4 "2023-04-19T19:15:25Z")

</div>

> [@Beliavsky](#):
>
> I think gfortran cannot see that the subroutine is pure because it is not in a module and an INTERFACE was not provided

**Interface block** usually serves that purpose, as to my understanding.

But using it still produces the same error!

```auto
program test
  implicit none 

  interface
     subroutine Get_dphi ( phi_, dfdphi_, i_, j_ )
       implicit none

       integer ( kind = 4 ), parameter :: Nx = 64,Ny = 64
       real ( kind = 8 ), dimension ( Nx, Ny ), intent ( in ) :: phi_
       real ( kind = 8 ), dimension ( Nx, Ny ), intent ( out ) :: dfdphi_
       integer ( kind = 4 ), intent ( in ) :: i_, j_
     end subroutine Get_dphi
  end interface
  
  integer ( kind = 4 ), parameter :: Nx = 64, Ny = 64
  real ( kind = 8 ) , dimension ( Nx, Ny ) :: phi, dfdphi
  integer ( kind = 4 ) :: i, j

  call random_number (phi)    

  do concurrent ( i = 1 : Nx , j = 1 : Ny )
     call Get_dphi ( phi, dfdphi, i, j )
  end do

end program

```

---

<div class="post-metadata">

**Author:** ![FortranFan](https://avatars.discourse-cdn.com/v4/letter/f/96bed5/32.png) [@FortranFan](https://fortran-lang.discourse.group/u/FortranFan)\
**Post date:** [April 19, 2023, 7:18pm UTC](https://fortran-lang.discourse.group/t/pure-subroutine-error-with-do-concurrent-for-external-routine/5604/5 "2023-04-19T19:18:03Z")

</div>

> [@Shahid](#):
>
> still produces the same error!
> 
> ```auto
> program test
> implicit none 
> 
> interface
> subroutine Get_dphi ( phi_, dfdphi_, i_, j_ )
> implicit none
> 
> ```

The declared interface is not marked as `PURE`
