Heading - PowerPoint PPT Presentation

About This Presentation
Title:

Heading

Description:

fastcall first two parameters are passed through CPU registers, other from right to left. ... Compile *.f files into a set of *.obj files (gcc.exe) ... – PowerPoint PPT presentation

Number of Views:18
Avg rating:3.0/5.0
Slides: 32
Provided by: ker149
Category:
Tags: compile | heading

less

Transcript and Presenter's Notes

Title: Heading


1
(No Transcript)
2
Contents
  • Fortran benefits and drawbacks
  • 2. Means of utilizing Fortran sources
  • 3. How DLL works
  • 4. Improvements of the DLL
  • 5. Possible difficulties
  • 6. Practical applications
  • Conclusion

3
1 Fortran benefits and drawbacks
  • Benefits
  • Fortran is designed for high performance
    calculations
  • Huge amount of free distributed Fortran code
  • Drawbacks
  • Boring and troublesome coding
  • Low reliability
  • Bad implementation of API for GUI
  • Increased application development time in
    comparison with Delphi, C, C, Java.

4
1.1 Free Fortran sources and libraries
  • Netlib www.netlib.org
  • Collection of mathematical software, papers
    and databases. It holds about 150 libraries and
    most of them are Fortran sources. In particular
    LAPACK, FFTPACK and ODEPACK
  • GAMS www.gams.nist.gov
  • Cross index and repository of mathematical and
    statistical software (both free and commercial)
  • FLIB www.pnl.gov/berc/flib
  • Set of useful general purpose and numeric
    routines
  • SLEIGN www.math.niu.edu/zettl/SL2
  • Code for solving Sturm-Liouville problems

5
2 Means of utilizing Fortran sources
  • Compiling as a part of Fortran project
  • Translation to other language (manual or machine)
  • Linking with compiled modules (.obj or .lib
    files)
  • Importing routines from dynamic link libraries
    (DLL, SO)
  • Creating COM, .NET components, Java classes

6
2.1 Translation to other language
  • Benefits
  • Simplicity of use
  • Full compatibility with destination language
  • Drawbacks
  • Possible loss of performance and/or precision
  • Great increase of development time for manual
    translation
  • Low translation reliability

7
2.2 Linking with compiled modules
  • Benefits
  • Fortran code is left untouched (reliability
    increase)
  • High performance and precision
  • Drawbacks
  • Complete incompatibility with languages being
    unable to perform linking with compiled modules
    (Java, Visual Basic, .NET managed code)
  • Possible incompatibility with destination
    language. (External references in .lib or .obj
    files may have no corresponding function
    implementations in destination language
    libraries.)

8
2.3 Problem of external references
  • Duplicate reference
  • Unresolved reference
  • Function incompatibility
  • Memory managers incompatibility

9
2.4 Dynamic link libraries (DLL)
  • Benefits
  • Fortran code is left untouched (reliability
    increase)
  • High performance and precision
  • High level of compatibility since Fortran code is
    fully compiled and linked
  • Drawbacks
  • High risk of runtime errors, due to unreliable
    mechanism of dynamic linking (no check of call
    correctness could be performed).
  • Complexity of implementation

10
2.5 COM or .NET components
  • Benefits
  • All DLL benefits
  • Interfaces eliminate risk of incorrect calls
  • Drawbacks
  • Programming language must have support of object
    model
  • Implementation is much more complex than the
    DLLs
  • User must be an expert in object oriented
    programming

11
3 How DLL works
Source code (.f)
Compiler
Object files (.obj)
Definitions file (.def)
external Object files (.obj) Libraries (.lib)
Linker
Executable module (.dll)
Static link library (.lib)
The resulting DLL file is executable code that is
fully independent of .f, .obj, .lib
files. Definitions file is used to define
functions that DLL will export. Pointers to these
functions could be obtained by means of OS at run
time.
12
3.1 Explicit and implicit linking
Explicit linking is performed by programmer.
Pointers to imported functions are obtained by
means of OS API. (LoadLibrary and GetProcAddress
for Windows OS). Implicit linking is more easy in
use. Import library is used to tell linker to add
import section to executable. When executable is
started OS examines this section, loads all
required DLLs and binds imported functions. Main
restriction is that DLL must be accessible to OS
at load time.
13
3.2 Sample code
The result will be the Sqr.dll file which
contains one export function with name sqr and
ordinal number 1
Fortran REAL FUNCTION S(X) REAL X
S XXEND
Definitions file LIBRARY SQREXPORTS
sqr s _at_1
DLL (sqr.dll)
Import library (sqr.lib)
C, implicit linking(some .h file)
extern C double _cdecl Sqr(double X)(.cpp
file) double Y Sqr(10)
It is assumed that DLL was created by GCC Fortran
for Windows (MiniGW). All parameters to functions
are passed by reference. cdecl calling
convention is used.
C, exlpicit linkingtypedef double (_cdecl
LPFNSQR)(double X) HANDLE hLib
LoadLibrary(Sqr.dll)LPFNSQR lpSQR
GetProcAddress(hLib, Sqr)double Y lpSQR(10)
14
3.3 Calling conventions
  • It is necessary to know calling convention that
    was used by compiler for exported functions.
  • stdcall parameters are pushed on the stack from
    right to left. Stack is freed by invoked
    procedure.
  • cdecl parameters are pushed right to left,
    stack is freed by caller.
  • fastcall first two parameters are passed
    through CPU registers, other from right to left.
  • safecall is used in Delphi for correct handling
    of raised exceptions. Analogue of stdcall.

