Results 1 to 3 of 3

Thread: How to make a FORTRAN library

  1. #1
    Join Date
    Dec 2004
    Location
    GOSAT, CGER, National Institute of Env. Studies (NIES), Tsukuba
    Posts
    337
    Thanks
    1
    Thanked 18 Times in 11 Posts

    How to make a FORTRAN library

    This is something related to Fortran programming. It is a widely accepted programming language especially for Atmospheric/Oceanographic studies. Generally large scale GCMs (General Circulation Models) are written in Fortran code.

    We are familiar with sub-routines and functions in programing.
    May be many of us are not familiar with the 'so called libraries'.

    What is a Library

    Simply speaking, Library is an archive of a set of general sub-routines which can be called/used/linked to any of our programs. For example, if we want to find average of an array (a set of numbers), in our main program, we have to write a sub-routine to find the average and have to call in the main program. For that, we have to include this sub-routine codes inside our main program or as a separate FORTRAN file and compile it with main program. Thus at each time we we have to repeat this additional sub-routine (to find average) and compile them. There comes the role of Libraries. In the library we can store the pre-compiled sub-routines as an 'object file', and can be called from anywhere, by linking these libraries. Let us understand this process by an example.


    ---------------------------------------------------------------------------------------------------------
    cccccccccc an example program main.F cccccccccccccc

    program main
    parameter (lon=14)
    real data (lon), zaverage

    write (*,*)' This is a program to find zonal average of a data'

    cccccccc read the data cccccccccccc

    open (1,file='inputdata.dat')
    do i=1,lon
    read (1,*) data(i)
    enddo

    cccccccccc find zonal average ccccccccccccccc

    call average (lon, data, zaverage)

    write(*,10)'The zonal average of given data is ', zaverage
    10 format(a40,f8.3)

    end

    subroutine average (N, A, ave)
    integer N
    real A(N), ave
    ave = 0.0
    do i=1,N
    ave = ave + A(i)
    enddo
    ave = ave/N

    return
    end

    ------------------------------------------------------------------------------------------------------------


    This way of coding, we have to repeat the steps for averaging (red color) every time. However, we can avoid that by making a 'General sub-routine' for finding average and make it as an object file and put it in a library, as follows.

    Step-1
    -----------------------------------------------------------------------------------------------------------
    c file name average.F
    c subroutine to find average of an array size data(A)

    subroutine average (N, A, ave)
    integer N
    real A(N), ave
    ave = 0.0
    do i=1,N
    ave = ave + A(i)
    enddo
    ave = ave/N

    return
    end
    ------------------------------------------------------------------------------------------------------------


    step-2

    Compile this sub-routine and make an object file. The fallowing steps uses f77 compiler under UNIX/Linux

    [name@xxxxx]$ f77 -c average.F

    this will give the object file average.o. Using the object file make a library

    [name@xxxxx]$ ar r libmylib.a average.o

    The command 'ar' is to make archives. This will give a library (libmylib.a).

    step-3

    To link this library into your programs, use the following command
    [name@xxxxx]$ f77 main.F -L/path/ -lmylib

    This step will compile the program main.F (or whatever), and link it to the library called 'libmylib.a'. The -L/path/ referes to the location of your library. You can put it in any directory in your machine or standard locations where the compiler is looking when a compilation takes place (e.g. /usr/lib /usr/local/lib etc.. ) In a similar manner, you can make 'as many general subroutine as you want', and make them object files and put it together in a library. It can be used simply by linking those libraries on your compilation. For many number of object files, (e.g.)

    [name@xxxxx]$ f77 -c average.F mean.F sort.F area_average.F climatology.F

    [name@xxxxx]$ ls *.o
    average.o mean.o sort.o area_average.o climatology.o


    [name@xxxxx]$ ar r libmylib.a average.o mean.o sort.o area_average.o climatology.o

    To append an object file into an existing library is the same as above
    [name@xxxxx]$ ar r libmylib.a daily_climatology.o

    This is the way, the libraries are making. We may be familiar in using NETCDF libraries. Actually it is a set of object files archived into a library. To list out the object files in a library, you can use

    [name@xxxxx]$ ar t libmylib.a

    mean.o
    sort.o
    area_average.o
    climatology.o
    daily_climatology.o


    These are 'so many standard libraries' are available for FORTRAN programs. for example BLAS (Basic Linear Algebra Subprograms), LAPACK (Linear Algebra Package) etc.. are popular libraries for FORTRAN programs. (It exist for C-programing as well), which do simple algebra to very complex matrix calculations.


    CAUTION! if you make your own library, and using other standard libraries like BLAS or LAPACK, care must be taken that your variables are not conflicting with the variables of other libraries. This may give erroneous results. Moreover, a pre-compliled library for one machine architecture may be conflict with
    other machines. The byte compatibility is different for different machines (like Sun, Alpha Linux, other Unix platforms)

    -----------------------------------------------------------------------------------------------------------------------------

  2. #2
    Join Date
    Dec 2004
    Location
    Earth & Atmospheric Sciences, U of A, Edmonton, CANADA
    Posts
    215
    Thanks
    2
    Thanked 3 Times in 2 Posts
    i was late to appreciate this post. good for a start. Thanks Vinu
    Praveen V K

  3. #3
    Join Date
    Jan 2007
    Location
    Beijing China
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    This is very useful for us. but I also want to know how to make the lib?

Similar Threads

  1. Worldwide Coral Reef Library
    By arjunsmenon in forum Ocean News
    Replies: 1
    Last Post: 18th January 2005, 09:43 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •