31
1

0 1 Contents 1.Fortran benefits and drawbacks 2. Means of utilizing Fortran sources 3. How DLL works 4. Improvements of the DLL 5. Possible difficulties

Embed Size (px)

Citation preview

Page 1: 0 1 Contents 1.Fortran benefits and drawbacks 2. Means of utilizing Fortran sources 3. How DLL works 4. Improvements of the DLL 5. Possible difficulties

1

Page 2: 0 1 Contents 1.Fortran benefits and drawbacks 2. Means of utilizing Fortran sources 3. How DLL works 4. Improvements of the DLL 5. Possible difficulties

2

Contents

1.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

Page 3: 0 1 Contents 1.Fortran benefits and drawbacks 2. Means of utilizing Fortran sources 3. How DLL works 4. Improvements of the DLL 5. Possible difficulties

3

1 Fortran benefits and drawbacksBenefits:

• 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.

Page 4: 0 1 Contents 1.Fortran benefits and drawbacks 2. Means of utilizing Fortran sources 3. How DLL works 4. Improvements of the DLL 5. Possible difficulties

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/flibSet of useful general purpose and numeric routines

• SLEIGN – www.math.niu.edu/~zettl/SL2 Code for solving Sturm-Liouville problems

Page 5: 0 1 Contents 1.Fortran benefits and drawbacks 2. Means of utilizing Fortran sources 3. How DLL works 4. Improvements of the DLL 5. Possible difficulties

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

Page 6: 0 1 Contents 1.Fortran benefits and drawbacks 2. Means of utilizing Fortran sources 3. How DLL works 4. Improvements of the DLL 5. Possible difficulties

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

Page 7: 0 1 Contents 1.Fortran benefits and drawbacks 2. Means of utilizing Fortran sources 3. How DLL works 4. Improvements of the DLL 5. Possible difficulties

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.)

Page 8: 0 1 Contents 1.Fortran benefits and drawbacks 2. Means of utilizing Fortran sources 3. How DLL works 4. Improvements of the DLL 5. Possible difficulties

8

• Duplicate reference

• Unresolved reference

• Function incompatibility

• Memory managers incompatibility

2.3 Problem of external references

Compiled .obj filesFortranstandard libraries

User libraries

Source .f files

Fortran compiler

Linker

Executable

Standard build process

Other languagestandard libraries

Source .? files

? compiler

Compiled .obj files

Fortran Other language

Page 9: 0 1 Contents 1.Fortran benefits and drawbacks 2. Means of utilizing Fortran sources 3. How DLL works 4. Improvements of the DLL 5. Possible difficulties

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

Page 10: 0 1 Contents 1.Fortran benefits and drawbacks 2. Means of utilizing Fortran sources 3. How DLL works 4. Improvements of the DLL 5. Possible difficulties

10

2.5 COM or .NET componentsBenefits:

• 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 DLL’s

• User must be an expert in object oriented programming

Page 11: 0 1 Contents 1.Fortran benefits and drawbacks 2. Means of utilizing Fortran sources 3. How DLL works 4. Improvements of the DLL 5. Possible difficulties

11

3 How DLL works

Source code (*.f) Compiler

Object files (*.obj)

Linkerexternal

Object files (*.obj)Libraries (*.lib)

Definitions file (*.def)

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.

Page 12: 0 1 Contents 1.Fortran benefits and drawbacks 2. Means of utilizing Fortran sources 3. How DLL works 4. Improvements of the DLL 5. Possible difficulties

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.

Page 13: 0 1 Contents 1.Fortran benefits and drawbacks 2. Means of utilizing Fortran sources 3. How DLL works 4. Improvements of the DLL 5. Possible difficulties

13

3.2 Sample codeFortran: REAL FUNCTION S(X) REAL X S = X*X;END

Definitions file: LIBRARY SQREXPORTS sqr = s @1

The result will be the ‘Sqr.dll’ file which contains one export function with name ‘sqr’ and ordinal number ‘1’

C++, exlpicit linking:typedef double (_cdecl *LPFNSQR)(double* X); …HANDLE hLib = LoadLibrary(“Sqr.dll”)LPFNSQR lpSQR = GetProcAddress(hLib, “Sqr”);double Y = lpSQR(10);

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.

DLL (sqr.dll) Import library (sqr.lib)

Page 14: 0 1 Contents 1.Fortran benefits and drawbacks 2. Means of utilizing Fortran sources 3. How DLL works 4. Improvements of the DLL 5. Possible difficulties

14

3.3 Calling conventionsIt 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.

Page 15: 0 1 Contents 1.Fortran benefits and drawbacks 2. Means of utilizing Fortran sources 3. How DLL works 4. Improvements of the DLL 5. Possible difficulties

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.

Page 16: 0 1 Contents 1.Fortran benefits and drawbacks 2. Means of utilizing Fortran sources 3. How DLL works 4. Improvements of the DLL 5. Possible difficulties

16

4.1 Core DLL

This section describes a way of creating DLL from Fortran sources using GCC (MiniGW).