15
4 Improvements of simple DLL
  • Problem
  • Too many parameters in calls to Fortran functions
  • Complex Fortran functions interface
  • Temporary memory buffers
  • Extended parameter check
  • Solution is to create another DLL that will act
    as intermediate or shell DLL between programmer
    and Fortran DLL. It allows to simplify and
    improve initial Fortran interface.

16
4.1 Core DLL
  • This section describes a way of creating DLL
    from Fortran sources using GCC (MiniGW).
  • Compile .f files into a set of .obj files
    (gcc.exe)
  • Create .lib file from .obj files (ar.exe)
  • Use dlltool.exe to list all function names that
    could be found in .lib file into .def file
  • Modify .def file to export only necessary
    functions
  • Use dllwrap.exe to create .dll file from .lib
    using .def
  • Third party tool could be used to create import
    library from .def file (something like lib.exe
    from free Microsoft Visual C compiler)

17
4.2 Shell DLL
  • Shell DLL could be written in any language
    capable of producing DLLs. Following
    recommendations could be taken in account
  • Try to validate as maximum input parameters as
    possible,but not to get performance penalty
  • Try to minimize count of parameters that are
    passed to shell DLL functions. Other solution is
    to create simple and complex versions of one
    Fortran function
  • Perform thorough check of count and types of
    parameters and calling conventions used

18
4.3 Full dynamic link scheme
Sources (.f,.obj,.def)
Sources are used only once, when core DLL was
created It is easier to use implicit linking to
connect Core and Shell DLLs User program may use
any way of linking with Shell DLL
Use once
Core DLL
Implicit linking
Shell DLL
User program
19
5 Possible difficulties
  • Even by using a complex scheme some difficulties
    with no simple solution may arise
  • Extended double precision is used in Fortran
    code, but Shell DLL programming language does not
    support this data type (Fortran - C - any
    language)
  • User of Shell DLL is expected to use extended
    double precision data type (Fortran - C -
    Delphi)
  • Unexpected losses in performance (up to 2 times),
    when working with double precision arrays
  • Callback functions used by Fortran code

20
5.1 Floating point values storage types
  • Floating point types supported by numerical
    coprocessor
  • Float32 (C float, Delphi single, Fortran
    REAL4)
  • Float64 (C and Delphi double, Fortran
    REAL8)
  • Float80 (Delphi Extended, native coprocessor
    type)
  • When calling DLL routines from languages that
    support Float80 data type (e.g. Delphi) the
    conversion to Float64 or Float32 must be
    performed
  • Possible way for fast conversionis to use 80x87
    coprocessor instructionsFLD - to push value on
    to FPU stackFSTP to pop and store value to
    memory

