[ anterior ] [ Contenidos ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] [ 7 ] [ 8 ] [ 9 ] [ 10 ] [ 11 ] [ 12 ] [ 13 ] [ siguiente ]
Fortran 90
para la asignatura Química Computacional
BLAS
y LAPACK
Los objetivos de esta clase son los siguientes:
familiarizar el alumno con la compilación de programas y la instalación de
librerías o bibliotecas usando el compilador gfortran
.
Instalar las bibliotecas de interés científico BLAS
y
LAPACK
.
Aprender a hacer uso de dichas bibliotecas.
Existe una gran cantidad de código Fortran
accesible de forma
abierta, ya sea como código fuente o en forma de biblioteca. En la presente
clase el alumno se familiariza con la obtención, compilación, instalación y uso
de dos bibliotecas de subrutinas de interés algebraico, BLAS
y
LAPACK
.
Indicaremos de forma escalonada los diferentes pasos que hay que seguir para la instalación de estas bibliotecas.
El código fuente de las bibliotecas BLAS
y LAPACK
puede descargarse de diferentes lugares, o instalarse a partir de paquetes de
la distribución Debian
o Ubuntu
que se esté
utilizando. En vez de ello las instalaremos compilándolas en nuestro
ordenador.
Descarga de código fuente de la biblioteca BLAS
.
Se puede descargar de la web de NETLIB[13], usando este enlace BLAS tgz (Netlib)
.
Una vez descargado el código fuente, se descomprime, se compila y se crea finalmente la librería.
tar xzf blas.tgz cd BLAS gfortran -O2 -c *.f ar cr libblas.a *.o
Con lo que se debe haber creado la librería estática libblas.a.
A continuación se sitúa dicha librería en un lugar apropiado, por ejemplo con
sudo cp libblas.a /usr/local/lib
y se comprueba que tiene los permisos adecuados.
Descarga del código fuente de la biblioteca LAPACK
.
Se puede descargar también de la web de NETLIB usando este enlace
LAPACK tgz
(Netlib)
. Tras su descarga se desempaquetan los ficheros.
tar xzf lapack.tgz cd lapack-3.2.1
Esta biblioteca si tiene una serie de ficheros makefile para su
compilación. Hemos de preparar un fichero make.inc adecuado, como
el que hemos incluido en Ejemplo de fichero
make.inc para LAPACK
, Sección 12.3.1 y que está
disponible en Moodle
en un fichero llamado make.inc.lapack.ubuntu
.
Usando este fichero compilamos la librería haciendo
make
Por último, instalamos la librería copiando los ficheros creados al lugar que creamos más adecuado para su ubicación.
sudo cp lapack_LINUX.a /usr/local/lib sudo cp tmglib_LINUX.a /usr/local/lib
Para terminar descargamos el código fuente de la biblioteca
LAPACK95
.
Se puede descargar también de la web de NETLIB usando el enlace
LAPACK95 tgz
(Netlib)
. Tras su descarga se desempaquetan los ficheros.
tar xzf lapack95.tgz cd LAPACK95
Esta biblioteca también tiene una serie de ficheros makefile para
su compilación. Hemos de preparar de nuevo un fichero make.inc
adecuado, como el que hemos incluido en Ejemplo de
fichero make.inc para LAPACK95
, Sección 12.3.2 y
que está disponible en Moodle
en un fichero llamado make.inc.lapack95.ubuntu
.
Usando este fichero compilamos la librería haciendo
cd SRC make single_double_complex_dcomplex
La opción escogida es la más general, pues general la librería para precisión simple, doble, compleja simple y compleja doble.
Por último, instalamos la librería copiando los ficheros creados al lugar que creamos más adecuado para su ubicación.
sudo cp lapack95.a /usr/local/lib sudo cp -r lapack95_modules /usr/local/lib
En los directorios de ejemplos (LAPACK95/EXAMPLES1 y
LAPACK95/EXAMPLES2) encontramos un gran número de ejemplos que
podemos correr y comprobar las salidas obtenidas con las que se encuentran en
Lapack95 User's
guide
. Las instrucciones para compilar y correr los ejemplos
proporcionados pueden verse en el fichero README del directorio
donde se encuentra el código fuente de los ejemplos.
En el ejemplo Ejemplo de programa que invoca
LAPACK95
, Sección 12.3.3 se encuentra el código de un programa
donde se recurre a la subrutina la_spsv para hallar la solución de
un sistema lineal de ecuaciones, Ax = B, donde la matriz del sistema,
A, es simétrica y se almacena de forma compacta y x,
B son vectores. Es importante que comprenda como funciona este
programa, así como que se sepa extraer de la documentación de
LAPACK95
el significado de los argumentos de entrada y salida de
la subrutina.
Para correr este programa es necesario descargar el código ejemplo_la_spsv.f90 y los ficheros de datos spsv.ma y spsv.mb de la web del curso. Para compilar el programa se ejecuta la orden
gfortran -o ejemplo_la_spsv -I/usr/local/lib/lapack95_modules ejemplo_la_spsv.f90 /usr/local/lib/lapack95.a /usr/local/lib/tmglib_LINUX.a /usr/local/lib/lapack_LINUX.a /usr/local/lib/libblas.a
En esta orden de compilación se incluyen todas las librerías y módulos
necesarios para que pueda crearse el ejecutable, haciendo uso de las librerías
BLAS
, LAPACK
y LAPACK95
que hemos
instalado.
Para proyectos más complejos y evitar tener que escribir comandos de
compilación tan complejos como el anterior es posible usar un fichero
makefile como el que se proporciona en el ejemplo Ejemplo de makefile para compilar programas
que invocan LAPACK95
, Sección 12.3.4. Para usar este fichero
en la compilación del ejemplo Ejemplo de programa que
invoca LAPACK95
, Sección 12.3.3 es preciso copiar el fichero
proporcionado o descargar el fichero makefile_lapack95 y ejecutar
la orden
make -f makefile_lapack95 ejemplo_la_spsv
LAPACK
#################################################################### # LAPACK make include file. # # LAPACK, Version 3.2.1 # # MAY 2009 # # Modified by Currix # #################################################################### # SHELL = /bin/sh # # The machine (platform) identifier to append to the library names # PLAT = _LINUX # # Modify the FORTRAN and OPTS definitions to refer to the # compiler and desired compiler options for your machine. NOOPT # refers to the compiler options desired when NO OPTIMIZATION is # selected. Define LOADER and LOADOPTS to refer to the loader and # desired load options for your machine. # FORTRAN = gfortran OPTS = -O2 DRVOPTS = $(OPTS) NOOPT = -O0 LOADER = gfortran LOADOPTS = # # Timer for the SECOND and DSECND routines # # Default : SECOND and DSECND will use a call to the EXTERNAL FUNCTION ETIME #TIMER = EXT_ETIME # For RS6K : SECOND and DSECND will use a call to the EXTERNAL FUNCTION ETIME_ # TIMER = EXT_ETIME_ # For gfortran compiler: SECOND and DSECND will use a call to the INTERNAL FUNCTION ETIME TIMER = INT_ETIME # If your Fortran compiler does not provide etime (like Nag Fortran Compiler, etc...) # SECOND and DSECND will use a call to the INTERNAL FUNCTION CPU_TIME # TIMER = INT_CPU_TIME # If neither of this works...you can use the NONE value... In that case, SECOND and DSECND will always return 0 # TIMER = NONE # # The archiver and the flag(s) to use when building archive (library) # If you system has no ranlib, set RANLIB = echo. # ARCH = ar ARCHFLAGS= cr RANLIB = ranlib # # Location of the extended-precision BLAS (XBLAS) Fortran library # used for building and testing extended-precision routines. The # relevant routines will be compiled and XBLAS will be linked only if # USEXBLAS is defined. # # USEXBLAS = Yes XBLASLIB = # XBLASLIB = -lxblas # # The location of the libraries to which you will link. (The # machine-specific, optimized BLAS library should be used whenever # possible.) # #BLASLIB = ../../blas$(PLAT).a BLASLIB = /usr/local/lib/libblas.a LAPACKLIB = lapack$(PLAT).a TMGLIB = tmglib$(PLAT).a EIGSRCLIB = eigsrc$(PLAT).a LINSRCLIB = linsrc$(PLAT).a
LAPACK95
# # -- LAPACK95 interface driver routine (version 2.0) -- # UNI-C, Denmark; Univ. of Tennessee, USA; NAG Ltd., UK # August 5, 2000 # FC = gfortran FC1 = gfortran # -dcfuns Enable recognition of non-standard double # precision complex intrinsic functions # -dusty Allows the compilation and execution of "legacy" # software by downgrading the category of common # errors found in such software from "Error" to # -ieee=full enables all IEEE arithmetic facilities # including non-stop arithmetic. OPTS0 = -u -V -dcfuns -dusty -ieee=full MODLIB = -I./../lapack95_modules OPTS1 = -c $(OPTS0) OPTS3 = $(OPTS1) $(MODLIB) OPTL = -o OPTLIB = LAPACK_PATH = /usr/local/lib/ LAPACK95 = ../lapack95.a LAPACK77 = $(LAPACK_PATH)/lapack_LINUX.a TMG77 = $(LAPACK_PATH)/tmglib_LINUX.a BLAS = $(LAPACK_PATH)/libblas.a LIBS = $(LAPACK95) $(TMG77) $(LAPACK77) $(BLAS) SUF = f90 XX = 'rm' -f $@; \ 'rm' -f $@.res; \ $(FC) $(OPTS0) -o $@ $(MODLIB) $@.$(SUF) $(OPTLIB) $(LIBS); \ $@ < $@.dat > $@.res; \ 'rm' -f $@ YY = $(FC) $(OPTS0) -o $@ $(MODLIB) $@.$(SUF) $(OPTLIB) $(LIBS) .SUFFIXES: .f90 .f .o .$(SUF).o: $(FC) $(OPTS3) $< .f.o: $(FC1) $(OPTS3) $<
LAPACK95
PROGRAM LA_SSPSV_EXAMPLE ! -- LAPACK95 EXAMPLE DRIVER ROUTINE (VERSION 1.0) -- ! UNI-C, DENMARK ! DECEMBER, 1999 ! ! .. "Use Statements" USE LA_PRECISION, ONLY: WP => SP USE F95_LAPACK, ONLY: LA_SPSV ! .. "Implicit Statement" .. IMPLICIT NONE ! .. "Local Scalars" .. INTEGER :: I, N, NN, NRHS ! .. "Local Arrays" .. INTEGER, ALLOCATABLE :: IPIV(:) REAL(WP), ALLOCATABLE :: B(:,:), AP(:) ! .. "Executable Statements" .. WRITE (*,*) 'SSPSV Example Program Results.' N = 5; NRHS = 1 WRITE(*,'(5H N = , I4, 9H; NRHS = , I4)') N, NRHS NN = N*(N+1)/2 ALLOCATE ( AP(NN), B(N,NRHS), IPIV(N) ) ! OPEN(UNIT=21,FILE='spsv.ma',STATUS='UNKNOWN') DO I=1,NN READ(21,'(F3.0)') AP(I) ENDDO CLOSE(21) ! WRITE(*,*)'Matrix AP :' DO I=1,NN; WRITE(*,"(15(I3,1X,1X),I3,1X))") INT(AP(I)); ENDDO ! OPEN(UNIT=21,FILE='spsv.mb',STATUS='UNKNOWN') DO I=1,N READ(21,'(F3.0)') B(I,1) ENDDO CLOSE(21) ! WRITE(*,*)'Matrix B :' DO I=1,N; WRITE(*,"(10(I3,1X,1X),I3,1X)')") INT(B(I,1)); ENDDO ! WRITE(*,*)" CALL LA_SPSV( AP, B, 'L', IPIV )" ! CALL LA_SPSV( AP, B, 'L', IPIV ) ! WRITE(*,*)'AP on exit: ' DO I=1,NN; WRITE(*,"(15(E13.5))") AP(I); ENDDO ! WRITE(*,*)'Matrix B on exit :' DO I=1,N; WRITE(*,"(F9.5)") B(I,1); ENDDO WRITE(*,*)'IPIV = ', IPIV ! END PROGRAM LA_SSPSV_EXAMPLE
LAPACK95
# # -- LAPACK95 makefile (version 1.0) -- # FC = gfortran # MODLIB = -I/usr/local/lib/lapack95_modules OPTS1 = -c OPTS3 = $(OPTS1) $(MODLIB) OPTL = -o OPTLIB = LAPACK_PATH = /usr/local/lib LAPACK95_PATH = /usr/local/lib LAPACK95 = $(LAPACK95_PATH)/lapack95.a LAPACK77 = $(LAPACK_PATH)/lapack_LINUX.a TMG77 = $(LAPACK_PATH)/tmglib_LINUX.a BLAS = $(LAPACK_PATH)/libblas.a LIBS = $(LAPACK95) $(TMG77) $(LAPACK77) $(BLAS) SUF = f90 YY = $(FC) -o $@ $(MODLIB) $@.$(SUF) $(OPTLIB) $(LIBS) .SUFFIXES: .f90 .f .o .$(SUF).o: $(FC) $(OPTS3) $< ejemplo_la_spsv: $(YY) clean: 'rm' -f *.o *.mod core
[ anterior ] [ Contenidos ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] [ 7 ] [ 8 ] [ 9 ] [ 10 ] [ 11 ] [ 12 ] [ 13 ] [ siguiente ]
Lecciones de Fortran 90
para la asignatura Química Computacional
mailto:francisco.perez@dfaie.uhu.es