1) Compile *.f files into a set of *.obj files (gcc.exe)

2) Create *.lib file from *.obj files (ar.exe)

3) Use dlltool.exe to list all function names that could be found in *.lib file into *.def file

4) Modify *.def file to export only necessary functions

5) Use dllwrap.exe to create *.dll file from *.lib using *.def

6) Third party tool could be used to create import library from *.def file (something like lib.exe from free Microsoft Visual C++ compiler)

Page 17: 0 1 Contents 1.Fortran benefits and drawbacks 2. Means of utilizing Fortran sources 3. How DLL works 4. Improvements of the DLL 5. Possible difficulties

17

4.2 Shell DLLShell 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

Page 18: 0 1 Contents 1.Fortran benefits and drawbacks 2. Means of utilizing Fortran sources 3. How DLL works 4. Improvements of the DLL 5. Possible difficulties

18

4.3 Full dynamic link scheme

Core DLL

Shell DLL User program

Sources (*.f,*.obj,*.def)

Implicit linking

Use once

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

Page 19: 0 1 Contents 1.Fortran benefits and drawbacks 2. Means of utilizing Fortran sources 3. How DLL works 4. Improvements of the DLL 5. Possible difficulties

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

Page 20: 0 1 Contents 1.Fortran benefits and drawbacks 2. Means of utilizing Fortran sources 3. How DLL works 4. Improvements of the DLL 5. Possible difficulties

20

5.1 Floating point values storage typesFloating point types supported by numerical coprocessor:

• Float32 (C++ ‘float’, Delphi ‘single’, Fortran ‘REAL*4’)• Float64 (C++ and Delphi ‘double’, Fortran ‘REAL*8’)• 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 instructions:FLD - to push value on to FPU stackFSTP – to pop and store value to memory

Sample assembler code:lp: fld TBYTE PTR [esi] fstp QWORD PTR [edi] add esi, 10 add edi, 8 dec ecx jnz lp

Following assumptions are made:ESI 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

Page 21: 0 1 Contents 1.Fortran benefits and drawbacks 2. Means of utilizing Fortran sources 3. How DLL works 4. Improvements of the DLL 5. Possible difficulties

21

5.2 Alignment importance

F11 F12 F21 F22 F31 F32 F41 F42 F51 F52 F61 F62 F71 F72 F81 F82 F91 F92 …

Example of misaligned array of Float64 values:Memory block, starting at 8 byte boundary, each square is 4 bytes long:

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.

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 … …

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

Memory

CPU cache

Memory

F11 F12 F21 F22 F31 F32 F41 F42

F51 F52 F61 F62 F71 F72 F81 F82

F91 F92 …

CPU cache

Page 22: 0 1 Contents 1.Fortran benefits and drawbacks 2. Means of utilizing Fortran sources 3. How DLL works 4. Improvements of the DLL 5. Possible difficulties

22

5.3 Callback functionsFortran subroutine may expect one it’s 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 declaration:SUBROUTINE 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.

Page 23: 0 1 Contents 1.Fortran benefits and drawbacks 2. Means of utilizing Fortran sources 3. How DLL works 4. Improvements of the DLL 5. Possible difficulties

23

6 Practical applicationsDescribed 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);

Page 24: 0 1 Contents 1.Fortran benefits and drawbacks 2. Means of utilizing Fortran sources 3. How DLL works 4. Improvements of the DLL 5. Possible difficulties

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

Page 25: 0 1 Contents 1.Fortran benefits and drawbacks 2. Means of utilizing Fortran sources 3. How DLL works 4. Improvements of the DLL 5. Possible difficulties

25

6.2 High performance BARSICHigh 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.

Page 26: 0 1 Contents 1.Fortran benefits and drawbacks 2. Means of utilizing Fortran sources 3. How DLL works 4. Improvements of the DLL 5. Possible difficulties

26

6.3 BARSIC interface example (FFT)

Page 27: 0 1 Contents 1.Fortran benefits and drawbacks 2. Means of utilizing Fortran sources 3. How DLL works 4. Improvements of the DLL 5. Possible difficulties

27

6.4 BARSIC interface (LAPACK)

Page 28: 0 1 Contents 1.Fortran benefits and drawbacks 2. Means of utilizing Fortran sources 3. How DLL works 4. Improvements of the DLL 5. Possible difficulties

28

6.5 LAPACK SVD benchmark

Page 29: 0 1 Contents 1.Fortran benefits and drawbacks 2. Means of utilizing Fortran sources 3. How DLL works 4. Improvements of the DLL 5. Possible difficulties

29

6.6 LAPACK benchmark (performance)

Page 30: 0 1 Contents 1.Fortran benefits and drawbacks 2. Means of utilizing Fortran sources 3. How DLL works 4. Improvements of the DLL 5. Possible difficulties

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

Page 31: 0 1 Contents 1.Fortran benefits and drawbacks 2. Means of utilizing Fortran sources 3. How DLL works 4. Improvements of the DLL 5. Possible difficulties

31

Acknowledgments

• Professor S. Slavyanov

• Associate professor V. Monakhov