Kristoffer Lindvall
Kristoffer Lindvall

Reputation: 141

Segmentation fault - invalid memory reference

Hey I am trying to get my LAPACK libraries to work and I have searched and searched but I can't seem to figure out what I am doing wrong.

I try running my code, and I get the following error

Program received signal SIGSEGV: Segmentation fault - invalid memory reference.

Backtrace for this error:
#0  0x7FFB23D405F7
#1  0x7FFB23D40C3E
#2  0x7FFB23692EAF
#3  0x401ED1 in sgesv_
#4  0x401D0B in MAIN__ at CFDtest.f03:? Segmentation fault (core dumped)

I will paste my main code here, hopefully someone can help me with this problem.

****************************************************
PROGRAM CFD_TEST

USE MY_LIB

IMPLICIT DOUBLE PRECISION (A-H,O-Z)

DIMENSION ET(0:10), VN(0:10), WT(0:10)

DIMENSION SO(0:10), FU(0:10), DMA(0:10,0:10)

DIMENSION DMA2(0:10,0:10), QN(0:10), WKSPCE(0:10)

INTEGER*8 :: pivot(10), inf

INTEGER*8 :: N

EXTERNAL SGESV

!SET THE PARAMETERS

SIGMA1 = 0.D0

SIGMA2 = 0.D0

TAU = 1.D0

EF  = 1.D0

EXP  = 2.71828182845904509D0

COST = EXP/(1.D0+EXP*EXP)

DO 1 N=2, 10

!COMPUATION OF THE NODES, WEIGHTS AND DERIVATIVE MATRIX

CALL ZELEGL(N,ET,VN)   

CALL WELEGL(N,ET,VN,WT)  

CALL DMLEGL(N,10,ET,VN,DMA)


!CONSTRUCTION OF THE MATRIX CORRESPONDING TO THE

!DIFFERENTIAL OPERATOR

DO 2 I=0, N

DO 2 J=0, N

SUM = 0.D0

DO 3 K=0, N

SUM = SUM + DMA(I,K)*DMA(K,J)

3 CONTINUE

OPER = -SUM

IF(I .EQ. J) OPER = -SUM + TAU

DMA2(I,J) = OPER

2 CONTINUE

!CHANGE OF THE ENTRIES OF THE MATRIX ACCORDING TO THE

!BOUNDARY CONDITIONS

DO 4 J=0, N

DMA2(0,J) = 0.D0

DMA2(N,J) = 0.D0


4 CONTINUE

DMA2(0,0) = 1.D0

DMA2(N,N) = 1.D0


!CONSTRUCTION OF THE RIGHT-HAND SIDE VECTOR

DO 5 I=1, N-1 

FU(I) = EF

5 CONTINUE

FU(0) = SIGMA1

FU(N) = SIGMA2

!SOLUTION OF THE LINEAR SYSTEM

N1 = N + 1

CALL SGESV(N,N,DMA2,pivot,FU,N,inf)

DO 6 I = 0, N

FU(I) = SO(I)

6 CONTINUE

PRINT *, pivot

1 CONTINUE

RETURN

END PROGRAM CFD_TEST

*****************************************************

The commands I run to compile are

gfortran -c MY_LIB.f03

gfortran -c CFDtest.f03

gfortran MY_LIB.o CFDtest.o -o CFDtest -L/usr/local/lib -llapack -lblas


I ran the command

-fbacktrace -g -Wall -Wextra CFDtest

CFDtest: In function _fini': (.fini+0x0): multiple definition of_fini' /usr/lib/gcc/x86_64-linux-gnu/4.9/../../../x86_64-linux-gnu/crti.o:/build/buildd/glibc-2.19/csu/../sysdeps/x86_64/crti.S:80: first defined here CFDtest: In function data_start': (.data+0x0): multiple definition ofdata_start' /usr/lib/gcc/x86_64-linux-gnu/4.9/../../../x86_64-linux-gnu/crt1.o:(.data+0x0): first defined here CFDtest: In function data_start': (.data+0x8): multiple definition of__dso_handle' /usr/lib/gcc/x86_64-linux-gnu/4.9/crtbegin.o:(.data+0x0): first defined here CFDtest:(.rodata+0x0): multiple definition of _IO_stdin_used' /usr/lib/gcc/x86_64-linux-gnu/4.9/../../../x86_64-linux-gnu/crt1.o:(.rodata.cst4+0x0): first defined here CFDtest: In function_start': (.text+0x0): multiple definition of _start' /usr/lib/gcc/x86_64-linux-gnu/4.9/../../../x86_64-linux-gnu/crt1.o:(.text+0x0): first defined here CFDtest: In function_init': (.init+0x0): multiple definition of _init' /usr/lib/gcc/x86_64-linux-gnu/4.9/../../../x86_64-linux-gnu/crti.o:/build/buildd/glibc-2.19/csu/../sysdeps/x86_64/crti.S:64: first defined here /usr/lib/gcc/x86_64-linux-gnu/4.9/crtend.o:(.tm_clone_table+0x0): multiple definition of__TMC_END' CFDtest:(.data+0x10): first defined here /usr/bin/ld: error in CFDtest(.eh_frame); no .eh_frame_hdr table will be created. collect2: error: ld returned 1 exit status

Upvotes: 1

Views: 7212

Answers (2)

MatCross
MatCross

Reputation: 389

You haven't posted your code for MY_LIB.f03 so we cannot compile CFDtest.f03 exactly as you have supplied it.

(As an aside, the usual naming convention is that f90 in a .f90 file is not supposed to imply the language version being targeted. Rather, .f90 denotes free format while .f is used for fixed format. By extension, your .f03 files would be better (i.e., more portable if) named as .f90.)

