Computational Science I - PowerPoint PPT Presentation

1 / 15
About This Presentation
Title:

Computational Science I

Description:

... out if the f77 (or g77) compiler appends an underscore to all function names on ... the issue as some Fortran compilers will append even more extra underscores... – PowerPoint PPT presentation

Number of Views:118
Avg rating:3.0/5.0
Slides: 16
Provided by: timwar1
Category:

less

Transcript and Presenter's Notes

Title: Computational Science I


1
Computational Science I
  • Lecture 12
  • CAAM 420, Fall 2004
  • Instructor Tim Warburton

2
Recall calling fortran subroutines from C
  • Last lecture we covered how to invoke a F77
    subroutine from C.
  • This is the recipe
  • 1) use nm to find out if the f77 (or g77)
    compiler appends an underscore to all function
    names on the Fortran side. (i.e. foo is saved as
    foo_ in the .o file)
  • 2) Fortran passes by pointer, so we need to send
    pointers to variables.
  • 3) Extra If an argument to a Fortran subroutine
    represents an array in Fortran, we need to send
    the pointer to the first entry in the array (not
    the pointer to pointer)
  • 4) Extra do not use _ in your Fortran function
    names, this may confuse the issue as some Fortran
    compilers will append even more extra underscores

3
C-gtF77 Example
  • Note
  • The _ after myfortranfn on the C side
  • We send in pointers (using to obtain the
    address of variable) as arguments from the C side
  • On the Fortran side, this is plain vanilla
    Fortran (i.e. nothing special is done).

4
C-gtF77 Example With Array Passing
  • In this example I create an array on the C side
    and pass in the pointer to the F77 side.
  • The F77 function is able to access and change the
    contents of the array.
  • On the F77 side notice array accesses start from
    1
  • On the F77 side notice how array accesses start
    from 0

5
C-gtF77 Example with array passing cont
  • Compiling and running

6
Leading Dimension
  • The leading dimension of an array is the number
    of rows in Fortran (since Fortran is column
    major).
  • In your dmat struct it depends on whether you
    chose to store your matrix in row or column
    major.
  • If the matrix is row major, then the leading
    dimension is the number of columns.
  • If the matrix is column major, then the leading
    dimension is the number of rows.
  • In the following I am using Fortran routines to
    compute matrix algebraic operations so I set up
    my dmat to be column major.

7
BLAS
  • We have been building up to using
  • the Basic Linear Algebra Subroutine library BLAS
  • This is available for download online at
  • http//www.netlib.org/blas/
  • There is a nice quick guide to the subroutines
    included in BLAS at
  • http//www.netlib.org/blas/blasqr.ps
  • However, I already downloaded a version and
    compiled it up into a library for you
  • /net/cage/timwar/libblas.a

8
BLAS
  • BLAS is divided into four types and three levels.
  • Functions are prefixed with the type of the
    variables
  • s,d,c, or z for single, double, complex, or
    double complex (z).
  • Level 1 - Vector algebra routines, notably
    include
  • dcopy which copies one vector of doubles to a
    different vector of doubles
  • dscal which multiplies the entries of a vector of
    doubles by a scalar
  • daxpy which multiples one vector of doubles by a
    scalar and adds the result to a second vector
  • ddot which computes the dot product (also known
    as vector inner product) of two vectors of
    doubles.

9
BLAS Level 1 Examples
  • I hid prototypes for the blas functions in blas.h
  • Note the BLAS function naming is not
    particularly intuitive.
  • Thats why a lot of people build these calls into
    their struct interfaces.
  • Also hard coding the extra underscore as done
    here is dangerous.

10
BLAS Level 2
  • These functions are generally matrix-vector
    multiplying routines.
  • Notably
  • dgemv
  • For this example, I am using my own dmat which is
    indexed from 1 and column major (as determined by
    dmatget and dmatset).

11
dgemv
  • In this example I build a vector x, then compute
  • alphax betaAx
  • This code is a disaster the intent is hidden in
    all those function arguments.

12
BLAS Level 3
  • These are typically matrix-matrix multiplying
    functions.
  • Notably
  • dgemm
  • This time I have hidden some of the functionality
    of dgemm inside my dmat interface functions.

13
dgemm
  • What I did was to hide the nasty dgemm call
    inside my dmatmultiply function.
  • The user never needs to see the horrible dgemm
    function construction.

14
What happens in dmatmultiply
  • As you are not the user you should see what I
    did in dmatmultiply

15
Class Exercise
  • Download the BlasDemo.tar.gz tarball from the
    website and
  • make sure you can do the following
  • gzip d BlasDemo.tar.gz
  • tar xvf BlasDemo.tar.gz
  • cd BlasDemo
  • make blas1
  • ./myblaslevel1
  • Modify the myblaslevel1.c file to compute
  • c a.(alphaa b) using ddot, and daxpy
  • c b.(alphaa) using ddot, and dscal
Write a Comment
User Comments (0)
About PowerShow.com