passlab / rexompiler

REX OpenMP Compiler
https://passlab.github.io/rexompiler/
Other
1 stars 1 forks source link

Fortran support with source-to-source #67

Open yanyh15 opened 3 years ago

yanyh15 commented 3 years ago
  1. XOMP use weak symbol
  2. How KMP support fortran. Use Fortran hello world OpenMP program to demonstrate.
World-Worst-Detector commented 3 years ago

For a simple Fortran code:

      PROGRAM HELLO
!$OMP PARALLEL
      PRINT *, 'Hello World!'
c$omp end parallel 
      PRINT *, 'the end'
      END

We have the transformed code like this:

      PROGRAM HELLO
      use iso_c_binding
      external :: outlined_function
!      external :: KMPC_fork_call
!      CALL KMPC_fork_call(outlined_function)
      interface
       SUBROUTINE KMPC_fork_call(loc,arg,func) BIND(C)
!        IMPLICIT NONE
        use iso_c_binding
        INTEGER :: loc, arg
        type( c_funptr), value :: func
       END SUBROUTINE KMPC_fork_call
      end interface
      TYPE(C_FUNPTR) :: cproc
      cproc = C_FUNLOC (outlined_function)
      CALL KMPC_fork_call(0,1,cproc)
      PRINT *, 'the end'
      END PROGRAM

!      SUBROUTINE outlined_function(out_argv)
!      include "omp_lib.h"
!      INTEGER :: out_argv
!      PRINT *, 'Hello World!'
!      END SUBROUTINE
      SUBROUTINE outlined_function(global_id,bound_id)
                IMPLICIT NONE
                INTEGER, POINTER, INTENT(IN)::global_id, bound_id
                PRINT *, 'Hello from process:'
      END SUBROUTINE outlined_function

!      SUBROUTINE KMPC_fork_call(outlined_function)
!        external :: outlined_function
!        external :: kmpc_fork_call
!        BIND(C) KMPC_fork_call, kmpc_fork_call
!        CALL kmpc_fork_call(0,1,,outlined_function)
!      END SUBROUTINE
World-Worst-Detector commented 3 years ago

Using Command line: gfortran rex_hello.f -L/opt/llvm/llvm-10.x-install/lib

Can not link to the library, and find kmpc_fork_call /tmp/ccOnFd4E.o: In function MAIN__': rex_hello.f:(.text+0x2c): undefined reference tokmpc_fork_call' collect2: error: ld returned 1 exit status

World-Worst-Detector commented 3 years ago

1.call KMPC(without ) at Fortran 1.add new c function which only has a KMPC function and this function calls kmpc(like a mini xomp function).

World-Worst-Detector commented 3 years ago

Fortran_to_C - It could be a good reference but it can't be used as our project for several reasons:

  1. Build based on rose. This tool was built based on rose. It has to work with rose since it use serval rose function from rose library such as rose_assert(), sageinterface and so on.

  2. It only supports serval simple transformation such as simple program, function declaration, if and so on. It still has many functions wait to implement loop, block, I/O(print read write) and so on.

  3. It does not support OMP feature, and it can not recognized OMP.

  4. It is an unfinished and out-date project. I tried to run and test it. It can not work with current ROSE(V0.11.15.2). It generated a rose parse error message that input language is not same as output language or undefined the input language.(Fortran_to_C: /rose/rose_src/src/backend/unparser/unparser.C:915: void Unparser::unparseFile(SgSourceFile, SgUnparse_Info&, SgScopeStatement): Assertion `file->get_inputLanguage() == file->get_outputLanguage()' failed.)

F2C - It is a pretty decent source to source language transformation tools. It is a pretty good reference tool. It is a standalone program. However, it's an old program which support Fortran 77. It may not support new feature for fortran 90 or 95. And it doesn't support any OMP feature and consider it as comment. I tested it with Hello.F and this is the output:

include "f2c.h"

/ Table of constant values /

static integer c9 = 9; static integer c1 = 1;

/ Main program / int MAIN__(void) { / Builtin functions / integer s_wsle(cilist ), do_lio(integer , integer , char , ftnlen), e_wsle(void);

/* Fortran I/O blocks */
static cilist io___1 = { 0, 6, 0, 0, 0 };
static cilist io___2 = { 0, 6, 0, 0, 0 };

/ $OMP PARALLEL / s_wsle(&io_1); do_lio(&c9, &c1, "Hello World!", (ftnlen)12); e_wsle(); / $omp end parallel / s_wsle(&io___2); do_lio(&c9, &c__1, "the end", (ftnlen)7); e_wsle(); return 0; } / MAIN__ /

/ Main program alias / int hello_ () { MAIN__ (); return 0; }

yanyh15 commented 3 years ago

Let us leave cross-language fortran-to-C transformation as a solution of shooting a fly with rocket rocket solution. It has value of cross-language translation though.

Solution to eliminate the __ prefix of the symbol used by Fortran

  1. script or patch to eliminate __ of those exported symbols that are used by the compiler transformation
  2. objcopy binary editing tool can change a symbol name in a binary program, which we can use to change those symbols with __ prefix.

call a C variadic function support kmp has a call __kmpc_fork_call that is a variadic function, search to see how to support calling a C variadic function in the Fortran transformed code for parallel directive.

World-Worst-Detector commented 3 years ago

There is no possible way to directly call a C variadic function in a standard way( https://www-mipl.jpl.nasa.gov/portguide/subsection3.4.1.html ). But I find out that there are several hack solutions:

  1. Define the C function with variable number parameters, then consider it as a string list in C. The reference link is: https://stackoverflow.com/a/19673446, http://stackoverflow.com/a/19171063/1162141 (I have tried some simple examples and It needs c program to handle the variable argument)
  2. Use optional arguments to allow different numbers of parameters to call in Fortran. Here is the link: https://stackoverflow.com/q/52099973, http://computer-programming-forum.com/49-fortran/b806991184b29812.html, and http://www.netlib.org/ccm/page/api/optional.html (this is a new feature in Fortran 90)
  3. Use C wrapper (https://community.intel.com/t5/Intel-Fortran-Compiler/Calling-vararg-functions-from-Fortran/td-p/749771 )
  4. Another example calls C variable variadic from Fortran, they consider 'vararg' as a list: https://forums.developer.nvidia.com/t/calling-varlist-c-routine-from-fortran-corrupted-stack/129921