Using Fortran 90 and CTT's Fortran 90 MP Modules
This document discusses a VNI Inc. sample program that uses two Fortran 90 MP modules—one to generate a random test matrix and one to calculate its singular value decomposition (SVD). The program also checks the results by using Fortran 90 matrix intrinsic functions. These functions replace the need to call older IMSL basic matrix algebra operations such as matrix multiplication. This session was run using the Sun Studio 11 compilers and SMP Fortran 90 load libraries. The cttshell alias and environment variables used below are available if you have made the suggested UNIX configuration changes described in Getting Started with CTT: Setting Up Your UNIX Account.
|setenv commands in setup files|
% mkdir testctt <2>% cd testctt <3>% cp $CTT_EXAMPLES/f90/manual/lin_svd_ex1.f90 . <4>% make lin_svd_ex1 f90 -openmp=noopt -ftrap=%none -xtarget=generic -M/opt/vni/CTT6.1/include/solaris -o lin_svd_ex1 lin_svd_ex1.f90 -R/opt/vni/CTT6.1/lib/lib.solaris -L/opt/vni/CTT6.1/lib/lib.solaris -Bdynamic -limsl -limslsmp -xlic_lib=sunperf -ldl -lnsl -lsocket <5>% ./lin_svd_ex1 Example 1 for LIN_SVD is correct. <6>% exit <101>%
- The cttshell command starts a new shell with the correct environment for all CTT products. The command could also be followed by a 4-digit UNIX group project ID if desired.
- The sample program lin_svd_ex1.f90 (whose listing is below) is copied from the CTT examples directory to a new directory for testing.
- The make lin_svd_ex1 command finds the Fortran 90 source file and creates the executable lin_svd_ex1. Since the source file has an ".f90" extension, make uses a suffix rule to invoke an f90 compiler. (The specific compiler used is the one defined by the environment variable F90, which has been set to the Sun Studio 11 f90 compiler as part of the configuration setup.)
- The executable is run and it reports the SVD solution is correct.
- Finally, exiting the shell returns to the parent shell (command number <101>) with its previous environment variable and alias definitions.
Listing of the lin_svd_ex1.f90 sample program
use lin_svd_int use rand_gen_int implicit none ! This is Example 1 for LIN_SVD. integer, parameter :: n=32 real(kind(1d0)), parameter :: one=1d0 real(kind(1d0)) err real(kind(1d0)), dimension(n,n) :: A, U, V, S(n), y(n*n) ! Generate a random n by n matrix. call rand_gen(y) A = reshape(y,(/n,n/)) ! Compute the singular value decomposition. call lin_svd(A, S, U, V) ! Check for small residuals of the expression A*V - U*S. err = sum(abs(matmul(A,V) - U*spread(S,dim=1,ncopies=n))) & / sum(abs(S)) if (err <= sqrt(epsilon(one))) then write (*,*) 'Example 1 for LIN_SVD is correct.' end if end