Sample assembler codelp fld TBYTE PTR esi
fstp QWORD PTR edi add esi, 10 add edi, 8
dec ecx jnz lp
Following assumptions are madeESI holds a
pointer to source array of Float80 valuesEDI
points to destination array of Float64 valuesECX
holds number of elements for conversionSample
code converts array of Float80 values into array
of Float64 values
21
5.2 Alignment importance
Example of misaligned array of Float64 values
Memory block, starting at 8 byte boundary, each
square is 4 bytes long
F11 F12 F21 F22 F31 F32 F41 F42 F51 F52 F61 F62 F71 F72 F81 F82 F91 F92
Memory
It representation in the processor cache (with 32
byte cache line)
F11 F12 F21 F22 F31 F32 F41
F42 F51 F52 F61 F62 F71 F72 F81
F82 F91 F92
Array elements F4 and F8 are located on 2 cache
lines.
CPU cache
Example of aligned array of Float64 values
F11 F12 F21 F22 F31 F32 F41 F42 F51 F52 F61 F62 F71 F72 F81 F82 F91 F92
Memory
F11 F12 F21 F22 F31 F32 F41 F42
F51 F52 F61 F62 F71 F72 F81 F82
F91 F92
CPU cache
Each element fit only one cache line
Misalignment of Float64 array results in
performance drop because processor is forced to
work with 2 cache lines when accessing each 4th
array element. Insure that your Float64 data is
aligned at least to 8 byte boundary
22
5.3 Callback functions
Fortran subroutine may expect one its parameters
to be a pointer to user-defined callback
function. Problem occurs when callback function
implementation in the destination language could
not be made as it is declared in Fortran.
Fortran declaration of ODE Solver from
ODEPACK SUBROUTINE DLSODE (F, NEQ, Y, T, TOUT,
ITOL, RTOL, ATOL, ITASK,
ISTATE, IOPT, RWORK, LRW, IWORK,
LIW, JAC, MF) EXTERNAL F, JAC INTEGER
NEQ, ITOL, ITASK, ISTATE, IOPT, LRW, IWORK, LIW,
MF DOUBLE PRECISION Y, T, TOUT, RTOL, ATOL,
RWORK DIMENSION NEQ(), Y(), RTOL(),
ATOL(), RWORK(LRW), IWORK(LIW) Problem F is
assumed to be ODE definition function with
following declarationSUBROUTINE F (NEQ, T, Y,
YDOT) INTEGER NEQ DOUBLE PRECISION
T, Y(), YDOT()
Solution is to write Shell DLL on the language
that allows callback definition compatible with
Fortran.
23
6 Practical applications
Described method of linking was applied to
functions from LAPACK, FFTPACK, SLEIGN2 and
ODEPACK packages.
Here is declaration of Fortran function from
LAPACK and corresponding C function from shell
DLL.
Fortran declaration of ODE Solver from
ODEPACK SUBROUTINE DSYEVX(JOBZ, RANGE, UPLO, N,
A, LDA, VL, VU, IL, IU,
ABSTOL, M, W, Z, LDZ, WORK, LWORK,
IWORK,
IFAIL, INFO) CHARACTER JOBZ,
RANGE, UPLO INTEGER IL, INFO,
IU, LDA, LDZ, LWORK, M, N DOUBLE PRECISION
ABSTOL, VL, VU INTEGER
IFAIL( ), IWORK( ) DOUBLE PRECISION
A( LDA, ), W( ), WORK( ), Z( LDZ, ) C
declaration of function exported from Shell
DLL long _stdcall EV_DC_X_EX( long n, long type,
double m, double eval,
double evect, double
abstol, double min,
double max, long lpevcnt)
24
6.1 BARSIC
  • BARSIC (Business And
    Research Scientific Interactive
    Calculator) is new programming language for
    education, research and
    business. It is a powerful tool
    to develop applications for mathematical
    simulation, data processing and visualization,
    numerical calculations and computer animation.
    Main field of BARSIC applications is Physics and
    Mathematical Physics.
  • BARSIC has built-in classes for easy work with
    files, 2d- and 3d-graphics, computer animation,
    numerical mathematical calculations, text
    processing, multimedia. Visual design of
    application user's interface is similar to Visual
    BASIC and Delphi.
  • BARSIC is a powerful tool to develop applications
    for small computerized installations, especially
    for computerized laboratory experiment
  • Home page http//www.niif.spbu.ru/monakhov

25
6.2 High performance BARSIC
High performance calculations that involve huge
amount of data (e.g. matrix operations, frequency
analysis) could not be performed directly in
BARSIC.
It is obvious that calculations of such kind are
to be implemented in compilative programming
language like C or Delphi.
DLL linking was used to extend BARSIC
functionality with matrix operations, Fourier
transform, Sturm-Liouville problem solver and ODE
solver by reusing free distributed Fortran code
of LAPACK, FFTPACK, SLEIGN2 and ODEPACK libraries.
26
6.3 BARSIC interface example (FFT)
27
6.4 BARSIC interface (LAPACK)
28
6.5 LAPACK SVD benchmark
29
6.6 LAPACK benchmark (performance)
30
Conclusion
Technique described above
  • keeps performance level of the original code
  • is the safest way to use Fortran code
  • provides maximum compatibility when compiling
    Fortran
  • allows linking with most of programming languages
  • is the easiest way to enhance program
    functionality with powerful numerical algorithms

31
Acknowledgments
  • Professor S. Slavyanov
  • Associate professor V. Monakhov
Write a Comment
User Comments (0)
About PowerShow.com