Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
Announcements
FPGA community forums and blogs on community.intel.com are migrating to the new Altera Community and are read-only. For urgent support needs during this transition, please visit the FPGA Design Resources page or contact an Altera Authorized Distributor.
29285 Discussions

How to declare pointers to subroutines?

rwg
Novice
1,048 Views
Today I found some old code with pointers to subroutines. It looks like this:
[fxfortran]      PROGRAM TEST
INTEGER LOCEX
EXTERNAL DEFEX
EXTERNAL TESTEX
C Save Pointer to external subroutine
CALL EXTERNALCALL('SAVE',LOC(TESTEX))
C Get Pointer to external subroutine
CALL EXTERNALCALL('GET',LOCEX)
C If not associated take DEFEX
IF(LOCEX.EQ.0) LOCEX=LOC(DEFEX)
C CALL LOCEX IN EXTERALTOOL
CALL CALLEXTERNAL(%VAL(LOCEX))
END


SUBROUTINE CALLEXTERNAL(EXPROG)
C Call an external subroutine
EXTERNAL EXPROG
CALL EXPROG
END


SUBROUTINE EXTERNALCALL(OPT,LOCEX)
C Save pointer to an external subroutine or return pointer to subroutine
CHARACTER(*) OPT
INTEGER LOCEX
INTEGER LOCEXSAVE
SAVE LOCEXSAVE
DATA LOCEXSAVE /0/
IF(OPT.EQ.'GET') THEN
LOCEX=LOCEXSAVE
ELSEIF(OPT.EQ.'SAVE') THEN
LOCEXSAVE=LOCEX
ENDIF
END


SUBROUTINE DEFEX
WRITE(*,*) "DEFEX"
READ(*,*)
END


SUBROUTINE TESTEX
WRITE(*,*) "TESTEX"
READ(*,*)
END[/fxfortran]
This code runs fine but it compiles only without /warn:interfaces. My attempts to correct the declarations result in internal compiler errors. Does someone know how to correct the declarations?
0 Kudos
7 Replies
mecej4
Honored Contributor III
1,048 Views
What you have done with the nonstandard extension functions %LOC and %VAL can be achieved with the procedure pointer feature of Fortran 2003. This feature is (newly) available in Intel Fortran, but many other compilers do not provide it yet.

Here is a simple example:

[fortran]PROGRAM PROC_PTR_EXAMPLE
  REAL :: R1
  INTEGER :: I1
  INTERFACE
    SUBROUTINE SUB(X)
      REAL, INTENT(IN) :: X
    END SUBROUTINE SUB
  END INTERFACE
  PROCEDURE(SUB), POINTER :: PTR_TO_SUB => NULL()              ! with explicit interface
  PROCEDURE(SUB), POINTER :: PTR_TO_SUB1 => NULL()             ! with explicit interface
  EXTERNAL :: SUB1, SUB2

  PTR_TO_SUB => SUB1
  CALL PTR_TO_SUB(1.0)

  PTR_TO_SUB1 => SUB2                           ! save pointer
  NULLIFY(PTR_TO_SUB)                           ! make PTR_TO_SUB not callable
!...
!...
  PTR_TO_SUB => PTR_TO_SUB1                     ! restore pointer
  CALL PTR_TO_SUB(2.0)
END PROGRAM PROC_PTR_EXAMPLE

subroutine SUB1(X)
  real, intent(in) :: X
  write(*,10)x
  return
  10 format(' SUB1 ',1pE15.5)
end subroutine SUB1

subroutine SUB2(X)
  real, intent(in) :: X
  write(*,10)x*5
  return
  10 format(' SUB2 ',1pE15.5)
end subroutine SUB2
[/fortran]
0 Kudos
rwg
Novice
1,048 Views


Even though I would prefer a solution for FORTRAN90/95 I also tried it with procedure pointes but all my attempts result in internal compiler errors.

Your solution also gives me an internal compiler error. When I remove line 13 and 16 (PTR_TO_SUB=>SUB1 and PTR_TO_SUB=>PTR_TO_SUB1) the error is gone.

I'm using Intel Visual Fortran 11.1.065 [IA-32]

0 Kudos
Steven_L_Intel1
Employee
1,048 Views
I don't see an error with 11.1.065. Which compiler options are you using?
0 Kudos
rwg
Novice
1,048 Views
Here are my compiler options:

/nologo /debug:full /Od /gen-interfaces /debug-parameters:all /warn:declarations /warn:unused /warn:ignore_loc /warn:uncalled /warn:interfaces /iface:cvf /module:"c:/sw/wg/testprj_fortran" /object:"Debug\" /traceback /check:bounds /libs:static /threads /dbglibs /c
0 Kudos
Steven_L_Intel1
Employee
1,048 Views
Ok - remove /iface:cvf and it works. Reported as issue DPD200158540.
0 Kudos
rwg
Novice
1,048 Views
I'm glad to hear that it was not again my stupidity. Thank you!
0 Kudos
Steven_L_Intel1
Employee
1,048 Views
This has been fixed in our sources. I'm not exactly sure of the timing - the fix might be in Update 2 (out in a couple of weeks) but if not, Update 3.
0 Kudos
Reply