PGI fortran supports Finalization typebound procedure?

Dear all,

I am trying to parallelize my Fortran code using openacc. My code contains Finalization typebound procedure, like:

module m_array
type array
data members here …
contains
method members here …
Final :: array_delete
end type
contains
subroutine array_delete(this)
type(array) this

end subroutine
end module

It seems that the PGI Fortran compiler doesn’t support this Finalization method. The compiler gives me the message:

error LNK2001: unresolved external symbol m_array_array_td_ft_

So I am asking whether the PGI Fortran compiler support the Finalization method or not?

Good question. I looked up an example for Final, and found this

module my_type_module
implicit none

type my_type
contains
final :: destructor
end type my_type

interface my_type
! Constructor declaration
module procedure my_type_constructor
end interface my_type

contains
function my_type_constructor()
type(my_type) :: my_type_constructor

print *, 'In constructor address is: ', loc(my_type_constructor)

end function my_type_constructor

subroutine destructor(this)
type(my_type) :: this
print *, 'Destructor of my_type object with address: ', loc(this)
end subroutine destructor

end module my_type_module

program trial

use my_type_module
implicit none

type(my_type) :: argument

print *, ‘Trial program starts’
print *, ‘Initial address is’, loc(argument)

argument = my_type_constructor()

print *, ‘Address afer constructor is called is’, loc(argument)

print *, ‘doing some work…’
print *, ‘finishing program…’

print *, ‘Final address is’, loc(argument)

end program trial


It compiles and gives the same results as gfortran.
But the message from the destructor is not visible in either pgfortran
or gfortran, but does appear with ifort.

dave

Upon reflection, the example I provided may have missed the point of your
question.

Please turn your program fragment into a real program that should not fail and send it
to trs@pgroup.com. These types of issues can be dependent upon specific
data types, and we want to solve/correct the issue you are having.

thanks,
dave

m_array.f90:

!---------------------------
    module m_array
!---------------------------
implicit none
integer, parameter :: dp = kind(0.d0)

type array
   character(len=16) :: name = ''
   integer :: ndim1
   integer :: rank = 0
   real(dp), pointer, dimension(:) :: r  => null()

   contains
       final :: array_final
end type

!=======
contains
!=======

!--------------------------------------------
       subroutine array_final(this)
!--------------------------------------------
implicit none
type(array) :: this

if(associated(this%r))deallocate(this%r)

return
end subroutine

end module

m_particles.f90


module m_particles
  use m_array
  implicit none
end module

main.f90

program SPH
use m_array
implicit none

write(*,*) 'Hello'

end

With the ifort it works:

D:\LiuSPH\data\cas_mix_Savage9904_tmp>ifort -o beham.exe m_array.f90 m_particles.f90 main.f90
Intel(R) Visual Fortran Intel(R) 64 Compiler XE for applications running on Intel(R) 64, Version 14.0.1.139 Build 20131008
Copyright (C) 1985-2013 Intel Corporation. All rights reserved.

Microsoft (R) Incremental Linker Version 12.00.21005.1
Copyright (C) Microsoft Corporation. All rights reserved.

-out:beham.exe
-subsystem:console
m_array.obj
m_particles.obj
main.obj

pgfortran gives the following messages:

D:\LiuSPH\data\cas_mix_Savage9904_tmp>pgfortran -o beham.exe m_array.f90 m_particles.f90 main.f90
m_array.f90:
NOTE: your trial license will expire in 9 days, 6.7 hours.
NOTE: your trial license will expire in 9 days, 6.7 hours.
m_particles.f90:
NOTE: your trial license will expire in 9 days, 6.7 hours.
NOTE: your trial license will expire in 9 days, 6.7 hours.
main.f90:
NOTE: your trial license will expire in 9 days, 6.7 hours.
NOTE: your trial license will expire in 9 days, 6.7 hours.
main.obj : error LNK2005: m_array_array_td_ft_ already defined in m_particles.obj
beham.exe : fatal error LNK1169: one or more multiply defined symbols found


If I remove the second file m_particles.f90 or remove the finalization method from module m_array, pgi compiler works well.
Please let me know if I have used the pgi compiler correctly or not! Thanks
I have sent the codes to trs@pgroup.com to have a check.


Eric

We filed a problem report 22830 for this issue. Here is engineering’s
explanation…

=================================================
This is expected behavior. Below is the description in the Fotran 2008 language specification (page 76):

4.5.6.4 Entities that are not Finalized If image execution is terminated, either by an error (e.g. an allocation failure) or by execution of a stop-stmt, error-stop-stmt, or end-program-stmt, entities existing immediately prior to termination are not finalized.

Similar language is in the Fortran 2003 language specification (page 60):

4.5.5.3 Entities that are not finalized If program execution is terminated, either by an error (e.g. an allocation failure) or by execution of a STOP or END PROGRAM statement, entities existing immediately prior to termination are not finalized.

In other words, objects that live in the “main program”, do not get finalized. If we change “program trial” to “subroutine trial()” and call “trial” from the “main program”, the destructor gets called.

So we are closing TPR 22830.

Thanks for the information!

But the problem is still there! PGI compiler doesn’t work for the above codes! It may not have been caused by the finalization method, but due to other reasons.

I remember that you have reported a problem TPR 22839 for this issue in the last email. You told me that this problem appears when using PGI compiler for Windows. Has this problem been solved? Anyway, thank you very much!

Eric

I believe the argument is that Intel and gfortran are not implementing the
Standard correctly, or they are extending it. In either case, you are not programming to the standard, so the outcome cannot be guaranteed.

What the response said is that you are not programming to the standard.
If you did, all three compilers should give the same correct result, or there is
an error.

dave

Dear Dave,

I have tried according to your instructions, but failed again. My codes are there and are really simple. Could you please check the codes and tell me what the problem is in my codes?

The situation now is that my codes cannot pass the compiler. The problem of finalization can be put aside at the moment.

Thanks,

Eric