Open yanyh15 opened 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
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 to
kmpc_fork_call'
collect2: error: ld returned 1 exit status
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).
Fortran_to_C - It could be a good reference but it can't be used as our project for several reasons:
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.
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.
It does not support OMP feature, and it can not recognized OMP.
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:
/ 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; }
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
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.
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: