Solving Fortran Lapack Compilation Issues

In summary: F15.5,1X,F15.5))" ! Output the input mat and vec DO i=1,numEq WRITE(*,fmt) mat(i,:) ENDDO WRITE(*,fmt) vec ! Solve the Linear System CALL ZGESV(numEq,1,mat,numEq,IPIV,vec,numEq,INFO) ! Output the solution WRITE(*,*) 'Solved Solution:' WRITE(*,*) 'x | u(x)' DO i=1,m WRITE(*,"(F15.5,1X,F15.5)") x(i),vec(i) ENDDO
  • #1
Takafoo
2
0
Can you please help me I can't compile using lapack libraries and I can't find anyone who can tell me how to compile.

I have blas_LINUX.a and lapack_LINUX.a and I am trying to use ifort to compile my program but I always get the below error. Please help.

In function `MAIN__':
1001.f90:(.text+0x23f2): undefined reference to `zgesv_'

These are the libraries I have in the same directory

blas_LINUX.a
lapack_LINUX.a
liblapack_LINUX.a
 
Technology news on Phys.org
  • #2
Takafoo said:
Can you please help me I can't compile using lapack libraries and I can't find anyone who can tell me how to compile.

I have blas_LINUX.a and lapack_LINUX.a and I am trying to use ifort to compile my program but I always get the below error. Please help.



These are the libraries I have in the same directory

blas_LINUX.a
lapack_LINUX.a
liblapack_LINUX.a

I'm not 100% sure, but it looks like you're referencing a variable, possibly a string, and its not defined.

Since I'm not familiar with the library, if I were you, I would find out what that variable corresponds to. Since it is in main, if your library is just a code library, then most likely the MAIN routine is part of another code-base (possibly your own).

If the code entry point is part of the library itself, find a variable/function/interface etc reference and find out what variable it is.

Again, I'm guessing that if you are using an external library and the entry point is in your code, its not a problem with the library, its a problem with your code base.
 
  • #3
This is my code if it helps. Live 158 is where I call the function from the library... I can't see where I've gone wrong at all unfortunately.

Code:
PROGRAM 1001

   IMPLICIT NONE

! Declare Variables

   COMPLEX(KIND=8), DIMENSION(:,:), ALLOCATABLE :: mat
   COMPLEX(KIND=8), DIMENSION(:), ALLOCATABLE :: vec, vec_2
   REAL(KIND=8), DIMENSION(:,:), ALLOCATABLE :: mat_r, mat_i
   REAL(KIND=8), DIMENSION(:), ALLOCATABLE :: vec_r, vec_i, a, b, x
   REAL(KIND=8) :: n_x, n_0, k, beta, pi, exp, step, x_i, u_i
   REAL(KIND=8) :: start_time, end_time
   INTEGER :: lambda, L, m, numEq, i
   INTEGER :: allocate_status=0, IO_status
   INTEGER :: NRHS, LD_mat, LD_vec, INFO
   INTEGER, DIMENSION(:), ALLOCATABLE :: IPIV
   CHARACTER(LEN=30) :: fmt, int_char, filename

! Load Parameters File

   PRINT *, "Please specify parameter filename: "
   READ (*,*) filename

  OPEN(1, file=filename, status='old', IOSTAT=IO_status)
  IF (IO_status /= 0) THEN
    WRITE(*,*) "Error!"
    WRITE(*,*) "Cannot open file ", filename, " File cannot be located. Please check file and try again."
    GOTO 100
  ENDIF
    READ(1,*) lambda
    READ(1,*) n_x
    READ(1,*) n_0
    READ(1,*) L
    READ(1,*) m
  CLOSE(1)

! Output File Contents

   WRITE(*,*) 'Wavelength = ' , lambda
   WRITE(*,*) 'Refractive Index Function = ' , n_x
   WRITE(*,*) 'Refractive Index = ' , n_0
   WRITE(*,*) 'Length = ' , L
   WRITE(*,*) 'Grid Point Parameter = ',  m

