Wrapping Fortran libraries Arjen Markus June 2010.

Slides:



Advertisements
Similar presentations
Computational Physics Linear Algebra Dr. Guy Tel-Zur Sunset in Caruaru by Jaime JaimeJunior. publicdomainpictures.netVersion , 14:00.
Advertisements

879 CISC Parallel Computation High Performance Fortran (HPF) Ibrahim Halil Saruhan Although the [Fortran] group broke new ground …
Chapter 7 Introduction to Procedures. So far, all programs written in such way that all subtasks are integrated in one single large program. There is.
Chapter 2, Linear Systems, Mainly LU Decomposition.
Functions Most useful programs are much larger than the programs that we have considered so far. To make large programs manageable, programmers modularize.
Arrays An array is a sequence of objects all of which have the same type. The objects are called the elements of the array and are numbered consecutively.
ECIV 301 Programming & Graphics Numerical Methods for Engineers Lecture 4 Programming and Software EXCEL and MathCAD.
Fortran 9x HTML version. New F90 features Free source form Modules User-defined data types and operators Generic user-defined procedures Interface blocks.
1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 8 focuses on: array declaration and use passing arrays and array.
 2006 Pearson Education, Inc. All rights reserved Arrays.
Chapter Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
NEW APPROACH TO CALCULATION OF RANGE OF POLYNOMIALS USING BERNSTEIN FORMS.
C++ for Engineers and Scientists Third Edition
C++ for Engineers and Scientists Third Edition
Chapter 9 Introduction to Procedures Dr. Ali Can Takinacı İstanbul Technical University Faculty of Naval Architecture and Ocean Engineering İstanbul -
Introduction to Array The fundamental unit of data in any MATLAB program is the array. 1. An array is a collection of data values organized into rows and.
FunctionsFunctions Systems Programming Concepts. Functions   Simple Function Example   Function Prototype and Declaration   Math Library Functions.
AceGen and AceFEM packages
Fortran- Subprograms Chapters 6, 7 in your Fortran book.
Fruitful functions. Return values The built-in functions we have used, such as abs, pow, int, max, and range, have produced results. Calling each of these.
Engineering Analysis ENG 3420 Fall 2009 Dan C. Marinescu Office: HEC 439 B Office hours: Tu-Th 11:00-12:00.
Chapter 7: Arrays. In this chapter, you will learn about: One-dimensional arrays Array initialization Declaring and processing two-dimensional arrays.
Multi-Dimensional Arrays
 2006 Pearson Education, Inc. All rights reserved Arrays.
A First Book of ANSI C Fourth Edition
1 Semantic Analysis Aaron Bloomfield CS 415 Fall 2005.
1 Chapter 4: Selection Structures. In this chapter, you will learn about: – Selection criteria – The if-else statement – Nested if statements – The switch.
Computational Methods of Scientific Programming Lecturers Thomas A Herring, Room A, Chris Hill, Room ,
CMPS 1371 Introduction to Computing for Engineers MATRICES.
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Matlab for Engineers Manipulating Matlab Matrices Chapter 4.
Methods in Java. Program Modules in Java  Java programs are written by combining new methods and classes with predefined methods in the Java Application.
C++ for Engineers and Scientists Second Edition Chapter 11 Arrays.
Data TypestMyn1 Data Types The type of a variable is not set by the programmer; rather, it is decided at runtime by PHP depending on the context in which.
More About Objects and Methods Chapter 5. Outline Programming with Methods Static Methods and Static Variables Designing Methods Overloading Constructors.
Covenant College November 27, Laura Broussard, Ph.D. Professor COS 131: Computing for Engineers Chapter 5: Functions.
Earth System Modeling Framework Python Interface (ESMP) October 2011 Ryan O’Kuinghttons Robert Oehmke Cecelia DeLuca.
ME 142 Engineering Computation I Using Subroutines Effectively.
2 Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
Linear Algebra Libraries: BLAS, LAPACK, ScaLAPACK, PLASMA, MAGMA
FORTRAN History. FORTRAN - Interesting Facts n FORTRAN is the oldest Language actively in use today. n FORTRAN is still used for new software development.
C Wrapper to LAPACK by Rémi Delmas supervised by Julien Langou.
ME 142 Engineering Computation I Using Subroutines Effectively.
Methods Methods are how we implement actions – actions that objects can do, or actions that can be done to objects. In Alice, we have methods such as move,
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
INTRODUCTION TO MATLAB DAVID COOPER SUMMER Course Layout SundayMondayTuesdayWednesdayThursdayFridaySaturday 67 Intro 89 Scripts 1011 Work
C++ for Engineers and Scientists Second Edition
Chapter 5 Defining Classes II Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
CSci 162 Lecture 8 Martin van Bommel. Large-scale Programming Up to now we have been writing relatively short programs to solve simple problem Want to.
Arrays Chapter 7. MIS Object Oriented Systems Arrays UTD, SOM 2 Objectives Nature and purpose of an array Using arrays in Java programs Methods.
Slava Bunichev, Moscow State University in collaboration with A.Kryukov.
Multidimensional Arrays Computer and Programming.
Chapter 1: Introduction to Computers and Programming.
Arrays Chap. 9 Storing Collections of Values 1. Introductory Example Problem: Teachers need to be able to compute a variety of grading statistics for.
Arrays An array is a sequence of objects all of which have the same type. The objects are called the elements of the array and are numbered consecutively.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the structure of a C-language program. ❏ To write your first C.
C++ for Engineers and Scientists Second Edition Chapter 4 Selection Structures.
mathemaTcl – a collection of mathematical extensions
More About Objects and Methods
Chapter 2, Linear Systems, Mainly LU Decomposition
APPENDIX a WRITING SUBROUTINES IN C
MatLab Programming By Kishan Kathiriya.
Linear Systems, Mainly LU Decomposition
Wrapping Fortran libraries
User-Defined Functions
Subprograms and Programmer Defined Data Type
7 Arrays.
ENERGY 211 / CME 211 Lecture 27 November 21, 2008.
Presentation transcript:

