1

I am having an issue with a Fortran program where the program is written to evaluate local density of sample data. In my original program, n_grid is actually calculated using L and ds values. I have tried changing the values of the 'ds' and 'n_grid', and at the end concluded that when the value of 'n_grid' is 20 or greater, the program runs perfectly but if 'n_grid' is 19 or less, the program runs upto line 67 but if i try to write the data to an output file at line 69, it crashes and gives a "malloc(): corrupted top size" error. How can I fix this issue and what could be causing it?

program localdensity

implicit none

    integer :: i , j , k , a , b , n_grid , n , nc , p , q , r , ncin1
    double precision ::  L , ds  , ignore 
    double precision, allocatable :: dn(:,:,:)                               
    double precision, allocatable :: rco(:,:)             
      
    ! INPUT PARAMETERS !

    ncin1 = 14                        ! total configurations in each block file
    nc =  ncin1*1                     ! total block files
    ds = 3d0                          ! dx = dy = dz = 2 A 

    ! INPUT PARAMETERS !

    print*, 'started reading data file'
    open (1, file = 'Silica_13824_negative_press-11_100.txt', status = 'old')  
  
    read(1,*) n ,  ignore  ,  L                         
  
    close(1) 
    
    print* ,  n , L
    L = 59.99999d0
    !L = 60.0001d0

    allocate(rco(3,nc*n))
    n_grid = 19
    allocate(dn(n_grid , n_grid , n_grid))

    open (1, file = 'Silica_13824_negative_press-11_100.txt', status = 'old')  

    do j = 1 , nc

        read(1,*)                            
        do i=1,n
            k=i+(j-1)*n
            read(1,*)  a , b , rco(1,k), rco(2,k), rco(3,k) ! reading data (x,y,z)
        end do
    end do
    print*, 'ended reading data file n :' , n , L
    close(1)  
   
    rco(:,:) = rco(:,:) - L*floor(rco(:,:)/L) ! Appying Periodic Boundary condition

    dn = 0   
    print* 

    do i = 1,nc
        do j = 1,n

            p = floor( rco(1 , (i-1)*n + j) / ds) + 1
            q = floor( rco(2 , (i-1)*n + j) / ds) + 1
            r = floor( rco(3 , (i-1)*n + j) / ds) + 1
            dn(p,q,r) = dn(p,q,r)  + 1 
         
        end do  
    if (mod(i,ncin1)==0) print*, 'block', i/ncin1, 'done'
    end do

    print*, 'total particles counted :', sum(dn) ,'; n*nc = ', n*nc
    print*, 'nc is ' ,  nc   
    dn = dn/nc
    print*, sum(dn)
    print*,'n_grid' ,n_grid

    open (unit = 2, file = "parameters_neg.txt" , action="write" , status="replace")
        write(2,*) n , nc , L , n_grid
    close(2)

    open (unit = 3, file = "data_from_program_neg.txt" , action="write" , status="replace")
             
    do i = 1 , n_grid
        do j = 1 , n_grid
            do k = 1 , n_grid   
         
                write(3,*) i , j , k , dn(i,j,k)  
         
            end do
        end do                          
    end do  

    close(3)
    
    print*, 'Program has computed everything, data has been saved to data_from_program.txt. Now run file named l ocal_density.py '

endprogram localdensity

When i run it :

 started reading data file
       13824   57.988311117244002     
 ended reading data file n :       13824   59.999989999999997     

 block           1 done
 total particles counted :   183355.00000000000      ; n*nc =       193536
 nc is           14
   13096.785714285699     
 n_grid          19
malloc(): corrupted top size

Program received signal SIGABRT: Process abort signal.

Backtrace for this error:
#0  0x7f7360b16d21 in ???
#1  0x7f7360b15ef5 in ???
#2  0x7f736094708f in ???
    at /build/glibc-SzIz7B/glibc-2.31/signal/../sysdeps/unix/sysv/linux/x86_64/sigaction.c:0
#3  0x7f736094700b in __GI_raise
    at ../sysdeps/unix/sysv/linux/raise.c:51
#4  0x7f7360926858 in __GI_abort
    at /build/glibc-SzIz7B/glibc-2.31/stdlib/abort.c:79
#5  0x7f736099126d in __libc_message
    at ../sysdeps/posix/libc_fatal.c:155
#6  0x7f73609992fb in malloc_printerr
    at /build/glibc-SzIz7B/glibc-2.31/malloc/malloc.c:5347
#7  0x7f736099c6b9 in _int_malloc
    at /build/glibc-SzIz7B/glibc-2.31/malloc/malloc.c:4107
#8  0x7f736099e153 in __GI___libc_malloc
    at /build/glibc-SzIz7B/glibc-2.31/malloc/malloc.c:3058
#9  0x7f7360b161d8 in ???
#10  0x7f7360d61df4 in ???
#11  0x7f7360d57300 in ???
#12  0x7f7360d57ccc in ???
#13  0x561eddba1a54 in ???
#14  0x561eddba1e89 in ???
#15  0x7f7360928082 in __libc_start_main
    at ../csu/libc-start.c:308
#16  0x561eddba017d in ???
#17  0xffffffffffffffff in ???
Aborted (core dumped)

Integer :: i , k
Integer , parameter :: Br_sn_cvo = 10
Integer , parameter
  • 1
    Have you compiled with all runtime error checks enabled? Note that without the input it is impossible for anyone else to run your program to explore in detail. (See [mre]). – francescalus Jan 14 '23 at 12:27
  • Have you checked that p, q and r are within bounds (1-ngrid) before you use dn(p,q,r)? – lastchance Jan 14 '23 at 12:46
  • @francescalus I did compiled using the command gfortran -C local_den.f90 and it shows the same output as it shows above. Are you referring to this check only or something else? – Mayank Sharma Jan 14 '23 at 13:38
  • gfortran's `-C` has little to do with runtime checks. See [this other question](https://stackoverflow.com/q/3676322/3157076) and its answers. – francescalus Jan 14 '23 at 13:42
  • 1
    Try -fcheck=all – lastchance Jan 14 '23 at 13:45
  • To be exact -C has *nothing* to do with runtime checks for gfortran, have a look at `man gfortran` – Ian Bush Jan 14 '23 at 13:52
  • Dear @lastchance , I think you're right, I made a silly mistake that you pointed out. When I increased the size of dn by 1 in each dimension, the code actually worked. There were some rco values that were in those extreme indices. I am really sorry for posting this without thinking about this aspect. Thank you very much. Also as a newbie to this site, do i need to close this question or anything else? – Mayank Sharma Jan 14 '23 at 13:53
  • Also, as I thought about it, the code shouldn't have worked even for the case where n_grid > 20. When I used the suffix '-fcheck=all', it does show the error, which it does not show when it's not used. Again, thank you everyone for helping me out. – Mayank Sharma Jan 14 '23 at 14:18
  • I've marked this question as a duplicate of the one about debugging compiler flags, because the error in this particular code is shown immediately when using such options. It's possible, however, that an [edit] to the question making it much more general may be useful to others and form the basis of an introduction to such debugging efforts. – francescalus Jan 14 '23 at 14:32
  • Being a duplicate doesn't mean that the question is bad or wrong, so don't feel you should delete it. As a specific example of applying the debug flags, readers of the linked question (which links back here) can be motivated by it. – francescalus Jan 14 '23 at 14:34

0 Answers0