! Set Variables

   pi = 3.14159265
   exp = 2.71828183
   k = 2 * pi / lambda
   beta = n_0 * k
   step = L / m

! Error Checking

   IF (lambda <= 0) THEN
     WRITE(*,*) "ERROR! Wavelength <= 0"
     GOTO 100
   ENDIF

   IF (n_x <= 0) THEN
     WRITE(*,*) "ERROR! Refractive Index Function <= 0"
     GOTO 100
   ENDIF

   IF (n_0 <= 0) THEN
     WRITE(*,*) "ERROR! Refractive Index <= 0"
     GOTO 100
   ENDIF

  IF (L <= 0) THEN
     WRITE(*,*) "ERROR! Length <= 0"
     GOTO 100
   ENDIF

  IF (m <= 2) THEN
     WRITE(*,*) "ERROR! Grid Point Parameter < 2"
     GOTO 100
   ENDIF
! Bulk Coding

   numEq = m + 3
   WRITE(*,*) 'Step Size = ', step

   ALLOCATE(mat(numEq,numEq), vec(numEq), x(m), &
            mat_r(numEq,numEq), vec_r(numEq), a(m-1), &
            mat_i(numEq,numEq), vec_i(numEq), b(m-1), &
            IPIV(numEq), STAT=allocate_status)

   CALL cpu_time(start_time)

! Initialise Matrices and Vectors

   mat = 0.0d0
   mat_r = 0.0d0
   mat_i = 0.0d0
   vec = 0.0d0
   vec_r = 0.0d0
   vec_i = 0.0d0

   DO i=0, m
     x(i) = i * step
   ENDDO

   DO i=1, m-1
     a(i) = -2+(step**2)*(n_x**2)*(k**2)
     b(i) = 1
   ENDDO

   i=1
     mat_r(i,i) = -1
     mat_r(i+1,i) = 1
   DO i=2,m
     mat_r(i-1,i) = b(i-1)
     mat_r(i,i) = a(i-1)
     mat_r(i+1,i) = b(i-1)
   ENDDO
   i=m+1
     mat_r(i-1,i) = -1
     mat_r(i,i) = 1
     mat_r(1,i+1) = 1
     mat_r(i+1,i+1) = -1
     mat_r(i,i+2) = 1

   ! Input value to imaginary mat
   i=1
     mat_i(numEq-1,i) = step * beta
   i=m+1
     mat_i(numEq,i) = -step * beta * (exp**(beta * L))
     mat_i(numEq,i+2) = -exp**(beta * L)

   ! Input value to real and imaginary vec
   vec_r(numEq-1) = 1
   vec_i(1) = step * beta

   ! Combine real and imaginary data to mat and vec
   mat = mat_r+(0.0d0, 1.0d0)*mat_i
   vec = vec_r+(0.0d0, 1.0d0)*vec_i

   ! Formatting fmt
   write(int_char,*) numEq
   FMT = "(" // TRIM(ADJUSTL(int_char)) // "(' (',f10.4,',',f10.4,') '))"

   IF (numEq <= 10) then
     WRITE(*,*) "Matrix (A):"
     WRITE(*,FMT) mat
     WRITE(*,*) "Vector (B):"
     WRITE(*,FMT) vec
   ENDIF

! Solve

   NRHS = 1
   LD_mat = numEq
   LD_vec = numEq
   CALL ZGESV(numEq, NRHS, mat, LD_mat, IPIV, vec, LD_vec, INFO)

   IF (numEq <= 10) then
     WRITE(*,FMT) vec
   ENDIF

   CALL cpu_time(end_time)

   WRITE(*,*) "Run time = ", end_time - start_time

   ! Saving the solution
   OPEN(2, file='solution.txt', status='unknown', IOSTAT=IO_status)
     IF (IO_status /= 0) then
       WRITE(*,*) "Error opening the file solutions.txt"
       GOTO 100
     ELSE
       i=numEq-1
         WRITE(2,'(2(f20.15))') 'r = ', vec(i)
       i=numEq
         WRITE(2,'(2(f20.15))') 't = ', vec(i)
     ENDIF
   CLOSE(2)

   100 CONTINUE

