Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using LAPACK library in fortran codes

Status
Not open for further replies.

dibas

Programmer
Nov 23, 2011
24
0
0
ZA
Has anyone ever used LAPACK in their fortran(90) codes? I believe this
can help me compare the results from my hand-coded linear-algebra codes -
or even replace these in case LAPACK gives far better performance.
Small pieces of example code might be helpful.
I've also tried installing this package on my system (Linux, Ubuntu 11.10)
but without success. Maybe someone can also help with the practical steps of
doing this.

Thank you in advance for your help.
 
Hi dibas,
I only tried it with gfortran on windows.
Instead of building LAPACK libraries self, I got the compiled binaries liblapack.lib and liblapack.dll from here:

Now I have this short example for trying LAPACK
example.f95
Code:
[COLOR=#a020f0]Program[/color] LinearEquations
  [COLOR=#0000ff]! solving the matrix equation A*x=b using LAPACK[/color]
  [COLOR=#2e8b57][b]Implicit[/b][/color] [COLOR=#2e8b57][b]none[/b][/color]

  [COLOR=#0000ff]! declarations[/color]
  [COLOR=#2e8b57][b]double precision[/b][/color] :: A([COLOR=#ff00ff]3[/color],[COLOR=#ff00ff]3[/color]), b([COLOR=#ff00ff]3[/color])
  [COLOR=#2e8b57][b]integer[/b][/color] :: i, pivot([COLOR=#ff00ff]3[/color]), ok
  
  [COLOR=#0000ff]! matrix A[/color]
  A([COLOR=#ff00ff]1[/color],:)[COLOR=#804040][b]=[/b][/color]([COLOR=#804040][b]/[/b][/color][COLOR=#ff00ff]3[/color], [COLOR=#ff00ff]1[/color], [COLOR=#ff00ff]3[/color][COLOR=#804040][b]/[/b][/color])
  A([COLOR=#ff00ff]2[/color],:)[COLOR=#804040][b]=[/b][/color]([COLOR=#804040][b]/[/b][/color][COLOR=#ff00ff]1[/color], [COLOR=#ff00ff]5[/color], [COLOR=#ff00ff]9[/color][COLOR=#804040][b]/[/b][/color])
  A([COLOR=#ff00ff]3[/color],:)[COLOR=#804040][b]=[/b][/color]([COLOR=#804040][b]/[/b][/color][COLOR=#ff00ff]2[/color], [COLOR=#ff00ff]6[/color], [COLOR=#ff00ff]5[/color][COLOR=#804040][b]/[/b][/color])
  
  [COLOR=#0000ff]! vector b[/color]
  b(:)[COLOR=#804040][b]=[/b][/color]([COLOR=#804040][b]/-[/b][/color][COLOR=#ff00ff]1[/color], [COLOR=#ff00ff]3[/color], [COLOR=#804040][b]-[/b][/color][COLOR=#ff00ff]3[/color][COLOR=#804040][b]/[/b][/color])
  [COLOR=#0000ff]!b(:)=(/2, 2, 9/)[/color]

  [COLOR=#0000ff]! find the solution using the LAPACK routine DGESV[/color]
  [COLOR=#008080]call[/color] DGESV([COLOR=#ff00ff]3[/color], [COLOR=#ff00ff]1[/color], A, [COLOR=#ff00ff]3[/color], pivot, b, [COLOR=#ff00ff]3[/color], ok)

  [COLOR=#0000ff]! print the solution x[/color]
  [COLOR=#804040][b]do[/b][/color] i[COLOR=#804040][b]=[/b][/color][COLOR=#ff00ff]1[/color], [COLOR=#ff00ff]3[/color]
    [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#ff00ff]9[/color]) i, b(i)
  [COLOR=#804040][b]end do[/b][/color]  

[COLOR=#6a5acd]9[/color] [COLOR=#804040][b]format[/b][/color]([COLOR=#ff00ff]'x['[/color], i1, [COLOR=#ff00ff]']= '[/color], [COLOR=#008080]f5.2[/color])  
[COLOR=#a020f0]end program[/color] LinearEquations
I can compile the example above either with liblapack.lib:
Code:
$ gfortran -o example example.f95 -L. -lliblapack
or with liblapack.dll:
Code:
$ gfortran -o example example.f95 -L. liblapack.dll
(I have example.f95, liblapack.lib, liblapack.dll in the same direcory)

Both executables give the result
Code:
$ example
x[1]= -1.00
x[2]= -1.00
x[3]=  1.00
If you are on Linux, try to get LAPACK binary for your distribution or build it from source.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top