Wrapping Fortran libraries Arjen Markus June 2010

Wrapping Fortran libraries Examples of numerical libraries Designing the interface Generating the wrapper code Further developments

A strategy Wrapping such libraries makes this functionality available to Tcl programmers It also makes Tcl’s flexibility available to engineers and scientists

Examples of numerical libraries LAPACK: Linear algebra problems Specfunc: Special mathematical functions MINPACK: least-squares minimisation Evaluating multidimensional integrals (Stroud) FFTW: Fourier transforms

Designing the interfaces Some typical examples Good fit to Tcl? SUBROUTINE AIRYA(X,AI,BI,AD,BD) C C ====================================================== C Purpose: Compute Airy functions and their derivatives C Input: x --- Argument of Airy function C Output: AI --- Ai(x) C BI --- Bi(x) C AD --- Ai'(x) C BD --- Bi'(x) C Routine called: C AJYIK for computing Jv(x), Yv(x), Iv(x) and C Kv(x) with v=1/3 and 2/3 C ====================================================== C IMPLICIT DOUBLE PRECISION (A-H,O-Z)

Interface: fit to Tcl? - part 1 Option 1: proc airy {x ai_ bi_ ad_ bd_} { upvar 1 $ai_ ai upvar 1 $bi_ bi upvar 1 $ad_ ad upvar 1 $bd_ bd... } Pass variable names – rather unusual

Interface: fit to Tcl? – part 2 Option 2: Return a list of values proc airy {x} {... return [list $ai $bi $ad $bd] } Option 3: Separate routines proc airyA {x} { proc airyB {x} return $ai return $bi } }

LAPACK routines – part 1 SUBROUTINE DBDSDC( UPLO, COMPQ, N, D, E, U, LDU, VT, LDVT, Q, IQ, $ WORK, IWORK, INFO ) * * -- LAPACK routine (version 3.2) -- * -- LAPACK is a software package provided by Univ. of Tennessee, -- * -- Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd..- - * November 2006 * *.. Scalar Arguments.. CHARACTER COMPQ, UPLO INTEGER INFO, LDU, LDVT, N *.. *.. Array Arguments.. INTEGER IQ( * ), IWORK( * ) DOUBLE PRECISION D( * ), E( * ), Q( * ), U( LDU, * ), $ VT( LDVT, * ), WORK( * ) *.. *

LAPACK routines – part 2 Long argument list Arguments that are array dimensions Arguments that provide workspace Multiple output arguments Needed in Tcl? Not if we use lists to store the data

LAPACK: final interface Hiding the unnecessary arguments: proc dbdsdc { uplo compq d e } {... return [list $dnew $u $vt $q $iq $info] ;# Leaving out "e" }

Data structures Main types of data: Scalars (mostly floating-point and integer) One-dimensional arrays (vectors) Two-dimensional arrays (matrices) Currently: mapped onto Tcl lists and nested lists

Generating the wrapper code Distinguish the various roles of the arguments: Input data (scalar/array) Output arguments (scalar/array) Sizes of arrays Workspace Options Function names

Handling the roles Boilerplate code (integer array v): Declaration: long *v; int size__v; Initialisation: if ( WrapCopyIntListToArray( interp, objv[1], &v, &size__v ) != TCL_OK ) { Tcl_SetResult( interp, “Argument 1 must be a list of integers”, NULL ); return TCL_ERROR; } Clean-up: ckfree((char *)v);

Definition of arguments Fortran routine (arrays x and w are returned): subroutine lagzo( n, x, w ) integer n double precision x(n), w(n)... end Tcl wrapper: Wrapfort::fproc ::Specfunc::laguerreZeros lagzo { integer n input double-array x {allocate n} double-array w {allocate n} code {} { lagzo( &n, x, w ); … additional code: return arrays x and w … }

Technical issues Role of arguments can not be deduced automatically Some knowledge of C and Fortran and interfacing the two is required Returning more than one result (as a list) requires extra code – not handled automatically yet

LAPACK - wrapping Hundreds of routines Of most interest: the “driver” routines Arguments are well described – automation possible Some handcrafting still required (to make the interface more Tcl-like)

MINPACK: user-defined functions Finding zeros for a vector-valued function: subroutine hybrd1(fcn,n,x,fvec,tol,info,wa,lwa) integer n,info,lwa double precision tol double precision x(n),fvec(n),wa(lwa) external fcn... end The function is implemented via a user-defined subroutine: subroutine fcn(n,x,fvec,iflag) integer n,iflag double precision x(n),fvec(n) calculate the functions at x and return this vector in fvec return end

MINPACK – part 2 The corresponding Tcl procedure: proc fcn {x} {... if {... $x too far away... } { return –code error “Unacceptable solution” } else {... return $fvec ;# Vector-valued function }

MINPACK - wrapping Definition via Wrapfort: subroutine fcn(n,x,fvec,iflag) Wrapfort::fexternal fcn { fortran { integer n input double-array x {input n} double-array fvec {output n} integer iflag output } toproc { x input fvec result } onerror { *iflag = -1; }

More about Ftcl and Wrapfort Ftcl is a project on SourceForge to combine Tcl and Fortran Wrapfort is part of that project, focusing on generating interfaces You can avoid all C programming

Further developments Performance: Tcl lists may not be the best representation in all cases Wrapping C libraries (akin to Critcl) Combining whole programs – develop a strategy