END PROGRAM 1001
 
  • #4
The ZGESV is a function in your MAIN routine.

I'm not a FORTRAN coder (VB,C,C++,ASM, not FORTRAN), but my guess is that you need to basically declare your function.

I'm guessing that the routine is part of your library. Based on that you will probably have to provide an external reference in your code.

What should happen is with the reference, your code will compile the code into an object file. Then when you use the linker, it will search for all variables, functions and so on, and link everything together into your executable. This is standard procedure for any type of executable code and is independent of the language used (FORTRAN, C, C++ etc).

I just looked up some FORTRAN doc and it said that there is an EXTERNAL keyword. I'm guessing that's what you need to use.
 
  • #5
Your code looks OK to me.

Just putting the libraries in your current directory is not enough. You have to tell the compiler to search them as well.

You need to do something like
f77 myfile.f -l lapack_LINUX.a -l liblapack_LINUX.a -l blas_LINUX.a

Note, I don't use Linux, so that may not be exactly right, but most compilers have similar options.

There should be an example in the library documentation of what to do, or look at the documentation of your compiler to see how to include routines from libraries.

If you are using "make", you should be able to set a variable in your makefile to the list of libraries you want to search.
 
  • #6
Ok this thread is old but... At least for gfortran (and I strongly believe that for ifortran too): Never start your first line with "program nameofprogram" where nameofprogram contains numbers. :/ You'll get an error when compiling, so it won't compile.
 

1. What is Fortran Lapack and why is it used in scientific computing?

Fortran Lapack (Linear Algebra Package) is a collection of subroutines used for solving linear algebra problems, such as matrix computations and solving systems of linear equations. It is commonly used in scientific computing because of its efficient and accurate numerical algorithms, making it a reliable tool for performing complex calculations.

2. How do I compile Fortran Lapack code on my computer?

To compile Fortran Lapack code, you will need a Fortran compiler, such as GNU Fortran (gfortran) or Intel Fortran Compiler (ifort). You will also need to link the Lapack library to your code during compilation. The specific steps will depend on your operating system and compiler, so it is best to refer to the documentation for your specific setup.

3. What is the most common issue when compiling Fortran Lapack code?

The most common issue when compiling Fortran Lapack code is not properly linking the Lapack library. This can result in errors such as "undefined reference to Lapack routines" or "Lapack library not found". It is important to make sure the path to the Lapack library is correctly specified during compilation.

4. Can I use Fortran Lapack with other programming languages?

Yes, Fortran Lapack can be used with other programming languages through interfaces, such as the Fortran/C interface (FCI) or the Fortran/Python interface (F2PY). These interfaces allow for seamless integration of Fortran Lapack code into programs written in other languages.

5. Is it necessary to have a strong background in mathematics to use Fortran Lapack?

While a strong understanding of linear algebra principles is helpful, it is not necessary to have a deep mathematical background to use Fortran Lapack. Many of the subroutines in Lapack are designed to handle complex calculations and can be used by scientists with varying levels of mathematical knowledge.

Similar threads

  • Programming and Computer Science
Replies
2
Views
2K
Replies
6
Views
1K
  • Programming and Computer Science
Replies
14
Views
4K
  • Programming and Computer Science
Replies
22
Views
4K
  • Programming and Computer Science
Replies
4
Views
3K
  • Programming and Computer Science
Replies
4
Views
6K
  • Programming and Computer Science
Replies
29
Views
2K
  • Programming and Computer Science
Replies
5
Views
986
  • Programming and Computer Science
Replies
13
Views
2K
  • Programming and Computer Science
Replies
1
Views
5K
Back
Top