Quantcast
Channel: Intel® Software - Intel® Visual Fortran Compiler for Windows*
Viewing all articles
Browse latest Browse all 5691

Creating a thread-safe version of legacy F77 code containing common blocks

$
0
0

Consider the situation where I have a set of Fortran 77 subroutines with one driver subroutine that represents a numerical method. These subroutines have many common blocks that share data and variables between them. I would like to make the common block information thread-safe. Is that possible with the following procedure:

(1) create a module subprogram in F90 but in a fixed source format file (*.for).
(2) paste the F77 subroutines in the module in the "contains" section.
(3) declare all the subroutines to be private
(4) write a public wrapper subroutine within the module that calls the driver.

An explanatory code is below (the calculations are only an example):

MODULE THREAD_SAFE_FUNCTION

      PRIVATE
      
      TYPE, PUBLIC :: MYVAR
      
      INTEGER :: N
      REAL :: S
      REAL, DIMENSION(:), ALLOCATABLE :: P

      CONTAINS

      PROCEDURE, PASS(SELF) :: DRIVER
      
      END TYPE MYVAR

      CONTAINS

      SUBROUTINE DRIVER(SELF,N,Y,S)
      CLASS(MYVAR) :: SELF
      INTEGER :: N
      REAL :: S,Y(N)
      SELF%N = N
      ALLOCATE(SELF%P(N))
      SELF%P = Y
      CALL TASK1(N,SELF%P,S)
      SELF%P=SELF%P**2
      END SUBROUTINE DRIVER

      SUBROUTINE TASK1(N,Y,S)
      COMMON /COM1/A,B,C  ! WANT COM1 AND COM2 TO BE THREAD-SAFE
      COMMON /COM2/Z(10)
      INTEGER N
      REAL S,Y(N)
      INTEGER I
      A=1
      B=2
      C=3
      DO I=1,N
         Z(I)=I
      ENDDO
      CALL TASK2(N,Y,S)
      RETURN
      END

      SUBROUTINE TASK2(N,X,S)
      COMMON /COM1/A,B,C
      COMMON /COM2/Z(10)
      INTEGER N
      REAL S,X(N,3)
      INTEGER I
C     SOME CALCULATIONS HERE
      A=2*A
      S=0.0D0
      DO I=1,N
         S=S+Z(I)**2
      ENDDO
      S=SQRT(S)
      RETURN
      END

      END MODULE THREAD_SAFE_FUNCTION

 


Viewing all articles
Browse latest Browse all 5691

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>