I commented out the USE MY_LIB line and ran your code through nagfor -u -c cfd_test.f90. The output, broken down, is

Extension: cfd_test.f90, line 13: Byte count on numeric data type
           detected at *@8
Extension: cfd_test.f90, line 15: Byte count on numeric data type
           detected at *@8

Byte counts are not portable. The kind value for an 8-byte integer is selected_int_kind(18). (Similarly you might like to use a kind(0.0d0) kind value for your double precision data.)

Error: cfd_test.f90, line 48: Implicit type for I
       detected at 2@I
Error: cfd_test.f90, line 50: Implicit type for J
       detected at 2@J
Error: cfd_test.f90, line 54: Implicit type for K
       detected at 3@K
Error: cfd_test.f90, line 100: Implicit type for N1
       detected at N1@=

You have these implicitly typed, which implies they are 4-byte (default) integers. You should probably declare these explicitly as 8-byte integers (using the 8-byte integer kind value above) if that's what you intend.

Questionable: cfd_test.f90, line 116: Variable COST set but never referenced
Questionable: cfd_test.f90, line 116: Variable N1 set but never referenced
Warning: cfd_test.f90, line 116: Unused local variable QN
Warning: cfd_test.f90, line 116: Unused local variable WKSPCE

You need to decide what you intend to do with these, or whether they are just deletable cruft.

With the implicit integers declared explicitly, there is further output

Warning: cfd_test.f90, line 116: Variable SO referenced but never set

This looks bad.

Obsolescent: cfd_test.f90, line 66: 2 is a shared DO termination label

Your DO loops would probably be better using the modern END DO terminators (not shared!)

Error: cfd_test.f90, line 114: RETURN is only allowed in SUBROUTINEs and FUNCTIONs

This is obviously easy to fix.

For the LAPACK call, one source of explicit interfaces for these routines is the NAG Fortran Library (through the nag_library module). Since your real data is not single precision, you should be using dgesv instead of sgesv. Adding USE nag_library, ONLY: dgesv and switching to call dgesv instead of sgesv, then recompiling as above, reveals

Incorrect data type INTEGER(KIND=4) (expected INTEGER) for argument N (no. 1) of DGESV

so you should indeed be using default (4-byte integers) - at least for the LAPACK build on your system, which will almost certainly be using 4-byte integers. Thus you might want to forget all about kinding your integers and just use the default integer type for all. Correcting this gives

Array supplied for scalar argument LDA (no. 4) of DGESV

so you do need to add this argument. Maybe pass size(DMA2,1)?

With this argument added to the call the code compiles successfully, but without the definitions for your *LEGL functions I couldn't go through any run-time testing.

Here is my modified (and pretty-printed) version of your program

Program cfd_test

! Use my_lib
! Use nag_library, Only: dgesv
  Implicit None
  Integer, Parameter :: wp = kind(0.0D0)
  Real (Kind=wp) :: ef, oper, sigma1, sigma2, tau
  Integer :: i, inf, j, k, n, sum
  Real (Kind=wp) :: dma(0:10, 0:10), dma2(0:10, 0:10), et(0:10), fu(0:10), &
    so(0:10), vn(0:10), wt(0:10)
  Integer :: pivot(10)
  External :: dgesv, dmlegl, welegl, zelegl
  Intrinsic :: kind, size

! SET THE PARAMETERS

  sigma1 = 0._wp

  sigma2 = 0._wp

  tau = 1._wp

  ef = 1._wp

  Do n = 2, 10

!   COMPUATION OF THE NODES, WEIGHTS AND DERIVATIVE MATRIX

    Call zelegl(n, et, vn)

    Call welegl(n, et, vn, wt)

    Call dmlegl(n, 10, et, vn, dma)


!   CONSTRUCTION OF THE MATRIX CORRESPONDING TO THE

!   DIFFERENTIAL OPERATOR

    Do i = 0, n

      Do j = 0, n

        sum = 0._wp

        Do k = 0, n

          sum = sum + dma(i, k)*dma(k, j)

        End Do

        oper = -sum

        If (i==j) oper = -sum + tau

        dma2(i, j) = oper

      End Do

    End Do

!   CHANGE OF THE ENTRIES OF THE MATRIX ACCORDING TO THE

!   BOUNDARY CONDITIONS

    Do j = 0, n

      dma2(0, j) = 0._wp

      dma2(n, j) = 0._wp


    End Do

    dma2(0, 0) = 1._wp

    dma2(n, n) = 1._wp


!   CONSTRUCTION OF THE RIGHT-HAND SIDE VECTOR

    Do i = 1, n - 1

      fu(i) = ef

    End Do

    fu(0) = sigma1

    fu(n) = sigma2

!   SOLUTION OF THE LINEAR SYSTEM

    Call dgesv(n, n, dma2, size(dma2,1), pivot, fu, n, inf)

    Do i = 0, n

      fu(i) = so(i)

    End Do

    Print *, pivot

  End Do

End Program

In general your development experience will be the most pleasant if you use as good a checking compiler as you can get your hands on and if you make sure you ask it to diagnose as much as it can for you.

Upvotes: 3

albapa
albapa

Reputation: 261

As far as I can tell, there could be a number of problems:

  • Your integers with INTEGER*8 might be too long, maybe INTEGER*4 or simply INTEGER would be better
  • You call SGESV on double arguments instead of DGESV
  • Your LDA argument is missing, so your code should perhaps look like CALL DGESV(N,N,DMA2,N,pivot,FU,N,inf) but you need to check whether this is what you want.

Upvotes: 1

Related Questions